Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 24 | |
| 25 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 26 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 27 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 28 | #include "marshal.h" |
| 29 | #include "import.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 30 | |
| 31 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 32 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 33 | #include <Aliases.h> |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 34 | #include <Folders.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 35 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 36 | #include <Resources.h> |
| 37 | #include <Memory.h> |
| 38 | #include <Events.h> |
| 39 | #include <Windows.h> |
| 40 | #include <Desk.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 41 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 42 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 43 | #include <Fonts.h> |
| 44 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 45 | #include <TextUtils.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 46 | #ifdef THINK_C |
| 47 | #include <OSEvents.h> /* For EvQElPtr */ |
| 48 | #endif |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 49 | #ifdef __MWERKS__ |
| 50 | #include <SIOUX.h> |
| 51 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 53 | #ifndef HAVE_UNIVERSAL_HEADERS |
| 54 | #define GetResourceSizeOnDisk(x) SizeResource(x) |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 55 | typedef DlgHookYDProcPtr DlgHookYDUPP; |
| 56 | #define NewDlgHookYDProc(userRoutine) ((DlgHookYDUPP) (userRoutine)) |
| 57 | typedef FileFilterYDProcPtr FileFilterYDUPP; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 58 | #endif |
| 59 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 60 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 61 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 62 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 63 | /* The alert for "No Python directory, where is it?" */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 64 | #define NOPYTHON_ALERT 128 |
| 65 | #define YES_ITEM 1 |
| 66 | #define NO_ITEM 2 |
| 67 | #define CURWD_ITEM 3 |
| 68 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 69 | /* The alert for "this is an applet template" */ |
| 70 | #define NOPYC_ALERT 129 |
| 71 | |
| 72 | /* The dialog for our GetDirectory call */ |
| 73 | #define GETDIR_ID 130 /* Resource ID for our "get directory" */ |
| 74 | #define SELECTCUR_ITEM 10 /* "Select current directory" button */ |
| 75 | |
Jack Jansen | 292b0f9 | 1995-07-29 13:52:37 +0000 | [diff] [blame] | 76 | /* The dialog for interactive options */ |
| 77 | #define OPT_DIALOG 131 /* Resource ID for dialog */ |
| 78 | #define OPT_OK 1 |
| 79 | #define OPT_CANCEL 2 |
| 80 | #define OPT_INSPECT 3 |
| 81 | #define OPT_VERBOSE 4 |
| 82 | #define OPT_SUPPRESS 5 |
| 83 | #define OPT_UNBUFFERED 6 |
| 84 | #define OPT_DEBUGGING 7 |
| 85 | |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 86 | /* The STR# resource for sys.path initialization */ |
| 87 | #define PYTHONPATH_ID 128 |
| 88 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 89 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 90 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 91 | ** 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] | 92 | ** handle them). Note that we don't know who has windows open, so |
| 93 | ** 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] | 94 | */ |
| 95 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 96 | |
| 97 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 99 | /* 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] | 100 | ** with the python errors.h. */ |
| 101 | #define fnfErr -43 |
| 102 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 103 | /* Declared in macfsmodule.c: */ |
| 104 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
| 105 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 106 | /* Interrupt code variables: */ |
| 107 | static int interrupted; /* Set to true when cmd-. seen */ |
| 108 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 109 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 110 | /* |
| 111 | ** We attempt to be a good citizen by giving up the CPU periodically. |
| 112 | ** When in the foreground we do this less often and for shorter periods |
| 113 | ** 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] | 114 | ** pass them off to SIOUX, if compiling with mwerks. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 115 | ** The counts here are in ticks of 1/60th second. |
| 116 | ** XXXX The initial values here are not based on anything. |
| 117 | ** FG-python gives up the cpu for 1/60th 5 times per second, |
| 118 | ** BG-python for .2 second 10 times per second. |
| 119 | */ |
| 120 | static long interval_fg = 12; |
| 121 | static long interval_bg = 6; |
| 122 | static long yield_fg = 1; |
| 123 | static long yield_bg = 12; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 124 | static long lastyield; |
| 125 | static int in_foreground; |
| 126 | |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 127 | /* |
| 128 | ** When > 0, do full scanning for events (program is not event aware) |
| 129 | ** when == 0, only scan for Command-period |
| 130 | ** when < 0, don't do any event scanning |
| 131 | */ |
| 132 | int PyMac_DoYieldEnabled = 1; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 133 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 134 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 135 | unsigned char * |
| 136 | Pstring(char *str) |
| 137 | { |
| 138 | static Str255 buf; |
| 139 | int len; |
| 140 | |
| 141 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 142 | if (len > 255) |
| 143 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 144 | buf[0] = (unsigned char)len; |
| 145 | strncpy((char *)buf+1, str, len); |
| 146 | return buf; |
| 147 | } |
| 148 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 149 | /* Like strerror() but for Mac OS error numbers */ |
| 150 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 151 | { |
| 152 | static char buf[256]; |
| 153 | Handle h; |
| 154 | char *str; |
| 155 | |
| 156 | h = GetResource('Estr', err); |
| 157 | if ( h ) { |
| 158 | HLock(h); |
| 159 | str = (char *)*h; |
| 160 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 161 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 162 | HUnlock(h); |
| 163 | ReleaseResource(h); |
| 164 | } else { |
| 165 | sprintf(buf, "Mac OS error code %d", err); |
| 166 | } |
| 167 | return buf; |
| 168 | } |
| 169 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 170 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 171 | PyObject *PyMac_OSErrException; |
| 172 | |
| 173 | /* Initialize and return PyMac_OSErrException */ |
| 174 | PyObject * |
| 175 | PyMac_GetOSErrException() |
| 176 | { |
| 177 | if (PyMac_OSErrException == NULL) |
| 178 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 179 | return PyMac_OSErrException; |
| 180 | } |
| 181 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 182 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 183 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 184 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 185 | { |
| 186 | char *msg; |
| 187 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 189 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 190 | Py_INCREF(Py_None); |
| 191 | return Py_None; |
| 192 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 193 | if (err == -1 && PyErr_Occurred()) |
| 194 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 195 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 196 | v = Py_BuildValue("(is)", err, msg); |
| 197 | PyErr_SetObject(eobj, v); |
| 198 | Py_DECREF(v); |
| 199 | return NULL; |
| 200 | } |
| 201 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 202 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 203 | PyObject * |
| 204 | PyMac_Error(OSErr err) |
| 205 | { |
| 206 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 207 | } |
| 208 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 209 | /* The catcher routine (which may not be used for all compilers) */ |
| 210 | static RETSIGTYPE |
| 211 | intcatcher(sig) |
| 212 | int sig; |
| 213 | { |
| 214 | interrupted = 1; |
| 215 | signal(SIGINT, intcatcher); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | PyOS_InitInterrupts() |
| 220 | { |
| 221 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 222 | signal(SIGINT, intcatcher); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | ** This routine scans the event queue looking for cmd-. |
| 227 | ** This is the only way to get an interrupt under THINK (since it |
| 228 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 229 | ** the full-fledged event loop is disabled. This way, we can at least |
| 230 | ** interrupt a runaway python program. |
| 231 | */ |
| 232 | static void |
| 233 | scan_event_queue(flush) |
| 234 | int flush; |
| 235 | { |
Jack Jansen | f74f63a | 1995-06-27 13:18:14 +0000 | [diff] [blame] | 236 | #if defined(__MWERKS__) && defined(__CFM68K__) |
| 237 | return; /* No GetEvQHdr yet */ |
| 238 | #else |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 239 | register EvQElPtr q; |
| 240 | |
| 241 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 242 | |
| 243 | for (; q; q = (EvQElPtr)q->qLink) { |
| 244 | if (q->evtQWhat == keyDown && |
| 245 | (char)q->evtQMessage == '.' && |
| 246 | (q->evtQModifiers & cmdKey) != 0) { |
| 247 | if ( flush ) |
| 248 | FlushEvents(keyDownMask, 0); |
| 249 | interrupted = 1; |
| 250 | break; |
| 251 | } |
| 252 | } |
Jack Jansen | f74f63a | 1995-06-27 13:18:14 +0000 | [diff] [blame] | 253 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | int |
| 257 | PyOS_InterruptOccurred() |
| 258 | { |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 259 | if (PyMac_DoYieldEnabled < 0) |
| 260 | return 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 261 | #ifdef THINK_C |
| 262 | scan_event_queue(1); |
| 263 | #endif |
| 264 | PyMac_Yield(); |
| 265 | if (interrupted) { |
| 266 | interrupted = 0; |
| 267 | return 1; |
| 268 | } |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | /* intrpeek() is like intrcheck(), but it doesn't flush the events. The |
| 273 | ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return |
| 274 | ** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon |
| 275 | ** thereafter. |
| 276 | */ |
| 277 | static int |
| 278 | intrpeek() |
| 279 | { |
| 280 | #ifdef THINK_C |
| 281 | scan_event_queue(0); |
| 282 | #endif |
| 283 | return interrupted; |
| 284 | } |
| 285 | |
| 286 | /* Check whether we are in the foreground */ |
| 287 | int |
| 288 | PyMac_InForeground() |
| 289 | { |
| 290 | static ProcessSerialNumber ours; |
| 291 | static inited; |
| 292 | ProcessSerialNumber curfg; |
| 293 | Boolean eq; |
| 294 | |
| 295 | if ( inited == 0 ) |
| 296 | (void)GetCurrentProcess(&ours); |
| 297 | inited = 1; |
| 298 | if ( GetFrontProcess(&curfg) < 0 ) |
| 299 | eq = 1; |
| 300 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 301 | eq = 1; |
| 302 | return (int)eq; |
| 303 | |
| 304 | } |
| 305 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 306 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 307 | ** Set yield timeouts |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 308 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 309 | void |
| 310 | PyMac_SetYield(long fgi, long fgy, long bgi, long bgy) |
| 311 | { |
| 312 | interval_fg = fgi; |
| 313 | yield_fg = fgy; |
| 314 | interval_bg = bgi; |
| 315 | yield_bg = bgy; |
| 316 | } |
| 317 | |
| 318 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 319 | ** Handle an event, either one found in the mainloop eventhandler or |
| 320 | ** one passed back from the python program. |
| 321 | */ |
| 322 | void |
| 323 | PyMac_HandleEvent(evp) |
| 324 | EventRecord *evp; |
| 325 | { |
| 326 | WindowPtr wp; |
| 327 | |
| 328 | #ifdef __MWERKS__ |
| 329 | /* If SIOUX wants it we're done */ |
| 330 | (void)SIOUXHandleOneEvent(evp); |
| 331 | #else |
| 332 | /* Other compilers are just unlucky: we only weed out clicks in other applications */ |
| 333 | if ( evp->what == mouseDown ) { |
| 334 | if ( FindWindow(evp->where, &wp) == inSysWindow ) |
| 335 | SystemClick(evp, wp); |
| 336 | } |
| 337 | #endif /* !__MWERKS__ */ |
| 338 | } |
| 339 | |
| 340 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 341 | ** Yield the CPU to other tasks. |
| 342 | */ |
| 343 | static |
| 344 | PyMac_DoYield() |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 345 | { |
| 346 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 347 | long yield; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 348 | static int no_waitnextevent = -1; |
| 349 | int gotone; |
| 350 | |
| 351 | if ( no_waitnextevent < 0 ) { |
| 352 | no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) == |
| 353 | NGetTrapAddress(_Unimplemented, ToolTrap)); |
| 354 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 355 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 356 | if ( !PyMac_DoYieldEnabled ) { |
| 357 | #ifndef THINK_C |
| 358 | /* Under think this has been done before in intrcheck() or intrpeek() */ |
| 359 | scan_event_queue(0); |
| 360 | #endif |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 361 | return; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 362 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 363 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 364 | in_foreground = PyMac_InForeground(); |
| 365 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 366 | yield = yield_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 367 | else |
| 368 | yield = yield_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 369 | while ( 1 ) { |
| 370 | if ( no_waitnextevent ) { |
| 371 | SystemTask(); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 372 | gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 373 | } else { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 374 | gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 375 | } |
| 376 | /* Get out quickly if nothing interesting is happening */ |
| 377 | if ( !gotone || ev.what == nullEvent ) |
| 378 | break; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 379 | PyMac_HandleEvent(&ev); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 380 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 381 | lastyield = TickCount(); |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | ** Yield the CPU to other tasks if opportune |
| 386 | */ |
| 387 | void |
| 388 | PyMac_Yield() { |
| 389 | long iv; |
| 390 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 391 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 392 | iv = interval_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 393 | else |
| 394 | iv = interval_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 395 | if ( TickCount() > lastyield + iv ) |
| 396 | PyMac_DoYield(); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | ** Idle routine for busy-wait loops. |
| 401 | ** Gives up CPU, handles events and returns true if an interrupt is pending |
| 402 | ** (but not actually handled yet). |
| 403 | */ |
| 404 | int |
| 405 | PyMac_Idle() |
| 406 | { |
| 407 | PyMac_DoYield(); |
| 408 | return intrpeek(); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 411 | /* |
| 412 | ** Return the name of the Python directory |
| 413 | */ |
| 414 | char * |
| 415 | PyMac_GetPythonDir() |
| 416 | { |
| 417 | int item; |
| 418 | static char name[256]; |
| 419 | AliasHandle handle; |
| 420 | FSSpec dirspec; |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 421 | int ok = 0; |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 422 | Boolean modified = 0, cannotmodify = 0; |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 423 | short oldrh, prefrh; |
| 424 | short prefdirRefNum; |
| 425 | long prefdirDirID; |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 426 | |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 427 | /* |
| 428 | ** Remember old resource file and try to open preferences file |
| 429 | ** in the preferences folder. If it doesn't exist we try to create |
| 430 | ** it. If anything fails here we limp on, but set cannotmodify so |
| 431 | ** we don't try to store things later on. |
| 432 | */ |
| 433 | oldrh = CurResFile(); |
| 434 | if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, |
| 435 | &prefdirDirID) != noErr ) { |
| 436 | /* Something wrong with preferences folder */ |
| 437 | cannotmodify = 1; |
| 438 | } else { |
| 439 | (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec); |
| 440 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 441 | if ( prefrh == -1 ) { |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 442 | #ifdef USE_MAC_MODPREFS |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 443 | /* It doesn't exist. Try to create it */ |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 444 | FSpCreateResFile(&dirspec, 'PYTH', 'pref', 0); |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 445 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 446 | if ( prefrh == -1 ) { |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 447 | /* This is strange, what should we do now? */ |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 448 | cannotmodify = 1; |
| 449 | } else { |
| 450 | UseResFile(prefrh); |
| 451 | } |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 452 | #else |
| 453 | printf("Error: no Preferences file. Attempting to limp on...\n"); |
| 454 | name[0] = 0; |
| 455 | getwd(name); |
| 456 | return name; |
| 457 | #endif |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | /* So, we've opened our preferences file, we hope. Look for the alias */ |
| 461 | handle = (AliasHandle)Get1Resource('alis', 128); |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 462 | if ( handle ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 463 | /* It exists. Resolve it (possibly updating it) */ |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 464 | if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr ) { |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 465 | ok = 1; |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 466 | } |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 467 | } |
| 468 | if ( !ok ) { |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 469 | #ifdef USE_MAC_MODPREFS |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 470 | /* No luck, so far. ask the user for help */ |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 471 | item = Alert(NOPYTHON_ALERT, NULL); |
| 472 | if ( item == YES_ITEM ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 473 | /* The user wants to point us to a directory. Let her do so */ |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 474 | ok = PyMac_GetDirectory(&dirspec); |
| 475 | if ( ok ) |
| 476 | modified = 1; |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 477 | } else if ( item == CURWD_ITEM ) { |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 478 | /* 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] | 479 | if ( getwd(name) ) { |
| 480 | if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) { |
| 481 | ok = 1; |
| 482 | modified = 1; |
| 483 | } |
| 484 | } |
| 485 | } |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 486 | if ( handle ) { |
| 487 | /* Set the (old, invalid) alias record to the new data */ |
| 488 | UpdateAlias(NULL, &dirspec, handle, &modified); |
| 489 | } |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 490 | #else |
Jack Jansen | cf63693 | 1995-08-07 14:36:06 +0000 | [diff] [blame^] | 491 | printf("Error: corrupted Preferences file. Attempting to limp on...\n"); |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 492 | name[0] = 0; |
| 493 | getwd(name); |
| 494 | return name; |
| 495 | #endif |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 496 | } |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 497 | #ifdef USE_MAC_MODPREFS |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 498 | if ( ok && modified && !cannotmodify) { |
| 499 | /* We have a new, valid fsspec and we can update the preferences file. Do so. */ |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 500 | if ( !handle ) { |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 501 | if (NewAlias(NULL, &dirspec, &handle) == 0 ) |
| 502 | AddResource((Handle)handle, 'alis', 128, "\p"); |
| 503 | } else { |
| 504 | ChangedResource((Handle)handle); |
| 505 | } |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 506 | UpdateResFile(prefrh); |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 507 | } |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 508 | #endif |
Jack Jansen | 6cfab23 | 1995-02-13 22:46:00 +0000 | [diff] [blame] | 509 | if ( !cannotmodify ) { |
| 510 | /* This means we have the resfile open. Close it. */ |
| 511 | CloseResFile(prefrh); |
| 512 | } |
| 513 | /* Back to the old resource file */ |
| 514 | UseResFile(oldrh); |
| 515 | /* 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] | 516 | if ( ok ) { |
| 517 | ok = (nfullpath(&dirspec, name) == 0); |
| 518 | if ( ok ) strcat(name, ":"); |
| 519 | } |
| 520 | if ( !ok ) { |
| 521 | /* If all fails, we return the current directory */ |
| 522 | name[0] = 0; |
| 523 | (void)getwd(name); |
| 524 | } |
| 525 | return name; |
| 526 | } |
| 527 | |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 528 | #ifndef USE_BUILTIN_PATH |
| 529 | char * |
| 530 | PyMac_GetPythonPath(dir) |
| 531 | char *dir; |
| 532 | { |
| 533 | FSSpec dirspec; |
| 534 | short oldrh, prefrh = -1; |
| 535 | short prefdirRefNum; |
| 536 | long prefdirDirID; |
| 537 | char *rv; |
| 538 | int i, newlen; |
| 539 | Str255 pathitem; |
| 540 | |
| 541 | /* |
| 542 | ** Remember old resource file and try to open preferences file |
| 543 | ** in the preferences folder. |
| 544 | */ |
| 545 | oldrh = CurResFile(); |
| 546 | if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, |
| 547 | &prefdirDirID) == noErr ) { |
| 548 | (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec); |
| 549 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 550 | } |
| 551 | /* At this point, we may or may not have the preferences file open, and it |
| 552 | ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't |
| 553 | ** exist we use the one from the application (the default). |
| 554 | ** We put an initial '\n' in front of the path that we don't return to the caller |
| 555 | */ |
| 556 | if( (rv = malloc(2)) == NULL ) |
| 557 | goto out; |
| 558 | strcpy(rv, "\n"); |
| 559 | for(i=1; ; i++) { |
| 560 | GetIndString(pathitem, PYTHONPATH_ID, i); |
| 561 | if( pathitem[0] == 0 ) |
| 562 | break; |
| 563 | if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) { |
| 564 | /* We have to put the directory in place */ |
| 565 | newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2; |
| 566 | if( (rv=realloc(rv, newlen)) == NULL) |
| 567 | goto out; |
| 568 | strcat(rv, dir); |
| 569 | /* Skip a colon at the beginning of the item */ |
| 570 | if ( pathitem[0] > 9 && pathitem[1+9] == ':' ) { |
| 571 | memcpy(rv+strlen(rv), pathitem+1+10, pathitem[0]-10); |
| 572 | newlen--; |
| 573 | } else { |
| 574 | memcpy(rv+strlen(rv), pathitem+1+9, pathitem[0]-9); |
| 575 | } |
| 576 | rv[newlen-2] = '\n'; |
| 577 | rv[newlen-1] = 0; |
| 578 | } else { |
| 579 | /* Use as-is */ |
| 580 | newlen = strlen(rv) + (pathitem[0]) + 2; |
| 581 | if( (rv=realloc(rv, newlen)) == NULL) |
| 582 | goto out; |
| 583 | memcpy(rv+strlen(rv), pathitem+1, pathitem[0]); |
| 584 | rv[newlen-2] = '\n'; |
| 585 | rv[newlen-1] = 0; |
| 586 | } |
| 587 | } |
| 588 | if( strlen(rv) == 1) { |
| 589 | free(rv); |
| 590 | rv = NULL; |
| 591 | } |
| 592 | if ( rv ) { |
| 593 | rv[strlen(rv)-1] = 0; |
| 594 | rv++; |
| 595 | } |
| 596 | out: |
| 597 | if ( prefrh ) { |
| 598 | CloseResFile(prefrh); |
| 599 | UseResFile(oldrh); |
| 600 | } |
| 601 | return rv; |
| 602 | } |
| 603 | #endif /* !USE_BUILTIN_PATH */ |
| 604 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 605 | /* |
| 606 | ** Returns true if the argument has a resource fork, and it contains |
| 607 | ** a 'PYC ' resource of the correct name |
| 608 | */ |
| 609 | int |
| 610 | PyMac_FindResourceModule(module, filename) |
| 611 | char *module; |
| 612 | char *filename; |
| 613 | { |
| 614 | FSSpec fss; |
| 615 | FInfo finfo; |
| 616 | short oldrh, filerh; |
| 617 | int ok; |
| 618 | Handle h; |
| 619 | |
| 620 | if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ) |
| 621 | return 0; /* It doesn't exist */ |
| 622 | if ( FSpGetFInfo(&fss, &finfo) != noErr ) |
| 623 | return 0; /* shouldn't happen, I guess */ |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 624 | oldrh = CurResFile(); |
| 625 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 626 | if ( filerh == -1 ) |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 627 | return 0; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 628 | UseResFile(filerh); |
| 629 | SetResLoad(0); |
| 630 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 631 | SetResLoad(1); |
| 632 | ok = (h != NULL); |
| 633 | CloseResFile(filerh); |
| 634 | UseResFile(oldrh); |
| 635 | return ok; |
| 636 | } |
| 637 | |
| 638 | /* |
| 639 | ** Load the specified module from a resource |
| 640 | */ |
| 641 | PyObject * |
| 642 | PyMac_LoadResourceModule(module, filename) |
| 643 | char *module; |
| 644 | char *filename; |
| 645 | { |
| 646 | FSSpec fss; |
| 647 | FInfo finfo; |
| 648 | short oldrh, filerh; |
| 649 | int ok; |
| 650 | Handle h; |
| 651 | OSErr err; |
| 652 | PyObject *m, *co; |
| 653 | long num, size; |
| 654 | |
| 655 | if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr ) |
| 656 | goto error; |
| 657 | if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr ) |
| 658 | goto error; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 659 | oldrh = CurResFile(); |
| 660 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 661 | if ( filerh == -1 ) { |
| 662 | err = ResError(); |
| 663 | goto error; |
| 664 | } |
| 665 | UseResFile(filerh); |
| 666 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 667 | if ( h == NULL ) { |
| 668 | err = ResError(); |
| 669 | goto error; |
| 670 | } |
| 671 | HLock(h); |
| 672 | /* |
| 673 | ** XXXX The next few lines are intimately tied to the format of pyc |
| 674 | ** files. I'm not sure whether this code should be here or in import.c -- Jack |
| 675 | */ |
| 676 | size = GetHandleSize(h); |
| 677 | if ( size < 8 ) { |
| 678 | PyErr_SetString(PyExc_ImportError, "Resource too small"); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 679 | co = NULL; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 680 | } else { |
| 681 | num = (*h)[0] & 0xff; |
| 682 | num = num | (((*h)[1] & 0xff) << 8); |
| 683 | num = num | (((*h)[2] & 0xff) << 16); |
| 684 | num = num | (((*h)[3] & 0xff) << 24); |
| 685 | if ( num != PyImport_GetMagicNumber() ) { |
| 686 | PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource"); |
| 687 | co = NULL; |
| 688 | } else { |
| 689 | co = PyMarshal_ReadObjectFromString((*h)+8, size-8); |
| 690 | } |
| 691 | } |
| 692 | HUnlock(h); |
| 693 | CloseResFile(filerh); |
| 694 | UseResFile(oldrh); |
| 695 | if ( co ) { |
| 696 | m = PyImport_ExecCodeModule(module, co); |
| 697 | Py_DECREF(co); |
| 698 | } else { |
| 699 | m = NULL; |
| 700 | } |
| 701 | return m; |
| 702 | error: |
| 703 | { |
| 704 | char buf[512]; |
| 705 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 706 | sprintf(buf, "%s: %s", filename, PyMac_StrError(err)); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 707 | PyErr_SetString(PyExc_ImportError, buf); |
| 708 | return NULL; |
| 709 | } |
| 710 | } |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 711 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 712 | /* |
| 713 | ** Helper routine for GetDirectory |
| 714 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 715 | static pascal short |
| 716 | myhook_proc(short item, DialogPtr theDialog, void *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 717 | { |
| 718 | if ( item == SELECTCUR_ITEM ) { |
| 719 | item = sfItemCancelButton; |
| 720 | * (int *)dataptr = 1; |
| 721 | } |
| 722 | return item; |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | ** Ask the user for a directory. I still can't understand |
| 727 | ** why Apple doesn't provide a standard solution for this... |
| 728 | */ |
| 729 | int |
| 730 | PyMac_GetDirectory(dirfss) |
| 731 | FSSpec *dirfss; |
| 732 | { |
| 733 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 734 | static Point where = {-1, -1}; |
| 735 | static DlgHookYDUPP myhook_upp; |
| 736 | static int upp_inited = 0; |
| 737 | StandardFileReply reply; |
| 738 | int select_clicked = 0; |
| 739 | |
| 740 | if ( !upp_inited ) { |
| 741 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 742 | upp_inited = 1; |
| 743 | } |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 744 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 745 | NULL, NULL, NULL, (void *)&select_clicked); |
| 746 | |
| 747 | reply.sfFile.name[0] = 0; |
| 748 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 749 | return 0; |
| 750 | return select_clicked; |
| 751 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 752 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 753 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 754 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 755 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 756 | { |
| 757 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 758 | PyErr_SetString(PyExc_TypeError, |
| 759 | "OSType arg must be string of 4 chars"); |
| 760 | return 0; |
| 761 | } |
| 762 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 763 | return 1; |
| 764 | } |
| 765 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 766 | /* Convert an OSType value to a 4-char string object */ |
| 767 | PyObject * |
| 768 | PyMac_BuildOSType(OSType t) |
| 769 | { |
| 770 | return PyString_FromStringAndSize((char *)&t, 4); |
| 771 | } |
| 772 | |
| 773 | |
| 774 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 775 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 776 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 777 | { |
| 778 | int len; |
| 779 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 780 | PyErr_SetString(PyExc_TypeError, |
| 781 | "Str255 arg must be string of at most 255 chars"); |
| 782 | return 0; |
| 783 | } |
| 784 | pbuf[0] = len; |
| 785 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 786 | return 1; |
| 787 | } |
| 788 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 789 | /* Convert a Str255 to a Python string object */ |
| 790 | PyObject * |
| 791 | PyMac_BuildStr255(Str255 s) |
| 792 | { |
| 793 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 794 | } |
| 795 | |
| 796 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 797 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 798 | ** Convert a Python object to an FSSpec. |
| 799 | ** The object may either be a full pathname or a triple |
| 800 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 801 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 802 | ** The caller is responsible for not calling this routine |
| 803 | ** in those cases (which is fine, since everyone calling |
| 804 | ** this is probably sys7 dependent anyway). |
| 805 | */ |
| 806 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 807 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 808 | { |
| 809 | Str255 path; |
| 810 | short refnum; |
| 811 | long parid; |
| 812 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 813 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 814 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 815 | /* first check whether it already is an FSSpec */ |
| 816 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 817 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 818 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 819 | return 1; |
| 820 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 821 | if ( PyString_Check(v) ) { |
| 822 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 823 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 824 | return 0; |
| 825 | refnum = 0; /* XXXX Should get CurWD here... */ |
| 826 | parid = 0; |
| 827 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 828 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 829 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 830 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 831 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 832 | } |
| 833 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 834 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 835 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 836 | return 0; |
| 837 | } |
| 838 | return 1; |
| 839 | } |
| 840 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 841 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 842 | |
| 843 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 844 | The object must be a (left, top, right, bottom) tuple. |
| 845 | (This differs from the order in the struct but is consistent with |
| 846 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 847 | int |
| 848 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 849 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 850 | 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] | 851 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 852 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 853 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 854 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 855 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 856 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 857 | 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] | 858 | } |
| 859 | |
| 860 | |
| 861 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 862 | The object must be a (h, v) tuple. |
| 863 | (This differs from the order in the struct but is consistent with |
| 864 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 865 | int |
| 866 | PyMac_GetPoint(PyObject *v, Point *p) |
| 867 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 868 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* Convert a Point to a Python object */ |
| 872 | PyObject * |
| 873 | PyMac_BuildPoint(Point p) |
| 874 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 875 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | |
| 879 | /* Convert a Python object to an EventRecord. |
| 880 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 881 | int |
| 882 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 883 | { |
| 884 | return PyArg_Parse(v, "(hll(hh)h)", |
| 885 | &e->what, |
| 886 | &e->message, |
| 887 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 888 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 889 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 890 | &e->modifiers); |
| 891 | } |
| 892 | |
| 893 | /* Convert a Rect to an EventRecord object */ |
| 894 | PyObject * |
| 895 | PyMac_BuildEventRecord(EventRecord *e) |
| 896 | { |
| 897 | return Py_BuildValue("(hll(hh)h)", |
| 898 | e->what, |
| 899 | e->message, |
| 900 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 901 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 902 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 903 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 904 | } |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 905 | |
| 906 | |
Jack Jansen | 76efd8e | 1995-02-24 22:53:16 +0000 | [diff] [blame] | 907 | #ifdef USE_MAC_APPLET_SUPPORT |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 908 | /* Applet support */ |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 909 | |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 910 | /* Run a compiled Python Python script from 'PYC ' resource __main__ */ |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 911 | static int |
| 912 | run_main_resource() |
| 913 | { |
| 914 | Handle h; |
| 915 | long size; |
| 916 | PyObject *code; |
| 917 | PyObject *result; |
| 918 | |
| 919 | h = GetNamedResource('PYC ', "\p__main__"); |
| 920 | if (h == NULL) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 921 | Alert(NOPYC_ALERT, NULL); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 922 | return 1; |
| 923 | } |
| 924 | size = GetResourceSizeOnDisk(h); |
| 925 | HLock(h); |
| 926 | code = PyMarshal_ReadObjectFromString(*h + 8, (int)(size - 8)); |
| 927 | HUnlock(h); |
| 928 | ReleaseResource(h); |
| 929 | if (code == NULL) { |
| 930 | PyErr_Print(); |
| 931 | return 1; |
| 932 | } |
| 933 | result = PyImport_ExecCodeModule("__main__", code); |
| 934 | Py_DECREF(code); |
| 935 | if (result == NULL) { |
| 936 | PyErr_Print(); |
| 937 | return 1; |
| 938 | } |
| 939 | Py_DECREF(result); |
| 940 | return 0; |
| 941 | } |
| 942 | |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 943 | /* Initialization sequence for applets */ |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 944 | void |
| 945 | PyMac_InitApplet() |
| 946 | { |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 947 | int argc; |
| 948 | char **argv; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 949 | int err; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 950 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 951 | PyMac_AddLibResources(); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 952 | #ifdef __MWERKS__ |
| 953 | SIOUXSettings.asktosaveonclose = 0; |
| 954 | SIOUXSettings.showstatusline = 0; |
| 955 | SIOUXSettings.tabspaces = 4; |
| 956 | #endif |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 957 | argc = PyMac_GetArgv(&argv); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 958 | Py_Initialize(); |
Guido van Rossum | 8c89a6f | 1995-02-19 15:52:17 +0000 | [diff] [blame] | 959 | PySys_SetArgv(argc, argv); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 960 | err = run_main_resource(); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 961 | fflush(stderr); |
| 962 | fflush(stdout); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 963 | #ifdef __MWERKS__ |
| 964 | if (!err) |
| 965 | SIOUXSettings.autocloseonquit = 1; |
| 966 | else |
Jack Jansen | 292b0f9 | 1995-07-29 13:52:37 +0000 | [diff] [blame] | 967 | printf("\n[Terminated]\n"); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 968 | #endif |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 969 | /* XXX Should we bother to Py_Exit(sts)? */ |
| 970 | } |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 971 | |
Jack Jansen | 76efd8e | 1995-02-24 22:53:16 +0000 | [diff] [blame] | 972 | #endif /* USE_MAC_APPLET_SUPPORT */ |
| 973 | |
| 974 | /* For normal application */ |
| 975 | void |
| 976 | PyMac_InitApplication() |
| 977 | { |
| 978 | int argc; |
| 979 | char **argv; |
| 980 | |
| 981 | #ifdef USE_MAC_SHARED_LIBRARY |
| 982 | PyMac_AddLibResources(); |
| 983 | #endif |
| 984 | #ifdef __MWERKS__ |
| 985 | SIOUXSettings.asktosaveonclose = 0; |
| 986 | SIOUXSettings.showstatusline = 0; |
| 987 | SIOUXSettings.tabspaces = 4; |
| 988 | #endif |
| 989 | argc = PyMac_GetArgv(&argv); |
| 990 | if ( argc > 1 ) { |
| 991 | /* We're running a script. Attempt to change current directory */ |
| 992 | char curwd[256], *endp; |
| 993 | |
| 994 | strcpy(curwd, argv[1]); |
| 995 | endp = strrchr(curwd, ':'); |
| 996 | if ( endp && endp > curwd ) { |
| 997 | *endp = '\0'; |
Jack Jansen | 45ff77f | 1995-04-24 12:41:41 +0000 | [diff] [blame] | 998 | |
Jack Jansen | 76efd8e | 1995-02-24 22:53:16 +0000 | [diff] [blame] | 999 | chdir(curwd); |
| 1000 | } |
| 1001 | } |
| 1002 | Py_Main(argc, argv); |
| 1003 | } |
Jack Jansen | 292b0f9 | 1995-07-29 13:52:37 +0000 | [diff] [blame] | 1004 | |
| 1005 | /* |
| 1006 | ** PyMac_InteractiveOptions - Allow user to set options if option key is pressed |
| 1007 | */ |
| 1008 | void |
| 1009 | PyMac_InteractiveOptions(int *inspect, int *verbose, int *suppress_print, |
| 1010 | int *unbuffered, int *debugging) |
| 1011 | { |
| 1012 | KeyMap rmap; |
| 1013 | unsigned char *map; |
| 1014 | short item, type; |
| 1015 | ControlHandle handle; |
| 1016 | DialogPtr dialog; |
| 1017 | Rect rect; |
| 1018 | |
| 1019 | GetKeys(rmap); |
| 1020 | map = (unsigned char *)rmap; |
| 1021 | if ( ( map[0x3a>>3] & (1<<(0x3a&7)) ) == 0 ) /* option key is 3a */ |
| 1022 | return; |
| 1023 | |
| 1024 | dialog = GetNewDialog(OPT_DIALOG, NULL, (WindowPtr)-1); |
Jack Jansen | cf63693 | 1995-08-07 14:36:06 +0000 | [diff] [blame^] | 1025 | if ( dialog == NULL ) { |
| 1026 | printf("Option dialog not found - cannot set options\n"); |
Jack Jansen | 292b0f9 | 1995-07-29 13:52:37 +0000 | [diff] [blame] | 1027 | return; |
Jack Jansen | cf63693 | 1995-08-07 14:36:06 +0000 | [diff] [blame^] | 1028 | } |
Jack Jansen | 292b0f9 | 1995-07-29 13:52:37 +0000 | [diff] [blame] | 1029 | while (1) { |
| 1030 | handle = NULL; |
| 1031 | ModalDialog(NULL, &item); |
| 1032 | if ( item == OPT_OK ) |
| 1033 | break; |
| 1034 | if ( item == OPT_CANCEL ) { |
| 1035 | DisposDialog(dialog); |
| 1036 | exit(0); |
| 1037 | } |
| 1038 | #define OPT_ITEM(num, var) \ |
| 1039 | if ( item == (num) ) { \ |
| 1040 | *(var) = !*(var); \ |
| 1041 | GetDialogItem(dialog, (num), &type, (Handle *)&handle, &rect); \ |
| 1042 | SetCtlValue(handle, (short)*(var)); \ |
| 1043 | } |
| 1044 | |
| 1045 | OPT_ITEM(OPT_INSPECT, inspect); |
| 1046 | OPT_ITEM(OPT_VERBOSE, verbose); |
| 1047 | OPT_ITEM(OPT_SUPPRESS, suppress_print); |
| 1048 | OPT_ITEM(OPT_UNBUFFERED, unbuffered); |
| 1049 | OPT_ITEM(OPT_DEBUGGING, debugging); |
| 1050 | |
| 1051 | #undef OPT_ITEM |
| 1052 | } |
| 1053 | DisposDialog(dialog); |
Jack Jansen | 292b0f9 | 1995-07-29 13:52:37 +0000 | [diff] [blame] | 1054 | } |