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