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 | |
| 248 | if ( s1 < s2 ) |
| 249 | return -1; |
| 250 | else if ( s1 > s2 ) |
| 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 | */ |
| 663 | void |
| 664 | SIOUXDoAboutBox(void) |
| 665 | { |
| 666 | DialogPtr theDialog; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 667 | WindowRef theWindow; |
| 668 | CGrafPtr thePort; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 669 | short item; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 670 | short xpos, ypos, width, height, swidth, sheight; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 671 | |
| 672 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 673 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 674 | theWindow = GetDialogWindow(theDialog); |
| 675 | thePort = GetWindowPort(theWindow); |
| 676 | width = thePort->portRect.right - thePort->portRect.left; |
| 677 | height = thePort->portRect.bottom - thePort->portRect.top; |
| 678 | swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left; |
| 679 | sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight(); |
| 680 | xpos = (swidth-width)/2; |
Jack Jansen | 0e06e7e | 1996-09-23 15:51:06 +0000 | [diff] [blame] | 681 | ypos = (sheight-height)/5 + LMGetMBarHeight(); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 682 | MoveWindow(theWindow, xpos, ypos, 0); |
| 683 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 684 | ModalDialog(NULL, &item); |
| 685 | DisposeDialog(theDialog); |
| 686 | } |
| 687 | |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 688 | #if 0 |
| 689 | int |
| 690 | PyMac_FileExists(char *name) |
| 691 | { |
| 692 | FSSpec fss; |
| 693 | |
| 694 | if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr ) |
| 695 | return 1; |
| 696 | return 0; |
| 697 | } |
| 698 | #endif |
| 699 | |
| 700 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 701 | ** Helper routine for GetDirectory |
| 702 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 703 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 704 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 705 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 706 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 707 | Handle prompth; |
| 708 | short type; |
| 709 | Rect rect; |
| 710 | |
| 711 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 712 | if ( prompth ) |
| 713 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 714 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 715 | if ( item == SELECTCUR_ITEM ) { |
| 716 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 717 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 718 | } |
| 719 | return item; |
| 720 | } |
| 721 | |
| 722 | /* |
| 723 | ** Ask the user for a directory. I still can't understand |
| 724 | ** why Apple doesn't provide a standard solution for this... |
| 725 | */ |
| 726 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 727 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 728 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 729 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 730 | { |
| 731 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 732 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 733 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 734 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 735 | |
| 736 | if ( !upp_inited ) { |
| 737 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 738 | upp_inited = 1; |
| 739 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 740 | if ( prompt && *prompt ) |
| 741 | hook_args.prompt = (char *)Pstring(prompt); |
| 742 | else |
| 743 | hook_args.prompt = NULL; |
| 744 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 745 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 746 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 747 | |
| 748 | reply.sfFile.name[0] = 0; |
| 749 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 750 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 751 | return hook_args.selectcur_hit; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 756 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 757 | StandardFileReply *reply, char *prompt) |
| 758 | { |
| 759 | static Point where = {-1, -1}; |
| 760 | struct hook_args hook_args; |
| 761 | |
| 762 | if ( !upp_inited ) { |
| 763 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 764 | upp_inited = 1; |
| 765 | } |
| 766 | if ( prompt && *prompt ) |
| 767 | hook_args.prompt = (char *)Pstring(prompt); |
| 768 | else |
| 769 | hook_args.prompt = NULL; |
| 770 | hook_args.selectcur_hit = 0; |
| 771 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 772 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 773 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 774 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 775 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 776 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 777 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 778 | { |
| 779 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 780 | PyErr_SetString(PyExc_TypeError, |
| 781 | "OSType arg must be string of 4 chars"); |
| 782 | return 0; |
| 783 | } |
| 784 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 785 | return 1; |
| 786 | } |
| 787 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 788 | /* Convert an OSType value to a 4-char string object */ |
| 789 | PyObject * |
| 790 | PyMac_BuildOSType(OSType t) |
| 791 | { |
| 792 | return PyString_FromStringAndSize((char *)&t, 4); |
| 793 | } |
| 794 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 795 | /* Convert an NumVersion value to a 4-element tuple */ |
| 796 | PyObject * |
| 797 | PyMac_BuildNumVersion(NumVersion t) |
| 798 | { |
| 799 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 800 | } |
| 801 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 802 | |
| 803 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 804 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 805 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 806 | { |
| 807 | int len; |
| 808 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 809 | PyErr_SetString(PyExc_TypeError, |
| 810 | "Str255 arg must be string of at most 255 chars"); |
| 811 | return 0; |
| 812 | } |
| 813 | pbuf[0] = len; |
| 814 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 815 | return 1; |
| 816 | } |
| 817 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 818 | /* Convert a Str255 to a Python string object */ |
| 819 | PyObject * |
| 820 | PyMac_BuildStr255(Str255 s) |
| 821 | { |
| 822 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 823 | } |
| 824 | |
| 825 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 826 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 827 | ** Convert a Python object to an FSSpec. |
| 828 | ** The object may either be a full pathname or a triple |
| 829 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 830 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 831 | ** The caller is responsible for not calling this routine |
| 832 | ** in those cases (which is fine, since everyone calling |
| 833 | ** this is probably sys7 dependent anyway). |
| 834 | */ |
| 835 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 836 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 837 | { |
| 838 | Str255 path; |
| 839 | short refnum; |
| 840 | long parid; |
| 841 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 842 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 843 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 844 | /* first check whether it already is an FSSpec */ |
| 845 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 846 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 847 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 848 | return 1; |
| 849 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 850 | if ( PyString_Check(v) ) { |
| 851 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 852 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 853 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 854 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 855 | parid = 0; |
| 856 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 857 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 858 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 859 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 860 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 861 | } |
| 862 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 863 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 864 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 865 | return 0; |
| 866 | } |
| 867 | return 1; |
| 868 | } |
| 869 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 870 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 871 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 872 | The object must be a (left, top, right, bottom) tuple. |
| 873 | (This differs from the order in the struct but is consistent with |
| 874 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 875 | int |
| 876 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 877 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 878 | 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] | 879 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 880 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 881 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 882 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 883 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 884 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 885 | 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] | 886 | } |
| 887 | |
| 888 | |
| 889 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 890 | The object must be a (h, v) tuple. |
| 891 | (This differs from the order in the struct but is consistent with |
| 892 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 893 | int |
| 894 | PyMac_GetPoint(PyObject *v, Point *p) |
| 895 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 896 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* Convert a Point to a Python object */ |
| 900 | PyObject * |
| 901 | PyMac_BuildPoint(Point p) |
| 902 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 903 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
| 907 | /* Convert a Python object to an EventRecord. |
| 908 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 909 | int |
| 910 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 911 | { |
| 912 | return PyArg_Parse(v, "(hll(hh)h)", |
| 913 | &e->what, |
| 914 | &e->message, |
| 915 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 916 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 917 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 918 | &e->modifiers); |
| 919 | } |
| 920 | |
| 921 | /* Convert a Rect to an EventRecord object */ |
| 922 | PyObject * |
| 923 | PyMac_BuildEventRecord(EventRecord *e) |
| 924 | { |
| 925 | return Py_BuildValue("(hll(hh)h)", |
| 926 | e->what, |
| 927 | e->message, |
| 928 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 929 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 930 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 931 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 932 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 933 | |
| 934 | /* Convert Python object to Fixed */ |
| 935 | int |
| 936 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 937 | { |
| 938 | double d; |
| 939 | |
| 940 | if( !PyArg_Parse(v, "d", &d)) |
| 941 | return 0; |
| 942 | *f = (Fixed)(d * 0x10000); |
Jack Jansen | 31dd5c0 | 1996-05-31 13:01:39 +0000 | [diff] [blame] | 943 | return 1; |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* Convert a Point to a Python object */ |
| 947 | PyObject * |
| 948 | PyMac_BuildFixed(Fixed f) |
| 949 | { |
| 950 | double d; |
| 951 | |
| 952 | d = f; |
| 953 | d = d / 0x10000; |
| 954 | return Py_BuildValue("d", d); |
| 955 | } |
| 956 | |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 957 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 958 | int |
| 959 | PyMac_Getwide(PyObject *v, wide *rv) |
| 960 | { |
| 961 | if (PyInt_Check(v)) { |
| 962 | rv->hi = 0; |
| 963 | rv->lo = PyInt_AsLong(v); |
| 964 | if( rv->lo & 0x80000000 ) |
| 965 | rv->hi = -1; |
| 966 | return 1; |
| 967 | } |
| 968 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 969 | } |
| 970 | |
| 971 | |
| 972 | PyObject * |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 973 | PyMac_Buildwide(wide *w) |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 974 | { |
Jack Jansen | bbf4be2 | 1998-04-23 13:20:17 +0000 | [diff] [blame] | 975 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 976 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 977 | return PyInt_FromLong(w->lo); |
| 978 | return Py_BuildValue("(ll)", w->hi, w->lo); |
Jack Jansen | d58cd63 | 1998-04-21 15:24:39 +0000 | [diff] [blame] | 979 | } |