/Python/mactoolboxglue.c

http://unladen-swallow.googlecode.com/ · C · 474 lines · 344 code · 67 blank · 63 comment · 48 complexity · 57b7c6e01f02cf2d0a9174b0371dd76d MD5 · raw file

  1. /***********************************************************
  2. Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4. All Rights Reserved
  5. Permission to use, copy, modify, and distribute this software and its
  6. documentation for any purpose and without fee is hereby granted,
  7. provided that the above copyright notice appear in all copies and that
  8. both that copyright notice and this permission notice appear in
  9. supporting documentation, and that the names of Stichting Mathematisch
  10. Centrum or CWI not be used in advertising or publicity pertaining to
  11. distribution of the software without specific, written prior permission.
  12. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  13. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  14. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  15. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  18. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. ******************************************************************/
  20. #include "Python.h"
  21. #include "pymactoolbox.h"
  22. #include <arpa/inet.h> /* for ntohl, htonl */
  23. /* Like strerror() but for Mac OS error numbers */
  24. char *
  25. PyMac_StrError(int err)
  26. {
  27. static char buf[256];
  28. PyObject *m;
  29. PyObject *rv;
  30. m = PyImport_ImportModuleNoBlock("MacOS");
  31. if (!m) {
  32. if (Py_VerboseFlag)
  33. PyErr_Print();
  34. PyErr_Clear();
  35. rv = NULL;
  36. }
  37. else {
  38. rv = PyObject_CallMethod(m, "GetErrorString", "i", err);
  39. if (!rv)
  40. PyErr_Clear();
  41. }
  42. if (!rv) {
  43. buf[0] = '\0';
  44. }
  45. else {
  46. char *input = PyString_AsString(rv);
  47. if (!input) {
  48. PyErr_Clear();
  49. buf[0] = '\0';
  50. } else {
  51. strncpy(buf, input, sizeof(buf) - 1);
  52. buf[sizeof(buf) - 1] = '\0';
  53. }
  54. Py_DECREF(rv);
  55. }
  56. Py_XDECREF(m);
  57. return buf;
  58. }
  59. /* Exception object shared by all Mac specific modules for Mac OS errors */
  60. PyObject *PyMac_OSErrException;
  61. /* Initialize and return PyMac_OSErrException */
  62. PyObject *
  63. PyMac_GetOSErrException(void)
  64. {
  65. if (PyMac_OSErrException == NULL)
  66. PyMac_OSErrException = PyErr_NewException("MacOS.Error", NULL, NULL);
  67. return PyMac_OSErrException;
  68. }
  69. /* Set a MAC-specific error from errno, and return NULL; return None if no error */
  70. PyObject *
  71. PyErr_Mac(PyObject *eobj, int err)
  72. {
  73. char *msg;
  74. PyObject *v;
  75. if (err == 0 && !PyErr_Occurred()) {
  76. Py_INCREF(Py_None);
  77. return Py_None;
  78. }
  79. if (err == -1 && PyErr_Occurred())
  80. return NULL;
  81. msg = PyMac_StrError(err);
  82. v = Py_BuildValue("(is)", err, msg);
  83. PyErr_SetObject(eobj, v);
  84. Py_DECREF(v);
  85. return NULL;
  86. }
  87. /* Call PyErr_Mac with PyMac_OSErrException */
  88. PyObject *
  89. PyMac_Error(OSErr err)
  90. {
  91. return PyErr_Mac(PyMac_GetOSErrException(), err);
  92. }
  93. #ifndef __LP64__
  94. OSErr
  95. PyMac_GetFullPathname(FSSpec *fss, char *path, int len)
  96. {
  97. PyObject *fs, *exc;
  98. PyObject *rv = NULL;
  99. char *input;
  100. OSErr err = noErr;
  101. *path = '\0';
  102. fs = PyMac_BuildFSSpec(fss);
  103. if (!fs)
  104. goto error;
  105. rv = PyObject_CallMethod(fs, "as_pathname", "");
  106. if (!rv)
  107. goto error;
  108. input = PyString_AsString(rv);
  109. if (!input)
  110. goto error;
  111. strncpy(path, input, len - 1);
  112. path[len - 1] = '\0';
  113. Py_XDECREF(rv);
  114. Py_XDECREF(fs);
  115. return err;
  116. error:
  117. exc = PyErr_Occurred();
  118. if (exc && PyErr_GivenExceptionMatches(exc,
  119. PyMac_GetOSErrException())) {
  120. PyObject *args = PyObject_GetAttrString(exc, "args");
  121. if (args) {
  122. char *ignore;
  123. PyArg_ParseTuple(args, "is", &err, &ignore);
  124. Py_XDECREF(args);
  125. }
  126. }
  127. if (err == noErr)
  128. err = -1;
  129. PyErr_Clear();
  130. Py_XDECREF(rv);
  131. Py_XDECREF(fs);
  132. return err;
  133. }
  134. #endif /* !__LP64__ */
  135. /* Convert a 4-char string object argument to an OSType value */
  136. int
  137. PyMac_GetOSType(PyObject *v, OSType *pr)
  138. {
  139. uint32_t tmp;
  140. if (!PyString_Check(v) || PyString_Size(v) != 4) {
  141. PyErr_SetString(PyExc_TypeError,
  142. "OSType arg must be string of 4 chars");
  143. return 0;
  144. }
  145. memcpy((char *)&tmp, PyString_AsString(v), 4);
  146. *pr = (OSType)ntohl(tmp);
  147. return 1;
  148. }
  149. /* Convert an OSType value to a 4-char string object */
  150. PyObject *
  151. PyMac_BuildOSType(OSType t)
  152. {
  153. uint32_t tmp = htonl((uint32_t)t);
  154. return PyString_FromStringAndSize((char *)&tmp, 4);
  155. }
  156. /* Convert an NumVersion value to a 4-element tuple */
  157. PyObject *
  158. PyMac_BuildNumVersion(NumVersion t)
  159. {
  160. return Py_BuildValue("(hhhh)", t.majorRev, t.minorAndBugRev, t.stage, t.nonRelRev);
  161. }
  162. /* Convert a Python string object to a Str255 */
  163. int
  164. PyMac_GetStr255(PyObject *v, Str255 pbuf)
  165. {
  166. int len;
  167. if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
  168. PyErr_SetString(PyExc_TypeError,
  169. "Str255 arg must be string of at most 255 chars");
  170. return 0;
  171. }
  172. pbuf[0] = len;
  173. memcpy((char *)(pbuf+1), PyString_AsString(v), len);
  174. return 1;
  175. }
  176. /* Convert a Str255 to a Python string object */
  177. PyObject *
  178. PyMac_BuildStr255(Str255 s)
  179. {
  180. if ( s == NULL ) {
  181. PyErr_SetString(PyExc_SystemError, "Str255 pointer is NULL");
  182. return NULL;
  183. }
  184. return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
  185. }
  186. PyObject *
  187. PyMac_BuildOptStr255(Str255 s)
  188. {
  189. if ( s == NULL ) {
  190. Py_INCREF(Py_None);
  191. return Py_None;
  192. }
  193. return PyString_FromStringAndSize((char *)&s[1], (int)s[0]);
  194. }
  195. /* Convert a Python object to a Rect.
  196. The object must be a (left, top, right, bottom) tuple.
  197. (This differs from the order in the struct but is consistent with
  198. the arguments to SetRect(), and also with STDWIN). */
  199. int
  200. PyMac_GetRect(PyObject *v, Rect *r)
  201. {
  202. return PyArg_Parse(v, "(hhhh)", &r->left, &r->top, &r->right, &r->bottom);
  203. }
  204. /* Convert a Rect to a Python object */
  205. PyObject *
  206. PyMac_BuildRect(Rect *r)
  207. {
  208. return Py_BuildValue("(hhhh)", r->left, r->top, r->right, r->bottom);
  209. }
  210. /* Convert a Python object to a Point.
  211. The object must be a (h, v) tuple.
  212. (This differs from the order in the struct but is consistent with
  213. the arguments to SetPoint(), and also with STDWIN). */
  214. int
  215. PyMac_GetPoint(PyObject *v, Point *p)
  216. {
  217. return PyArg_Parse(v, "(hh)", &p->h, &p->v);
  218. }
  219. /* Convert a Point to a Python object */
  220. PyObject *
  221. PyMac_BuildPoint(Point p)
  222. {
  223. return Py_BuildValue("(hh)", p.h, p.v);
  224. }
  225. /* Convert a Python object to an EventRecord.
  226. The object must be a (what, message, when, (v, h), modifiers) tuple. */
  227. int
  228. PyMac_GetEventRecord(PyObject *v, EventRecord *e)
  229. {
  230. return PyArg_Parse(v, "(Hkk(hh)H)",
  231. &e->what,
  232. &e->message,
  233. &e->when,
  234. &e->where.h,
  235. &e->where.v,
  236. &e->modifiers);
  237. }
  238. /* Convert a Rect to an EventRecord object */
  239. PyObject *
  240. PyMac_BuildEventRecord(EventRecord *e)
  241. {
  242. return Py_BuildValue("(hll(hh)h)",
  243. e->what,
  244. e->message,
  245. e->when,
  246. e->where.h,
  247. e->where.v,
  248. e->modifiers);
  249. }
  250. /* Convert Python object to Fixed */
  251. int
  252. PyMac_GetFixed(PyObject *v, Fixed *f)
  253. {
  254. double d;
  255. if( !PyArg_Parse(v, "d", &d))
  256. return 0;
  257. *f = (Fixed)(d * 0x10000);
  258. return 1;
  259. }
  260. /* Convert a Fixed to a Python object */
  261. PyObject *
  262. PyMac_BuildFixed(Fixed f)
  263. {
  264. double d;
  265. d = f;
  266. d = d / 0x10000;
  267. return Py_BuildValue("d", d);
  268. }
  269. /* Convert wide to/from Python int or (hi, lo) tuple. XXXX Should use Python longs */
  270. int
  271. PyMac_Getwide(PyObject *v, wide *rv)
  272. {
  273. if (PyInt_Check(v)) {
  274. rv->hi = 0;
  275. rv->lo = PyInt_AsLong(v);
  276. if( rv->lo & 0x80000000 )
  277. rv->hi = -1;
  278. return 1;
  279. }
  280. return PyArg_Parse(v, "(kk)", &rv->hi, &rv->lo);
  281. }
  282. PyObject *
  283. PyMac_Buildwide(wide *w)
  284. {
  285. if ( (w->hi == 0 && (w->lo & 0x80000000) == 0) ||
  286. (w->hi == -1 && (w->lo & 0x80000000) ) )
  287. return PyInt_FromLong(w->lo);
  288. return Py_BuildValue("(ll)", w->hi, w->lo);
  289. }
  290. #ifdef USE_TOOLBOX_OBJECT_GLUE
  291. /*
  292. ** Glue together the toolbox objects.
  293. **
  294. ** Because toolbox modules interdepend on each other, they use each others
  295. ** object types, on MacOSX/MachO this leads to the situation that they
  296. ** cannot be dynamically loaded (or they would all have to be lumped into
  297. ** a single .so, but this would be bad for extensibility).
  298. **
  299. ** This file defines wrappers for all the _New and _Convert functions,
  300. ** which are the Py_BuildValue and PyArg_ParseTuple helpers. The wrappers
  301. ** check an indirection function pointer, and if it isn't filled in yet
  302. ** they import the appropriate module, whose init routine should fill in
  303. ** the pointer.
  304. */
  305. #define GLUE_NEW(object, routinename, module) \
  306. PyObject *(*PyMacGluePtr_##routinename)(object); \
  307. \
  308. PyObject *routinename(object cobj) { \
  309. if (!PyMacGluePtr_##routinename) { \
  310. if (!PyImport_ImportModule(module)) return NULL; \
  311. if (!PyMacGluePtr_##routinename) { \
  312. PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \
  313. return NULL; \
  314. } \
  315. } \
  316. return (*PyMacGluePtr_##routinename)(cobj); \
  317. }
  318. #define GLUE_CONVERT(object, routinename, module) \
  319. int (*PyMacGluePtr_##routinename)(PyObject *, object *); \
  320. \
  321. int routinename(PyObject *pyobj, object *cobj) { \
  322. if (!PyMacGluePtr_##routinename) { \
  323. if (!PyImport_ImportModule(module)) return 0; \
  324. if (!PyMacGluePtr_##routinename) { \
  325. PyErr_SetString(PyExc_ImportError, "Module did not provide routine: " module ": " #routinename); \
  326. return 0; \
  327. } \
  328. } \
  329. return (*PyMacGluePtr_##routinename)(pyobj, cobj); \
  330. }
  331. GLUE_NEW(FSSpec *, PyMac_BuildFSSpec, "Carbon.File")
  332. GLUE_CONVERT(FSSpec, PyMac_GetFSSpec, "Carbon.File")
  333. GLUE_NEW(FSRef *, PyMac_BuildFSRef, "Carbon.File")
  334. GLUE_CONVERT(FSRef, PyMac_GetFSRef, "Carbon.File")
  335. GLUE_NEW(AppleEvent *, AEDesc_New, "Carbon.AE") /* XXXX Why by address? */
  336. GLUE_NEW(AppleEvent *, AEDesc_NewBorrowed, "Carbon.AE")
  337. GLUE_CONVERT(AppleEvent, AEDesc_Convert, "Carbon.AE")
  338. GLUE_NEW(Component, CmpObj_New, "Carbon.Cm")
  339. GLUE_CONVERT(Component, CmpObj_Convert, "Carbon.Cm")
  340. GLUE_NEW(ComponentInstance, CmpInstObj_New, "Carbon.Cm")
  341. GLUE_CONVERT(ComponentInstance, CmpInstObj_Convert, "Carbon.Cm")
  342. GLUE_NEW(ControlHandle, CtlObj_New, "Carbon.Ctl")
  343. GLUE_CONVERT(ControlHandle, CtlObj_Convert, "Carbon.Ctl")
  344. GLUE_NEW(DialogPtr, DlgObj_New, "Carbon.Dlg")
  345. GLUE_CONVERT(DialogPtr, DlgObj_Convert, "Carbon.Dlg")
  346. GLUE_NEW(DialogPtr, DlgObj_WhichDialog, "Carbon.Dlg")
  347. GLUE_NEW(DragReference, DragObj_New, "Carbon.Drag")
  348. GLUE_CONVERT(DragReference, DragObj_Convert, "Carbon.Drag")
  349. GLUE_NEW(ListHandle, ListObj_New, "Carbon.List")
  350. GLUE_CONVERT(ListHandle, ListObj_Convert, "Carbon.List")
  351. GLUE_NEW(MenuHandle, MenuObj_New, "Carbon.Menu")
  352. GLUE_CONVERT(MenuHandle, MenuObj_Convert, "Carbon.Menu")
  353. GLUE_NEW(GrafPtr, GrafObj_New, "Carbon.Qd")
  354. GLUE_CONVERT(GrafPtr, GrafObj_Convert, "Carbon.Qd")
  355. GLUE_NEW(BitMapPtr, BMObj_New, "Carbon.Qd")
  356. GLUE_CONVERT(BitMapPtr, BMObj_Convert, "Carbon.Qd")
  357. GLUE_NEW(RGBColor *, QdRGB_New, "Carbon.Qd") /* XXXX Why? */
  358. GLUE_CONVERT(RGBColor, QdRGB_Convert, "Carbon.Qd")
  359. GLUE_NEW(GWorldPtr, GWorldObj_New, "Carbon.Qdoffs")
  360. GLUE_CONVERT(GWorldPtr, GWorldObj_Convert, "Carbon.Qdoffs")
  361. #ifndef __LP64__
  362. GLUE_NEW(Track, TrackObj_New, "Carbon.Qt")
  363. GLUE_CONVERT(Track, TrackObj_Convert, "Carbon.Qt")
  364. GLUE_NEW(Movie, MovieObj_New, "Carbon.Qt")
  365. GLUE_CONVERT(Movie, MovieObj_Convert, "Carbon.Qt")
  366. GLUE_NEW(MovieController, MovieCtlObj_New, "Carbon.Qt")
  367. GLUE_CONVERT(MovieController, MovieCtlObj_Convert, "Carbon.Qt")
  368. GLUE_NEW(TimeBase, TimeBaseObj_New, "Carbon.Qt")
  369. GLUE_CONVERT(TimeBase, TimeBaseObj_Convert, "Carbon.Qt")
  370. GLUE_NEW(UserData, UserDataObj_New, "Carbon.Qt")
  371. GLUE_CONVERT(UserData, UserDataObj_Convert, "Carbon.Qt")
  372. GLUE_NEW(Media, MediaObj_New, "Carbon.Qt")
  373. GLUE_CONVERT(Media, MediaObj_Convert, "Carbon.Qt")
  374. #endif /* !__LP64__ */
  375. GLUE_NEW(Handle, ResObj_New, "Carbon.Res")
  376. GLUE_CONVERT(Handle, ResObj_Convert, "Carbon.Res")
  377. GLUE_NEW(Handle, OptResObj_New, "Carbon.Res")
  378. GLUE_CONVERT(Handle, OptResObj_Convert, "Carbon.Res")
  379. GLUE_NEW(TEHandle, TEObj_New, "Carbon.TE")
  380. GLUE_CONVERT(TEHandle, TEObj_Convert, "Carbon.TE")
  381. GLUE_NEW(WindowPtr, WinObj_New, "Carbon.Win")
  382. GLUE_CONVERT(WindowPtr, WinObj_Convert, "Carbon.Win")
  383. GLUE_NEW(WindowPtr, WinObj_WhichWindow, "Carbon.Win")
  384. GLUE_CONVERT(CFTypeRef, CFObj_Convert, "Carbon.CF")
  385. GLUE_NEW(CFTypeRef, CFObj_New, "Carbon.CF")
  386. GLUE_CONVERT(CFTypeRef, CFTypeRefObj_Convert, "Carbon.CF")
  387. GLUE_NEW(CFTypeRef, CFTypeRefObj_New, "Carbon.CF")
  388. GLUE_CONVERT(CFStringRef, CFStringRefObj_Convert, "Carbon.CF")
  389. GLUE_NEW(CFStringRef, CFStringRefObj_New, "Carbon.CF")
  390. GLUE_CONVERT(CFMutableStringRef, CFMutableStringRefObj_Convert, "Carbon.CF")
  391. GLUE_NEW(CFMutableStringRef, CFMutableStringRefObj_New, "Carbon.CF")
  392. GLUE_CONVERT(CFArrayRef, CFArrayRefObj_Convert, "Carbon.CF")
  393. GLUE_NEW(CFArrayRef, CFArrayRefObj_New, "Carbon.CF")
  394. GLUE_CONVERT(CFMutableArrayRef, CFMutableArrayRefObj_Convert, "Carbon.CF")
  395. GLUE_NEW(CFMutableArrayRef, CFMutableArrayRefObj_New, "Carbon.CF")
  396. GLUE_CONVERT(CFDictionaryRef, CFDictionaryRefObj_Convert, "Carbon.CF")
  397. GLUE_NEW(CFDictionaryRef, CFDictionaryRefObj_New, "Carbon.CF")
  398. GLUE_CONVERT(CFMutableDictionaryRef, CFMutableDictionaryRefObj_Convert, "Carbon.CF")
  399. GLUE_NEW(CFMutableDictionaryRef, CFMutableDictionaryRefObj_New, "Carbon.CF")
  400. GLUE_CONVERT(CFURLRef, CFURLRefObj_Convert, "Carbon.CF")
  401. GLUE_CONVERT(CFURLRef, OptionalCFURLRefObj_Convert, "Carbon.CF")
  402. GLUE_NEW(CFURLRef, CFURLRefObj_New, "Carbon.CF")
  403. #endif /* USE_TOOLBOX_OBJECT_GLUE */