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