Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 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 | |
| 25 | #include "Python.h" |
Guido van Rossum | becdbec | 1995-02-14 01:27:24 +0000 | [diff] [blame] | 26 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 27 | #include "macglue.h" |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 28 | #include "marshal.h" |
| 29 | #include "import.h" |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 30 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 31 | #include "pythonresources.h" |
| 32 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 33 | #include <OSUtils.h> /* for Set(Current)A5 */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 34 | #include <Files.h> |
Jack Jansen | 8cd2b72 | 1995-02-13 11:33:28 +0000 | [diff] [blame] | 35 | #include <StandardFile.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 36 | #include <Resources.h> |
| 37 | #include <Memory.h> |
| 38 | #include <Events.h> |
| 39 | #include <Windows.h> |
| 40 | #include <Desk.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 41 | #include <Traps.h> |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 42 | #include <Processes.h> |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 43 | #include <Fonts.h> |
| 44 | #include <Menus.h> |
Jack Jansen | 0830550 | 1995-06-18 20:03:40 +0000 | [diff] [blame] | 45 | #include <TextUtils.h> |
Guido van Rossum | cc0d879 | 1995-01-30 08:57:13 +0000 | [diff] [blame] | 46 | #ifdef THINK_C |
| 47 | #include <OSEvents.h> /* For EvQElPtr */ |
| 48 | #endif |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 49 | #ifdef __MWERKS__ |
| 50 | #include <SIOUX.h> |
| 51 | #endif |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 52 | #ifdef USE_GUSI |
| 53 | #include <TFileSpec.h> /* For Path2FSSpec */ |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 54 | #include <LowMem.h> /* For SetSFCurDir, etc */ |
Jack Jansen | 911ad6b | 1996-03-05 16:56:24 +0000 | [diff] [blame] | 55 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 57 | #ifndef HAVE_UNIVERSAL_HEADERS |
| 58 | #define GetResourceSizeOnDisk(x) SizeResource(x) |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 59 | typedef DlgHookYDProcPtr DlgHookYDUPP; |
| 60 | #define NewDlgHookYDProc(userRoutine) ((DlgHookYDUPP) (userRoutine)) |
| 61 | typedef FileFilterYDProcPtr FileFilterYDUPP; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 64 | #include <signal.h> |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 65 | #include <stdio.h> |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 66 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 67 | /* |
Jack Jansen | 16df2aa | 1995-02-27 16:17:28 +0000 | [diff] [blame] | 68 | ** We have to be careful, since we can't handle |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 69 | ** 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] | 70 | ** handle them). Note that we don't know who has windows open, so |
| 71 | ** 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] | 72 | */ |
| 73 | #define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 74 | |
| 75 | #include <signal.h> |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 76 | |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 77 | /* 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] | 78 | ** with the python errors.h. */ |
| 79 | #define fnfErr -43 |
| 80 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 81 | /* Declared in macfsmodule.c: */ |
| 82 | extern FSSpec *mfs_GetFSSpecFSSpec(); |
| 83 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 84 | /* Interrupt code variables: */ |
| 85 | static int interrupted; /* Set to true when cmd-. seen */ |
| 86 | static RETSIGTYPE intcatcher Py_PROTO((int)); |
| 87 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 88 | /* |
| 89 | ** We attempt to be a good citizen by giving up the CPU periodically. |
| 90 | ** When in the foreground we do this less often and for shorter periods |
| 91 | ** than when in the background. At this time we also check for events and |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 92 | ** pass them off to SIOUX, if compiling with mwerks. |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 93 | ** The counts here are in ticks of 1/60th second. |
| 94 | ** XXXX The initial values here are not based on anything. |
| 95 | ** FG-python gives up the cpu for 1/60th 5 times per second, |
| 96 | ** BG-python for .2 second 10 times per second. |
| 97 | */ |
| 98 | static long interval_fg = 12; |
| 99 | static long interval_bg = 6; |
| 100 | static long yield_fg = 1; |
| 101 | static long yield_bg = 12; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 102 | static long lastyield; |
| 103 | static int in_foreground; |
| 104 | |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 105 | /* |
| 106 | ** When > 0, do full scanning for events (program is not event aware) |
| 107 | ** when == 0, only scan for Command-period |
| 108 | ** when < 0, don't do any event scanning |
| 109 | */ |
| 110 | int PyMac_DoYieldEnabled = 1; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 111 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 112 | /* |
| 113 | ** Some stuff for our GetDirectory and PromptGetFile routines |
| 114 | */ |
| 115 | struct hook_args { |
| 116 | int selectcur_hit; /* Set to true when "select current" selected */ |
| 117 | char *prompt; /* The prompt */ |
| 118 | }; |
| 119 | static DlgHookYDUPP myhook_upp; |
| 120 | static int upp_inited = 0; |
| 121 | |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 122 | #ifdef USE_GUSI |
| 123 | /* |
| 124 | ** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of |
| 125 | ** the working directory. Hence, we call this routine after each call |
| 126 | ** to chdir() to rectify things. |
| 127 | */ |
| 128 | void |
| 129 | PyMac_FixGUSIcd() |
| 130 | { |
| 131 | WDPBRec pb; |
| 132 | FSSpec curdirfss; |
| 133 | |
| 134 | if ( Path2FSSpec(":x", &curdirfss) != noErr ) |
| 135 | return; |
| 136 | |
| 137 | /* Set MacOS "working directory" */ |
| 138 | pb.ioNamePtr= "\p"; |
| 139 | pb.ioVRefNum= curdirfss.vRefNum; |
| 140 | pb.ioWDDirID= curdirfss.parID; |
| 141 | if (PBHSetVol(&pb, 0) != noErr) |
| 142 | return; |
| 143 | |
Jack Jansen | 975aa22 | 1996-03-25 15:46:25 +0000 | [diff] [blame] | 144 | #if 0 |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 145 | /* Set standard-file working directory */ |
| 146 | LMSetSFSaveDisk(-curdirfss.vRefNum); |
| 147 | LMSetCurDirStore(curdirfss.parID); |
Jack Jansen | 975aa22 | 1996-03-25 15:46:25 +0000 | [diff] [blame] | 148 | #endif |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 149 | } |
| 150 | #endif |
| 151 | |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 152 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 153 | /* Convert C to Pascal string. Returns pointer to static buffer. */ |
| 154 | unsigned char * |
| 155 | Pstring(char *str) |
| 156 | { |
| 157 | static Str255 buf; |
| 158 | int len; |
| 159 | |
| 160 | len = strlen(str); |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 161 | if (len > 255) |
| 162 | len = 255; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 163 | buf[0] = (unsigned char)len; |
| 164 | strncpy((char *)buf+1, str, len); |
| 165 | return buf; |
| 166 | } |
| 167 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 168 | /* Like strerror() but for Mac OS error numbers */ |
| 169 | char *PyMac_StrError(int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 170 | { |
| 171 | static char buf[256]; |
| 172 | Handle h; |
| 173 | char *str; |
| 174 | |
| 175 | h = GetResource('Estr', err); |
| 176 | if ( h ) { |
| 177 | HLock(h); |
| 178 | str = (char *)*h; |
| 179 | memcpy(buf, str+1, (unsigned char)str[0]); |
Guido van Rossum | cc9bc8f | 1995-02-13 16:17:03 +0000 | [diff] [blame] | 180 | buf[(unsigned char)str[0]] = '\0'; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 181 | HUnlock(h); |
| 182 | ReleaseResource(h); |
| 183 | } else { |
| 184 | sprintf(buf, "Mac OS error code %d", err); |
| 185 | } |
| 186 | return buf; |
| 187 | } |
| 188 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 189 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 190 | PyObject *PyMac_OSErrException; |
| 191 | |
| 192 | /* Initialize and return PyMac_OSErrException */ |
| 193 | PyObject * |
| 194 | PyMac_GetOSErrException() |
| 195 | { |
| 196 | if (PyMac_OSErrException == NULL) |
| 197 | PyMac_OSErrException = PyString_FromString("Mac OS Error"); |
| 198 | return PyMac_OSErrException; |
| 199 | } |
| 200 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 201 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 202 | PyObject * |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 203 | PyErr_Mac(PyObject *eobj, int err) |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 204 | { |
| 205 | char *msg; |
| 206 | PyObject *v; |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 208 | if (err == 0 && !PyErr_Occurred()) { |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 209 | Py_INCREF(Py_None); |
| 210 | return Py_None; |
| 211 | } |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 212 | if (err == -1 && PyErr_Occurred()) |
| 213 | return NULL; |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 214 | msg = PyMac_StrError(err); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 215 | v = Py_BuildValue("(is)", err, msg); |
| 216 | PyErr_SetObject(eobj, v); |
| 217 | Py_DECREF(v); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 221 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 222 | PyObject * |
| 223 | PyMac_Error(OSErr err) |
| 224 | { |
| 225 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 226 | } |
| 227 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 228 | /* The catcher routine (which may not be used for all compilers) */ |
| 229 | static RETSIGTYPE |
| 230 | intcatcher(sig) |
| 231 | int sig; |
| 232 | { |
| 233 | interrupted = 1; |
| 234 | signal(SIGINT, intcatcher); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | PyOS_InitInterrupts() |
| 239 | { |
| 240 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
| 241 | signal(SIGINT, intcatcher); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | ** This routine scans the event queue looking for cmd-. |
| 246 | ** This is the only way to get an interrupt under THINK (since it |
| 247 | ** doesn't do SIGINT handling), but is also used under MW, when |
| 248 | ** the full-fledged event loop is disabled. This way, we can at least |
| 249 | ** interrupt a runaway python program. |
| 250 | */ |
| 251 | static void |
| 252 | scan_event_queue(flush) |
| 253 | int flush; |
| 254 | { |
Jack Jansen | f74f63a | 1995-06-27 13:18:14 +0000 | [diff] [blame] | 255 | #if defined(__MWERKS__) && defined(__CFM68K__) |
| 256 | return; /* No GetEvQHdr yet */ |
| 257 | #else |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 258 | register EvQElPtr q; |
| 259 | |
| 260 | q = (EvQElPtr) GetEvQHdr()->qHead; |
| 261 | |
| 262 | for (; q; q = (EvQElPtr)q->qLink) { |
| 263 | if (q->evtQWhat == keyDown && |
| 264 | (char)q->evtQMessage == '.' && |
| 265 | (q->evtQModifiers & cmdKey) != 0) { |
| 266 | if ( flush ) |
| 267 | FlushEvents(keyDownMask, 0); |
| 268 | interrupted = 1; |
| 269 | break; |
| 270 | } |
| 271 | } |
Jack Jansen | f74f63a | 1995-06-27 13:18:14 +0000 | [diff] [blame] | 272 | #endif |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | int |
| 276 | PyOS_InterruptOccurred() |
| 277 | { |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 278 | if (PyMac_DoYieldEnabled < 0) |
| 279 | return 0; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 280 | #ifdef THINK_C |
| 281 | scan_event_queue(1); |
| 282 | #endif |
| 283 | PyMac_Yield(); |
| 284 | if (interrupted) { |
| 285 | interrupted = 0; |
| 286 | return 1; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | /* intrpeek() is like intrcheck(), but it doesn't flush the events. The |
| 292 | ** idea is that you call intrpeek() somewhere in a busy-wait loop, and return |
| 293 | ** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon |
| 294 | ** thereafter. |
| 295 | */ |
| 296 | static int |
| 297 | intrpeek() |
| 298 | { |
| 299 | #ifdef THINK_C |
| 300 | scan_event_queue(0); |
| 301 | #endif |
| 302 | return interrupted; |
| 303 | } |
| 304 | |
| 305 | /* Check whether we are in the foreground */ |
| 306 | int |
| 307 | PyMac_InForeground() |
| 308 | { |
| 309 | static ProcessSerialNumber ours; |
| 310 | static inited; |
| 311 | ProcessSerialNumber curfg; |
| 312 | Boolean eq; |
| 313 | |
| 314 | if ( inited == 0 ) |
| 315 | (void)GetCurrentProcess(&ours); |
| 316 | inited = 1; |
| 317 | if ( GetFrontProcess(&curfg) < 0 ) |
| 318 | eq = 1; |
| 319 | else if ( SameProcess(&ours, &curfg, &eq) < 0 ) |
| 320 | eq = 1; |
| 321 | return (int)eq; |
| 322 | |
| 323 | } |
| 324 | |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 325 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 326 | ** Set yield timeouts |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 327 | */ |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 328 | void |
| 329 | PyMac_SetYield(long fgi, long fgy, long bgi, long bgy) |
| 330 | { |
| 331 | interval_fg = fgi; |
| 332 | yield_fg = fgy; |
| 333 | interval_bg = bgi; |
| 334 | yield_bg = bgy; |
| 335 | } |
| 336 | |
| 337 | /* |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 338 | ** Handle an event, either one found in the mainloop eventhandler or |
| 339 | ** one passed back from the python program. |
| 340 | */ |
| 341 | void |
| 342 | PyMac_HandleEvent(evp) |
| 343 | EventRecord *evp; |
| 344 | { |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 345 | |
| 346 | #ifdef __MWERKS__ |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 347 | { |
| 348 | int siouxdidit; |
| 349 | |
| 350 | /* If SIOUX wants it we're done */ |
| 351 | siouxdidit = SIOUXHandleOneEvent(evp); |
| 352 | if ( siouxdidit ) |
| 353 | return; |
| 354 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 355 | #else |
| 356 | /* Other compilers are just unlucky: we only weed out clicks in other applications */ |
| 357 | if ( evp->what == mouseDown ) { |
Jack Jansen | 847e89e | 1995-08-31 13:57:40 +0000 | [diff] [blame] | 358 | WindowPtr wp; |
| 359 | |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 360 | if ( FindWindow(evp->where, &wp) == inSysWindow ) { |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 361 | SystemClick(evp, wp); |
Jack Jansen | 38e9766 | 1995-11-10 14:53:00 +0000 | [diff] [blame] | 362 | return; |
| 363 | } |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 364 | } |
| 365 | #endif /* !__MWERKS__ */ |
| 366 | } |
| 367 | |
| 368 | /* |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 369 | ** Yield the CPU to other tasks. |
| 370 | */ |
| 371 | static |
| 372 | PyMac_DoYield() |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 373 | { |
| 374 | EventRecord ev; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 375 | long yield; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 376 | static int no_waitnextevent = -1; |
| 377 | int gotone; |
| 378 | |
| 379 | if ( no_waitnextevent < 0 ) { |
| 380 | no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) == |
| 381 | NGetTrapAddress(_Unimplemented, ToolTrap)); |
| 382 | } |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 383 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 384 | if ( !PyMac_DoYieldEnabled ) { |
| 385 | #ifndef THINK_C |
| 386 | /* Under think this has been done before in intrcheck() or intrpeek() */ |
| 387 | scan_event_queue(0); |
| 388 | #endif |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 389 | return; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 390 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 391 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 392 | in_foreground = PyMac_InForeground(); |
| 393 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 394 | yield = yield_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 395 | else |
| 396 | yield = yield_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 397 | while ( 1 ) { |
| 398 | if ( no_waitnextevent ) { |
| 399 | SystemTask(); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 400 | gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 401 | } else { |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 402 | gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 403 | } |
| 404 | /* Get out quickly if nothing interesting is happening */ |
| 405 | if ( !gotone || ev.what == nullEvent ) |
| 406 | break; |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 407 | PyMac_HandleEvent(&ev); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 408 | } |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 409 | lastyield = TickCount(); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | ** Yield the CPU to other tasks if opportune |
| 414 | */ |
| 415 | void |
| 416 | PyMac_Yield() { |
| 417 | long iv; |
| 418 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 419 | if ( in_foreground ) |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 420 | iv = interval_fg; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 421 | else |
| 422 | iv = interval_bg; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 423 | if ( TickCount() > lastyield + iv ) |
| 424 | PyMac_DoYield(); |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | ** Idle routine for busy-wait loops. |
| 429 | ** Gives up CPU, handles events and returns true if an interrupt is pending |
| 430 | ** (but not actually handled yet). |
| 431 | */ |
| 432 | int |
| 433 | PyMac_Idle() |
| 434 | { |
| 435 | PyMac_DoYield(); |
| 436 | return intrpeek(); |
Jack Jansen | f93c72a | 1994-12-14 14:07:50 +0000 | [diff] [blame] | 437 | } |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 438 | /* |
| 439 | ** Returns true if the argument has a resource fork, and it contains |
| 440 | ** a 'PYC ' resource of the correct name |
| 441 | */ |
| 442 | int |
| 443 | PyMac_FindResourceModule(module, filename) |
| 444 | char *module; |
| 445 | char *filename; |
| 446 | { |
| 447 | FSSpec fss; |
| 448 | FInfo finfo; |
| 449 | short oldrh, filerh; |
| 450 | int ok; |
| 451 | Handle h; |
| 452 | |
| 453 | if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr ) |
| 454 | return 0; /* It doesn't exist */ |
| 455 | if ( FSpGetFInfo(&fss, &finfo) != noErr ) |
| 456 | return 0; /* shouldn't happen, I guess */ |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 457 | oldrh = CurResFile(); |
| 458 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 459 | if ( filerh == -1 ) |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 460 | return 0; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 461 | UseResFile(filerh); |
| 462 | SetResLoad(0); |
| 463 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 464 | SetResLoad(1); |
| 465 | ok = (h != NULL); |
| 466 | CloseResFile(filerh); |
| 467 | UseResFile(oldrh); |
| 468 | return ok; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | ** Load the specified module from a resource |
| 473 | */ |
| 474 | PyObject * |
| 475 | PyMac_LoadResourceModule(module, filename) |
| 476 | char *module; |
| 477 | char *filename; |
| 478 | { |
| 479 | FSSpec fss; |
| 480 | FInfo finfo; |
| 481 | short oldrh, filerh; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 482 | Handle h; |
| 483 | OSErr err; |
| 484 | PyObject *m, *co; |
| 485 | long num, size; |
| 486 | |
| 487 | if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr ) |
| 488 | goto error; |
| 489 | if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr ) |
| 490 | goto error; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 491 | oldrh = CurResFile(); |
| 492 | filerh = FSpOpenResFile(&fss, fsRdPerm); |
| 493 | if ( filerh == -1 ) { |
| 494 | err = ResError(); |
| 495 | goto error; |
| 496 | } |
| 497 | UseResFile(filerh); |
| 498 | h = Get1NamedResource('PYC ', Pstring(module)); |
| 499 | if ( h == NULL ) { |
| 500 | err = ResError(); |
| 501 | goto error; |
| 502 | } |
| 503 | HLock(h); |
| 504 | /* |
| 505 | ** XXXX The next few lines are intimately tied to the format of pyc |
| 506 | ** files. I'm not sure whether this code should be here or in import.c -- Jack |
| 507 | */ |
| 508 | size = GetHandleSize(h); |
| 509 | if ( size < 8 ) { |
| 510 | PyErr_SetString(PyExc_ImportError, "Resource too small"); |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 511 | co = NULL; |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 512 | } else { |
| 513 | num = (*h)[0] & 0xff; |
| 514 | num = num | (((*h)[1] & 0xff) << 8); |
| 515 | num = num | (((*h)[2] & 0xff) << 16); |
| 516 | num = num | (((*h)[3] & 0xff) << 24); |
| 517 | if ( num != PyImport_GetMagicNumber() ) { |
| 518 | PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource"); |
| 519 | co = NULL; |
| 520 | } else { |
| 521 | co = PyMarshal_ReadObjectFromString((*h)+8, size-8); |
| 522 | } |
| 523 | } |
| 524 | HUnlock(h); |
| 525 | CloseResFile(filerh); |
| 526 | UseResFile(oldrh); |
| 527 | if ( co ) { |
| 528 | m = PyImport_ExecCodeModule(module, co); |
| 529 | Py_DECREF(co); |
| 530 | } else { |
| 531 | m = NULL; |
| 532 | } |
| 533 | return m; |
| 534 | error: |
| 535 | { |
| 536 | char buf[512]; |
| 537 | |
Guido van Rossum | c3d1c8e | 1995-02-18 15:01:31 +0000 | [diff] [blame] | 538 | sprintf(buf, "%s: %s", filename, PyMac_StrError(err)); |
Jack Jansen | 74162f3 | 1995-02-15 22:58:33 +0000 | [diff] [blame] | 539 | PyErr_SetString(PyExc_ImportError, buf); |
| 540 | return NULL; |
| 541 | } |
| 542 | } |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 543 | |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 544 | /* |
| 545 | ** Helper routine for GetDirectory |
| 546 | */ |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 547 | static pascal short |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 548 | myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 549 | { |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 550 | if ( item == sfHookFirstCall && dataptr->prompt) { |
| 551 | Handle prompth; |
| 552 | short type; |
| 553 | Rect rect; |
| 554 | |
| 555 | GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect); |
| 556 | if ( prompth ) |
| 557 | SetDialogItemText(prompth, (unsigned char *)dataptr->prompt); |
| 558 | } else |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 559 | if ( item == SELECTCUR_ITEM ) { |
| 560 | item = sfItemCancelButton; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 561 | dataptr->selectcur_hit = 1; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 562 | } |
| 563 | return item; |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | ** Ask the user for a directory. I still can't understand |
| 568 | ** why Apple doesn't provide a standard solution for this... |
| 569 | */ |
| 570 | int |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 571 | PyMac_GetDirectory(dirfss, prompt) |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 572 | FSSpec *dirfss; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 573 | char *prompt; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 574 | { |
| 575 | static SFTypeList list = {'fldr', 0, 0, 0}; |
| 576 | static Point where = {-1, -1}; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 577 | StandardFileReply reply; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 578 | struct hook_args hook_args; |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 579 | |
| 580 | if ( !upp_inited ) { |
| 581 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 582 | upp_inited = 1; |
| 583 | } |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 584 | if ( prompt && *prompt ) |
| 585 | hook_args.prompt = (char *)Pstring(prompt); |
| 586 | else |
| 587 | hook_args.prompt = NULL; |
| 588 | hook_args.selectcur_hit = 0; |
Guido van Rossum | 24a45e3 | 1995-02-20 23:45:53 +0000 | [diff] [blame] | 589 | CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp, |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 590 | NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 591 | |
| 592 | reply.sfFile.name[0] = 0; |
| 593 | if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) ) |
| 594 | return 0; |
Jack Jansen | 819f177 | 1995-08-14 12:35:10 +0000 | [diff] [blame] | 595 | return hook_args.selectcur_hit; |
| 596 | } |
| 597 | |
| 598 | /* |
| 599 | ** Slightly extended StandardGetFile: accepts a prompt */ |
| 600 | void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList, |
| 601 | StandardFileReply *reply, char *prompt) |
| 602 | { |
| 603 | static Point where = {-1, -1}; |
| 604 | struct hook_args hook_args; |
| 605 | |
| 606 | if ( !upp_inited ) { |
| 607 | myhook_upp = NewDlgHookYDProc(myhook_proc); |
| 608 | upp_inited = 1; |
| 609 | } |
| 610 | if ( prompt && *prompt ) |
| 611 | hook_args.prompt = (char *)Pstring(prompt); |
| 612 | else |
| 613 | hook_args.prompt = NULL; |
| 614 | hook_args.selectcur_hit = 0; |
| 615 | CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where, |
| 616 | myhook_upp, NULL, NULL, NULL, (void *)&hook_args); |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 617 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 618 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 619 | /* Convert a 4-char string object argument to an OSType value */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 620 | int |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 621 | PyMac_GetOSType(PyObject *v, OSType *pr) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 622 | { |
| 623 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 624 | PyErr_SetString(PyExc_TypeError, |
| 625 | "OSType arg must be string of 4 chars"); |
| 626 | return 0; |
| 627 | } |
| 628 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 629 | return 1; |
| 630 | } |
| 631 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 632 | /* Convert an OSType value to a 4-char string object */ |
| 633 | PyObject * |
| 634 | PyMac_BuildOSType(OSType t) |
| 635 | { |
| 636 | return PyString_FromStringAndSize((char *)&t, 4); |
| 637 | } |
| 638 | |
| 639 | |
| 640 | /* Convert a Python string object to a Str255 */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 641 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 642 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 643 | { |
| 644 | int len; |
| 645 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 646 | PyErr_SetString(PyExc_TypeError, |
| 647 | "Str255 arg must be string of at most 255 chars"); |
| 648 | return 0; |
| 649 | } |
| 650 | pbuf[0] = len; |
| 651 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 652 | return 1; |
| 653 | } |
| 654 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 655 | /* Convert a Str255 to a Python string object */ |
| 656 | PyObject * |
| 657 | PyMac_BuildStr255(Str255 s) |
| 658 | { |
| 659 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 660 | } |
| 661 | |
| 662 | |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 663 | /* |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 664 | ** Convert a Python object to an FSSpec. |
| 665 | ** The object may either be a full pathname or a triple |
| 666 | ** (vrefnum, dirid, path). |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 667 | ** NOTE: This routine will fail on pre-sys7 machines. |
| 668 | ** The caller is responsible for not calling this routine |
| 669 | ** in those cases (which is fine, since everyone calling |
| 670 | ** this is probably sys7 dependent anyway). |
| 671 | */ |
| 672 | int |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 673 | PyMac_GetFSSpec(PyObject *v, FSSpec *fs) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 674 | { |
| 675 | Str255 path; |
| 676 | short refnum; |
| 677 | long parid; |
| 678 | OSErr err; |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 679 | FSSpec *fs2; |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 680 | |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 681 | /* first check whether it already is an FSSpec */ |
| 682 | fs2 = mfs_GetFSSpecFSSpec(v); |
| 683 | if ( fs2 ) { |
Jack Jansen | 3ec804a | 1995-02-20 15:56:10 +0000 | [diff] [blame] | 684 | (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs); |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 685 | return 1; |
| 686 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 687 | if ( PyString_Check(v) ) { |
| 688 | /* It's a pathname */ |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 689 | if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) ) |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 690 | return 0; |
Jack Jansen | 378815c | 1996-03-06 16:21:34 +0000 | [diff] [blame] | 691 | refnum = 0; /* XXXX Should get CurWD here?? */ |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 692 | parid = 0; |
| 693 | } else { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 694 | if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)", |
| 695 | &refnum, &parid, PyMac_GetStr255, &path)) { |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 696 | return 0; |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 697 | } |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 698 | } |
| 699 | err = FSMakeFSSpec(refnum, parid, path, fs); |
| 700 | if ( err && err != fnfErr ) { |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 701 | PyErr_Mac(PyExc_ValueError, err); |
Jack Jansen | 5f65309 | 1995-01-18 13:53:49 +0000 | [diff] [blame] | 702 | return 0; |
| 703 | } |
| 704 | return 1; |
| 705 | } |
| 706 | |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 707 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 708 | /* Convert a Python object to a Rect. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 709 | The object must be a (left, top, right, bottom) tuple. |
| 710 | (This differs from the order in the struct but is consistent with |
| 711 | the arguments to SetRect(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 712 | int |
| 713 | PyMac_GetRect(PyObject *v, Rect *r) |
Guido van Rossum | 8f69179 | 1995-01-18 23:57:26 +0000 | [diff] [blame] | 714 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 715 | 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] | 716 | } |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 717 | |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 718 | /* Convert a Rect to a Python object */ |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 719 | PyObject * |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 720 | PyMac_BuildRect(Rect *r) |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 721 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 722 | 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] | 723 | } |
| 724 | |
| 725 | |
| 726 | /* Convert a Python object to a Point. |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 727 | The object must be a (h, v) tuple. |
| 728 | (This differs from the order in the struct but is consistent with |
| 729 | the arguments to SetPoint(), and also with STDWIN). */ |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 730 | int |
| 731 | PyMac_GetPoint(PyObject *v, Point *p) |
| 732 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 733 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* Convert a Point to a Python object */ |
| 737 | PyObject * |
| 738 | PyMac_BuildPoint(Point p) |
| 739 | { |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 740 | return Py_BuildValue("(hh)", p.h, p.v); |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | |
| 744 | /* Convert a Python object to an EventRecord. |
| 745 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 746 | int |
| 747 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 748 | { |
| 749 | return PyArg_Parse(v, "(hll(hh)h)", |
| 750 | &e->what, |
| 751 | &e->message, |
| 752 | &e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 753 | &e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 754 | &e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 755 | &e->modifiers); |
| 756 | } |
| 757 | |
| 758 | /* Convert a Rect to an EventRecord object */ |
| 759 | PyObject * |
| 760 | PyMac_BuildEventRecord(EventRecord *e) |
| 761 | { |
| 762 | return Py_BuildValue("(hll(hh)h)", |
| 763 | e->what, |
| 764 | e->message, |
| 765 | e->when, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 766 | e->where.h, |
Guido van Rossum | 5279ec6 | 1995-01-26 22:56:59 +0000 | [diff] [blame] | 767 | e->where.v, |
Guido van Rossum | cf27c2d | 1995-01-25 23:06:44 +0000 | [diff] [blame] | 768 | e->modifiers); |
Guido van Rossum | b340466 | 1995-01-22 18:36:13 +0000 | [diff] [blame] | 769 | } |
Jack Jansen | fa4d5d0 | 1995-11-15 15:19:29 +0000 | [diff] [blame] | 770 | |
| 771 | /* Convert Python object to Fixed */ |
| 772 | int |
| 773 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 774 | { |
| 775 | double d; |
| 776 | |
| 777 | if( !PyArg_Parse(v, "d", &d)) |
| 778 | return 0; |
| 779 | *f = (Fixed)(d * 0x10000); |
| 780 | } |
| 781 | |
| 782 | /* Convert a Point to a Python object */ |
| 783 | PyObject * |
| 784 | PyMac_BuildFixed(Fixed f) |
| 785 | { |
| 786 | double d; |
| 787 | |
| 788 | d = f; |
| 789 | d = d / 0x10000; |
| 790 | return Py_BuildValue("d", d); |
| 791 | } |
| 792 | |