blob: 0b8d8561b10992e9ef26dd48a31a3b29d4164c3e [file] [log] [blame]
Guido van Rossum2d167031994-09-16 10:54:21 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossum99546991995-01-08 14:33:34 +00003The Netherlands.
Guido van Rossum2d167031994-09-16 10:54:21 +00004
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******************************************************************/
24
25/* Macintosh OS-specific interface */
26
27#include "Python.h"
Jack Jansen97ce3611994-12-14 14:02:24 +000028#include "macglue.h"
Jack Jansenb19c6672000-10-12 21:24:05 +000029#include "pythonresources.h"
Guido van Rossum2d167031994-09-16 10:54:21 +000030
Jack Jansen6143d532001-05-19 12:34:59 +000031#ifdef WITHOUT_FRAMEWORKS
Jack Jansenee23d6e1995-01-27 14:43:25 +000032#include <Windows.h>
Jack Jansen76a05891996-02-29 16:11:32 +000033#include <Files.h>
Jack Janseneb76b841996-09-30 14:43:22 +000034#include <LowMem.h>
Jack Jansencbe6a531998-02-20 15:59:59 +000035#include <Sound.h>
Jack Jansene79dc762000-06-02 21:35:07 +000036#include <Events.h>
Jack Jansen6143d532001-05-19 12:34:59 +000037#else
38#include <Carbon/Carbon.h>
39#endif
Jack Jansenee23d6e1995-01-27 14:43:25 +000040
Guido van Rossum2d167031994-09-16 10:54:21 +000041static PyObject *MacOS_Error; /* Exception MacOS.Error */
42
Guido van Rossume6d9ccc1995-02-21 21:01:05 +000043#ifdef MPW
Guido van Rossum9fed1831995-02-18 15:02:02 +000044#define bufferIsSmall -607 /*error returns from Post and Accept */
45#endif
46
Jack Jansen76a05891996-02-29 16:11:32 +000047/* ----------------------------------------------------- */
48
49/* Declarations for objects of type Resource fork */
50
51typedef struct {
52 PyObject_HEAD
53 short fRefNum;
54 int isclosed;
55} rfobject;
56
57staticforward PyTypeObject Rftype;
58
59
60
61/* ---------------------------------------------------------------- */
62
63static void
Jack Jansendeefbe52001-08-08 13:46:49 +000064do_close(rfobject *self)
Jack Jansen76a05891996-02-29 16:11:32 +000065{
66 if (self->isclosed ) return;
67 (void)FSClose(self->fRefNum);
68 self->isclosed = 1;
69}
70
71static char rf_read__doc__[] =
72"Read data from resource fork"
73;
74
75static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +000076rf_read(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +000077{
78 long n;
79 PyObject *v;
80 OSErr err;
81
82 if (self->isclosed) {
83 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
84 return NULL;
85 }
86
87 if (!PyArg_ParseTuple(args, "l", &n))
88 return NULL;
89
90 v = PyString_FromStringAndSize((char *)NULL, n);
91 if (v == NULL)
92 return NULL;
93
94 err = FSRead(self->fRefNum, &n, PyString_AsString(v));
95 if (err && err != eofErr) {
96 PyMac_Error(err);
97 Py_DECREF(v);
98 return NULL;
99 }
100 _PyString_Resize(&v, n);
101 return v;
102}
103
104
105static char rf_write__doc__[] =
106"Write to resource fork"
107;
108
109static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000110rf_write(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000111{
112 char *buffer;
113 long size;
114 OSErr err;
115
116 if (self->isclosed) {
117 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
118 return NULL;
119 }
Jack Janseneeccca91997-05-07 15:46:31 +0000120 if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
Jack Jansen76a05891996-02-29 16:11:32 +0000121 return NULL;
122 err = FSWrite(self->fRefNum, &size, buffer);
123 if (err) {
124 PyMac_Error(err);
125 return NULL;
126 }
127 Py_INCREF(Py_None);
128 return Py_None;
129}
130
131
132static char rf_seek__doc__[] =
133"Set file position"
134;
135
136static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000137rf_seek(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000138{
139 long amount, pos;
140 int whence = SEEK_SET;
141 long eof;
142 OSErr err;
143
144 if (self->isclosed) {
145 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
146 return NULL;
147 }
148 if (!PyArg_ParseTuple(args, "l|i", &amount, &whence))
149 return NULL;
150
Jack Jansendeefbe52001-08-08 13:46:49 +0000151 if ((err = GetEOF(self->fRefNum, &eof)))
Jack Jansen76a05891996-02-29 16:11:32 +0000152 goto ioerr;
153
154 switch (whence) {
155 case SEEK_CUR:
Jack Jansendeefbe52001-08-08 13:46:49 +0000156 if ((err = GetFPos(self->fRefNum, &pos)))
Jack Jansen76a05891996-02-29 16:11:32 +0000157 goto ioerr;
158 break;
159 case SEEK_END:
160 pos = eof;
161 break;
162 case SEEK_SET:
163 pos = 0;
164 break;
165 default:
166 PyErr_BadArgument();
167 return NULL;
168 }
169
170 pos += amount;
171
172 /* Don't bother implementing seek past EOF */
173 if (pos > eof || pos < 0) {
174 PyErr_BadArgument();
175 return NULL;
176 }
177
Jack Jansendeefbe52001-08-08 13:46:49 +0000178 if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000179ioerr:
180 PyMac_Error(err);
181 return NULL;
182 }
183 Py_INCREF(Py_None);
184 return Py_None;
185}
186
187
188static char rf_tell__doc__[] =
189"Get file position"
190;
191
192static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000193rf_tell(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000194{
195 long where;
196 OSErr err;
197
198 if (self->isclosed) {
199 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
200 return NULL;
201 }
202 if (!PyArg_ParseTuple(args, ""))
203 return NULL;
Jack Jansendeefbe52001-08-08 13:46:49 +0000204 if ((err = GetFPos(self->fRefNum, &where)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000205 PyMac_Error(err);
206 return NULL;
207 }
208 return PyInt_FromLong(where);
209}
210
211static char rf_close__doc__[] =
212"Close resource fork"
213;
214
215static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000216rf_close(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000217{
Jack Jansen76a05891996-02-29 16:11:32 +0000218 if (!PyArg_ParseTuple(args, ""))
219 return NULL;
220 do_close(self);
221 Py_INCREF(Py_None);
222 return Py_None;
223}
224
225
226static struct PyMethodDef rf_methods[] = {
Jack Jansendeefbe52001-08-08 13:46:49 +0000227 {"read", (PyCFunction)rf_read, 1, rf_read__doc__},
228 {"write", (PyCFunction)rf_write, 1, rf_write__doc__},
229 {"seek", (PyCFunction)rf_seek, 1, rf_seek__doc__},
230 {"tell", (PyCFunction)rf_tell, 1, rf_tell__doc__},
231 {"close", (PyCFunction)rf_close, 1, rf_close__doc__},
Jack Jansen76a05891996-02-29 16:11:32 +0000232
233 {NULL, NULL} /* sentinel */
234};
235
236/* ---------- */
237
238
239static rfobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000240newrfobject(void)
Jack Jansen76a05891996-02-29 16:11:32 +0000241{
242 rfobject *self;
243
244 self = PyObject_NEW(rfobject, &Rftype);
245 if (self == NULL)
246 return NULL;
247 self->isclosed = 1;
248 return self;
249}
250
251
252static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000253rf_dealloc(rfobject *self)
Jack Jansen76a05891996-02-29 16:11:32 +0000254{
255 do_close(self);
256 PyMem_DEL(self);
257}
258
259static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000260rf_getattr(rfobject *self, char *name)
Jack Jansen76a05891996-02-29 16:11:32 +0000261{
262 return Py_FindMethod(rf_methods, (PyObject *)self, name);
263}
264
265static char Rftype__doc__[] =
266"Resource fork file object"
267;
268
269static PyTypeObject Rftype = {
270 PyObject_HEAD_INIT(&PyType_Type)
271 0, /*ob_size*/
Jack Jansena755e681997-09-20 17:40:22 +0000272 "ResourceFork", /*tp_name*/
Jack Jansen76a05891996-02-29 16:11:32 +0000273 sizeof(rfobject), /*tp_basicsize*/
274 0, /*tp_itemsize*/
275 /* methods */
276 (destructor)rf_dealloc, /*tp_dealloc*/
277 (printfunc)0, /*tp_print*/
278 (getattrfunc)rf_getattr, /*tp_getattr*/
279 (setattrfunc)0, /*tp_setattr*/
280 (cmpfunc)0, /*tp_compare*/
281 (reprfunc)0, /*tp_repr*/
282 0, /*tp_as_number*/
283 0, /*tp_as_sequence*/
284 0, /*tp_as_mapping*/
285 (hashfunc)0, /*tp_hash*/
286 (ternaryfunc)0, /*tp_call*/
287 (reprfunc)0, /*tp_str*/
288
289 /* Space for future expansion */
290 0L,0L,0L,0L,
291 Rftype__doc__ /* Documentation string */
292};
293
294/* End of code for Resource fork objects */
295/* -------------------------------------------------------- */
Guido van Rossum2d167031994-09-16 10:54:21 +0000296
297/*----------------------------------------------------------------------*/
Guido van Rossume791c2e1995-01-09 13:20:04 +0000298/* Miscellaneous File System Operations */
299
Jack Jansen120a1051997-06-03 15:29:41 +0000300static char getcrtp_doc[] = "Obsolete, use macfs module";
301
Guido van Rossume791c2e1995-01-09 13:20:04 +0000302static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000303MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +0000304{
Jack Jansene79dc762000-06-02 21:35:07 +0000305 FSSpec fss;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000306 FInfo info;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000307 PyObject *creator, *type, *res;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000308 OSErr err;
309
Jack Jansene79dc762000-06-02 21:35:07 +0000310 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Guido van Rossume791c2e1995-01-09 13:20:04 +0000311 return NULL;
Jack Jansene79dc762000-06-02 21:35:07 +0000312 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000313 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000314 creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000315 type = PyString_FromStringAndSize((char *)&info.fdType, 4);
316 res = Py_BuildValue("OO", creator, type);
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000317 Py_DECREF(creator);
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000318 Py_DECREF(type);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000319 return res;
320}
321
Jack Jansen120a1051997-06-03 15:29:41 +0000322static char setcrtp_doc[] = "Obsolete, use macfs module";
323
Guido van Rossume791c2e1995-01-09 13:20:04 +0000324static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000325MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +0000326{
Jack Jansene79dc762000-06-02 21:35:07 +0000327 FSSpec fss;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000328 ResType creator, type;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000329 FInfo info;
330 OSErr err;
331
332 if (!PyArg_ParseTuple(args, "O&O&O&",
Jack Jansene79dc762000-06-02 21:35:07 +0000333 PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Guido van Rossume791c2e1995-01-09 13:20:04 +0000334 return NULL;
Jack Jansene79dc762000-06-02 21:35:07 +0000335 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000336 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000337 info.fdCreator = creator;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000338 info.fdType = type;
Jack Jansene79dc762000-06-02 21:35:07 +0000339 if ((err = FSpSetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000340 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000341 Py_INCREF(Py_None);
342 return Py_None;
343}
344
Jack Jansen6143d532001-05-19 12:34:59 +0000345#if TARGET_API_MAC_OS8
Guido van Rossume791c2e1995-01-09 13:20:04 +0000346/*----------------------------------------------------------------------*/
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000347/* STDWIN High Level Event interface */
348
349#include <EPPC.h>
350#include <Events.h>
351
Jack Jansen120a1051997-06-03 15:29:41 +0000352static char accepthle_doc[] = "Get arguments of pending high-level event";
353
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000354static PyObject *
355MacOS_AcceptHighLevelEvent(self, args)
356 PyObject *self;
357 PyObject *args;
358{
359 TargetID sender;
360 unsigned long refcon;
361 Ptr buf;
362 unsigned long len;
363 OSErr err;
364 PyObject *res;
365
366 buf = NULL;
367 len = 0;
368 err = AcceptHighLevelEvent(&sender, &refcon, buf, &len);
369 if (err == bufferIsSmall) {
370 buf = malloc(len);
371 if (buf == NULL)
372 return PyErr_NoMemory();
373 err = AcceptHighLevelEvent(&sender, &refcon, buf, &len);
374 if (err != noErr) {
375 free(buf);
376 return PyErr_Mac(MacOS_Error, (int)err);
377 }
378 }
379 else if (err != noErr)
380 return PyErr_Mac(MacOS_Error, (int)err);
381 res = Py_BuildValue("s#ls#",
382 (char *)&sender, (int)(sizeof sender), refcon, (char *)buf, (int)len);
383 free(buf);
384 return res;
385}
Jack Jansene79dc762000-06-02 21:35:07 +0000386#endif
Jack Jansenf3163302001-05-19 12:50:05 +0000387
388#if !TARGET_API_MAC_OSX
Jack Jansen120a1051997-06-03 15:29:41 +0000389static char schedparams_doc[] = "Set/return mainloop interrupt check flag, etc";
390
Jack Jansene8e8ae01995-01-26 16:36:45 +0000391/*
Jack Jansen120a1051997-06-03 15:29:41 +0000392** Set scheduler parameters
Jack Jansene8e8ae01995-01-26 16:36:45 +0000393*/
394static PyObject *
Jack Jansen120a1051997-06-03 15:29:41 +0000395MacOS_SchedParams(PyObject *self, PyObject *args)
Jack Jansene8e8ae01995-01-26 16:36:45 +0000396{
Jack Jansen120a1051997-06-03 15:29:41 +0000397 PyMacSchedParams old, new;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000398
Jack Jansen120a1051997-06-03 15:29:41 +0000399 PyMac_GetSchedParams(&old);
400 new = old;
401 if (!PyArg_ParseTuple(args, "|iiidd", &new.check_interrupt, &new.process_events,
402 &new.besocial, &new.check_interval, &new.bg_yield))
Jack Jansene8e8ae01995-01-26 16:36:45 +0000403 return NULL;
Jack Jansen120a1051997-06-03 15:29:41 +0000404 PyMac_SetSchedParams(&new);
405 return Py_BuildValue("iiidd", old.check_interrupt, old.process_events,
406 old.besocial, old.check_interval, old.bg_yield);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000407}
408
Jack Jansen120a1051997-06-03 15:29:41 +0000409static char appswitch_doc[] = "Obsolete, use SchedParams";
410
411/* Obsolete, for backward compatability */
Jack Jansenee23d6e1995-01-27 14:43:25 +0000412static PyObject *
413MacOS_EnableAppswitch(PyObject *self, PyObject *args)
414{
Jack Jansen120a1051997-06-03 15:29:41 +0000415 int new, old;
416 PyMacSchedParams schp;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000417
Guido van Rossume7134aa1995-02-26 10:20:53 +0000418 if (!PyArg_ParseTuple(args, "i", &new))
Jack Jansenee23d6e1995-01-27 14:43:25 +0000419 return NULL;
Jack Jansen120a1051997-06-03 15:29:41 +0000420 PyMac_GetSchedParams(&schp);
421 if ( schp.process_events )
422 old = 1;
423 else if ( schp.check_interrupt )
Jack Jansen120a1051997-06-03 15:29:41 +0000424 old = 0;
Jack Jansen2e871e41997-09-08 13:23:19 +0000425 else
426 old = -1;
Jack Jansen120a1051997-06-03 15:29:41 +0000427 if ( new > 0 ) {
428 schp.process_events = mDownMask|keyDownMask|osMask;
429 schp.check_interrupt = 1;
Jack Jansen2e871e41997-09-08 13:23:19 +0000430 } else if ( new == 0 ) {
Jack Jansen120a1051997-06-03 15:29:41 +0000431 schp.process_events = 0;
432 schp.check_interrupt = 1;
433 } else {
434 schp.process_events = 0;
435 schp.check_interrupt = 0;
436 }
437 PyMac_SetSchedParams(&schp);
Guido van Rossume7134aa1995-02-26 10:20:53 +0000438 return Py_BuildValue("i", old);
Jack Jansenee23d6e1995-01-27 14:43:25 +0000439}
440
Jack Jansen883765e1997-06-20 16:19:38 +0000441static char setevh_doc[] = "Set python event handler to be called in mainloop";
442
443static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000444MacOS_SetEventHandler(PyObject *self, PyObject *args)
Jack Jansen883765e1997-06-20 16:19:38 +0000445{
446 PyObject *evh = NULL;
447
448 if (!PyArg_ParseTuple(args, "|O", &evh))
449 return NULL;
450 if (evh == Py_None)
451 evh = NULL;
452 if ( evh && !PyCallable_Check(evh) ) {
453 PyErr_SetString(PyExc_ValueError, "SetEventHandler argument must be callable");
454 return NULL;
455 }
456 if ( !PyMac_SetEventHandler(evh) )
457 return NULL;
458 Py_INCREF(Py_None);
459 return Py_None;
460}
461
Jack Jansen120a1051997-06-03 15:29:41 +0000462static char handleev_doc[] = "Pass event to other interested parties like sioux";
Jack Jansena76382a1995-02-02 14:25:56 +0000463
464static PyObject *
465MacOS_HandleEvent(PyObject *self, PyObject *args)
466{
467 EventRecord ev;
468
469 if (!PyArg_ParseTuple(args, "O&", PyMac_GetEventRecord, &ev))
470 return NULL;
Jack Jansen883765e1997-06-20 16:19:38 +0000471 PyMac_HandleEventIntern(&ev);
Jack Jansena76382a1995-02-02 14:25:56 +0000472 Py_INCREF(Py_None);
473 return Py_None;
474}
Jack Jansenf3163302001-05-19 12:50:05 +0000475#endif /* !TARGET_API_MAC_OSX */
Jack Jansena76382a1995-02-02 14:25:56 +0000476
Jack Jansen120a1051997-06-03 15:29:41 +0000477static char geterr_doc[] = "Convert OSErr number to string";
478
Jack Jansen829f88c1995-07-17 11:36:01 +0000479static PyObject *
480MacOS_GetErrorString(PyObject *self, PyObject *args)
481{
482 int errn;
483
484 if (!PyArg_ParseTuple(args, "i", &errn))
485 return NULL;
486 return Py_BuildValue("s", PyMac_StrError(errn));
487}
488
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000489static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)";
490
491static PyObject *
492MacOS_splash(PyObject *self, PyObject *args)
493{
Jack Jansendf34cf11996-09-15 22:12:00 +0000494 int resid = -1;
Jack Jansen450ae9f1997-05-13 15:41:48 +0000495 static DialogPtr curdialog = NULL;
Jack Jansen2e871e41997-09-08 13:23:19 +0000496 DialogPtr olddialog;
Jack Jansen04df9d51996-09-23 15:49:43 +0000497 WindowRef theWindow;
498 CGrafPtr thePort;
Jack Jansene79dc762000-06-02 21:35:07 +0000499#if 0
Jack Jansen04df9d51996-09-23 15:49:43 +0000500 short xpos, ypos, width, height, swidth, sheight;
Jack Jansene79dc762000-06-02 21:35:07 +0000501#endif
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000502
Jack Jansendf34cf11996-09-15 22:12:00 +0000503 if (!PyArg_ParseTuple(args, "|i", &resid))
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000504 return NULL;
Jack Jansen2e871e41997-09-08 13:23:19 +0000505 olddialog = curdialog;
Jack Jansencbe6a531998-02-20 15:59:59 +0000506 curdialog = NULL;
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000507
Jack Jansendf34cf11996-09-15 22:12:00 +0000508 if ( resid != -1 ) {
509 curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
Jack Jansen04df9d51996-09-23 15:49:43 +0000510 if ( curdialog ) {
511 theWindow = GetDialogWindow(curdialog);
512 thePort = GetWindowPort(theWindow);
Jack Jansene79dc762000-06-02 21:35:07 +0000513#if 0
Jack Jansen04df9d51996-09-23 15:49:43 +0000514 width = thePort->portRect.right - thePort->portRect.left;
515 height = thePort->portRect.bottom - thePort->portRect.top;
516 swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
517 sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight();
518 xpos = (swidth-width)/2;
519 ypos = (sheight-height)/5 + LMGetMBarHeight();
520 MoveWindow(theWindow, xpos, ypos, 0);
521 ShowWindow(theWindow);
Jack Jansene79dc762000-06-02 21:35:07 +0000522#endif
Jack Jansendf34cf11996-09-15 22:12:00 +0000523 DrawDialog(curdialog);
Jack Jansen04df9d51996-09-23 15:49:43 +0000524 }
Jack Jansendf34cf11996-09-15 22:12:00 +0000525 }
Jack Jansen2e871e41997-09-08 13:23:19 +0000526 if (olddialog)
527 DisposeDialog(olddialog);
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000528 Py_INCREF(Py_None);
529 return Py_None;
530}
531
Jack Janseneb76b841996-09-30 14:43:22 +0000532static char DebugStr_doc[] = "Switch to low-level debugger with a message";
533
534static PyObject *
535MacOS_DebugStr(PyObject *self, PyObject *args)
536{
537 Str255 message;
538 PyObject *object = 0;
539
540 if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
541 return NULL;
542 DebugStr(message);
543 Py_INCREF(Py_None);
544 return Py_None;
545}
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000546
Jack Jansen2e871e41997-09-08 13:23:19 +0000547static char SysBeep_doc[] = "BEEEEEP!!!";
548
549static PyObject *
550MacOS_SysBeep(PyObject *self, PyObject *args)
551{
552 int duration = 6;
553
554 if (!PyArg_ParseTuple(args, "|i", &duration))
555 return NULL;
556 SysBeep(duration);
557 Py_INCREF(Py_None);
558 return Py_None;
559}
560
Jack Jansen898ac1b1997-09-01 15:38:12 +0000561static char GetTicks_doc[] = "Return number of ticks since bootup";
562
563static PyObject *
564MacOS_GetTicks(PyObject *self, PyObject *args)
565{
Jack Jansene79dc762000-06-02 21:35:07 +0000566 return Py_BuildValue("i", (int)TickCount());
Jack Jansen898ac1b1997-09-01 15:38:12 +0000567}
568
Jack Jansen76a05891996-02-29 16:11:32 +0000569static char openrf_doc[] = "Open resource fork of a file";
570
571static PyObject *
572MacOS_openrf(PyObject *self, PyObject *args)
573{
574 OSErr err;
575 char *mode = "r";
576 FSSpec fss;
577 SignedByte permission = 1;
578 rfobject *fp;
579
580 if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSSpec, &fss, &mode))
581 return NULL;
582 while (*mode) {
583 switch (*mode++) {
584 case '*': break;
585 case 'r': permission = 1; break;
586 case 'w': permission = 2; break;
587 case 'b': break;
588 default:
589 PyErr_BadArgument();
590 return NULL;
591 }
592 }
593
594 if ( (fp = newrfobject()) == NULL )
595 return NULL;
596
597 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
598
Jack Jansendeefbe52001-08-08 13:46:49 +0000599#if !TARGET_API_MAC_OSX
Jack Jansen76a05891996-02-29 16:11:32 +0000600 if ( err == fnfErr ) {
601 /* In stead of doing complicated things here to get creator/type
602 ** correct we let the standard i/o library handle it
603 */
604 FILE *tfp;
605 char pathname[257];
606
Jack Jansen84fb1fa1996-11-09 18:46:57 +0000607 if ( err=PyMac_GetFullPath(&fss, pathname) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000608 PyMac_Error(err);
609 Py_DECREF(fp);
610 return NULL;
611 }
612
613 if ( (tfp = fopen(pathname, "w")) == NULL ) {
614 PyMac_Error(fnfErr); /* What else... */
615 Py_DECREF(fp);
616 return NULL;
617 }
618 fclose(tfp);
619 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
620 }
Jack Jansendeefbe52001-08-08 13:46:49 +0000621#endif
Jack Jansen76a05891996-02-29 16:11:32 +0000622 if ( err ) {
623 Py_DECREF(fp);
624 PyMac_Error(err);
625 return NULL;
626 }
627 fp->isclosed = 0;
628 return (PyObject *)fp;
629}
630
Jack Jansenf3163302001-05-19 12:50:05 +0000631#if !TARGET_API_MAC_OSX
Jack Jansen957d07a2000-02-21 11:07:37 +0000632static char FreeMem_doc[] = "Return the total amount of free space in the heap";
633
634static PyObject *
635MacOS_FreeMem(PyObject *self, PyObject *args)
636{
637 long rv;
638
639 if (!PyArg_ParseTuple(args, ""))
640 return NULL;
641 rv = FreeMem();
642 return Py_BuildValue("l", rv);
643}
644
645static char MaxBlock_doc[] = "Return the largest contiguous block of free space in the heap";
646
647static PyObject *
648MacOS_MaxBlock(PyObject *self, PyObject *args)
649{
650 long rv;
651
652 if (!PyArg_ParseTuple(args, ""))
653 return NULL;
654 rv = MaxBlock();
655 return Py_BuildValue("l", rv);
656}
657
658static char CompactMem_doc[] = "(wanted size)->actual largest block after compacting";
659
660static PyObject *
661MacOS_CompactMem(PyObject *self, PyObject *args)
662{
663 long value;
664 long rv;
665
666 if (!PyArg_ParseTuple(args, "l", &value))
667 return NULL;
668 rv = CompactMem(value);
669 return Py_BuildValue("l", rv);
670}
671
Jack Jansenb19c6672000-10-12 21:24:05 +0000672static char KeepConsole_doc[] = "(flag) Keep console open 0:never, 1:on output 2:on error, 3:always";
673
674static PyObject *
675MacOS_KeepConsole(PyObject *self, PyObject *args)
676{
677 int value;
678
679 if (!PyArg_ParseTuple(args, "i", &value))
680 return NULL;
681 PyMac_options.keep_console = value;
682 Py_INCREF(Py_None);
683 return Py_None;
684}
685
Jack Jansen8413b472000-10-19 22:02:16 +0000686static char OutputSeen_doc[] = "Call to reset the 'unseen output' flag for the keep-console-open option";
687
688static PyObject *
689MacOS_OutputSeen(PyObject *self, PyObject *args)
690{
691 if (!PyArg_ParseTuple(args, ""))
692 return NULL;
693 PyMac_OutputSeen();
694 Py_INCREF(Py_None);
695 return Py_None;
696}
Jack Jansenf3163302001-05-19 12:50:05 +0000697#endif /* !TARGET_API_MAC_OSX */
Jack Jansen8413b472000-10-19 22:02:16 +0000698
Guido van Rossum2d167031994-09-16 10:54:21 +0000699static PyMethodDef MacOS_Methods[] = {
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000700#if TARGET_API_MAC_OS8
Jack Jansen120a1051997-06-03 15:29:41 +0000701 {"AcceptHighLevelEvent", MacOS_AcceptHighLevelEvent, 1, accepthle_doc},
Jack Jansene79dc762000-06-02 21:35:07 +0000702#endif
Jack Jansen120a1051997-06-03 15:29:41 +0000703 {"GetCreatorAndType", MacOS_GetCreatorAndType, 1, getcrtp_doc},
704 {"SetCreatorAndType", MacOS_SetCreatorAndType, 1, setcrtp_doc},
Jack Jansenf3163302001-05-19 12:50:05 +0000705#if !TARGET_API_MAC_OSX
Jack Jansen120a1051997-06-03 15:29:41 +0000706 {"SchedParams", MacOS_SchedParams, 1, schedparams_doc},
707 {"EnableAppswitch", MacOS_EnableAppswitch, 1, appswitch_doc},
Jack Jansen883765e1997-06-20 16:19:38 +0000708 {"SetEventHandler", MacOS_SetEventHandler, 1, setevh_doc},
Jack Jansen120a1051997-06-03 15:29:41 +0000709 {"HandleEvent", MacOS_HandleEvent, 1, handleev_doc},
Jack Jansenf3163302001-05-19 12:50:05 +0000710#endif
Jack Jansen120a1051997-06-03 15:29:41 +0000711 {"GetErrorString", MacOS_GetErrorString, 1, geterr_doc},
712 {"openrf", MacOS_openrf, 1, openrf_doc},
713 {"splash", MacOS_splash, 1, splash_doc},
714 {"DebugStr", MacOS_DebugStr, 1, DebugStr_doc},
Jack Jansen898ac1b1997-09-01 15:38:12 +0000715 {"GetTicks", MacOS_GetTicks, 1, GetTicks_doc},
Jack Jansen2e871e41997-09-08 13:23:19 +0000716 {"SysBeep", MacOS_SysBeep, 1, SysBeep_doc},
Jack Jansenf3163302001-05-19 12:50:05 +0000717#if !TARGET_API_MAC_OSX
Jack Jansen957d07a2000-02-21 11:07:37 +0000718 {"FreeMem", MacOS_FreeMem, 1, FreeMem_doc},
719 {"MaxBlock", MacOS_MaxBlock, 1, MaxBlock_doc},
720 {"CompactMem", MacOS_CompactMem, 1, CompactMem_doc},
Jack Jansenb19c6672000-10-12 21:24:05 +0000721 {"KeepConsole", MacOS_KeepConsole, 1, KeepConsole_doc},
Jack Jansen8413b472000-10-19 22:02:16 +0000722 {"OutputSeen", MacOS_OutputSeen, 1, OutputSeen_doc},
Jack Jansenf3163302001-05-19 12:50:05 +0000723#endif
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000724 {NULL, NULL} /* Sentinel */
Guido van Rossum2d167031994-09-16 10:54:21 +0000725};
726
727
728void
Jack Jansendeefbe52001-08-08 13:46:49 +0000729initMacOS(void)
Guido van Rossum2d167031994-09-16 10:54:21 +0000730{
731 PyObject *m, *d;
732
733 m = Py_InitModule("MacOS", MacOS_Methods);
734 d = PyModule_GetDict(m);
735
736 /* Initialize MacOS.Error exception */
Guido van Rossumbf068b11995-01-25 23:09:20 +0000737 MacOS_Error = PyMac_GetOSErrException();
Guido van Rossume433c971994-09-29 10:02:56 +0000738 if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000739 return;
Jack Jansena755e681997-09-20 17:40:22 +0000740 Rftype.ob_type = &PyType_Type;
741 Py_INCREF(&Rftype);
742 if (PyDict_SetItemString(d, "ResourceForkType", (PyObject *)&Rftype) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000743 return;
Jack Jansenf73bab71997-04-03 14:51:03 +0000744 /*
745 ** This is a hack: the following constant added to the id() of a string
746 ** object gives you the address of the data. Unfortunately, it is needed for
747 ** some of the image and sound processing interfaces on the mac:-(
748 */
749 {
750 PyStringObject *p = 0;
751 long off = (long)&(p->ob_sval[0]);
752
753 if( PyDict_SetItemString(d, "string_id_to_buffer", Py_BuildValue("i", off)) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000754 return;
Jack Jansenf73bab71997-04-03 14:51:03 +0000755 }
Jack Jansendeefbe52001-08-08 13:46:49 +0000756#if !TARGET_API_MAC_OSX
Jack Jansen8f5725a1999-12-07 23:08:10 +0000757 if (PyDict_SetItemString(d, "AppearanceCompliant",
758 Py_BuildValue("i", PyMac_AppearanceCompliant)) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000759 return;
Jack Jansendeefbe52001-08-08 13:46:49 +0000760#endif
Jack Jansenf3163302001-05-19 12:50:05 +0000761#if TARGET_API_MAC_OSX
762#define PY_RUNTIMEMODEL "macho"
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000763#elif TARGET_API_MAC_OS8
Jack Jansen193509b2001-01-23 22:38:23 +0000764#define PY_RUNTIMEMODEL "ppc"
Jack Jansenf3163302001-05-19 12:50:05 +0000765#elif TARGET_API_MAC_CARBON
766#define PY_RUNTIMEMODEL "carbon"
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000767#else
768#error "None of the TARGET_API_MAC_XXX I know about is set"
Jack Jansen193509b2001-01-23 22:38:23 +0000769#endif
770 if (PyDict_SetItemString(d, "runtimemodel",
771 Py_BuildValue("s", PY_RUNTIMEMODEL)) != 0)
772 return;
Guido van Rossum2d167031994-09-16 10:54:21 +0000773}
Guido van Rossume7134aa1995-02-26 10:20:53 +0000774