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