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 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 26 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 27 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 28 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 29 | #include "marshal.h" |
| 30 | #include "import.h" |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 31 | #include "importdl.h" |
Jack Jansen | 8bb1dc1 | 2001-05-19 12:31:09 +0000 | [diff] [blame] | 32 | #include "pymactoolbox.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 33 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 34 | #include "pythonresources.h" |
| 35 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 36 | #ifdef WITHOUT_FRAMEWORKS |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 37 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 38 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 39 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 40 | #include <Resources.h> |
| 41 | #include <Memory.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 42 | #include <Windows.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 43 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 44 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 45 | #include <Fonts.h> |
| 46 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 47 | #include <TextUtils.h> |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 48 | #include <LowMem.h> |
| 49 | #ifdef __CFM68K__ |
| 50 | /* cfm68k InterfaceLib exports GetEventQueue, but Events.h doesn't know this |
| 51 | ** and defines it as GetEvQHdr (which is correct for PPC). This fix is for |
| 52 | ** CW9, check that the workaround is still needed for the next release. |
| 53 | */ |
| 54 | #define GetEvQHdr GetEventQueue |
| 55 | #endif /* __CFM68K__ */ |
| 56 | |
| 57 | #include <Events.h> |
| 58 | |
| 59 | #ifdef __CFM68K__ |
| 60 | #undef GetEventQueue |
| 61 | #endif /* __CFM68K__ */ |
| 62 | #else |
| 63 | #include <Carbon/Carbon.h> |
| 64 | #endif |
| 65 | |
| 66 | #if !TARGET_API_MAC_OS8 |
| 67 | /* Unfortunately this call is probably slower... */ |
| 68 | #define LMGetTicks() TickCount() |
| 69 | #endif |
| 70 | |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 71 | #ifdef __MWERKS__ |
| 72 | #include <SIOUX.h> |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 73 | extern void SIOUXSetupMenus(void); |
| 74 | extern void SIOUXDoAboutBox(void); |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 75 | #endif |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 76 | #ifdef USE_GUSI |
| 77 | /* Functions we redefine because they're in obscure libraries */ |
| 78 | extern void SpinCursor(short x); |
| 79 | extern void RotateCursor(short x); |
| 80 | extern pascal void PLstrcpy(unsigned char *, unsigned char *); |
| 81 | extern pascal int PLstrcmp(unsigned char *, unsigned char *); |
| 82 | extern pascal unsigned char *PLstrrchr(unsigned char *, unsigned char); |
| 83 | |
| 84 | #endif |
| 85 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 86 | #ifdef USE_GUSI1 |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 87 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 88 | #include <GUSI.h> |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 89 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 90 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 91 | /* The ID of the Sioux apple menu */ |
| 92 | #define SIOUX_APPLEID 32000 |
| 93 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 94 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 95 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 96 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 97 | /* |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 98 | ** When less than this amount of stackspace is left we |
| 99 | ** raise a MemoryError. |
| 100 | */ |
| 101 | #ifndef MINIMUM_STACK_SIZE |
| 102 | #ifdef __powerc |
| 103 | #define MINIMUM_STACK_SIZE 8192 |
| 104 | #else |
| 105 | #define MINIMUM_STACK_SIZE 4096 |
| 106 | #endif |
| 107 | #endif |
| 108 | |
Jack Jansen | 340eb88 | 2001-02-02 22:40:28 +0000 | [diff] [blame] | 109 | #if TARGET_API_MAC_CARBON |
| 110 | /* |
| 111 | ** On MacOSX StackSpace() lies: it gives the distance from heap end to stack pointer, |
| 112 | ** but the stack cannot grow that far due to rlimit values. We cannot get at this value |
| 113 | ** from Carbon, so we set a maximum to the stack here that is based on the default |
| 114 | ** stack limit of 512K. |
| 115 | */ |
| 116 | #define MAXIMUM_STACK_SIZE (256*1024) |
| 117 | #endif |
| 118 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 119 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 120 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 121 | ** 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] | 122 | ** handle them). Note that we don't know who has windows open, so |
| 123 | ** 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] | 124 | */ |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 125 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 126 | |
| 127 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 129 | /* 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] | 130 | ** with the python errors.h. */ |
| 131 | #define fnfErr -43 |
| 132 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 133 | /* Interrupt code variables: */ |
| 134 | static int interrupted; /* Set to true when cmd-. seen */ |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 135 | static RETSIGTYPE intcatcher(int); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 136 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 137 | #if !TARGET_API_MAC_OSX |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 138 | static int PyMac_Yield(void); |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 139 | #endif |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 140 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 141 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 142 | ** These are the real scheduling parameters that control what we check |
| 143 | ** in the event loop, and how often we check. The values are initialized |
| 144 | ** from pyMac_SchedParamStruct. |
| 145 | */ |
| 146 | |
| 147 | struct real_sched_param_struct { |
| 148 | int check_interrupt; /* if true check for command-dot */ |
| 149 | int process_events; /* if nonzero enable evt processing, this mask */ |
| 150 | int besocial; /* if nonzero be a little social with CPU */ |
| 151 | unsigned long check_interval; /* how often to check, in ticks */ |
| 152 | unsigned long bg_yield; /* yield so long when in background */ |
| 153 | /* these are computed from previous and clock and such */ |
| 154 | int enabled; /* check_interrupt OR process_event OR yield */ |
| 155 | unsigned long next_check; /* when to check/yield next, in ticks */ |
| 156 | }; |
| 157 | |
| 158 | static struct real_sched_param_struct schedparams = |
| 159 | { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0}; |
| 160 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 161 | /* |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 162 | ** Workaround for sioux/gusi combo: set when we are exiting |
| 163 | */ |
| 164 | int PyMac_ConsoleIsDead; |
| 165 | |
| 166 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 167 | ** Sioux menu bar, saved early so we can restore it |
| 168 | */ |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 169 | static MenuBarHandle sioux_mbar; |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 170 | |
| 171 | /* |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 172 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 173 | */ |
| 174 | struct hook_args { |
| 175 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 176 | char *prompt; /* The prompt */ |
| 177 | }; |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 178 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 179 | /* The StandardFile hooks don't exist in Carbon. This breaks GetDirectory, |
| 180 | ** but the macfsn code will replace it by a NavServices version anyway. |
| 181 | */ |
| 182 | #define myhook_upp NULL |
| 183 | #else |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 184 | static DlgHookYDUPP myhook_upp; |
| 185 | static int upp_inited = 0; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 186 | #endif |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 187 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 188 | /* |
| 189 | ** The python-code event handler |
| 190 | */ |
| 191 | static PyObject *python_event_handler; |
| 192 | |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 193 | /* |
| 194 | ** Set to true if we're appearance-compliant |
| 195 | */ |
| 196 | int PyMac_AppearanceCompliant; |
| 197 | |
Jack Jansen | 1f9f2f4 | 2000-07-24 19:50:16 +0000 | [diff] [blame] | 198 | /* |
| 199 | ** Find out what the current script is. |
| 200 | ** Donated by Fredrik Lund. |
| 201 | */ |
| 202 | char *PyMac_getscript() |
| 203 | { |
| 204 | int font, script, lang; |
| 205 | font = 0; |
| 206 | font = GetSysFont(); |
| 207 | script = FontToScript(font); |
| 208 | switch (script) { |
| 209 | case smRoman: |
| 210 | lang = GetScriptVariable(script, smScriptLang); |
| 211 | if (lang == langIcelandic) |
| 212 | return "mac-iceland"; |
| 213 | else if (lang == langTurkish) |
| 214 | return "mac-turkish"; |
| 215 | else if (lang == langGreek) |
| 216 | return "mac-greek"; |
| 217 | else |
| 218 | return "mac-roman"; |
| 219 | break; |
unknown | f428aef | 2001-07-04 22:36:27 +0000 | [diff] [blame] | 220 | #if 0 |
| 221 | /* We don't have a codec for this, so don't return it */ |
Jack Jansen | 1f9f2f4 | 2000-07-24 19:50:16 +0000 | [diff] [blame] | 222 | case smJapanese: |
| 223 | return "mac-japan"; |
unknown | f428aef | 2001-07-04 22:36:27 +0000 | [diff] [blame] | 224 | #endif |
Jack Jansen | 1f9f2f4 | 2000-07-24 19:50:16 +0000 | [diff] [blame] | 225 | case smGreek: |
| 226 | return "mac-greek"; |
| 227 | case smCyrillic: |
| 228 | return "mac-cyrillic"; |
| 229 | default: |
| 230 | return "mac-roman"; /* better than nothing */ |
| 231 | } |
| 232 | } |
| 233 | |
Jack Jansen | 741e037 | 2001-05-19 12:55:57 +0000 | [diff] [blame] | 234 | /* Given an FSSpec, return the FSSpec of the parent folder */ |
| 235 | |
| 236 | static OSErr |
| 237 | get_folder_parent (FSSpec * fss, FSSpec * parent) |
| 238 | { |
| 239 | CInfoPBRec rec; |
| 240 | short err; |
| 241 | |
| 242 | * parent = * fss; |
| 243 | rec.hFileInfo.ioNamePtr = parent->name; |
| 244 | rec.hFileInfo.ioVRefNum = parent->vRefNum; |
| 245 | rec.hFileInfo.ioDirID = parent->parID; |
| 246 | rec.hFileInfo.ioFDirIndex = -1; |
| 247 | rec.hFileInfo.ioFVersNum = 0; |
| 248 | if (err = PBGetCatInfoSync (& rec)) |
| 249 | return err; |
| 250 | parent->parID = rec.dirInfo.ioDrParID; |
| 251 | /* parent->name[0] = 0; */ |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* Given an FSSpec return a full, colon-separated pathname */ |
| 256 | |
| 257 | OSErr |
| 258 | PyMac_GetFullPath (FSSpec *fss, char *buf) |
| 259 | { |
| 260 | short err; |
| 261 | FSSpec fss_parent, fss_current; |
| 262 | char tmpbuf[1024]; |
| 263 | int plen; |
| 264 | |
| 265 | fss_current = *fss; |
| 266 | plen = fss_current.name[0]; |
| 267 | memcpy(buf, &fss_current.name[1], plen); |
| 268 | buf[plen] = 0; |
| 269 | /* Special case for disk names */ |
| 270 | if ( fss_current.parID <= 1 ) { |
| 271 | buf[plen++] = ':'; |
| 272 | buf[plen] = 0; |
| 273 | return 0; |
| 274 | } |
| 275 | while (fss_current.parID > 1) { |
| 276 | /* Get parent folder name */ |
| 277 | if (err = get_folder_parent(&fss_current, &fss_parent)) |
| 278 | return err; |
| 279 | fss_current = fss_parent; |
| 280 | /* Prepend path component just found to buf */ |
| 281 | plen = fss_current.name[0]; |
| 282 | if (strlen(buf) + plen + 1 > 1024) { |
| 283 | /* Oops... Not enough space (shouldn't happen) */ |
| 284 | *buf = 0; |
| 285 | return -1; |
| 286 | } |
| 287 | memcpy(tmpbuf, &fss_current.name[1], plen); |
| 288 | tmpbuf[plen] = ':'; |
| 289 | strcpy(&tmpbuf[plen+1], buf); |
| 290 | strcpy(buf, tmpbuf); |
| 291 | } |
| 292 | return 0; |
| 293 | } |
Jack Jansen | 1f9f2f4 | 2000-07-24 19:50:16 +0000 | [diff] [blame] | 294 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 295 | #ifdef USE_GUSI1 |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 296 | /* |
| 297 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 298 | ** the working directory. Hence, we call this routine after each call |
| 299 | ** to chdir() to rectify things. |
| 300 | */ |
| 301 | void |
| 302 | PyMac_FixGUSIcd() |
| 303 | { |
| 304 | WDPBRec pb; |
| 305 | FSSpec curdirfss; |
| 306 | |
| 307 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 308 | return; |
| 309 | |
| 310 | /* Set MacOS "working directory" */ |
| 311 | pb.ioNamePtr= "\p"; |
| 312 | pb.ioVRefNum= curdirfss.vRefNum; |
| 313 | pb.ioWDDirID= curdirfss.parID; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 314 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 315 | return; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 316 | } |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 317 | #endif |
Jack Jansen | 7ac70af | 1996-08-19 11:01:05 +0000 | [diff] [blame] | 318 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 319 | #ifdef USE_GUSI |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 320 | /* |
| 321 | ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we |
| 322 | ** provide a dummy here. |
| 323 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 324 | void SpinCursor(short x) { /* Dummy */ } |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 325 | void RotateCursor(short x) { /* Dummy */ } |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 326 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 327 | /* |
| 328 | ** Replacement GUSI Spin function |
| 329 | */ |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 330 | #ifdef USE_GUSI1 |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 331 | static int |
| 332 | PyMac_GUSISpin(spin_msg msg, long arg) |
| 333 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 334 | static Boolean inForeground = true; |
| 335 | int maxsleep = 6; /* 6 ticks is "normal" sleeptime */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 336 | |
| 337 | if (PyMac_ConsoleIsDead) return 0; |
| 338 | #if 0 |
| 339 | if (inForeground) |
| 340 | SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1); |
| 341 | #endif |
| 342 | |
| 343 | if (interrupted) return -1; |
| 344 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 345 | if ( msg == SP_AUTO_SPIN ) |
| 346 | maxsleep = 0; |
| 347 | if ( msg==SP_SLEEP||msg==SP_SELECT ) |
| 348 | maxsleep = arg; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 349 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 350 | 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] | 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | PyMac_SetGUSISpin() { |
| 357 | GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin); |
| 358 | } |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 359 | #endif |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 360 | |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 361 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 362 | void |
| 363 | PyMac_StopGUSISpin() { |
| 364 | PyMac_ConsoleIsDead = 1; |
| 365 | } |
| 366 | |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 367 | #if TARGET_API_MAC_OS8 |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 368 | /* |
| 369 | ** Replacement routines for the PLstr... functions so we don't need |
Jack Jansen | 6ba34aa | 2001-01-11 23:03:56 +0000 | [diff] [blame] | 370 | ** StdCLib. |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 371 | */ |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 372 | pascal void |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 373 | PLstrcpy(unsigned char *to, unsigned char *fr) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 374 | { |
| 375 | memcpy(to, fr, fr[0]+1); |
| 376 | } |
| 377 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 378 | pascal int |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 379 | PLstrcmp(unsigned char *s1, unsigned char *s2) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 380 | { |
| 381 | int res; |
| 382 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 383 | |
| 384 | res = memcmp(s1+1, s2+1, l); |
| 385 | if ( res != 0 ) |
| 386 | return res; |
| 387 | |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 388 | if ( s1[0] < s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 389 | return -1; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 390 | else if ( s1[0] > s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 391 | return 1; |
| 392 | else |
| 393 | return 0; |
| 394 | } |
| 395 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 396 | pascal unsigned char * |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 397 | PLstrrchr(unsigned char *str, unsigned char chr) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 398 | { |
| 399 | unsigned char *ptr = 0; |
| 400 | unsigned char *p; |
| 401 | |
| 402 | for(p=str+1; p<str+str[0]; p++) |
| 403 | if ( *p == chr ) |
| 404 | ptr = p; |
| 405 | return ptr; |
| 406 | } |
| 407 | |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 408 | #endif /* TARGET_API_MAC_OS8 */ |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 409 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 410 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 411 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 412 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 413 | unsigned char * |
| 414 | Pstring(char *str) |
| 415 | { |
| 416 | static Str255 buf; |
| 417 | int len; |
| 418 | |
| 419 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 420 | if (len > 255) |
| 421 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 422 | buf[0] = (unsigned char)len; |
| 423 | strncpy((char *)buf+1, str, len); |
| 424 | return buf; |
| 425 | } |
| 426 | |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 427 | #if TARGET_API_MAC_OS8 |
Jack Jansen | 5afad83 | 2000-12-12 22:12:14 +0000 | [diff] [blame] | 428 | void |
| 429 | c2pstrcpy(unsigned char *dst, const char *src) |
| 430 | { |
| 431 | int len; |
| 432 | |
| 433 | len = strlen(src); |
| 434 | if ( len > 255 ) len = 255; |
| 435 | strncpy((char *)dst+1, src, len); |
| 436 | dst[0] = len; |
| 437 | } |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 438 | #endif /* TARGET_API_MAC_OS8 */ |
Jack Jansen | 5afad83 | 2000-12-12 22:12:14 +0000 | [diff] [blame] | 439 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 440 | /* Like strerror() but for Mac OS error numbers */ |
| 441 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 442 | { |
| 443 | static char buf[256]; |
| 444 | Handle h; |
| 445 | char *str; |
| 446 | |
| 447 | h = GetResource('Estr', err); |
| 448 | if ( h ) { |
| 449 | HLock(h); |
| 450 | str = (char *)*h; |
| 451 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 452 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 453 | HUnlock(h); |
| 454 | ReleaseResource(h); |
| 455 | } else { |
| 456 | sprintf(buf, "Mac OS error code %d", err); |
| 457 | } |
| 458 | return buf; |
| 459 | } |
| 460 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 461 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 462 | PyObject *PyMac_OSErrException; |
| 463 | |
| 464 | /* Initialize and return PyMac_OSErrException */ |
| 465 | PyObject * |
| 466 | PyMac_GetOSErrException() |
| 467 | { |
| 468 | if (PyMac_OSErrException == NULL) |
unknown | f428aef | 2001-07-04 22:36:27 +0000 | [diff] [blame] | 469 | PyMac_OSErrException = PyString_FromString("MacOS.Error"); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 470 | return PyMac_OSErrException; |
| 471 | } |
| 472 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 473 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 474 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 475 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 476 | { |
| 477 | char *msg; |
| 478 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 479 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 480 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 481 | Py_INCREF(Py_None); |
| 482 | return Py_None; |
| 483 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 484 | if (err == -1 && PyErr_Occurred()) |
| 485 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 486 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 487 | v = Py_BuildValue("(is)", err, msg); |
| 488 | PyErr_SetObject(eobj, v); |
| 489 | Py_DECREF(v); |
| 490 | return NULL; |
| 491 | } |
| 492 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 493 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 494 | PyObject * |
| 495 | PyMac_Error(OSErr err) |
| 496 | { |
| 497 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 498 | } |
| 499 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 500 | #ifdef USE_STACKCHECK |
| 501 | /* Check for stack overflow */ |
| 502 | int |
| 503 | PyOS_CheckStack() |
| 504 | { |
Jack Jansen | 14a9171 | 2000-08-25 21:57:23 +0000 | [diff] [blame] | 505 | char here; |
| 506 | static char *sentinel = 0; |
Jack Jansen | 53bafd9 | 2000-09-08 22:05:48 +0000 | [diff] [blame] | 507 | static PyThreadState *thread_for_sentinel = 0; |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 508 | |
Jack Jansen | 340eb88 | 2001-02-02 22:40:28 +0000 | [diff] [blame] | 509 | if ( sentinel == 0 ) { |
| 510 | unsigned long stackspace = StackSpace(); |
| 511 | |
| 512 | #ifdef MAXIMUM_STACK_SIZE |
| 513 | /* See the comment at the definition */ |
| 514 | if ( stackspace > MAXIMUM_STACK_SIZE ) |
| 515 | stackspace = MAXIMUM_STACK_SIZE; |
| 516 | #endif |
| 517 | sentinel = &here - stackspace + MINIMUM_STACK_SIZE; |
Jack Jansen | 14a9171 | 2000-08-25 21:57:23 +0000 | [diff] [blame] | 518 | } |
Jack Jansen | 53bafd9 | 2000-09-08 22:05:48 +0000 | [diff] [blame] | 519 | if ( thread_for_sentinel == 0 ) { |
| 520 | thread_for_sentinel = PyThreadState_Get(); |
| 521 | } |
| 522 | if ( &here < sentinel ) { |
| 523 | if (thread_for_sentinel == PyThreadState_Get()) { |
| 524 | return -1; |
| 525 | #if 0 |
| 526 | } else { |
| 527 | /* Else we are unsure... */ |
| 528 | fprintf(stderr, "Stackcheck in other thread (was %x now %x)\n", thread_for_sentinel,PyThreadState_Get()); |
| 529 | #endif |
| 530 | } |
| 531 | } |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | #endif /* USE_STACKCHECK */ |
| 535 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 536 | #if !TARGET_API_MAC_OSX |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 537 | /* The catcher routine (which may not be used for all compilers) */ |
| 538 | static RETSIGTYPE |
| 539 | intcatcher(sig) |
| 540 | int sig; |
| 541 | { |
| 542 | interrupted = 1; |
| 543 | signal(SIGINT, intcatcher); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | PyOS_InitInterrupts() |
| 548 | { |
| 549 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 550 | signal(SIGINT, intcatcher); |
| 551 | } |
| 552 | |
Jack Jansen | a8441de | 1997-08-08 14:57:37 +0000 | [diff] [blame] | 553 | void |
| 554 | PyOS_FiniInterrupts() |
| 555 | { |
| 556 | } |
| 557 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 558 | /* |
| 559 | ** This routine scans the event queue looking for cmd-. |
| 560 | ** This is the only way to get an interrupt under THINK (since it |
| 561 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 562 | ** the full-fledged event loop is disabled. This way, we can at least |
| 563 | ** interrupt a runaway python program. |
| 564 | */ |
| 565 | static void |
| 566 | scan_event_queue(flush) |
| 567 | int flush; |
| 568 | { |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 569 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | d7b6802 | 2001-01-12 23:42:28 +0000 | [diff] [blame] | 570 | if ( CheckEventQueueForUserCancel() ) |
| 571 | interrupted = 1; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 572 | #else |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 573 | register EvQElPtr q; |
| 574 | |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 575 | q = (EvQElPtr) LMGetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 576 | |
| 577 | for (; q; q = (EvQElPtr)q->qLink) { |
| 578 | if (q->evtQWhat == keyDown && |
| 579 | (char)q->evtQMessage == '.' && |
| 580 | (q->evtQModifiers & cmdKey) != 0) { |
| 581 | if ( flush ) |
| 582 | FlushEvents(keyDownMask, 0); |
| 583 | interrupted = 1; |
| 584 | break; |
| 585 | } |
| 586 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 587 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | int |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 591 | PyErr_CheckSignals() |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 592 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 593 | if (schedparams.enabled) { |
| 594 | if ( (unsigned long)LMGetTicks() > schedparams.next_check ) { |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 595 | if ( PyMac_Yield() < 0) |
| 596 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 597 | schedparams.next_check = (unsigned long)LMGetTicks() |
| 598 | + schedparams.check_interval; |
| 599 | if (interrupted) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 600 | scan_event_queue(1); /* Eat events up to cmd-. */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 601 | interrupted = 0; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 602 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 603 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 606 | } |
| 607 | return 0; |
| 608 | } |
| 609 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 610 | int |
| 611 | PyOS_InterruptOccurred() |
| 612 | { |
| 613 | scan_event_queue(1); |
| 614 | return interrupted; |
| 615 | } |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 616 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 617 | /* Check whether we are in the foreground */ |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 618 | static int |
| 619 | PyMac_InForeground(void) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 620 | { |
| 621 | static ProcessSerialNumber ours; |
| 622 | static inited; |
| 623 | ProcessSerialNumber curfg; |
| 624 | Boolean eq; |
| 625 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 626 | if ( inited == 0 ) { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 627 | (void)GetCurrentProcess(&ours); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 628 | inited = 1; |
| 629 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 630 | if ( GetFrontProcess(&curfg) < 0 ) |
| 631 | eq = 1; |
| 632 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 633 | eq = 1; |
| 634 | return (int)eq; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 635 | } |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 636 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 637 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 638 | int |
| 639 | PyMac_SetEventHandler(PyObject *evh) |
| 640 | { |
| 641 | if ( evh && python_event_handler ) { |
| 642 | PyErr_SetString(PyExc_RuntimeError, "Python event handler already set"); |
| 643 | return 0; |
| 644 | } |
| 645 | if ( python_event_handler ) |
| 646 | Py_DECREF(python_event_handler); |
| 647 | if ( evh ) |
| 648 | Py_INCREF(evh); |
| 649 | python_event_handler = evh; |
| 650 | return 1; |
| 651 | } |
| 652 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 653 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 654 | ** Handle an event, either one found in the mainloop eventhandler or |
| 655 | ** one passed back from the python program. |
| 656 | */ |
| 657 | void |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 658 | PyMac_HandleEventIntern(evp) |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 659 | EventRecord *evp; |
| 660 | { |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 661 | #if TARGET_API_MAC_OS8 |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 662 | if ( evp->what == mouseDown ) { |
| 663 | WindowPtr wp; |
| 664 | |
| 665 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
| 666 | SystemClick(evp, wp); |
| 667 | return; |
| 668 | } |
| 669 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 670 | #endif |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 671 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 672 | { |
| 673 | int siouxdidit; |
| 674 | |
| 675 | /* If SIOUX wants it we're done */ |
| 676 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 677 | if ( siouxdidit ) |
| 678 | return; |
| 679 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 680 | #else |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 681 | /* Other compilers are just unlucky... */ |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 682 | #endif /* !__MWERKS__ */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | /* |
| 686 | ** Handle an event, either through HandleEvent or by passing it to the Python |
| 687 | ** event handler. |
| 688 | */ |
| 689 | int |
| 690 | PyMac_HandleEvent(evp) |
| 691 | EventRecord *evp; |
| 692 | { |
| 693 | PyObject *rv; |
| 694 | |
| 695 | if ( python_event_handler ) { |
| 696 | rv = PyObject_CallFunction(python_event_handler, "(O&)", |
| 697 | PyMac_BuildEventRecord, evp); |
| 698 | if ( rv ) |
| 699 | Py_DECREF(rv); |
| 700 | else |
| 701 | return -1; /* Propagate exception */ |
| 702 | } else { |
| 703 | PyMac_HandleEventIntern(evp); |
| 704 | } |
| 705 | return 0; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 708 | #if !TARGET_API_MAC_OSX |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 709 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 710 | ** Yield the CPU to other tasks without processing events. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 711 | */ |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 712 | int |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 713 | PyMac_DoYield(int maxsleep, int maycallpython) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 714 | { |
| 715 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 716 | int gotone; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 717 | long latest_time_ready; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 718 | static int in_here = 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 719 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 720 | in_here++; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 721 | /* |
| 722 | ** First check for interrupts, if wanted. |
| 723 | ** This sets a flag that will be picked up at an appropriate |
| 724 | ** moment in the mainloop. |
| 725 | */ |
| 726 | if (schedparams.check_interrupt) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 727 | scan_event_queue(0); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 728 | |
| 729 | /* XXXX Implementing an idle routine goes here */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 730 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 731 | /* |
| 732 | ** Check which of the eventloop cases we have: |
| 733 | ** - process events |
| 734 | ** - don't process events but do yield |
| 735 | ** - do neither |
| 736 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 737 | if( in_here > 1 || !schedparams.process_events || |
| 738 | (python_event_handler && !maycallpython) ) { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 739 | if ( maxsleep >= 0 ) { |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 740 | #if TARGET_API_MAC_OS8 |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 741 | SystemTask(); |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 742 | #else |
| 743 | int xxx = 0; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 744 | #endif |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 745 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 746 | } else { |
| 747 | latest_time_ready = LMGetTicks() + maxsleep; |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 748 | do { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 749 | /* XXXX Hack by Jack. |
| 750 | ** In time.sleep() you can click to another application |
| 751 | ** once only. If you come back to Python you cannot get away |
| 752 | ** again. |
| 753 | **/ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 754 | gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 755 | /* Get out quickly if nothing interesting is happening */ |
| 756 | if ( !gotone || ev.what == nullEvent ) |
| 757 | break; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 758 | if ( PyMac_HandleEvent(&ev) < 0 ) { |
| 759 | in_here--; |
| 760 | return -1; |
| 761 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 762 | maxsleep = latest_time_ready - LMGetTicks(); |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 763 | } while ( maxsleep > 0 ); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 764 | } |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 765 | in_here--; |
| 766 | return 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 770 | ** Process events and/or yield the CPU to other tasks if opportune |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 771 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 772 | int |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 773 | PyMac_Yield() { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 774 | unsigned long maxsleep; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 775 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 776 | if( PyMac_InForeground() ) |
| 777 | maxsleep = 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 778 | else |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 779 | maxsleep = schedparams.bg_yield; |
| 780 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 781 | return PyMac_DoYield(maxsleep, 1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 785 | ** Return current scheduler parameters |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 786 | */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 787 | void |
| 788 | PyMac_GetSchedParams(PyMacSchedParams *sp) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 789 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 790 | sp->check_interrupt = schedparams.check_interrupt; |
| 791 | sp->process_events = schedparams.process_events; |
| 792 | sp->besocial = schedparams.besocial; |
| 793 | sp->check_interval = schedparams.check_interval / 60.0; |
| 794 | sp->bg_yield = schedparams.bg_yield / 60.0; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 795 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 796 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 797 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 798 | ** Set current scheduler parameters |
| 799 | */ |
| 800 | void |
| 801 | PyMac_SetSchedParams(PyMacSchedParams *sp) |
| 802 | { |
| 803 | schedparams.check_interrupt = sp->check_interrupt; |
| 804 | schedparams.process_events = sp->process_events; |
| 805 | schedparams.besocial = sp->besocial; |
| 806 | schedparams.check_interval = (unsigned long)(sp->check_interval*60); |
| 807 | schedparams.bg_yield = (unsigned long)(sp->bg_yield*60); |
| 808 | if ( schedparams.check_interrupt || schedparams.process_events || |
| 809 | schedparams.besocial ) |
| 810 | schedparams.enabled = 1; |
| 811 | else |
| 812 | schedparams.enabled = 0; |
| 813 | schedparams.next_check = 0; /* Check immedeately */ |
| 814 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 815 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 816 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 817 | ** Install our menu bar. |
| 818 | */ |
| 819 | void |
| 820 | PyMac_InitMenuBar() |
| 821 | { |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 822 | MenuHandle applemenu; |
| 823 | |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 824 | if ( sioux_mbar ) return; |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 825 | if ( (sioux_mbar=GetMenuBar()) == NULL ) { |
| 826 | /* Sioux menu not installed yet. Do so */ |
| 827 | SIOUXSetupMenus(); |
| 828 | if ( (sioux_mbar=GetMenuBar()) == NULL ) |
| 829 | return; |
| 830 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 831 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 832 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 833 | } |
| 834 | |
| 835 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 836 | ** Restore sioux menu bar |
| 837 | */ |
| 838 | void |
| 839 | PyMac_RestoreMenuBar() |
| 840 | { |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 841 | #if 1 |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 842 | /* This doesn't seem to work anymore? Or only for Carbon? */ |
| 843 | MenuBarHandle curmenubar; |
| 844 | |
| 845 | curmenubar = GetMenuBar(); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 846 | if ( sioux_mbar ) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 847 | SetMenuBar(sioux_mbar); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 848 | DrawMenuBar(); |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 849 | } else { |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 850 | PyMac_InitMenuBar(); |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 851 | DrawMenuBar(); |
| 852 | } |
| 853 | #endif |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 856 | void |
| 857 | PyMac_RaiseConsoleWindow() |
| 858 | { |
| 859 | /* Note: this is a hack. SIOUXTextWindow is SIOUX's internal structure |
| 860 | ** and we happen to know that the first entry is the window pointer. |
| 861 | */ |
| 862 | extern WindowRef *SIOUXTextWindow; |
| 863 | |
| 864 | if ( SIOUXTextWindow == NULL || *SIOUXTextWindow == NULL ) |
| 865 | return; |
| 866 | if ( FrontWindow() != *SIOUXTextWindow ) |
| 867 | BringToFront(*SIOUXTextWindow); |
| 868 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 869 | |
| 870 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 871 | ** Our replacement about box |
| 872 | */ |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 873 | |
| 874 | #include "patchlevel.h" |
| 875 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 876 | void |
| 877 | SIOUXDoAboutBox(void) |
| 878 | { |
| 879 | DialogPtr theDialog; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 880 | WindowPtr theWindow; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 881 | short item; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 882 | short fontID; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 883 | |
| 884 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 885 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 886 | theWindow = GetDialogWindow(theDialog); |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 887 | SetPortWindowPort(theWindow); |
| 888 | GetFNum("\pPython-Sans", &fontID); |
| 889 | if (fontID == 0) |
| 890 | fontID = kFontIDGeneva; |
| 891 | TextFont(fontID); |
| 892 | TextSize(9); |
| 893 | ParamText(Pstring(PATCHLEVEL), "\p", "\p", "\p"); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 894 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 895 | ModalDialog(NULL, &item); |
| 896 | DisposeDialog(theDialog); |
| 897 | } |
| 898 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 899 | #endif /* !TARGET_API_MAC_OSX */ |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 900 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 901 | #if TARGET_API_MAC_OS8 |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 902 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 903 | ** Helper routine for GetDirectory |
| 904 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 905 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 906 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 907 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 908 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 909 | Handle prompth; |
| 910 | short type; |
| 911 | Rect rect; |
| 912 | |
| 913 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 914 | if ( prompth ) |
| 915 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 916 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 917 | if ( item == SELECTCUR_ITEM ) { |
| 918 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 919 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 920 | } |
| 921 | return item; |
| 922 | } |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 923 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 924 | /* |
| 925 | ** Ask the user for a directory. I still can't understand |
| 926 | ** why Apple doesn't provide a standard solution for this... |
| 927 | */ |
| 928 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 929 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 930 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 931 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 932 | { |
| 933 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 934 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 935 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 936 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 937 | |
| 938 | if ( !upp_inited ) { |
| 939 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 940 | upp_inited = 1; |
| 941 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 942 | if ( prompt && *prompt ) |
| 943 | hook_args.prompt = (char *)Pstring(prompt); |
| 944 | else |
| 945 | hook_args.prompt = NULL; |
| 946 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 947 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 948 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 949 | |
| 950 | reply.sfFile.name[0] = 0; |
| 951 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 952 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 953 | return hook_args.selectcur_hit; |
| 954 | } |
| 955 | |
| 956 | /* |
| 957 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 958 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 959 | StandardFileReply *reply, char *prompt) |
| 960 | { |
| 961 | static Point where = {-1, -1}; |
| 962 | struct hook_args hook_args; |
| 963 | |
| 964 | if ( !upp_inited ) { |
| 965 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 966 | upp_inited = 1; |
| 967 | } |
| 968 | if ( prompt && *prompt ) |
| 969 | hook_args.prompt = (char *)Pstring(prompt); |
| 970 | else |
| 971 | hook_args.prompt = NULL; |
| 972 | hook_args.selectcur_hit = 0; |
| 973 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 974 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 975 | } |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 976 | #endif /* TARGET_API_MAC_OS8 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 977 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 978 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 979 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 980 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 981 | { |
| 982 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 983 | PyErr_SetString(PyExc_TypeError, |
| 984 | "OSType arg must be string of 4 chars"); |
| 985 | return 0; |
| 986 | } |
| 987 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 988 | return 1; |
| 989 | } |
| 990 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 991 | /* Convert an OSType value to a 4-char string object */ |
| 992 | PyObject * |
| 993 | PyMac_BuildOSType(OSType t) |
| 994 | { |
| 995 | return PyString_FromStringAndSize((char *)&t, 4); |
| 996 | } |
| 997 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 998 | /* Convert an NumVersion value to a 4-element tuple */ |
| 999 | PyObject * |
| 1000 | PyMac_BuildNumVersion(NumVersion t) |
| 1001 | { |
| 1002 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 1003 | } |
| 1004 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1005 | |
| 1006 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1007 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1008 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 1009 | { |
| 1010 | int len; |
| 1011 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 1012 | PyErr_SetString(PyExc_TypeError, |
| 1013 | "Str255 arg must be string of at most 255 chars"); |
| 1014 | return 0; |
| 1015 | } |
| 1016 | pbuf[0] = len; |
| 1017 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1021 | /* Convert a Str255 to a Python string object */ |
| 1022 | PyObject * |
| 1023 | PyMac_BuildStr255(Str255 s) |
| 1024 | { |
Jack Jansen | b734ade | 1999-12-17 17:15:50 +0000 | [diff] [blame] | 1025 | if ( s == NULL ) { |
| 1026 | PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL"); |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 1030 | } |
| 1031 | |
| 1032 | PyObject * |
| 1033 | PyMac_BuildOptStr255(Str255 s) |
| 1034 | { |
| 1035 | if ( s == NULL ) { |
| 1036 | Py_INCREF(Py_None); |
| 1037 | return Py_None; |
| 1038 | } |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1039 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 1040 | } |
| 1041 | |
| 1042 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1043 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1044 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1045 | The object must be a (left, top, right, bottom) tuple. |
| 1046 | (This differs from the order in the struct but is consistent with |
| 1047 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1048 | int |
| 1049 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 1050 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1051 | 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] | 1052 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1053 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1054 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1055 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1056 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1057 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1058 | 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] | 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1063 | The object must be a (h, v) tuple. |
| 1064 | (This differs from the order in the struct but is consistent with |
| 1065 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1066 | int |
| 1067 | PyMac_GetPoint(PyObject *v, Point *p) |
| 1068 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1069 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | /* Convert a Point to a Python object */ |
| 1073 | PyObject * |
| 1074 | PyMac_BuildPoint(Point p) |
| 1075 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1076 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | |
| 1080 | /* Convert a Python object to an EventRecord. |
| 1081 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 1082 | int |
| 1083 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 1084 | { |
Jack Jansen | 2c278ed | 2001-07-12 21:48:10 +0000 | [diff] [blame] | 1085 | return PyArg_Parse(v, "(Hll(hh)H)", |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1086 | &e->what, |
| 1087 | &e->message, |
| 1088 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1089 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1090 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1091 | &e->modifiers); |
| 1092 | } |
| 1093 | |
| 1094 | /* Convert a Rect to an EventRecord object */ |
| 1095 | PyObject * |
| 1096 | PyMac_BuildEventRecord(EventRecord *e) |
| 1097 | { |
| 1098 | return Py_BuildValue("(hll(hh)h)", |
| 1099 | e->what, |
| 1100 | e->message, |
| 1101 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1102 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 1103 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 1104 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1105 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 1106 | |
| 1107 | /* Convert Python object to Fixed */ |
| 1108 | int |
| 1109 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 1110 | { |
| 1111 | double d; |
| 1112 | |
| 1113 | if( !PyArg_Parse(v, "d", &d)) |
| 1114 | return 0; |
| 1115 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 1116 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /* Convert a Point to a Python object */ |
| 1120 | PyObject * |
| 1121 | PyMac_BuildFixed(Fixed f) |
| 1122 | { |
| 1123 | double d; |
| 1124 | |
| 1125 | d = f; |
| 1126 | d = d / 0x10000; |
| 1127 | return Py_BuildValue("d", d); |
| 1128 | } |
| 1129 | |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1130 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 1131 | int |
| 1132 | PyMac_Getwide(PyObject *v, wide *rv) |
| 1133 | { |
| 1134 | if (PyInt_Check(v)) { |
| 1135 | rv->hi = 0; |
| 1136 | rv->lo = PyInt_AsLong(v); |
| 1137 | if( rv->lo & 0x80000000 ) |
| 1138 | rv->hi = -1; |
| 1139 | return 1; |
| 1140 | } |
| 1141 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | PyObject * |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1146 | PyMac_Buildwide(wide *w) |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1147 | { |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 1148 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 1149 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 1150 | return PyInt_FromLong(w->lo); |
| 1151 | return Py_BuildValue("(ll)", w->hi, w->lo); |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 1152 | } |
Jack Jansen | 8bb1dc1 | 2001-05-19 12:31:09 +0000 | [diff] [blame] | 1153 | |
| 1154 | #ifdef USE_TOOLBOX_OBJECT_GLUE |
| 1155 | /* |
| 1156 | ** Glue together the toolbox objects. |
| 1157 | ** |
| 1158 | ** Because toolbox modules interdepend on each other, they use each others |
| 1159 | ** object types, on MacOSX/MachO this leads to the situation that they |
| 1160 | ** cannot be dynamically loaded (or they would all have to be lumped into |
| 1161 | ** a single .so, but this would be bad for extensibility). |
| 1162 | ** |
| 1163 | ** This file defines wrappers for all the _New and _Convert functions, |
| 1164 | ** which are the Py_BuildValue and PyArg_ParseTuple helpers. The wrappers |
| 1165 | ** check an indirection function pointer, and if it isn't filled in yet |
| 1166 | ** they import the appropriate module, whose init routine should fill in |
| 1167 | ** the pointer. |
| 1168 | */ |
| 1169 | |
| 1170 | #define GLUE_NEW(object, routinename, module) \ |
| 1171 | PyObject *(*PyMacGluePtr_##routinename)(object); \ |
| 1172 | \ |
| 1173 | PyObject *routinename(object cobj) { \ |
| 1174 | if (!PyMacGluePtr_##routinename) { \ |
| 1175 | if (!PyImport_ImportModule(module)) return NULL; \ |
| 1176 | if (!PyMacGluePtr_##routinename) { \ |
| 1177 | PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \ |
| 1178 | return NULL; \ |
| 1179 | } \ |
| 1180 | } \ |
| 1181 | return (*PyMacGluePtr_##routinename)(cobj); \ |
| 1182 | } |
| 1183 | |
| 1184 | #define GLUE_CONVERT(object, routinename, module) \ |
| 1185 | int (*PyMacGluePtr_##routinename)(PyObject *, object *); \ |
| 1186 | \ |
| 1187 | int routinename(PyObject *pyobj, object *cobj) { \ |
| 1188 | if (!PyMacGluePtr_##routinename) { \ |
| 1189 | if (!PyImport_ImportModule(module)) return NULL; \ |
| 1190 | if (!PyMacGluePtr_##routinename) { \ |
| 1191 | PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \ |
| 1192 | return NULL; \ |
| 1193 | } \ |
| 1194 | } \ |
| 1195 | return (*PyMacGluePtr_##routinename)(pyobj, cobj); \ |
| 1196 | } |
| 1197 | |
| 1198 | GLUE_NEW(AppleEvent *, AEDesc_New, "AE") /* XXXX Why by address? */ |
| 1199 | GLUE_CONVERT(AppleEvent, AEDesc_Convert, "AE") |
| 1200 | |
| 1201 | GLUE_NEW(Component, CmpObj_New, "Cm") |
| 1202 | GLUE_CONVERT(Component, CmpObj_Convert, "Cm") |
| 1203 | GLUE_NEW(ComponentInstance, CmpInstObj_New, "Cm") |
| 1204 | GLUE_CONVERT(ComponentInstance, CmpInstObj_Convert, "Cm") |
| 1205 | |
| 1206 | GLUE_NEW(ControlHandle, CtlObj_New, "Ctl") |
| 1207 | GLUE_CONVERT(ControlHandle, CtlObj_Convert, "Ctl") |
| 1208 | |
| 1209 | GLUE_NEW(DialogPtr, DlgObj_New, "Dlg") |
| 1210 | GLUE_CONVERT(DialogPtr, DlgObj_Convert, "Dlg") |
| 1211 | GLUE_NEW(DialogPtr, DlgObj_WhichDialog, "Dlg") |
| 1212 | |
| 1213 | GLUE_NEW(DragReference, DragObj_New, "Drag") |
| 1214 | GLUE_CONVERT(DragReference, DragObj_Convert, "Drag") |
| 1215 | |
| 1216 | GLUE_NEW(ListHandle, ListObj_New, "List") |
| 1217 | GLUE_CONVERT(ListHandle, ListObj_Convert, "List") |
| 1218 | |
| 1219 | GLUE_NEW(MenuHandle, MenuObj_New, "Menu") |
| 1220 | GLUE_CONVERT(MenuHandle, MenuObj_Convert, "Menu") |
| 1221 | |
| 1222 | GLUE_NEW(GrafPtr, GrafObj_New, "Qd") |
| 1223 | GLUE_CONVERT(GrafPtr, GrafObj_Convert, "Qd") |
| 1224 | GLUE_NEW(BitMapPtr, BMObj_New, "Qd") |
| 1225 | GLUE_CONVERT(BitMapPtr, BMObj_Convert, "Qd") |
| 1226 | GLUE_NEW(RGBColor *, QdRGB_New, "Qd") /* XXXX Why? */ |
| 1227 | GLUE_CONVERT(RGBColor, QdRGB_Convert, "Qd") |
| 1228 | |
| 1229 | GLUE_NEW(GWorldPtr, GWorldObj_New, "Qdoffs") |
| 1230 | GLUE_CONVERT(GWorldPtr, GWorldObj_Convert, "Qdoffs") |
| 1231 | |
| 1232 | GLUE_NEW(Track, TrackObj_New, "Qt") |
| 1233 | GLUE_CONVERT(Track, TrackObj_Convert, "Qt") |
| 1234 | GLUE_NEW(Movie, MovieObj_New, "Qt") |
| 1235 | GLUE_CONVERT(Movie, MovieObj_Convert, "Qt") |
| 1236 | GLUE_NEW(MovieController, MovieCtlObj_New, "Qt") |
| 1237 | GLUE_CONVERT(MovieController, MovieCtlObj_Convert, "Qt") |
| 1238 | GLUE_NEW(TimeBase, TimeBaseObj_New, "Qt") |
| 1239 | GLUE_CONVERT(TimeBase, TimeBaseObj_Convert, "Qt") |
| 1240 | GLUE_NEW(UserData, UserDataObj_New, "Qt") |
| 1241 | GLUE_CONVERT(UserData, UserDataObj_Convert, "Qt") |
| 1242 | GLUE_NEW(Media, MediaObj_New, "Qt") |
| 1243 | GLUE_CONVERT(Media, MediaObj_Convert, "Qt") |
| 1244 | |
| 1245 | GLUE_NEW(Handle, ResObj_New, "Res") |
| 1246 | GLUE_CONVERT(Handle, ResObj_Convert, "Res") |
| 1247 | GLUE_NEW(Handle, OptResObj_New, "Res") |
| 1248 | GLUE_CONVERT(Handle, OptResObj_Convert, "Res") |
| 1249 | |
| 1250 | GLUE_NEW(TEHandle, TEObj_New, "TE") |
| 1251 | GLUE_CONVERT(TEHandle, TEObj_Convert, "TE") |
| 1252 | |
| 1253 | GLUE_NEW(WindowPtr, WinObj_New, "Win") |
| 1254 | GLUE_CONVERT(WindowPtr, WinObj_Convert, "Win") |
| 1255 | GLUE_NEW(WindowPtr, WinObj_WhichWindow, "Win") |
| 1256 | |
| 1257 | #endif /* USE_TOOLBOX_OBJECT_GLUE */ |