blob: e2ca49464efa68164dec6aaa2e7feec091d0c8e2 [file] [log] [blame]
Guido van Rossumb3404661995-01-22 18:36:13 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossumb3404661995-01-22 18:36:13 +00003The 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
Jack Jansenbf05d4c1996-08-19 15:11:45 +000025#ifdef __CFM68K__
26/* cfm68k InterfaceLib exports GetEventQueue, but Events.h doesn't know this
27** and defines it as GetEvQHdr (which is correct for PPC). This fix is for
28** CW9, check that the workaround is still needed for the next release.
29*/
30#define GetEvQHdr GetEventQueue
31#endif /* __CFM68K__ */
32
33#include <Events.h>
34
35#ifdef __CFM68K__
36#undef GetEventQueue
37#endif /* __CFM68K__ */
38
Jack Jansenf93c72a1994-12-14 14:07:50 +000039#include "Python.h"
Guido van Rossumbecdbec1995-02-14 01:27:24 +000040
Jack Jansenf93c72a1994-12-14 14:07:50 +000041#include "macglue.h"
Jack Jansen74162f31995-02-15 22:58:33 +000042#include "marshal.h"
43#include "import.h"
Jack Jansenf93c72a1994-12-14 14:07:50 +000044
Jack Jansen819f1771995-08-14 12:35:10 +000045#include "pythonresources.h"
46
Jack Jansenf93c72a1994-12-14 14:07:50 +000047#include <OSUtils.h> /* for Set(Current)A5 */
Jack Jansene8e8ae01995-01-26 16:36:45 +000048#include <Files.h>
Jack Jansen8cd2b721995-02-13 11:33:28 +000049#include <StandardFile.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000050#include <Resources.h>
51#include <Memory.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000052#include <Windows.h>
53#include <Desk.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000054#include <Traps.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000055#include <Processes.h>
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +000056#include <Fonts.h>
57#include <Menus.h>
Jack Jansen08305501995-06-18 20:03:40 +000058#include <TextUtils.h>
Guido van Rossumcc0d8791995-01-30 08:57:13 +000059#ifdef THINK_C
60#include <OSEvents.h> /* For EvQElPtr */
61#endif
Jack Jansen16df2aa1995-02-27 16:17:28 +000062#ifdef __MWERKS__
63#include <SIOUX.h>
64#endif
Jack Jansen911ad6b1996-03-05 16:56:24 +000065#ifdef USE_GUSI
66#include <TFileSpec.h> /* For Path2FSSpec */
Jack Jansen378815c1996-03-06 16:21:34 +000067#include <LowMem.h> /* For SetSFCurDir, etc */
Jack Jansenf6865f71996-09-04 15:24:59 +000068#include <GUSI.h>
Jack Jansen911ad6b1996-03-05 16:56:24 +000069#endif
Jack Jansenee23d6e1995-01-27 14:43:25 +000070
Jack Jansen3469e991996-09-06 00:30:45 +000071/* The ID of the Sioux apple menu */
72#define SIOUX_APPLEID 32000
73
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +000074#ifndef HAVE_UNIVERSAL_HEADERS
75#define GetResourceSizeOnDisk(x) SizeResource(x)
Guido van Rossum24a45e31995-02-20 23:45:53 +000076typedef DlgHookYDProcPtr DlgHookYDUPP;
77#define NewDlgHookYDProc(userRoutine) ((DlgHookYDUPP) (userRoutine))
78typedef FileFilterYDProcPtr FileFilterYDUPP;
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +000079#endif
80
Jack Jansenee23d6e1995-01-27 14:43:25 +000081#include <signal.h>
Jack Jansen74162f31995-02-15 22:58:33 +000082#include <stdio.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000083
Jack Jansenee23d6e1995-01-27 14:43:25 +000084/*
Jack Jansend1f06311996-08-01 15:23:54 +000085** When less than this amount of stackspace is left we
86** raise a MemoryError.
87*/
88#ifndef MINIMUM_STACK_SIZE
89#ifdef __powerc
90#define MINIMUM_STACK_SIZE 8192
91#else
92#define MINIMUM_STACK_SIZE 4096
93#endif
94#endif
95
96/*
Jack Jansen16df2aa1995-02-27 16:17:28 +000097** We have to be careful, since we can't handle
Jack Jansenee23d6e1995-01-27 14:43:25 +000098** things like updates (and they'll keep coming back if we don't
Jack Jansen16df2aa1995-02-27 16:17:28 +000099** handle them). Note that we don't know who has windows open, so
100** even handing updates off to SIOUX under MW isn't going to work.
Jack Jansenee23d6e1995-01-27 14:43:25 +0000101*/
102#define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask)
Jack Jansene8e8ae01995-01-26 16:36:45 +0000103
104#include <signal.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +0000105
Guido van Rossumb3404661995-01-22 18:36:13 +0000106/* XXX We should include Errors.h here, but it has a name conflict
Jack Jansen5f653091995-01-18 13:53:49 +0000107** with the python errors.h. */
108#define fnfErr -43
109
Jack Jansene8e8ae01995-01-26 16:36:45 +0000110/* Declared in macfsmodule.c: */
111extern FSSpec *mfs_GetFSSpecFSSpec();
112
Jack Jansenee23d6e1995-01-27 14:43:25 +0000113/* Interrupt code variables: */
114static int interrupted; /* Set to true when cmd-. seen */
115static RETSIGTYPE intcatcher Py_PROTO((int));
116
Jack Jansenf6865f71996-09-04 15:24:59 +0000117static void PyMac_DoYield Py_PROTO((int));
118
Jack Jansene8e8ae01995-01-26 16:36:45 +0000119/*
120** We attempt to be a good citizen by giving up the CPU periodically.
121** When in the foreground we do this less often and for shorter periods
122** than when in the background. At this time we also check for events and
Jack Jansenee23d6e1995-01-27 14:43:25 +0000123** pass them off to SIOUX, if compiling with mwerks.
Jack Jansene8e8ae01995-01-26 16:36:45 +0000124** The counts here are in ticks of 1/60th second.
125** XXXX The initial values here are not based on anything.
126** FG-python gives up the cpu for 1/60th 5 times per second,
127** BG-python for .2 second 10 times per second.
128*/
129static long interval_fg = 12;
130static long interval_bg = 6;
131static long yield_fg = 1;
Jack Jansend617c571996-09-22 22:14:30 +0000132static long yield_bg = 2;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000133static long lastyield;
134static int in_foreground;
135
Guido van Rossume7134aa1995-02-26 10:20:53 +0000136/*
137** When > 0, do full scanning for events (program is not event aware)
138** when == 0, only scan for Command-period
139** when < 0, don't do any event scanning
140*/
141int PyMac_DoYieldEnabled = 1;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000142
Jack Jansen819f1771995-08-14 12:35:10 +0000143/*
Jack Jansenf6865f71996-09-04 15:24:59 +0000144** Workaround for sioux/gusi combo: set when we are exiting
145*/
146int PyMac_ConsoleIsDead;
147
148/*
Jack Jansen819f1771995-08-14 12:35:10 +0000149** Some stuff for our GetDirectory and PromptGetFile routines
150*/
151struct hook_args {
152 int selectcur_hit; /* Set to true when "select current" selected */
153 char *prompt; /* The prompt */
154};
155static DlgHookYDUPP myhook_upp;
156static int upp_inited = 0;
157
Jack Jansen378815c1996-03-06 16:21:34 +0000158#ifdef USE_GUSI
159/*
160** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of
161** the working directory. Hence, we call this routine after each call
162** to chdir() to rectify things.
163*/
164void
165PyMac_FixGUSIcd()
166{
167 WDPBRec pb;
168 FSSpec curdirfss;
169
170 if ( Path2FSSpec(":x", &curdirfss) != noErr )
171 return;
172
173 /* Set MacOS "working directory" */
174 pb.ioNamePtr= "\p";
175 pb.ioVRefNum= curdirfss.vRefNum;
176 pb.ioWDDirID= curdirfss.parID;
177 if (PBHSetVol(&pb, 0) != noErr)
178 return;
Jack Jansen378815c1996-03-06 16:21:34 +0000179}
Jack Jansen7ac70af1996-08-19 11:01:05 +0000180
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000181/*
182** SpinCursor (needed by GUSI) drags in heaps of stuff, so we
183** provide a dummy here.
184*/
Jack Jansencfadbd41996-08-19 11:36:25 +0000185void SpinCursor(short x) { /* Dummy */ }
Jack Jansencfadbd41996-08-19 11:36:25 +0000186
Jack Jansenf6865f71996-09-04 15:24:59 +0000187/*
188** Replacement GUSI Spin function
189*/
190static int
191PyMac_GUSISpin(spin_msg msg, long arg)
192{
193 static Boolean inForeground = true;
Jack Jansenf6865f71996-09-04 15:24:59 +0000194 int maysleep;
195
196 if (PyMac_ConsoleIsDead) return 0;
197#if 0
198 if (inForeground)
199 SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1);
200#endif
201
202 if (interrupted) return -1;
203
204 if ( msg == SP_AUTO_SPIN || ((msg==SP_SLEEP||msg==SP_SELECT) && arg <= yield_fg))
205 maysleep = 0;
206 else
207 maysleep = 0;
208
209 PyMac_DoYield(maysleep);
210
211 return 0;
212}
213
214void
215PyMac_SetGUSISpin() {
216 GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin);
217}
218
Jack Jansen378815c1996-03-06 16:21:34 +0000219#endif
220
Jack Jansen819f1771995-08-14 12:35:10 +0000221
Jack Jansen5f653091995-01-18 13:53:49 +0000222/* Convert C to Pascal string. Returns pointer to static buffer. */
223unsigned char *
224Pstring(char *str)
225{
226 static Str255 buf;
227 int len;
228
229 len = strlen(str);
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000230 if (len > 255)
231 len = 255;
Jack Jansen5f653091995-01-18 13:53:49 +0000232 buf[0] = (unsigned char)len;
233 strncpy((char *)buf+1, str, len);
234 return buf;
235}
236
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +0000237/* Like strerror() but for Mac OS error numbers */
238char *PyMac_StrError(int err)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000239{
240 static char buf[256];
241 Handle h;
242 char *str;
243
244 h = GetResource('Estr', err);
245 if ( h ) {
246 HLock(h);
247 str = (char *)*h;
248 memcpy(buf, str+1, (unsigned char)str[0]);
Guido van Rossumcc9bc8f1995-02-13 16:17:03 +0000249 buf[(unsigned char)str[0]] = '\0';
Jack Jansenf93c72a1994-12-14 14:07:50 +0000250 HUnlock(h);
251 ReleaseResource(h);
252 } else {
253 sprintf(buf, "Mac OS error code %d", err);
254 }
255 return buf;
256}
257
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000258/* Exception object shared by all Mac specific modules for Mac OS errors */
259PyObject *PyMac_OSErrException;
260
261/* Initialize and return PyMac_OSErrException */
262PyObject *
263PyMac_GetOSErrException()
264{
265 if (PyMac_OSErrException == NULL)
266 PyMac_OSErrException = PyString_FromString("Mac OS Error");
267 return PyMac_OSErrException;
268}
269
Jack Jansenf93c72a1994-12-14 14:07:50 +0000270/* Set a MAC-specific error from errno, and return NULL; return None if no error */
271PyObject *
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000272PyErr_Mac(PyObject *eobj, int err)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000273{
274 char *msg;
275 PyObject *v;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000276
Guido van Rossum8f691791995-01-18 23:57:26 +0000277 if (err == 0 && !PyErr_Occurred()) {
Jack Jansenf93c72a1994-12-14 14:07:50 +0000278 Py_INCREF(Py_None);
279 return Py_None;
280 }
Guido van Rossum8f691791995-01-18 23:57:26 +0000281 if (err == -1 && PyErr_Occurred())
282 return NULL;
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +0000283 msg = PyMac_StrError(err);
Jack Jansenf93c72a1994-12-14 14:07:50 +0000284 v = Py_BuildValue("(is)", err, msg);
285 PyErr_SetObject(eobj, v);
286 Py_DECREF(v);
287 return NULL;
288}
289
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000290/* Call PyErr_Mac with PyMac_OSErrException */
291PyObject *
292PyMac_Error(OSErr err)
293{
294 return PyErr_Mac(PyMac_GetOSErrException(), err);
295}
296
Jack Jansen1ed95291996-07-22 15:25:10 +0000297#ifdef USE_STACKCHECK
298/* Check for stack overflow */
299int
300PyOS_CheckStack()
301{
302 long left;
303
304 left = StackSpace();
Jack Jansend1f06311996-08-01 15:23:54 +0000305 if ( left < MINIMUM_STACK_SIZE )
Jack Jansen1ed95291996-07-22 15:25:10 +0000306 return -1;
307 return 0;
308}
309#endif /* USE_STACKCHECK */
310
Jack Jansenee23d6e1995-01-27 14:43:25 +0000311/* The catcher routine (which may not be used for all compilers) */
312static RETSIGTYPE
313intcatcher(sig)
314 int sig;
315{
316 interrupted = 1;
317 signal(SIGINT, intcatcher);
318}
319
320void
321PyOS_InitInterrupts()
322{
323 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
324 signal(SIGINT, intcatcher);
325}
326
327/*
328** This routine scans the event queue looking for cmd-.
329** This is the only way to get an interrupt under THINK (since it
330** doesn't do SIGINT handling), but is also used under MW, when
331** the full-fledged event loop is disabled. This way, we can at least
332** interrupt a runaway python program.
333*/
334static void
335scan_event_queue(flush)
336 int flush;
337{
338 register EvQElPtr q;
339
Jack Jansencfadbd41996-08-19 11:36:25 +0000340 q = (EvQElPtr) GetEventQueue()->qHead;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000341
342 for (; q; q = (EvQElPtr)q->qLink) {
343 if (q->evtQWhat == keyDown &&
344 (char)q->evtQMessage == '.' &&
345 (q->evtQModifiers & cmdKey) != 0) {
346 if ( flush )
347 FlushEvents(keyDownMask, 0);
348 interrupted = 1;
349 break;
350 }
351 }
352}
353
354int
355PyOS_InterruptOccurred()
356{
Guido van Rossume7134aa1995-02-26 10:20:53 +0000357 if (PyMac_DoYieldEnabled < 0)
358 return 0;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000359#ifdef THINK_C
360 scan_event_queue(1);
361#endif
362 PyMac_Yield();
363 if (interrupted) {
364 interrupted = 0;
365 return 1;
366 }
367 return 0;
368}
369
370/* intrpeek() is like intrcheck(), but it doesn't flush the events. The
371** idea is that you call intrpeek() somewhere in a busy-wait loop, and return
372** None as soon as it returns 1. The mainloop will then pick up the cmd-. soon
373** thereafter.
374*/
375static int
376intrpeek()
377{
378#ifdef THINK_C
379 scan_event_queue(0);
380#endif
381 return interrupted;
382}
383
384/* Check whether we are in the foreground */
385int
386PyMac_InForeground()
387{
388 static ProcessSerialNumber ours;
389 static inited;
390 ProcessSerialNumber curfg;
391 Boolean eq;
392
393 if ( inited == 0 )
394 (void)GetCurrentProcess(&ours);
395 inited = 1;
396 if ( GetFrontProcess(&curfg) < 0 )
397 eq = 1;
398 else if ( SameProcess(&ours, &curfg, &eq) < 0 )
399 eq = 1;
400 return (int)eq;
401
402}
403
Jack Jansenf93c72a1994-12-14 14:07:50 +0000404/*
Jack Jansene8e8ae01995-01-26 16:36:45 +0000405** Set yield timeouts
Jack Jansenf93c72a1994-12-14 14:07:50 +0000406*/
Jack Jansene8e8ae01995-01-26 16:36:45 +0000407void
408PyMac_SetYield(long fgi, long fgy, long bgi, long bgy)
409{
410 interval_fg = fgi;
411 yield_fg = fgy;
412 interval_bg = bgi;
413 yield_bg = bgy;
414}
415
416/*
Jack Jansena76382a1995-02-02 14:25:56 +0000417** Handle an event, either one found in the mainloop eventhandler or
418** one passed back from the python program.
419*/
420void
421PyMac_HandleEvent(evp)
422 EventRecord *evp;
423{
Jack Jansena76382a1995-02-02 14:25:56 +0000424
425#ifdef __MWERKS__
Jack Jansen38e97661995-11-10 14:53:00 +0000426 {
427 int siouxdidit;
428
429 /* If SIOUX wants it we're done */
430 siouxdidit = SIOUXHandleOneEvent(evp);
431 if ( siouxdidit )
432 return;
433 }
Jack Jansena76382a1995-02-02 14:25:56 +0000434#else
435 /* Other compilers are just unlucky: we only weed out clicks in other applications */
436 if ( evp->what == mouseDown ) {
Jack Jansen847e89e1995-08-31 13:57:40 +0000437 WindowPtr wp;
438
Jack Jansen38e97661995-11-10 14:53:00 +0000439 if ( FindWindow(evp->where, &wp) == inSysWindow ) {
Jack Jansena76382a1995-02-02 14:25:56 +0000440 SystemClick(evp, wp);
Jack Jansen38e97661995-11-10 14:53:00 +0000441 return;
442 }
Jack Jansena76382a1995-02-02 14:25:56 +0000443 }
444#endif /* !__MWERKS__ */
445}
446
447/*
Jack Jansene8e8ae01995-01-26 16:36:45 +0000448** Yield the CPU to other tasks.
449*/
Jack Jansencfadbd41996-08-19 11:36:25 +0000450static void
Jack Jansenf6865f71996-09-04 15:24:59 +0000451PyMac_DoYield(int maysleep)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000452{
453 EventRecord ev;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000454 long yield;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000455 static int no_waitnextevent = -1;
456 int gotone;
457
458 if ( no_waitnextevent < 0 ) {
459 no_waitnextevent = (NGetTrapAddress(_WaitNextEvent, ToolTrap) ==
460 NGetTrapAddress(_Unimplemented, ToolTrap));
461 }
Jack Jansenf93c72a1994-12-14 14:07:50 +0000462
Jack Jansenf6865f71996-09-04 15:24:59 +0000463 lastyield = TickCount();
Jack Jansenee23d6e1995-01-27 14:43:25 +0000464#ifndef THINK_C
Jack Jansenf6865f71996-09-04 15:24:59 +0000465 /* Under think this has been done before in intrcheck() or intrpeek() */
466 if (PyMac_DoYieldEnabled >= 0)
Jack Jansenee23d6e1995-01-27 14:43:25 +0000467 scan_event_queue(0);
468#endif
Jack Jansenf6865f71996-09-04 15:24:59 +0000469 if (PyMac_DoYieldEnabled == 0)
Jack Jansene8e8ae01995-01-26 16:36:45 +0000470 return;
471
Jack Jansenee23d6e1995-01-27 14:43:25 +0000472 in_foreground = PyMac_InForeground();
Jack Jansenf6865f71996-09-04 15:24:59 +0000473 if ( maysleep ) {
474 if ( in_foreground )
475 yield = yield_fg;
476 else
477 yield = yield_bg;
478 } else {
479 yield = 0;
480 }
481
Jack Jansene8e8ae01995-01-26 16:36:45 +0000482 while ( 1 ) {
483 if ( no_waitnextevent ) {
484 SystemTask();
Jack Jansenee23d6e1995-01-27 14:43:25 +0000485 gotone = GetNextEvent(MAINLOOP_EVENTMASK, &ev);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000486 } else {
Jack Jansenee23d6e1995-01-27 14:43:25 +0000487 gotone = WaitNextEvent(MAINLOOP_EVENTMASK, &ev, yield, NULL);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000488 }
489 /* Get out quickly if nothing interesting is happening */
490 if ( !gotone || ev.what == nullEvent )
491 break;
Jack Jansena76382a1995-02-02 14:25:56 +0000492 PyMac_HandleEvent(&ev);
Jack Jansenf93c72a1994-12-14 14:07:50 +0000493 }
Jack Jansene8e8ae01995-01-26 16:36:45 +0000494}
495
496/*
497** Yield the CPU to other tasks if opportune
498*/
499void
500PyMac_Yield() {
501 long iv;
502
Jack Jansenee23d6e1995-01-27 14:43:25 +0000503 if ( in_foreground )
Jack Jansene8e8ae01995-01-26 16:36:45 +0000504 iv = interval_fg;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000505 else
506 iv = interval_bg;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000507 if ( TickCount() > lastyield + iv )
Jack Jansenf6865f71996-09-04 15:24:59 +0000508 PyMac_DoYield(1);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000509}
510
Jack Jansenf6865f71996-09-04 15:24:59 +0000511#ifdef USE_MACTCP
Jack Jansene8e8ae01995-01-26 16:36:45 +0000512/*
513** Idle routine for busy-wait loops.
514** Gives up CPU, handles events and returns true if an interrupt is pending
515** (but not actually handled yet).
516*/
517int
518PyMac_Idle()
519{
Jack Jansenf6865f71996-09-04 15:24:59 +0000520 PyMac_DoYield(1);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000521 return intrpeek();
Jack Jansenf93c72a1994-12-14 14:07:50 +0000522}
Jack Jansenf6865f71996-09-04 15:24:59 +0000523#endif
524
Jack Jansen74162f31995-02-15 22:58:33 +0000525/*
Jack Jansen3469e991996-09-06 00:30:45 +0000526** Install our menu bar.
527*/
528void
529PyMac_InitMenuBar()
530{
531 Handle bar;
532 MenuHandle applemenu;
533
534 if ( (bar=GetMenuBar()) == NULL ) return;
535 if ( (applemenu=GetMHandle(SIOUX_APPLEID)) == NULL ) return;
536 SetMenuItemText(applemenu, 1, "\pAbout Python...");
537}
538
539/*
540** Our replacement about box
541*/
542void
543SIOUXDoAboutBox(void)
544{
545 DialogPtr theDialog;
Jack Jansend617c571996-09-22 22:14:30 +0000546 WindowRef theWindow;
547 CGrafPtr thePort;
Jack Jansen3469e991996-09-06 00:30:45 +0000548 short item;
Jack Jansend617c571996-09-22 22:14:30 +0000549 short xpos, ypos, width, height, swidth, sheight;
Jack Jansen3469e991996-09-06 00:30:45 +0000550
551 if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL )
552 return;
Jack Jansend617c571996-09-22 22:14:30 +0000553 theWindow = GetDialogWindow(theDialog);
554 thePort = GetWindowPort(theWindow);
555 width = thePort->portRect.right - thePort->portRect.left;
556 height = thePort->portRect.bottom - thePort->portRect.top;
557 swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
558 sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight();
559 xpos = (swidth-width)/2;
Jack Jansen0e06e7e1996-09-23 15:51:06 +0000560 ypos = (sheight-height)/5 + LMGetMBarHeight();
Jack Jansend617c571996-09-22 22:14:30 +0000561 MoveWindow(theWindow, xpos, ypos, 0);
562 ShowWindow(theWindow);
Jack Jansen3469e991996-09-06 00:30:45 +0000563 ModalDialog(NULL, &item);
564 DisposeDialog(theDialog);
565}
566
567/*
Jack Jansen74162f31995-02-15 22:58:33 +0000568** Returns true if the argument has a resource fork, and it contains
569** a 'PYC ' resource of the correct name
570*/
571int
572PyMac_FindResourceModule(module, filename)
573char *module;
574char *filename;
575{
576 FSSpec fss;
577 FInfo finfo;
578 short oldrh, filerh;
579 int ok;
580 Handle h;
581
Jack Jansen8096daa1996-11-09 18:43:44 +0000582 if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
583 /*
584 ** Special case: the application itself. Use a shortcut to
585 ** forestall opening and closing the application numerous times
586 ** (which is dead slow when running from CDROM)
587 */
588 oldrh = CurResFile();
589 UseResFile(PyMac_AppRefNum);
590 filerh = -1;
591 } else {
592 if ( FSMakeFSSpec(0, 0, Pstring(filename), &fss) != noErr )
593 return 0; /* It doesn't exist */
594 if ( FSpGetFInfo(&fss, &finfo) != noErr )
595 return 0; /* shouldn't happen, I guess */
596 oldrh = CurResFile();
597 filerh = FSpOpenResFile(&fss, fsRdPerm);
598 if ( filerh == -1 )
599 return 0;
600 UseResFile(filerh);
601 }
Jack Jansen74162f31995-02-15 22:58:33 +0000602 SetResLoad(0);
603 h = Get1NamedResource('PYC ', Pstring(module));
604 SetResLoad(1);
605 ok = (h != NULL);
Jack Jansen8096daa1996-11-09 18:43:44 +0000606 if ( filerh != -1 )
607 CloseResFile(filerh);
Jack Jansen74162f31995-02-15 22:58:33 +0000608 UseResFile(oldrh);
609 return ok;
610}
611
612/*
613** Load the specified module from a resource
614*/
615PyObject *
616PyMac_LoadResourceModule(module, filename)
617char *module;
618char *filename;
619{
620 FSSpec fss;
621 FInfo finfo;
622 short oldrh, filerh;
Jack Jansen74162f31995-02-15 22:58:33 +0000623 Handle h;
624 OSErr err;
625 PyObject *m, *co;
626 long num, size;
627
Jack Jansen8096daa1996-11-09 18:43:44 +0000628 if ( strcmp(filename, PyMac_ApplicationPath) == 0 ) {
629 /*
630 ** Special case: the application itself. Use a shortcut to
631 ** forestall opening and closing the application numerous times
632 ** (which is dead slow when running from CDROM)
633 */
634 oldrh = CurResFile();
635 UseResFile(PyMac_AppRefNum);
636 filerh = -1;
637 } else {
638 if ( (err=FSMakeFSSpec(0, 0, Pstring(filename), &fss)) != noErr )
639 goto error;
640 if ( (err=FSpGetFInfo(&fss, &finfo)) != noErr )
641 goto error;
642 oldrh = CurResFile();
643 filerh = FSpOpenResFile(&fss, fsRdPerm);
644 if ( filerh == -1 ) {
645 err = ResError();
646 goto error;
647 }
648 UseResFile(filerh);
Jack Jansen74162f31995-02-15 22:58:33 +0000649 }
Jack Jansen74162f31995-02-15 22:58:33 +0000650 h = Get1NamedResource('PYC ', Pstring(module));
651 if ( h == NULL ) {
652 err = ResError();
653 goto error;
654 }
655 HLock(h);
656 /*
657 ** XXXX The next few lines are intimately tied to the format of pyc
658 ** files. I'm not sure whether this code should be here or in import.c -- Jack
659 */
660 size = GetHandleSize(h);
661 if ( size < 8 ) {
662 PyErr_SetString(PyExc_ImportError, "Resource too small");
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +0000663 co = NULL;
Jack Jansen74162f31995-02-15 22:58:33 +0000664 } else {
665 num = (*h)[0] & 0xff;
666 num = num | (((*h)[1] & 0xff) << 8);
667 num = num | (((*h)[2] & 0xff) << 16);
668 num = num | (((*h)[3] & 0xff) << 24);
669 if ( num != PyImport_GetMagicNumber() ) {
670 PyErr_SetString(PyExc_ImportError, "Bad MAGIC in resource");
671 co = NULL;
672 } else {
673 co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
674 }
675 }
676 HUnlock(h);
Jack Jansen8096daa1996-11-09 18:43:44 +0000677 if ( filerh != -1 )
678 CloseResFile(filerh);
Jack Jansen74162f31995-02-15 22:58:33 +0000679 UseResFile(oldrh);
680 if ( co ) {
681 m = PyImport_ExecCodeModule(module, co);
682 Py_DECREF(co);
683 } else {
684 m = NULL;
685 }
686 return m;
687error:
688 {
689 char buf[512];
690
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +0000691 sprintf(buf, "%s: %s", filename, PyMac_StrError(err));
Jack Jansen74162f31995-02-15 22:58:33 +0000692 PyErr_SetString(PyExc_ImportError, buf);
693 return NULL;
694 }
695}
Guido van Rossum24a45e31995-02-20 23:45:53 +0000696
Jack Jansen3ec804a1995-02-20 15:56:10 +0000697/*
698** Helper routine for GetDirectory
699*/
Guido van Rossum24a45e31995-02-20 23:45:53 +0000700static pascal short
Jack Jansen819f1771995-08-14 12:35:10 +0000701myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr)
Jack Jansen3ec804a1995-02-20 15:56:10 +0000702{
Jack Jansen819f1771995-08-14 12:35:10 +0000703 if ( item == sfHookFirstCall && dataptr->prompt) {
704 Handle prompth;
705 short type;
706 Rect rect;
707
708 GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect);
709 if ( prompth )
710 SetDialogItemText(prompth, (unsigned char *)dataptr->prompt);
711 } else
Jack Jansen3ec804a1995-02-20 15:56:10 +0000712 if ( item == SELECTCUR_ITEM ) {
713 item = sfItemCancelButton;
Jack Jansen819f1771995-08-14 12:35:10 +0000714 dataptr->selectcur_hit = 1;
Jack Jansen3ec804a1995-02-20 15:56:10 +0000715 }
716 return item;
717}
718
719/*
720** Ask the user for a directory. I still can't understand
721** why Apple doesn't provide a standard solution for this...
722*/
723int
Jack Jansen819f1771995-08-14 12:35:10 +0000724PyMac_GetDirectory(dirfss, prompt)
Jack Jansen3ec804a1995-02-20 15:56:10 +0000725 FSSpec *dirfss;
Jack Jansen819f1771995-08-14 12:35:10 +0000726 char *prompt;
Jack Jansen3ec804a1995-02-20 15:56:10 +0000727{
728 static SFTypeList list = {'fldr', 0, 0, 0};
729 static Point where = {-1, -1};
Jack Jansen3ec804a1995-02-20 15:56:10 +0000730 StandardFileReply reply;
Jack Jansen819f1771995-08-14 12:35:10 +0000731 struct hook_args hook_args;
Jack Jansen3ec804a1995-02-20 15:56:10 +0000732
733 if ( !upp_inited ) {
734 myhook_upp = NewDlgHookYDProc(myhook_proc);
735 upp_inited = 1;
736 }
Jack Jansen819f1771995-08-14 12:35:10 +0000737 if ( prompt && *prompt )
738 hook_args.prompt = (char *)Pstring(prompt);
739 else
740 hook_args.prompt = NULL;
741 hook_args.selectcur_hit = 0;
Guido van Rossum24a45e31995-02-20 23:45:53 +0000742 CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp,
Jack Jansen819f1771995-08-14 12:35:10 +0000743 NULL, NULL, NULL, (void *)&hook_args);
Jack Jansen3ec804a1995-02-20 15:56:10 +0000744
745 reply.sfFile.name[0] = 0;
746 if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) )
747 return 0;
Jack Jansen819f1771995-08-14 12:35:10 +0000748 return hook_args.selectcur_hit;
749}
750
751/*
752** Slightly extended StandardGetFile: accepts a prompt */
753void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList,
754 StandardFileReply *reply, char *prompt)
755{
756 static Point where = {-1, -1};
757 struct hook_args hook_args;
758
759 if ( !upp_inited ) {
760 myhook_upp = NewDlgHookYDProc(myhook_proc);
761 upp_inited = 1;
762 }
763 if ( prompt && *prompt )
764 hook_args.prompt = (char *)Pstring(prompt);
765 else
766 hook_args.prompt = NULL;
767 hook_args.selectcur_hit = 0;
768 CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where,
769 myhook_upp, NULL, NULL, NULL, (void *)&hook_args);
Jack Jansen3ec804a1995-02-20 15:56:10 +0000770}
Jack Jansen5f653091995-01-18 13:53:49 +0000771
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000772/* Convert a 4-char string object argument to an OSType value */
Jack Jansen5f653091995-01-18 13:53:49 +0000773int
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000774PyMac_GetOSType(PyObject *v, OSType *pr)
Jack Jansen5f653091995-01-18 13:53:49 +0000775{
776 if (!PyString_Check(v) || PyString_Size(v) != 4) {
777 PyErr_SetString(PyExc_TypeError,
778 "OSType arg must be string of 4 chars");
779 return 0;
780 }
781 memcpy((char *)pr, PyString_AsString(v), 4);
782 return 1;
783}
784
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000785/* Convert an OSType value to a 4-char string object */
786PyObject *
787PyMac_BuildOSType(OSType t)
788{
789 return PyString_FromStringAndSize((char *)&t, 4);
790}
791
Jack Jansend1f06311996-08-01 15:23:54 +0000792/* Convert an NumVersion value to a 4-element tuple */
793PyObject *
794PyMac_BuildNumVersion(NumVersion t)
795{
796 return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev);
797}
798
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000799
800/* Convert a Python string object to a Str255 */
Jack Jansen5f653091995-01-18 13:53:49 +0000801int
Guido van Rossum8f691791995-01-18 23:57:26 +0000802PyMac_GetStr255(PyObject *v, Str255 pbuf)
Jack Jansen5f653091995-01-18 13:53:49 +0000803{
804 int len;
805 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
806 PyErr_SetString(PyExc_TypeError,
807 "Str255 arg must be string of at most 255 chars");
808 return 0;
809 }
810 pbuf[0] = len;
811 memcpy((char *)(pbuf+1), PyString_AsString(v), len);
812 return 1;
813}
814
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000815/* Convert a Str255 to a Python string object */
816PyObject *
817PyMac_BuildStr255(Str255 s)
818{
819 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
820}
821
822
Jack Jansen5f653091995-01-18 13:53:49 +0000823/*
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000824** Convert a Python object to an FSSpec.
825** The object may either be a full pathname or a triple
826** (vrefnum, dirid, path).
Jack Jansen5f653091995-01-18 13:53:49 +0000827** NOTE: This routine will fail on pre-sys7 machines.
828** The caller is responsible for not calling this routine
829** in those cases (which is fine, since everyone calling
830** this is probably sys7 dependent anyway).
831*/
832int
Guido van Rossum8f691791995-01-18 23:57:26 +0000833PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
Jack Jansen5f653091995-01-18 13:53:49 +0000834{
835 Str255 path;
836 short refnum;
837 long parid;
838 OSErr err;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000839 FSSpec *fs2;
Jack Jansen5f653091995-01-18 13:53:49 +0000840
Jack Jansene8e8ae01995-01-26 16:36:45 +0000841 /* first check whether it already is an FSSpec */
842 fs2 = mfs_GetFSSpecFSSpec(v);
843 if ( fs2 ) {
Jack Jansen3ec804a1995-02-20 15:56:10 +0000844 (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000845 return 1;
846 }
Jack Jansen5f653091995-01-18 13:53:49 +0000847 if ( PyString_Check(v) ) {
848 /* It's a pathname */
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000849 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
Jack Jansen5f653091995-01-18 13:53:49 +0000850 return 0;
Jack Jansen378815c1996-03-06 16:21:34 +0000851 refnum = 0; /* XXXX Should get CurWD here?? */
Jack Jansen5f653091995-01-18 13:53:49 +0000852 parid = 0;
853 } else {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000854 if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)",
855 &refnum, &parid, PyMac_GetStr255, &path)) {
Jack Jansen5f653091995-01-18 13:53:49 +0000856 return 0;
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000857 }
Jack Jansen5f653091995-01-18 13:53:49 +0000858 }
859 err = FSMakeFSSpec(refnum, parid, path, fs);
860 if ( err && err != fnfErr ) {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000861 PyErr_Mac(PyExc_ValueError, err);
Jack Jansen5f653091995-01-18 13:53:49 +0000862 return 0;
863 }
864 return 1;
865}
866
Guido van Rossum8f691791995-01-18 23:57:26 +0000867
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000868/* Convert a Python object to a Rect.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000869 The object must be a (left, top, right, bottom) tuple.
870 (This differs from the order in the struct but is consistent with
871 the arguments to SetRect(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000872int
873PyMac_GetRect(PyObject *v, Rect *r)
Guido van Rossum8f691791995-01-18 23:57:26 +0000874{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000875 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
Guido van Rossum8f691791995-01-18 23:57:26 +0000876}
Guido van Rossumb3404661995-01-22 18:36:13 +0000877
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000878/* Convert a Rect to a Python object */
Guido van Rossumb3404661995-01-22 18:36:13 +0000879PyObject *
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000880PyMac_BuildRect(Rect *r)
Guido van Rossumb3404661995-01-22 18:36:13 +0000881{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000882 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000883}
884
885
886/* Convert a Python object to a Point.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000887 The object must be a (h, v) tuple.
888 (This differs from the order in the struct but is consistent with
889 the arguments to SetPoint(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000890int
891PyMac_GetPoint(PyObject *v, Point *p)
892{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000893 return PyArg_Parse(v, "(hh)", &p->h, &p->v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000894}
895
896/* Convert a Point to a Python object */
897PyObject *
898PyMac_BuildPoint(Point p)
899{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000900 return Py_BuildValue("(hh)", p.h, p.v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000901}
902
903
904/* Convert a Python object to an EventRecord.
905 The object must be a (what, message, when, (v, h), modifiers) tuple. */
906int
907PyMac_GetEventRecord(PyObject *v, EventRecord *e)
908{
909 return PyArg_Parse(v, "(hll(hh)h)",
910 &e->what,
911 &e->message,
912 &e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000913 &e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +0000914 &e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000915 &e->modifiers);
916}
917
918/* Convert a Rect to an EventRecord object */
919PyObject *
920PyMac_BuildEventRecord(EventRecord *e)
921{
922 return Py_BuildValue("(hll(hh)h)",
923 e->what,
924 e->message,
925 e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000926 e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +0000927 e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000928 e->modifiers);
Guido van Rossumb3404661995-01-22 18:36:13 +0000929}
Jack Jansenfa4d5d01995-11-15 15:19:29 +0000930
931/* Convert Python object to Fixed */
932int
933PyMac_GetFixed(PyObject *v, Fixed *f)
934{
935 double d;
936
937 if( !PyArg_Parse(v, "d", &d))
938 return 0;
939 *f = (Fixed)(d * 0x10000);
Jack Jansen31dd5c01996-05-31 13:01:39 +0000940 return 1;
Jack Jansenfa4d5d01995-11-15 15:19:29 +0000941}
942
943/* Convert a Point to a Python object */
944PyObject *
945PyMac_BuildFixed(Fixed f)
946{
947 double d;
948
949 d = f;
950 d = d / 0x10000;
951 return Py_BuildValue("d", d);
952}
953