Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 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 | |
| 26 | #include "Python.h" |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 27 | #include "pymactoolbox.h" |
| 28 | |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 29 | #ifdef WITHOUT_FRAMEWORKS |
Jack Jansen | d844a5f | 2001-08-08 15:28:03 +0000 | [diff] [blame] | 30 | #include <Script.h> |
| 31 | #include <Resources.h> |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | /* |
| 35 | ** Find out what the current script is. |
| 36 | ** Donated by Fredrik Lund. |
| 37 | */ |
| 38 | char *PyMac_getscript() |
| 39 | { |
Jack Jansen | 666b1e7 | 2001-10-31 12:11:48 +0000 | [diff] [blame] | 40 | #if TARGET_API_MAC_OSX |
Martin v. Löwis | 52ea7e9 | 2002-11-26 09:05:36 +0000 | [diff] [blame] | 41 | CFStringEncoding enc = CFStringGetSystemEncoding(); |
| 42 | static CFStringRef name = NULL; |
| 43 | /* Return the code name for the encodings for which we have codecs. */ |
| 44 | switch(enc) { |
| 45 | case kCFStringEncodingMacRoman: return "mac-roman"; |
| 46 | case kCFStringEncodingMacGreek: return "mac-greek"; |
| 47 | case kCFStringEncodingMacCyrillic: return "mac-cyrillic"; |
| 48 | case kCFStringEncodingMacTurkish: return "mac-turkish"; |
| 49 | case kCFStringEncodingMacIcelandic: return "mac-icelandic"; |
| 50 | /* XXX which one is mac-latin2? */ |
| 51 | } |
| 52 | if (!name) { |
| 53 | /* This leaks a an object. */ |
| 54 | name = CFStringConvertEncodingToIANACharSetName(enc); |
| 55 | } |
Jack Jansen | 0a116f3 | 2002-12-23 21:03:36 +0000 | [diff] [blame] | 56 | return (char *)CFStringGetCStringPtr(name, 0); |
Jack Jansen | 666b1e7 | 2001-10-31 12:11:48 +0000 | [diff] [blame] | 57 | #else |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 58 | int font, script, lang; |
| 59 | font = 0; |
| 60 | font = GetSysFont(); |
| 61 | script = FontToScript(font); |
| 62 | switch (script) { |
| 63 | case smRoman: |
| 64 | lang = GetScriptVariable(script, smScriptLang); |
| 65 | if (lang == langIcelandic) |
| 66 | return "mac-iceland"; |
| 67 | else if (lang == langTurkish) |
| 68 | return "mac-turkish"; |
| 69 | else if (lang == langGreek) |
| 70 | return "mac-greek"; |
| 71 | else |
| 72 | return "mac-roman"; |
| 73 | break; |
| 74 | #if 0 |
| 75 | /* We don't have a codec for this, so don't return it */ |
| 76 | case smJapanese: |
| 77 | return "mac-japan"; |
| 78 | #endif |
| 79 | case smGreek: |
| 80 | return "mac-greek"; |
| 81 | case smCyrillic: |
| 82 | return "mac-cyrillic"; |
| 83 | default: |
| 84 | return "ascii"; /* better than nothing */ |
| 85 | } |
Jack Jansen | 666b1e7 | 2001-10-31 12:11:48 +0000 | [diff] [blame] | 86 | #endif /* TARGET_API_MAC_OSX */ |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* Like strerror() but for Mac OS error numbers */ |
| 90 | char *PyMac_StrError(int err) |
| 91 | { |
| 92 | static char buf[256]; |
| 93 | Handle h; |
| 94 | char *str; |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 95 | static int errors_loaded; |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 96 | |
| 97 | h = GetResource('Estr', err); |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 98 | if (!h && !errors_loaded) { |
| 99 | /* |
| 100 | ** Attempt to open the resource file containing the |
| 101 | ** Estr resources. We ignore all errors. We also try |
| 102 | ** this only once. |
| 103 | */ |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 104 | PyObject *m, *rv; |
Michael W. Hudson | 5f26dda | 2002-11-09 14:47:18 +0000 | [diff] [blame] | 105 | errors_loaded = 1; |
Jack Jansen | dde800e | 2002-11-07 23:07:05 +0000 | [diff] [blame] | 106 | |
| 107 | m = PyImport_ImportModule("macresource"); |
| 108 | if (!m) { |
| 109 | if (Py_VerboseFlag) |
| 110 | PyErr_Print(); |
| 111 | PyErr_Clear(); |
| 112 | } else { |
| 113 | rv = PyObject_CallMethod(m, "open_error_resource", ""); |
| 114 | if (!rv) { |
| 115 | if (Py_VerboseFlag) |
| 116 | PyErr_Print(); |
| 117 | PyErr_Clear(); |
| 118 | } else { |
| 119 | Py_DECREF(rv); |
| 120 | /* And try again... */ |
| 121 | h = GetResource('Estr', err); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | /* |
| 126 | ** Whether the code above succeeded or not, we won't try |
| 127 | ** again. |
| 128 | */ |
| 129 | errors_loaded = 1; |
| 130 | |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 131 | if ( h ) { |
| 132 | HLock(h); |
| 133 | str = (char *)*h; |
| 134 | memcpy(buf, str+1, (unsigned char)str[0]); |
| 135 | buf[(unsigned char)str[0]] = '\0'; |
| 136 | HUnlock(h); |
| 137 | ReleaseResource(h); |
| 138 | } else { |
Tim Peters | 22a51ef | 2001-12-04 01:11:32 +0000 | [diff] [blame] | 139 | PyOS_snprintf(buf, sizeof(buf), "Mac OS error code %d", err); |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 140 | } |
| 141 | return buf; |
| 142 | } |
| 143 | |
| 144 | /* Exception object shared by all Mac specific modules for Mac OS errors */ |
| 145 | PyObject *PyMac_OSErrException; |
| 146 | |
| 147 | /* Initialize and return PyMac_OSErrException */ |
| 148 | PyObject * |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 149 | PyMac_GetOSErrException(void) |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 150 | { |
| 151 | if (PyMac_OSErrException == NULL) |
Jack Jansen | 8fce2ef | 2002-10-19 22:02:21 +0000 | [diff] [blame] | 152 | PyMac_OSErrException = PyErr_NewException("MacOS.Error", NULL, NULL); |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 153 | return PyMac_OSErrException; |
| 154 | } |
| 155 | |
| 156 | /* Set a MAC-specific error from errno, and return NULL; return None if no error */ |
| 157 | PyObject * |
| 158 | PyErr_Mac(PyObject *eobj, int err) |
| 159 | { |
| 160 | char *msg; |
| 161 | PyObject *v; |
| 162 | |
| 163 | if (err == 0 && !PyErr_Occurred()) { |
| 164 | Py_INCREF(Py_None); |
| 165 | return Py_None; |
| 166 | } |
| 167 | if (err == -1 && PyErr_Occurred()) |
| 168 | return NULL; |
| 169 | msg = PyMac_StrError(err); |
| 170 | v = Py_BuildValue("(is)", err, msg); |
| 171 | PyErr_SetObject(eobj, v); |
| 172 | Py_DECREF(v); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | /* Call PyErr_Mac with PyMac_OSErrException */ |
| 177 | PyObject * |
| 178 | PyMac_Error(OSErr err) |
| 179 | { |
| 180 | return PyErr_Mac(PyMac_GetOSErrException(), err); |
| 181 | } |
| 182 | |
Jack Jansen | 697842f | 2001-09-10 22:00:39 +0000 | [diff] [blame] | 183 | |
| 184 | #if TARGET_API_MAC_OSX |
| 185 | OSErr |
| 186 | PyMac_GetFullPathname(FSSpec *fss, char *path, int len) |
| 187 | { |
| 188 | FSRef fsr; |
| 189 | OSErr err; |
| 190 | |
| 191 | *path = '\0'; |
| 192 | err = FSpMakeFSRef(fss, &fsr); |
| 193 | if ( err == fnfErr ) { |
| 194 | /* FSSpecs can point to non-existing files, fsrefs can't. */ |
| 195 | FSSpec fss2; |
| 196 | int tocopy; |
| 197 | |
| 198 | err = FSMakeFSSpec(fss->vRefNum, fss->parID, "", &fss2); |
| 199 | if ( err ) return err; |
| 200 | err = FSpMakeFSRef(&fss2, &fsr); |
| 201 | if ( err ) return err; |
| 202 | err = (OSErr)FSRefMakePath(&fsr, path, len-1); |
| 203 | if ( err ) return err; |
| 204 | /* This part is not 100% safe: we append the filename part, but |
| 205 | ** I'm not sure that we don't run afoul of the various 8bit |
| 206 | ** encodings here. Will have to look this up at some point... |
| 207 | */ |
| 208 | strcat(path, "/"); |
| 209 | tocopy = fss->name[0]; |
| 210 | if ( strlen(path) + tocopy >= len ) |
| 211 | tocopy = len - strlen(path) - 1; |
| 212 | if ( tocopy > 0 ) |
| 213 | strncat(path, fss->name+1, tocopy); |
| 214 | } else { |
| 215 | if ( err ) return err; |
| 216 | err = (OSErr)FSRefMakePath(&fsr, path, len); |
| 217 | if ( err ) return err; |
| 218 | } |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | #endif /* TARGET_API_MAC_OSX */ |
Jack Jansen | 21ed16a | 2002-08-02 14:11:24 +0000 | [diff] [blame] | 223 | |
| 224 | #ifdef WITH_NEXT_FRAMEWORK |
| 225 | /* |
| 226 | ** In a bundle, find a file "resourceName" of type "resourceType". Return the |
| 227 | ** full pathname in "resourceURLCstr". |
| 228 | */ |
| 229 | static int |
| 230 | locateResourcePy(CFStringRef resourceType, CFStringRef resourceName, char *resourceURLCStr, int length) |
| 231 | { |
| 232 | CFBundleRef mainBundle = NULL; |
| 233 | CFURLRef URL, absoluteURL; |
| 234 | CFStringRef filenameString, filepathString; |
| 235 | CFIndex size, i; |
| 236 | CFArrayRef arrayRef = NULL; |
| 237 | int success = 0; |
| 238 | |
| 239 | #if TARGET_API_MAC_OSX |
| 240 | CFURLPathStyle thePathStyle = kCFURLPOSIXPathStyle; |
| 241 | #else |
| 242 | CFURLPathStyle thePathStyle = kCFURLHFSPathStyle; |
| 243 | #endif |
| 244 | |
| 245 | /* Get a reference to our main bundle */ |
| 246 | mainBundle = CFBundleGetMainBundle(); |
| 247 | |
| 248 | /* If we are running inside a bundle, look through it. Otherwise, do nothing. */ |
| 249 | if (mainBundle) { |
| 250 | |
| 251 | /* Look for py files in the main bundle by type */ |
| 252 | arrayRef = CFBundleCopyResourceURLsOfType( mainBundle, |
| 253 | resourceType, |
| 254 | NULL ); |
| 255 | |
| 256 | /* See if there are any filename matches */ |
| 257 | size = CFArrayGetCount(arrayRef); |
| 258 | for (i = 0; i < size; i++) { |
| 259 | URL = CFArrayGetValueAtIndex(arrayRef, i); |
| 260 | filenameString = CFURLCopyLastPathComponent(URL); |
| 261 | if (CFStringCompare(filenameString, resourceName, 0) == kCFCompareEqualTo) { |
| 262 | /* We found a match, get the file's full path */ |
| 263 | absoluteURL = CFURLCopyAbsoluteURL(URL); |
| 264 | filepathString = CFURLCopyFileSystemPath(absoluteURL, thePathStyle); |
| 265 | CFRelease(absoluteURL); |
| 266 | |
| 267 | /* Copy the full path into the caller's character buffer */ |
| 268 | success = CFStringGetCString(filepathString, resourceURLCStr, length, |
| 269 | kCFStringEncodingMacRoman); |
| 270 | |
| 271 | CFRelease(filepathString); |
| 272 | } |
| 273 | CFRelease(filenameString); |
| 274 | } |
| 275 | CFRelease(arrayRef); |
| 276 | } |
| 277 | return success; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | ** iff we are running in a .app framework then we could be |
| 282 | ** the main program for an applet. In that case, return the |
| 283 | ** script filename for the applet. |
| 284 | ** Otherwise return NULL. |
| 285 | */ |
| 286 | char * |
| 287 | PyMac_GetAppletScriptFile(void) |
| 288 | { |
| 289 | static char scriptpath[1024]; |
| 290 | |
| 291 | /* First we see whether we have __rawmain__.py and run that if it |
| 292 | ** is there. This is used for applets that want sys.argv to be |
| 293 | ** unix-like: __rawmain__ will construct it (from the initial appleevent) |
| 294 | ** and then call __main__.py. |
| 295 | */ |
| 296 | if (locateResourcePy(CFSTR("py"), CFSTR("__rawmain__.py"), scriptpath, 1024)) { |
| 297 | return scriptpath; |
| 298 | } else if (locateResourcePy(CFSTR("pyc"), CFSTR("__rawmain__.pyc"), scriptpath, 1024)) { |
| 299 | return scriptpath; |
| 300 | } else if (locateResourcePy(CFSTR("py"), CFSTR("__main__.py"), scriptpath, 1024)) { |
| 301 | return scriptpath; |
| 302 | } else if (locateResourcePy(CFSTR("pyc"), CFSTR("__main__.pyc"), scriptpath, 1024)) { |
| 303 | return scriptpath; |
| 304 | } |
| 305 | return NULL; |
| 306 | } |
| 307 | |
| 308 | #endif |
| 309 | |
| 310 | |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 311 | /* Convert a 4-char string object argument to an OSType value */ |
| 312 | int |
| 313 | PyMac_GetOSType(PyObject *v, OSType *pr) |
| 314 | { |
| 315 | if (!PyString_Check(v) || PyString_Size(v) != 4) { |
| 316 | PyErr_SetString(PyExc_TypeError, |
| 317 | "OSType arg must be string of 4 chars"); |
| 318 | return 0; |
| 319 | } |
| 320 | memcpy((char *)pr, PyString_AsString(v), 4); |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | /* Convert an OSType value to a 4-char string object */ |
| 325 | PyObject * |
| 326 | PyMac_BuildOSType(OSType t) |
| 327 | { |
| 328 | return PyString_FromStringAndSize((char *)&t, 4); |
| 329 | } |
| 330 | |
| 331 | /* Convert an NumVersion value to a 4-element tuple */ |
| 332 | PyObject * |
| 333 | PyMac_BuildNumVersion(NumVersion t) |
| 334 | { |
| 335 | return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev); |
| 336 | } |
| 337 | |
| 338 | |
| 339 | /* Convert a Python string object to a Str255 */ |
| 340 | int |
| 341 | PyMac_GetStr255(PyObject *v, Str255 pbuf) |
| 342 | { |
| 343 | int len; |
| 344 | if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) { |
| 345 | PyErr_SetString(PyExc_TypeError, |
| 346 | "Str255 arg must be string of at most 255 chars"); |
| 347 | return 0; |
| 348 | } |
| 349 | pbuf[0] = len; |
| 350 | memcpy((char *)(pbuf+1), PyString_AsString(v), len); |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | /* Convert a Str255 to a Python string object */ |
| 355 | PyObject * |
| 356 | PyMac_BuildStr255(Str255 s) |
| 357 | { |
| 358 | if ( s == NULL ) { |
| 359 | PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL"); |
| 360 | return NULL; |
| 361 | } |
| 362 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 363 | } |
| 364 | |
| 365 | PyObject * |
| 366 | PyMac_BuildOptStr255(Str255 s) |
| 367 | { |
| 368 | if ( s == NULL ) { |
| 369 | Py_INCREF(Py_None); |
| 370 | return Py_None; |
| 371 | } |
| 372 | return PyString_FromStringAndSize((char *)&s[1], (int)s[0]); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | /* Convert a Python object to a Rect. |
| 378 | The object must be a (left, top, right, bottom) tuple. |
| 379 | (This differs from the order in the struct but is consistent with |
| 380 | the arguments to SetRect(), and also with STDWIN). */ |
| 381 | int |
| 382 | PyMac_GetRect(PyObject *v, Rect *r) |
| 383 | { |
| 384 | return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom); |
| 385 | } |
| 386 | |
| 387 | /* Convert a Rect to a Python object */ |
| 388 | PyObject * |
| 389 | PyMac_BuildRect(Rect *r) |
| 390 | { |
| 391 | return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom); |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /* Convert a Python object to a Point. |
| 396 | The object must be a (h, v) tuple. |
| 397 | (This differs from the order in the struct but is consistent with |
| 398 | the arguments to SetPoint(), and also with STDWIN). */ |
| 399 | int |
| 400 | PyMac_GetPoint(PyObject *v, Point *p) |
| 401 | { |
| 402 | return PyArg_Parse(v, "(hh)", &p->h, &p->v); |
| 403 | } |
| 404 | |
| 405 | /* Convert a Point to a Python object */ |
| 406 | PyObject * |
| 407 | PyMac_BuildPoint(Point p) |
| 408 | { |
| 409 | return Py_BuildValue("(hh)", p.h, p.v); |
| 410 | } |
| 411 | |
| 412 | |
| 413 | /* Convert a Python object to an EventRecord. |
| 414 | The object must be a (what, message, when, (v, h), modifiers) tuple. */ |
| 415 | int |
| 416 | PyMac_GetEventRecord(PyObject *v, EventRecord *e) |
| 417 | { |
| 418 | return PyArg_Parse(v, "(Hll(hh)H)", |
| 419 | &e->what, |
| 420 | &e->message, |
| 421 | &e->when, |
| 422 | &e->where.h, |
| 423 | &e->where.v, |
| 424 | &e->modifiers); |
| 425 | } |
| 426 | |
| 427 | /* Convert a Rect to an EventRecord object */ |
| 428 | PyObject * |
| 429 | PyMac_BuildEventRecord(EventRecord *e) |
| 430 | { |
| 431 | return Py_BuildValue("(hll(hh)h)", |
| 432 | e->what, |
| 433 | e->message, |
| 434 | e->when, |
| 435 | e->where.h, |
| 436 | e->where.v, |
| 437 | e->modifiers); |
| 438 | } |
| 439 | |
| 440 | /* Convert Python object to Fixed */ |
| 441 | int |
| 442 | PyMac_GetFixed(PyObject *v, Fixed *f) |
| 443 | { |
| 444 | double d; |
| 445 | |
| 446 | if( !PyArg_Parse(v, "d", &d)) |
| 447 | return 0; |
| 448 | *f = (Fixed)(d * 0x10000); |
| 449 | return 1; |
| 450 | } |
| 451 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 452 | /* Convert a Fixed to a Python object */ |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 453 | PyObject * |
| 454 | PyMac_BuildFixed(Fixed f) |
| 455 | { |
| 456 | double d; |
| 457 | |
| 458 | d = f; |
| 459 | d = d / 0x10000; |
| 460 | return Py_BuildValue("d", d); |
| 461 | } |
| 462 | |
| 463 | /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */ |
| 464 | int |
| 465 | PyMac_Getwide(PyObject *v, wide *rv) |
| 466 | { |
| 467 | if (PyInt_Check(v)) { |
| 468 | rv->hi = 0; |
| 469 | rv->lo = PyInt_AsLong(v); |
| 470 | if( rv->lo & 0x80000000 ) |
| 471 | rv->hi = -1; |
| 472 | return 1; |
| 473 | } |
| 474 | return PyArg_Parse(v, "(ll)", &rv->hi, &rv->lo); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | PyObject * |
| 479 | PyMac_Buildwide(wide *w) |
| 480 | { |
| 481 | if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) || |
| 482 | (w->hi == -1 && (w->lo & 0x80000000) ) ) |
| 483 | return PyInt_FromLong(w->lo); |
| 484 | return Py_BuildValue("(ll)", w->hi, w->lo); |
| 485 | } |
| 486 | |
| 487 | #ifdef USE_TOOLBOX_OBJECT_GLUE |
| 488 | /* |
| 489 | ** Glue together the toolbox objects. |
| 490 | ** |
| 491 | ** Because toolbox modules interdepend on each other, they use each others |
| 492 | ** object types, on MacOSX/MachO this leads to the situation that they |
| 493 | ** cannot be dynamically loaded (or they would all have to be lumped into |
| 494 | ** a single .so, but this would be bad for extensibility). |
| 495 | ** |
| 496 | ** This file defines wrappers for all the _New and _Convert functions, |
| 497 | ** which are the Py_BuildValue and PyArg_ParseTuple helpers. The wrappers |
| 498 | ** check an indirection function pointer, and if it isn't filled in yet |
| 499 | ** they import the appropriate module, whose init routine should fill in |
| 500 | ** the pointer. |
| 501 | */ |
| 502 | |
| 503 | #define GLUE_NEW(object, routinename, module) \ |
| 504 | PyObject *(*PyMacGluePtr_##routinename)(object); \ |
| 505 | \ |
| 506 | PyObject *routinename(object cobj) { \ |
| 507 | if (!PyMacGluePtr_##routinename) { \ |
| 508 | if (!PyImport_ImportModule(module)) return NULL; \ |
| 509 | if (!PyMacGluePtr_##routinename) { \ |
| 510 | PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \ |
| 511 | return NULL; \ |
| 512 | } \ |
| 513 | } \ |
| 514 | return (*PyMacGluePtr_##routinename)(cobj); \ |
| 515 | } |
| 516 | |
| 517 | #define GLUE_CONVERT(object, routinename, module) \ |
| 518 | int (*PyMacGluePtr_##routinename)(PyObject *, object *); \ |
| 519 | \ |
| 520 | int routinename(PyObject *pyobj, object *cobj) { \ |
| 521 | if (!PyMacGluePtr_##routinename) { \ |
| 522 | if (!PyImport_ImportModule(module)) return NULL; \ |
| 523 | if (!PyMacGluePtr_##routinename) { \ |
| 524 | PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \ |
| 525 | return NULL; \ |
| 526 | } \ |
| 527 | } \ |
| 528 | return (*PyMacGluePtr_##routinename)(pyobj, cobj); \ |
| 529 | } |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 530 | |
| 531 | GLUE_NEW(FSSpec *, PyMac_BuildFSSpec, "macfs") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 532 | GLUE_CONVERT(FSSpec, PyMac_GetFSSpec, "macfs") |
Jack Jansen | fabd00f | 2001-09-01 23:39:58 +0000 | [diff] [blame] | 533 | GLUE_NEW(FSRef *, PyMac_BuildFSRef, "macfs") |
| 534 | GLUE_CONVERT(FSRef, PyMac_GetFSRef, "macfs") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 535 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 536 | GLUE_NEW(AppleEvent *, AEDesc_New, "Carbon.AE") /* XXXX Why by address? */ |
Jack Jansen | b2a5772 | 2003-01-17 23:11:17 +0000 | [diff] [blame] | 537 | GLUE_NEW(AppleEvent *, AEDesc_NewBorrowed, "Carbon.AE") |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 538 | GLUE_CONVERT(AppleEvent, AEDesc_Convert, "Carbon.AE") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 539 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 540 | GLUE_NEW(Component, CmpObj_New, "Carbon.Cm") |
| 541 | GLUE_CONVERT(Component, CmpObj_Convert, "Carbon.Cm") |
| 542 | GLUE_NEW(ComponentInstance, CmpInstObj_New, "Carbon.Cm") |
| 543 | GLUE_CONVERT(ComponentInstance, CmpInstObj_Convert, "Carbon.Cm") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 544 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 545 | GLUE_NEW(ControlHandle, CtlObj_New, "Carbon.Ctl") |
| 546 | GLUE_CONVERT(ControlHandle, CtlObj_Convert, "Carbon.Ctl") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 547 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 548 | GLUE_NEW(DialogPtr, DlgObj_New, "Carbon.Dlg") |
| 549 | GLUE_CONVERT(DialogPtr, DlgObj_Convert, "Carbon.Dlg") |
| 550 | GLUE_NEW(DialogPtr, DlgObj_WhichDialog, "Carbon.Dlg") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 551 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 552 | GLUE_NEW(DragReference, DragObj_New, "Carbon.Drag") |
| 553 | GLUE_CONVERT(DragReference, DragObj_Convert, "Carbon.Drag") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 554 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 555 | GLUE_NEW(ListHandle, ListObj_New, "Carbon.List") |
| 556 | GLUE_CONVERT(ListHandle, ListObj_Convert, "Carbon.List") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 557 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 558 | GLUE_NEW(MenuHandle, MenuObj_New, "Carbon.Menu") |
| 559 | GLUE_CONVERT(MenuHandle, MenuObj_Convert, "Carbon.Menu") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 560 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 561 | GLUE_NEW(GrafPtr, GrafObj_New, "Carbon.Qd") |
| 562 | GLUE_CONVERT(GrafPtr, GrafObj_Convert, "Carbon.Qd") |
| 563 | GLUE_NEW(BitMapPtr, BMObj_New, "Carbon.Qd") |
| 564 | GLUE_CONVERT(BitMapPtr, BMObj_Convert, "Carbon.Qd") |
| 565 | GLUE_NEW(RGBColor *, QdRGB_New, "Carbon.Qd") /* XXXX Why? */ |
| 566 | GLUE_CONVERT(RGBColor, QdRGB_Convert, "Carbon.Qd") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 567 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 568 | GLUE_NEW(GWorldPtr, GWorldObj_New, "Carbon.Qdoffs") |
| 569 | GLUE_CONVERT(GWorldPtr, GWorldObj_Convert, "Carbon.Qdoffs") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 570 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 571 | GLUE_NEW(Track, TrackObj_New, "Carbon.Qt") |
| 572 | GLUE_CONVERT(Track, TrackObj_Convert, "Carbon.Qt") |
| 573 | GLUE_NEW(Movie, MovieObj_New, "Carbon.Qt") |
| 574 | GLUE_CONVERT(Movie, MovieObj_Convert, "Carbon.Qt") |
| 575 | GLUE_NEW(MovieController, MovieCtlObj_New, "Carbon.Qt") |
| 576 | GLUE_CONVERT(MovieController, MovieCtlObj_Convert, "Carbon.Qt") |
| 577 | GLUE_NEW(TimeBase, TimeBaseObj_New, "Carbon.Qt") |
| 578 | GLUE_CONVERT(TimeBase, TimeBaseObj_Convert, "Carbon.Qt") |
| 579 | GLUE_NEW(UserData, UserDataObj_New, "Carbon.Qt") |
| 580 | GLUE_CONVERT(UserData, UserDataObj_Convert, "Carbon.Qt") |
| 581 | GLUE_NEW(Media, MediaObj_New, "Carbon.Qt") |
| 582 | GLUE_CONVERT(Media, MediaObj_Convert, "Carbon.Qt") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 583 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 584 | GLUE_NEW(Handle, ResObj_New, "Carbon.Res") |
| 585 | GLUE_CONVERT(Handle, ResObj_Convert, "Carbon.Res") |
| 586 | GLUE_NEW(Handle, OptResObj_New, "Carbon.Res") |
| 587 | GLUE_CONVERT(Handle, OptResObj_Convert, "Carbon.Res") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 588 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 589 | GLUE_NEW(TEHandle, TEObj_New, "Carbon.TE") |
| 590 | GLUE_CONVERT(TEHandle, TEObj_Convert, "Carbon.TE") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 591 | |
Jack Jansen | 06bd323 | 2001-08-27 14:01:05 +0000 | [diff] [blame] | 592 | GLUE_NEW(WindowPtr, WinObj_New, "Carbon.Win") |
| 593 | GLUE_CONVERT(WindowPtr, WinObj_Convert, "Carbon.Win") |
| 594 | GLUE_NEW(WindowPtr, WinObj_WhichWindow, "Carbon.Win") |
Jack Jansen | 94bebc0 | 2001-08-08 13:17:31 +0000 | [diff] [blame] | 595 | |
Jack Jansen | 537a69f | 2001-11-05 14:39:22 +0000 | [diff] [blame] | 596 | GLUE_CONVERT(CFTypeRef, CFTypeRefObj_Convert, "Carbon.CF") |
| 597 | GLUE_NEW(CFTypeRef, CFTypeRefObj_New, "Carbon.CF") |
| 598 | |
| 599 | GLUE_CONVERT(CFStringRef, CFStringRefObj_Convert, "Carbon.CF") |
| 600 | GLUE_NEW(CFStringRef, CFStringRefObj_New, "Carbon.CF") |
| 601 | GLUE_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert, "Carbon.CF") |
| 602 | GLUE_NEW(CFMutableStringRef, CFMutableStringRefObj_New, "Carbon.CF") |
| 603 | |
| 604 | GLUE_CONVERT(CFArrayRef, CFArrayRefObj_Convert, "Carbon.CF") |
| 605 | GLUE_NEW(CFArrayRef, CFArrayRefObj_New, "Carbon.CF") |
| 606 | GLUE_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert, "Carbon.CF") |
| 607 | GLUE_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New, "Carbon.CF") |
| 608 | |
| 609 | GLUE_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert, "Carbon.CF") |
| 610 | GLUE_NEW(CFDictionaryRef, CFDictionaryRefObj_New, "Carbon.CF") |
| 611 | GLUE_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert, "Carbon.CF") |
| 612 | GLUE_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New, "Carbon.CF") |
| 613 | |
| 614 | GLUE_CONVERT(CFURLRef, CFURLRefObj_Convert, "Carbon.CF") |
| 615 | GLUE_CONVERT(CFURLRef, OptionalCFURLRefObj_Convert, "Carbon.CF") |
| 616 | GLUE_NEW(CFURLRef, CFURLRefObj_New, "Carbon.CF") |
| 617 | |
Michael W. Hudson | 5f26dda | 2002-11-09 14:47:18 +0000 | [diff] [blame] | 618 | #endif /* USE_TOOLBOX_OBJECT_GLUE */ |