blob: c5f14d6202c0a8558a7dfc0c5b15be8f62e837c7 [file] [log] [blame]
Guido van Rossum2d167031994-09-16 10:54:21 +00001/***********************************************************
Guido van Rossum99546991995-01-08 14:33:34 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The 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"
Guido van Rossum2d167031994-09-16 10:54:21 +000029
Jack Jansenee23d6e1995-01-27 14:43:25 +000030#include <Windows.h>
31
Guido van Rossum2d167031994-09-16 10:54:21 +000032static PyObject *MacOS_Error; /* Exception MacOS.Error */
33
Guido van Rossume6d9ccc1995-02-21 21:01:05 +000034#ifdef MPW
Guido van Rossum9fed1831995-02-18 15:02:02 +000035#define bufferIsSmall -607 /*error returns from Post and Accept */
36#endif
37
Guido van Rossum2d167031994-09-16 10:54:21 +000038
39/*----------------------------------------------------------------------*/
Guido van Rossume791c2e1995-01-09 13:20:04 +000040/* Miscellaneous File System Operations */
41
42static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +000043MacOS_GetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +000044{
45 Str255 name;
46 FInfo info;
Guido van Rossumb7e79e51995-01-22 18:42:12 +000047 PyObject *creator, *type, *res;
Guido van Rossume791c2e1995-01-09 13:20:04 +000048 OSErr err;
49
Guido van Rossum9aa3d131995-01-21 13:46:04 +000050 if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, &name))
Guido van Rossume791c2e1995-01-09 13:20:04 +000051 return NULL;
Guido van Rossumb7e79e51995-01-22 18:42:12 +000052 if ((err = GetFInfo(name, 0, &info)) != noErr)
53 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +000054 creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4);
Guido van Rossumb7e79e51995-01-22 18:42:12 +000055 type = PyString_FromStringAndSize((char *)&info.fdType, 4);
56 res = Py_BuildValue("OO", creator, type);
Guido van Rossumfffb8bb1995-01-12 12:37:24 +000057 Py_DECREF(creator);
Guido van Rossumb7e79e51995-01-22 18:42:12 +000058 Py_DECREF(type);
Guido van Rossume791c2e1995-01-09 13:20:04 +000059 return res;
60}
61
62static PyObject *
Guido van Rossumb7e79e51995-01-22 18:42:12 +000063MacOS_SetCreatorAndType(PyObject *self, PyObject *args)
Guido van Rossume791c2e1995-01-09 13:20:04 +000064{
65 Str255 name;
Guido van Rossumb7e79e51995-01-22 18:42:12 +000066 ResType creator, type;
Guido van Rossume791c2e1995-01-09 13:20:04 +000067 FInfo info;
68 OSErr err;
69
70 if (!PyArg_ParseTuple(args, "O&O&O&",
Guido van Rossumb7e79e51995-01-22 18:42:12 +000071 PyMac_GetStr255, &name, PyMac_GetOSType, &creator, PyMac_GetOSType, &type))
Guido van Rossume791c2e1995-01-09 13:20:04 +000072 return NULL;
Guido van Rossumb7e79e51995-01-22 18:42:12 +000073 if ((err = GetFInfo(name, 0, &info)) != noErr)
74 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +000075 info.fdCreator = creator;
Guido van Rossumb7e79e51995-01-22 18:42:12 +000076 info.fdType = type;
77 if ((err = SetFInfo(name, 0, &info)) != noErr)
78 return PyErr_Mac(MacOS_Error, err);
Guido van Rossume791c2e1995-01-09 13:20:04 +000079 Py_INCREF(Py_None);
80 return Py_None;
81}
82
83/*----------------------------------------------------------------------*/
Guido van Rossumf74d4e21995-01-18 23:58:07 +000084/* STDWIN High Level Event interface */
85
86#include <EPPC.h>
87#include <Events.h>
88
89#ifdef USE_STDWIN
90
91extern void (*_w_high_level_event_proc)(EventRecord *);
92
93static PyObject *MacOS_HighLevelEventHandler = NULL;
94
95static void
Guido van Rossumbf068b11995-01-25 23:09:20 +000096MacOS_HighLevelEventProc(EventRecord *e)
Guido van Rossumf74d4e21995-01-18 23:58:07 +000097{
98 if (MacOS_HighLevelEventHandler != NULL) {
Guido van Rossumbf068b11995-01-25 23:09:20 +000099 PyObject *args = PyMac_BuildEventRecord(e);
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000100 PyObject *res;
101 if (args == NULL)
102 res = NULL;
103 else {
104 res = PyEval_CallObject(MacOS_HighLevelEventHandler, args);
105 Py_DECREF(args);
106 }
107 if (res == NULL) {
108 fprintf(stderr, "Exception in MacOS_HighLevelEventProc:\n");
109 PyErr_Print();
110 }
111 else
112 Py_DECREF(res);
113 }
114}
115
Jack Jansene8e8ae01995-01-26 16:36:45 +0000116/* XXXX Need to come here from PyMac_DoYield too... */
117
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000118static PyObject *
119MacOS_SetHighLevelEventHandler(self, args)
120 PyObject *self;
121 PyObject *args;
122{
123 PyObject *previous = MacOS_HighLevelEventHandler;
124 PyObject *next = NULL;
125 if (!PyArg_ParseTuple(args, "|O", &next))
126 return NULL;
127 if (next == Py_None)
128 next = NULL;
129 Py_INCREF(next);
130 MacOS_HighLevelEventHandler = next;
131 if (next == NULL)
132 _w_high_level_event_proc = NULL;
133 else
134 _w_high_level_event_proc = MacOS_HighLevelEventProc;
135 if (previous == NULL) {
136 Py_INCREF(Py_None);
137 previous = Py_None;
138 }
139 return previous;
140}
141
142#endif /* USE_STDWIN */
143
144static PyObject *
145MacOS_AcceptHighLevelEvent(self, args)
146 PyObject *self;
147 PyObject *args;
148{
149 TargetID sender;
150 unsigned long refcon;
151 Ptr buf;
152 unsigned long len;
153 OSErr err;
154 PyObject *res;
155
156 buf = NULL;
157 len = 0;
158 err = AcceptHighLevelEvent(&sender, &refcon, buf, &len);
159 if (err == bufferIsSmall) {
160 buf = malloc(len);
161 if (buf == NULL)
162 return PyErr_NoMemory();
163 err = AcceptHighLevelEvent(&sender, &refcon, buf, &len);
164 if (err != noErr) {
165 free(buf);
166 return PyErr_Mac(MacOS_Error, (int)err);
167 }
168 }
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);
173 free(buf);
174 return res;
175}
176
Jack Jansene8e8ae01995-01-26 16:36:45 +0000177/*
178** Set poll frequency and cpu-yield-time
179*/
180static PyObject *
181MacOS_SetScheduleTimes(PyObject *self, PyObject *args)
182{
183 long fgi, fgy, bgi, bgy;
184
185 bgi = bgy = -2;
186 if (!PyArg_ParseTuple(args, "ll|ll", &fgi, &fgy, &bgi, &bgy))
187 return NULL;
Jack Jansenb2f6a7e1995-02-20 15:46:10 +0000188 if ( bgi == -2 || bgy == -2 ) {
Jack Jansene8e8ae01995-01-26 16:36:45 +0000189 bgi = fgi;
190 bgy = fgy;
191 }
192 PyMac_SetYield(fgi, fgy, bgi, bgy);
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196
Jack Jansenee23d6e1995-01-27 14:43:25 +0000197static PyObject *
198MacOS_EnableAppswitch(PyObject *self, PyObject *args)
199{
Guido van Rossume7134aa1995-02-26 10:20:53 +0000200 int old, new;
Jack Jansenee23d6e1995-01-27 14:43:25 +0000201
Guido van Rossume7134aa1995-02-26 10:20:53 +0000202 if (!PyArg_ParseTuple(args, "i", &new))
Jack Jansenee23d6e1995-01-27 14:43:25 +0000203 return NULL;
Guido van Rossume7134aa1995-02-26 10:20:53 +0000204 old = PyMac_DoYieldEnabled;
205 PyMac_DoYieldEnabled = new;
206 return Py_BuildValue("i", old);
Jack Jansenee23d6e1995-01-27 14:43:25 +0000207}
208
Jack Jansena76382a1995-02-02 14:25:56 +0000209
210static PyObject *
211MacOS_HandleEvent(PyObject *self, PyObject *args)
212{
213 EventRecord ev;
214
215 if (!PyArg_ParseTuple(args, "O&", PyMac_GetEventRecord, &ev))
216 return NULL;
217 PyMac_HandleEvent(&ev);
218 Py_INCREF(Py_None);
219 return Py_None;
220}
221
Guido van Rossum2d167031994-09-16 10:54:21 +0000222static PyMethodDef MacOS_Methods[] = {
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000223 {"AcceptHighLevelEvent", MacOS_AcceptHighLevelEvent, 1},
Guido van Rossumb7e79e51995-01-22 18:42:12 +0000224 {"GetCreatorAndType", MacOS_GetCreatorAndType, 1},
225 {"SetCreatorAndType", MacOS_SetCreatorAndType, 1},
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000226#ifdef USE_STDWIN
227 {"SetHighLevelEventHandler", MacOS_SetHighLevelEventHandler, 1},
228#endif
Jack Jansene8e8ae01995-01-26 16:36:45 +0000229 {"SetScheduleTimes", MacOS_SetScheduleTimes, 1},
Jack Jansenee23d6e1995-01-27 14:43:25 +0000230 {"EnableAppswitch", MacOS_EnableAppswitch, 1},
Jack Jansena76382a1995-02-02 14:25:56 +0000231 {"HandleEvent", MacOS_HandleEvent, 1},
Guido van Rossumf74d4e21995-01-18 23:58:07 +0000232 {NULL, NULL} /* Sentinel */
Guido van Rossum2d167031994-09-16 10:54:21 +0000233};
234
235
236void
237MacOS_Init()
238{
239 PyObject *m, *d;
240
241 m = Py_InitModule("MacOS", MacOS_Methods);
242 d = PyModule_GetDict(m);
243
244 /* Initialize MacOS.Error exception */
Guido van Rossumbf068b11995-01-25 23:09:20 +0000245 MacOS_Error = PyMac_GetOSErrException();
Guido van Rossume433c971994-09-29 10:02:56 +0000246 if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
Guido van Rossum2d167031994-09-16 10:54:21 +0000247 Py_FatalError("can't define MacOS.Error");
248}
Guido van Rossume7134aa1995-02-26 10:20:53 +0000249