Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 24 | |
Jack Jansen | bf05d4c | 1996-08-19 15:11:45 +0000 | [diff] [blame] | 25 | #ifdef __CFM68K__ |
| 26 | /* cfm68k InterfaceLib exports GetEventQueue, but Events.h doesn't know this |
| 27 | ** and defines it as GetEvQHdr (which is correct for PPC). This fix is for |
| 28 | ** CW9, check that the workaround is still needed for the next release. |
| 29 | */ |
| 30 | #define GetEvQHdr GetEventQueue |
| 31 | #endif /* __CFM68K__ */ |
| 32 | |
| 33 | #include <Events.h> |
| 34 | |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 35 | #ifdef TARGET_API_MAC_CARBON |
| 36 | /* Unfortunately this call is probably slower... */ |
| 37 | #define LMGetTicks() TickCount() |
| 38 | #endif |
| 39 | |
Jack Jansen | bf05d4c | 1996-08-19 15:11:45 +0000 | [diff] [blame] | 40 | #ifdef __CFM68K__ |
| 41 | #undef GetEventQueue |
| 42 | #endif /* __CFM68K__ */ |
| 43 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 44 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 45 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 46 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 47 | #include "marshal.h" |
| 48 | #include "import.h" |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 49 | #include "importdl.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 50 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 51 | #include "pythonresources.h" |
| 52 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 53 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 54 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 55 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 56 | #include <Resources.h> |
| 57 | #include <Memory.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 58 | #include <Windows.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 59 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 60 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 61 | #include <Fonts.h> |
| 62 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 63 | #include <TextUtils.h> |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 64 | #ifdef __MWERKS__ |
| 65 | #include <SIOUX.h> |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 66 | extern void SIOUXSetupMenus(void); |
| 67 | extern void SIOUXDoAboutBox(void); |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 68 | #endif |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 69 | #ifdef USE_GUSI |
| 70 | /* Functions we redefine because they're in obscure libraries */ |
| 71 | extern void SpinCursor(short x); |
| 72 | extern void RotateCursor(short x); |
| 73 | extern pascal void PLstrcpy(unsigned char *, unsigned char *); |
| 74 | extern pascal int PLstrcmp(unsigned char *, unsigned char *); |
| 75 | extern pascal unsigned char *PLstrrchr(unsigned char *, unsigned char); |
| 76 | |
| 77 | #endif |
| 78 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 79 | #ifdef USE_GUSI1 |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 80 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 81 | #include <GUSI.h> |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 82 | #endif |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 83 | #include <LowMem.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 84 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 85 | /* The ID of the Sioux apple menu */ |
| 86 | #define SIOUX_APPLEID 32000 |
| 87 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 88 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 89 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 90 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 91 | /* |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 92 | ** When less than this amount of stackspace is left we |
| 93 | ** raise a MemoryError. |
| 94 | */ |
| 95 | #ifndef MINIMUM_STACK_SIZE |
| 96 | #ifdef __powerc |
| 97 | #define MINIMUM_STACK_SIZE 8192 |
| 98 | #else |
| 99 | #define MINIMUM_STACK_SIZE 4096 |
| 100 | #endif |
| 101 | #endif |
| 102 | |
| 103 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 104 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 105 | ** things like updates (and they'll keep coming back if we don't |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 106 | ** handle them). Note that we don't know who has windows open, so |
| 107 | ** even handing updates off to SIOUX under MW isn't going to work. |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 108 | */ |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 109 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 110 | |
| 111 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 113 | /* XXX We should include Errors.h here, but it has a name conflict |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 114 | ** with the python errors.h. */ |
| 115 | #define fnfErr -43 |
| 116 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 117 | /* Declared in macfsmodule.c: */ |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 118 | extern FSSpec *mfs_GetFSSpecFSSpec(PyObject *); |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 119 | extern PyObject *newmfssobject(FSSpec *); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 120 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 121 | /* Interrupt code variables: */ |
| 122 | static int interrupted; /* Set to true when cmd-. seen */ |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 123 | static RETSIGTYPE intcatcher(int); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 124 | |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 125 | static int PyMac_Yield(void); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 126 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 127 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 128 | ** These are the real scheduling parameters that control what we check |
| 129 | ** in the event loop, and how often we check. The values are initialized |
| 130 | ** from pyMac_SchedParamStruct. |
| 131 | */ |
| 132 | |
| 133 | struct real_sched_param_struct { |
| 134 | int check_interrupt; /* if true check for command-dot */ |
| 135 | int process_events; /* if nonzero enable evt processing, this mask */ |
| 136 | int besocial; /* if nonzero be a little social with CPU */ |
| 137 | unsigned long check_interval; /* how often to check, in ticks */ |
| 138 | unsigned long bg_yield; /* yield so long when in background */ |
| 139 | /* these are computed from previous and clock and such */ |
| 140 | int enabled; /* check_interrupt OR process_event OR yield */ |
| 141 | unsigned long next_check; /* when to check/yield next, in ticks */ |
| 142 | }; |
| 143 | |
| 144 | static struct real_sched_param_struct schedparams = |
| 145 | { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0}; |
| 146 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 147 | /* |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 148 | ** Workaround for sioux/gusi combo: set when we are exiting |
| 149 | */ |
| 150 | int PyMac_ConsoleIsDead; |
| 151 | |
| 152 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 153 | ** Sioux menu bar, saved early so we can restore it |
| 154 | */ |
| 155 | static Handle sioux_mbar; |
| 156 | |
| 157 | /* |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 158 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 159 | */ |
| 160 | struct hook_args { |
| 161 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 162 | char *prompt; /* The prompt */ |
| 163 | }; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 164 | #ifdef TARGET_API_MAC_CARBON |
| 165 | /* The StandardFile hooks don't exist in Carbon. This breaks GetDirectory, |
| 166 | ** but the macfsn code will replace it by a NavServices version anyway. |
| 167 | */ |
| 168 | #define myhook_upp NULL |
| 169 | #else |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 170 | static DlgHookYDUPP myhook_upp; |
| 171 | static int upp_inited = 0; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 172 | #endif |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 173 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 174 | /* |
| 175 | ** The python-code event handler |
| 176 | */ |
| 177 | static PyObject *python_event_handler; |
| 178 | |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 179 | /* |
| 180 | ** Set to true if we're appearance-compliant |
| 181 | */ |
| 182 | int PyMac_AppearanceCompliant; |
| 183 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 184 | #ifdef USE_GUSI1 |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 185 | /* |
| 186 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 187 | ** the working directory. Hence, we call this routine after each call |
| 188 | ** to chdir() to rectify things. |
| 189 | */ |
| 190 | void |
| 191 | PyMac_FixGUSIcd() |
| 192 | { |
| 193 | WDPBRec pb; |
| 194 | FSSpec curdirfss; |
| 195 | |
| 196 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 197 | return; |
| 198 | |
| 199 | /* Set MacOS "working directory" */ |
| 200 | pb.ioNamePtr= "\p"; |
| 201 | pb.ioVRefNum= curdirfss.vRefNum; |
| 202 | pb.ioWDDirID= curdirfss.parID; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 203 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 204 | return; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 205 | } |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 206 | #endif |
Jack Jansen | 7ac70af | 1996-08-19 11:01:05 +0000 | [diff] [blame] | 207 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 208 | #ifdef USE_GUSI |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 209 | /* |
| 210 | ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we |
| 211 | ** provide a dummy here. |
| 212 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 213 | void SpinCursor(short x) { /* Dummy */ } |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 214 | void RotateCursor(short x) { /* Dummy */ } |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 215 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 216 | /* |
| 217 | ** Replacement GUSI Spin function |
| 218 | */ |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 219 | #ifdef USE_GUSI1 |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 220 | static int |
| 221 | PyMac_GUSISpin(spin_msg msg, long arg) |
| 222 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 223 | static Boolean inForeground = true; |
| 224 | int maxsleep = 6; /* 6 ticks is "normal" sleeptime */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 225 | |
| 226 | if (PyMac_ConsoleIsDead) return 0; |
| 227 | #if 0 |
| 228 | if (inForeground) |
| 229 | SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1); |
| 230 | #endif |
| 231 | |
| 232 | if (interrupted) return -1; |
| 233 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 234 | if ( msg == SP_AUTO_SPIN ) |
| 235 | maxsleep = 0; |
| 236 | if ( msg==SP_SLEEP||msg==SP_SELECT ) |
| 237 | maxsleep = arg; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 238 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 239 | PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | PyMac_SetGUSISpin() { |
| 246 | GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin); |
| 247 | } |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 248 | #endif |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 249 | |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 250 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 251 | void |
| 252 | PyMac_StopGUSISpin() { |
| 253 | PyMac_ConsoleIsDead = 1; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | ** Replacement routines for the PLstr... functions so we don't need |
| 258 | ** StdCLib. Moreover, that implementation is broken under cfm68k... |
| 259 | */ |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 260 | pascal void |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 261 | PLstrcpy(unsigned char *to, unsigned char *fr) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 262 | { |
| 263 | memcpy(to, fr, fr[0]+1); |
| 264 | } |
| 265 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 266 | pascal int |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 267 | PLstrcmp(unsigned char *s1, unsigned char *s2) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 268 | { |
| 269 | int res; |
| 270 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 271 | |
| 272 | res = memcmp(s1+1, s2+1, l); |
| 273 | if ( res != 0 ) |
| 274 | return res; |
| 275 | |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 276 | if ( s1[0] < s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 277 | return -1; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 278 | else if ( s1[0] > s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 279 | return 1; |
| 280 | else |
| 281 | return 0; |
| 282 | } |
| 283 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 284 | pascal unsigned char * |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 285 | PLstrrchr(unsigned char *str, unsigned char chr) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 286 | { |
| 287 | unsigned char *ptr = 0; |
| 288 | unsigned char *p; |
| 289 | |
| 290 | for(p=str+1; p<str+str[0]; p++) |
| 291 | if ( *p == chr ) |
| 292 | ptr = p; |
| 293 | return ptr; |
| 294 | } |
| 295 | |
| 296 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 297 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 298 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 299 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 300 | unsigned char * |
| 301 | Pstring(char *str) |
| 302 | { |
| 303 | static Str255 buf; |
| 304 | int len; |
| 305 | |
| 306 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 307 | if (len > 255) |
| 308 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 309 | buf[0] = (unsigned char)len; |
| 310 | strncpy((char *)buf+1, str, len); |
| 311 | return buf; |
| 312 | } |
| 313 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 314 | /* Like strerror() but for Mac OS error numbers */ |
| 315 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 316 | { |
| 317 | static char buf[256]; |
| 318 | Handle h; |
| 319 | char *str; |
| 320 | |
| 321 | h = GetResource('Estr', err); |
| 322 | if ( h ) { |
| 323 | HLock(h); |
| 324 | str = (char *)*h; |
| 325 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 326 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 327 | HUnlock(h); |
| 328 | ReleaseResource(h); |
| 329 | } else { |
| 330 | sprintf(buf, "Mac OS error code %d", err); |
| 331 | } |
| 332 | return buf; |
| 333 | } |
| 334 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 335 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 336 | PyObject *PyMac_OSErrException; |
| 337 | |
| 338 | /* Initialize and return PyMac_OSErrException */ |
| 339 | PyObject * |
| 340 | PyMac_GetOSErrException() |
| 341 | { |
| 342 | if (PyMac_OSErrException == NULL) |
| 343 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 344 | return PyMac_OSErrException; |
| 345 | } |
| 346 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 347 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 348 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 349 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 350 | { |
| 351 | char *msg; |
| 352 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 353 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 354 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 355 | Py_INCREF(Py_None); |
| 356 | return Py_None; |
| 357 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 358 | if (err == -1 && PyErr_Occurred()) |
| 359 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 360 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 361 | v = Py_BuildValue("(is)", err, msg); |
| 362 | PyErr_SetObject(eobj, v); |
| 363 | Py_DECREF(v); |
| 364 | return NULL; |
| 365 | } |
| 366 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 367 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 368 | PyObject * |
| 369 | PyMac_Error(OSErr err) |
| 370 | { |
| 371 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 372 | } |
| 373 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 374 | #ifdef USE_STACKCHECK |
| 375 | /* Check for stack overflow */ |
| 376 | int |
| 377 | PyOS_CheckStack() |
| 378 | { |
| 379 | long left; |
| 380 | |
| 381 | left = StackSpace(); |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 382 | if ( left < MINIMUM_STACK_SIZE ) |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 383 | return -1; |
| 384 | return 0; |
| 385 | } |
| 386 | #endif /* USE_STACKCHECK */ |
| 387 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 388 | /* The catcher routine (which may not be used for all compilers) */ |
| 389 | static RETSIGTYPE |
| 390 | intcatcher(sig) |
| 391 | int sig; |
| 392 | { |
| 393 | interrupted = 1; |
| 394 | signal(SIGINT, intcatcher); |
| 395 | } |
| 396 | |
| 397 | void |
| 398 | PyOS_InitInterrupts() |
| 399 | { |
| 400 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 401 | signal(SIGINT, intcatcher); |
| 402 | } |
| 403 | |
Jack Jansen | a8441de | 1997-08-08 14:57:37 +0000 | [diff] [blame] | 404 | void |
| 405 | PyOS_FiniInterrupts() |
| 406 | { |
| 407 | } |
| 408 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 409 | /* |
| 410 | ** This routine scans the event queue looking for cmd-. |
| 411 | ** This is the only way to get an interrupt under THINK (since it |
| 412 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 413 | ** the full-fledged event loop is disabled. This way, we can at least |
| 414 | ** interrupt a runaway python program. |
| 415 | */ |
| 416 | static void |
| 417 | scan_event_queue(flush) |
| 418 | int flush; |
| 419 | { |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 420 | #ifdef TARGET_API_MAC_CARBON |
| 421 | /* CARBONXXXX To be implemented */ |
| 422 | return; |
| 423 | #else |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 424 | register EvQElPtr q; |
| 425 | |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 426 | q = (EvQElPtr) LMGetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 427 | |
| 428 | for (; q; q = (EvQElPtr)q->qLink) { |
| 429 | if (q->evtQWhat == keyDown && |
| 430 | (char)q->evtQMessage == '.' && |
| 431 | (q->evtQModifiers & cmdKey) != 0) { |
| 432 | if ( flush ) |
| 433 | FlushEvents(keyDownMask, 0); |
| 434 | interrupted = 1; |
| 435 | break; |
| 436 | } |
| 437 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 438 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | int |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 442 | PyErr_CheckSignals() |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 443 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 444 | if (schedparams.enabled) { |
| 445 | if ( (unsigned long)LMGetTicks() > schedparams.next_check ) { |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 446 | if ( PyMac_Yield() < 0) |
| 447 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 448 | schedparams.next_check = (unsigned long)LMGetTicks() |
| 449 | + schedparams.check_interval; |
| 450 | if (interrupted) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 451 | scan_event_queue(1); /* Eat events up to cmd-. */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 452 | interrupted = 0; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 453 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 454 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 457 | } |
| 458 | return 0; |
| 459 | } |
| 460 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 461 | int |
| 462 | PyOS_InterruptOccurred() |
| 463 | { |
| 464 | scan_event_queue(1); |
| 465 | return interrupted; |
| 466 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 467 | /* Check whether we are in the foreground */ |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 468 | static int |
| 469 | PyMac_InForeground(void) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 470 | { |
| 471 | static ProcessSerialNumber ours; |
| 472 | static inited; |
| 473 | ProcessSerialNumber curfg; |
| 474 | Boolean eq; |
| 475 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 476 | if ( inited == 0 ) { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 477 | (void)GetCurrentProcess(&ours); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 478 | inited = 1; |
| 479 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 480 | if ( GetFrontProcess(&curfg) < 0 ) |
| 481 | eq = 1; |
| 482 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 483 | eq = 1; |
| 484 | return (int)eq; |
| 485 | |
| 486 | } |
| 487 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 488 | int |
| 489 | PyMac_SetEventHandler(PyObject *evh) |
| 490 | { |
| 491 | if ( evh && python_event_handler ) { |
| 492 | PyErr_SetString(PyExc_RuntimeError, "Python event handler already set"); |
| 493 | return 0; |
| 494 | } |
| 495 | if ( python_event_handler ) |
| 496 | Py_DECREF(python_event_handler); |
| 497 | if ( evh ) |
| 498 | Py_INCREF(evh); |
| 499 | python_event_handler = evh; |
| 500 | return 1; |
| 501 | } |
| 502 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 503 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 504 | ** Handle an event, either one found in the mainloop eventhandler or |
| 505 | ** one passed back from the python program. |
| 506 | */ |
| 507 | void |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 508 | PyMac_HandleEventIntern(evp) |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 509 | EventRecord *evp; |
| 510 | { |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 511 | #ifndef TARGET_API_MAC_CARBON |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 512 | if ( evp->what == mouseDown ) { |
| 513 | WindowPtr wp; |
| 514 | |
| 515 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
| 516 | SystemClick(evp, wp); |
| 517 | return; |
| 518 | } |
| 519 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 520 | #endif |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 521 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 522 | { |
| 523 | int siouxdidit; |
| 524 | |
| 525 | /* If SIOUX wants it we're done */ |
| 526 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 527 | if ( siouxdidit ) |
| 528 | return; |
| 529 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 530 | #else |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 531 | /* Other compilers are just unlucky... */ |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 532 | #endif /* !__MWERKS__ */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /* |
| 536 | ** Handle an event, either through HandleEvent or by passing it to the Python |
| 537 | ** event handler. |
| 538 | */ |
| 539 | int |
| 540 | PyMac_HandleEvent(evp) |
| 541 | EventRecord *evp; |
| 542 | { |
| 543 | PyObject *rv; |
| 544 | |
| 545 | if ( python_event_handler ) { |
| 546 | rv = PyObject_CallFunction(python_event_handler, "(O&)", |
| 547 | PyMac_BuildEventRecord, evp); |
| 548 | if ( rv ) |
| 549 | Py_DECREF(rv); |
| 550 | else |
| 551 | return -1; /* Propagate exception */ |
| 552 | } else { |
| 553 | PyMac_HandleEventIntern(evp); |
| 554 | } |
| 555 | return 0; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 559 | ** Yield the CPU to other tasks without processing events. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 560 | */ |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 561 | int |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 562 | PyMac_DoYield(int maxsleep, int maycallpython) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 563 | { |
| 564 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 565 | int gotone; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 566 | long latest_time_ready; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 567 | static int in_here = 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 568 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 569 | in_here++; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 570 | /* |
| 571 | ** First check for interrupts, if wanted. |
| 572 | ** This sets a flag that will be picked up at an appropriate |
| 573 | ** moment in the mainloop. |
| 574 | */ |
| 575 | if (schedparams.check_interrupt) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 576 | scan_event_queue(0); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 577 | |
| 578 | /* XXXX Implementing an idle routine goes here */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 579 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 580 | /* |
| 581 | ** Check which of the eventloop cases we have: |
| 582 | ** - process events |
| 583 | ** - don't process events but do yield |
| 584 | ** - do neither |
| 585 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 586 | if( in_here > 1 || !schedparams.process_events || |
| 587 | (python_event_handler && !maycallpython) ) { |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 588 | #ifndef TARGET_API_MAC_CARBON |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 589 | if ( maxsleep >= 0 ) { |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 590 | SystemTask(); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 591 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 592 | #endif |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 593 | } else { |
| 594 | latest_time_ready = LMGetTicks() + maxsleep; |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 595 | do { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 596 | /* XXXX Hack by Jack. |
| 597 | ** In time.sleep() you can click to another application |
| 598 | ** once only. If you come back to Python you cannot get away |
| 599 | ** again. |
| 600 | **/ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 601 | gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 602 | /* Get out quickly if nothing interesting is happening */ |
| 603 | if ( !gotone || ev.what == nullEvent ) |
| 604 | break; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 605 | if ( PyMac_HandleEvent(&ev) < 0 ) { |
| 606 | in_here--; |
| 607 | return -1; |
| 608 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 609 | maxsleep = latest_time_ready - LMGetTicks(); |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 610 | } while ( maxsleep > 0 ); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 611 | } |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 612 | in_here--; |
| 613 | return 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 617 | ** Process events and/or yield the CPU to other tasks if opportune |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 618 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 619 | int |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 620 | PyMac_Yield() { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 621 | unsigned long maxsleep; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 622 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 623 | if( PyMac_InForeground() ) |
| 624 | maxsleep = 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 625 | else |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 626 | maxsleep = schedparams.bg_yield; |
| 627 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 628 | return PyMac_DoYield(maxsleep, 1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 632 | ** Return current scheduler parameters |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 633 | */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 634 | void |
| 635 | PyMac_GetSchedParams(PyMacSchedParams *sp) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 636 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 637 | sp->check_interrupt = schedparams.check_interrupt; |
| 638 | sp->process_events = schedparams.process_events; |
| 639 | sp->besocial = schedparams.besocial; |
| 640 | sp->check_interval = schedparams.check_interval / 60.0; |
| 641 | sp->bg_yield = schedparams.bg_yield / 60.0; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 642 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 643 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 644 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 645 | ** Set current scheduler parameters |
| 646 | */ |
| 647 | void |
| 648 | PyMac_SetSchedParams(PyMacSchedParams *sp) |
| 649 | { |
| 650 | schedparams.check_interrupt = sp->check_interrupt; |
| 651 | schedparams.process_events = sp->process_events; |
| 652 | schedparams.besocial = sp->besocial; |
| 653 | schedparams.check_interval = (unsigned long)(sp->check_interval*60); |
| 654 | schedparams.bg_yield = (unsigned long)(sp->bg_yield*60); |
| 655 | if ( schedparams.check_interrupt || schedparams.process_events || |
| 656 | schedparams.besocial ) |
| 657 | schedparams.enabled = 1; |
| 658 | else |
| 659 | schedparams.enabled = 0; |
| 660 | schedparams.next_check = 0; /* Check immedeately */ |
| 661 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 662 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 663 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 664 | ** Install our menu bar. |
| 665 | */ |
| 666 | void |
| 667 | PyMac_InitMenuBar() |
| 668 | { |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 669 | MenuHandle applemenu; |
| 670 | |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 671 | if ( (sioux_mbar=GetMenuBar()) == NULL ) { |
| 672 | /* Sioux menu not installed yet. Do so */ |
| 673 | SIOUXSetupMenus(); |
| 674 | if ( (sioux_mbar=GetMenuBar()) == NULL ) |
| 675 | return; |
| 676 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 677 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 678 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 679 | } |
| 680 | |
| 681 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 682 | ** Restore sioux menu bar |
| 683 | */ |
| 684 | void |
| 685 | PyMac_RestoreMenuBar() |
| 686 | { |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 687 | if ( sioux_mbar ) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 688 | SetMenuBar(sioux_mbar); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 689 | DrawMenuBar(); |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 690 | } else |
| 691 | PyMac_InitMenuBar(); |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | |
| 695 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 696 | ** Our replacement about box |
| 697 | */ |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 698 | |
| 699 | #include "patchlevel.h" |
| 700 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 701 | void |
| 702 | SIOUXDoAboutBox(void) |
| 703 | { |
| 704 | DialogPtr theDialog; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 705 | WindowPtr theWindow; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 706 | short item; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 707 | short fontID; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 708 | |
| 709 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 710 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 711 | theWindow = GetDialogWindow(theDialog); |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 712 | SetPortWindowPort(theWindow); |
| 713 | GetFNum("\pPython-Sans", &fontID); |
| 714 | if (fontID == 0) |
| 715 | fontID = kFontIDGeneva; |
| 716 | TextFont(fontID); |
| 717 | TextSize(9); |
| 718 | ParamText(Pstring(PATCHLEVEL), "\p", "\p", "\p"); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 719 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 720 | ModalDialog(NULL, &item); |
| 721 | DisposeDialog(theDialog); |
| 722 | } |
| 723 | |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 724 | #if 0 |
| 725 | int |
| 726 | PyMac_FileExists(char *name) |
| 727 | { |
| 728 | FSSpec fss; |
| 729 | |
| 730 | if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr ) |
| 731 | return 1; |
| 732 | return 0; |
| 733 | } |
| 734 | #endif |
| 735 | |
| 736 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 737 | ** Helper routine for GetDirectory |
| 738 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 739 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 740 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 741 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 742 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 743 | Handle prompth; |
| 744 | short type; |
| 745 | Rect rect; |
| 746 | |
| 747 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 748 | if ( prompth ) |
| 749 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 750 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 751 | if ( item == SELECTCUR_ITEM ) { |
| 752 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 753 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 754 | } |
| 755 | return item; |
| 756 | } |
Jack Jansen | bb7e213 | 2000-07-14 22:15:24 +0000 | [diff] [blame] | 757 | #ifndef TARGET_API_MAC_CARBON |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 758 | /* |
| 759 | ** Ask the user for a directory. I still can't understand |
| 760 | ** why Apple doesn't provide a standard solution for this... |
| 761 | */ |
| 762 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 763 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 764 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 765 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 766 | { |
| 767 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 768 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 769 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 770 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 771 | |
| 772 | if ( !upp_inited ) { |
| 773 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 774 | upp_inited = 1; |
| 775 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 776 | if ( prompt && *prompt ) |
| 777 | hook_args.prompt = (char *)Pstring(prompt); |
| 778 | else |
| 779 | hook_args.prompt = NULL; |
| 780 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 781 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 782 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 783 | |
| 784 | reply.sfFile.name[0] = 0; |
| 785 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 786 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 787 | return hook_args.selectcur_hit; |
| 788 | } |
| 789 | |
| 790 | /* |
| 791 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 792 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 793 | StandardFileReply *reply, char *prompt) |
| 794 | { |
| 795 | static Point where = {-1, -1}; |
| 796 | struct hook_args hook_args; |
| 797 | |
| 798 | if ( !upp_inited ) { |
| 799 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 800 | upp_inited = 1; |
| 801 | } |
| 802 | if ( prompt && *prompt ) |
| 803 | hook_args.prompt = (char *)Pstring(prompt); |
| 804 | else |
| 805 | hook_args.prompt = NULL; |
| 806 | hook_args.selectcur_hit = 0; |
| 807 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 808 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 809 | } |
Jack Jansen | bb7e213 | 2000-07-14 22:15:24 +0000 | [diff] [blame] | 810 | #endif /* TARGET_API_MAC_CARBON */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 811 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 812 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 813 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 814 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 815 | { |
| 816 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 817 | PyErr_SetString(PyExc_TypeError, |
| 818 | "OSType arg must be string of 4 chars"); |
| 819 | return 0; |
| 820 | } |
| 821 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 822 | return 1; |
| 823 | } |
| 824 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 825 | /* Convert an OSType value to a 4-char string object */ |
| 826 | PyObject * |
| 827 | PyMac_BuildOSType(OSType t) |
| 828 | { |
| 829 | return PyString_FromStringAndSize((char *)&t, 4); |
| 830 | } |
| 831 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 832 | /* Convert an NumVersion value to a 4-element tuple */ |
| 833 | PyObject * |
| 834 | PyMac_BuildNumVersion(NumVersion t) |
| 835 | { |
| 836 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 837 | } |
| 838 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 839 | |
| 840 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 841 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 842 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 843 | { |
| 844 | int len; |
| 845 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 846 | PyErr_SetString(PyExc_TypeError, |
| 847 | "Str255 arg must be string of at most 255 chars"); |
| 848 | return 0; |
| 849 | } |
| 850 | pbuf[0] = len; |
| 851 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 852 | return 1; |
| 853 | } |
| 854 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 855 | /* Convert a Str255 to a Python string object */ |
| 856 | PyObject * |
| 857 | PyMac_BuildStr255(Str255 s) |
| 858 | { |
Jack Jansen | b734ade | 1999-12-17 17:15:50 +0000 | [diff] [blame] | 859 | if ( s == NULL ) { |
| 860 | PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL"); |
| 861 | return NULL; |
| 862 | } |
| 863 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 864 | } |
| 865 | |
| 866 | PyObject * |
| 867 | PyMac_BuildOptStr255(Str255 s) |
| 868 | { |
| 869 | if ( s == NULL ) { |
| 870 | Py_INCREF(Py_None); |
| 871 | return Py_None; |
| 872 | } |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 873 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 874 | } |
| 875 | |
| 876 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 877 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 878 | ** Convert a Python object to an FSSpec. |
| 879 | ** The object may either be a full pathname or a triple |
| 880 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 881 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 882 | ** The caller is responsible for not calling this routine |
| 883 | ** in those cases (which is fine, since everyone calling |
| 884 | ** this is probably sys7 dependent anyway). |
| 885 | */ |
| 886 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 887 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 888 | { |
| 889 | Str255 path; |
| 890 | short refnum; |
| 891 | long parid; |
| 892 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 893 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 894 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 895 | /* first check whether it already is an FSSpec */ |
| 896 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 897 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 898 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 899 | return 1; |
| 900 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 901 | if ( PyString_Check(v) ) { |
| 902 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 903 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 904 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 905 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 906 | parid = 0; |
| 907 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 908 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 909 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 910 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 911 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 912 | } |
| 913 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 914 | if ( err && err != fnfErr ) { |
Jack Jansen | 3d75754 | 2000-01-24 09:56:06 +0000 | [diff] [blame] | 915 | PyMac_Error(err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 916 | return 0; |
| 917 | } |
| 918 | return 1; |
| 919 | } |
| 920 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 921 | /* Convert FSSpec to PyObject */ |
| 922 | PyObject *PyMac_BuildFSSpec(FSSpec *v) |
| 923 | { |
| 924 | return newmfssobject(v); |
| 925 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 926 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 927 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 928 | The object must be a (left, top, right, bottom) tuple. |
| 929 | (This differs from the order in the struct but is consistent with |
| 930 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 931 | int |
| 932 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 933 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 934 | return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom); |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 935 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 936 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 937 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 938 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 939 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 940 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 941 | return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | |
| 945 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 946 | The object must be a (h, v) tuple. |
| 947 | (This differs from the order in the struct but is consistent with |
| 948 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 949 | int |
| 950 | PyMac_GetPoint(PyObject *v, Point *p) |
| 951 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 952 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* Convert a Point to a Python object */ |
| 956 | PyObject * |
| 957 | PyMac_BuildPoint(Point p) |
| 958 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 959 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | |
| 963 | /* Convert a Python object to an EventRecord. |
| 964 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 965 | int |
| 966 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 967 | { |
| 968 | return PyArg_Parse(v, "(hll(hh)h)", |
| 969 | &e->what, |
| 970 | &e->message, |
| 971 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 972 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 973 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 974 | &e->modifiers); |
| 975 | } |
| 976 | |
| 977 | /* Convert a Rect to an EventRecord object */ |
| 978 | PyObject * |
| 979 | PyMac_BuildEventRecord(EventRecord *e) |
| 980 | { |
| 981 | return Py_BuildValue("(hll(hh)h)", |
| 982 | e->what, |
| 983 | e->message, |
| 984 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 985 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 986 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 987 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 988 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 989 | |
| 990 | /* Convert Python object to Fixed */ |
| 991 | int |
| 992 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 993 | { |
| 994 | double d; |
| 995 | |
| 996 | if( !PyArg_Parse(v, "d", &d)) |
| 997 | return 0; |
| 998 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 999 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* Convert a Point to a Python object */ |
| 1003 | PyObject * |
| 1004 | PyMac_BuildFixed(Fixed f) |
| 1005 | { |
| 1006 | double d; |
| 1007 | |
| 1008 | d = f; |
| 1009 | d = d / 0x10000; |
| 1010 | return Py_BuildValue("d", d); |
| 1011 | } |
| 1012 | |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1013 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 1014 | int |
| 1015 | PyMac_Getwide(PyObject *v, wide *rv) |
| 1016 | { |
| 1017 | if (PyInt_Check(v)) { |
| 1018 | rv->hi = 0; |
| 1019 | rv->lo = PyInt_AsLong(v); |
| 1020 | if( rv->lo & 0x80000000 ) |
| 1021 | rv->hi = -1; |
| 1022 | return 1; |
| 1023 | } |
| 1024 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 1025 | } |
| 1026 | |
| 1027 | |
| 1028 | PyObject * |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1029 | PyMac_Buildwide(wide *w) |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1030 | { |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1031 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 1032 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 1033 | return PyInt_FromLong(w->lo); |
| 1034 | return Py_BuildValue("(ll)", w->hi, w->lo); |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1035 | } |