blob: cbeeee71f4ef753d5f27485903dbe0f1b9a8d1b2 [file] [log] [blame]
Guido van Rossumb3404661995-01-22 18:36:13 +00001/***********************************************************
2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
Jack Jansenf93c72a1994-12-14 14:07:50 +000024
25#include "Python.h"
26#include "macglue.h"
27
28#include <OSUtils.h> /* for Set(Current)A5 */
Jack Jansene8e8ae01995-01-26 16:36:45 +000029#include <Files.h>
Jack Jansen8cd2b721995-02-13 11:33:28 +000030#include <Aliases.h>
31#include <StandardFile.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000032#include <Resources.h>
33#include <Memory.h>
34#include <Events.h>
35#include <Windows.h>
36#include <Desk.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000037#include <Traps.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000038#include <Processes.h>
Guido van Rossumcc0d8791995-01-30 08:57:13 +000039#ifdef THINK_C
40#include <OSEvents.h> /* For EvQElPtr */
41#endif
Jack Jansenee23d6e1995-01-27 14:43:25 +000042
43#include <signal.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000044
Jack Jansen8cd2b721995-02-13 11:33:28 +000045#define NOPYTHON_ALERT 128
46#define YES_ITEM 1
47#define NO_ITEM 2
48#define CURWD_ITEM 3
49
Jack Jansene8e8ae01995-01-26 16:36:45 +000050#ifdef __MWERKS__
Jack Jansenee23d6e1995-01-27 14:43:25 +000051/*
52** With MW we can pass the event off to the console window, so
53** we might as well handle all events.
54*/
Jack Jansene8e8ae01995-01-26 16:36:45 +000055#include <SIOUX.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000056#define MAINLOOP_EVENTMASK everyEvent
57#else
58/*
59** For other compilers we're more careful, since we can't handle
60** things like updates (and they'll keep coming back if we don't
61** handle them)
62*/
63#define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask)
Jack Jansene8e8ae01995-01-26 16:36:45 +000064#endif /* __MWERKS__ */
65
66#include <signal.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000067
Guido van Rossumb3404661995-01-22 18:36:13 +000068/* XXX We should include Errors.h here, but it has a name conflict
Jack Jansen5f653091995-01-18 13:53:49 +000069** with the python errors.h. */
70#define fnfErr -43
71
Jack Jansene8e8ae01995-01-26 16:36:45 +000072/* Declared in macfsmodule.c: */
73extern FSSpec *mfs_GetFSSpecFSSpec();
74
Jack Jansenee23d6e1995-01-27 14:43:25 +000075/* Interrupt code variables: */
76static int interrupted; /* Set to true when cmd-. seen */
77static RETSIGTYPE intcatcher Py_PROTO((int));
78
Jack Jansene8e8ae01995-01-26 16:36:45 +000079/*
80** We attempt to be a good citizen by giving up the CPU periodically.
81** When in the foreground we do this less often and for shorter periods
82** than when in the background. At this time we also check for events and
Jack Jansenee23d6e1995-01-27 14:43:25 +000083** pass them off to SIOUX, if compiling with mwerks.
Jack Jansene8e8ae01995-01-26 16:36:45 +000084** The counts here are in ticks of 1/60th second.
85** XXXX The initial values here are not based on anything.
86** FG-python gives up the cpu for 1/60th 5 times per second,
87** BG-python for .2 second 10 times per second.
88*/
89static long interval_fg = 12;
90static long interval_bg = 6;
91static long yield_fg = 1;
92static long yield_bg = 12;
Jack Jansenee23d6e1995-01-27 14:43:25 +000093static long lastyield;
94static int in_foreground;
95
96int PyMac_DoYieldEnabled = 1; /* Don't do eventloop when false */
97
Jack Jansene8e8ae01995-01-26 16:36:45 +000098
Jack Jansen5f653091995-01-18 13:53:49 +000099/* Convert C to Pascal string. Returns pointer to static buffer. */
100unsigned char *
101Pstring(char *str)
102{
103 static Str255 buf;
104 int len;
105
106 len = strlen(str);
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000107 if (len > 255)
108 len = 255;
Jack Jansen5f653091995-01-18 13:53:49 +0000109 buf[0] = (unsigned char)len;
110 strncpy((char *)buf+1, str, len);
111 return buf;
112}
113
Jack Jansenf93c72a1994-12-14 14:07:50 +0000114/* Replace strerror with something that might work */
115char *macstrerror(int err)
116{
117 static char buf[256];
118 Handle h;
119 char *str;
120
121 h = GetResource('Estr', err);
122 if ( h ) {
123 HLock(h);
124 str = (char *)*h;
125 memcpy(buf, str+1, (unsigned char)str[0]);
126 HUnlock(h);
127 ReleaseResource(h);
128 } else {
129 sprintf(buf, "Mac OS error code %d", err);
130 }
131 return buf;
132}
133
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000134/* Exception object shared by all Mac specific modules for Mac OS errors */
135PyObject *PyMac_OSErrException;
136
137/* Initialize and return PyMac_OSErrException */
138PyObject *
139PyMac_GetOSErrException()
140{
141 if (PyMac_OSErrException == NULL)
142 PyMac_OSErrException = PyString_FromString("Mac OS Error");
143 return PyMac_OSErrException;
144}
145
Jack Jansenf93c72a1994-12-14 14:07:50 +0000146/* Set a MAC-specific error from errno, and return NULL; return None if no error */
147PyObject *
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000148PyErr_Mac(PyObject *eobj, int err)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000149{
150 char *msg;
151 PyObject *v;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000152
Guido van Rossum8f691791995-01-18 23:57:26 +0000153 if (err == 0 && !PyErr_Occurred()) {
Jack Jansenf93c72a1994-12-14 14:07:50 +0000154 Py_INCREF(Py_None);
155 return Py_None;
156 }
Guido van Rossum8f691791995-01-18 23:57:26 +0000157 if (err == -1 && PyErr_Occurred())
158 return NULL;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000159 msg = macstrerror(err);
160 v = Py_BuildValue("(is)", err, msg);
161 PyErr_SetObject(eobj, v);
162 Py_DECREF(v);
163 return NULL;
164}
165
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000166/* Call PyErr_Mac with PyMac_OSErrException */
167PyObject *
168PyMac_Error(OSErr err)
169{
170 return PyErr_Mac(PyMac_GetOSErrException(), err);
171}
172
Jack Jansenee23d6e1995-01-27 14:43:25 +0000173/* The catcher routine (which may not be used for all compilers) */
174static RETSIGTYPE
175intcatcher(sig)
176 int sig;
177{
178 interrupted = 1;
179 signal(SIGINT, intcatcher);
180}
181
182void
183PyOS_InitInterrupts()
184{
185 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
186 signal(SIGINT, intcatcher);
187}
188
189/*
190** This routine scans the event queue looking for cmd-.
191** This is the only way to get an interrupt under THINK (since it
192** doesn't do SIGINT handling), but is also used under MW, when
193** the full-fledged event loop is disabled. This way, we can at least
194** interrupt a runaway python program.
195*/
196static void
197scan_event_queue(flush)
198 int flush;
199{
200 register EvQElPtr q;
201
202 q = (EvQElPtr) GetEvQHdr()->qHead;
203
204 for (; q; q = (EvQElPtr)q->qLink) {
205 if (q->evtQWhat == keyDown &&
206 (char)q->evtQMessage == '.' &&
207 (q->evtQModifiers & cmdKey) != 0) {
208 if ( flush )
209 FlushEvents(keyDownMask, 0);
210 interrupted = 1;
211 break;
212 }
213 }
214}
215
216int
217PyOS_InterruptOccurred()
218{
219#ifdef THINK_C
220 scan_event_queue(1);
221#endif
222 PyMac_Yield();
223 if (interrupted) {
224 interrupted = 0;
225 return 1;
226 }
227 return 0;
228}
229
230/* intrpeek() is like intrcheck(), but it doesn't flush the events. The
231** idea is that you call intrpeek() somewhere in a busy-wait loop, and return
232** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon
233** thereafter.
234*/
235static int
236intrpeek()
237{
238#ifdef THINK_C
239 scan_event_queue(0);
240#endif
241 return interrupted;
242}
243
244/* Check whether we are in the foreground */
245int
246PyMac_InForeground()
247{
248 static ProcessSerialNumber ours;
249 static inited;
250 ProcessSerialNumber curfg;
251 Boolean eq;
252
253 if ( inited == 0 )
254 (void)GetCurrentProcess(&ours);
255 inited = 1;
256 if ( GetFrontProcess(&curfg) < 0 )
257 eq = 1;
258 else if ( SameProcess(&ours, &curfg, &eq) < 0 )
259 eq = 1;
260 return (int)eq;
261
262}
263
Jack Jansenf93c72a1994-12-14 14:07:50 +0000264/*
Jack Jansene8e8ae01995-01-26 16:36:45 +0000265** Set yield timeouts
Jack Jansenf93c72a1994-12-14 14:07:50 +0000266*/
Jack Jansene8e8ae01995-01-26 16:36:45 +0000267void
268PyMac_SetYield(long fgi, long fgy, long bgi, long bgy)
269{
270 interval_fg = fgi;
271 yield_fg = fgy;
272 interval_bg = bgi;
273 yield_bg = bgy;
274}
275
276/*
Jack Jansena76382a1995-02-02 14:25:56 +0000277** Handle an event, either one found in the mainloop eventhandler or
278** one passed back from the python program.
279*/
280void
281PyMac_HandleEvent(evp)
282 EventRecord *evp;
283{
284 WindowPtr wp;
285
286#ifdef __MWERKS__
287 /* If SIOUX wants it we're done */
288 (void)SIOUXHandleOneEvent(evp);
289#else
290 /* Other compilers are just unlucky: we only weed out clicks in other applications */
291 if ( evp->what == mouseDown ) {
292 if ( FindWindow(evp->where, &wp) == inSysWindow )
293 SystemClick(evp, wp);
294 }
295#endif /* !__MWERKS__ */
296}
297
298/*
Jack Jansene8e8ae01995-01-26 16:36:45 +0000299** Yield the CPU to other tasks.
300*/
301static
302PyMac_DoYield()
Jack Jansenf93c72a1994-12-14 14:07:50 +0000303{
304 EventRecord ev;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000305 long yield;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000306 static int no_waitnextevent = -1;
307 int gotone;
308
309 if ( no_waitnextevent < 0 ) {
310 no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) ==
311 NGetTrapAddress(_Unimplemented, ToolTrap));
312 }
Jack Jansenf93c72a1994-12-14 14:07:50 +0000313
Jack Jansenee23d6e1995-01-27 14:43:25 +0000314 if ( !PyMac_DoYieldEnabled ) {
315#ifndef THINK_C
316 /* Under think this has been done before in intrcheck() or intrpeek() */
317 scan_event_queue(0);
318#endif
Jack Jansene8e8ae01995-01-26 16:36:45 +0000319 return;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000320 }
Jack Jansene8e8ae01995-01-26 16:36:45 +0000321
Jack Jansenee23d6e1995-01-27 14:43:25 +0000322 in_foreground = PyMac_InForeground();
323 if ( in_foreground )
Jack Jansene8e8ae01995-01-26 16:36:45 +0000324 yield = yield_fg;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000325 else
326 yield = yield_bg;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000327 while ( 1 ) {
328 if ( no_waitnextevent ) {
329 SystemTask();
Jack Jansenee23d6e1995-01-27 14:43:25 +0000330 gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000331 } else {
Jack Jansenee23d6e1995-01-27 14:43:25 +0000332 gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000333 }
334 /* Get out quickly if nothing interesting is happening */
335 if ( !gotone || ev.what == nullEvent )
336 break;
Jack Jansena76382a1995-02-02 14:25:56 +0000337 PyMac_HandleEvent(&ev);
Jack Jansenf93c72a1994-12-14 14:07:50 +0000338 }
Jack Jansene8e8ae01995-01-26 16:36:45 +0000339 lastyield = TickCount();
340}
341
342/*
343** Yield the CPU to other tasks if opportune
344*/
345void
346PyMac_Yield() {
347 long iv;
348
Jack Jansenee23d6e1995-01-27 14:43:25 +0000349 if ( in_foreground )
Jack Jansene8e8ae01995-01-26 16:36:45 +0000350 iv = interval_fg;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000351 else
352 iv = interval_bg;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000353 if ( TickCount() > lastyield + iv )
354 PyMac_DoYield();
355}
356
357/*
358** Idle routine for busy-wait loops.
359** Gives up CPU, handles events and returns true if an interrupt is pending
360** (but not actually handled yet).
361*/
362int
363PyMac_Idle()
364{
365 PyMac_DoYield();
366 return intrpeek();
Jack Jansenf93c72a1994-12-14 14:07:50 +0000367}
368
Jack Jansen8cd2b721995-02-13 11:33:28 +0000369/*
370** Return the name of the Python directory
371*/
372char *
373PyMac_GetPythonDir()
374{
375 int item;
376 static char name[256];
377 AliasHandle handle;
378 FSSpec dirspec;
379 int ok = 0, exists = 0;
380 Boolean modified = 0;
381 StandardFileReply sfreply;
382
383 handle = (AliasHandle)GetResource('alis', 128);
384 if ( handle ) {
385 if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr )
386 ok = 1;
387 exists = 1;
388 }
389 if ( !ok ) {
390 item = Alert(NOPYTHON_ALERT, NULL);
391 if ( item == YES_ITEM ) {
392 StandardGetFile(NULL, 0, NULL, &sfreply);
393 if ( sfreply.sfGood ) {
394 if ( sfreply.sfIsFolder ) {
395 dirspec = sfreply.sfFile;
396 ok = 1;
397 modified = 1;
398 } else {
399 /* User selected a file. Use folder containing it */
400 if (FSMakeFSSpec(sfreply.sfFile.vRefNum,
401 sfreply.sfFile.parID, "\p", &dirspec) == 0) {
402 ok = 1;
403 modified = 1;
404 }
405 }
406 }
407 } else if ( item == CURWD_ITEM ) {
408 if ( getwd(name) ) {
409 if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) {
410 ok = 1;
411 modified = 1;
412 }
413 }
414 }
415 }
416 if ( ok && modified ) {
417 if ( !exists ) {
418 if (NewAlias(NULL, &dirspec, &handle) == 0 )
419 AddResource((Handle)handle, 'alis', 128, "\p");
420 } else {
421 ChangedResource((Handle)handle);
422 }
423 UpdateResFile(CurResFile());
424 }
425 if ( ok ) {
426 ok = (nfullpath(&dirspec, name) == 0);
427 if ( ok ) strcat(name, ":");
428 }
429 if ( !ok ) {
430 /* If all fails, we return the current directory */
431 name[0] = 0;
432 (void)getwd(name);
433 }
434 return name;
435}
436
Jack Jansen5f653091995-01-18 13:53:49 +0000437
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000438/* Convert a 4-char string object argument to an OSType value */
Jack Jansen5f653091995-01-18 13:53:49 +0000439int
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000440PyMac_GetOSType(PyObject *v, OSType *pr)
Jack Jansen5f653091995-01-18 13:53:49 +0000441{
442 if (!PyString_Check(v) || PyString_Size(v) != 4) {
443 PyErr_SetString(PyExc_TypeError,
444 "OSType arg must be string of 4 chars");
445 return 0;
446 }
447 memcpy((char *)pr, PyString_AsString(v), 4);
448 return 1;
449}
450
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000451/* Convert an OSType value to a 4-char string object */
452PyObject *
453PyMac_BuildOSType(OSType t)
454{
455 return PyString_FromStringAndSize((char *)&t, 4);
456}
457
458
459/* Convert a Python string object to a Str255 */
Jack Jansen5f653091995-01-18 13:53:49 +0000460int
Guido van Rossum8f691791995-01-18 23:57:26 +0000461PyMac_GetStr255(PyObject *v, Str255 pbuf)
Jack Jansen5f653091995-01-18 13:53:49 +0000462{
463 int len;
464 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
465 PyErr_SetString(PyExc_TypeError,
466 "Str255 arg must be string of at most 255 chars");
467 return 0;
468 }
469 pbuf[0] = len;
470 memcpy((char *)(pbuf+1), PyString_AsString(v), len);
471 return 1;
472}
473
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000474/* Convert a Str255 to a Python string object */
475PyObject *
476PyMac_BuildStr255(Str255 s)
477{
478 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
479}
480
481
Jack Jansen5f653091995-01-18 13:53:49 +0000482/*
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000483** Convert a Python object to an FSSpec.
484** The object may either be a full pathname or a triple
485** (vrefnum, dirid, path).
Jack Jansen5f653091995-01-18 13:53:49 +0000486** NOTE: This routine will fail on pre-sys7 machines.
487** The caller is responsible for not calling this routine
488** in those cases (which is fine, since everyone calling
489** this is probably sys7 dependent anyway).
490*/
491int
Guido van Rossum8f691791995-01-18 23:57:26 +0000492PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
Jack Jansen5f653091995-01-18 13:53:49 +0000493{
494 Str255 path;
495 short refnum;
496 long parid;
497 OSErr err;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000498 FSSpec *fs2;
Jack Jansen5f653091995-01-18 13:53:49 +0000499
Jack Jansene8e8ae01995-01-26 16:36:45 +0000500 /* first check whether it already is an FSSpec */
501 fs2 = mfs_GetFSSpecFSSpec(v);
502 if ( fs2 ) {
503 fs = fs2;
504 return 1;
505 }
Jack Jansen5f653091995-01-18 13:53:49 +0000506 if ( PyString_Check(v) ) {
507 /* It's a pathname */
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000508 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
Jack Jansen5f653091995-01-18 13:53:49 +0000509 return 0;
510 refnum = 0; /* XXXX Should get CurWD here... */
511 parid = 0;
512 } else {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000513 if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)",
514 &refnum, &parid, PyMac_GetStr255, &path)) {
Jack Jansen5f653091995-01-18 13:53:49 +0000515 return 0;
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000516 }
Jack Jansen5f653091995-01-18 13:53:49 +0000517 }
518 err = FSMakeFSSpec(refnum, parid, path, fs);
519 if ( err && err != fnfErr ) {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000520 PyErr_Mac(PyExc_ValueError, err);
Jack Jansen5f653091995-01-18 13:53:49 +0000521 return 0;
522 }
523 return 1;
524}
525
Guido van Rossum8f691791995-01-18 23:57:26 +0000526
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000527
528/* Convert a Python object to a Rect.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000529 The object must be a (left, top, right, bottom) tuple.
530 (This differs from the order in the struct but is consistent with
531 the arguments to SetRect(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000532int
533PyMac_GetRect(PyObject *v, Rect *r)
Guido van Rossum8f691791995-01-18 23:57:26 +0000534{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000535 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
Guido van Rossum8f691791995-01-18 23:57:26 +0000536}
Guido van Rossumb3404661995-01-22 18:36:13 +0000537
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000538/* Convert a Rect to a Python object */
Guido van Rossumb3404661995-01-22 18:36:13 +0000539PyObject *
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000540PyMac_BuildRect(Rect *r)
Guido van Rossumb3404661995-01-22 18:36:13 +0000541{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000542 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000543}
544
545
546/* Convert a Python object to a Point.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000547 The object must be a (h, v) tuple.
548 (This differs from the order in the struct but is consistent with
549 the arguments to SetPoint(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000550int
551PyMac_GetPoint(PyObject *v, Point *p)
552{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000553 return PyArg_Parse(v, "(hh)", &p->h, &p->v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000554}
555
556/* Convert a Point to a Python object */
557PyObject *
558PyMac_BuildPoint(Point p)
559{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000560 return Py_BuildValue("(hh)", p.h, p.v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000561}
562
563
564/* Convert a Python object to an EventRecord.
565 The object must be a (what, message, when, (v, h), modifiers) tuple. */
566int
567PyMac_GetEventRecord(PyObject *v, EventRecord *e)
568{
569 return PyArg_Parse(v, "(hll(hh)h)",
570 &e->what,
571 &e->message,
572 &e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000573 &e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +0000574 &e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000575 &e->modifiers);
576}
577
578/* Convert a Rect to an EventRecord object */
579PyObject *
580PyMac_BuildEventRecord(EventRecord *e)
581{
582 return Py_BuildValue("(hll(hh)h)",
583 e->what,
584 e->message,
585 e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000586 e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +0000587 e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000588 e->modifiers);
Guido van Rossumb3404661995-01-22 18:36:13 +0000589}