| 1 | n/a | /*********************************************************** |
|---|
| 2 | n/a | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
|---|
| 3 | n/a | The Netherlands. |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | All Rights Reserved |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | Permission to use, copy, modify, and distribute this software and its |
|---|
| 8 | n/a | documentation for any purpose and without fee is hereby granted, |
|---|
| 9 | n/a | provided that the above copyright notice appear in all copies and that |
|---|
| 10 | n/a | both that copyright notice and this permission notice appear in |
|---|
| 11 | n/a | supporting documentation, and that the names of Stichting Mathematisch |
|---|
| 12 | n/a | Centrum or CWI not be used in advertising or publicity pertaining to |
|---|
| 13 | n/a | distribution of the software without specific, written prior permission. |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|---|
| 16 | n/a | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|---|
| 17 | n/a | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
|---|
| 18 | n/a | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|---|
| 19 | n/a | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|---|
| 20 | n/a | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|---|
| 21 | n/a | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | ******************************************************************/ |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | /* Macintosh OS-specific interface */ |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | #include "Python.h" |
|---|
| 28 | n/a | #include "pymactoolbox.h" |
|---|
| 29 | n/a | |
|---|
| 30 | n/a | #include <Carbon/Carbon.h> |
|---|
| 31 | n/a | #include <ApplicationServices/ApplicationServices.h> |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | #include <arpa/inet.h> /* for ntohl, htonl */ |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #ifndef HAVE_OSX105_SDK |
|---|
| 37 | n/a | typedef SInt16 FSIORefNum; |
|---|
| 38 | n/a | #endif |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | static PyObject *MacOS_Error; /* Exception MacOS.Error */ |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | #define PATHNAMELEN 1024 |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | /* ----------------------------------------------------- */ |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | /* Declarations for objects of type Resource fork */ |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | typedef struct { |
|---|
| 49 | n/a | PyObject_HEAD |
|---|
| 50 | n/a | FSIORefNum fRefNum; |
|---|
| 51 | n/a | int isclosed; |
|---|
| 52 | n/a | } rfobject; |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | static PyTypeObject Rftype; |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | /* ---------------------------------------------------------------- */ |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | static void |
|---|
| 61 | n/a | do_close(rfobject *self) |
|---|
| 62 | n/a | { |
|---|
| 63 | n/a | if (self->isclosed ) return; |
|---|
| 64 | n/a | (void)FSCloseFork(self->fRefNum); |
|---|
| 65 | n/a | self->isclosed = 1; |
|---|
| 66 | n/a | } |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | static char rf_read__doc__[] = |
|---|
| 69 | n/a | "Read data from resource fork" |
|---|
| 70 | n/a | ; |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | static PyObject * |
|---|
| 73 | n/a | rf_read(rfobject *self, PyObject *args) |
|---|
| 74 | n/a | { |
|---|
| 75 | n/a | long n; |
|---|
| 76 | n/a | PyObject *v; |
|---|
| 77 | n/a | OSErr err; |
|---|
| 78 | n/a | ByteCount n2; |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | if (self->isclosed) { |
|---|
| 81 | n/a | PyErr_SetString(PyExc_ValueError, "Operation on closed file"); |
|---|
| 82 | n/a | return NULL; |
|---|
| 83 | n/a | } |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | if (!PyArg_ParseTuple(args, "l", &n)) |
|---|
| 86 | n/a | return NULL; |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | v = PyBytes_FromStringAndSize((char *)NULL, n); |
|---|
| 89 | n/a | if (v == NULL) |
|---|
| 90 | n/a | return NULL; |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | err = FSReadFork(self->fRefNum, fsAtMark, 0, n, PyString_AsString(v), &n2); |
|---|
| 93 | n/a | if (err && err != eofErr) { |
|---|
| 94 | n/a | PyMac_Error(err); |
|---|
| 95 | n/a | Py_DECREF(v); |
|---|
| 96 | n/a | return NULL; |
|---|
| 97 | n/a | } |
|---|
| 98 | n/a | _PyString_Resize(&v, n2); |
|---|
| 99 | n/a | return v; |
|---|
| 100 | n/a | } |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | static char rf_write__doc__[] = |
|---|
| 104 | n/a | "Write to resource fork" |
|---|
| 105 | n/a | ; |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | static PyObject * |
|---|
| 108 | n/a | rf_write(rfobject *self, PyObject *args) |
|---|
| 109 | n/a | { |
|---|
| 110 | n/a | char *buffer; |
|---|
| 111 | n/a | long size; |
|---|
| 112 | n/a | OSErr err; |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | if (self->isclosed) { |
|---|
| 115 | n/a | PyErr_SetString(PyExc_ValueError, "Operation on closed file"); |
|---|
| 116 | n/a | return NULL; |
|---|
| 117 | n/a | } |
|---|
| 118 | n/a | if (!PyArg_ParseTuple(args, "s#", &buffer, &size)) |
|---|
| 119 | n/a | return NULL; |
|---|
| 120 | n/a | err = FSWriteFork(self->fRefNum, fsAtMark, 0, size, buffer, NULL); |
|---|
| 121 | n/a | if (err) { |
|---|
| 122 | n/a | PyMac_Error(err); |
|---|
| 123 | n/a | return NULL; |
|---|
| 124 | n/a | } |
|---|
| 125 | n/a | Py_INCREF(Py_None); |
|---|
| 126 | n/a | return Py_None; |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | static char rf_seek__doc__[] = |
|---|
| 131 | n/a | "Set file position" |
|---|
| 132 | n/a | ; |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | static PyObject * |
|---|
| 135 | n/a | rf_seek(rfobject *self, PyObject *args) |
|---|
| 136 | n/a | { |
|---|
| 137 | n/a | long amount; |
|---|
| 138 | n/a | int whence = SEEK_SET; |
|---|
| 139 | n/a | int mode; |
|---|
| 140 | n/a | OSErr err; |
|---|
| 141 | n/a | |
|---|
| 142 | n/a | if (self->isclosed) { |
|---|
| 143 | n/a | PyErr_SetString(PyExc_ValueError, "Operation on closed file"); |
|---|
| 144 | n/a | return NULL; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | if (!PyArg_ParseTuple(args, "l|i", &amount, &whence)) { |
|---|
| 147 | n/a | return NULL; |
|---|
| 148 | n/a | } |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | switch (whence) { |
|---|
| 151 | n/a | case SEEK_CUR: |
|---|
| 152 | n/a | mode = fsFromMark; |
|---|
| 153 | n/a | break; |
|---|
| 154 | n/a | case SEEK_END: |
|---|
| 155 | n/a | mode = fsFromLEOF; |
|---|
| 156 | n/a | break; |
|---|
| 157 | n/a | case SEEK_SET: |
|---|
| 158 | n/a | mode = fsFromStart; |
|---|
| 159 | n/a | break; |
|---|
| 160 | n/a | default: |
|---|
| 161 | n/a | PyErr_BadArgument(); |
|---|
| 162 | n/a | return NULL; |
|---|
| 163 | n/a | } |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | err = FSSetForkPosition(self->fRefNum, mode, amount); |
|---|
| 166 | n/a | if (err != noErr) { |
|---|
| 167 | n/a | PyMac_Error(err); |
|---|
| 168 | n/a | return NULL; |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | Py_INCREF(Py_None); |
|---|
| 171 | n/a | return Py_None; |
|---|
| 172 | n/a | } |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | static char rf_tell__doc__[] = |
|---|
| 176 | n/a | "Get file position" |
|---|
| 177 | n/a | ; |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | static PyObject * |
|---|
| 180 | n/a | rf_tell(rfobject *self, PyObject *args) |
|---|
| 181 | n/a | { |
|---|
| 182 | n/a | long long where; |
|---|
| 183 | n/a | OSErr err; |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | if (self->isclosed) { |
|---|
| 186 | n/a | PyErr_SetString(PyExc_ValueError, "Operation on closed file"); |
|---|
| 187 | n/a | return NULL; |
|---|
| 188 | n/a | } |
|---|
| 189 | n/a | if (!PyArg_ParseTuple(args, "")) |
|---|
| 190 | n/a | return NULL; |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | err = FSGetForkPosition(self->fRefNum, &where); |
|---|
| 193 | n/a | if (err != noErr) { |
|---|
| 194 | n/a | PyMac_Error(err); |
|---|
| 195 | n/a | return NULL; |
|---|
| 196 | n/a | } |
|---|
| 197 | n/a | return PyLong_FromLongLong(where); |
|---|
| 198 | n/a | } |
|---|
| 199 | n/a | |
|---|
| 200 | n/a | static char rf_close__doc__[] = |
|---|
| 201 | n/a | "Close resource fork" |
|---|
| 202 | n/a | ; |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | static PyObject * |
|---|
| 205 | n/a | rf_close(rfobject *self, PyObject *args) |
|---|
| 206 | n/a | { |
|---|
| 207 | n/a | if (!PyArg_ParseTuple(args, "")) |
|---|
| 208 | n/a | return NULL; |
|---|
| 209 | n/a | do_close(self); |
|---|
| 210 | n/a | Py_INCREF(Py_None); |
|---|
| 211 | n/a | return Py_None; |
|---|
| 212 | n/a | } |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | |
|---|
| 215 | n/a | static struct PyMethodDef rf_methods[] = { |
|---|
| 216 | n/a | {"read", (PyCFunction)rf_read, 1, rf_read__doc__}, |
|---|
| 217 | n/a | {"write", (PyCFunction)rf_write, 1, rf_write__doc__}, |
|---|
| 218 | n/a | {"seek", (PyCFunction)rf_seek, 1, rf_seek__doc__}, |
|---|
| 219 | n/a | {"tell", (PyCFunction)rf_tell, 1, rf_tell__doc__}, |
|---|
| 220 | n/a | {"close", (PyCFunction)rf_close, 1, rf_close__doc__}, |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 223 | n/a | }; |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | /* ---------- */ |
|---|
| 226 | n/a | |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | static rfobject * |
|---|
| 229 | n/a | newrfobject(void) |
|---|
| 230 | n/a | { |
|---|
| 231 | n/a | rfobject *self; |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | self = PyObject_NEW(rfobject, &Rftype); |
|---|
| 234 | n/a | if (self == NULL) |
|---|
| 235 | n/a | return NULL; |
|---|
| 236 | n/a | self->isclosed = 1; |
|---|
| 237 | n/a | return self; |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | static void |
|---|
| 242 | n/a | rf_dealloc(rfobject *self) |
|---|
| 243 | n/a | { |
|---|
| 244 | n/a | do_close(self); |
|---|
| 245 | n/a | PyObject_DEL(self); |
|---|
| 246 | n/a | } |
|---|
| 247 | n/a | |
|---|
| 248 | n/a | static PyObject * |
|---|
| 249 | n/a | rf_getattr(rfobject *self, char *name) |
|---|
| 250 | n/a | { |
|---|
| 251 | n/a | return Py_FindMethod(rf_methods, (PyObject *)self, name); |
|---|
| 252 | n/a | } |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | static char Rftype__doc__[] = |
|---|
| 255 | n/a | "Resource fork file object" |
|---|
| 256 | n/a | ; |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | static PyTypeObject Rftype = { |
|---|
| 259 | n/a | PyObject_HEAD_INIT(&PyType_Type) |
|---|
| 260 | n/a | 0, /*ob_size*/ |
|---|
| 261 | n/a | "MacOS.ResourceFork", /*tp_name*/ |
|---|
| 262 | n/a | sizeof(rfobject), /*tp_basicsize*/ |
|---|
| 263 | n/a | 0, /*tp_itemsize*/ |
|---|
| 264 | n/a | /* methods */ |
|---|
| 265 | n/a | (destructor)rf_dealloc, /*tp_dealloc*/ |
|---|
| 266 | n/a | (printfunc)0, /*tp_print*/ |
|---|
| 267 | n/a | (getattrfunc)rf_getattr, /*tp_getattr*/ |
|---|
| 268 | n/a | (setattrfunc)0, /*tp_setattr*/ |
|---|
| 269 | n/a | (cmpfunc)0, /*tp_compare*/ |
|---|
| 270 | n/a | (reprfunc)0, /*tp_repr*/ |
|---|
| 271 | n/a | 0, /*tp_as_number*/ |
|---|
| 272 | n/a | 0, /*tp_as_sequence*/ |
|---|
| 273 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 274 | n/a | (hashfunc)0, /*tp_hash*/ |
|---|
| 275 | n/a | (ternaryfunc)0, /*tp_call*/ |
|---|
| 276 | n/a | (reprfunc)0, /*tp_str*/ |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | /* Space for future expansion */ |
|---|
| 279 | n/a | 0L,0L,0L,0L, |
|---|
| 280 | n/a | Rftype__doc__ /* Documentation string */ |
|---|
| 281 | n/a | }; |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | /* End of code for Resource fork objects */ |
|---|
| 285 | n/a | /* -------------------------------------------------------- */ |
|---|
| 286 | n/a | |
|---|
| 287 | n/a | /*----------------------------------------------------------------------*/ |
|---|
| 288 | n/a | /* Miscellaneous File System Operations */ |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | static char getcrtp_doc[] = "Get MacOS 4-char creator and type for a file"; |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | static PyObject * |
|---|
| 293 | n/a | MacOS_GetCreatorAndType(PyObject *self, PyObject *args) |
|---|
| 294 | n/a | { |
|---|
| 295 | n/a | PyObject *creator, *type, *res; |
|---|
| 296 | n/a | OSErr err; |
|---|
| 297 | n/a | FSRef ref; |
|---|
| 298 | n/a | FSCatalogInfo cataloginfo; |
|---|
| 299 | n/a | FileInfo* finfo; |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSRef, &ref)) { |
|---|
| 302 | n/a | #ifndef __LP64__ |
|---|
| 303 | n/a | /* This function is documented to take an FSSpec as well, |
|---|
| 304 | n/a | * which only works in 32-bit mode. |
|---|
| 305 | n/a | */ |
|---|
| 306 | n/a | PyErr_Clear(); |
|---|
| 307 | n/a | FSSpec fss; |
|---|
| 308 | n/a | FInfo info; |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | if (!PyArg_ParseTuple(args, "O&", PyMac_GetFSSpec, &fss)) |
|---|
| 311 | n/a | return NULL; |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | if ((err = FSpGetFInfo(&fss, &info)) != noErr) { |
|---|
| 314 | n/a | return PyErr_Mac(MacOS_Error, err); |
|---|
| 315 | n/a | } |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | info.fdCreator = ntohl(info.fdCreator); |
|---|
| 318 | n/a | info.fdType = ntohl(info.fdType); |
|---|
| 319 | n/a | |
|---|
| 320 | n/a | creator = PyString_FromStringAndSize( |
|---|
| 321 | n/a | (char *)&info.fdCreator, 4); |
|---|
| 322 | n/a | type = PyString_FromStringAndSize((char *)&info.fdType, 4); |
|---|
| 323 | n/a | res = Py_BuildValue("OO", creator, type); |
|---|
| 324 | n/a | Py_DECREF(creator); |
|---|
| 325 | n/a | Py_DECREF(type); |
|---|
| 326 | n/a | return res; |
|---|
| 327 | n/a | #else /* __LP64__ */ |
|---|
| 328 | n/a | return NULL; |
|---|
| 329 | n/a | #endif /* __LP64__ */ |
|---|
| 330 | n/a | } |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | err = FSGetCatalogInfo(&ref, |
|---|
| 333 | n/a | kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo, |
|---|
| 334 | n/a | NULL, NULL, NULL); |
|---|
| 335 | n/a | if (err != noErr) { |
|---|
| 336 | n/a | PyErr_Mac(MacOS_Error, err); |
|---|
| 337 | n/a | return NULL; |
|---|
| 338 | n/a | } |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) { |
|---|
| 341 | n/a | /* Directory: doesn't have type/creator info. |
|---|
| 342 | n/a | * |
|---|
| 343 | n/a | * The specific error code is for backward compatibility with |
|---|
| 344 | n/a | * earlier versions. |
|---|
| 345 | n/a | */ |
|---|
| 346 | n/a | PyErr_Mac(MacOS_Error, fnfErr); |
|---|
| 347 | n/a | return NULL; |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | } |
|---|
| 350 | n/a | finfo = (FileInfo*)&(cataloginfo.finderInfo); |
|---|
| 351 | n/a | finfo->fileCreator = ntohl(finfo->fileCreator); |
|---|
| 352 | n/a | finfo->fileType = ntohl(finfo->fileType); |
|---|
| 353 | n/a | creator = PyString_FromStringAndSize((char*)&(finfo->fileCreator), 4); |
|---|
| 354 | n/a | type = PyString_FromStringAndSize((char*)&(finfo->fileType), 4); |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | res = Py_BuildValue("OO", creator, type); |
|---|
| 357 | n/a | Py_DECREF(creator); |
|---|
| 358 | n/a | Py_DECREF(type); |
|---|
| 359 | n/a | return res; |
|---|
| 360 | n/a | } |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | static char setcrtp_doc[] = "Set MacOS 4-char creator and type for a file"; |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | static PyObject * |
|---|
| 365 | n/a | MacOS_SetCreatorAndType(PyObject *self, PyObject *args) |
|---|
| 366 | n/a | { |
|---|
| 367 | n/a | ResType creator, type; |
|---|
| 368 | n/a | FSRef ref; |
|---|
| 369 | n/a | FileInfo* finfo; |
|---|
| 370 | n/a | OSErr err; |
|---|
| 371 | n/a | FSCatalogInfo cataloginfo; |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | if (!PyArg_ParseTuple(args, "O&O&O&", |
|---|
| 374 | n/a | PyMac_GetFSRef, &ref, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) { |
|---|
| 375 | n/a | #ifndef __LP64__ |
|---|
| 376 | n/a | /* Try to handle FSSpec arguments, for backward compatibility */ |
|---|
| 377 | n/a | FSSpec fss; |
|---|
| 378 | n/a | FInfo info; |
|---|
| 379 | n/a | |
|---|
| 380 | n/a | if (!PyArg_ParseTuple(args, "O&O&O&", |
|---|
| 381 | n/a | PyMac_GetFSSpec, &fss, PyMac_GetOSType, &creator, PyMac_GetOSType, &type)) |
|---|
| 382 | n/a | return NULL; |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | if ((err = FSpGetFInfo(&fss, &info)) != noErr) |
|---|
| 385 | n/a | return PyErr_Mac(MacOS_Error, err); |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | info.fdCreator = creator; |
|---|
| 388 | n/a | info.fdType = type; |
|---|
| 389 | n/a | |
|---|
| 390 | n/a | if ((err = FSpSetFInfo(&fss, &info)) != noErr) |
|---|
| 391 | n/a | return PyErr_Mac(MacOS_Error, err); |
|---|
| 392 | n/a | Py_INCREF(Py_None); |
|---|
| 393 | n/a | return Py_None; |
|---|
| 394 | n/a | #else /* __LP64__ */ |
|---|
| 395 | n/a | return NULL; |
|---|
| 396 | n/a | #endif /* __LP64__ */ |
|---|
| 397 | n/a | } |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | err = FSGetCatalogInfo(&ref, |
|---|
| 400 | n/a | kFSCatInfoFinderInfo|kFSCatInfoNodeFlags, &cataloginfo, |
|---|
| 401 | n/a | NULL, NULL, NULL); |
|---|
| 402 | n/a | if (err != noErr) { |
|---|
| 403 | n/a | PyErr_Mac(MacOS_Error, err); |
|---|
| 404 | n/a | return NULL; |
|---|
| 405 | n/a | } |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | if ((cataloginfo.nodeFlags & kFSNodeIsDirectoryMask) != 0) { |
|---|
| 408 | n/a | /* Directory: doesn't have type/creator info. |
|---|
| 409 | n/a | * |
|---|
| 410 | n/a | * The specific error code is for backward compatibility with |
|---|
| 411 | n/a | * earlier versions. |
|---|
| 412 | n/a | */ |
|---|
| 413 | n/a | PyErr_Mac(MacOS_Error, fnfErr); |
|---|
| 414 | n/a | return NULL; |
|---|
| 415 | n/a | |
|---|
| 416 | n/a | } |
|---|
| 417 | n/a | finfo = (FileInfo*)&(cataloginfo.finderInfo); |
|---|
| 418 | n/a | finfo->fileCreator = creator; |
|---|
| 419 | n/a | finfo->fileType = type; |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | err = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &cataloginfo); |
|---|
| 422 | n/a | if (err != noErr) { |
|---|
| 423 | n/a | PyErr_Mac(MacOS_Error, fnfErr); |
|---|
| 424 | n/a | return NULL; |
|---|
| 425 | n/a | } |
|---|
| 426 | n/a | |
|---|
| 427 | n/a | Py_INCREF(Py_None); |
|---|
| 428 | n/a | return Py_None; |
|---|
| 429 | n/a | } |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | static char geterr_doc[] = "Convert OSErr number to string"; |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | static PyObject * |
|---|
| 435 | n/a | MacOS_GetErrorString(PyObject *self, PyObject *args) |
|---|
| 436 | n/a | { |
|---|
| 437 | n/a | int err; |
|---|
| 438 | n/a | char buf[256]; |
|---|
| 439 | n/a | Handle h; |
|---|
| 440 | n/a | char *str; |
|---|
| 441 | n/a | static int errors_loaded; |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | if (!PyArg_ParseTuple(args, "i", &err)) |
|---|
| 444 | n/a | return NULL; |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | h = GetResource('Estr', err); |
|---|
| 447 | n/a | if (!h && !errors_loaded) { |
|---|
| 448 | n/a | /* |
|---|
| 449 | n/a | ** Attempt to open the resource file containing the |
|---|
| 450 | n/a | ** Estr resources. We ignore all errors. We also try |
|---|
| 451 | n/a | ** this only once. |
|---|
| 452 | n/a | */ |
|---|
| 453 | n/a | PyObject *m, *rv; |
|---|
| 454 | n/a | errors_loaded = 1; |
|---|
| 455 | n/a | |
|---|
| 456 | n/a | m = PyImport_ImportModuleNoBlock("macresource"); |
|---|
| 457 | n/a | if (!m) { |
|---|
| 458 | n/a | if (Py_VerboseFlag) |
|---|
| 459 | n/a | PyErr_Print(); |
|---|
| 460 | n/a | PyErr_Clear(); |
|---|
| 461 | n/a | } |
|---|
| 462 | n/a | else { |
|---|
| 463 | n/a | rv = PyObject_CallMethod(m, "open_error_resource", ""); |
|---|
| 464 | n/a | if (!rv) { |
|---|
| 465 | n/a | if (Py_VerboseFlag) |
|---|
| 466 | n/a | PyErr_Print(); |
|---|
| 467 | n/a | PyErr_Clear(); |
|---|
| 468 | n/a | } |
|---|
| 469 | n/a | else { |
|---|
| 470 | n/a | Py_DECREF(rv); |
|---|
| 471 | n/a | /* And try again... */ |
|---|
| 472 | n/a | h = GetResource('Estr', err); |
|---|
| 473 | n/a | } |
|---|
| 474 | n/a | Py_DECREF(m); |
|---|
| 475 | n/a | } |
|---|
| 476 | n/a | } |
|---|
| 477 | n/a | /* |
|---|
| 478 | n/a | ** Whether the code above succeeded or not, we won't try |
|---|
| 479 | n/a | ** again. |
|---|
| 480 | n/a | */ |
|---|
| 481 | n/a | errors_loaded = 1; |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | if (h) { |
|---|
| 484 | n/a | HLock(h); |
|---|
| 485 | n/a | str = (char *)*h; |
|---|
| 486 | n/a | memcpy(buf, str+1, (unsigned char)str[0]); |
|---|
| 487 | n/a | buf[(unsigned char)str[0]] = '\0'; |
|---|
| 488 | n/a | HUnlock(h); |
|---|
| 489 | n/a | ReleaseResource(h); |
|---|
| 490 | n/a | } |
|---|
| 491 | n/a | else { |
|---|
| 492 | n/a | PyOS_snprintf(buf, sizeof(buf), "Mac OS error code %d", err); |
|---|
| 493 | n/a | } |
|---|
| 494 | n/a | |
|---|
| 495 | n/a | return Py_BuildValue("s", buf); |
|---|
| 496 | n/a | } |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | #ifndef __LP64__ |
|---|
| 500 | n/a | |
|---|
| 501 | n/a | static char splash_doc[] = "Open a splash-screen dialog by resource-id (0=close)"; |
|---|
| 502 | n/a | |
|---|
| 503 | n/a | static PyObject * |
|---|
| 504 | n/a | MacOS_splash(PyObject *self, PyObject *args) |
|---|
| 505 | n/a | { |
|---|
| 506 | n/a | int resid = -1; |
|---|
| 507 | n/a | static DialogPtr curdialog = NULL; |
|---|
| 508 | n/a | DialogPtr olddialog; |
|---|
| 509 | n/a | WindowRef theWindow; |
|---|
| 510 | n/a | CGrafPtr thePort; |
|---|
| 511 | n/a | #if 0 |
|---|
| 512 | n/a | short xpos, ypos, width, height, swidth, sheight; |
|---|
| 513 | n/a | #endif |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | if (!PyArg_ParseTuple(args, "|i", &resid)) |
|---|
| 516 | n/a | return NULL; |
|---|
| 517 | n/a | olddialog = curdialog; |
|---|
| 518 | n/a | curdialog = NULL; |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | if ( resid != -1 ) { |
|---|
| 521 | n/a | curdialog = GetNewDialog(resid, NULL, (WindowPtr)-1); |
|---|
| 522 | n/a | if ( curdialog ) { |
|---|
| 523 | n/a | theWindow = GetDialogWindow(curdialog); |
|---|
| 524 | n/a | thePort = GetWindowPort(theWindow); |
|---|
| 525 | n/a | #if 0 |
|---|
| 526 | n/a | width = thePort->portRect.right - thePort->portRect.left; |
|---|
| 527 | n/a | height = thePort->portRect.bottom - thePort->portRect.top; |
|---|
| 528 | n/a | swidth = qd.screenBits.bounds.right - qd.screenBits.bounds.left; |
|---|
| 529 | n/a | sheight = qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - LMGetMBarHeight(); |
|---|
| 530 | n/a | xpos = (swidth-width)/2; |
|---|
| 531 | n/a | ypos = (sheight-height)/5 + LMGetMBarHeight(); |
|---|
| 532 | n/a | MoveWindow(theWindow, xpos, ypos, 0); |
|---|
| 533 | n/a | ShowWindow(theWindow); |
|---|
| 534 | n/a | #endif |
|---|
| 535 | n/a | DrawDialog(curdialog); |
|---|
| 536 | n/a | } |
|---|
| 537 | n/a | } |
|---|
| 538 | n/a | if (olddialog) |
|---|
| 539 | n/a | DisposeDialog(olddialog); |
|---|
| 540 | n/a | Py_INCREF(Py_None); |
|---|
| 541 | n/a | return Py_None; |
|---|
| 542 | n/a | } |
|---|
| 543 | n/a | |
|---|
| 544 | n/a | static char DebugStr_doc[] = "Switch to low-level debugger with a message"; |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | static PyObject * |
|---|
| 547 | n/a | MacOS_DebugStr(PyObject *self, PyObject *args) |
|---|
| 548 | n/a | { |
|---|
| 549 | n/a | Str255 message; |
|---|
| 550 | n/a | PyObject *object = 0; |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | if (!PyArg_ParseTuple(args, "O&|O", PyMac_GetStr255, message, &object)) |
|---|
| 553 | n/a | return NULL; |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | DebugStr(message); |
|---|
| 556 | n/a | Py_INCREF(Py_None); |
|---|
| 557 | n/a | return Py_None; |
|---|
| 558 | n/a | } |
|---|
| 559 | n/a | |
|---|
| 560 | n/a | |
|---|
| 561 | n/a | static char SysBeep_doc[] = "BEEEEEP!!!"; |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | static PyObject * |
|---|
| 564 | n/a | MacOS_SysBeep(PyObject *self, PyObject *args) |
|---|
| 565 | n/a | { |
|---|
| 566 | n/a | int duration = 6; |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | if (!PyArg_ParseTuple(args, "|i", &duration)) |
|---|
| 569 | n/a | return NULL; |
|---|
| 570 | n/a | SysBeep(duration); |
|---|
| 571 | n/a | Py_INCREF(Py_None); |
|---|
| 572 | n/a | return Py_None; |
|---|
| 573 | n/a | } |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | #endif /* __LP64__ */ |
|---|
| 576 | n/a | |
|---|
| 577 | n/a | static char WMAvailable_doc[] = |
|---|
| 578 | n/a | "True if this process can interact with the display." |
|---|
| 579 | n/a | "Will foreground the application on the first call as a side-effect." |
|---|
| 580 | n/a | ; |
|---|
| 581 | n/a | |
|---|
| 582 | n/a | static PyObject * |
|---|
| 583 | n/a | MacOS_WMAvailable(PyObject *self, PyObject *args) |
|---|
| 584 | n/a | { |
|---|
| 585 | n/a | static PyObject *rv = NULL; |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | if (!PyArg_ParseTuple(args, "")) |
|---|
| 588 | n/a | return NULL; |
|---|
| 589 | n/a | if (!rv) { |
|---|
| 590 | n/a | ProcessSerialNumber psn; |
|---|
| 591 | n/a | |
|---|
| 592 | n/a | /* |
|---|
| 593 | n/a | ** This is a fairly innocuous call to make if we don't have a window |
|---|
| 594 | n/a | ** manager, or if we have no permission to talk to it. It will print |
|---|
| 595 | n/a | ** a message on stderr, but at least it won't abort the process. |
|---|
| 596 | n/a | ** It appears the function caches the result itself, and it's cheap, so |
|---|
| 597 | n/a | ** no need for us to cache. |
|---|
| 598 | n/a | */ |
|---|
| 599 | n/a | #ifdef kCGNullDirectDisplay |
|---|
| 600 | n/a | /* On 10.1 CGMainDisplayID() isn't available, and |
|---|
| 601 | n/a | ** kCGNullDirectDisplay isn't defined. |
|---|
| 602 | n/a | */ |
|---|
| 603 | n/a | if (CGMainDisplayID() == 0) { |
|---|
| 604 | n/a | rv = Py_False; |
|---|
| 605 | n/a | } else { |
|---|
| 606 | n/a | #else |
|---|
| 607 | n/a | { |
|---|
| 608 | n/a | #endif |
|---|
| 609 | n/a | if (GetCurrentProcess(&psn) < 0 || |
|---|
| 610 | n/a | SetFrontProcess(&psn) < 0) { |
|---|
| 611 | n/a | rv = Py_False; |
|---|
| 612 | n/a | } else { |
|---|
| 613 | n/a | rv = Py_True; |
|---|
| 614 | n/a | } |
|---|
| 615 | n/a | } |
|---|
| 616 | n/a | } |
|---|
| 617 | n/a | Py_INCREF(rv); |
|---|
| 618 | n/a | return rv; |
|---|
| 619 | n/a | } |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | static char GetTicks_doc[] = "Return number of ticks since bootup"; |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | static PyObject * |
|---|
| 624 | n/a | MacOS_GetTicks(PyObject *self, PyObject *args) |
|---|
| 625 | n/a | { |
|---|
| 626 | n/a | return Py_BuildValue("i", (int)TickCount()); |
|---|
| 627 | n/a | } |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | static char openrf_doc[] = "Open resource fork of a file"; |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | static PyObject * |
|---|
| 632 | n/a | MacOS_openrf(PyObject *self, PyObject *args) |
|---|
| 633 | n/a | { |
|---|
| 634 | n/a | OSErr err; |
|---|
| 635 | n/a | char *mode = "r"; |
|---|
| 636 | n/a | FSRef ref; |
|---|
| 637 | n/a | SInt8 permission = fsRdPerm; |
|---|
| 638 | n/a | rfobject *fp; |
|---|
| 639 | n/a | HFSUniStr255 name; |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | if (!PyArg_ParseTuple(args, "O&|s", PyMac_GetFSRef, &ref, &mode)) |
|---|
| 642 | n/a | return NULL; |
|---|
| 643 | n/a | while (*mode) { |
|---|
| 644 | n/a | switch (*mode++) { |
|---|
| 645 | n/a | case '*': break; |
|---|
| 646 | n/a | case 'r': permission = fsRdPerm; break; |
|---|
| 647 | n/a | case 'w': permission = fsWrPerm; break; |
|---|
| 648 | n/a | case 'b': break; |
|---|
| 649 | n/a | default: |
|---|
| 650 | n/a | PyErr_BadArgument(); |
|---|
| 651 | n/a | return NULL; |
|---|
| 652 | n/a | } |
|---|
| 653 | n/a | } |
|---|
| 654 | n/a | |
|---|
| 655 | n/a | err = FSGetResourceForkName(&name); |
|---|
| 656 | n/a | if (err != noErr) { |
|---|
| 657 | n/a | PyMac_Error(err); |
|---|
| 658 | n/a | return NULL; |
|---|
| 659 | n/a | } |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | if ( (fp = newrfobject()) == NULL ) |
|---|
| 662 | n/a | return NULL; |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | |
|---|
| 665 | n/a | err = FSOpenFork(&ref, name.length, name.unicode, permission, &fp->fRefNum); |
|---|
| 666 | n/a | if (err != noErr) { |
|---|
| 667 | n/a | Py_DECREF(fp); |
|---|
| 668 | n/a | PyMac_Error(err); |
|---|
| 669 | n/a | return NULL; |
|---|
| 670 | n/a | } |
|---|
| 671 | n/a | fp->isclosed = 0; |
|---|
| 672 | n/a | return (PyObject *)fp; |
|---|
| 673 | n/a | } |
|---|
| 674 | n/a | |
|---|
| 675 | n/a | |
|---|
| 676 | n/a | |
|---|
| 677 | n/a | static PyMethodDef MacOS_Methods[] = { |
|---|
| 678 | n/a | {"GetCreatorAndType", MacOS_GetCreatorAndType, 1, getcrtp_doc}, |
|---|
| 679 | n/a | {"SetCreatorAndType", MacOS_SetCreatorAndType, 1, setcrtp_doc}, |
|---|
| 680 | n/a | {"GetErrorString", MacOS_GetErrorString, 1, geterr_doc}, |
|---|
| 681 | n/a | {"openrf", MacOS_openrf, 1, openrf_doc}, |
|---|
| 682 | n/a | #ifndef __LP64__ |
|---|
| 683 | n/a | {"splash", MacOS_splash, 1, splash_doc}, |
|---|
| 684 | n/a | {"DebugStr", MacOS_DebugStr, 1, DebugStr_doc}, |
|---|
| 685 | n/a | {"SysBeep", MacOS_SysBeep, 1, SysBeep_doc}, |
|---|
| 686 | n/a | #endif /* __LP64__ */ |
|---|
| 687 | n/a | {"GetTicks", MacOS_GetTicks, 1, GetTicks_doc}, |
|---|
| 688 | n/a | {"WMAvailable", MacOS_WMAvailable, 1, WMAvailable_doc}, |
|---|
| 689 | n/a | {NULL, NULL} /* Sentinel */ |
|---|
| 690 | n/a | }; |
|---|
| 691 | n/a | |
|---|
| 692 | n/a | |
|---|
| 693 | n/a | void |
|---|
| 694 | n/a | initMacOS(void) |
|---|
| 695 | n/a | { |
|---|
| 696 | n/a | PyObject *m, *d; |
|---|
| 697 | n/a | |
|---|
| 698 | n/a | if (PyErr_WarnPy3k("In 3.x, the MacOS module is removed.", 1)) |
|---|
| 699 | n/a | return; |
|---|
| 700 | n/a | |
|---|
| 701 | n/a | m = Py_InitModule("MacOS", MacOS_Methods); |
|---|
| 702 | n/a | d = PyModule_GetDict(m); |
|---|
| 703 | n/a | |
|---|
| 704 | n/a | /* Initialize MacOS.Error exception */ |
|---|
| 705 | n/a | MacOS_Error = PyMac_GetOSErrException(); |
|---|
| 706 | n/a | if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0) |
|---|
| 707 | n/a | return; |
|---|
| 708 | n/a | Rftype.ob_type = &PyType_Type; |
|---|
| 709 | n/a | Py_INCREF(&Rftype); |
|---|
| 710 | n/a | if (PyDict_SetItemString(d, "ResourceForkType", (PyObject *)&Rftype) != 0) |
|---|
| 711 | n/a | return; |
|---|
| 712 | n/a | /* |
|---|
| 713 | n/a | ** This is a hack: the following constant added to the id() of a string |
|---|
| 714 | n/a | ** object gives you the address of the data. Unfortunately, it is needed for |
|---|
| 715 | n/a | ** some of the image and sound processing interfaces on the mac:-( |
|---|
| 716 | n/a | */ |
|---|
| 717 | n/a | { |
|---|
| 718 | n/a | PyStringObject *p = 0; |
|---|
| 719 | n/a | long off = (long)&(p->ob_sval[0]); |
|---|
| 720 | n/a | |
|---|
| 721 | n/a | if( PyDict_SetItemString(d, "string_id_to_buffer", Py_BuildValue("i", off)) != 0) |
|---|
| 722 | n/a | return; |
|---|
| 723 | n/a | } |
|---|
| 724 | n/a | #define PY_RUNTIMEMODEL "macho" |
|---|
| 725 | n/a | if (PyDict_SetItemString(d, "runtimemodel", |
|---|
| 726 | n/a | Py_BuildValue("s", PY_RUNTIMEMODEL)) != 0) |
|---|
| 727 | n/a | return; |
|---|
| 728 | n/a | #if defined(WITH_NEXT_FRAMEWORK) |
|---|
| 729 | n/a | #define PY_LINKMODEL "framework" |
|---|
| 730 | n/a | #elif defined(Py_ENABLE_SHARED) |
|---|
| 731 | n/a | #define PY_LINKMODEL "shared" |
|---|
| 732 | n/a | #else |
|---|
| 733 | n/a | #define PY_LINKMODEL "static" |
|---|
| 734 | n/a | #endif |
|---|
| 735 | n/a | if (PyDict_SetItemString(d, "linkmodel", |
|---|
| 736 | n/a | Py_BuildValue("s", PY_LINKMODEL)) != 0) |
|---|
| 737 | n/a | return; |
|---|
| 738 | n/a | |
|---|
| 739 | n/a | } |
|---|