blob: f5fc23e6dbc4a9ae7267be890c9518c73ddd51e7 [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#include <Carbon/Carbon.h>
Jack Jansend7c17232003-02-21 16:31:11 +000032#include <ApplicationServices/ApplicationServices.h>
Jack Jansenee23d6e1995-01-27 14:43:25 +000033
Guido van Rossum2d167031994-09-16 10:54:21 +000034static PyObject *MacOS_Error; /* Exception MacOS.Error */
35
Jack Jansen697842f2001-09-10 22:00:39 +000036#define PATHNAMELEN 1024
Jack Jansen697842f2001-09-10 22:00:39 +000037
Jack Jansen76a05891996-02-29 16:11:32 +000038/* ----------------------------------------------------- */
39
40/* Declarations for objects of type Resource fork */
41
42typedef struct {
43 PyObject_HEAD
44 short fRefNum;
45 int isclosed;
46} rfobject;
47
Jeremy Hylton938ace62002-07-17 16:30:39 +000048static PyTypeObject Rftype;
Jack Jansen76a05891996-02-29 16:11:32 +000049
50
51
52/* ---------------------------------------------------------------- */
53
54static void
Jack Jansendeefbe52001-08-08 13:46:49 +000055do_close(rfobject *self)
Jack Jansen76a05891996-02-29 16:11:32 +000056{
57 if (self->isclosed ) return;
58 (void)FSClose(self->fRefNum);
59 self->isclosed = 1;
60}
61
62static char rf_read__doc__[] =
63"Read data from resource fork"
64;
65
66static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +000067rf_read(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +000068{
69 long n;
70 PyObject *v;
71 OSErr err;
72
73 if (self->isclosed) {
74 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
75 return NULL;
76 }
77
78 if (!PyArg_ParseTuple(args, "l", &n))
79 return NULL;
80
81 v = PyString_FromStringAndSize((char *)NULL, n);
82 if (v == NULL)
83 return NULL;
84
85 err = FSRead(self->fRefNum, &n, PyString_AsString(v));
86 if (err && err != eofErr) {
87 PyMac_Error(err);
88 Py_DECREF(v);
89 return NULL;
90 }
91 _PyString_Resize(&v, n);
92 return v;
93}
94
95
96static char rf_write__doc__[] =
97"Write to resource fork"
98;
99
100static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000101rf_write(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000102{
103 char *buffer;
104 long size;
105 OSErr err;
106
107 if (self->isclosed) {
108 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
109 return NULL;
110 }
Jack Janseneeccca91997-05-07 15:46:31 +0000111 if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
Jack Jansen76a05891996-02-29 16:11:32 +0000112 return NULL;
113 err = FSWrite(self->fRefNum, &size, buffer);
114 if (err) {
115 PyMac_Error(err);
116 return NULL;
117 }
118 Py_INCREF(Py_None);
119 return Py_None;
120}
121
122
123static char rf_seek__doc__[] =
124"Set file position"
125;
126
127static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000128rf_seek(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000129{
130 long amount, pos;
131 int whence = SEEK_SET;
132 long eof;
133 OSErr err;
134
135 if (self->isclosed) {
136 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
137 return NULL;
138 }
139 if (!PyArg_ParseTuple(args, "l|i", &amount, &whence))
140 return NULL;
141
Jack Jansendeefbe52001-08-08 13:46:49 +0000142 if ((err = GetEOF(self->fRefNum, &eof)))
Jack Jansen76a05891996-02-29 16:11:32 +0000143 goto ioerr;
144
145 switch (whence) {
146 case SEEK_CUR:
Jack Jansendeefbe52001-08-08 13:46:49 +0000147 if ((err = GetFPos(self->fRefNum, &pos)))
Jack Jansen76a05891996-02-29 16:11:32 +0000148 goto ioerr;
149 break;
150 case SEEK_END:
151 pos = eof;
152 break;
153 case SEEK_SET:
154 pos = 0;
155 break;
156 default:
157 PyErr_BadArgument();
158 return NULL;
159 }
160
161 pos += amount;
162
163 /* Don't bother implementing seek past EOF */
164 if (pos > eof || pos < 0) {
165 PyErr_BadArgument();
166 return NULL;
167 }
168
Jack Jansendeefbe52001-08-08 13:46:49 +0000169 if ((err = SetFPos(self->fRefNum, fsFromStart, pos)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000170ioerr:
171 PyMac_Error(err);
172 return NULL;
173 }
174 Py_INCREF(Py_None);
175 return Py_None;
176}
177
178
179static char rf_tell__doc__[] =
180"Get file position"
181;
182
183static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000184rf_tell(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000185{
186 long where;
187 OSErr err;
188
189 if (self->isclosed) {
190 PyErr_SetString(PyExc_ValueError, "Operation on closed file");
191 return NULL;
192 }
193 if (!PyArg_ParseTuple(args, ""))
194 return NULL;
Jack Jansendeefbe52001-08-08 13:46:49 +0000195 if ((err = GetFPos(self->fRefNum, &where)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000196 PyMac_Error(err);
197 return NULL;
198 }
199 return PyInt_FromLong(where);
200}
201
202static char rf_close__doc__[] =
203"Close resource fork"
204;
205
206static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000207rf_close(rfobject *self, PyObject *args)
Jack Jansen76a05891996-02-29 16:11:32 +0000208{
Jack Jansen76a05891996-02-29 16:11:32 +0000209 if (!PyArg_ParseTuple(args, ""))
210 return NULL;
211 do_close(self);
212 Py_INCREF(Py_None);
213 return Py_None;
214}
215
216
217static struct PyMethodDef rf_methods[] = {
Jack Jansendeefbe52001-08-08 13:46:49 +0000218 {"read", (PyCFunction)rf_read, 1, rf_read__doc__},
219 {"write", (PyCFunction)rf_write, 1, rf_write__doc__},
220 {"seek", (PyCFunction)rf_seek, 1, rf_seek__doc__},
221 {"tell", (PyCFunction)rf_tell, 1, rf_tell__doc__},
222 {"close", (PyCFunction)rf_close, 1, rf_close__doc__},
Jack Jansen76a05891996-02-29 16:11:32 +0000223
224 {NULL, NULL} /* sentinel */
225};
226
227/* ---------- */
228
229
230static rfobject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000231newrfobject(void)
Jack Jansen76a05891996-02-29 16:11:32 +0000232{
233 rfobject *self;
234
235 self = PyObject_NEW(rfobject, &Rftype);
236 if (self == NULL)
237 return NULL;
238 self->isclosed = 1;
239 return self;
240}
241
242
243static void
Jack Jansendeefbe52001-08-08 13:46:49 +0000244rf_dealloc(rfobject *self)
Jack Jansen76a05891996-02-29 16:11:32 +0000245{
246 do_close(self);
Jack Jansen0e2f7982002-05-22 14:31:48 +0000247 PyObject_DEL(self);
Jack Jansen76a05891996-02-29 16:11:32 +0000248}
249
250static PyObject *
Jack Jansendeefbe52001-08-08 13:46:49 +0000251rf_getattr(rfobject *self, char *name)
Jack Jansen76a05891996-02-29 16:11:32 +0000252{
253 return Py_FindMethod(rf_methods, (PyObject *)self, name);
254}
255
256static char Rftype__doc__[] =
257"Resource fork file object"
258;
259
260static PyTypeObject Rftype = {
261 PyObject_HEAD_INIT(&PyType_Type)
262 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000263 "MacOS.ResourceFork", /*tp_name*/
Jack Jansen76a05891996-02-29 16:11:32 +0000264 sizeof(rfobject), /*tp_basicsize*/
265 0, /*tp_itemsize*/
266 /* methods */
267 (destructor)rf_dealloc, /*tp_dealloc*/
268 (printfunc)0, /*tp_print*/
269 (getattrfunc)rf_getattr, /*tp_getattr*/
270 (setattrfunc)0, /*tp_setattr*/
271 (cmpfunc)0, /*tp_compare*/
272 (reprfunc)0, /*tp_repr*/
273 0, /*tp_as_number*/
274 0, /*tp_as_sequence*/
275 0, /*tp_as_mapping*/
276 (hashfunc)0, /*tp_hash*/
277 (ternaryfunc)0, /*tp_call*/
278 (reprfunc)0, /*tp_str*/
279
280 /* Space for future expansion */
281 0L,0L,0L,0L,
282 Rftype__doc__ /* Documentation string */
283};
284
285/* End of code for Resource fork objects */
286/* -------------------------------------------------------- */
Guido van Rossum2d167031994-09-16 10:54:21 +0000287
288/*----------------------------------------------------------------------*/
Guido van Rossume791c2e1995-01-09 13:20:04 +0000289/* Miscellaneous File System Operations */
290
Jack Jansenfe94e972003-03-19 22:51:42 +0000291static char getcrtp_doc[] = "Get MacOS 4-char creator and type for a file";
Jack Jansen120a1051997-06-03 15:29:41 +0000292
Guido van Rossume791c2e1995-01-09 13:20:04 +0000293static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000294MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +0000295{
Jack Jansene79dc762000-06-02 21:35:07 +0000296 FSSpec fss;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000297 FInfo info;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000298 PyObject *creator, *type, *res;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000299 OSErr err;
300
Jack Jansene79dc762000-06-02 21:35:07 +0000301 if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss))
Guido van Rossume791c2e1995-01-09 13:20:04 +0000302 return NULL;
Jack Jansene79dc762000-06-02 21:35:07 +0000303 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000304 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000305 creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000306 type = PyString_FromStringAndSize((char *)&info.fdType, 4);
307 res = Py_BuildValue("OO", creator, type);
Guido van Rossumfffb8bb1995-01-12 12:37:24 +0000308 Py_DECREF(creator);
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000309 Py_DECREF(type);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000310 return res;
311}
312
Jack Jansenfe94e972003-03-19 22:51:42 +0000313static char setcrtp_doc[] = "Set MacOS 4-char creator and type for a file";
Jack Jansen120a1051997-06-03 15:29:41 +0000314
Guido van Rossume791c2e1995-01-09 13:20:04 +0000315static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000316MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +0000317{
Jack Jansene79dc762000-06-02 21:35:07 +0000318 FSSpec fss;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000319 ResType creator, type;
Guido van Rossume791c2e1995-01-09 13:20:04 +0000320 FInfo info;
321 OSErr err;
322
323 if (!PyArg_ParseTuple(args, "O&O&O&",
Jack Jansene79dc762000-06-02 21:35:07 +0000324 PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Guido van Rossume791c2e1995-01-09 13:20:04 +0000325 return NULL;
Jack Jansene79dc762000-06-02 21:35:07 +0000326 if ((err = FSpGetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000327 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000328 info.fdCreator = creator;
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000329 info.fdType = type;
Jack Jansene79dc762000-06-02 21:35:07 +0000330 if ((err = FSpSetFInfo(&fss, &info)) != noErr)
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000331 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +0000332 Py_INCREF(Py_None);
333 return Py_None;
334}
335
Jack Jansena76382a1995-02-02 14:25:56 +0000336
Jack Jansen120a1051997-06-03 15:29:41 +0000337static char geterr_doc[] = "Convert OSErr number to string";
338
Jack Jansen829f88c1995-07-17 11:36:01 +0000339static PyObject *
340MacOS_GetErrorString(PyObject *self, PyObject *args)
341{
342 int errn;
343
344 if (!PyArg_ParseTuple(args, "i", &errn))
345 return NULL;
346 return Py_BuildValue("s", PyMac_StrError(errn));
347}
348
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000349static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)";
350
351static PyObject *
352MacOS_splash(PyObject *self, PyObject *args)
353{
Jack Jansendf34cf11996-09-15 22:12:00 +0000354 int resid = -1;
Jack Jansen450ae9f1997-05-13 15:41:48 +0000355 static DialogPtr curdialog = NULL;
Jack Jansen2e871e41997-09-08 13:23:19 +0000356 DialogPtr olddialog;
Jack Jansen04df9d51996-09-23 15:49:43 +0000357 WindowRef theWindow;
358 CGrafPtr thePort;
Jack Jansene79dc762000-06-02 21:35:07 +0000359#if 0
Jack Jansen04df9d51996-09-23 15:49:43 +0000360 short xpos, ypos, width, height, swidth, sheight;
Jack Jansene79dc762000-06-02 21:35:07 +0000361#endif
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000362
Jack Jansendf34cf11996-09-15 22:12:00 +0000363 if (!PyArg_ParseTuple(args, "|i", &resid))
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000364 return NULL;
Jack Jansen2e871e41997-09-08 13:23:19 +0000365 olddialog = curdialog;
Jack Jansencbe6a531998-02-20 15:59:59 +0000366 curdialog = NULL;
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000367
Jack Jansendf34cf11996-09-15 22:12:00 +0000368 if ( resid != -1 ) {
369 curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1);
Jack Jansen04df9d51996-09-23 15:49:43 +0000370 if ( curdialog ) {
371 theWindow = GetDialogWindow(curdialog);
372 thePort = GetWindowPort(theWindow);
Jack Jansene79dc762000-06-02 21:35:07 +0000373#if 0
Jack Jansen04df9d51996-09-23 15:49:43 +0000374 width = thePort->portRect.right - thePort->portRect.left;
375 height = thePort->portRect.bottom - thePort->portRect.top;
376 swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left;
377 sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight();
378 xpos = (swidth-width)/2;
379 ypos = (sheight-height)/5 + LMGetMBarHeight();
380 MoveWindow(theWindow, xpos, ypos, 0);
381 ShowWindow(theWindow);
Jack Jansene79dc762000-06-02 21:35:07 +0000382#endif
Jack Jansendf34cf11996-09-15 22:12:00 +0000383 DrawDialog(curdialog);
Jack Jansen04df9d51996-09-23 15:49:43 +0000384 }
Jack Jansendf34cf11996-09-15 22:12:00 +0000385 }
Jack Jansen2e871e41997-09-08 13:23:19 +0000386 if (olddialog)
387 DisposeDialog(olddialog);
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000388 Py_INCREF(Py_None);
389 return Py_None;
390}
391
Jack Janseneb76b841996-09-30 14:43:22 +0000392static char DebugStr_doc[] = "Switch to low-level debugger with a message";
393
394static PyObject *
395MacOS_DebugStr(PyObject *self, PyObject *args)
396{
397 Str255 message;
398 PyObject *object = 0;
399
400 if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object))
401 return NULL;
402 DebugStr(message);
403 Py_INCREF(Py_None);
404 return Py_None;
405}
Jack Jansenab7fcdd1996-05-20 11:32:00 +0000406
Jack Jansen2e871e41997-09-08 13:23:19 +0000407static char SysBeep_doc[] = "BEEEEEP!!!";
408
409static PyObject *
410MacOS_SysBeep(PyObject *self, PyObject *args)
411{
412 int duration = 6;
413
414 if (!PyArg_ParseTuple(args, "|i", &duration))
415 return NULL;
416 SysBeep(duration);
417 Py_INCREF(Py_None);
418 return Py_None;
419}
420
Jack Jansend7c17232003-02-21 16:31:11 +0000421static char WMAvailable_doc[] =
422 "True if this process can interact with the display."
423 "Will foreground the application on the first call as a side-effect."
424 ;
425
426static PyObject *
427MacOS_WMAvailable(PyObject *self, PyObject *args)
428{
429 static PyObject *rv = NULL;
430
431 if (!PyArg_ParseTuple(args, ""))
432 return NULL;
433 if (!rv) {
Jack Jansend7c17232003-02-21 16:31:11 +0000434 ProcessSerialNumber psn;
435
436 /*
437 ** This is a fairly innocuous call to make if we don't have a window
438 ** manager, or if we have no permission to talk to it. It will print
439 ** a message on stderr, but at least it won't abort the process.
440 ** It appears the function caches the result itself, and it's cheap, so
441 ** no need for us to cache.
442 */
443 if (CGMainDisplayID() == 0) {
444 rv = Py_False;
445 } else {
446 if (GetCurrentProcess(&psn) < 0 ||
447 SetFrontProcess(&psn) < 0) {
448 rv = Py_False;
449 } else {
450 rv = Py_True;
451 }
452 }
Jack Jansend7c17232003-02-21 16:31:11 +0000453 }
454 Py_INCREF(rv);
455 return rv;
456}
457
Jack Jansen898ac1b1997-09-01 15:38:12 +0000458static char GetTicks_doc[] = "Return number of ticks since bootup";
459
460static PyObject *
461MacOS_GetTicks(PyObject *self, PyObject *args)
462{
Jack Jansene79dc762000-06-02 21:35:07 +0000463 return Py_BuildValue("i", (int)TickCount());
Jack Jansen898ac1b1997-09-01 15:38:12 +0000464}
465
Jack Jansen76a05891996-02-29 16:11:32 +0000466static char openrf_doc[] = "Open resource fork of a file";
467
468static PyObject *
469MacOS_openrf(PyObject *self, PyObject *args)
470{
471 OSErr err;
472 char *mode = "r";
473 FSSpec fss;
474 SignedByte permission = 1;
475 rfobject *fp;
476
477 if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSSpec, &fss, &mode))
478 return NULL;
479 while (*mode) {
480 switch (*mode++) {
481 case '*': break;
482 case 'r': permission = 1; break;
483 case 'w': permission = 2; break;
484 case 'b': break;
485 default:
486 PyErr_BadArgument();
487 return NULL;
488 }
489 }
490
491 if ( (fp = newrfobject()) == NULL )
492 return NULL;
493
494 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
495
496 if ( err == fnfErr ) {
497 /* In stead of doing complicated things here to get creator/type
498 ** correct we let the standard i/o library handle it
499 */
500 FILE *tfp;
Jack Jansen697842f2001-09-10 22:00:39 +0000501 char pathname[PATHNAMELEN];
Jack Jansen76a05891996-02-29 16:11:32 +0000502
Jack Jansenb7348692002-12-23 23:16:25 +0000503 if ( (err=PyMac_GetFullPathname(&fss, pathname, PATHNAMELEN)) ) {
Jack Jansen76a05891996-02-29 16:11:32 +0000504 PyMac_Error(err);
505 Py_DECREF(fp);
506 return NULL;
507 }
508
509 if ( (tfp = fopen(pathname, "w")) == NULL ) {
510 PyMac_Error(fnfErr); /* What else... */
511 Py_DECREF(fp);
512 return NULL;
513 }
514 fclose(tfp);
515 err = HOpenRF(fss.vRefNum, fss.parID, fss.name, permission, &fp->fRefNum);
516 }
517 if ( err ) {
518 Py_DECREF(fp);
519 PyMac_Error(err);
520 return NULL;
521 }
522 fp->isclosed = 0;
523 return (PyObject *)fp;
524}
525
Jack Jansen8413b472000-10-19 22:02:16 +0000526
Guido van Rossum2d167031994-09-16 10:54:21 +0000527static PyMethodDef MacOS_Methods[] = {
Jack Jansen120a1051997-06-03 15:29:41 +0000528 {"GetCreatorAndType", MacOS_GetCreatorAndType, 1, getcrtp_doc},
529 {"SetCreatorAndType", MacOS_SetCreatorAndType, 1, setcrtp_doc},
Jack Jansen120a1051997-06-03 15:29:41 +0000530 {"GetErrorString", MacOS_GetErrorString, 1, geterr_doc},
531 {"openrf", MacOS_openrf, 1, openrf_doc},
532 {"splash", MacOS_splash, 1, splash_doc},
533 {"DebugStr", MacOS_DebugStr, 1, DebugStr_doc},
Jack Jansen898ac1b1997-09-01 15:38:12 +0000534 {"GetTicks", MacOS_GetTicks, 1, GetTicks_doc},
Jack Jansen2e871e41997-09-08 13:23:19 +0000535 {"SysBeep", MacOS_SysBeep, 1, SysBeep_doc},
Jack Jansend7c17232003-02-21 16:31:11 +0000536 {"WMAvailable", MacOS_WMAvailable, 1, WMAvailable_doc},
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000537 {NULL, NULL} /* Sentinel */
Guido van Rossum2d167031994-09-16 10:54:21 +0000538};
539
540
541void
Jack Jansendeefbe52001-08-08 13:46:49 +0000542initMacOS(void)
Guido van Rossum2d167031994-09-16 10:54:21 +0000543{
544 PyObject *m, *d;
545
546 m = Py_InitModule("MacOS", MacOS_Methods);
547 d = PyModule_GetDict(m);
548
549 /* Initialize MacOS.Error exception */
Guido van Rossumbf068b11995-01-25 23:09:20 +0000550 MacOS_Error = PyMac_GetOSErrException();
Guido van Rossume433c971994-09-29 10:02:56 +0000551 if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000552 return;
Jack Jansena755e681997-09-20 17:40:22 +0000553 Rftype.ob_type = &PyType_Type;
554 Py_INCREF(&Rftype);
555 if (PyDict_SetItemString(d, "ResourceForkType", (PyObject *)&Rftype) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000556 return;
Jack Jansenf73bab71997-04-03 14:51:03 +0000557 /*
558 ** This is a hack: the following constant added to the id() of a string
559 ** object gives you the address of the data. Unfortunately, it is needed for
560 ** some of the image and sound processing interfaces on the mac:-(
561 */
562 {
563 PyStringObject *p = 0;
564 long off = (long)&(p->ob_sval[0]);
565
566 if( PyDict_SetItemString(d, "string_id_to_buffer", Py_BuildValue("i", off)) != 0)
Jack Jansenfa1e27d2000-09-08 10:21:44 +0000567 return;
Jack Jansenf73bab71997-04-03 14:51:03 +0000568 }
Jack Jansenf3163302001-05-19 12:50:05 +0000569#define PY_RUNTIMEMODEL "macho"
Jack Jansen193509b2001-01-23 22:38:23 +0000570 if (PyDict_SetItemString(d, "runtimemodel",
571 Py_BuildValue("s", PY_RUNTIMEMODEL)) != 0)
572 return;
Jack Jansena53f4eb2003-11-19 16:34:04 +0000573#if defined(WITH_NEXT_FRAMEWORK)
Jack Jansen8cd9a4f2003-02-23 23:23:47 +0000574#define PY_LINKMODEL "framework"
575#elif defined(Py_ENABLE_SHARED)
576#define PY_LINKMODEL "shared"
577#else
578#define PY_LINKMODEL "static"
579#endif
580 if (PyDict_SetItemString(d, "linkmodel",
581 Py_BuildValue("s", PY_LINKMODEL)) != 0)
582 return;
583
Guido van Rossum2d167031994-09-16 10:54:21 +0000584}