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