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