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