blob: 6790418b9619758997ce9e98d00472d43b8e5c4c [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>
Jack Jansend7c17232003-02-21 16:31:11 +000039#include <ApplicationServices/ApplicationServices.h>
Jack Jansen6143d532001-05-19 12:34:59 +000040#endif
Jack Jansenee23d6e1995-01-27 14:43:25 +000041
Guido van Rossum2d167031994-09-16 10:54:21 +000042static PyObject *MacOS_Error; /* Exception MacOS.Error */
43
Jack Jansen697842f2001-09-10 22:00:39 +000044#ifdef TARGET_API_MAC_OSX
45#define PATHNAMELEN 1024
46#else
47#define PATHNAMELEN 256
48#endif
49
Guido van Rossume6d9ccc1995-02-21 21:01:05 +000050#ifdef MPW
Guido van Rossum9fed1831995-02-18 15:02:02 +000051#define bufferIsSmall -607 /*error returns from Post and Accept */
52#endif
53
Jack Jansen76a05891996-02-29 16:11:32 +000054/* ----------------------------------------------------- */
55
56/* Declarations for objects of type Resource fork */
57
58typedef struct {
59 PyObject_HEAD
60 short fRefNum;
61 int isclosed;
62} rfobject;
63
Jeremy Hylton938ace62002-07-17 16:30:39 +000064static PyTypeObject Rftype;
Jack Jansen76a05891996-02-29 16:11:32 +000065
66
67
68/* ---------------------------------------------------------------- */
69
70static void
Jack Jansendeefbe52001-08-08 13:46:49 +000071do_close(rfobject *self)
Jack Jansen76a05891996-02-29 16:11:32 +000072{
73 if (self->isclosed ) return;
74 (void)FSClose(self->fRefNum);
75 self->isclosed = 1;
76}
77
78static char rf_read__doc__[] =
79"Read data from resource fork"
80;
81
82static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +000083rf_read(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +000084{
85 long n;
86 PyObject *v;
87 OSErr err;
88
89 if (self->isclosed) {
90 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
91 return NULL;
92 }
93
94 if (!PyArg_ParseTuple(args, "l", &n))
95 return NULL;
96
97 v = PyString_FromStringAndSize((char *)NULL, n);
98 if (v == NULL)
99 return NULL;
100
101 err = FSRead(self->fRefNum, &n, PyString_AsString(v));
102 if (err && err != eofErr) {
103 PyMac_Error(err);
104 Py_DECREF(v);
105 return NULL;
106 }
107 _PyString_Resize(&v, n);
108 return v;
109}
110
111
112static char rf_write__doc__[] =
113"Write to resource fork"
114;
115
116static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000117rf_write(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000118{
119 char *buffer;
120 long size;
121 OSErr err;
122
123 if (self->isclosed) {
124 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
125 return NULL;
126 }
Jack Janseneeccca91997-05-07 15:46:31 +0000127 if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
Jack Jansen76a05891996-02-29 16:11:32 +0000128 return NULL;
129 err = FSWrite(self->fRefNum, &size, buffer);
130 if (err) {
131 PyMac_Error(err);
132 return NULL;
133 }
134 Py_INCREF(Py_None);
135 return Py_None;
136}
137
138
139static char rf_seek__doc__[] =
140"Set file position"
141;
142
143static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000144rf_seek(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000145{
146 long amount, pos;
147 int whence = SEEK_SET;
148 long eof;
149 OSErr err;
150
151 if (self->isclosed) {
152 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
153 return NULL;
154 }
155 if (!PyArg_ParseTuple(args, "l|i", &amount, &whence))
156 return NULL;
157
Jack Jansendeefbe52001-08-08 13:46:49 +0000158 if ((err = GetEOF(self->fRefNum, &eof)))
Jack Jansen76a05891996-02-29 16:11:32 +0000159 goto ioerr;
160
161 switch (whence) {
162 case SEEK_CUR:
Jack Jansendeefbe52001-08-08 13:46:49 +0000163 if ((err = GetFPos(self->fRefNum, &pos)))
Jack Jansen76a05891996-02-29 16:11:32 +0000164 goto ioerr;
165 break;
166 case SEEK_END:
167 pos = eof;
168 break;
169 case SEEK_SET:
170 pos = 0;
171 break;
172 default:
173 PyErr_BadArgument();
174 return NULL;
175 }
176
177 pos += amount;
178
179 /* Don't bother implementing seek past EOF */
180 if (pos > eof || pos < 0) {
181 PyErr_BadArgument();
182 return NULL;
183 }
184
Jack Jansendeefbe52001-08-08 13:46:49 +0000185 if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000186ioerr:
187 PyMac_Error(err);
188 return NULL;
189 }
190 Py_INCREF(Py_None);
191 return Py_None;
192}
193
194
195static char rf_tell__doc__[] =
196"Get file position"
197;
198
199static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000200rf_tell(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000201{
202 long where;
203 OSErr err;
204
205 if (self->isclosed) {
206 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
207 return NULL;
208 }
209 if (!PyArg_ParseTuple(args, ""))
210 return NULL;
Jack Jansendeefbe52001-08-08 13:46:49 +0000211 if ((err = GetFPos(self->fRefNum, &where)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000212 PyMac_Error(err);
213 return NULL;
214 }
215 return PyInt_FromLong(where);
216}
217
218static char rf_close__doc__[] =
219"Close resource fork"
220;
221
222static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000223rf_close(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000224{
Jack Jansen76a05891996-02-29 16:11:32 +0000225 if (!PyArg_ParseTuple(args, ""))
226 return NULL;
227 do_close(self);
228 Py_INCREF(Py_None);
229 return Py_None;
230}
231
232
233static struct PyMethodDef rf_methods[] = {
Jack Jansendeefbe52001-08-08 13:46:49 +0000234 {"read", (PyCFunction)rf_read, 1, rf_read__doc__},
235 {"write", (PyCFunction)rf_write, 1, rf_write__doc__},
236 {"seek", (PyCFunction)rf_seek, 1, rf_seek__doc__},
237 {"tell", (PyCFunction)rf_tell, 1, rf_tell__doc__},
238 {"close", (PyCFunction)rf_close, 1, rf_close__doc__},
Jack Jansen76a05891996-02-29 16:11:32 +0000239
240 {NULL, NULL} /* sentinel */
241};
242
243/* ---------- */
244
245
246static rfobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000247newrfobject(void)
Jack Jansen76a05891996-02-29 16:11:32 +0000248{
249 rfobject *self;
250
251 self = PyObject_NEW(rfobject, &Rftype);
252 if (self == NULL)
253 return NULL;
254 self->isclosed = 1;
255 return self;
256}
257
258
259static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000260rf_dealloc(rfobject *self)
Jack Jansen76a05891996-02-29 16:11:32 +0000261{
262 do_close(self);
Jack Jansen0e2f7982002-05-22 14:31:48 +0000263 PyObject_DEL(self);
Jack Jansen76a05891996-02-29 16:11:32 +0000264}
265
266static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000267rf_getattr(rfobject *self, char *name)
Jack Jansen76a05891996-02-29 16:11:32 +0000268{
269 return Py_FindMethod(rf_methods, (PyObject *)self, name);
270}
271
272static char Rftype__doc__[] =
273"Resource fork file object"
274;
275
276static PyTypeObject Rftype = {
277 PyObject_HEAD_INIT(&PyType_Type)
278 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000279 "MacOS.ResourceFork", /*tp_name*/
Jack Jansen76a05891996-02-29 16:11:32 +0000280 sizeof(rfobject), /*tp_basicsize*/
281 0, /*tp_itemsize*/
282 /* methods */
283 (destructor)rf_dealloc, /*tp_dealloc*/
284 (printfunc)0, /*tp_print*/
285 (getattrfunc)rf_getattr, /*tp_getattr*/
286 (setattrfunc)0, /*tp_setattr*/
287 (cmpfunc)0, /*tp_compare*/
288 (reprfunc)0, /*tp_repr*/
289 0, /*tp_as_number*/
290 0, /*tp_as_sequence*/
291 0, /*tp_as_mapping*/
292 (hashfunc)0, /*tp_hash*/
293 (ternaryfunc)0, /*tp_call*/
294 (reprfunc)0, /*tp_str*/
295
296 /* Space for future expansion */
297 0L,0L,0L,0L,
298 Rftype__doc__ /* Documentation string */
299};
300
301/* End of code for Resource fork objects */
302/* -------------------------------------------------------- */
Guido van Rossum2d167031994-09-16 10:54:21 +0000303
304/*----------------------------------------------------------------------*/
Guido van Rossume791c2e1995-01-09 13:20:04 +0000305/* Miscellaneous File System Operations */
306
Jack Jansenfe94e972003-03-19 22:51:42 +0000307static char getcrtp_doc[] = "Get MacOS 4-char creator and type for a file";
Jack Jansen120a1051997-06-03 15:29:41 +0000308
Guido van Rossume791c2e1995-01-09 13:20:04 +0000309static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000310MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +0000311{
Jack Jansene79dc762000-06-02 21:35:07 +0000312 FSSpec fss;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000313 FInfo info;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000314 PyObject *creator, *type, *res;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000315 OSErr err;
316
Jack Jansene79dc762000-06-02 21:35:07 +0000317 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Guido van Rossume791c2e1995-01-09 13:20:04 +0000318 return NULL;
Jack Jansene79dc762000-06-02 21:35:07 +0000319 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000320 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000321 creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000322 type = PyString_FromStringAndSize((char *)&info.fdType, 4);
323 res = Py_BuildValue("OO", creator, type);
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000324 Py_DECREF(creator);
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000325 Py_DECREF(type);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000326 return res;
327}
328
Jack Jansenfe94e972003-03-19 22:51:42 +0000329static char setcrtp_doc[] = "Set MacOS 4-char creator and type for a file";
Jack Jansen120a1051997-06-03 15:29:41 +0000330
Guido van Rossume791c2e1995-01-09 13:20:04 +0000331static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000332MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +0000333{
Jack Jansene79dc762000-06-02 21:35:07 +0000334 FSSpec fss;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000335 ResType creator, type;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000336 FInfo info;
337 OSErr err;
338
339 if (!PyArg_ParseTuple(args, "O&O&O&",
Jack Jansene79dc762000-06-02 21:35:07 +0000340 PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Guido van Rossume791c2e1995-01-09 13:20:04 +0000341 return NULL;
Jack Jansene79dc762000-06-02 21:35:07 +0000342 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000343 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000344 info.fdCreator = creator;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000345 info.fdType = type;
Jack Jansene79dc762000-06-02 21:35:07 +0000346 if ((err = FSpSetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000347 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000348 Py_INCREF(Py_None);
349 return Py_None;
350}
351
Jack Jansenf3163302001-05-19 12:50:05 +0000352#if !TARGET_API_MAC_OSX
Jack Jansen120a1051997-06-03 15:29:41 +0000353static char schedparams_doc[] = "Set/return mainloop interrupt check flag, etc";
354
Jack Jansene8e8ae01995-01-26 16:36:45 +0000355/*
Jack Jansen120a1051997-06-03 15:29:41 +0000356** Set scheduler parameters
Jack Jansene8e8ae01995-01-26 16:36:45 +0000357*/
358static PyObject *
Jack Jansen120a1051997-06-03 15:29:41 +0000359MacOS_SchedParams(PyObject *self, PyObject *args)
Jack Jansene8e8ae01995-01-26 16:36:45 +0000360{
Jack Jansen120a1051997-06-03 15:29:41 +0000361 PyMacSchedParams old, new;
Jack Jansene8e8ae01995-01-26 16:36:45 +0000362
Jack Jansen120a1051997-06-03 15:29:41 +0000363 PyMac_GetSchedParams(&old);
364 new = old;
365 if (!PyArg_ParseTuple(args, "|iiidd", &new.check_interrupt, &new.process_events,
366 &new.besocial, &new.check_interval, &new.bg_yield))
Jack Jansene8e8ae01995-01-26 16:36:45 +0000367 return NULL;
Jack Jansen120a1051997-06-03 15:29:41 +0000368 PyMac_SetSchedParams(&new);
369 return Py_BuildValue("iiidd", old.check_interrupt, old.process_events,
370 old.besocial, old.check_interval, old.bg_yield);
Jack Jansene8e8ae01995-01-26 16:36:45 +0000371}
372
Jack Jansen120a1051997-06-03 15:29:41 +0000373static char appswitch_doc[] = "Obsolete, use SchedParams";
374
375/* Obsolete, for backward compatability */
Jack Jansenee23d6e1995-01-27 14:43:25 +0000376static PyObject *
377MacOS_EnableAppswitch(PyObject *self, PyObject *args)
378{
Jack Jansen120a1051997-06-03 15:29:41 +0000379 int new, old;
380 PyMacSchedParams schp;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000381
Guido van Rossume7134aa1995-02-26 10:20:53 +0000382 if (!PyArg_ParseTuple(args, "i", &new))
Jack Jansenee23d6e1995-01-27 14:43:25 +0000383 return NULL;
Jack Jansen120a1051997-06-03 15:29:41 +0000384 PyMac_GetSchedParams(&schp);
385 if ( schp.process_events )
386 old = 1;
387 else if ( schp.check_interrupt )
Jack Jansen120a1051997-06-03 15:29:41 +0000388 old = 0;
Jack Jansen2e871e41997-09-08 13:23:19 +0000389 else
390 old = -1;
Jack Jansen120a1051997-06-03 15:29:41 +0000391 if ( new > 0 ) {
392 schp.process_events = mDownMask|keyDownMask|osMask;
393 schp.check_interrupt = 1;
Jack Jansen2e871e41997-09-08 13:23:19 +0000394 } else if ( new == 0 ) {
Jack Jansen120a1051997-06-03 15:29:41 +0000395 schp.process_events = 0;
396 schp.check_interrupt = 1;
397 } else {
398 schp.process_events = 0;
399 schp.check_interrupt = 0;
400 }
401 PyMac_SetSchedParams(&schp);
Guido van Rossume7134aa1995-02-26 10:20:53 +0000402 return Py_BuildValue("i", old);
Jack Jansenee23d6e1995-01-27 14:43:25 +0000403}
404
Jack Jansen883765e1997-06-20 16:19:38 +0000405static char setevh_doc[] = "Set python event handler to be called in mainloop";
406
407static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000408MacOS_SetEventHandler(PyObject *self, PyObject *args)
Jack Jansen883765e1997-06-20 16:19:38 +0000409{
410 PyObject *evh = NULL;
411
412 if (!PyArg_ParseTuple(args, "|O", &evh))
413 return NULL;
414 if (evh == Py_None)
415 evh = NULL;
416 if ( evh && !PyCallable_Check(evh) ) {
417 PyErr_SetString(PyExc_ValueError, "SetEventHandler argument must be callable");
418 return NULL;
419 }
420 if ( !PyMac_SetEventHandler(evh) )
421 return NULL;
422 Py_INCREF(Py_None);
423 return Py_None;
424}
425
Jack Jansen120a1051997-06-03 15:29:41 +0000426static char handleev_doc[] = "Pass event to other interested parties like sioux";
Jack Jansena76382a1995-02-02 14:25:56 +0000427
428static PyObject *
429MacOS_HandleEvent(PyObject *self, PyObject *args)
430{
431 EventRecord ev;
432
433 if (!PyArg_ParseTuple(args, "O&", PyMac_GetEventRecord, &ev))
434 return NULL;
Jack Jansen883765e1997-06-20 16:19:38 +0000435 PyMac_HandleEventIntern(&ev);
Jack Jansena76382a1995-02-02 14:25:56 +0000436 Py_INCREF(Py_None);
437 return Py_None;
438}
Jack Jansenf3163302001-05-19 12:50:05 +0000439#endif /* !TARGET_API_MAC_OSX */
Jack Jansena76382a1995-02-02 14:25:56 +0000440
Jack Jansen120a1051997-06-03 15:29:41 +0000441static char geterr_doc[] = "Convert OSErr number to string";
442
Jack Jansen829f88c1995-07-17 11:36:01 +0000443static PyObject *
444MacOS_GetErrorString(PyObject *self, PyObject *args)
445{
446 int errn;
447
448 if (!PyArg_ParseTuple(args, "i", &errn))
449 return NULL;
450 return Py_BuildValue("s", PyMac_StrError(errn));
451}
452
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000453static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)";
454
455static PyObject *
456MacOS_splash(PyObject *self, PyObject *args)
457{
Jack Jansendf34cf11996-09-15 22:12:00 +0000458 int resid = -1;
Jack Jansen450ae9f1997-05-13 15:41:48 +0000459 static DialogPtr curdialog = NULL;
Jack Jansen2e871e41997-09-08 13:23:19 +0000460 DialogPtr olddialog;
Jack Jansen04df9d51996-09-23 15:49:43 +0000461 WindowRef theWindow;
462 CGrafPtr thePort;
Jack Jansene79dc762000-06-02 21:35:07 +0000463#if 0
Jack Jansen04df9d51996-09-23 15:49:43 +0000464 short xpos, ypos, width, height, swidth, sheight;
Jack Jansene79dc762000-06-02 21:35:07 +0000465#endif
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000466
Jack Jansendf34cf11996-09-15 22:12:00 +0000467 if (!PyArg_ParseTuple(args, "|i", &resid))
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000468 return NULL;
Jack Jansen2e871e41997-09-08 13:23:19 +0000469 olddialog = curdialog;
Jack Jansencbe6a531998-02-20 15:59:59 +0000470 curdialog = NULL;
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000471
Jack Jansendf34cf11996-09-15 22:12:00 +0000472 if ( resid != -1 ) {
473 curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
Jack Jansen04df9d51996-09-23 15:49:43 +0000474 if ( curdialog ) {
475 theWindow = GetDialogWindow(curdialog);
476 thePort = GetWindowPort(theWindow);
Jack Jansene79dc762000-06-02 21:35:07 +0000477#if 0
Jack Jansen04df9d51996-09-23 15:49:43 +0000478 width = thePort->portRect.right - thePort->portRect.left;
479 height = thePort->portRect.bottom - thePort->portRect.top;
480 swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
481 sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight();
482 xpos = (swidth-width)/2;
483 ypos = (sheight-height)/5 + LMGetMBarHeight();
484 MoveWindow(theWindow, xpos, ypos, 0);
485 ShowWindow(theWindow);
Jack Jansene79dc762000-06-02 21:35:07 +0000486#endif
Jack Jansendf34cf11996-09-15 22:12:00 +0000487 DrawDialog(curdialog);
Jack Jansen04df9d51996-09-23 15:49:43 +0000488 }
Jack Jansendf34cf11996-09-15 22:12:00 +0000489 }
Jack Jansen2e871e41997-09-08 13:23:19 +0000490 if (olddialog)
491 DisposeDialog(olddialog);
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000492 Py_INCREF(Py_None);
493 return Py_None;
494}
495
Jack Janseneb76b841996-09-30 14:43:22 +0000496static char DebugStr_doc[] = "Switch to low-level debugger with a message";
497
498static PyObject *
499MacOS_DebugStr(PyObject *self, PyObject *args)
500{
501 Str255 message;
502 PyObject *object = 0;
503
504 if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
505 return NULL;
506 DebugStr(message);
507 Py_INCREF(Py_None);
508 return Py_None;
509}
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000510
Jack Jansen2e871e41997-09-08 13:23:19 +0000511static char SysBeep_doc[] = "BEEEEEP!!!";
512
513static PyObject *
514MacOS_SysBeep(PyObject *self, PyObject *args)
515{
516 int duration = 6;
517
518 if (!PyArg_ParseTuple(args, "|i", &duration))
519 return NULL;
520 SysBeep(duration);
521 Py_INCREF(Py_None);
522 return Py_None;
523}
524
Jack Jansend7c17232003-02-21 16:31:11 +0000525static char WMAvailable_doc[] =
526 "True if this process can interact with the display."
527 "Will foreground the application on the first call as a side-effect."
528 ;
529
530static PyObject *
531MacOS_WMAvailable(PyObject *self, PyObject *args)
532{
533 static PyObject *rv = NULL;
534
535 if (!PyArg_ParseTuple(args, ""))
536 return NULL;
537 if (!rv) {
538#if TARGET_API_MAC_OSX
539 ProcessSerialNumber psn;
540
541 /*
542 ** This is a fairly innocuous call to make if we don't have a window
543 ** manager, or if we have no permission to talk to it. It will print
544 ** a message on stderr, but at least it won't abort the process.
545 ** It appears the function caches the result itself, and it's cheap, so
546 ** no need for us to cache.
547 */
548 if (CGMainDisplayID() == 0) {
549 rv = Py_False;
550 } else {
551 if (GetCurrentProcess(&psn) < 0 ||
552 SetFrontProcess(&psn) < 0) {
553 rv = Py_False;
554 } else {
555 rv = Py_True;
556 }
557 }
558#else
559 rv = Py_True;
560#endif
561 }
562 Py_INCREF(rv);
563 return rv;
564}
565
Jack Jansen898ac1b1997-09-01 15:38:12 +0000566static char GetTicks_doc[] = "Return number of ticks since bootup";
567
568static PyObject *
569MacOS_GetTicks(PyObject *self, PyObject *args)
570{
Jack Jansene79dc762000-06-02 21:35:07 +0000571 return Py_BuildValue("i", (int)TickCount());
Jack Jansen898ac1b1997-09-01 15:38:12 +0000572}
573
Jack Jansen76a05891996-02-29 16:11:32 +0000574static char openrf_doc[] = "Open resource fork of a file";
575
576static PyObject *
577MacOS_openrf(PyObject *self, PyObject *args)
578{
579 OSErr err;
580 char *mode = "r";
581 FSSpec fss;
582 SignedByte permission = 1;
583 rfobject *fp;
584
585 if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSSpec, &fss, &mode))
586 return NULL;
587 while (*mode) {
588 switch (*mode++) {
589 case '*': break;
590 case 'r': permission = 1; break;
591 case 'w': permission = 2; break;
592 case 'b': break;
593 default:
594 PyErr_BadArgument();
595 return NULL;
596 }
597 }
598
599 if ( (fp = newrfobject()) == NULL )
600 return NULL;
601
602 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
603
604 if ( err == fnfErr ) {
605 /* In stead of doing complicated things here to get creator/type
606 ** correct we let the standard i/o library handle it
607 */
608 FILE *tfp;
Jack Jansen697842f2001-09-10 22:00:39 +0000609 char pathname[PATHNAMELEN];
Jack Jansen76a05891996-02-29 16:11:32 +0000610
Jack Jansenb7348692002-12-23 23:16:25 +0000611 if ( (err=PyMac_GetFullPathname(&fss, pathname, PATHNAMELEN)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000612 PyMac_Error(err);
613 Py_DECREF(fp);
614 return NULL;
615 }
616
617 if ( (tfp = fopen(pathname, "w")) == NULL ) {
618 PyMac_Error(fnfErr); /* What else... */
619 Py_DECREF(fp);
620 return NULL;
621 }
622 fclose(tfp);
623 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
624 }
625 if ( err ) {
626 Py_DECREF(fp);
627 PyMac_Error(err);
628 return NULL;
629 }
630 fp->isclosed = 0;
631 return (PyObject *)fp;
632}
633
Jack Jansenf3163302001-05-19 12:50:05 +0000634#if !TARGET_API_MAC_OSX
Jack Jansen957d07a2000-02-21 11:07:37 +0000635static char FreeMem_doc[] = "Return the total amount of free space in the heap";
636
637static PyObject *
638MacOS_FreeMem(PyObject *self, PyObject *args)
639{
640 long rv;
641
642 if (!PyArg_ParseTuple(args, ""))
643 return NULL;
644 rv = FreeMem();
645 return Py_BuildValue("l", rv);
646}
647
648static char MaxBlock_doc[] = "Return the largest contiguous block of free space in the heap";
649
650static PyObject *
651MacOS_MaxBlock(PyObject *self, PyObject *args)
652{
653 long rv;
654
655 if (!PyArg_ParseTuple(args, ""))
656 return NULL;
657 rv = MaxBlock();
658 return Py_BuildValue("l", rv);
659}
660
661static char CompactMem_doc[] = "(wanted size)->actual largest block after compacting";
662
663static PyObject *
664MacOS_CompactMem(PyObject *self, PyObject *args)
665{
666 long value;
667 long rv;
668
669 if (!PyArg_ParseTuple(args, "l", &value))
670 return NULL;
671 rv = CompactMem(value);
672 return Py_BuildValue("l", rv);
673}
674
Jack Jansenb19c6672000-10-12 21:24:05 +0000675static char KeepConsole_doc[] = "(flag) Keep console open 0:never, 1:on output 2:on error, 3:always";
676
677static PyObject *
678MacOS_KeepConsole(PyObject *self, PyObject *args)
679{
680 int value;
681
682 if (!PyArg_ParseTuple(args, "i", &value))
683 return NULL;
684 PyMac_options.keep_console = value;
685 Py_INCREF(Py_None);
686 return Py_None;
687}
688
Jack Jansen8413b472000-10-19 22:02:16 +0000689static char OutputSeen_doc[] = "Call to reset the 'unseen output' flag for the keep-console-open option";
690
691static PyObject *
692MacOS_OutputSeen(PyObject *self, PyObject *args)
693{
694 if (!PyArg_ParseTuple(args, ""))
695 return NULL;
696 PyMac_OutputSeen();
697 Py_INCREF(Py_None);
698 return Py_None;
699}
Jack Jansenf3163302001-05-19 12:50:05 +0000700#endif /* !TARGET_API_MAC_OSX */
Jack Jansen8413b472000-10-19 22:02:16 +0000701
Guido van Rossum2d167031994-09-16 10:54:21 +0000702static PyMethodDef MacOS_Methods[] = {
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 Jansend7c17232003-02-21 16:31:11 +0000717 {"WMAvailable", MacOS_WMAvailable, 1, WMAvailable_doc},
Jack Jansenf3163302001-05-19 12:50:05 +0000718#if !TARGET_API_MAC_OSX
Jack Jansen957d07a2000-02-21 11:07:37 +0000719 {"FreeMem", MacOS_FreeMem, 1, FreeMem_doc},
720 {"MaxBlock", MacOS_MaxBlock, 1, MaxBlock_doc},
721 {"CompactMem", MacOS_CompactMem, 1, CompactMem_doc},
Jack Jansenb19c6672000-10-12 21:24:05 +0000722 {"KeepConsole", MacOS_KeepConsole, 1, KeepConsole_doc},
Jack Jansen8413b472000-10-19 22:02:16 +0000723 {"OutputSeen", MacOS_OutputSeen, 1, OutputSeen_doc},
Jack Jansenf3163302001-05-19 12:50:05 +0000724#endif
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000725 {NULL, NULL} /* Sentinel */
Guido van Rossum2d167031994-09-16 10:54:21 +0000726};
727
728
729void
Jack Jansendeefbe52001-08-08 13:46:49 +0000730initMacOS(void)
Guido van Rossum2d167031994-09-16 10:54:21 +0000731{
732 PyObject *m, *d;
733
734 m = Py_InitModule("MacOS", MacOS_Methods);
735 d = PyModule_GetDict(m);
736
737 /* Initialize MacOS.Error exception */
Guido van Rossumbf068b11995-01-25 23:09:20 +0000738 MacOS_Error = PyMac_GetOSErrException();
Guido van Rossume433c971994-09-29 10:02:56 +0000739 if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000740 return;
Jack Jansena755e681997-09-20 17:40:22 +0000741 Rftype.ob_type = &PyType_Type;
742 Py_INCREF(&Rftype);
743 if (PyDict_SetItemString(d, "ResourceForkType", (PyObject *)&Rftype) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000744 return;
Jack Jansenf73bab71997-04-03 14:51:03 +0000745 /*
746 ** This is a hack: the following constant added to the id() of a string
747 ** object gives you the address of the data. Unfortunately, it is needed for
748 ** some of the image and sound processing interfaces on the mac:-(
749 */
750 {
751 PyStringObject *p = 0;
752 long off = (long)&(p->ob_sval[0]);
753
754 if( PyDict_SetItemString(d, "string_id_to_buffer", Py_BuildValue("i", off)) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000755 return;
Jack Jansenf73bab71997-04-03 14:51:03 +0000756 }
Jack Jansenf3163302001-05-19 12:50:05 +0000757#if TARGET_API_MAC_OSX
758#define PY_RUNTIMEMODEL "macho"
Jack Jansenf3163302001-05-19 12:50:05 +0000759#elif TARGET_API_MAC_CARBON
760#define PY_RUNTIMEMODEL "carbon"
Jack Jansen6e68a7e2001-05-12 21:31:34 +0000761#else
762#error "None of the TARGET_API_MAC_XXX I know about is set"
Jack Jansen193509b2001-01-23 22:38:23 +0000763#endif
764 if (PyDict_SetItemString(d, "runtimemodel",
765 Py_BuildValue("s", PY_RUNTIMEMODEL)) != 0)
766 return;
Jack Jansen8cd9a4f2003-02-23 23:23:47 +0000767#if !TARGET_API_MAC_OSX
768#define PY_LINKMODEL "cfm"
769#elif defined(WITH_NEXT_FRAMEWORK)
770#define PY_LINKMODEL "framework"
771#elif defined(Py_ENABLE_SHARED)
772#define PY_LINKMODEL "shared"
773#else
774#define PY_LINKMODEL "static"
775#endif
776 if (PyDict_SetItemString(d, "linkmodel",
777 Py_BuildValue("s", PY_LINKMODEL)) != 0)
778 return;
779
Guido van Rossum2d167031994-09-16 10:54:21 +0000780}
Guido van Rossume7134aa1995-02-26 10:20:53 +0000781