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" |
| 26 | #include "macglue.h" |
| 27 | |
| 28 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 29 | #include <Files.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 30 | #include <Resources.h> |
| 31 | #include <Memory.h> |
| 32 | #include <Events.h> |
| 33 | #include <Windows.h> |
| 34 | #include <Desk.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 35 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 36 | #include <Processes.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 37 | #ifdef THINK_C |
| 38 | #include <OSEvents.h> /* For EvQElPtr */ |
| 39 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 40 | |
| 41 | #include <signal.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 42 | |
| 43 | #ifdef __MWERKS__ |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 44 | /* |
| 45 | ** With MW we can pass the event off to the console window, so |
| 46 | ** we might as well handle all events. |
| 47 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 48 | #include <SIOUX.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 49 | #define MAINLOOP_EVENTMASK everyEvent |
| 50 | #else |
| 51 | /* |
| 52 | ** For other compilers we're more careful, since we can't handle |
| 53 | ** things like updates (and they'll keep coming back if we don't |
| 54 | ** handle them) |
| 55 | */ |
| 56 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 57 | #endif /* __MWERKS__ */ |
| 58 | |
| 59 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 61 | /* 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] | 62 | ** with the python errors.h. */ |
| 63 | #define fnfErr -43 |
| 64 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 65 | /* Declared in macfsmodule.c: */ |
| 66 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
| 67 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 68 | /* Interrupt code variables: */ |
| 69 | static int interrupted; /* Set to true when cmd-. seen */ |
| 70 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 71 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 72 | /* |
| 73 | ** We attempt to be a good citizen by giving up the CPU periodically. |
| 74 | ** When in the foreground we do this less often and for shorter periods |
| 75 | ** 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] | 76 | ** pass them off to SIOUX, if compiling with mwerks. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 77 | ** The counts here are in ticks of 1/60th second. |
| 78 | ** XXXX The initial values here are not based on anything. |
| 79 | ** FG-python gives up the cpu for 1/60th 5 times per second, |
| 80 | ** BG-python for .2 second 10 times per second. |
| 81 | */ |
| 82 | static long interval_fg = 12; |
| 83 | static long interval_bg = 6; |
| 84 | static long yield_fg = 1; |
| 85 | static long yield_bg = 12; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 86 | static long lastyield; |
| 87 | static int in_foreground; |
| 88 | |
| 89 | int PyMac_DoYieldEnabled = 1; /* Don't do eventloop when false */ |
| 90 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 91 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 92 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 93 | unsigned char * |
| 94 | Pstring(char *str) |
| 95 | { |
| 96 | static Str255 buf; |
| 97 | int len; |
| 98 | |
| 99 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 100 | if (len > 255) |
| 101 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 102 | buf[0] = (unsigned char)len; |
| 103 | strncpy((char *)buf+1, str, len); |
| 104 | return buf; |
| 105 | } |
| 106 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 107 | /* Replace strerror with something that might work */ |
| 108 | char *macstrerror(int err) |
| 109 | { |
| 110 | static char buf[256]; |
| 111 | Handle h; |
| 112 | char *str; |
| 113 | |
| 114 | h = GetResource('Estr', err); |
| 115 | if ( h ) { |
| 116 | HLock(h); |
| 117 | str = (char *)*h; |
| 118 | memcpy(buf, str+1, (unsigned char)str[0]); |
| 119 | HUnlock(h); |
| 120 | ReleaseResource(h); |
| 121 | } else { |
| 122 | sprintf(buf, "Mac OS error code %d", err); |
| 123 | } |
| 124 | return buf; |
| 125 | } |
| 126 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 127 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 128 | PyObject *PyMac_OSErrException; |
| 129 | |
| 130 | /* Initialize and return PyMac_OSErrException */ |
| 131 | PyObject * |
| 132 | PyMac_GetOSErrException() |
| 133 | { |
| 134 | if (PyMac_OSErrException == NULL) |
| 135 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 136 | return PyMac_OSErrException; |
| 137 | } |
| 138 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 139 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 140 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 141 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 142 | { |
| 143 | char *msg; |
| 144 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 146 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 147 | Py_INCREF(Py_None); |
| 148 | return Py_None; |
| 149 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 150 | if (err == -1 && PyErr_Occurred()) |
| 151 | return NULL; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 152 | msg = macstrerror(err); |
| 153 | v = Py_BuildValue("(is)", err, msg); |
| 154 | PyErr_SetObject(eobj, v); |
| 155 | Py_DECREF(v); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 159 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 160 | PyObject * |
| 161 | PyMac_Error(OSErr err) |
| 162 | { |
| 163 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 164 | } |
| 165 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 166 | /* The catcher routine (which may not be used for all compilers) */ |
| 167 | static RETSIGTYPE |
| 168 | intcatcher(sig) |
| 169 | int sig; |
| 170 | { |
| 171 | interrupted = 1; |
| 172 | signal(SIGINT, intcatcher); |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | PyOS_InitInterrupts() |
| 177 | { |
| 178 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 179 | signal(SIGINT, intcatcher); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | ** This routine scans the event queue looking for cmd-. |
| 184 | ** This is the only way to get an interrupt under THINK (since it |
| 185 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 186 | ** the full-fledged event loop is disabled. This way, we can at least |
| 187 | ** interrupt a runaway python program. |
| 188 | */ |
| 189 | static void |
| 190 | scan_event_queue(flush) |
| 191 | int flush; |
| 192 | { |
| 193 | register EvQElPtr q; |
| 194 | |
| 195 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 196 | |
| 197 | for (; q; q = (EvQElPtr)q->qLink) { |
| 198 | if (q->evtQWhat == keyDown && |
| 199 | (char)q->evtQMessage == '.' && |
| 200 | (q->evtQModifiers & cmdKey) != 0) { |
| 201 | if ( flush ) |
| 202 | FlushEvents(keyDownMask, 0); |
| 203 | interrupted = 1; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | PyOS_InterruptOccurred() |
| 211 | { |
| 212 | #ifdef THINK_C |
| 213 | scan_event_queue(1); |
| 214 | #endif |
| 215 | PyMac_Yield(); |
| 216 | if (interrupted) { |
| 217 | interrupted = 0; |
| 218 | return 1; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | /* intrpeek() is like intrcheck(), but it doesn't flush the events. The |
| 224 | ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return |
| 225 | ** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon |
| 226 | ** thereafter. |
| 227 | */ |
| 228 | static int |
| 229 | intrpeek() |
| 230 | { |
| 231 | #ifdef THINK_C |
| 232 | scan_event_queue(0); |
| 233 | #endif |
| 234 | return interrupted; |
| 235 | } |
| 236 | |
| 237 | /* Check whether we are in the foreground */ |
| 238 | int |
| 239 | PyMac_InForeground() |
| 240 | { |
| 241 | static ProcessSerialNumber ours; |
| 242 | static inited; |
| 243 | ProcessSerialNumber curfg; |
| 244 | Boolean eq; |
| 245 | |
| 246 | if ( inited == 0 ) |
| 247 | (void)GetCurrentProcess(&ours); |
| 248 | inited = 1; |
| 249 | if ( GetFrontProcess(&curfg) < 0 ) |
| 250 | eq = 1; |
| 251 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 252 | eq = 1; |
| 253 | return (int)eq; |
| 254 | |
| 255 | } |
| 256 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 257 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 258 | ** Set yield timeouts |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 259 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 260 | void |
| 261 | PyMac_SetYield(long fgi, long fgy, long bgi, long bgy) |
| 262 | { |
| 263 | interval_fg = fgi; |
| 264 | yield_fg = fgy; |
| 265 | interval_bg = bgi; |
| 266 | yield_bg = bgy; |
| 267 | } |
| 268 | |
| 269 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 270 | ** Handle an event, either one found in the mainloop eventhandler or |
| 271 | ** one passed back from the python program. |
| 272 | */ |
| 273 | void |
| 274 | PyMac_HandleEvent(evp) |
| 275 | EventRecord *evp; |
| 276 | { |
| 277 | WindowPtr wp; |
| 278 | |
| 279 | #ifdef __MWERKS__ |
| 280 | /* If SIOUX wants it we're done */ |
| 281 | (void)SIOUXHandleOneEvent(evp); |
| 282 | #else |
| 283 | /* Other compilers are just unlucky: we only weed out clicks in other applications */ |
| 284 | if ( evp->what == mouseDown ) { |
| 285 | if ( FindWindow(evp->where, &wp) == inSysWindow ) |
| 286 | SystemClick(evp, wp); |
| 287 | } |
| 288 | #endif /* !__MWERKS__ */ |
| 289 | } |
| 290 | |
| 291 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 292 | ** Yield the CPU to other tasks. |
| 293 | */ |
| 294 | static |
| 295 | PyMac_DoYield() |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 296 | { |
| 297 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 298 | long yield; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 299 | static int no_waitnextevent = -1; |
| 300 | int gotone; |
| 301 | |
| 302 | if ( no_waitnextevent < 0 ) { |
| 303 | no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) == |
| 304 | NGetTrapAddress(_Unimplemented, ToolTrap)); |
| 305 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 306 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 307 | if ( !PyMac_DoYieldEnabled ) { |
| 308 | #ifndef THINK_C |
| 309 | /* Under think this has been done before in intrcheck() or intrpeek() */ |
| 310 | scan_event_queue(0); |
| 311 | #endif |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 312 | return; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 313 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 314 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 315 | in_foreground = PyMac_InForeground(); |
| 316 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 317 | yield = yield_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 318 | else |
| 319 | yield = yield_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 320 | while ( 1 ) { |
| 321 | if ( no_waitnextevent ) { |
| 322 | SystemTask(); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 323 | gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 324 | } else { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 325 | gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 326 | } |
| 327 | /* Get out quickly if nothing interesting is happening */ |
| 328 | if ( !gotone || ev.what == nullEvent ) |
| 329 | break; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 330 | PyMac_HandleEvent(&ev); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 331 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 332 | lastyield = TickCount(); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | ** Yield the CPU to other tasks if opportune |
| 337 | */ |
| 338 | void |
| 339 | PyMac_Yield() { |
| 340 | long iv; |
| 341 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 342 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 343 | iv = interval_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 344 | else |
| 345 | iv = interval_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 346 | if ( TickCount() > lastyield + iv ) |
| 347 | PyMac_DoYield(); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | ** Idle routine for busy-wait loops. |
| 352 | ** Gives up CPU, handles events and returns true if an interrupt is pending |
| 353 | ** (but not actually handled yet). |
| 354 | */ |
| 355 | int |
| 356 | PyMac_Idle() |
| 357 | { |
| 358 | PyMac_DoYield(); |
| 359 | return intrpeek(); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 362 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 363 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 364 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 365 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 366 | { |
| 367 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 368 | PyErr_SetString(PyExc_TypeError, |
| 369 | "OSType arg must be string of 4 chars"); |
| 370 | return 0; |
| 371 | } |
| 372 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 373 | return 1; |
| 374 | } |
| 375 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 376 | /* Convert an OSType value to a 4-char string object */ |
| 377 | PyObject * |
| 378 | PyMac_BuildOSType(OSType t) |
| 379 | { |
| 380 | return PyString_FromStringAndSize((char *)&t, 4); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 385 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 386 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 387 | { |
| 388 | int len; |
| 389 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 390 | PyErr_SetString(PyExc_TypeError, |
| 391 | "Str255 arg must be string of at most 255 chars"); |
| 392 | return 0; |
| 393 | } |
| 394 | pbuf[0] = len; |
| 395 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 396 | return 1; |
| 397 | } |
| 398 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 399 | /* Convert a Str255 to a Python string object */ |
| 400 | PyObject * |
| 401 | PyMac_BuildStr255(Str255 s) |
| 402 | { |
| 403 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 404 | } |
| 405 | |
| 406 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 407 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 408 | ** Convert a Python object to an FSSpec. |
| 409 | ** The object may either be a full pathname or a triple |
| 410 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 411 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 412 | ** The caller is responsible for not calling this routine |
| 413 | ** in those cases (which is fine, since everyone calling |
| 414 | ** this is probably sys7 dependent anyway). |
| 415 | */ |
| 416 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 417 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 418 | { |
| 419 | Str255 path; |
| 420 | short refnum; |
| 421 | long parid; |
| 422 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 423 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 424 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 425 | /* first check whether it already is an FSSpec */ |
| 426 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 427 | if ( fs2 ) { |
| 428 | fs = fs2; |
| 429 | return 1; |
| 430 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 431 | if ( PyString_Check(v) ) { |
| 432 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 433 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 434 | return 0; |
| 435 | refnum = 0; /* XXXX Should get CurWD here... */ |
| 436 | parid = 0; |
| 437 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 438 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 439 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 440 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 441 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 442 | } |
| 443 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 444 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 445 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 446 | return 0; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 451 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 452 | |
| 453 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 454 | The object must be a (left, top, right, bottom) tuple. |
| 455 | (This differs from the order in the struct but is consistent with |
| 456 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 457 | int |
| 458 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 459 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 460 | 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] | 461 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 462 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 463 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 464 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 465 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 466 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 467 | 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] | 468 | } |
| 469 | |
| 470 | |
| 471 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 472 | The object must be a (h, v) tuple. |
| 473 | (This differs from the order in the struct but is consistent with |
| 474 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 475 | int |
| 476 | PyMac_GetPoint(PyObject *v, Point *p) |
| 477 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 478 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* Convert a Point to a Python object */ |
| 482 | PyObject * |
| 483 | PyMac_BuildPoint(Point p) |
| 484 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 485 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | |
| 489 | /* Convert a Python object to an EventRecord. |
| 490 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 491 | int |
| 492 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 493 | { |
| 494 | return PyArg_Parse(v, "(hll(hh)h)", |
| 495 | &e->what, |
| 496 | &e->message, |
| 497 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 498 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 499 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 500 | &e->modifiers); |
| 501 | } |
| 502 | |
| 503 | /* Convert a Rect to an EventRecord object */ |
| 504 | PyObject * |
| 505 | PyMac_BuildEventRecord(EventRecord *e) |
| 506 | { |
| 507 | return Py_BuildValue("(hll(hh)h)", |
| 508 | e->what, |
| 509 | e->message, |
| 510 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 511 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 512 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 513 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 514 | } |