blob: f8e59cea6c395ff8164889e287535b6a2d7c2dd4 [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 Jansenf93c72a1994-12-14 14:07:50 +000030#include <Resources.h>
31#include <Memory.h>
32#include <Events.h>
33#include <Windows.h>
34#include <Desk.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000035#include <Traps.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000036#include <Processes.h>
37
38#include <signal.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000039
40#ifdef __MWERKS__
Jack Jansenee23d6e1995-01-27 14:43:25 +000041/*
42** With MW we can pass the event off to the console window, so
43** we might as well handle all events.
44*/
Jack Jansene8e8ae01995-01-26 16:36:45 +000045#include <SIOUX.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000046#define MAINLOOP_EVENTMASK everyEvent
47#else
48/*
49** For other compilers we're more careful, since we can't handle
50** things like updates (and they'll keep coming back if we don't
51** handle them)
52*/
53#define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask)
Jack Jansene8e8ae01995-01-26 16:36:45 +000054#endif /* __MWERKS__ */
55
56#include <signal.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000057
Guido van Rossumb3404661995-01-22 18:36:13 +000058/* XXX We should include Errors.h here, but it has a name conflict
Jack Jansen5f653091995-01-18 13:53:49 +000059** with the python errors.h. */
60#define fnfErr -43
61
Jack Jansene8e8ae01995-01-26 16:36:45 +000062/* Declared in macfsmodule.c: */
63extern FSSpec *mfs_GetFSSpecFSSpec();
64
Jack Jansenee23d6e1995-01-27 14:43:25 +000065/* Interrupt code variables: */
66static int interrupted; /* Set to true when cmd-. seen */
67static RETSIGTYPE intcatcher Py_PROTO((int));
68
Jack Jansene8e8ae01995-01-26 16:36:45 +000069/*
70** We attempt to be a good citizen by giving up the CPU periodically.
71** When in the foreground we do this less often and for shorter periods
72** than when in the background. At this time we also check for events and
Jack Jansenee23d6e1995-01-27 14:43:25 +000073** pass them off to SIOUX, if compiling with mwerks.
Jack Jansene8e8ae01995-01-26 16:36:45 +000074** The counts here are in ticks of 1/60th second.
75** XXXX The initial values here are not based on anything.
76** FG-python gives up the cpu for 1/60th 5 times per second,
77** BG-python for .2 second 10 times per second.
78*/
79static long interval_fg = 12;
80static long interval_bg = 6;
81static long yield_fg = 1;
82static long yield_bg = 12;
Jack Jansenee23d6e1995-01-27 14:43:25 +000083static long lastyield;
84static int in_foreground;
85
86int PyMac_DoYieldEnabled = 1; /* Don't do eventloop when false */
87
Jack Jansene8e8ae01995-01-26 16:36:45 +000088
Jack Jansen5f653091995-01-18 13:53:49 +000089/* Convert C to Pascal string. Returns pointer to static buffer. */
90unsigned char *
91Pstring(char *str)
92{
93 static Str255 buf;
94 int len;
95
96 len = strlen(str);
Guido van Rossum9aa3d131995-01-21 13:46:04 +000097 if (len > 255)
98 len = 255;
Jack Jansen5f653091995-01-18 13:53:49 +000099 buf[0] = (unsigned char)len;
100 strncpy((char *)buf+1, str, len);
101 return buf;
102}
103
Jack Jansenf93c72a1994-12-14 14:07:50 +0000104/* Replace strerror with something that might work */
105char *macstrerror(int err)
106{
107 static char buf[256];
108 Handle h;
109 char *str;
110
111 h = GetResource('Estr', err);
112 if ( h ) {
113 HLock(h);
114 str = (char *)*h;
115 memcpy(buf, str+1, (unsigned char)str[0]);
116 HUnlock(h);
117 ReleaseResource(h);
118 } else {
119 sprintf(buf, "Mac OS error code %d", err);
120 }
121 return buf;
122}
123
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000124/* Exception object shared by all Mac specific modules for Mac OS errors */
125PyObject *PyMac_OSErrException;
126
127/* Initialize and return PyMac_OSErrException */
128PyObject *
129PyMac_GetOSErrException()
130{
131 if (PyMac_OSErrException == NULL)
132 PyMac_OSErrException = PyString_FromString("Mac OS Error");
133 return PyMac_OSErrException;
134}
135
Jack Jansenf93c72a1994-12-14 14:07:50 +0000136/* Set a MAC-specific error from errno, and return NULL; return None if no error */
137PyObject *
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000138PyErr_Mac(PyObject *eobj, int err)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000139{
140 char *msg;
141 PyObject *v;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000142
Guido van Rossum8f691791995-01-18 23:57:26 +0000143 if (err == 0 && !PyErr_Occurred()) {
Jack Jansenf93c72a1994-12-14 14:07:50 +0000144 Py_INCREF(Py_None);
145 return Py_None;
146 }
Guido van Rossum8f691791995-01-18 23:57:26 +0000147 if (err == -1 && PyErr_Occurred())
148 return NULL;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000149 msg = macstrerror(err);
150 v = Py_BuildValue("(is)", err, msg);
151 PyErr_SetObject(eobj, v);
152 Py_DECREF(v);
153 return NULL;
154}
155
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000156/* Call PyErr_Mac with PyMac_OSErrException */
157PyObject *
158PyMac_Error(OSErr err)
159{
160 return PyErr_Mac(PyMac_GetOSErrException(), err);
161}
162
Jack Jansenee23d6e1995-01-27 14:43:25 +0000163/* The catcher routine (which may not be used for all compilers) */
164static RETSIGTYPE
165intcatcher(sig)
166 int sig;
167{
168 interrupted = 1;
169 signal(SIGINT, intcatcher);
170}
171
172void
173PyOS_InitInterrupts()
174{
175 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
176 signal(SIGINT, intcatcher);
177}
178
179/*
180** This routine scans the event queue looking for cmd-.
181** This is the only way to get an interrupt under THINK (since it
182** doesn't do SIGINT handling), but is also used under MW, when
183** the full-fledged event loop is disabled. This way, we can at least
184** interrupt a runaway python program.
185*/
186static void
187scan_event_queue(flush)
188 int flush;
189{
190 register EvQElPtr q;
191
192 q = (EvQElPtr) GetEvQHdr()->qHead;
193
194 for (; q; q = (EvQElPtr)q->qLink) {
195 if (q->evtQWhat == keyDown &&
196 (char)q->evtQMessage == '.' &&
197 (q->evtQModifiers & cmdKey) != 0) {
198 if ( flush )
199 FlushEvents(keyDownMask, 0);
200 interrupted = 1;
201 break;
202 }
203 }
204}
205
206int
207PyOS_InterruptOccurred()
208{
209#ifdef THINK_C
210 scan_event_queue(1);
211#endif
212 PyMac_Yield();
213 if (interrupted) {
214 interrupted = 0;
215 return 1;
216 }
217 return 0;
218}
219
220/* intrpeek() is like intrcheck(), but it doesn't flush the events. The
221** idea is that you call intrpeek() somewhere in a busy-wait loop, and return
222** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon
223** thereafter.
224*/
225static int
226intrpeek()
227{
228#ifdef THINK_C
229 scan_event_queue(0);
230#endif
231 return interrupted;
232}
233
234/* Check whether we are in the foreground */
235int
236PyMac_InForeground()
237{
238 static ProcessSerialNumber ours;
239 static inited;
240 ProcessSerialNumber curfg;
241 Boolean eq;
242
243 if ( inited == 0 )
244 (void)GetCurrentProcess(&ours);
245 inited = 1;
246 if ( GetFrontProcess(&curfg) < 0 )
247 eq = 1;
248 else if ( SameProcess(&ours, &curfg, &eq) < 0 )
249 eq = 1;
250 return (int)eq;
251
252}
253
Jack Jansenf93c72a1994-12-14 14:07:50 +0000254/*
Jack Jansene8e8ae01995-01-26 16:36:45 +0000255** Set yield timeouts
Jack Jansenf93c72a1994-12-14 14:07:50 +0000256*/
Jack Jansene8e8ae01995-01-26 16:36:45 +0000257void
258PyMac_SetYield(long fgi, long fgy, long bgi, long bgy)
259{
260 interval_fg = fgi;
261 yield_fg = fgy;
262 interval_bg = bgi;
263 yield_bg = bgy;
264}
265
266/*
267** Yield the CPU to other tasks.
268*/
269static
270PyMac_DoYield()
Jack Jansenf93c72a1994-12-14 14:07:50 +0000271{
272 EventRecord ev;
273 WindowPtr wp;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000274 long yield;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000275 static int no_waitnextevent = -1;
276 int gotone;
277
278 if ( no_waitnextevent < 0 ) {
279 no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) ==
280 NGetTrapAddress(_Unimplemented, ToolTrap));
281 }
Jack Jansenf93c72a1994-12-14 14:07:50 +0000282
Jack Jansenee23d6e1995-01-27 14:43:25 +0000283 if ( !PyMac_DoYieldEnabled ) {
284#ifndef THINK_C
285 /* Under think this has been done before in intrcheck() or intrpeek() */
286 scan_event_queue(0);
287#endif
Jack Jansene8e8ae01995-01-26 16:36:45 +0000288 return;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000289 }
Jack Jansene8e8ae01995-01-26 16:36:45 +0000290
Jack Jansenee23d6e1995-01-27 14:43:25 +0000291 in_foreground = PyMac_InForeground();
292 if ( in_foreground )
Jack Jansene8e8ae01995-01-26 16:36:45 +0000293 yield = yield_fg;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000294 else
295 yield = yield_bg;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000296 while ( 1 ) {
297 if ( no_waitnextevent ) {
298 SystemTask();
Jack Jansenee23d6e1995-01-27 14:43:25 +0000299 gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000300 } else {
Jack Jansenee23d6e1995-01-27 14:43:25 +0000301 gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000302 }
303 /* Get out quickly if nothing interesting is happening */
304 if ( !gotone || ev.what == nullEvent )
305 break;
306
Jack Jansene8e8ae01995-01-26 16:36:45 +0000307#ifdef __MWERKS__
308 /* If SIOUX wants it we're done too */
309 (void)SIOUXHandleOneEvent(&ev);
310#else
311 /* Other compilers are just unlucky: we only weed out clicks in other applications */
Jack Jansenf93c72a1994-12-14 14:07:50 +0000312 if ( ev.what == mouseDown ) {
313 if ( FindWindow(ev.where, &wp) == inSysWindow )
314 SystemClick(&ev, wp);
315 }
Jack Jansene8e8ae01995-01-26 16:36:45 +0000316#endif /* !__MWERKS__ */
Jack Jansenf93c72a1994-12-14 14:07:50 +0000317 }
Jack Jansene8e8ae01995-01-26 16:36:45 +0000318 lastyield = TickCount();
319}
320
321/*
322** Yield the CPU to other tasks if opportune
323*/
324void
325PyMac_Yield() {
326 long iv;
327
Jack Jansenee23d6e1995-01-27 14:43:25 +0000328 if ( in_foreground )
Jack Jansene8e8ae01995-01-26 16:36:45 +0000329 iv = interval_fg;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000330 else
331 iv = interval_bg;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000332 if ( TickCount() > lastyield + iv )
333 PyMac_DoYield();
334}
335
336/*
337** Idle routine for busy-wait loops.
338** Gives up CPU, handles events and returns true if an interrupt is pending
339** (but not actually handled yet).
340*/
341int
342PyMac_Idle()
343{
344 PyMac_DoYield();
345 return intrpeek();
Jack Jansenf93c72a1994-12-14 14:07:50 +0000346}
347
Jack Jansen5f653091995-01-18 13:53:49 +0000348
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000349/* Convert a 4-char string object argument to an OSType value */
Jack Jansen5f653091995-01-18 13:53:49 +0000350int
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000351PyMac_GetOSType(PyObject *v, OSType *pr)
Jack Jansen5f653091995-01-18 13:53:49 +0000352{
353 if (!PyString_Check(v) || PyString_Size(v) != 4) {
354 PyErr_SetString(PyExc_TypeError,
355 "OSType arg must be string of 4 chars");
356 return 0;
357 }
358 memcpy((char *)pr, PyString_AsString(v), 4);
359 return 1;
360}
361
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000362/* Convert an OSType value to a 4-char string object */
363PyObject *
364PyMac_BuildOSType(OSType t)
365{
366 return PyString_FromStringAndSize((char *)&t, 4);
367}
368
369
370/* Convert a Python string object to a Str255 */
Jack Jansen5f653091995-01-18 13:53:49 +0000371int
Guido van Rossum8f691791995-01-18 23:57:26 +0000372PyMac_GetStr255(PyObject *v, Str255 pbuf)
Jack Jansen5f653091995-01-18 13:53:49 +0000373{
374 int len;
375 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
376 PyErr_SetString(PyExc_TypeError,
377 "Str255 arg must be string of at most 255 chars");
378 return 0;
379 }
380 pbuf[0] = len;
381 memcpy((char *)(pbuf+1), PyString_AsString(v), len);
382 return 1;
383}
384
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000385/* Convert a Str255 to a Python string object */
386PyObject *
387PyMac_BuildStr255(Str255 s)
388{
389 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
390}
391
392
Jack Jansen5f653091995-01-18 13:53:49 +0000393/*
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000394** Convert a Python object to an FSSpec.
395** The object may either be a full pathname or a triple
396** (vrefnum, dirid, path).
Jack Jansen5f653091995-01-18 13:53:49 +0000397** NOTE: This routine will fail on pre-sys7 machines.
398** The caller is responsible for not calling this routine
399** in those cases (which is fine, since everyone calling
400** this is probably sys7 dependent anyway).
401*/
402int
Guido van Rossum8f691791995-01-18 23:57:26 +0000403PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
Jack Jansen5f653091995-01-18 13:53:49 +0000404{
405 Str255 path;
406 short refnum;
407 long parid;
408 OSErr err;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000409 FSSpec *fs2;
Jack Jansen5f653091995-01-18 13:53:49 +0000410
Jack Jansene8e8ae01995-01-26 16:36:45 +0000411 /* first check whether it already is an FSSpec */
412 fs2 = mfs_GetFSSpecFSSpec(v);
413 if ( fs2 ) {
414 fs = fs2;
415 return 1;
416 }
Jack Jansen5f653091995-01-18 13:53:49 +0000417 if ( PyString_Check(v) ) {
418 /* It's a pathname */
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000419 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
Jack Jansen5f653091995-01-18 13:53:49 +0000420 return 0;
421 refnum = 0; /* XXXX Should get CurWD here... */
422 parid = 0;
423 } else {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000424 if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)",
425 &refnum, &parid, PyMac_GetStr255, &path)) {
Jack Jansen5f653091995-01-18 13:53:49 +0000426 return 0;
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000427 }
Jack Jansen5f653091995-01-18 13:53:49 +0000428 }
429 err = FSMakeFSSpec(refnum, parid, path, fs);
430 if ( err && err != fnfErr ) {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000431 PyErr_Mac(PyExc_ValueError, err);
Jack Jansen5f653091995-01-18 13:53:49 +0000432 return 0;
433 }
434 return 1;
435}
436
Guido van Rossum8f691791995-01-18 23:57:26 +0000437
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000438
439/* Convert a Python object to a Rect.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000440 The object must be a (left, top, right, bottom) tuple.
441 (This differs from the order in the struct but is consistent with
442 the arguments to SetRect(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000443int
444PyMac_GetRect(PyObject *v, Rect *r)
Guido van Rossum8f691791995-01-18 23:57:26 +0000445{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000446 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
Guido van Rossum8f691791995-01-18 23:57:26 +0000447}
Guido van Rossumb3404661995-01-22 18:36:13 +0000448
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000449/* Convert a Rect to a Python object */
Guido van Rossumb3404661995-01-22 18:36:13 +0000450PyObject *
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000451PyMac_BuildRect(Rect *r)
Guido van Rossumb3404661995-01-22 18:36:13 +0000452{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000453 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000454}
455
456
457/* Convert a Python object to a Point.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000458 The object must be a (h, v) tuple.
459 (This differs from the order in the struct but is consistent with
460 the arguments to SetPoint(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000461int
462PyMac_GetPoint(PyObject *v, Point *p)
463{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000464 return PyArg_Parse(v, "(hh)", &p->h, &p->v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000465}
466
467/* Convert a Point to a Python object */
468PyObject *
469PyMac_BuildPoint(Point p)
470{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000471 return Py_BuildValue("(hh)", p.h, p.v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000472}
473
474
475/* Convert a Python object to an EventRecord.
476 The object must be a (what, message, when, (v, h), modifiers) tuple. */
477int
478PyMac_GetEventRecord(PyObject *v, EventRecord *e)
479{
480 return PyArg_Parse(v, "(hll(hh)h)",
481 &e->what,
482 &e->message,
483 &e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000484 &e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +0000485 &e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000486 &e->modifiers);
487}
488
489/* Convert a Rect to an EventRecord object */
490PyObject *
491PyMac_BuildEventRecord(EventRecord *e)
492{
493 return Py_BuildValue("(hll(hh)h)",
494 e->what,
495 e->message,
496 e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000497 e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +0000498 e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000499 e->modifiers);
Guido van Rossumb3404661995-01-22 18:36:13 +0000500}