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