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