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; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 176 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 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 | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 218 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 219 | void |
| 220 | PyMac_StopGUSISpin() { |
| 221 | PyMac_ConsoleIsDead = 1; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | ** Replacement routines for the PLstr... functions so we don't need |
| 226 | ** StdCLib. Moreover, that implementation is broken under cfm68k... |
| 227 | */ |
| 228 | void |
| 229 | PLstrcpy(to, fr) |
| 230 | unsigned char *to, *fr; |
| 231 | { |
| 232 | memcpy(to, fr, fr[0]+1); |
| 233 | } |
| 234 | |
| 235 | int |
| 236 | PLstrcmp(s1, s2) |
| 237 | unsigned char *s1, *s2; |
| 238 | { |
| 239 | int res; |
| 240 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 241 | |
| 242 | res = memcmp(s1+1, s2+1, l); |
| 243 | if ( res != 0 ) |
| 244 | return res; |
| 245 | |
| 246 | if ( s1 < s2 ) |
| 247 | return -1; |
| 248 | else if ( s1 > s2 ) |
| 249 | return 1; |
| 250 | else |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | unsigned char * |
| 255 | PLstrrchr(str, chr) |
| 256 | unsigned char *str; |
| 257 | unsigned char chr; |
| 258 | { |
| 259 | unsigned char *ptr = 0; |
| 260 | unsigned char *p; |
| 261 | |
| 262 | for(p=str+1; p<str+str[0]; p++) |
| 263 | if ( *p == chr ) |
| 264 | ptr = p; |
| 265 | return ptr; |
| 266 | } |
| 267 | |
| 268 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 269 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 270 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 271 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 272 | unsigned char * |
| 273 | Pstring(char *str) |
| 274 | { |
| 275 | static Str255 buf; |
| 276 | int len; |
| 277 | |
| 278 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 279 | if (len > 255) |
| 280 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 281 | buf[0] = (unsigned char)len; |
| 282 | strncpy((char *)buf+1, str, len); |
| 283 | return buf; |
| 284 | } |
| 285 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 286 | /* Like strerror() but for Mac OS error numbers */ |
| 287 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 288 | { |
| 289 | static char buf[256]; |
| 290 | Handle h; |
| 291 | char *str; |
| 292 | |
| 293 | h = GetResource('Estr', err); |
| 294 | if ( h ) { |
| 295 | HLock(h); |
| 296 | str = (char *)*h; |
| 297 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 298 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 299 | HUnlock(h); |
| 300 | ReleaseResource(h); |
| 301 | } else { |
| 302 | sprintf(buf, "Mac OS error code %d", err); |
| 303 | } |
| 304 | return buf; |
| 305 | } |
| 306 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 307 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 308 | PyObject *PyMac_OSErrException; |
| 309 | |
| 310 | /* Initialize and return PyMac_OSErrException */ |
| 311 | PyObject * |
| 312 | PyMac_GetOSErrException() |
| 313 | { |
| 314 | if (PyMac_OSErrException == NULL) |
| 315 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 316 | return PyMac_OSErrException; |
| 317 | } |
| 318 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 319 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 320 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 321 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 322 | { |
| 323 | char *msg; |
| 324 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 326 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 327 | Py_INCREF(Py_None); |
| 328 | return Py_None; |
| 329 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 330 | if (err == -1 && PyErr_Occurred()) |
| 331 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 332 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 333 | v = Py_BuildValue("(is)", err, msg); |
| 334 | PyErr_SetObject(eobj, v); |
| 335 | Py_DECREF(v); |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 339 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 340 | PyObject * |
| 341 | PyMac_Error(OSErr err) |
| 342 | { |
| 343 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 344 | } |
| 345 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 346 | #ifdef USE_STACKCHECK |
| 347 | /* Check for stack overflow */ |
| 348 | int |
| 349 | PyOS_CheckStack() |
| 350 | { |
| 351 | long left; |
| 352 | |
| 353 | left = StackSpace(); |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 354 | if ( left < MINIMUM_STACK_SIZE ) |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 355 | return -1; |
| 356 | return 0; |
| 357 | } |
| 358 | #endif /* USE_STACKCHECK */ |
| 359 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 360 | /* The catcher routine (which may not be used for all compilers) */ |
| 361 | static RETSIGTYPE |
| 362 | intcatcher(sig) |
| 363 | int sig; |
| 364 | { |
| 365 | interrupted = 1; |
| 366 | signal(SIGINT, intcatcher); |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | PyOS_InitInterrupts() |
| 371 | { |
| 372 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 373 | signal(SIGINT, intcatcher); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | ** This routine scans the event queue looking for cmd-. |
| 378 | ** This is the only way to get an interrupt under THINK (since it |
| 379 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 380 | ** the full-fledged event loop is disabled. This way, we can at least |
| 381 | ** interrupt a runaway python program. |
| 382 | */ |
| 383 | static void |
| 384 | scan_event_queue(flush) |
| 385 | int flush; |
| 386 | { |
| 387 | register EvQElPtr q; |
| 388 | |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 389 | q = (EvQElPtr) GetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 390 | |
| 391 | for (; q; q = (EvQElPtr)q->qLink) { |
| 392 | if (q->evtQWhat == keyDown && |
| 393 | (char)q->evtQMessage == '.' && |
| 394 | (q->evtQModifiers & cmdKey) != 0) { |
| 395 | if ( flush ) |
| 396 | FlushEvents(keyDownMask, 0); |
| 397 | interrupted = 1; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
Jack Jansen | 7e94404 | 1997-02-20 15:23:18 +0000 | [diff] [blame] | 403 | #define TICKCOUNT 6 |
| 404 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 405 | int |
| 406 | PyOS_InterruptOccurred() |
| 407 | { |
Jack Jansen | 7e94404 | 1997-02-20 15:23:18 +0000 | [diff] [blame] | 408 | static unsigned long nextticktime; |
| 409 | unsigned long curticktime; |
| 410 | |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 411 | if (PyMac_DoYieldEnabled < 0) |
| 412 | return 0; |
Jack Jansen | 7e94404 | 1997-02-20 15:23:18 +0000 | [diff] [blame] | 413 | curticktime = (unsigned long)LMGetTicks(); |
| 414 | if ( curticktime < nextticktime ) |
| 415 | return 0; |
| 416 | nextticktime = curticktime + TICKCOUNT; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 417 | #ifdef THINK_C |
| 418 | scan_event_queue(1); |
| 419 | #endif |
| 420 | PyMac_Yield(); |
| 421 | if (interrupted) { |
| 422 | interrupted = 0; |
| 423 | return 1; |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /* intrpeek() is like intrcheck(), but it doesn't flush the events. The |
| 429 | ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return |
| 430 | ** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon |
| 431 | ** thereafter. |
| 432 | */ |
| 433 | static int |
| 434 | intrpeek() |
| 435 | { |
| 436 | #ifdef THINK_C |
| 437 | scan_event_queue(0); |
| 438 | #endif |
| 439 | return interrupted; |
| 440 | } |
| 441 | |
| 442 | /* Check whether we are in the foreground */ |
| 443 | int |
| 444 | PyMac_InForeground() |
| 445 | { |
| 446 | static ProcessSerialNumber ours; |
| 447 | static inited; |
| 448 | ProcessSerialNumber curfg; |
| 449 | Boolean eq; |
| 450 | |
| 451 | if ( inited == 0 ) |
| 452 | (void)GetCurrentProcess(&ours); |
| 453 | inited = 1; |
| 454 | if ( GetFrontProcess(&curfg) < 0 ) |
| 455 | eq = 1; |
| 456 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 457 | eq = 1; |
| 458 | return (int)eq; |
| 459 | |
| 460 | } |
| 461 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 462 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 463 | ** Set yield timeouts |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 464 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 465 | void |
| 466 | PyMac_SetYield(long fgi, long fgy, long bgi, long bgy) |
| 467 | { |
| 468 | interval_fg = fgi; |
| 469 | yield_fg = fgy; |
| 470 | interval_bg = bgi; |
| 471 | yield_bg = bgy; |
| 472 | } |
| 473 | |
| 474 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 475 | ** Handle an event, either one found in the mainloop eventhandler or |
| 476 | ** one passed back from the python program. |
| 477 | */ |
| 478 | void |
| 479 | PyMac_HandleEvent(evp) |
| 480 | EventRecord *evp; |
| 481 | { |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 482 | |
| 483 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 484 | { |
| 485 | int siouxdidit; |
| 486 | |
| 487 | /* If SIOUX wants it we're done */ |
| 488 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 489 | if ( siouxdidit ) |
| 490 | return; |
| 491 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 492 | #else |
| 493 | /* Other compilers are just unlucky: we only weed out clicks in other applications */ |
| 494 | if ( evp->what == mouseDown ) { |
Jack Jansen | 847e89e | 1995-08-31 13:57:40 +0000 | [diff] [blame] | 495 | WindowPtr wp; |
| 496 | |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 497 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 498 | SystemClick(evp, wp); |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 499 | return; |
| 500 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 501 | } |
| 502 | #endif /* !__MWERKS__ */ |
| 503 | } |
| 504 | |
| 505 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 506 | ** Yield the CPU to other tasks. |
| 507 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 508 | static void |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 509 | PyMac_DoYield(int maysleep) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 510 | { |
| 511 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 512 | long yield; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 513 | static int no_waitnextevent = -1; |
| 514 | int gotone; |
| 515 | |
| 516 | if ( no_waitnextevent < 0 ) { |
| 517 | no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) == |
| 518 | NGetTrapAddress(_Unimplemented, ToolTrap)); |
| 519 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 520 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 521 | lastyield = TickCount(); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 522 | #ifndef THINK_C |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 523 | /* Under think this has been done before in intrcheck() or intrpeek() */ |
| 524 | if (PyMac_DoYieldEnabled >= 0) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 525 | scan_event_queue(0); |
| 526 | #endif |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 527 | if (PyMac_DoYieldEnabled == 0) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 528 | return; |
| 529 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 530 | in_foreground = PyMac_InForeground(); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 531 | if ( maysleep ) { |
| 532 | if ( in_foreground ) |
| 533 | yield = yield_fg; |
| 534 | else |
| 535 | yield = yield_bg; |
| 536 | } else { |
| 537 | yield = 0; |
| 538 | } |
| 539 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 540 | while ( 1 ) { |
| 541 | if ( no_waitnextevent ) { |
| 542 | SystemTask(); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 543 | gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 544 | } else { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 545 | gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 546 | } |
| 547 | /* Get out quickly if nothing interesting is happening */ |
| 548 | if ( !gotone || ev.what == nullEvent ) |
| 549 | break; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 550 | PyMac_HandleEvent(&ev); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 551 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* |
| 555 | ** Yield the CPU to other tasks if opportune |
| 556 | */ |
| 557 | void |
| 558 | PyMac_Yield() { |
| 559 | long iv; |
| 560 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 561 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 562 | iv = interval_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 563 | else |
| 564 | iv = interval_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 565 | if ( TickCount() > lastyield + iv ) |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 566 | PyMac_DoYield(1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 569 | #ifdef USE_MACTCP |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 570 | /* |
| 571 | ** Idle routine for busy-wait loops. |
| 572 | ** Gives up CPU, handles events and returns true if an interrupt is pending |
| 573 | ** (but not actually handled yet). |
| 574 | */ |
| 575 | int |
| 576 | PyMac_Idle() |
| 577 | { |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 578 | PyMac_DoYield(1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 579 | return intrpeek(); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 580 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 581 | #endif |
| 582 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 583 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 584 | ** Install our menu bar. |
| 585 | */ |
| 586 | void |
| 587 | PyMac_InitMenuBar() |
| 588 | { |
| 589 | Handle bar; |
| 590 | MenuHandle applemenu; |
| 591 | |
| 592 | if ( (bar=GetMenuBar()) == NULL ) return; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 593 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 594 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | ** Our replacement about box |
| 599 | */ |
| 600 | void |
| 601 | SIOUXDoAboutBox(void) |
| 602 | { |
| 603 | DialogPtr theDialog; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 604 | WindowRef theWindow; |
| 605 | CGrafPtr thePort; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 606 | short item; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 607 | short xpos, ypos, width, height, swidth, sheight; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 608 | |
| 609 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 610 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 611 | theWindow = GetDialogWindow(theDialog); |
| 612 | thePort = GetWindowPort(theWindow); |
| 613 | width = thePort->portRect.right - thePort->portRect.left; |
| 614 | height = thePort->portRect.bottom - thePort->portRect.top; |
| 615 | swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left; |
| 616 | sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight(); |
| 617 | xpos = (swidth-width)/2; |
Jack Jansen | 0e06e7e | 1996-09-23 15:51:06 +0000 | [diff] [blame] | 618 | ypos = (sheight-height)/5 + LMGetMBarHeight(); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 619 | MoveWindow(theWindow, xpos, ypos, 0); |
| 620 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 621 | ModalDialog(NULL, &item); |
| 622 | DisposeDialog(theDialog); |
| 623 | } |
| 624 | |
| 625 | /* |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 626 | ** Returns true if the argument has a resource fork, and it contains |
| 627 | ** a 'PYC ' resource of the correct name |
| 628 | */ |
| 629 | int |
| 630 | PyMac_FindResourceModule(module, filename) |
| 631 | char *module; |
| 632 | char *filename; |
| 633 | { |
| 634 | FSSpec fss; |
| 635 | FInfo finfo; |
| 636 | short oldrh, filerh; |
| 637 | int ok; |
| 638 | Handle h; |
| 639 | |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 640 | if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) { |
| 641 | /* |
| 642 | ** Special case: the application itself. Use a shortcut to |
| 643 | ** forestall opening and closing the application numerous times |
| 644 | ** (which is dead slow when running from CDROM) |
| 645 | */ |
| 646 | oldrh = CurResFile(); |
| 647 | UseResFile(PyMac_AppRefNum); |
| 648 | filerh = -1; |
| 649 | } else { |
| 650 | if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ) |
| 651 | return 0; /* It doesn't exist */ |
| 652 | if ( FSpGetFInfo(&fss, &finfo) != noErr ) |
| 653 | return 0; /* shouldn't happen, I guess */ |
| 654 | oldrh = CurResFile(); |
| 655 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 656 | if ( filerh == -1 ) |
| 657 | return 0; |
| 658 | UseResFile(filerh); |
| 659 | } |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 660 | SetResLoad(0); |
| 661 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 662 | SetResLoad(1); |
| 663 | ok = (h != NULL); |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 664 | if ( filerh != -1 ) |
| 665 | CloseResFile(filerh); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 666 | UseResFile(oldrh); |
| 667 | return ok; |
| 668 | } |
| 669 | |
| 670 | /* |
| 671 | ** Load the specified module from a resource |
| 672 | */ |
| 673 | PyObject * |
| 674 | PyMac_LoadResourceModule(module, filename) |
| 675 | char *module; |
| 676 | char *filename; |
| 677 | { |
| 678 | FSSpec fss; |
| 679 | FInfo finfo; |
| 680 | short oldrh, filerh; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 681 | Handle h; |
| 682 | OSErr err; |
| 683 | PyObject *m, *co; |
| 684 | long num, size; |
| 685 | |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 686 | if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) { |
| 687 | /* |
| 688 | ** Special case: the application itself. Use a shortcut to |
| 689 | ** forestall opening and closing the application numerous times |
| 690 | ** (which is dead slow when running from CDROM) |
| 691 | */ |
| 692 | oldrh = CurResFile(); |
| 693 | UseResFile(PyMac_AppRefNum); |
| 694 | filerh = -1; |
| 695 | } else { |
| 696 | if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr ) |
| 697 | goto error; |
| 698 | if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr ) |
| 699 | goto error; |
| 700 | oldrh = CurResFile(); |
| 701 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 702 | if ( filerh == -1 ) { |
| 703 | err = ResError(); |
| 704 | goto error; |
| 705 | } |
| 706 | UseResFile(filerh); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 707 | } |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 708 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 709 | if ( h == NULL ) { |
| 710 | err = ResError(); |
| 711 | goto error; |
| 712 | } |
| 713 | HLock(h); |
| 714 | /* |
| 715 | ** XXXX The next few lines are intimately tied to the format of pyc |
| 716 | ** files. I'm not sure whether this code should be here or in import.c -- Jack |
| 717 | */ |
| 718 | size = GetHandleSize(h); |
| 719 | if ( size < 8 ) { |
| 720 | PyErr_SetString(PyExc_ImportError, "Resource too small"); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 721 | co = NULL; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 722 | } else { |
| 723 | num = (*h)[0] & 0xff; |
| 724 | num = num | (((*h)[1] & 0xff) << 8); |
| 725 | num = num | (((*h)[2] & 0xff) << 16); |
| 726 | num = num | (((*h)[3] & 0xff) << 24); |
| 727 | if ( num != PyImport_GetMagicNumber() ) { |
| 728 | PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource"); |
| 729 | co = NULL; |
| 730 | } else { |
| 731 | co = PyMarshal_ReadObjectFromString((*h)+8, size-8); |
| 732 | } |
| 733 | } |
| 734 | HUnlock(h); |
Jack Jansen | 8096daa | 1996-11-09 18:43:44 +0000 | [diff] [blame] | 735 | if ( filerh != -1 ) |
| 736 | CloseResFile(filerh); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 737 | UseResFile(oldrh); |
| 738 | if ( co ) { |
| 739 | m = PyImport_ExecCodeModule(module, co); |
| 740 | Py_DECREF(co); |
| 741 | } else { |
| 742 | m = NULL; |
| 743 | } |
| 744 | return m; |
| 745 | error: |
| 746 | { |
| 747 | char buf[512]; |
| 748 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 749 | sprintf(buf, "%s: %s", filename, PyMac_StrError(err)); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 750 | PyErr_SetString(PyExc_ImportError, buf); |
| 751 | return NULL; |
| 752 | } |
| 753 | } |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 754 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 755 | /* |
| 756 | ** Helper routine for GetDirectory |
| 757 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 758 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 759 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 760 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 761 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 762 | Handle prompth; |
| 763 | short type; |
| 764 | Rect rect; |
| 765 | |
| 766 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 767 | if ( prompth ) |
| 768 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 769 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 770 | if ( item == SELECTCUR_ITEM ) { |
| 771 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 772 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 773 | } |
| 774 | return item; |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | ** Ask the user for a directory. I still can't understand |
| 779 | ** why Apple doesn't provide a standard solution for this... |
| 780 | */ |
| 781 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 782 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 783 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 784 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 785 | { |
| 786 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 787 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 788 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 789 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 790 | |
| 791 | if ( !upp_inited ) { |
| 792 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 793 | upp_inited = 1; |
| 794 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 795 | if ( prompt && *prompt ) |
| 796 | hook_args.prompt = (char *)Pstring(prompt); |
| 797 | else |
| 798 | hook_args.prompt = NULL; |
| 799 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 800 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 801 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 802 | |
| 803 | reply.sfFile.name[0] = 0; |
| 804 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 805 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 806 | return hook_args.selectcur_hit; |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 811 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 812 | StandardFileReply *reply, char *prompt) |
| 813 | { |
| 814 | static Point where = {-1, -1}; |
| 815 | struct hook_args hook_args; |
| 816 | |
| 817 | if ( !upp_inited ) { |
| 818 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 819 | upp_inited = 1; |
| 820 | } |
| 821 | if ( prompt && *prompt ) |
| 822 | hook_args.prompt = (char *)Pstring(prompt); |
| 823 | else |
| 824 | hook_args.prompt = NULL; |
| 825 | hook_args.selectcur_hit = 0; |
| 826 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 827 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 828 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 829 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 830 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 831 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 832 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 833 | { |
| 834 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 835 | PyErr_SetString(PyExc_TypeError, |
| 836 | "OSType arg must be string of 4 chars"); |
| 837 | return 0; |
| 838 | } |
| 839 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 840 | return 1; |
| 841 | } |
| 842 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 843 | /* Convert an OSType value to a 4-char string object */ |
| 844 | PyObject * |
| 845 | PyMac_BuildOSType(OSType t) |
| 846 | { |
| 847 | return PyString_FromStringAndSize((char *)&t, 4); |
| 848 | } |
| 849 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 850 | /* Convert an NumVersion value to a 4-element tuple */ |
| 851 | PyObject * |
| 852 | PyMac_BuildNumVersion(NumVersion t) |
| 853 | { |
| 854 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 855 | } |
| 856 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 857 | |
| 858 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 859 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 860 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 861 | { |
| 862 | int len; |
| 863 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 864 | PyErr_SetString(PyExc_TypeError, |
| 865 | "Str255 arg must be string of at most 255 chars"); |
| 866 | return 0; |
| 867 | } |
| 868 | pbuf[0] = len; |
| 869 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 870 | return 1; |
| 871 | } |
| 872 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 873 | /* Convert a Str255 to a Python string object */ |
| 874 | PyObject * |
| 875 | PyMac_BuildStr255(Str255 s) |
| 876 | { |
| 877 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 878 | } |
| 879 | |
| 880 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 881 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 882 | ** Convert a Python object to an FSSpec. |
| 883 | ** The object may either be a full pathname or a triple |
| 884 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 885 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 886 | ** The caller is responsible for not calling this routine |
| 887 | ** in those cases (which is fine, since everyone calling |
| 888 | ** this is probably sys7 dependent anyway). |
| 889 | */ |
| 890 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 891 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 892 | { |
| 893 | Str255 path; |
| 894 | short refnum; |
| 895 | long parid; |
| 896 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 897 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 898 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 899 | /* first check whether it already is an FSSpec */ |
| 900 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 901 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 902 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 903 | return 1; |
| 904 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 905 | if ( PyString_Check(v) ) { |
| 906 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 907 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 908 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 909 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 910 | parid = 0; |
| 911 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 912 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 913 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 914 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 915 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 916 | } |
| 917 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 918 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 919 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | } |
| 922 | return 1; |
| 923 | } |
| 924 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 925 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 926 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 927 | The object must be a (left, top, right, bottom) tuple. |
| 928 | (This differs from the order in the struct but is consistent with |
| 929 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 930 | int |
| 931 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 932 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 933 | 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] | 934 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 935 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 936 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 937 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 938 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 939 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 940 | 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] | 941 | } |
| 942 | |
| 943 | |
| 944 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 945 | The object must be a (h, v) tuple. |
| 946 | (This differs from the order in the struct but is consistent with |
| 947 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 948 | int |
| 949 | PyMac_GetPoint(PyObject *v, Point *p) |
| 950 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 951 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* Convert a Point to a Python object */ |
| 955 | PyObject * |
| 956 | PyMac_BuildPoint(Point p) |
| 957 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 958 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
| 962 | /* Convert a Python object to an EventRecord. |
| 963 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 964 | int |
| 965 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 966 | { |
| 967 | return PyArg_Parse(v, "(hll(hh)h)", |
| 968 | &e->what, |
| 969 | &e->message, |
| 970 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 971 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 972 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 973 | &e->modifiers); |
| 974 | } |
| 975 | |
| 976 | /* Convert a Rect to an EventRecord object */ |
| 977 | PyObject * |
| 978 | PyMac_BuildEventRecord(EventRecord *e) |
| 979 | { |
| 980 | return Py_BuildValue("(hll(hh)h)", |
| 981 | e->what, |
| 982 | e->message, |
| 983 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 984 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 985 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 986 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 987 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 988 | |
| 989 | /* Convert Python object to Fixed */ |
| 990 | int |
| 991 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 992 | { |
| 993 | double d; |
| 994 | |
| 995 | if( !PyArg_Parse(v, "d", &d)) |
| 996 | return 0; |
| 997 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 998 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* Convert a Point to a Python object */ |
| 1002 | PyObject * |
| 1003 | PyMac_BuildFixed(Fixed f) |
| 1004 | { |
| 1005 | double d; |
| 1006 | |
| 1007 | d = f; |
| 1008 | d = d / 0x10000; |
| 1009 | return Py_BuildValue("d", d); |
| 1010 | } |
| 1011 | |