1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Macintosh OS-specific interface */
32 static PyObject
*MacOS_Error
; /* Exception MacOS.Error */
35 #define bufferIsSmall -607 /*error returns from Post and Accept */
39 /*----------------------------------------------------------------------*/
40 /* Miscellaneous File System Operations */
43 MacOS_GetCreatorAndType(PyObject
*self
, PyObject
*args
)
47 PyObject
*creator
, *type
, *res
;
50 if (!PyArg_ParseTuple(args
, "O&", PyMac_GetStr255
, &name
))
52 if ((err
= GetFInfo(name
, 0, &info
)) != noErr
)
53 return PyErr_Mac(MacOS_Error
, err
);
54 creator
= PyString_FromStringAndSize((char *)&info
.fdCreator
, 4);
55 type
= PyString_FromStringAndSize((char *)&info
.fdType
, 4);
56 res
= Py_BuildValue("OO", creator
, type
);
63 MacOS_SetCreatorAndType(PyObject
*self
, PyObject
*args
)
66 ResType creator
, type
;
70 if (!PyArg_ParseTuple(args
, "O&O&O&",
71 PyMac_GetStr255
, &name
, PyMac_GetOSType
, &creator
, PyMac_GetOSType
, &type
))
73 if ((err
= GetFInfo(name
, 0, &info
)) != noErr
)
74 return PyErr_Mac(MacOS_Error
, err
);
75 info
.fdCreator
= creator
;
77 if ((err
= SetFInfo(name
, 0, &info
)) != noErr
)
78 return PyErr_Mac(MacOS_Error
, err
);
83 /*----------------------------------------------------------------------*/
84 /* STDWIN High Level Event interface */
91 extern void (*_w_high_level_event_proc
)(EventRecord
*);
93 static PyObject
*MacOS_HighLevelEventHandler
= NULL
;
96 MacOS_HighLevelEventProc(EventRecord
*e
)
98 if (MacOS_HighLevelEventHandler
!= NULL
) {
99 PyObject
*args
= PyMac_BuildEventRecord(e
);
104 res
= PyEval_CallObject(MacOS_HighLevelEventHandler
, args
);
108 fprintf(stderr
, "Exception in MacOS_HighLevelEventProc:\n");
116 /* XXXX Need to come here from PyMac_DoYield too... */
119 MacOS_SetHighLevelEventHandler(self
, args
)
123 PyObject
*previous
= MacOS_HighLevelEventHandler
;
124 PyObject
*next
= NULL
;
125 if (!PyArg_ParseTuple(args
, "|O", &next
))
130 MacOS_HighLevelEventHandler
= next
;
132 _w_high_level_event_proc
= NULL
;
134 _w_high_level_event_proc
= MacOS_HighLevelEventProc
;
135 if (previous
== NULL
) {
142 #endif /* USE_STDWIN */
145 MacOS_AcceptHighLevelEvent(self
, args
)
150 unsigned long refcon
;
158 err
= AcceptHighLevelEvent(&sender
, &refcon
, buf
, &len
);
159 if (err
== bufferIsSmall
) {
162 return PyErr_NoMemory();
163 err
= AcceptHighLevelEvent(&sender
, &refcon
, buf
, &len
);
166 return PyErr_Mac(MacOS_Error
, (int)err
);
169 else if (err
!= noErr
)
170 return PyErr_Mac(MacOS_Error
, (int)err
);
171 res
= Py_BuildValue("s#ls#",
172 (char *)&sender
, (int)(sizeof sender
), refcon
, (char *)buf
, (int)len
);
178 ** Set poll frequency and cpu-yield-time
181 MacOS_SetScheduleTimes(PyObject
*self
, PyObject
*args
)
183 long fgi
, fgy
, bgi
, bgy
;
186 if (!PyArg_ParseTuple(args
, "ll|ll", &fgi
, &fgy
, &bgi
, &bgy
))
188 if ( bgi
== -2 || bgy
== -2 ) {
192 PyMac_SetYield(fgi
, fgy
, bgi
, bgy
);
198 MacOS_EnableAppswitch(PyObject
*self
, PyObject
*args
)
202 if (!PyArg_ParseTuple(args
, "i", &new))
204 old
= PyMac_DoYieldEnabled
;
205 PyMac_DoYieldEnabled
= new;
206 return Py_BuildValue("i", old
);
211 MacOS_HandleEvent(PyObject
*self
, PyObject
*args
)
215 if (!PyArg_ParseTuple(args
, "O&", PyMac_GetEventRecord
, &ev
))
217 PyMac_HandleEvent(&ev
);
223 MacOS_GetErrorString(PyObject
*self
, PyObject
*args
)
227 if (!PyArg_ParseTuple(args
, "i", &errn
))
229 return Py_BuildValue("s", PyMac_StrError(errn
));
232 static PyMethodDef MacOS_Methods
[] = {
233 {"AcceptHighLevelEvent", MacOS_AcceptHighLevelEvent
, 1},
234 {"GetCreatorAndType", MacOS_GetCreatorAndType
, 1},
235 {"SetCreatorAndType", MacOS_SetCreatorAndType
, 1},
237 {"SetHighLevelEventHandler", MacOS_SetHighLevelEventHandler
, 1},
239 {"SetScheduleTimes", MacOS_SetScheduleTimes
, 1},
240 {"EnableAppswitch", MacOS_EnableAppswitch
, 1},
241 {"HandleEvent", MacOS_HandleEvent
, 1},
242 {"GetErrorString", MacOS_GetErrorString
, 1},
243 {NULL
, NULL
} /* Sentinel */
252 m
= Py_InitModule("MacOS", MacOS_Methods
);
253 d
= PyModule_GetDict(m
);
255 /* Initialize MacOS.Error exception */
256 MacOS_Error
= PyMac_GetOSErrException();
257 if (MacOS_Error
== NULL
|| PyDict_SetItemString(d
, "Error", MacOS_Error
) != 0)
258 Py_FatalError("can't define MacOS.Error");