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