blob: 67c39186860dbb05780897f99688adf07ff09798 [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
Jack Jansen74a1e632000-07-14 22:37:27 +000035#if TARGET_API_MAC_CARBON
Jack Jansenbb6d83a2000-06-02 21:27:11 +000036/* Unfortunately this call is probably slower... */
37#define LMGetTicks() TickCount()
38#endif
39
Jack Jansenbf05d4c1996-08-19 15:11:45 +000040#ifdef __CFM68K__
41#undef GetEventQueue
42#endif /* __CFM68K__ */
43
Jack Jansenf93c72a1994-12-14 14:07:50 +000044#include "Python.h"
Guido van Rossumbecdbec1995-02-14 01:27:24 +000045
Jack Jansenf93c72a1994-12-14 14:07:50 +000046#include "macglue.h"
Jack Jansen74162f31995-02-15 22:58:33 +000047#include "marshal.h"
48#include "import.h"
Jack Janseneda78631997-06-12 15:29:46 +000049#include "importdl.h"
Jack Jansenf93c72a1994-12-14 14:07:50 +000050
Jack Jansen819f1771995-08-14 12:35:10 +000051#include "pythonresources.h"
52
Jack Jansenf93c72a1994-12-14 14:07:50 +000053#include <OSUtils.h> /* for Set(Current)A5 */
Jack Jansene8e8ae01995-01-26 16:36:45 +000054#include <Files.h>
Jack Jansen8cd2b721995-02-13 11:33:28 +000055#include <StandardFile.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000056#include <Resources.h>
57#include <Memory.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +000058#include <Windows.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000059#include <Traps.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000060#include <Processes.h>
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +000061#include <Fonts.h>
62#include <Menus.h>
Jack Jansen08305501995-06-18 20:03:40 +000063#include <TextUtils.h>
Jack Jansen16df2aa1995-02-27 16:17:28 +000064#ifdef __MWERKS__
65#include <SIOUX.h>
Jack Jansen9ae898b2000-07-11 21:16:03 +000066extern void SIOUXSetupMenus(void);
67extern void SIOUXDoAboutBox(void);
Jack Jansen16df2aa1995-02-27 16:17:28 +000068#endif
Jack Jansen9ae898b2000-07-11 21:16:03 +000069#ifdef USE_GUSI
70/* Functions we redefine because they're in obscure libraries */
71extern void SpinCursor(short x);
72extern void RotateCursor(short x);
73extern pascal void PLstrcpy(unsigned char *, unsigned char *);
74extern pascal int PLstrcmp(unsigned char *, unsigned char *);
75extern pascal unsigned char *PLstrrchr(unsigned char *, unsigned char);
76
77#endif
78
Jack Jansen2d1306b2000-04-07 09:10:49 +000079#ifdef USE_GUSI1
Jack Jansen911ad6b1996-03-05 16:56:24 +000080#include <TFileSpec.h> /* For Path2FSSpec */
Jack Jansenf6865f71996-09-04 15:24:59 +000081#include <GUSI.h>
Jack Jansen911ad6b1996-03-05 16:56:24 +000082#endif
Jack Jansen2d1306b2000-04-07 09:10:49 +000083#include <LowMem.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000084
Jack Jansen3469e991996-09-06 00:30:45 +000085/* The ID of the Sioux apple menu */
86#define SIOUX_APPLEID 32000
87
Jack Jansenee23d6e1995-01-27 14:43:25 +000088#include <signal.h>
Jack Jansen74162f31995-02-15 22:58:33 +000089#include <stdio.h>
Jack Jansene8e8ae01995-01-26 16:36:45 +000090
Jack Jansenee23d6e1995-01-27 14:43:25 +000091/*
Jack Jansend1f06311996-08-01 15:23:54 +000092** When less than this amount of stackspace is left we
93** raise a MemoryError.
94*/
95#ifndef MINIMUM_STACK_SIZE
96#ifdef __powerc
97#define MINIMUM_STACK_SIZE 8192
98#else
99#define MINIMUM_STACK_SIZE 4096
100#endif
101#endif
102
103/*
Jack Jansen16df2aa1995-02-27 16:17:28 +0000104** We have to be careful, since we can't handle
Jack Jansenee23d6e1995-01-27 14:43:25 +0000105** things like updates (and they'll keep coming back if we don't
Jack Jansen16df2aa1995-02-27 16:17:28 +0000106** handle them). Note that we don't know who has windows open, so
107** even handing updates off to SIOUX under MW isn't going to work.
Jack Jansenee23d6e1995-01-27 14:43:25 +0000108*/
Jack Jansen0c968871997-08-26 13:20:34 +0000109#define MAINLOOP_EVENTMASK (mDownMask|keyDownMask|osMask|activMask)
Jack Jansene8e8ae01995-01-26 16:36:45 +0000110
111#include <signal.h>
Jack Jansenf93c72a1994-12-14 14:07:50 +0000112
Guido van Rossumb3404661995-01-22 18:36:13 +0000113/* XXX We should include Errors.h here, but it has a name conflict
Jack Jansen5f653091995-01-18 13:53:49 +0000114** with the python errors.h. */
115#define fnfErr -43
116
Jack Jansene8e8ae01995-01-26 16:36:45 +0000117/* Declared in macfsmodule.c: */
Jack Jansen9ae898b2000-07-11 21:16:03 +0000118extern FSSpec *mfs_GetFSSpecFSSpec(PyObject *);
Jack Jansend88296d2000-07-11 19:51:05 +0000119extern PyObject *newmfssobject(FSSpec *);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000120
Jack Jansenee23d6e1995-01-27 14:43:25 +0000121/* Interrupt code variables: */
122static int interrupted; /* Set to true when cmd-. seen */
Jack Jansend88296d2000-07-11 19:51:05 +0000123static RETSIGTYPE intcatcher(int);
Jack Jansenee23d6e1995-01-27 14:43:25 +0000124
Jack Jansend88296d2000-07-11 19:51:05 +0000125static int PyMac_Yield(void);
Jack Jansenf6865f71996-09-04 15:24:59 +0000126
Jack Jansene8e8ae01995-01-26 16:36:45 +0000127/*
Jack Jansene3ae0df1997-06-03 15:28:29 +0000128** These are the real scheduling parameters that control what we check
129** in the event loop, and how often we check. The values are initialized
130** from pyMac_SchedParamStruct.
131*/
132
133struct real_sched_param_struct {
134 int check_interrupt; /* if true check for command-dot */
135 int process_events; /* if nonzero enable evt processing, this mask */
136 int besocial; /* if nonzero be a little social with CPU */
137 unsigned long check_interval; /* how often to check, in ticks */
138 unsigned long bg_yield; /* yield so long when in background */
139 /* these are computed from previous and clock and such */
140 int enabled; /* check_interrupt OR process_event OR yield */
141 unsigned long next_check; /* when to check/yield next, in ticks */
142};
143
144static struct real_sched_param_struct schedparams =
145 { 1, MAINLOOP_EVENTMASK, 1, 15, 15, 1, 0};
146
Jack Jansen819f1771995-08-14 12:35:10 +0000147/*
Jack Jansenf6865f71996-09-04 15:24:59 +0000148** Workaround for sioux/gusi combo: set when we are exiting
149*/
150int PyMac_ConsoleIsDead;
151
152/*
Jack Jansencaa7c461997-06-12 10:49:13 +0000153** Sioux menu bar, saved early so we can restore it
154*/
155static Handle sioux_mbar;
156
157/*
Jack Jansen819f1771995-08-14 12:35:10 +0000158** Some stuff for our GetDirectory and PromptGetFile routines
159*/
160struct hook_args {
161 int selectcur_hit; /* Set to true when "select current" selected */
162 char *prompt; /* The prompt */
163};
Jack Jansen74a1e632000-07-14 22:37:27 +0000164#if TARGET_API_MAC_CARBON
Jack Jansenbb6d83a2000-06-02 21:27:11 +0000165/* The StandardFile hooks don't exist in Carbon. This breaks GetDirectory,
166** but the macfsn code will replace it by a NavServices version anyway.
167*/
168#define myhook_upp NULL
169#else
Jack Jansen819f1771995-08-14 12:35:10 +0000170static DlgHookYDUPP myhook_upp;
171static int upp_inited = 0;
Jack Jansenbb6d83a2000-06-02 21:27:11 +0000172#endif
Jack Jansen819f1771995-08-14 12:35:10 +0000173
Jack Jansen36ed5061997-06-20 16:18:15 +0000174/*
175** The python-code event handler
176*/
177static PyObject *python_event_handler;
178
Jack Jansen8f5725a1999-12-07 23:08:10 +0000179/*
180** Set to true if we're appearance-compliant
181*/
182int PyMac_AppearanceCompliant;
183
Jack Jansen1f9f2f42000-07-24 19:50:16 +0000184/*
185** Find out what the current script is.
186** Donated by Fredrik Lund.
187*/
188char *PyMac_getscript()
189{
190 int font, script, lang;
191 font = 0;
192 font = GetSysFont();
193 script = FontToScript(font);
194 switch (script) {
195 case smRoman:
196 lang = GetScriptVariable(script, smScriptLang);
197 if (lang == langIcelandic)
198 return "mac-iceland";
199 else if (lang == langTurkish)
200 return "mac-turkish";
201 else if (lang == langGreek)
202 return "mac-greek";
203 else
204 return "mac-roman";
205 break;
206 case smJapanese:
207 return "mac-japan";
208 case smGreek:
209 return "mac-greek";
210 case smCyrillic:
211 return "mac-cyrillic";
212 default:
213 return "mac-roman"; /* better than nothing */
214 }
215}
216
217
Jack Jansen2d1306b2000-04-07 09:10:49 +0000218#ifdef USE_GUSI1
Jack Jansen378815c1996-03-06 16:21:34 +0000219/*
220** GUSI (1.6.0 and earlier, at the least) do not set the MacOS idea of
221** the working directory. Hence, we call this routine after each call
222** to chdir() to rectify things.
223*/
224void
225PyMac_FixGUSIcd()
226{
227 WDPBRec pb;
228 FSSpec curdirfss;
229
230 if ( Path2FSSpec(":x", &curdirfss) != noErr )
231 return;
232
233 /* Set MacOS "working directory" */
234 pb.ioNamePtr= "\p";
235 pb.ioVRefNum= curdirfss.vRefNum;
236 pb.ioWDDirID= curdirfss.parID;
Jack Jansen08c3be31997-04-08 15:27:00 +0000237 if (PBHSetVolSync(&pb) != noErr)
Jack Jansen378815c1996-03-06 16:21:34 +0000238 return;
Jack Jansen378815c1996-03-06 16:21:34 +0000239}
Jack Jansen2d1306b2000-04-07 09:10:49 +0000240#endif
Jack Jansen7ac70af1996-08-19 11:01:05 +0000241
Jack Jansen2d1306b2000-04-07 09:10:49 +0000242#ifdef USE_GUSI
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000243/*
244** SpinCursor (needed by GUSI) drags in heaps of stuff, so we
245** provide a dummy here.
246*/
Jack Jansencfadbd41996-08-19 11:36:25 +0000247void SpinCursor(short x) { /* Dummy */ }
Jack Jansenefaada71998-02-20 16:03:15 +0000248void RotateCursor(short x) { /* Dummy */ }
Jack Jansencfadbd41996-08-19 11:36:25 +0000249
Jack Jansenf6865f71996-09-04 15:24:59 +0000250/*
251** Replacement GUSI Spin function
252*/
Jack Jansen2d1306b2000-04-07 09:10:49 +0000253#ifdef USE_GUSI1
Jack Jansenf6865f71996-09-04 15:24:59 +0000254static int
255PyMac_GUSISpin(spin_msg msg, long arg)
256{
Jack Jansene3ae0df1997-06-03 15:28:29 +0000257 static Boolean inForeground = true;
258 int maxsleep = 6; /* 6 ticks is "normal" sleeptime */
Jack Jansenf6865f71996-09-04 15:24:59 +0000259
260 if (PyMac_ConsoleIsDead) return 0;
261#if 0
262 if (inForeground)
263 SpinCursor(msg == SP_AUTO_SPIN ? short(arg) : 1);
264#endif
265
266 if (interrupted) return -1;
267
Jack Jansene3ae0df1997-06-03 15:28:29 +0000268 if ( msg == SP_AUTO_SPIN )
269 maxsleep = 0;
270 if ( msg==SP_SLEEP||msg==SP_SELECT )
271 maxsleep = arg;
Jack Jansenf6865f71996-09-04 15:24:59 +0000272
Jack Jansene3ae0df1997-06-03 15:28:29 +0000273 PyMac_DoYield(maxsleep, 0); /* XXXX or is it safe to call python here? */
Jack Jansenf6865f71996-09-04 15:24:59 +0000274
275 return 0;
276}
277
278void
279PyMac_SetGUSISpin() {
280 GUSISetHook(GUSI_SpinHook, (GUSIHook)PyMac_GUSISpin);
281}
Jack Jansen2d1306b2000-04-07 09:10:49 +0000282#endif
Jack Jansenf6865f71996-09-04 15:24:59 +0000283
Jack Jansena39f1b01997-05-23 15:35:14 +0000284/* Called at exit() time thru atexit(), to stop event processing */
285void
286PyMac_StopGUSISpin() {
287 PyMac_ConsoleIsDead = 1;
288}
289
290/*
291** Replacement routines for the PLstr... functions so we don't need
292** StdCLib. Moreover, that implementation is broken under cfm68k...
293*/
Jack Jansen8bb573e1999-12-12 21:37:14 +0000294pascal void
Jack Jansen9ae898b2000-07-11 21:16:03 +0000295PLstrcpy(unsigned char *to, unsigned char *fr)
Jack Jansena39f1b01997-05-23 15:35:14 +0000296{
297 memcpy(to, fr, fr[0]+1);
298}
299
Jack Jansen8bb573e1999-12-12 21:37:14 +0000300pascal int
Jack Jansen9ae898b2000-07-11 21:16:03 +0000301PLstrcmp(unsigned char *s1, unsigned char *s2)
Jack Jansena39f1b01997-05-23 15:35:14 +0000302{
303 int res;
304 int l = s1[0] < s2[0] ? s1[0] : s2[0];
305
306 res = memcmp(s1+1, s2+1, l);
307 if ( res != 0 )
308 return res;
309
Jack Jansen7e1fb7c1998-07-31 09:36:30 +0000310 if ( s1[0] < s2[0] )
Jack Jansena39f1b01997-05-23 15:35:14 +0000311 return -1;
Jack Jansen7e1fb7c1998-07-31 09:36:30 +0000312 else if ( s1[0] > s2[0] )
Jack Jansena39f1b01997-05-23 15:35:14 +0000313 return 1;
314 else
315 return 0;
316}
317
Jack Jansen8bb573e1999-12-12 21:37:14 +0000318pascal unsigned char *
Jack Jansen9ae898b2000-07-11 21:16:03 +0000319PLstrrchr(unsigned char *str, unsigned char chr)
Jack Jansena39f1b01997-05-23 15:35:14 +0000320{
321 unsigned char *ptr = 0;
322 unsigned char *p;
323
324 for(p=str+1; p<str+str[0]; p++)
325 if ( *p == chr )
326 ptr = p;
327 return ptr;
328}
329
330#endif /* USE_GUSI */
Jack Jansen378815c1996-03-06 16:21:34 +0000331
Jack Jansen819f1771995-08-14 12:35:10 +0000332
Jack Jansen5f653091995-01-18 13:53:49 +0000333/* Convert C to Pascal string. Returns pointer to static buffer. */
334unsigned char *
335Pstring(char *str)
336{
337 static Str255 buf;
338 int len;
339
340 len = strlen(str);
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000341 if (len > 255)
342 len = 255;
Jack Jansen5f653091995-01-18 13:53:49 +0000343 buf[0] = (unsigned char)len;
344 strncpy((char *)buf+1, str, len);
345 return buf;
346}
347
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +0000348/* Like strerror() but for Mac OS error numbers */
349char *PyMac_StrError(int err)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000350{
351 static char buf[256];
352 Handle h;
353 char *str;
354
355 h = GetResource('Estr', err);
356 if ( h ) {
357 HLock(h);
358 str = (char *)*h;
359 memcpy(buf, str+1, (unsigned char)str[0]);
Guido van Rossumcc9bc8f1995-02-13 16:17:03 +0000360 buf[(unsigned char)str[0]] = '\0';
Jack Jansenf93c72a1994-12-14 14:07:50 +0000361 HUnlock(h);
362 ReleaseResource(h);
363 } else {
364 sprintf(buf, "Mac OS error code %d", err);
365 }
366 return buf;
367}
368
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000369/* Exception object shared by all Mac specific modules for Mac OS errors */
370PyObject *PyMac_OSErrException;
371
372/* Initialize and return PyMac_OSErrException */
373PyObject *
374PyMac_GetOSErrException()
375{
376 if (PyMac_OSErrException == NULL)
377 PyMac_OSErrException = PyString_FromString("Mac OS Error");
378 return PyMac_OSErrException;
379}
380
Jack Jansenf93c72a1994-12-14 14:07:50 +0000381/* Set a MAC-specific error from errno, and return NULL; return None if no error */
382PyObject *
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000383PyErr_Mac(PyObject *eobj, int err)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000384{
385 char *msg;
386 PyObject *v;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000387
Guido van Rossum8f691791995-01-18 23:57:26 +0000388 if (err == 0 && !PyErr_Occurred()) {
Jack Jansenf93c72a1994-12-14 14:07:50 +0000389 Py_INCREF(Py_None);
390 return Py_None;
391 }
Guido van Rossum8f691791995-01-18 23:57:26 +0000392 if (err == -1 && PyErr_Occurred())
393 return NULL;
Guido van Rossumc3d1c8e1995-02-18 15:01:31 +0000394 msg = PyMac_StrError(err);
Jack Jansenf93c72a1994-12-14 14:07:50 +0000395 v = Py_BuildValue("(is)", err, msg);
396 PyErr_SetObject(eobj, v);
397 Py_DECREF(v);
398 return NULL;
399}
400
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000401/* Call PyErr_Mac with PyMac_OSErrException */
402PyObject *
403PyMac_Error(OSErr err)
404{
405 return PyErr_Mac(PyMac_GetOSErrException(), err);
406}
407
Jack Jansen1ed95291996-07-22 15:25:10 +0000408#ifdef USE_STACKCHECK
409/* Check for stack overflow */
410int
411PyOS_CheckStack()
412{
Jack Jansen14a91712000-08-25 21:57:23 +0000413 char here;
414 static char *sentinel = 0;
Jack Jansen53bafd92000-09-08 22:05:48 +0000415 static PyThreadState *thread_for_sentinel = 0;
Jack Jansen1ed95291996-07-22 15:25:10 +0000416
Jack Jansen14a91712000-08-25 21:57:23 +0000417 if ( sentinel == 0 ) {
418 sentinel = &here - StackSpace() + MINIMUM_STACK_SIZE;
419 }
Jack Jansen53bafd92000-09-08 22:05:48 +0000420 if ( thread_for_sentinel == 0 ) {
421 thread_for_sentinel = PyThreadState_Get();
422 }
423 if ( &here < sentinel ) {
424 if (thread_for_sentinel == PyThreadState_Get()) {
425 return -1;
426#if 0
427 } else {
428 /* Else we are unsure... */
429 fprintf(stderr, "Stackcheck in other thread (was %x now %x)\n", thread_for_sentinel,PyThreadState_Get());
430#endif
431 }
432 }
Jack Jansen1ed95291996-07-22 15:25:10 +0000433 return 0;
434}
435#endif /* USE_STACKCHECK */
436
Jack Jansenee23d6e1995-01-27 14:43:25 +0000437/* The catcher routine (which may not be used for all compilers) */
438static RETSIGTYPE
439intcatcher(sig)
440 int sig;
441{
442 interrupted = 1;
443 signal(SIGINT, intcatcher);
444}
445
446void
447PyOS_InitInterrupts()
448{
449 if (signal(SIGINT, SIG_IGN) != SIG_IGN)
450 signal(SIGINT, intcatcher);
451}
452
Jack Jansena8441de1997-08-08 14:57:37 +0000453void
454PyOS_FiniInterrupts()
455{
456}
457
Jack Jansenee23d6e1995-01-27 14:43:25 +0000458/*
459** This routine scans the event queue looking for cmd-.
460** This is the only way to get an interrupt under THINK (since it
461** doesn't do SIGINT handling), but is also used under MW, when
462** the full-fledged event loop is disabled. This way, we can at least
463** interrupt a runaway python program.
464*/
465static void
466scan_event_queue(flush)
467 int flush;
468{
Jack Jansen74a1e632000-07-14 22:37:27 +0000469#if TARGET_API_MAC_CARBON
Jack Jansenbb6d83a2000-06-02 21:27:11 +0000470 /* CARBONXXXX To be implemented */
471 return;
472#else
Jack Jansenee23d6e1995-01-27 14:43:25 +0000473 register EvQElPtr q;
474
Jack Jansenefaada71998-02-20 16:03:15 +0000475 q = (EvQElPtr) LMGetEventQueue()->qHead;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000476
477 for (; q; q = (EvQElPtr)q->qLink) {
478 if (q->evtQWhat == keyDown &&
479 (char)q->evtQMessage == '.' &&
480 (q->evtQModifiers & cmdKey) != 0) {
481 if ( flush )
482 FlushEvents(keyDownMask, 0);
483 interrupted = 1;
484 break;
485 }
486 }
Jack Jansenbb6d83a2000-06-02 21:27:11 +0000487#endif
Jack Jansenee23d6e1995-01-27 14:43:25 +0000488}
489
490int
Jack Jansen36ed5061997-06-20 16:18:15 +0000491PyErr_CheckSignals()
Jack Jansenee23d6e1995-01-27 14:43:25 +0000492{
Jack Jansene3ae0df1997-06-03 15:28:29 +0000493 if (schedparams.enabled) {
494 if ( (unsigned long)LMGetTicks() > schedparams.next_check ) {
Jack Jansen36ed5061997-06-20 16:18:15 +0000495 if ( PyMac_Yield() < 0)
496 return -1;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000497 schedparams.next_check = (unsigned long)LMGetTicks()
498 + schedparams.check_interval;
499 if (interrupted) {
Jack Jansencaa7c461997-06-12 10:49:13 +0000500 scan_event_queue(1); /* Eat events up to cmd-. */
Jack Jansene3ae0df1997-06-03 15:28:29 +0000501 interrupted = 0;
Jack Jansen36ed5061997-06-20 16:18:15 +0000502 PyErr_SetNone(PyExc_KeyboardInterrupt);
503 return -1;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000504 }
505 }
Jack Jansenee23d6e1995-01-27 14:43:25 +0000506 }
507 return 0;
508}
509
Jack Jansen36ed5061997-06-20 16:18:15 +0000510int
511PyOS_InterruptOccurred()
512{
513 scan_event_queue(1);
514 return interrupted;
515}
Jack Jansenee23d6e1995-01-27 14:43:25 +0000516/* Check whether we are in the foreground */
Jack Jansen9ae898b2000-07-11 21:16:03 +0000517static int
518PyMac_InForeground(void)
Jack Jansenee23d6e1995-01-27 14:43:25 +0000519{
520 static ProcessSerialNumber ours;
521 static inited;
522 ProcessSerialNumber curfg;
523 Boolean eq;
524
Jack Jansene3ae0df1997-06-03 15:28:29 +0000525 if ( inited == 0 ) {
Jack Jansenee23d6e1995-01-27 14:43:25 +0000526 (void)GetCurrentProcess(&ours);
Jack Jansene3ae0df1997-06-03 15:28:29 +0000527 inited = 1;
528 }
Jack Jansenee23d6e1995-01-27 14:43:25 +0000529 if ( GetFrontProcess(&curfg) < 0 )
530 eq = 1;
531 else if ( SameProcess(&ours, &curfg, &eq) < 0 )
532 eq = 1;
533 return (int)eq;
534
535}
536
Jack Jansen36ed5061997-06-20 16:18:15 +0000537int
538PyMac_SetEventHandler(PyObject *evh)
539{
540 if ( evh && python_event_handler ) {
541 PyErr_SetString(PyExc_RuntimeError, "Python event handler already set");
542 return 0;
543 }
544 if ( python_event_handler )
545 Py_DECREF(python_event_handler);
546 if ( evh )
547 Py_INCREF(evh);
548 python_event_handler = evh;
549 return 1;
550}
551
Jack Jansenf93c72a1994-12-14 14:07:50 +0000552/*
Jack Jansena76382a1995-02-02 14:25:56 +0000553** Handle an event, either one found in the mainloop eventhandler or
554** one passed back from the python program.
555*/
556void
Jack Jansen36ed5061997-06-20 16:18:15 +0000557PyMac_HandleEventIntern(evp)
Jack Jansena76382a1995-02-02 14:25:56 +0000558 EventRecord *evp;
559{
Jack Jansen74a1e632000-07-14 22:37:27 +0000560#if !TARGET_API_MAC_CARBON
Jack Jansen0c968871997-08-26 13:20:34 +0000561 if ( evp->what == mouseDown ) {
562 WindowPtr wp;
563
564 if ( FindWindow(evp->where, &wp) == inSysWindow ) {
565 SystemClick(evp, wp);
566 return;
567 }
568 }
Jack Jansenbb6d83a2000-06-02 21:27:11 +0000569#endif
Jack Jansena76382a1995-02-02 14:25:56 +0000570#ifdef __MWERKS__
Jack Jansen38e97661995-11-10 14:53:00 +0000571 {
572 int siouxdidit;
573
574 /* If SIOUX wants it we're done */
575 siouxdidit = SIOUXHandleOneEvent(evp);
576 if ( siouxdidit )
577 return;
578 }
Jack Jansena76382a1995-02-02 14:25:56 +0000579#else
Jack Jansen0c968871997-08-26 13:20:34 +0000580 /* Other compilers are just unlucky... */
Jack Jansena76382a1995-02-02 14:25:56 +0000581#endif /* !__MWERKS__ */
Jack Jansen36ed5061997-06-20 16:18:15 +0000582}
583
584/*
585** Handle an event, either through HandleEvent or by passing it to the Python
586** event handler.
587*/
588int
589PyMac_HandleEvent(evp)
590 EventRecord *evp;
591{
592 PyObject *rv;
593
594 if ( python_event_handler ) {
595 rv = PyObject_CallFunction(python_event_handler, "(O&)",
596 PyMac_BuildEventRecord, evp);
597 if ( rv )
598 Py_DECREF(rv);
599 else
600 return -1; /* Propagate exception */
601 } else {
602 PyMac_HandleEventIntern(evp);
603 }
604 return 0;
Jack Jansena76382a1995-02-02 14:25:56 +0000605}
606
607/*
Jack Jansene3ae0df1997-06-03 15:28:29 +0000608** Yield the CPU to other tasks without processing events.
Jack Jansene8e8ae01995-01-26 16:36:45 +0000609*/
Jack Jansen2d1306b2000-04-07 09:10:49 +0000610int
Jack Jansene3ae0df1997-06-03 15:28:29 +0000611PyMac_DoYield(int maxsleep, int maycallpython)
Jack Jansenf93c72a1994-12-14 14:07:50 +0000612{
613 EventRecord ev;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000614 int gotone;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000615 long latest_time_ready;
Jack Jansen36ed5061997-06-20 16:18:15 +0000616 static int in_here = 0;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000617
Jack Jansen36ed5061997-06-20 16:18:15 +0000618 in_here++;
Jack Jansene3ae0df1997-06-03 15:28:29 +0000619 /*
620 ** First check for interrupts, if wanted.
621 ** This sets a flag that will be picked up at an appropriate
622 ** moment in the mainloop.
623 */
624 if (schedparams.check_interrupt)
Jack Jansenee23d6e1995-01-27 14:43:25 +0000625 scan_event_queue(0);
Jack Jansene3ae0df1997-06-03 15:28:29 +0000626
627 /* XXXX Implementing an idle routine goes here */
Jack Jansene8e8ae01995-01-26 16:36:45 +0000628
Jack Jansene3ae0df1997-06-03 15:28:29 +0000629 /*
630 ** Check which of the eventloop cases we have:
631 ** - process events
632 ** - don't process events but do yield
633 ** - do neither
634 */
Jack Jansen36ed5061997-06-20 16:18:15 +0000635 if( in_here > 1 || !schedparams.process_events ||
636 (python_event_handler && !maycallpython) ) {
Jack Jansen74a1e632000-07-14 22:37:27 +0000637#if !TARGET_API_MAC_CARBON
Jack Jansene3ae0df1997-06-03 15:28:29 +0000638 if ( maxsleep >= 0 ) {
Jack Jansene8e8ae01995-01-26 16:36:45 +0000639 SystemTask();
Jack Jansene3ae0df1997-06-03 15:28:29 +0000640 }
Jack Jansenbb6d83a2000-06-02 21:27:11 +0000641#endif
Jack Jansene3ae0df1997-06-03 15:28:29 +0000642 } else {
643 latest_time_ready = LMGetTicks() + maxsleep;
Jack Jansen2d1306b2000-04-07 09:10:49 +0000644 do {
Jack Jansen0c968871997-08-26 13:20:34 +0000645 /* XXXX Hack by Jack.
646 ** In time.sleep() you can click to another application
647 ** once only. If you come back to Python you cannot get away
648 ** again.
649 **/
Jack Jansen36ed5061997-06-20 16:18:15 +0000650 gotone = WaitNextEvent(schedparams.process_events, &ev, maxsleep, NULL);
Jack Jansene3ae0df1997-06-03 15:28:29 +0000651 /* Get out quickly if nothing interesting is happening */
652 if ( !gotone || ev.what == nullEvent )
653 break;
Jack Jansen36ed5061997-06-20 16:18:15 +0000654 if ( PyMac_HandleEvent(&ev) < 0 ) {
655 in_here--;
656 return -1;
657 }
Jack Jansene3ae0df1997-06-03 15:28:29 +0000658 maxsleep = latest_time_ready - LMGetTicks();
Jack Jansen2d1306b2000-04-07 09:10:49 +0000659 } while ( maxsleep > 0 );
Jack Jansenf93c72a1994-12-14 14:07:50 +0000660 }
Jack Jansen36ed5061997-06-20 16:18:15 +0000661 in_here--;
662 return 0;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000663}
664
665/*
Jack Jansene3ae0df1997-06-03 15:28:29 +0000666** Process events and/or yield the CPU to other tasks if opportune
Jack Jansene8e8ae01995-01-26 16:36:45 +0000667*/
Jack Jansen36ed5061997-06-20 16:18:15 +0000668int
Jack Jansene8e8ae01995-01-26 16:36:45 +0000669PyMac_Yield() {
Jack Jansene3ae0df1997-06-03 15:28:29 +0000670 unsigned long maxsleep;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000671
Jack Jansene3ae0df1997-06-03 15:28:29 +0000672 if( PyMac_InForeground() )
673 maxsleep = 0;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000674 else
Jack Jansene3ae0df1997-06-03 15:28:29 +0000675 maxsleep = schedparams.bg_yield;
676
Jack Jansen36ed5061997-06-20 16:18:15 +0000677 return PyMac_DoYield(maxsleep, 1);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000678}
679
680/*
Jack Jansene3ae0df1997-06-03 15:28:29 +0000681** Return current scheduler parameters
Jack Jansene8e8ae01995-01-26 16:36:45 +0000682*/
Jack Jansene3ae0df1997-06-03 15:28:29 +0000683void
684PyMac_GetSchedParams(PyMacSchedParams *sp)
Jack Jansene8e8ae01995-01-26 16:36:45 +0000685{
Jack Jansene3ae0df1997-06-03 15:28:29 +0000686 sp->check_interrupt = schedparams.check_interrupt;
687 sp->process_events = schedparams.process_events;
688 sp->besocial = schedparams.besocial;
689 sp->check_interval = schedparams.check_interval / 60.0;
690 sp->bg_yield = schedparams.bg_yield / 60.0;
Jack Jansenf93c72a1994-12-14 14:07:50 +0000691}
Jack Jansenf6865f71996-09-04 15:24:59 +0000692
Jack Jansen74162f31995-02-15 22:58:33 +0000693/*
Jack Jansene3ae0df1997-06-03 15:28:29 +0000694** Set current scheduler parameters
695*/
696void
697PyMac_SetSchedParams(PyMacSchedParams *sp)
698{
699 schedparams.check_interrupt = sp->check_interrupt;
700 schedparams.process_events = sp->process_events;
701 schedparams.besocial = sp->besocial;
702 schedparams.check_interval = (unsigned long)(sp->check_interval*60);
703 schedparams.bg_yield = (unsigned long)(sp->bg_yield*60);
704 if ( schedparams.check_interrupt || schedparams.process_events ||
705 schedparams.besocial )
706 schedparams.enabled = 1;
707 else
708 schedparams.enabled = 0;
709 schedparams.next_check = 0; /* Check immedeately */
710}
Jack Jansencaa7c461997-06-12 10:49:13 +0000711
Jack Jansene3ae0df1997-06-03 15:28:29 +0000712/*
Jack Jansen3469e991996-09-06 00:30:45 +0000713** Install our menu bar.
714*/
715void
716PyMac_InitMenuBar()
717{
Jack Jansen3469e991996-09-06 00:30:45 +0000718 MenuHandle applemenu;
719
Jack Jansencaa7c461997-06-12 10:49:13 +0000720 if ( (sioux_mbar=GetMenuBar()) == NULL ) {
721 /* Sioux menu not installed yet. Do so */
722 SIOUXSetupMenus();
723 if ( (sioux_mbar=GetMenuBar()) == NULL )
724 return;
725 }
Jack Jansen08c3be31997-04-08 15:27:00 +0000726 if ( (applemenu=GetMenuHandle(SIOUX_APPLEID)) == NULL ) return;
Jack Jansen3469e991996-09-06 00:30:45 +0000727 SetMenuItemText(applemenu, 1, "\pAbout Python...");
728}
729
730/*
Jack Jansencaa7c461997-06-12 10:49:13 +0000731** Restore sioux menu bar
732*/
733void
734PyMac_RestoreMenuBar()
735{
Jack Janseneda78631997-06-12 15:29:46 +0000736 if ( sioux_mbar ) {
Jack Jansencaa7c461997-06-12 10:49:13 +0000737 SetMenuBar(sioux_mbar);
Jack Janseneda78631997-06-12 15:29:46 +0000738 DrawMenuBar();
Jack Jansenefaada71998-02-20 16:03:15 +0000739 } else
740 PyMac_InitMenuBar();
Jack Jansencaa7c461997-06-12 10:49:13 +0000741}
742
743
744/*
Jack Jansen3469e991996-09-06 00:30:45 +0000745** Our replacement about box
746*/
Jack Jansen7e1fb7c1998-07-31 09:36:30 +0000747
748#include "patchlevel.h"
749
Jack Jansen3469e991996-09-06 00:30:45 +0000750void
751SIOUXDoAboutBox(void)
752{
753 DialogPtr theDialog;
Jack Jansen7e1fb7c1998-07-31 09:36:30 +0000754 WindowPtr theWindow;
Jack Jansen3469e991996-09-06 00:30:45 +0000755 short item;
Jack Jansen7e1fb7c1998-07-31 09:36:30 +0000756 short fontID;
Jack Jansen3469e991996-09-06 00:30:45 +0000757
758 if( (theDialog = GetNewDialog(ABOUT_ID, NULL, (WindowPtr)-1)) == NULL )
759 return;
Jack Jansend617c571996-09-22 22:14:30 +0000760 theWindow = GetDialogWindow(theDialog);
Jack Jansen7e1fb7c1998-07-31 09:36:30 +0000761 SetPortWindowPort(theWindow);
762 GetFNum("\pPython-Sans", &fontID);
763 if (fontID == 0)
764 fontID = kFontIDGeneva;
765 TextFont(fontID);
766 TextSize(9);
767 ParamText(Pstring(PATCHLEVEL), "\p", "\p", "\p");
Jack Jansend617c571996-09-22 22:14:30 +0000768 ShowWindow(theWindow);
Jack Jansen3469e991996-09-06 00:30:45 +0000769 ModalDialog(NULL, &item);
770 DisposeDialog(theDialog);
771}
772
Jack Janseneda78631997-06-12 15:29:46 +0000773#if 0
774int
775PyMac_FileExists(char *name)
776{
777 FSSpec fss;
778
779 if ( FSMakeFSSpec(0, 0, Pstring(name), &fss) == noErr )
780 return 1;
781 return 0;
782}
783#endif
784
785/*
Jack Jansen3ec804a1995-02-20 15:56:10 +0000786** Helper routine for GetDirectory
787*/
Guido van Rossum24a45e31995-02-20 23:45:53 +0000788static pascal short
Jack Jansen819f1771995-08-14 12:35:10 +0000789myhook_proc(short item, DialogPtr theDialog, struct hook_args *dataptr)
Jack Jansen3ec804a1995-02-20 15:56:10 +0000790{
Jack Jansen819f1771995-08-14 12:35:10 +0000791 if ( item == sfHookFirstCall && dataptr->prompt) {
792 Handle prompth;
793 short type;
794 Rect rect;
795
796 GetDialogItem(theDialog, PROMPT_ITEM, &type, &prompth, &rect);
797 if ( prompth )
798 SetDialogItemText(prompth, (unsigned char *)dataptr->prompt);
799 } else
Jack Jansen3ec804a1995-02-20 15:56:10 +0000800 if ( item == SELECTCUR_ITEM ) {
801 item = sfItemCancelButton;
Jack Jansen819f1771995-08-14 12:35:10 +0000802 dataptr->selectcur_hit = 1;
Jack Jansen3ec804a1995-02-20 15:56:10 +0000803 }
804 return item;
805}
Jack Jansen74a1e632000-07-14 22:37:27 +0000806#if !TARGET_API_MAC_CARBON
Jack Jansen3ec804a1995-02-20 15:56:10 +0000807/*
808** Ask the user for a directory. I still can't understand
809** why Apple doesn't provide a standard solution for this...
810*/
811int
Jack Jansen819f1771995-08-14 12:35:10 +0000812PyMac_GetDirectory(dirfss, prompt)
Jack Jansen3ec804a1995-02-20 15:56:10 +0000813 FSSpec *dirfss;
Jack Jansen819f1771995-08-14 12:35:10 +0000814 char *prompt;
Jack Jansen3ec804a1995-02-20 15:56:10 +0000815{
816 static SFTypeList list = {'fldr', 0, 0, 0};
817 static Point where = {-1, -1};
Jack Jansen3ec804a1995-02-20 15:56:10 +0000818 StandardFileReply reply;
Jack Jansen819f1771995-08-14 12:35:10 +0000819 struct hook_args hook_args;
Jack Jansen3ec804a1995-02-20 15:56:10 +0000820
821 if ( !upp_inited ) {
822 myhook_upp = NewDlgHookYDProc(myhook_proc);
823 upp_inited = 1;
824 }
Jack Jansen819f1771995-08-14 12:35:10 +0000825 if ( prompt && *prompt )
826 hook_args.prompt = (char *)Pstring(prompt);
827 else
828 hook_args.prompt = NULL;
829 hook_args.selectcur_hit = 0;
Guido van Rossum24a45e31995-02-20 23:45:53 +0000830 CustomGetFile((FileFilterYDUPP)0, 1, list, &reply, GETDIR_ID, where, myhook_upp,
Jack Jansen819f1771995-08-14 12:35:10 +0000831 NULL, NULL, NULL, (void *)&hook_args);
Jack Jansen3ec804a1995-02-20 15:56:10 +0000832
833 reply.sfFile.name[0] = 0;
834 if( FSMakeFSSpec(reply.sfFile.vRefNum, reply.sfFile.parID, reply.sfFile.name, dirfss) )
835 return 0;
Jack Jansen819f1771995-08-14 12:35:10 +0000836 return hook_args.selectcur_hit;
837}
838
839/*
840** Slightly extended StandardGetFile: accepts a prompt */
841void PyMac_PromptGetFile(short numTypes, ConstSFTypeListPtr typeList,
842 StandardFileReply *reply, char *prompt)
843{
844 static Point where = {-1, -1};
845 struct hook_args hook_args;
846
847 if ( !upp_inited ) {
848 myhook_upp = NewDlgHookYDProc(myhook_proc);
849 upp_inited = 1;
850 }
851 if ( prompt && *prompt )
852 hook_args.prompt = (char *)Pstring(prompt);
853 else
854 hook_args.prompt = NULL;
855 hook_args.selectcur_hit = 0;
856 CustomGetFile((FileFilterYDUPP)0, numTypes, typeList, reply, GETFILEPROMPT_ID, where,
857 myhook_upp, NULL, NULL, NULL, (void *)&hook_args);
Jack Jansen3ec804a1995-02-20 15:56:10 +0000858}
Jack Jansenbb7e2132000-07-14 22:15:24 +0000859#endif /* TARGET_API_MAC_CARBON */
Jack Jansen5f653091995-01-18 13:53:49 +0000860
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000861/* Convert a 4-char string object argument to an OSType value */
Jack Jansen5f653091995-01-18 13:53:49 +0000862int
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000863PyMac_GetOSType(PyObject *v, OSType *pr)
Jack Jansen5f653091995-01-18 13:53:49 +0000864{
865 if (!PyString_Check(v) || PyString_Size(v) != 4) {
866 PyErr_SetString(PyExc_TypeError,
867 "OSType arg must be string of 4 chars");
868 return 0;
869 }
870 memcpy((char *)pr, PyString_AsString(v), 4);
871 return 1;
872}
873
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000874/* Convert an OSType value to a 4-char string object */
875PyObject *
876PyMac_BuildOSType(OSType t)
877{
878 return PyString_FromStringAndSize((char *)&t, 4);
879}
880
Jack Jansend1f06311996-08-01 15:23:54 +0000881/* Convert an NumVersion value to a 4-element tuple */
882PyObject *
883PyMac_BuildNumVersion(NumVersion t)
884{
885 return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev);
886}
887
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000888
889/* Convert a Python string object to a Str255 */
Jack Jansen5f653091995-01-18 13:53:49 +0000890int
Guido van Rossum8f691791995-01-18 23:57:26 +0000891PyMac_GetStr255(PyObject *v, Str255 pbuf)
Jack Jansen5f653091995-01-18 13:53:49 +0000892{
893 int len;
894 if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
895 PyErr_SetString(PyExc_TypeError,
896 "Str255 arg must be string of at most 255 chars");
897 return 0;
898 }
899 pbuf[0] = len;
900 memcpy((char *)(pbuf+1), PyString_AsString(v), len);
901 return 1;
902}
903
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000904/* Convert a Str255 to a Python string object */
905PyObject *
906PyMac_BuildStr255(Str255 s)
907{
Jack Jansenb734ade1999-12-17 17:15:50 +0000908 if ( s == NULL ) {
909 PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL");
910 return NULL;
911 }
912 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
913}
914
915PyObject *
916PyMac_BuildOptStr255(Str255 s)
917{
918 if ( s == NULL ) {
919 Py_INCREF(Py_None);
920 return Py_None;
921 }
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000922 return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
923}
924
925
Jack Jansen5f653091995-01-18 13:53:49 +0000926/*
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000927** Convert a Python object to an FSSpec.
928** The object may either be a full pathname or a triple
929** (vrefnum, dirid, path).
Jack Jansen5f653091995-01-18 13:53:49 +0000930** NOTE: This routine will fail on pre-sys7 machines.
931** The caller is responsible for not calling this routine
932** in those cases (which is fine, since everyone calling
933** this is probably sys7 dependent anyway).
934*/
935int
Guido van Rossum8f691791995-01-18 23:57:26 +0000936PyMac_GetFSSpec(PyObject *v, FSSpec *fs)
Jack Jansen5f653091995-01-18 13:53:49 +0000937{
938 Str255 path;
939 short refnum;
940 long parid;
941 OSErr err;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000942 FSSpec *fs2;
Jack Jansen5f653091995-01-18 13:53:49 +0000943
Jack Jansene8e8ae01995-01-26 16:36:45 +0000944 /* first check whether it already is an FSSpec */
945 fs2 = mfs_GetFSSpecFSSpec(v);
946 if ( fs2 ) {
Jack Jansen3ec804a1995-02-20 15:56:10 +0000947 (void)FSMakeFSSpec(fs2->vRefNum, fs2->parID, fs2->name, fs);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000948 return 1;
949 }
Jack Jansen5f653091995-01-18 13:53:49 +0000950 if ( PyString_Check(v) ) {
951 /* It's a pathname */
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000952 if( !PyArg_Parse(v, "O&", PyMac_GetStr255, &path) )
Jack Jansen5f653091995-01-18 13:53:49 +0000953 return 0;
Jack Jansen378815c1996-03-06 16:21:34 +0000954 refnum = 0; /* XXXX Should get CurWD here?? */
Jack Jansen5f653091995-01-18 13:53:49 +0000955 parid = 0;
956 } else {
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000957 if( !PyArg_Parse(v, "(hlO&); FSSpec should be fullpath or (vrefnum,dirid,path)",
958 &refnum, &parid, PyMac_GetStr255, &path)) {
Jack Jansen5f653091995-01-18 13:53:49 +0000959 return 0;
Guido van Rossum9aa3d131995-01-21 13:46:04 +0000960 }
Jack Jansen5f653091995-01-18 13:53:49 +0000961 }
962 err = FSMakeFSSpec(refnum, parid, path, fs);
963 if ( err && err != fnfErr ) {
Jack Jansen3d757542000-01-24 09:56:06 +0000964 PyMac_Error(err);
Jack Jansen5f653091995-01-18 13:53:49 +0000965 return 0;
966 }
967 return 1;
968}
969
Jack Jansen8bb573e1999-12-12 21:37:14 +0000970/* Convert FSSpec to PyObject */
971PyObject *PyMac_BuildFSSpec(FSSpec *v)
972{
973 return newmfssobject(v);
974}
Guido van Rossum8f691791995-01-18 23:57:26 +0000975
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000976/* Convert a Python object to a Rect.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000977 The object must be a (left, top, right, bottom) tuple.
978 (This differs from the order in the struct but is consistent with
979 the arguments to SetRect(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000980int
981PyMac_GetRect(PyObject *v, Rect *r)
Guido van Rossum8f691791995-01-18 23:57:26 +0000982{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000983 return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
Guido van Rossum8f691791995-01-18 23:57:26 +0000984}
Guido van Rossumb3404661995-01-22 18:36:13 +0000985
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000986/* Convert a Rect to a Python object */
Guido van Rossumb3404661995-01-22 18:36:13 +0000987PyObject *
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000988PyMac_BuildRect(Rect *r)
Guido van Rossumb3404661995-01-22 18:36:13 +0000989{
Guido van Rossum5279ec61995-01-26 22:56:59 +0000990 return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000991}
992
993
994/* Convert a Python object to a Point.
Guido van Rossum5279ec61995-01-26 22:56:59 +0000995 The object must be a (h, v) tuple.
996 (This differs from the order in the struct but is consistent with
997 the arguments to SetPoint(), and also with STDWIN). */
Guido van Rossumcf27c2d1995-01-25 23:06:44 +0000998int
999PyMac_GetPoint(PyObject *v, Point *p)
1000{
Guido van Rossum5279ec61995-01-26 22:56:59 +00001001 return PyArg_Parse(v, "(hh)", &p->h, &p->v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +00001002}
1003
1004/* Convert a Point to a Python object */
1005PyObject *
1006PyMac_BuildPoint(Point p)
1007{
Guido van Rossum5279ec61995-01-26 22:56:59 +00001008 return Py_BuildValue("(hh)", p.h, p.v);
Guido van Rossumcf27c2d1995-01-25 23:06:44 +00001009}
1010
1011
1012/* Convert a Python object to an EventRecord.
1013 The object must be a (what, message, when, (v, h), modifiers) tuple. */
1014int
1015PyMac_GetEventRecord(PyObject *v, EventRecord *e)
1016{
1017 return PyArg_Parse(v, "(hll(hh)h)",
1018 &e->what,
1019 &e->message,
1020 &e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +00001021 &e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +00001022 &e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +00001023 &e->modifiers);
1024}
1025
1026/* Convert a Rect to an EventRecord object */
1027PyObject *
1028PyMac_BuildEventRecord(EventRecord *e)
1029{
1030 return Py_BuildValue("(hll(hh)h)",
1031 e->what,
1032 e->message,
1033 e->when,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +00001034 e->where.h,
Guido van Rossum5279ec61995-01-26 22:56:59 +00001035 e->where.v,
Guido van Rossumcf27c2d1995-01-25 23:06:44 +00001036 e->modifiers);
Guido van Rossumb3404661995-01-22 18:36:13 +00001037}
Jack Jansenfa4d5d01995-11-15 15:19:29 +00001038
1039/* Convert Python object to Fixed */
1040int
1041PyMac_GetFixed(PyObject *v, Fixed *f)
1042{
1043 double d;
1044
1045 if( !PyArg_Parse(v, "d", &d))
1046 return 0;
1047 *f = (Fixed)(d * 0x10000);
Jack Jansen31dd5c01996-05-31 13:01:39 +00001048 return 1;
Jack Jansenfa4d5d01995-11-15 15:19:29 +00001049}
1050
1051/* Convert a Point to a Python object */
1052PyObject *
1053PyMac_BuildFixed(Fixed f)
1054{
1055 double d;
1056
1057 d = f;
1058 d = d / 0x10000;
1059 return Py_BuildValue("d", d);
1060}
1061
Jack Jansend58cd631998-04-21 15:24:39 +00001062/* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */
1063int
1064PyMac_Getwide(PyObject *v, wide *rv)
1065{
1066 if (PyInt_Check(v)) {
1067 rv->hi = 0;
1068 rv->lo = PyInt_AsLong(v);
1069 if( rv->lo & 0x80000000 )
1070 rv->hi = -1;
1071 return 1;
1072 }
1073 return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo);
1074}
1075
1076
1077PyObject *
Jack Jansenbbf4be21998-04-23 13:20:17 +00001078PyMac_Buildwide(wide *w)
Jack Jansend58cd631998-04-21 15:24:39 +00001079{
Jack Jansenbbf4be21998-04-23 13:20:17 +00001080 if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) ||
1081 (w->hi == -1 && (w->lo & 0x80000000) ) )
1082 return PyInt_FromLong(w->lo);
1083 return Py_BuildValue("(ll)", w->hi, w->lo);
Jack Jansend58cd631998-04-21 15:24:39 +00001084}