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