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 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 26 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 27 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 28 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 29 | #include "marshal.h" |
| 30 | #include "import.h" |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 31 | #include "importdl.h" |
Jack Jansen | 8bb1dc1 | 2001-05-19 12:31:09 +0000 | [diff] [blame] | 32 | #include "pymactoolbox.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 33 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 34 | #include "pythonresources.h" |
| 35 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 36 | #ifdef WITHOUT_FRAMEWORKS |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 37 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 38 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 39 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 40 | #include <Resources.h> |
| 41 | #include <Memory.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 42 | #include <Windows.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 43 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 44 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 45 | #include <Fonts.h> |
| 46 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 47 | #include <TextUtils.h> |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 48 | #include <LowMem.h> |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 49 | #include <Events.h> |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 50 | #else |
| 51 | #include <Carbon/Carbon.h> |
| 52 | #endif |
| 53 | |
| 54 | #if !TARGET_API_MAC_OS8 |
| 55 | /* Unfortunately this call is probably slower... */ |
| 56 | #define LMGetTicks() TickCount() |
| 57 | #endif |
| 58 | |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 59 | #ifdef __MWERKS__ |
| 60 | #include <SIOUX.h> |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 61 | extern void SIOUXSetupMenus(void); |
| 62 | extern void SIOUXDoAboutBox(void); |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 63 | #endif |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 64 | #ifdef USE_GUSI |
| 65 | /* Functions we redefine because they're in obscure libraries */ |
| 66 | extern void SpinCursor(short x); |
| 67 | extern void RotateCursor(short x); |
| 68 | extern pascal void PLstrcpy(unsigned char *, unsigned char *); |
| 69 | extern pascal int PLstrcmp(unsigned char *, unsigned char *); |
| 70 | extern pascal unsigned char *PLstrrchr(unsigned char *, unsigned char); |
| 71 | |
| 72 | #endif |
| 73 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 74 | #ifdef USE_GUSI1 |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 75 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 76 | #include <GUSI.h> |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 77 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 78 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 79 | /* The ID of the Sioux apple menu */ |
| 80 | #define SIOUX_APPLEID 32000 |
| 81 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 82 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 83 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 84 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 85 | /* |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 86 | ** When less than this amount of stackspace is left we |
| 87 | ** raise a MemoryError. |
| 88 | */ |
| 89 | #ifndef MINIMUM_STACK_SIZE |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 90 | #define MINIMUM_STACK_SIZE 8192 |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 91 | #endif |
| 92 | |
Jack Jansen | 340eb88 | 2001-02-02 22:40:28 +0000 | [diff] [blame] | 93 | #if TARGET_API_MAC_CARBON |
| 94 | /* |
| 95 | ** On MacOSX StackSpace() lies: it gives the distance from heap end to stack pointer, |
| 96 | ** but the stack cannot grow that far due to rlimit values. We cannot get at this value |
| 97 | ** from Carbon, so we set a maximum to the stack here that is based on the default |
| 98 | ** stack limit of 512K. |
| 99 | */ |
| 100 | #define MAXIMUM_STACK_SIZE (256*1024) |
| 101 | #endif |
| 102 | |
Jack Jansen | d1f0631 | 1996-08-01 15:23:54 +0000 | [diff] [blame] | 103 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 104 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 105 | ** 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] | 106 | ** handle them). Note that we don't know who has windows open, so |
| 107 | ** 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] | 108 | */ |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 109 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 110 | |
| 111 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 113 | /* 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] | 114 | ** with the python errors.h. */ |
| 115 | #define fnfErr -43 |
| 116 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 117 | /* Interrupt code variables: */ |
| 118 | static int interrupted; /* Set to true when cmd-. seen */ |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 119 | static RETSIGTYPE intcatcher(int); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 120 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 121 | #if !TARGET_API_MAC_OSX |
Jack Jansen | d88296d | 2000-07-11 19:51:05 +0000 | [diff] [blame] | 122 | static int PyMac_Yield(void); |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 123 | #endif |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 124 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 125 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 126 | ** These are the real scheduling parameters that control what we check |
| 127 | ** in the event loop, and how often we check. The values are initialized |
| 128 | ** from pyMac_SchedParamStruct. |
| 129 | */ |
| 130 | |
| 131 | struct real_sched_param_struct { |
| 132 | int check_interrupt; /* if true check for command-dot */ |
| 133 | int process_events; /* if nonzero enable evt processing, this mask */ |
| 134 | int besocial; /* if nonzero be a little social with CPU */ |
| 135 | unsigned long check_interval; /* how often to check, in ticks */ |
| 136 | unsigned long bg_yield; /* yield so long when in background */ |
| 137 | /* these are computed from previous and clock and such */ |
| 138 | int enabled; /* check_interrupt OR process_event OR yield */ |
| 139 | unsigned long next_check; /* when to check/yield next, in ticks */ |
| 140 | }; |
| 141 | |
| 142 | static struct real_sched_param_struct schedparams = |
| 143 | { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0}; |
| 144 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 145 | /* |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 146 | ** Workaround for sioux/gusi combo: set when we are exiting |
| 147 | */ |
| 148 | int PyMac_ConsoleIsDead; |
| 149 | |
| 150 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 151 | ** Sioux menu bar, saved early so we can restore it |
| 152 | */ |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 153 | static MenuBarHandle sioux_mbar; |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 156 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 157 | */ |
| 158 | struct hook_args { |
| 159 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 160 | char *prompt; /* The prompt */ |
| 161 | }; |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 162 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 163 | /* The StandardFile hooks don't exist in Carbon. This breaks GetDirectory, |
| 164 | ** but the macfsn code will replace it by a NavServices version anyway. |
| 165 | */ |
| 166 | #define myhook_upp NULL |
| 167 | #else |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 168 | static DlgHookYDUPP myhook_upp; |
| 169 | static int upp_inited = 0; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 170 | #endif |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 171 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 172 | /* |
| 173 | ** The python-code event handler |
| 174 | */ |
| 175 | static PyObject *python_event_handler; |
| 176 | |
Jack Jansen | 8f5725a | 1999-12-07 23:08:10 +0000 | [diff] [blame] | 177 | /* |
| 178 | ** Set to true if we're appearance-compliant |
| 179 | */ |
| 180 | int PyMac_AppearanceCompliant; |
| 181 | |
Jack Jansen | 741e037 | 2001-05-19 12:55:57 +0000 | [diff] [blame] | 182 | /* Given an FSSpec, return the FSSpec of the parent folder */ |
| 183 | |
| 184 | static OSErr |
| 185 | get_folder_parent (FSSpec * fss, FSSpec * parent) |
| 186 | { |
| 187 | CInfoPBRec rec; |
| 188 | short err; |
| 189 | |
| 190 | * parent = * fss; |
| 191 | rec.hFileInfo.ioNamePtr = parent->name; |
| 192 | rec.hFileInfo.ioVRefNum = parent->vRefNum; |
| 193 | rec.hFileInfo.ioDirID = parent->parID; |
| 194 | rec.hFileInfo.ioFDirIndex = -1; |
| 195 | rec.hFileInfo.ioFVersNum = 0; |
| 196 | if (err = PBGetCatInfoSync (& rec)) |
| 197 | return err; |
| 198 | parent->parID = rec.dirInfo.ioDrParID; |
| 199 | /* parent->name[0] = 0; */ |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* Given an FSSpec return a full, colon-separated pathname */ |
| 204 | |
| 205 | OSErr |
| 206 | PyMac_GetFullPath (FSSpec *fss, char *buf) |
| 207 | { |
| 208 | short err; |
| 209 | FSSpec fss_parent, fss_current; |
| 210 | char tmpbuf[1024]; |
| 211 | int plen; |
| 212 | |
| 213 | fss_current = *fss; |
| 214 | plen = fss_current.name[0]; |
| 215 | memcpy(buf, &fss_current.name[1], plen); |
| 216 | buf[plen] = 0; |
| 217 | /* Special case for disk names */ |
| 218 | if ( fss_current.parID <= 1 ) { |
| 219 | buf[plen++] = ':'; |
| 220 | buf[plen] = 0; |
| 221 | return 0; |
| 222 | } |
| 223 | while (fss_current.parID > 1) { |
| 224 | /* Get parent folder name */ |
| 225 | if (err = get_folder_parent(&fss_current, &fss_parent)) |
| 226 | return err; |
| 227 | fss_current = fss_parent; |
| 228 | /* Prepend path component just found to buf */ |
| 229 | plen = fss_current.name[0]; |
| 230 | if (strlen(buf) + plen + 1 > 1024) { |
| 231 | /* Oops... Not enough space (shouldn't happen) */ |
| 232 | *buf = 0; |
| 233 | return -1; |
| 234 | } |
| 235 | memcpy(tmpbuf, &fss_current.name[1], plen); |
| 236 | tmpbuf[plen] = ':'; |
| 237 | strcpy(&tmpbuf[plen+1], buf); |
| 238 | strcpy(buf, tmpbuf); |
| 239 | } |
| 240 | return 0; |
| 241 | } |
Jack Jansen | 1f9f2f4 | 2000-07-24 19:50:16 +0000 | [diff] [blame] | 242 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 243 | #ifdef USE_GUSI1 |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 244 | /* |
| 245 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 246 | ** the working directory. Hence, we call this routine after each call |
| 247 | ** to chdir() to rectify things. |
| 248 | */ |
| 249 | void |
| 250 | PyMac_FixGUSIcd() |
| 251 | { |
| 252 | WDPBRec pb; |
| 253 | FSSpec curdirfss; |
| 254 | |
| 255 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 256 | return; |
| 257 | |
| 258 | /* Set MacOS "working directory" */ |
| 259 | pb.ioNamePtr= "\p"; |
| 260 | pb.ioVRefNum= curdirfss.vRefNum; |
| 261 | pb.ioWDDirID= curdirfss.parID; |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 262 | if (PBHSetVolSync(&pb) != noErr) |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 263 | return; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 264 | } |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 265 | #endif |
Jack Jansen | 7ac70af | 1996-08-19 11:01:05 +0000 | [diff] [blame] | 266 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 267 | #ifdef USE_GUSI |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 268 | /* |
| 269 | ** SpinCursor (needed by GUSI) drags in heaps of stuff, so we |
| 270 | ** provide a dummy here. |
| 271 | */ |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 272 | void SpinCursor(short x) { /* Dummy */ } |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 273 | void RotateCursor(short x) { /* Dummy */ } |
Jack Jansen | cfadbd4 | 1996-08-19 11:36:25 +0000 | [diff] [blame] | 274 | |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 275 | /* |
| 276 | ** Replacement GUSI Spin function |
| 277 | */ |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 278 | #ifdef USE_GUSI1 |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 279 | static int |
| 280 | PyMac_GUSISpin(spin_msg msg, long arg) |
| 281 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 282 | static Boolean inForeground = true; |
| 283 | int maxsleep = 6; /* 6 ticks is "normal" sleeptime */ |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 284 | |
| 285 | if (PyMac_ConsoleIsDead) return 0; |
| 286 | #if 0 |
| 287 | if (inForeground) |
| 288 | SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1); |
| 289 | #endif |
| 290 | |
| 291 | if (interrupted) return -1; |
| 292 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 293 | if ( msg == SP_AUTO_SPIN ) |
| 294 | maxsleep = 0; |
| 295 | if ( msg==SP_SLEEP||msg==SP_SELECT ) |
| 296 | maxsleep = arg; |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 297 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 298 | 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] | 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | PyMac_SetGUSISpin() { |
| 305 | GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin); |
| 306 | } |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 307 | #endif |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 308 | |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 309 | /* Called at exit() time thru atexit(), to stop event processing */ |
| 310 | void |
| 311 | PyMac_StopGUSISpin() { |
| 312 | PyMac_ConsoleIsDead = 1; |
| 313 | } |
| 314 | |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 315 | #if TARGET_API_MAC_OS8 |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 316 | /* |
| 317 | ** Replacement routines for the PLstr... functions so we don't need |
Jack Jansen | 6ba34aa | 2001-01-11 23:03:56 +0000 | [diff] [blame] | 318 | ** StdCLib. |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 319 | */ |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 320 | pascal void |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 321 | PLstrcpy(unsigned char *to, unsigned char *fr) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 322 | { |
| 323 | memcpy(to, fr, fr[0]+1); |
| 324 | } |
| 325 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 326 | pascal int |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 327 | PLstrcmp(unsigned char *s1, unsigned char *s2) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 328 | { |
| 329 | int res; |
| 330 | int l = s1[0] < s2[0] ? s1[0] : s2[0]; |
| 331 | |
| 332 | res = memcmp(s1+1, s2+1, l); |
| 333 | if ( res != 0 ) |
| 334 | return res; |
| 335 | |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 336 | if ( s1[0] < s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 337 | return -1; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 338 | else if ( s1[0] > s2[0] ) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 339 | return 1; |
| 340 | else |
| 341 | return 0; |
| 342 | } |
| 343 | |
Jack Jansen | 8bb573e | 1999-12-12 21:37:14 +0000 | [diff] [blame] | 344 | pascal unsigned char * |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 345 | PLstrrchr(unsigned char *str, unsigned char chr) |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 346 | { |
| 347 | unsigned char *ptr = 0; |
| 348 | unsigned char *p; |
| 349 | |
| 350 | for(p=str+1; p<str+str[0]; p++) |
| 351 | if ( *p == chr ) |
| 352 | ptr = p; |
| 353 | return ptr; |
| 354 | } |
| 355 | |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 356 | #endif /* TARGET_API_MAC_OS8 */ |
Jack Jansen | a39f1b0 | 1997-05-23 15:35:14 +0000 | [diff] [blame] | 357 | #endif /* USE_GUSI */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 358 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 359 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 360 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 361 | unsigned char * |
| 362 | Pstring(char *str) |
| 363 | { |
| 364 | static Str255 buf; |
| 365 | int len; |
| 366 | |
| 367 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 368 | if (len > 255) |
| 369 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 370 | buf[0] = (unsigned char)len; |
| 371 | strncpy((char *)buf+1, str, len); |
| 372 | return buf; |
| 373 | } |
| 374 | |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 375 | #if TARGET_API_MAC_OS8 |
Jack Jansen | 5afad83 | 2000-12-12 22:12:14 +0000 | [diff] [blame] | 376 | void |
| 377 | c2pstrcpy(unsigned char *dst, const char *src) |
| 378 | { |
| 379 | int len; |
| 380 | |
| 381 | len = strlen(src); |
| 382 | if ( len > 255 ) len = 255; |
| 383 | strncpy((char *)dst+1, src, len); |
| 384 | dst[0] = len; |
| 385 | } |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 386 | #endif /* TARGET_API_MAC_OS8 */ |
Jack Jansen | 5afad83 | 2000-12-12 22:12:14 +0000 | [diff] [blame] | 387 | |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 388 | #ifdef USE_STACKCHECK |
| 389 | /* Check for stack overflow */ |
| 390 | int |
| 391 | PyOS_CheckStack() |
| 392 | { |
Jack Jansen | 14a9171 | 2000-08-25 21:57:23 +0000 | [diff] [blame] | 393 | char here; |
| 394 | static char *sentinel = 0; |
Jack Jansen | 53bafd9 | 2000-09-08 22:05:48 +0000 | [diff] [blame] | 395 | static PyThreadState *thread_for_sentinel = 0; |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 396 | |
Jack Jansen | 340eb88 | 2001-02-02 22:40:28 +0000 | [diff] [blame] | 397 | if ( sentinel == 0 ) { |
| 398 | unsigned long stackspace = StackSpace(); |
| 399 | |
| 400 | #ifdef MAXIMUM_STACK_SIZE |
| 401 | /* See the comment at the definition */ |
| 402 | if ( stackspace > MAXIMUM_STACK_SIZE ) |
| 403 | stackspace = MAXIMUM_STACK_SIZE; |
| 404 | #endif |
| 405 | sentinel = &here - stackspace + MINIMUM_STACK_SIZE; |
Jack Jansen | 14a9171 | 2000-08-25 21:57:23 +0000 | [diff] [blame] | 406 | } |
Jack Jansen | 53bafd9 | 2000-09-08 22:05:48 +0000 | [diff] [blame] | 407 | if ( thread_for_sentinel == 0 ) { |
| 408 | thread_for_sentinel = PyThreadState_Get(); |
| 409 | } |
| 410 | if ( &here < sentinel ) { |
| 411 | if (thread_for_sentinel == PyThreadState_Get()) { |
| 412 | return -1; |
| 413 | #if 0 |
| 414 | } else { |
| 415 | /* Else we are unsure... */ |
| 416 | fprintf(stderr, "Stackcheck in other thread (was %x now %x)\n", thread_for_sentinel,PyThreadState_Get()); |
| 417 | #endif |
| 418 | } |
| 419 | } |
Jack Jansen | 1ed9529 | 1996-07-22 15:25:10 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | #endif /* USE_STACKCHECK */ |
| 423 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 424 | #if !TARGET_API_MAC_OSX |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 425 | /* The catcher routine (which may not be used for all compilers) */ |
| 426 | static RETSIGTYPE |
| 427 | intcatcher(sig) |
| 428 | int sig; |
| 429 | { |
| 430 | interrupted = 1; |
| 431 | signal(SIGINT, intcatcher); |
| 432 | } |
| 433 | |
| 434 | void |
| 435 | PyOS_InitInterrupts() |
| 436 | { |
| 437 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 438 | signal(SIGINT, intcatcher); |
| 439 | } |
| 440 | |
Jack Jansen | a8441de | 1997-08-08 14:57:37 +0000 | [diff] [blame] | 441 | void |
| 442 | PyOS_FiniInterrupts() |
| 443 | { |
| 444 | } |
| 445 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 446 | /* |
| 447 | ** This routine scans the event queue looking for cmd-. |
| 448 | ** This is the only way to get an interrupt under THINK (since it |
| 449 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 450 | ** the full-fledged event loop is disabled. This way, we can at least |
| 451 | ** interrupt a runaway python program. |
| 452 | */ |
| 453 | static void |
| 454 | scan_event_queue(flush) |
| 455 | int flush; |
| 456 | { |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 457 | #if !TARGET_API_MAC_OS8 |
Jack Jansen | d7b6802 | 2001-01-12 23:42:28 +0000 | [diff] [blame] | 458 | if ( CheckEventQueueForUserCancel() ) |
| 459 | interrupted = 1; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 460 | #else |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 461 | register EvQElPtr q; |
| 462 | |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 463 | q = (EvQElPtr) LMGetEventQueue()->qHead; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 464 | |
| 465 | for (; q; q = (EvQElPtr)q->qLink) { |
| 466 | if (q->evtQWhat == keyDown && |
| 467 | (char)q->evtQMessage == '.' && |
| 468 | (q->evtQModifiers & cmdKey) != 0) { |
| 469 | if ( flush ) |
| 470 | FlushEvents(keyDownMask, 0); |
| 471 | interrupted = 1; |
| 472 | break; |
| 473 | } |
| 474 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 475 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | int |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 479 | PyErr_CheckSignals() |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 480 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 481 | if (schedparams.enabled) { |
| 482 | if ( (unsigned long)LMGetTicks() > schedparams.next_check ) { |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 483 | if ( PyMac_Yield() < 0) |
| 484 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 485 | schedparams.next_check = (unsigned long)LMGetTicks() |
| 486 | + schedparams.check_interval; |
| 487 | if (interrupted) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 488 | scan_event_queue(1); /* Eat events up to cmd-. */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 489 | interrupted = 0; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 490 | PyErr_SetNone(PyExc_KeyboardInterrupt); |
| 491 | return -1; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 494 | } |
| 495 | return 0; |
| 496 | } |
| 497 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 498 | int |
| 499 | PyOS_InterruptOccurred() |
| 500 | { |
| 501 | scan_event_queue(1); |
| 502 | return interrupted; |
| 503 | } |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 504 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 505 | /* Check whether we are in the foreground */ |
Jack Jansen | 9ae898b | 2000-07-11 21:16:03 +0000 | [diff] [blame] | 506 | static int |
| 507 | PyMac_InForeground(void) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 508 | { |
| 509 | static ProcessSerialNumber ours; |
| 510 | static inited; |
| 511 | ProcessSerialNumber curfg; |
| 512 | Boolean eq; |
| 513 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 514 | if ( inited == 0 ) { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 515 | (void)GetCurrentProcess(&ours); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 516 | inited = 1; |
| 517 | } |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 518 | if ( GetFrontProcess(&curfg) < 0 ) |
| 519 | eq = 1; |
| 520 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 521 | eq = 1; |
| 522 | return (int)eq; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 523 | } |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 524 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 525 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 526 | int |
| 527 | PyMac_SetEventHandler(PyObject *evh) |
| 528 | { |
| 529 | if ( evh && python_event_handler ) { |
| 530 | PyErr_SetString(PyExc_RuntimeError, "Python event handler already set"); |
| 531 | return 0; |
| 532 | } |
| 533 | if ( python_event_handler ) |
| 534 | Py_DECREF(python_event_handler); |
| 535 | if ( evh ) |
| 536 | Py_INCREF(evh); |
| 537 | python_event_handler = evh; |
| 538 | return 1; |
| 539 | } |
| 540 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 541 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 542 | ** Handle an event, either one found in the mainloop eventhandler or |
| 543 | ** one passed back from the python program. |
| 544 | */ |
| 545 | void |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 546 | PyMac_HandleEventIntern(evp) |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 547 | EventRecord *evp; |
| 548 | { |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 549 | #if TARGET_API_MAC_OS8 |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 550 | if ( evp->what == mouseDown ) { |
| 551 | WindowPtr wp; |
| 552 | |
| 553 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
| 554 | SystemClick(evp, wp); |
| 555 | return; |
| 556 | } |
| 557 | } |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 558 | #endif |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 559 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 560 | { |
| 561 | int siouxdidit; |
| 562 | |
| 563 | /* If SIOUX wants it we're done */ |
| 564 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 565 | if ( siouxdidit ) |
| 566 | return; |
| 567 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 568 | #else |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 569 | /* Other compilers are just unlucky... */ |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 570 | #endif /* !__MWERKS__ */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /* |
| 574 | ** Handle an event, either through HandleEvent or by passing it to the Python |
| 575 | ** event handler. |
| 576 | */ |
| 577 | int |
| 578 | PyMac_HandleEvent(evp) |
| 579 | EventRecord *evp; |
| 580 | { |
| 581 | PyObject *rv; |
| 582 | |
| 583 | if ( python_event_handler ) { |
| 584 | rv = PyObject_CallFunction(python_event_handler, "(O&)", |
| 585 | PyMac_BuildEventRecord, evp); |
| 586 | if ( rv ) |
| 587 | Py_DECREF(rv); |
| 588 | else |
| 589 | return -1; /* Propagate exception */ |
| 590 | } else { |
| 591 | PyMac_HandleEventIntern(evp); |
| 592 | } |
| 593 | return 0; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 596 | #if !TARGET_API_MAC_OSX |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 597 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 598 | ** Yield the CPU to other tasks without processing events. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 599 | */ |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 600 | int |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 601 | PyMac_DoYield(int maxsleep, int maycallpython) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 602 | { |
| 603 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 604 | int gotone; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 605 | long latest_time_ready; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 606 | static int in_here = 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 607 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 608 | in_here++; |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 609 | /* |
| 610 | ** First check for interrupts, if wanted. |
| 611 | ** This sets a flag that will be picked up at an appropriate |
| 612 | ** moment in the mainloop. |
| 613 | */ |
| 614 | if (schedparams.check_interrupt) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 615 | scan_event_queue(0); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 616 | |
| 617 | /* XXXX Implementing an idle routine goes here */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 618 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 619 | /* |
| 620 | ** Check which of the eventloop cases we have: |
| 621 | ** - process events |
| 622 | ** - don't process events but do yield |
| 623 | ** - do neither |
| 624 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 625 | if( in_here > 1 || !schedparams.process_events || |
| 626 | (python_event_handler && !maycallpython) ) { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 627 | if ( maxsleep >= 0 ) { |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 628 | #if TARGET_API_MAC_OS8 |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 629 | SystemTask(); |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 630 | #else |
| 631 | int xxx = 0; |
Jack Jansen | bb6d83a | 2000-06-02 21:27:11 +0000 | [diff] [blame] | 632 | #endif |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 633 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 634 | } else { |
| 635 | latest_time_ready = LMGetTicks() + maxsleep; |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 636 | do { |
Jack Jansen | 0c96887 | 1997-08-26 13:20:34 +0000 | [diff] [blame] | 637 | /* XXXX Hack by Jack. |
| 638 | ** In time.sleep() you can click to another application |
| 639 | ** once only. If you come back to Python you cannot get away |
| 640 | ** again. |
| 641 | **/ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 642 | gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL); |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 643 | /* Get out quickly if nothing interesting is happening */ |
| 644 | if ( !gotone || ev.what == nullEvent ) |
| 645 | break; |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 646 | if ( PyMac_HandleEvent(&ev) < 0 ) { |
| 647 | in_here--; |
| 648 | return -1; |
| 649 | } |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 650 | maxsleep = latest_time_ready - LMGetTicks(); |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 651 | } while ( maxsleep > 0 ); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 652 | } |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 653 | in_here--; |
| 654 | return 0; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 658 | ** Process events and/or yield the CPU to other tasks if opportune |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 659 | */ |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 660 | int |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 661 | PyMac_Yield() { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 662 | unsigned long maxsleep; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 663 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 664 | if( PyMac_InForeground() ) |
| 665 | maxsleep = 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 666 | else |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 667 | maxsleep = schedparams.bg_yield; |
| 668 | |
Jack Jansen | 36ed506 | 1997-06-20 16:18:15 +0000 | [diff] [blame] | 669 | return PyMac_DoYield(maxsleep, 1); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 673 | ** Return current scheduler parameters |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 674 | */ |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 675 | void |
| 676 | PyMac_GetSchedParams(PyMacSchedParams *sp) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 677 | { |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 678 | sp->check_interrupt = schedparams.check_interrupt; |
| 679 | sp->process_events = schedparams.process_events; |
| 680 | sp->besocial = schedparams.besocial; |
| 681 | sp->check_interval = schedparams.check_interval / 60.0; |
| 682 | sp->bg_yield = schedparams.bg_yield / 60.0; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 683 | } |
Jack Jansen | f6865f7 | 1996-09-04 15:24:59 +0000 | [diff] [blame] | 684 | |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 685 | /* |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 686 | ** Set current scheduler parameters |
| 687 | */ |
| 688 | void |
| 689 | PyMac_SetSchedParams(PyMacSchedParams *sp) |
| 690 | { |
| 691 | schedparams.check_interrupt = sp->check_interrupt; |
| 692 | schedparams.process_events = sp->process_events; |
| 693 | schedparams.besocial = sp->besocial; |
| 694 | schedparams.check_interval = (unsigned long)(sp->check_interval*60); |
| 695 | schedparams.bg_yield = (unsigned long)(sp->bg_yield*60); |
| 696 | if ( schedparams.check_interrupt || schedparams.process_events || |
| 697 | schedparams.besocial ) |
| 698 | schedparams.enabled = 1; |
| 699 | else |
| 700 | schedparams.enabled = 0; |
| 701 | schedparams.next_check = 0; /* Check immedeately */ |
| 702 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 703 | |
Jack Jansen | e3ae0df | 1997-06-03 15:28:29 +0000 | [diff] [blame] | 704 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 705 | ** Install our menu bar. |
| 706 | */ |
| 707 | void |
| 708 | PyMac_InitMenuBar() |
| 709 | { |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 710 | MenuHandle applemenu; |
| 711 | |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 712 | if ( sioux_mbar ) return; |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 713 | if ( (sioux_mbar=GetMenuBar()) == NULL ) { |
| 714 | /* Sioux menu not installed yet. Do so */ |
| 715 | SIOUXSetupMenus(); |
| 716 | if ( (sioux_mbar=GetMenuBar()) == NULL ) |
| 717 | return; |
| 718 | } |
Jack Jansen | 08c3be3 | 1997-04-08 15:27:00 +0000 | [diff] [blame] | 719 | if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 720 | SetMenuItemText(applemenu, 1, "\pAbout Python..."); |
| 721 | } |
| 722 | |
| 723 | /* |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 724 | ** Restore sioux menu bar |
| 725 | */ |
| 726 | void |
| 727 | PyMac_RestoreMenuBar() |
| 728 | { |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 729 | #if 1 |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 730 | /* This doesn't seem to work anymore? Or only for Carbon? */ |
| 731 | MenuBarHandle curmenubar; |
| 732 | |
| 733 | curmenubar = GetMenuBar(); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 734 | if ( sioux_mbar ) { |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 735 | SetMenuBar(sioux_mbar); |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 736 | DrawMenuBar(); |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 737 | } else { |
Jack Jansen | efaada7 | 1998-02-20 16:03:15 +0000 | [diff] [blame] | 738 | PyMac_InitMenuBar(); |
Jack Jansen | 657ba27 | 2001-02-17 22:02:07 +0000 | [diff] [blame] | 739 | DrawMenuBar(); |
| 740 | } |
| 741 | #endif |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Jack Jansen | 15f1c08 | 2001-04-25 22:07:27 +0000 | [diff] [blame] | 744 | void |
| 745 | PyMac_RaiseConsoleWindow() |
| 746 | { |
| 747 | /* Note: this is a hack. SIOUXTextWindow is SIOUX's internal structure |
| 748 | ** and we happen to know that the first entry is the window pointer. |
| 749 | */ |
| 750 | extern WindowRef *SIOUXTextWindow; |
| 751 | |
| 752 | if ( SIOUXTextWindow == NULL || *SIOUXTextWindow == NULL ) |
| 753 | return; |
| 754 | if ( FrontWindow() != *SIOUXTextWindow ) |
| 755 | BringToFront(*SIOUXTextWindow); |
| 756 | } |
Jack Jansen | caa7c46 | 1997-06-12 10:49:13 +0000 | [diff] [blame] | 757 | |
| 758 | /* |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 759 | ** Our replacement about box |
| 760 | */ |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 761 | |
| 762 | #include "patchlevel.h" |
| 763 | |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 764 | void |
| 765 | SIOUXDoAboutBox(void) |
| 766 | { |
| 767 | DialogPtr theDialog; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 768 | WindowPtr theWindow; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 769 | short item; |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 770 | short fontID; |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 771 | |
| 772 | if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL ) |
| 773 | return; |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 774 | theWindow = GetDialogWindow(theDialog); |
Jack Jansen | 7e1fb7c | 1998-07-31 09:36:30 +0000 | [diff] [blame] | 775 | SetPortWindowPort(theWindow); |
| 776 | GetFNum("\pPython-Sans", &fontID); |
| 777 | if (fontID == 0) |
| 778 | fontID = kFontIDGeneva; |
| 779 | TextFont(fontID); |
| 780 | TextSize(9); |
Jack Jansen | f60edf8 | 2001-08-19 22:32:27 +0000 | [diff] [blame^] | 781 | ParamText(Pstring(PY_VERSION), "\p", "\p", "\p"); |
Jack Jansen | d617c57 | 1996-09-22 22:14:30 +0000 | [diff] [blame] | 782 | ShowWindow(theWindow); |
Jack Jansen | 3469e99 | 1996-09-06 00:30:45 +0000 | [diff] [blame] | 783 | ModalDialog(NULL, &item); |
| 784 | DisposeDialog(theDialog); |
| 785 | } |
| 786 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 787 | #endif /* !TARGET_API_MAC_OSX */ |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 788 | |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 789 | #if TARGET_API_MAC_OS8 |
Jack Jansen | eda7863 | 1997-06-12 15:29:46 +0000 | [diff] [blame] | 790 | /* |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 791 | ** Helper routine for GetDirectory |
| 792 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 793 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 794 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 795 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 796 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 797 | Handle prompth; |
| 798 | short type; |
| 799 | Rect rect; |
| 800 | |
| 801 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 802 | if ( prompth ) |
| 803 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 804 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 805 | if ( item == SELECTCUR_ITEM ) { |
| 806 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 807 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 808 | } |
| 809 | return item; |
| 810 | } |
Jack Jansen | 0194ad5 | 2001-05-12 22:46:35 +0000 | [diff] [blame] | 811 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 812 | /* |
| 813 | ** Ask the user for a directory. I still can't understand |
| 814 | ** why Apple doesn't provide a standard solution for this... |
| 815 | */ |
| 816 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 817 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 818 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 819 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 820 | { |
| 821 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 822 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 823 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 824 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 825 | |
| 826 | if ( !upp_inited ) { |
| 827 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 828 | upp_inited = 1; |
| 829 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 830 | if ( prompt && *prompt ) |
| 831 | hook_args.prompt = (char *)Pstring(prompt); |
| 832 | else |
| 833 | hook_args.prompt = NULL; |
| 834 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 835 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 836 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 837 | |
| 838 | reply.sfFile.name[0] = 0; |
| 839 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 840 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 841 | return hook_args.selectcur_hit; |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 846 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 847 | StandardFileReply *reply, char *prompt) |
| 848 | { |
| 849 | static Point where = {-1, -1}; |
| 850 | struct hook_args hook_args; |
| 851 | |
| 852 | if ( !upp_inited ) { |
| 853 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 854 | upp_inited = 1; |
| 855 | } |
| 856 | if ( prompt && *prompt ) |
| 857 | hook_args.prompt = (char *)Pstring(prompt); |
| 858 | else |
| 859 | hook_args.prompt = NULL; |
| 860 | hook_args.selectcur_hit = 0; |
| 861 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 862 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 863 | } |
Jack Jansen | 6e68a7e | 2001-05-12 21:31:34 +0000 | [diff] [blame] | 864 | #endif /* TARGET_API_MAC_OS8 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 865 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 866 | |