Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 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 | |
| 25 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 26 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 27 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 28 | #include "marshal.h" |
| 29 | #include "import.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 30 | |
| 31 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 32 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 33 | #include <Aliases.h> |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 34 | #include <Folders.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 35 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 36 | #include <Resources.h> |
| 37 | #include <Memory.h> |
| 38 | #include <Events.h> |
| 39 | #include <Windows.h> |
| 40 | #include <Desk.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 41 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 42 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 43 | #include <Fonts.h> |
| 44 | #include <Menus.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 45 | #ifdef THINK_C |
| 46 | #include <OSEvents.h> /* For EvQElPtr */ |
| 47 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 49 | #ifndef HAVE_UNIVERSAL_HEADERS |
| 50 | #define GetResourceSizeOnDisk(x) SizeResource(x) |
| 51 | #endif |
| 52 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 53 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 54 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 55 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 56 | /* The alert for "No Python directory, where is it?" */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 57 | #define NOPYTHON_ALERT 128 |
| 58 | #define YES_ITEM 1 |
| 59 | #define NO_ITEM 2 |
| 60 | #define CURWD_ITEM 3 |
| 61 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 62 | /* The alert for "this is an applet template" */ |
| 63 | #define NOPYC_ALERT 129 |
| 64 | |
| 65 | /* The dialog for our GetDirectory call */ |
| 66 | #define GETDIR_ID 130 /* Resource ID for our "get directory" */ |
| 67 | #define SELECTCUR_ITEM 10 /* "Select current directory" button */ |
| 68 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 69 | #ifdef __MWERKS__ |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 70 | /* |
| 71 | ** With MW we can pass the event off to the console window, so |
| 72 | ** we might as well handle all events. |
| 73 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 74 | #include <SIOUX.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 75 | #define MAINLOOP_EVENTMASK everyEvent |
| 76 | #else |
| 77 | /* |
| 78 | ** For other compilers we're more careful, since we can't handle |
| 79 | ** things like updates (and they'll keep coming back if we don't |
| 80 | ** handle them) |
| 81 | */ |
| 82 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 83 | #endif /* __MWERKS__ */ |
| 84 | |
| 85 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 87 | /* 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] | 88 | ** with the python errors.h. */ |
| 89 | #define fnfErr -43 |
| 90 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 91 | /* Declared in macfsmodule.c: */ |
| 92 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
| 93 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 94 | /* Interrupt code variables: */ |
| 95 | static int interrupted; /* Set to true when cmd-. seen */ |
| 96 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 97 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 98 | /* |
| 99 | ** We attempt to be a good citizen by giving up the CPU periodically. |
| 100 | ** When in the foreground we do this less often and for shorter periods |
| 101 | ** than when in the background. At this time we also check for events and |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 102 | ** pass them off to SIOUX, if compiling with mwerks. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 103 | ** The counts here are in ticks of 1/60th second. |
| 104 | ** XXXX The initial values here are not based on anything. |
| 105 | ** FG-python gives up the cpu for 1/60th 5 times per second, |
| 106 | ** BG-python for .2 second 10 times per second. |
| 107 | */ |
| 108 | static long interval_fg = 12; |
| 109 | static long interval_bg = 6; |
| 110 | static long yield_fg = 1; |
| 111 | static long yield_bg = 12; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 112 | static long lastyield; |
| 113 | static int in_foreground; |
| 114 | |
| 115 | int PyMac_DoYieldEnabled = 1; /* Don't do eventloop when false */ |
| 116 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 117 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 118 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 119 | unsigned char * |
| 120 | Pstring(char *str) |
| 121 | { |
| 122 | static Str255 buf; |
| 123 | int len; |
| 124 | |
| 125 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 126 | if (len > 255) |
| 127 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 128 | buf[0] = (unsigned char)len; |
| 129 | strncpy((char *)buf+1, str, len); |
| 130 | return buf; |
| 131 | } |
| 132 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 133 | /* Like strerror() but for Mac OS error numbers */ |
| 134 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 135 | { |
| 136 | static char buf[256]; |
| 137 | Handle h; |
| 138 | char *str; |
| 139 | |
| 140 | h = GetResource('Estr', err); |
| 141 | if ( h ) { |
| 142 | HLock(h); |
| 143 | str = (char *)*h; |
| 144 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 145 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 146 | HUnlock(h); |
| 147 | ReleaseResource(h); |
| 148 | } else { |
| 149 | sprintf(buf, "Mac OS error code %d", err); |
| 150 | } |
| 151 | return buf; |
| 152 | } |
| 153 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 154 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 155 | PyObject *PyMac_OSErrException; |
| 156 | |
| 157 | /* Initialize and return PyMac_OSErrException */ |
| 158 | PyObject * |
| 159 | PyMac_GetOSErrException() |
| 160 | { |
| 161 | if (PyMac_OSErrException == NULL) |
| 162 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 163 | return PyMac_OSErrException; |
| 164 | } |
| 165 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 166 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 167 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 168 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 169 | { |
| 170 | char *msg; |
| 171 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 172 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 173 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 174 | Py_INCREF(Py_None); |
| 175 | return Py_None; |
| 176 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 177 | if (err == -1 && PyErr_Occurred()) |
| 178 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 179 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 180 | v = Py_BuildValue("(is)", err, msg); |
| 181 | PyErr_SetObject(eobj, v); |
| 182 | Py_DECREF(v); |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 186 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 187 | PyObject * |
| 188 | PyMac_Error(OSErr err) |
| 189 | { |
| 190 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 191 | } |
| 192 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 193 | /* The catcher routine (which may not be used for all compilers) */ |
| 194 | static RETSIGTYPE |
| 195 | intcatcher(sig) |
| 196 | int sig; |
| 197 | { |
| 198 | interrupted = 1; |
| 199 | signal(SIGINT, intcatcher); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | PyOS_InitInterrupts() |
| 204 | { |
| 205 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 206 | signal(SIGINT, intcatcher); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | ** This routine scans the event queue looking for cmd-. |
| 211 | ** This is the only way to get an interrupt under THINK (since it |
| 212 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 213 | ** the full-fledged event loop is disabled. This way, we can at least |
| 214 | ** interrupt a runaway python program. |
| 215 | */ |
| 216 | static void |
| 217 | scan_event_queue(flush) |
| 218 | int flush; |
| 219 | { |
| 220 | register EvQElPtr q; |
| 221 | |
| 222 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 223 | |
| 224 | for (; q; q = (EvQElPtr)q->qLink) { |
| 225 | if (q->evtQWhat == keyDown && |
| 226 | (char)q->evtQMessage == '.' && |
| 227 | (q->evtQModifiers & cmdKey) != 0) { |
| 228 | if ( flush ) |
| 229 | FlushEvents(keyDownMask, 0); |
| 230 | interrupted = 1; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | int |
| 237 | PyOS_InterruptOccurred() |
| 238 | { |
| 239 | #ifdef THINK_C |
| 240 | scan_event_queue(1); |
| 241 | #endif |
| 242 | PyMac_Yield(); |
| 243 | if (interrupted) { |
| 244 | interrupted = 0; |
| 245 | return 1; |
| 246 | } |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* intrpeek() is like intrcheck(), but it doesn't flush the events. The |
| 251 | ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return |
| 252 | ** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon |
| 253 | ** thereafter. |
| 254 | */ |
| 255 | static int |
| 256 | intrpeek() |
| 257 | { |
| 258 | #ifdef THINK_C |
| 259 | scan_event_queue(0); |
| 260 | #endif |
| 261 | return interrupted; |
| 262 | } |
| 263 | |
| 264 | /* Check whether we are in the foreground */ |
| 265 | int |
| 266 | PyMac_InForeground() |
| 267 | { |
| 268 | static ProcessSerialNumber ours; |
| 269 | static inited; |
| 270 | ProcessSerialNumber curfg; |
| 271 | Boolean eq; |
| 272 | |
| 273 | if ( inited == 0 ) |
| 274 | (void)GetCurrentProcess(&ours); |
| 275 | inited = 1; |
| 276 | if ( GetFrontProcess(&curfg) < 0 ) |
| 277 | eq = 1; |
| 278 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 279 | eq = 1; |
| 280 | return (int)eq; |
| 281 | |
| 282 | } |
| 283 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 284 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 285 | ** Set yield timeouts |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 286 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 287 | void |
| 288 | PyMac_SetYield(long fgi, long fgy, long bgi, long bgy) |
| 289 | { |
| 290 | interval_fg = fgi; |
| 291 | yield_fg = fgy; |
| 292 | interval_bg = bgi; |
| 293 | yield_bg = bgy; |
| 294 | } |
| 295 | |
| 296 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 297 | ** Handle an event, either one found in the mainloop eventhandler or |
| 298 | ** one passed back from the python program. |
| 299 | */ |
| 300 | void |
| 301 | PyMac_HandleEvent(evp) |
| 302 | EventRecord *evp; |
| 303 | { |
| 304 | WindowPtr wp; |
| 305 | |
| 306 | #ifdef __MWERKS__ |
| 307 | /* If SIOUX wants it we're done */ |
| 308 | (void)SIOUXHandleOneEvent(evp); |
| 309 | #else |
| 310 | /* Other compilers are just unlucky: we only weed out clicks in other applications */ |
| 311 | if ( evp->what == mouseDown ) { |
| 312 | if ( FindWindow(evp->where, &wp) == inSysWindow ) |
| 313 | SystemClick(evp, wp); |
| 314 | } |
| 315 | #endif /* !__MWERKS__ */ |
| 316 | } |
| 317 | |
| 318 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 319 | ** Yield the CPU to other tasks. |
| 320 | */ |
| 321 | static |
| 322 | PyMac_DoYield() |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 323 | { |
| 324 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 325 | long yield; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 326 | static int no_waitnextevent = -1; |
| 327 | int gotone; |
| 328 | |
| 329 | if ( no_waitnextevent < 0 ) { |
| 330 | no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) == |
| 331 | NGetTrapAddress(_Unimplemented, ToolTrap)); |
| 332 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 333 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 334 | if ( !PyMac_DoYieldEnabled ) { |
| 335 | #ifndef THINK_C |
| 336 | /* Under think this has been done before in intrcheck() or intrpeek() */ |
| 337 | scan_event_queue(0); |
| 338 | #endif |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 339 | return; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 340 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 341 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 342 | in_foreground = PyMac_InForeground(); |
| 343 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 344 | yield = yield_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 345 | else |
| 346 | yield = yield_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 347 | while ( 1 ) { |
| 348 | if ( no_waitnextevent ) { |
| 349 | SystemTask(); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 350 | gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 351 | } else { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 352 | gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 353 | } |
| 354 | /* Get out quickly if nothing interesting is happening */ |
| 355 | if ( !gotone || ev.what == nullEvent ) |
| 356 | break; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 357 | PyMac_HandleEvent(&ev); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 358 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 359 | lastyield = TickCount(); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | ** Yield the CPU to other tasks if opportune |
| 364 | */ |
| 365 | void |
| 366 | PyMac_Yield() { |
| 367 | long iv; |
| 368 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 369 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 370 | iv = interval_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 371 | else |
| 372 | iv = interval_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 373 | if ( TickCount() > lastyield + iv ) |
| 374 | PyMac_DoYield(); |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | ** Idle routine for busy-wait loops. |
| 379 | ** Gives up CPU, handles events and returns true if an interrupt is pending |
| 380 | ** (but not actually handled yet). |
| 381 | */ |
| 382 | int |
| 383 | PyMac_Idle() |
| 384 | { |
| 385 | PyMac_DoYield(); |
| 386 | return intrpeek(); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 389 | /* |
| 390 | ** Return the name of the Python directory |
| 391 | */ |
| 392 | char * |
| 393 | PyMac_GetPythonDir() |
| 394 | { |
| 395 | int item; |
| 396 | static char name[256]; |
| 397 | AliasHandle handle; |
| 398 | FSSpec dirspec; |
| 399 | int ok = 0, exists = 0; |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 400 | Boolean modified = 0, cannotmodify = 0; |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 401 | short oldrh, prefrh; |
| 402 | short prefdirRefNum; |
| 403 | long prefdirDirID; |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 404 | |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 405 | /* |
| 406 | ** Remember old resource file and try to open preferences file |
| 407 | ** in the preferences folder. If it doesn't exist we try to create |
| 408 | ** it. If anything fails here we limp on, but set cannotmodify so |
| 409 | ** we don't try to store things later on. |
| 410 | */ |
| 411 | oldrh = CurResFile(); |
| 412 | if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, |
| 413 | &prefdirDirID) != noErr ) { |
| 414 | /* Something wrong with preferences folder */ |
| 415 | cannotmodify = 1; |
| 416 | } else { |
| 417 | (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec); |
| 418 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 419 | if ( prefrh == -1 ) { |
| 420 | /* It doesn't exist. Try to create it */ |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 421 | FSpCreateResFile(&dirspec, 'PYTH', 'pref', 0); |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 422 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 423 | if ( prefrh == -1 ) { |
| 424 | cannotmodify = 1; |
| 425 | } else { |
| 426 | UseResFile(prefrh); |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | /* So, we've opened our preferences file, we hope. Look for the alias */ |
| 431 | handle = (AliasHandle)Get1Resource('alis', 128); |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 432 | if ( handle ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 433 | /* It exists. Resolve it (possibly updating it) */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 434 | if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr ) |
| 435 | ok = 1; |
| 436 | exists = 1; |
| 437 | } |
| 438 | if ( !ok ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 439 | /* No luck, so far. ask the user for help */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 440 | item = Alert(NOPYTHON_ALERT, NULL); |
| 441 | if ( item == YES_ITEM ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 442 | /* The user wants to point us to a directory. Let her do so */ |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 443 | ok = PyMac_GetDirectory(&dirspec); |
| 444 | if ( ok ) |
| 445 | modified = 1; |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 446 | } else if ( item == CURWD_ITEM ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 447 | /* The user told us the current directory is fine. Build an FSSpec for it */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 448 | if ( getwd(name) ) { |
| 449 | if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) { |
| 450 | ok = 1; |
| 451 | modified = 1; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 456 | if ( ok && modified && !cannotmodify) { |
| 457 | /* We have a new, valid fsspec and we can update the preferences file. Do so. */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 458 | if ( !exists ) { |
| 459 | if (NewAlias(NULL, &dirspec, &handle) == 0 ) |
| 460 | AddResource((Handle)handle, 'alis', 128, "\p"); |
| 461 | } else { |
| 462 | ChangedResource((Handle)handle); |
| 463 | } |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 464 | UpdateResFile(prefrh); |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 465 | } |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 466 | if ( !cannotmodify ) { |
| 467 | /* This means we have the resfile open. Close it. */ |
| 468 | CloseResFile(prefrh); |
| 469 | } |
| 470 | /* Back to the old resource file */ |
| 471 | UseResFile(oldrh); |
| 472 | /* Now turn the fsspec into a path to give back to our caller */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 473 | if ( ok ) { |
| 474 | ok = (nfullpath(&dirspec, name) == 0); |
| 475 | if ( ok ) strcat(name, ":"); |
| 476 | } |
| 477 | if ( !ok ) { |
| 478 | /* If all fails, we return the current directory */ |
| 479 | name[0] = 0; |
| 480 | (void)getwd(name); |
| 481 | } |
| 482 | return name; |
| 483 | } |
| 484 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 485 | /* |
| 486 | ** Returns true if the argument has a resource fork, and it contains |
| 487 | ** a 'PYC ' resource of the correct name |
| 488 | */ |
| 489 | int |
| 490 | PyMac_FindResourceModule(module, filename) |
| 491 | char *module; |
| 492 | char *filename; |
| 493 | { |
| 494 | FSSpec fss; |
| 495 | FInfo finfo; |
| 496 | short oldrh, filerh; |
| 497 | int ok; |
| 498 | Handle h; |
| 499 | |
| 500 | if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ) |
| 501 | return 0; /* It doesn't exist */ |
| 502 | if ( FSpGetFInfo(&fss, &finfo) != noErr ) |
| 503 | return 0; /* shouldn't happen, I guess */ |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 504 | oldrh = CurResFile(); |
| 505 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 506 | if ( filerh == -1 ) |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 507 | return 0; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 508 | UseResFile(filerh); |
| 509 | SetResLoad(0); |
| 510 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 511 | SetResLoad(1); |
| 512 | ok = (h != NULL); |
| 513 | CloseResFile(filerh); |
| 514 | UseResFile(oldrh); |
| 515 | return ok; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | ** Load the specified module from a resource |
| 520 | */ |
| 521 | PyObject * |
| 522 | PyMac_LoadResourceModule(module, filename) |
| 523 | char *module; |
| 524 | char *filename; |
| 525 | { |
| 526 | FSSpec fss; |
| 527 | FInfo finfo; |
| 528 | short oldrh, filerh; |
| 529 | int ok; |
| 530 | Handle h; |
| 531 | OSErr err; |
| 532 | PyObject *m, *co; |
| 533 | long num, size; |
| 534 | |
| 535 | if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr ) |
| 536 | goto error; |
| 537 | if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr ) |
| 538 | goto error; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 539 | oldrh = CurResFile(); |
| 540 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 541 | if ( filerh == -1 ) { |
| 542 | err = ResError(); |
| 543 | goto error; |
| 544 | } |
| 545 | UseResFile(filerh); |
| 546 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 547 | if ( h == NULL ) { |
| 548 | err = ResError(); |
| 549 | goto error; |
| 550 | } |
| 551 | HLock(h); |
| 552 | /* |
| 553 | ** XXXX The next few lines are intimately tied to the format of pyc |
| 554 | ** files. I'm not sure whether this code should be here or in import.c -- Jack |
| 555 | */ |
| 556 | size = GetHandleSize(h); |
| 557 | if ( size < 8 ) { |
| 558 | PyErr_SetString(PyExc_ImportError, "Resource too small"); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 559 | co = NULL; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 560 | } else { |
| 561 | num = (*h)[0] & 0xff; |
| 562 | num = num | (((*h)[1] & 0xff) << 8); |
| 563 | num = num | (((*h)[2] & 0xff) << 16); |
| 564 | num = num | (((*h)[3] & 0xff) << 24); |
| 565 | if ( num != PyImport_GetMagicNumber() ) { |
| 566 | PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource"); |
| 567 | co = NULL; |
| 568 | } else { |
| 569 | co = PyMarshal_ReadObjectFromString((*h)+8, size-8); |
| 570 | } |
| 571 | } |
| 572 | HUnlock(h); |
| 573 | CloseResFile(filerh); |
| 574 | UseResFile(oldrh); |
| 575 | if ( co ) { |
| 576 | m = PyImport_ExecCodeModule(module, co); |
| 577 | Py_DECREF(co); |
| 578 | } else { |
| 579 | m = NULL; |
| 580 | } |
| 581 | return m; |
| 582 | error: |
| 583 | { |
| 584 | char buf[512]; |
| 585 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 586 | sprintf(buf, "%s: %s", filename, PyMac_StrError(err)); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 587 | PyErr_SetString(PyExc_ImportError, buf); |
| 588 | return NULL; |
| 589 | } |
| 590 | } |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 591 | /* |
| 592 | ** Helper routine for GetDirectory |
| 593 | */ |
| 594 | static int |
| 595 | myhook_proc(item, theDialog, dataptr) |
| 596 | short item; |
| 597 | DialogPtr theDialog; |
| 598 | void *dataptr; |
| 599 | { |
| 600 | if ( item == SELECTCUR_ITEM ) { |
| 601 | item = sfItemCancelButton; |
| 602 | * (int *)dataptr = 1; |
| 603 | } |
| 604 | return item; |
| 605 | } |
| 606 | |
| 607 | /* |
| 608 | ** Ask the user for a directory. I still can't understand |
| 609 | ** why Apple doesn't provide a standard solution for this... |
| 610 | */ |
| 611 | int |
| 612 | PyMac_GetDirectory(dirfss) |
| 613 | FSSpec *dirfss; |
| 614 | { |
| 615 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 616 | static Point where = {-1, -1}; |
| 617 | static DlgHookYDUPP myhook_upp; |
| 618 | static int upp_inited = 0; |
| 619 | StandardFileReply reply; |
| 620 | int select_clicked = 0; |
| 621 | |
| 622 | if ( !upp_inited ) { |
| 623 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 624 | upp_inited = 1; |
| 625 | } |
| 626 | CustomGetFile((FileFilterUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
| 627 | NULL, NULL, NULL, (void *)&select_clicked); |
| 628 | |
| 629 | reply.sfFile.name[0] = 0; |
| 630 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 631 | return 0; |
| 632 | return select_clicked; |
| 633 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 634 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 635 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 636 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 637 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 638 | { |
| 639 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 640 | PyErr_SetString(PyExc_TypeError, |
| 641 | "OSType arg must be string of 4 chars"); |
| 642 | return 0; |
| 643 | } |
| 644 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 645 | return 1; |
| 646 | } |
| 647 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 648 | /* Convert an OSType value to a 4-char string object */ |
| 649 | PyObject * |
| 650 | PyMac_BuildOSType(OSType t) |
| 651 | { |
| 652 | return PyString_FromStringAndSize((char *)&t, 4); |
| 653 | } |
| 654 | |
| 655 | |
| 656 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 657 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 658 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 659 | { |
| 660 | int len; |
| 661 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 662 | PyErr_SetString(PyExc_TypeError, |
| 663 | "Str255 arg must be string of at most 255 chars"); |
| 664 | return 0; |
| 665 | } |
| 666 | pbuf[0] = len; |
| 667 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 668 | return 1; |
| 669 | } |
| 670 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 671 | /* Convert a Str255 to a Python string object */ |
| 672 | PyObject * |
| 673 | PyMac_BuildStr255(Str255 s) |
| 674 | { |
| 675 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 676 | } |
| 677 | |
| 678 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 679 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 680 | ** Convert a Python object to an FSSpec. |
| 681 | ** The object may either be a full pathname or a triple |
| 682 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 683 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 684 | ** The caller is responsible for not calling this routine |
| 685 | ** in those cases (which is fine, since everyone calling |
| 686 | ** this is probably sys7 dependent anyway). |
| 687 | */ |
| 688 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 689 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 690 | { |
| 691 | Str255 path; |
| 692 | short refnum; |
| 693 | long parid; |
| 694 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 695 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 696 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 697 | /* first check whether it already is an FSSpec */ |
| 698 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 699 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 700 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 701 | return 1; |
| 702 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 703 | if ( PyString_Check(v) ) { |
| 704 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 705 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 706 | return 0; |
| 707 | refnum = 0; /* XXXX Should get CurWD here... */ |
| 708 | parid = 0; |
| 709 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 710 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 711 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 712 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 713 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 714 | } |
| 715 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 716 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 717 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 718 | return 0; |
| 719 | } |
| 720 | return 1; |
| 721 | } |
| 722 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 723 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 724 | |
| 725 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 726 | The object must be a (left, top, right, bottom) tuple. |
| 727 | (This differs from the order in the struct but is consistent with |
| 728 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 729 | int |
| 730 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 731 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 732 | 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] | 733 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 734 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 735 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 736 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 737 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 738 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 739 | 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] | 740 | } |
| 741 | |
| 742 | |
| 743 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 744 | The object must be a (h, v) tuple. |
| 745 | (This differs from the order in the struct but is consistent with |
| 746 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 747 | int |
| 748 | PyMac_GetPoint(PyObject *v, Point *p) |
| 749 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 750 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | /* Convert a Point to a Python object */ |
| 754 | PyObject * |
| 755 | PyMac_BuildPoint(Point p) |
| 756 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 757 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | |
| 761 | /* Convert a Python object to an EventRecord. |
| 762 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 763 | int |
| 764 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 765 | { |
| 766 | return PyArg_Parse(v, "(hll(hh)h)", |
| 767 | &e->what, |
| 768 | &e->message, |
| 769 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 770 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 771 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 772 | &e->modifiers); |
| 773 | } |
| 774 | |
| 775 | /* Convert a Rect to an EventRecord object */ |
| 776 | PyObject * |
| 777 | PyMac_BuildEventRecord(EventRecord *e) |
| 778 | { |
| 779 | return Py_BuildValue("(hll(hh)h)", |
| 780 | e->what, |
| 781 | e->message, |
| 782 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 783 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 784 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 785 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 786 | } |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 787 | |
| 788 | |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 789 | /* ---------- */ |
| 790 | /* Applet support */ |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 791 | |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 792 | /* Run a compiled Python Python script from 'PYC ' resource __main__ */ |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 793 | static int |
| 794 | run_main_resource() |
| 795 | { |
| 796 | Handle h; |
| 797 | long size; |
| 798 | PyObject *code; |
| 799 | PyObject *result; |
| 800 | |
| 801 | h = GetNamedResource('PYC ', "\p__main__"); |
| 802 | if (h == NULL) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 803 | Alert(NOPYC_ALERT, NULL); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 804 | return 1; |
| 805 | } |
| 806 | size = GetResourceSizeOnDisk(h); |
| 807 | HLock(h); |
| 808 | code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8)); |
| 809 | HUnlock(h); |
| 810 | ReleaseResource(h); |
| 811 | if (code == NULL) { |
| 812 | PyErr_Print(); |
| 813 | return 1; |
| 814 | } |
| 815 | result = PyImport_ExecCodeModule("__main__", code); |
| 816 | Py_DECREF(code); |
| 817 | if (result == NULL) { |
| 818 | PyErr_Print(); |
| 819 | return 1; |
| 820 | } |
| 821 | Py_DECREF(result); |
| 822 | return 0; |
| 823 | } |
| 824 | |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 825 | /* Initialization sequence for applets */ |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 826 | void |
| 827 | PyMac_InitApplet() |
| 828 | { |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 829 | int argc; |
| 830 | char **argv; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 831 | |
| 832 | #ifdef __MWERKS__ |
| 833 | /* |
| 834 | ** Guido: you should fix this. You should arrange to have the |
| 835 | ** __sinit routine from macshlglue.c called so you can stuff |
| 836 | ** resources into the lib file. |
| 837 | */ |
| 838 | PyMac_AddLibResources(); |
| 839 | #else |
| 840 | KABOO! KABOO! |
| 841 | #endif |
| 842 | #ifdef __MWERKS__ |
| 843 | SIOUXSettings.asktosaveonclose = 0; |
| 844 | SIOUXSettings.showstatusline = 0; |
| 845 | SIOUXSettings.tabspaces = 4; |
| 846 | #endif |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 847 | argc = PyMac_GetArgv(&argv); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 848 | Py_Initialize(); |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 849 | PySys_SetArgv(argc, argv); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 850 | err = run_main_resource(); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 851 | fflush(stderr); |
| 852 | fflush(stdout); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 853 | #ifdef __MWERKS__ |
| 854 | if (!err) |
| 855 | SIOUXSettings.autocloseonquit = 1; |
| 856 | else |
| 857 | SIOUXSettings.showstatusline = 1; |
| 858 | #endif |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 859 | /* XXX Should we bother to Py_Exit(sts)? */ |
| 860 | } |