Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 9954699 | 1995-01-08 14:33:34 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 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. |
| 14 | |
| 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. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Macintosh OS-specific interface */ |
| 26 | |
| 27 | #include "Python.h" |
Jack Jansen | 97ce361 | 1994-12-14 14:02:24 +0000 | [diff] [blame] | 28 | #include "macglue.h" |
Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 29 | |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 30 | #include <Windows.h> |
| 31 | |
Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 32 | static PyObject *MacOS_Error; /* Exception MacOS.Error */ |
| 33 | |
Guido van Rossum | e6d9ccc | 1995-02-21 21:01:05 +0000 | [diff] [blame] | 34 | #ifdef MPW |
Guido van Rossum | 9fed183 | 1995-02-18 15:02:02 +0000 | [diff] [blame] | 35 | #define bufferIsSmall -607 /*error returns from Post and Accept */ |
| 36 | #endif |
| 37 | |
Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 38 | |
| 39 | /*----------------------------------------------------------------------*/ |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 40 | /* Miscellaneous File System Operations */ |
| 41 | |
| 42 | static PyObject * |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 43 | MacOS_GetCreatorAndType(PyObject *self, PyObject *args) |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 44 | { |
| 45 | Str255 name; |
| 46 | FInfo info; |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 47 | PyObject *creator, *type, *res; |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 48 | OSErr err; |
| 49 | |
Guido van Rossum | 9aa3d13 | 1995-01-21 13:46:04 +0000 | [diff] [blame] | 50 | if (!PyArg_ParseTuple(args, "O&", PyMac_GetStr255, &name)) |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 51 | return NULL; |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 52 | if ((err = GetFInfo(name, 0, &info)) != noErr) |
| 53 | return PyErr_Mac(MacOS_Error, err); |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 54 | creator = PyString_FromStringAndSize((char *)&info.fdCreator, 4); |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 55 | type = PyString_FromStringAndSize((char *)&info.fdType, 4); |
| 56 | res = Py_BuildValue("OO", creator, type); |
Guido van Rossum | fffb8bb | 1995-01-12 12:37:24 +0000 | [diff] [blame] | 57 | Py_DECREF(creator); |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 58 | Py_DECREF(type); |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 59 | return res; |
| 60 | } |
| 61 | |
| 62 | static PyObject * |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 63 | MacOS_SetCreatorAndType(PyObject *self, PyObject *args) |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 64 | { |
| 65 | Str255 name; |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 66 | ResType creator, type; |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 67 | FInfo info; |
| 68 | OSErr err; |
| 69 | |
| 70 | if (!PyArg_ParseTuple(args, "O&O&O&", |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 71 | PyMac_GetStr255, &name, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 72 | return NULL; |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 73 | if ((err = GetFInfo(name, 0, &info)) != noErr) |
| 74 | return PyErr_Mac(MacOS_Error, err); |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 75 | info.fdCreator = creator; |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 76 | info.fdType = type; |
| 77 | if ((err = SetFInfo(name, 0, &info)) != noErr) |
| 78 | return PyErr_Mac(MacOS_Error, err); |
Guido van Rossum | e791c2e | 1995-01-09 13:20:04 +0000 | [diff] [blame] | 79 | Py_INCREF(Py_None); |
| 80 | return Py_None; |
| 81 | } |
| 82 | |
| 83 | /*----------------------------------------------------------------------*/ |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 84 | /* STDWIN High Level Event interface */ |
| 85 | |
| 86 | #include <EPPC.h> |
| 87 | #include <Events.h> |
| 88 | |
| 89 | #ifdef USE_STDWIN |
| 90 | |
| 91 | extern void (*_w_high_level_event_proc)(EventRecord *); |
| 92 | |
| 93 | static PyObject *MacOS_HighLevelEventHandler = NULL; |
| 94 | |
| 95 | static void |
Guido van Rossum | bf068b1 | 1995-01-25 23:09:20 +0000 | [diff] [blame] | 96 | MacOS_HighLevelEventProc(EventRecord *e) |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 97 | { |
| 98 | if (MacOS_HighLevelEventHandler != NULL) { |
Guido van Rossum | bf068b1 | 1995-01-25 23:09:20 +0000 | [diff] [blame] | 99 | PyObject *args = PyMac_BuildEventRecord(e); |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 100 | 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 Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 116 | /* XXXX Need to come here from PyMac_DoYield too... */ |
| 117 | |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 118 | static PyObject * |
| 119 | MacOS_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 | |
| 144 | static PyObject * |
| 145 | MacOS_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 Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 177 | /* |
| 178 | ** Set poll frequency and cpu-yield-time |
| 179 | */ |
| 180 | static PyObject * |
| 181 | MacOS_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 Jansen | b2f6a7e | 1995-02-20 15:46:10 +0000 | [diff] [blame] | 188 | if ( bgi == -2 || bgy == -2 ) { |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 189 | 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 Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 197 | static PyObject * |
| 198 | MacOS_EnableAppswitch(PyObject *self, PyObject *args) |
| 199 | { |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 200 | int old, new; |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 202 | if (!PyArg_ParseTuple(args, "i", &new)) |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 203 | return NULL; |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 204 | old = PyMac_DoYieldEnabled; |
| 205 | PyMac_DoYieldEnabled = new; |
| 206 | return Py_BuildValue("i", old); |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 209 | |
| 210 | static PyObject * |
| 211 | MacOS_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 Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 222 | static PyMethodDef MacOS_Methods[] = { |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 223 | {"AcceptHighLevelEvent", MacOS_AcceptHighLevelEvent, 1}, |
Guido van Rossum | b7e79e5 | 1995-01-22 18:42:12 +0000 | [diff] [blame] | 224 | {"GetCreatorAndType", MacOS_GetCreatorAndType, 1}, |
| 225 | {"SetCreatorAndType", MacOS_SetCreatorAndType, 1}, |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 226 | #ifdef USE_STDWIN |
| 227 | {"SetHighLevelEventHandler", MacOS_SetHighLevelEventHandler, 1}, |
| 228 | #endif |
Jack Jansen | e8e8ae0 | 1995-01-26 16:36:45 +0000 | [diff] [blame] | 229 | {"SetScheduleTimes", MacOS_SetScheduleTimes, 1}, |
Jack Jansen | ee23d6e | 1995-01-27 14:43:25 +0000 | [diff] [blame] | 230 | {"EnableAppswitch", MacOS_EnableAppswitch, 1}, |
Jack Jansen | a76382a | 1995-02-02 14:25:56 +0000 | [diff] [blame] | 231 | {"HandleEvent", MacOS_HandleEvent, 1}, |
Guido van Rossum | f74d4e2 | 1995-01-18 23:58:07 +0000 | [diff] [blame] | 232 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | |
| 236 | void |
| 237 | MacOS_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 Rossum | bf068b1 | 1995-01-25 23:09:20 +0000 | [diff] [blame] | 245 | MacOS_Error = PyMac_GetOSErrException(); |
Guido van Rossum | e433c97 | 1994-09-29 10:02:56 +0000 | [diff] [blame] | 246 | if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0) |
Guido van Rossum | 2d16703 | 1994-09-16 10:54:21 +0000 | [diff] [blame] | 247 | Py_FatalError("can't define MacOS.Error"); |
| 248 | } |
Guido van Rossum | e7134aa | 1995-02-26 10:20:53 +0000 | [diff] [blame] | 249 | |