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 | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 44 | #include "importdl.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 45 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 46 | #include "pythonresources.h" |
| 47 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 48 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 49 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 50 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 51 | #include <Resources.h> |
| 52 | #include <Memory.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 53 | #include <Windows.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 54 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 55 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 56 | #include <Fonts.h> |
| 57 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 58 | #include <TextUtils.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 59 | #ifdef THINK_C |
| 60 | #include <OSEvents.h> /* For EvQElPtr */ |
| 61 | #endif |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 62 | #ifdef __MWERKS__ |
| 63 | #include <SIOUX.h> |
| 64 | #endif |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 65 | #ifdef USE_GUSI |
| 66 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 67 | #include <LowMem.h> /* For SetSFCurDir, etc */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 68 | #include <GUSI.h> |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 69 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 70 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 71 | /* The ID of the Sioux apple menu */ |
| 72 | #define SIOUX_APPLEID 32000 |
| 73 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 74 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 75 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 76 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 77 | /* |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 78 | ** When less than this amount of stackspace is left we |
| 79 | ** raise a MemoryError. |
| 80 | */ |
| 81 | #ifndef MINIMUM_STACK_SIZE |
| 82 | #ifdef __powerc |
| 83 | #define MINIMUM_STACK_SIZE 8192 |
| 84 | #else |
| 85 | #define MINIMUM_STACK_SIZE 4096 |
| 86 | #endif |
| 87 | #endif |
| 88 | |
| 89 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 90 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 91 | ** things like updates (and they'll keep coming back if we don't |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 92 | ** handle them). Note that we don't know who has windows open, so |
| 93 | ** even handing updates off to SIOUX under MW isn't going to work. |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 94 | */ |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 95 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 96 | |
| 97 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 99 | /* XXX We should include Errors.h here, but it has a name conflict |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 100 | ** with the python errors.h. */ |
| 101 | #define fnfErr -43 |
| 102 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 103 | /* Declared in macfsmodule.c: */ |
| 104 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
| 105 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 106 | /* Interrupt code variables: */ |
| 107 | static int interrupted; /* Set to true when cmd-. seen */ |
| 108 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 109 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 110 | static int PyMac_DoYield Py_PROTO((int, int)); |
| 111 | static int PyMac_Yield Py_PROTO((void)); |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 112 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 113 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 114 | ** These are the real scheduling parameters that control what we check |
| 115 | ** in the event loop, and how often we check. The values are initialized |
| 116 | ** from pyMac_SchedParamStruct. |
| 117 | */ |
| 118 | |
| 119 | struct real_sched_param_struct { |
| 120 | int check_interrupt; /* if true check for command-dot */ |
| 121 | int process_events; /* if nonzero enable evt processing, this mask */ |
| 122 | int besocial; /* if nonzero be a little social with CPU */ |
| 123 | unsigned long check_interval; /* how often to check, in ticks */ |
| 124 | unsigned long bg_yield; /* yield so long when in background */ |
| 125 | /* these are computed from previous and clock and such */ |
| 126 | int enabled; /* check_interrupt OR process_event OR yield */ |
| 127 | unsigned long next_check; /* when to check/yield next, in ticks */ |
| 128 | }; |
| 129 | |
| 130 | static struct real_sched_param_struct schedparams = |
| 131 | { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0}; |
| 132 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 133 | /* |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 134 | ** Workaround for sioux/gusi combo: set when we are exiting |
| 135 | */ |
| 136 | int PyMac_ConsoleIsDead; |
| 137 | |
| 138 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 139 | ** Sioux menu bar, saved early so we can restore it |
| 140 | */ |
| 141 | static Handle sioux_mbar; |
| 142 | |
| 143 | /* |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 144 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 145 | */ |
| 146 | struct hook_args { |
| 147 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 148 | char *prompt; /* The prompt */ |
| 149 | }; |
| 150 | static DlgHookYDUPP myhook_upp; |
| 151 | static int upp_inited = 0; |
| 152 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 153 | /* |
| 154 | ** The python-code event handler |
| 155 | */ |
| 156 | static PyObject *python_event_handler; |
| 157 | |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 158 | #ifdef USE_GUSI |
| 159 | /* |
| 160 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 161 | ** the working directory. Hence, we call this routine after each call |
| 162 | ** to chdir() to rectify things. |
| 163 | */ |
| 164 | void |
| 165 | PyMac_FixGUSIcd() |
| 166 | { |
| 167 | WDPBRec pb; |
| 168 | FSSpec curdirfss; |
| 169 | |
| 170 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 171 | return; |
| 172 | |
| 173 | /* Set MacOS "working directory" */ |
| 174 | pb.ioNamePtr= "\p"; |
| 175 | pb.ioVRefNum= curdirfss.vRefNum; |
| 176 | pb.ioWDDirID= curdirfss.parID; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 177 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 178 | return; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 179 | } |
Jack Jansen | 7ac70af | 1996-08-19 11:01:05 +0000 | [diff] [blame] | 180 | |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 181 | /* |
| 182 | ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we |
| 183 | ** provide a dummy here. |
| 184 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 185 | void SpinCursor(short x) { /* Dummy */ } |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 186 | void RotateCursor(short x) { /* Dummy */ } |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 187 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 188 | /* |
| 189 | ** Replacement GUSI Spin function |
| 190 | */ |
| 191 | static int |
| 192 | PyMac_GUSISpin(spin_msg msg, long arg) |
| 193 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 194 | static Boolean inForeground = true; |
| 195 | int maxsleep = 6; /* 6 ticks is "normal" sleeptime */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 196 | |
| 197 | if (PyMac_ConsoleIsDead) return 0; |
| 198 | #if 0 |
| 199 | if (inForeground) |
| 200 | SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1); |
| 201 | #endif |
| 202 | |
| 203 | if (interrupted) return -1; |
| 204 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 205 | if ( msg == SP_AUTO_SPIN ) |
| 206 | maxsleep = 0; |
| 207 | if ( msg==SP_SLEEP||msg==SP_SELECT ) |
| 208 | maxsleep = arg; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 209 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 210 | PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | void |
| 216 | PyMac_SetGUSISpin() { |
| 217 | GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin); |
| 218 | } |
| 219 | |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 220 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 221 | void |
| 222 | PyMac_StopGUSISpin() { |
| 223 | PyMac_ConsoleIsDead = 1; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | ** Replacement routines for the PLstr... functions so we don't need |
| 228 | ** StdCLib. Moreover, that implementation is broken under cfm68k... |
| 229 | */ |
| 230 | void |
| 231 | PLstrcpy(to, fr) |
| 232 | unsigned char *to, *fr; |
| 233 | { |
| 234 | memcpy(to, fr, fr[0]+1); |
| 235 | } |
| 236 | |
| 237 | int |
| 238 | PLstrcmp(s1, s2) |
| 239 | unsigned char *s1, *s2; |
| 240 | { |
| 241 | int res; |
| 242 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 243 | |
| 244 | res = memcmp(s1+1, s2+1, l); |
| 245 | if ( res != 0 ) |
| 246 | return res; |
| 247 | |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 248 | if ( s1[0] < s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 249 | return -1; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 250 | else if ( s1[0] > s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 251 | return 1; |
| 252 | else |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | unsigned char * |
| 257 | PLstrrchr(str, chr) |
| 258 | unsigned char *str; |
| 259 | unsigned char chr; |
| 260 | { |
| 261 | unsigned char *ptr = 0; |
| 262 | unsigned char *p; |
| 263 | |
| 264 | for(p=str+1; p<str+str[0]; p++) |
| 265 | if ( *p == chr ) |
| 266 | ptr = p; |
| 267 | return ptr; |
| 268 | } |
| 269 | |
| 270 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 271 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 272 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 273 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 274 | unsigned char * |
| 275 | Pstring(char *str) |
| 276 | { |
| 277 | static Str255 buf; |
| 278 | int len; |
| 279 | |
| 280 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 281 | if (len > 255) |
| 282 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 283 | buf[0] = (unsigned char)len; |
| 284 | strncpy((char *)buf+1, str, len); |
| 285 | return buf; |
| 286 | } |
| 287 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 288 | /* Like strerror() but for Mac OS error numbers */ |
| 289 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 290 | { |
| 291 | static char buf[256]; |
| 292 | Handle h; |
| 293 | char *str; |
| 294 | |
| 295 | h = GetResource('Estr', err); |
| 296 | if ( h ) { |
| 297 | HLock(h); |
| 298 | str = (char *)*h; |
| 299 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 300 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 301 | HUnlock(h); |
| 302 | ReleaseResource(h); |
| 303 | } else { |
| 304 | sprintf(buf, "Mac OS error code %d", err); |
| 305 | } |
| 306 | return buf; |
| 307 | } |
| 308 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 309 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 310 | PyObject *PyMac_OSErrException; |
| 311 | |
| 312 | /* Initialize and return PyMac_OSErrException */ |
| 313 | PyObject * |
| 314 | PyMac_GetOSErrException() |
| 315 | { |
| 316 | if (PyMac_OSErrException == NULL) |
| 317 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 318 | return PyMac_OSErrException; |
| 319 | } |
| 320 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 321 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 322 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 323 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 324 | { |
| 325 | char *msg; |
| 326 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 328 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 329 | Py_INCREF(Py_None); |
| 330 | return Py_None; |
| 331 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 332 | if (err == -1 && PyErr_Occurred()) |
| 333 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 334 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 335 | v = Py_BuildValue("(is)", err, msg); |
| 336 | PyErr_SetObject(eobj, v); |
| 337 | Py_DECREF(v); |
| 338 | return NULL; |
| 339 | } |
| 340 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 341 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 342 | PyObject * |
| 343 | PyMac_Error(OSErr err) |
| 344 | { |
| 345 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 346 | } |
| 347 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 348 | #ifdef USE_STACKCHECK |
| 349 | /* Check for stack overflow */ |
| 350 | int |
| 351 | PyOS_CheckStack() |
| 352 | { |
| 353 | long left; |
| 354 | |
| 355 | left = StackSpace(); |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 356 | if ( left < MINIMUM_STACK_SIZE ) |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 357 | return -1; |
| 358 | return 0; |
| 359 | } |
| 360 | #endif /* USE_STACKCHECK */ |
| 361 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 362 | /* The catcher routine (which may not be used for all compilers) */ |
| 363 | static RETSIGTYPE |
| 364 | intcatcher(sig) |
| 365 | int sig; |
| 366 | { |
| 367 | interrupted = 1; |
| 368 | signal(SIGINT, intcatcher); |
| 369 | } |
| 370 | |
| 371 | void |
| 372 | PyOS_InitInterrupts() |
| 373 | { |
| 374 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 375 | signal(SIGINT, intcatcher); |
| 376 | } |
| 377 | |
Jack Jansen | a8441de | 1997-08-08 14:57:37 +0000 | [diff] [blame] | 378 | void |
| 379 | PyOS_FiniInterrupts() |
| 380 | { |
| 381 | } |
| 382 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 383 | /* |
| 384 | ** This routine scans the event queue looking for cmd-. |
| 385 | ** This is the only way to get an interrupt under THINK (since it |
| 386 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 387 | ** the full-fledged event loop is disabled. This way, we can at least |
| 388 | ** interrupt a runaway python program. |
| 389 | */ |
| 390 | static void |
| 391 | scan_event_queue(flush) |
| 392 | int flush; |
| 393 | { |
| 394 | register EvQElPtr q; |
| 395 | |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 396 | q = (EvQElPtr) LMGetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 397 | |
| 398 | for (; q; q = (EvQElPtr)q->qLink) { |
| 399 | if (q->evtQWhat == keyDown && |
| 400 | (char)q->evtQMessage == '.' && |
| 401 | (q->evtQModifiers & cmdKey) != 0) { |
| 402 | if ( flush ) |
| 403 | FlushEvents(keyDownMask, 0); |
| 404 | interrupted = 1; |
| 405 | break; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | int |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 411 | PyErr_CheckSignals() |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 412 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 413 | if (schedparams.enabled) { |
| 414 | if ( (unsigned long)LMGetTicks() > schedparams.next_check ) { |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 415 | if ( PyMac_Yield() < 0) |
| 416 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 417 | schedparams.next_check = (unsigned long)LMGetTicks() |
| 418 | + schedparams.check_interval; |
| 419 | if (interrupted) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 420 | scan_event_queue(1); /* Eat events up to cmd-. */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 421 | interrupted = 0; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 422 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 423 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 424 | } |
| 425 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 430 | int |
| 431 | PyOS_InterruptOccurred() |
| 432 | { |
| 433 | scan_event_queue(1); |
| 434 | return interrupted; |
| 435 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 436 | /* Check whether we are in the foreground */ |
| 437 | int |
| 438 | PyMac_InForeground() |
| 439 | { |
| 440 | static ProcessSerialNumber ours; |
| 441 | static inited; |
| 442 | ProcessSerialNumber curfg; |
| 443 | Boolean eq; |
| 444 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 445 | if ( inited == 0 ) { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 446 | (void)GetCurrentProcess(&ours); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 447 | inited = 1; |
| 448 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 449 | if ( GetFrontProcess(&curfg) < 0 ) |
| 450 | eq = 1; |
| 451 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 452 | eq = 1; |
| 453 | return (int)eq; |
| 454 | |
| 455 | } |
| 456 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 457 | int |
| 458 | PyMac_SetEventHandler(PyObject *evh) |
| 459 | { |
| 460 | if ( evh && python_event_handler ) { |
| 461 | PyErr_SetString(PyExc_RuntimeError, "Python event handler already set"); |
| 462 | return 0; |
| 463 | } |
| 464 | if ( python_event_handler ) |
| 465 | Py_DECREF(python_event_handler); |
| 466 | if ( evh ) |
| 467 | Py_INCREF(evh); |
| 468 | python_event_handler = evh; |
| 469 | return 1; |
| 470 | } |
| 471 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 472 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 473 | ** Handle an event, either one found in the mainloop eventhandler or |
| 474 | ** one passed back from the python program. |
| 475 | */ |
| 476 | void |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 477 | PyMac_HandleEventIntern(evp) |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 478 | EventRecord *evp; |
| 479 | { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 480 | if ( evp->what == mouseDown ) { |
| 481 | WindowPtr wp; |
| 482 | |
| 483 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
| 484 | SystemClick(evp, wp); |
| 485 | return; |
| 486 | } |
| 487 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 488 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 489 | { |
| 490 | int siouxdidit; |
| 491 | |
| 492 | /* If SIOUX wants it we're done */ |
| 493 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 494 | if ( siouxdidit ) |
| 495 | return; |
| 496 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 497 | #else |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 498 | /* Other compilers are just unlucky... */ |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 499 | #endif /* !__MWERKS__ */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
| 503 | ** Handle an event, either through HandleEvent or by passing it to the Python |
| 504 | ** event handler. |
| 505 | */ |
| 506 | int |
| 507 | PyMac_HandleEvent(evp) |
| 508 | EventRecord *evp; |
| 509 | { |
| 510 | PyObject *rv; |
| 511 | |
| 512 | if ( python_event_handler ) { |
| 513 | rv = PyObject_CallFunction(python_event_handler, "(O&)", |
| 514 | PyMac_BuildEventRecord, evp); |
| 515 | if ( rv ) |
| 516 | Py_DECREF(rv); |
| 517 | else |
| 518 | return -1; /* Propagate exception */ |
| 519 | } else { |
| 520 | PyMac_HandleEventIntern(evp); |
| 521 | } |
| 522 | return 0; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 526 | ** Yield the CPU to other tasks without processing events. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 527 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 528 | static int |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 529 | PyMac_DoYield(int maxsleep, int maycallpython) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 530 | { |
| 531 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 532 | int gotone; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 533 | long latest_time_ready; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 534 | static int in_here = 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 535 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 536 | in_here++; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 537 | /* |
| 538 | ** First check for interrupts, if wanted. |
| 539 | ** This sets a flag that will be picked up at an appropriate |
| 540 | ** moment in the mainloop. |
| 541 | */ |
| 542 | if (schedparams.check_interrupt) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 543 | scan_event_queue(0); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 544 | |
| 545 | /* XXXX Implementing an idle routine goes here */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 546 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 547 | /* |
| 548 | ** Check which of the eventloop cases we have: |
| 549 | ** - process events |
| 550 | ** - don't process events but do yield |
| 551 | ** - do neither |
| 552 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 553 | if( in_here > 1 || !schedparams.process_events || |
| 554 | (python_event_handler && !maycallpython) ) { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 555 | if ( maxsleep >= 0 ) { |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 556 | SystemTask(); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 557 | } |
| 558 | } else { |
| 559 | latest_time_ready = LMGetTicks() + maxsleep; |
| 560 | while ( maxsleep >= 0 ) { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 561 | /* XXXX Hack by Jack. |
| 562 | ** In time.sleep() you can click to another application |
| 563 | ** once only. If you come back to Python you cannot get away |
| 564 | ** again. |
| 565 | **/ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 566 | gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 567 | /* Get out quickly if nothing interesting is happening */ |
| 568 | if ( !gotone || ev.what == nullEvent ) |
| 569 | break; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 570 | if ( PyMac_HandleEvent(&ev) < 0 ) { |
| 571 | in_here--; |
| 572 | return -1; |
| 573 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 574 | maxsleep = latest_time_ready - LMGetTicks(); |
| 575 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 576 | } |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 577 | in_here--; |
| 578 | return 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 582 | ** Process events and/or yield the CPU to other tasks if opportune |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 583 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 584 | int |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 585 | PyMac_Yield() { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 586 | unsigned long maxsleep; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 587 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 588 | if( PyMac_InForeground() ) |
| 589 | maxsleep = 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 590 | else |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 591 | maxsleep = schedparams.bg_yield; |
| 592 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 593 | return PyMac_DoYield(maxsleep, 1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 597 | ** Return current scheduler parameters |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 598 | */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 599 | void |
| 600 | PyMac_GetSchedParams(PyMacSchedParams *sp) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 601 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 602 | sp->check_interrupt = schedparams.check_interrupt; |
| 603 | sp->process_events = schedparams.process_events; |
| 604 | sp->besocial = schedparams.besocial; |
| 605 | sp->check_interval = schedparams.check_interval / 60.0; |
| 606 | sp->bg_yield = schedparams.bg_yield / 60.0; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 607 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 608 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 609 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 610 | ** Set current scheduler parameters |
| 611 | */ |
| 612 | void |
| 613 | PyMac_SetSchedParams(PyMacSchedParams *sp) |
| 614 | { |
| 615 | schedparams.check_interrupt = sp->check_interrupt; |
| 616 | schedparams.process_events = sp->process_events; |
| 617 | schedparams.besocial = sp->besocial; |
| 618 | schedparams.check_interval = (unsigned long)(sp->check_interval*60); |
| 619 | schedparams.bg_yield = (unsigned long)(sp->bg_yield*60); |
| 620 | if ( schedparams.check_interrupt || schedparams.process_events || |
| 621 | schedparams.besocial ) |
| 622 | schedparams.enabled = 1; |
| 623 | else |
| 624 | schedparams.enabled = 0; |
| 625 | schedparams.next_check = 0; /* Check immedeately */ |
| 626 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 627 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 628 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 629 | ** Install our menu bar. |
| 630 | */ |
| 631 | void |
| 632 | PyMac_InitMenuBar() |
| 633 | { |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 634 | MenuHandle applemenu; |
| 635 | |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 636 | if ( (sioux_mbar=GetMenuBar()) == NULL ) { |
| 637 | /* Sioux menu not installed yet. Do so */ |
| 638 | SIOUXSetupMenus(); |
| 639 | if ( (sioux_mbar=GetMenuBar()) == NULL ) |
| 640 | return; |
| 641 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 642 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 643 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 644 | } |
| 645 | |
| 646 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 647 | ** Restore sioux menu bar |
| 648 | */ |
| 649 | void |
| 650 | PyMac_RestoreMenuBar() |
| 651 | { |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 652 | if ( sioux_mbar ) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 653 | SetMenuBar(sioux_mbar); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 654 | DrawMenuBar(); |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 655 | } else |
| 656 | PyMac_InitMenuBar(); |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 661 | ** Our replacement about box |
| 662 | */ |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 663 | |
| 664 | #include "patchlevel.h" |
| 665 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 666 | void |
| 667 | SIOUXDoAboutBox(void) |
| 668 | { |
| 669 | DialogPtr theDialog; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 670 | WindowPtr theWindow; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 671 | short item; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 672 | short fontID; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 673 | |
| 674 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 675 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 676 | theWindow = GetDialogWindow(theDialog); |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 677 | SetPortWindowPort(theWindow); |
| 678 | GetFNum("\pPython-Sans", &fontID); |
| 679 | if (fontID == 0) |
| 680 | fontID = kFontIDGeneva; |
| 681 | TextFont(fontID); |
| 682 | TextSize(9); |
| 683 | ParamText(Pstring(PATCHLEVEL), "\p", "\p", "\p"); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 684 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 685 | ModalDialog(NULL, &item); |
| 686 | DisposeDialog(theDialog); |
| 687 | } |
| 688 | |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 689 | #if 0 |
| 690 | int |
| 691 | PyMac_FileExists(char *name) |
| 692 | { |
| 693 | FSSpec fss; |
| 694 | |
| 695 | if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr ) |
| 696 | return 1; |
| 697 | return 0; |
| 698 | } |
| 699 | #endif |
| 700 | |
| 701 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 702 | ** Helper routine for GetDirectory |
| 703 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 704 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 705 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 706 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 707 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 708 | Handle prompth; |
| 709 | short type; |
| 710 | Rect rect; |
| 711 | |
| 712 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 713 | if ( prompth ) |
| 714 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 715 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 716 | if ( item == SELECTCUR_ITEM ) { |
| 717 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 718 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 719 | } |
| 720 | return item; |
| 721 | } |
| 722 | |
| 723 | /* |
| 724 | ** Ask the user for a directory. I still can't understand |
| 725 | ** why Apple doesn't provide a standard solution for this... |
| 726 | */ |
| 727 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 728 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 729 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 730 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 731 | { |
| 732 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 733 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 734 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 735 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 736 | |
| 737 | if ( !upp_inited ) { |
| 738 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 739 | upp_inited = 1; |
| 740 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 741 | if ( prompt && *prompt ) |
| 742 | hook_args.prompt = (char *)Pstring(prompt); |
| 743 | else |
| 744 | hook_args.prompt = NULL; |
| 745 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 746 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 747 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 748 | |
| 749 | reply.sfFile.name[0] = 0; |
| 750 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 751 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 752 | return hook_args.selectcur_hit; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 757 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 758 | StandardFileReply *reply, char *prompt) |
| 759 | { |
| 760 | static Point where = {-1, -1}; |
| 761 | struct hook_args hook_args; |
| 762 | |
| 763 | if ( !upp_inited ) { |
| 764 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 765 | upp_inited = 1; |
| 766 | } |
| 767 | if ( prompt && *prompt ) |
| 768 | hook_args.prompt = (char *)Pstring(prompt); |
| 769 | else |
| 770 | hook_args.prompt = NULL; |
| 771 | hook_args.selectcur_hit = 0; |
| 772 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 773 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 774 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 775 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 776 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 777 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 778 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 779 | { |
| 780 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 781 | PyErr_SetString(PyExc_TypeError, |
| 782 | "OSType arg must be string of 4 chars"); |
| 783 | return 0; |
| 784 | } |
| 785 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 786 | return 1; |
| 787 | } |
| 788 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 789 | /* Convert an OSType value to a 4-char string object */ |
| 790 | PyObject * |
| 791 | PyMac_BuildOSType(OSType t) |
| 792 | { |
| 793 | return PyString_FromStringAndSize((char *)&t, 4); |
| 794 | } |
| 795 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 796 | /* Convert an NumVersion value to a 4-element tuple */ |
| 797 | PyObject * |
| 798 | PyMac_BuildNumVersion(NumVersion t) |
| 799 | { |
| 800 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 801 | } |
| 802 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 803 | |
| 804 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 805 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 806 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 807 | { |
| 808 | int len; |
| 809 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 810 | PyErr_SetString(PyExc_TypeError, |
| 811 | "Str255 arg must be string of at most 255 chars"); |
| 812 | return 0; |
| 813 | } |
| 814 | pbuf[0] = len; |
| 815 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 816 | return 1; |
| 817 | } |
| 818 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 819 | /* Convert a Str255 to a Python string object */ |
| 820 | PyObject * |
| 821 | PyMac_BuildStr255(Str255 s) |
| 822 | { |
| 823 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 824 | } |
| 825 | |
| 826 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 827 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 828 | ** Convert a Python object to an FSSpec. |
| 829 | ** The object may either be a full pathname or a triple |
| 830 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 831 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 832 | ** The caller is responsible for not calling this routine |
| 833 | ** in those cases (which is fine, since everyone calling |
| 834 | ** this is probably sys7 dependent anyway). |
| 835 | */ |
| 836 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 837 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 838 | { |
| 839 | Str255 path; |
| 840 | short refnum; |
| 841 | long parid; |
| 842 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 843 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 844 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 845 | /* first check whether it already is an FSSpec */ |
| 846 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 847 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 848 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 849 | return 1; |
| 850 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 851 | if ( PyString_Check(v) ) { |
| 852 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 853 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 854 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 855 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 856 | parid = 0; |
| 857 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 858 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 859 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 860 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 861 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 862 | } |
| 863 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 864 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 865 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 866 | return 0; |
| 867 | } |
| 868 | return 1; |
| 869 | } |
| 870 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 871 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 872 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 873 | The object must be a (left, top, right, bottom) tuple. |
| 874 | (This differs from the order in the struct but is consistent with |
| 875 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 876 | int |
| 877 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 878 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 879 | 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] | 880 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 881 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 882 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 883 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 884 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 885 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 886 | 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] | 887 | } |
| 888 | |
| 889 | |
| 890 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 891 | The object must be a (h, v) tuple. |
| 892 | (This differs from the order in the struct but is consistent with |
| 893 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 894 | int |
| 895 | PyMac_GetPoint(PyObject *v, Point *p) |
| 896 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 897 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | /* Convert a Point to a Python object */ |
| 901 | PyObject * |
| 902 | PyMac_BuildPoint(Point p) |
| 903 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 904 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | |
| 908 | /* Convert a Python object to an EventRecord. |
| 909 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 910 | int |
| 911 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 912 | { |
| 913 | return PyArg_Parse(v, "(hll(hh)h)", |
| 914 | &e->what, |
| 915 | &e->message, |
| 916 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 917 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 918 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 919 | &e->modifiers); |
| 920 | } |
| 921 | |
| 922 | /* Convert a Rect to an EventRecord object */ |
| 923 | PyObject * |
| 924 | PyMac_BuildEventRecord(EventRecord *e) |
| 925 | { |
| 926 | return Py_BuildValue("(hll(hh)h)", |
| 927 | e->what, |
| 928 | e->message, |
| 929 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 930 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 931 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 932 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 933 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 934 | |
| 935 | /* Convert Python object to Fixed */ |
| 936 | int |
| 937 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 938 | { |
| 939 | double d; |
| 940 | |
| 941 | if( !PyArg_Parse(v, "d", &d)) |
| 942 | return 0; |
| 943 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 944 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /* Convert a Point to a Python object */ |
| 948 | PyObject * |
| 949 | PyMac_BuildFixed(Fixed f) |
| 950 | { |
| 951 | double d; |
| 952 | |
| 953 | d = f; |
| 954 | d = d / 0x10000; |
| 955 | return Py_BuildValue("d", d); |
| 956 | } |
| 957 | |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 958 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 959 | int |
| 960 | PyMac_Getwide(PyObject *v, wide *rv) |
| 961 | { |
| 962 | if (PyInt_Check(v)) { |
| 963 | rv->hi = 0; |
| 964 | rv->lo = PyInt_AsLong(v); |
| 965 | if( rv->lo & 0x80000000 ) |
| 966 | rv->hi = -1; |
| 967 | return 1; |
| 968 | } |
| 969 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 970 | } |
| 971 | |
| 972 | |
| 973 | PyObject * |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 974 | PyMac_Buildwide(wide *w) |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 975 | { |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 976 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 977 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 978 | return PyInt_FromLong(w->lo); |
| 979 | return Py_BuildValue("(ll)", w->hi, w->lo); |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 980 | } |