home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Mac / macosmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  10.7 KB  |  486 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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. /* Macintosh OS-specific interface */
  26.  
  27. #include "Python.h"
  28.  
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <errno.h>
  32.  
  33. #include <OSUtils.h> /* for Set(Current)A5 */
  34. #include <Resources.h>
  35. #include <Sound.h>
  36.  
  37. /*----------------------------------------------------------------------*/
  38. /* General tools */
  39.  
  40. static PyObject *MacOS_Error; /* Exception MacOS.Error */
  41.  
  42. /* Set a MAC-specific error from errno, and return NULL; return None if no error */
  43. static PyObject * 
  44. Err(OSErr err)
  45. {
  46.     char buf[100];
  47.     PyObject *v;
  48.     if (err == 0) {
  49.         Py_INCREF(Py_None);
  50.         return Py_None;
  51.     }
  52.     sprintf(buf, "Mac OS error code %d", (int)err);
  53.     v = Py_BuildValue("(is)", (int)err, buf);
  54.     PyErr_SetObject(MacOS_Error, v);
  55.     Py_DECREF(v);
  56.     return NULL;
  57. }
  58.  
  59. /* Convert a ResType argument */
  60. static int
  61. GetOSType(PyObject *v, ResType *pr)
  62. {
  63.     if (!PyString_Check(v) || PyString_Size(v) != 4) {
  64.         PyErr_SetString(MacOS_Error,
  65.             "OSType arg must be string of 4 chars");
  66.         return 0;
  67.     }
  68.     memcpy((char *)pr, PyString_AsString(v), 4);
  69.     return 1;
  70. }
  71.  
  72. /* Convert a Str255 argument */
  73. static int
  74. GetStr255(PyObject *v, Str255 pbuf)
  75. {
  76.     int len;
  77.     if (!PyString_Check(v) || (len = PyString_Size(v)) > 255) {
  78.         PyErr_SetString(MacOS_Error,
  79.             "Str255 arg must be string of at most 255 chars");
  80.         return 0;
  81.     }
  82.     pbuf[0] = len;
  83.     memcpy((char *)(pbuf+1), PyString_AsString(v), len);
  84.     return 1;
  85. }
  86.  
  87. /*----------------------------------------------------------------------*/
  88. /* Resource objects */
  89.  
  90. typedef struct {
  91.     OB_HEAD
  92.     Handle h;
  93. } RsrcObject;
  94.  
  95. staticforward PyTypeObject RsrcType;
  96.  
  97. #define Rsrc_Check(r) ((r)->ob_type == &RsrcType)
  98.  
  99. static RsrcObject *
  100. Rsrc_FromHandle(Handle h)
  101. {
  102.     RsrcObject *r;
  103.     if (h == NULL)
  104.         return (RsrcObject *)Err(ResError());
  105.     r = PyObject_NEW(RsrcObject, &RsrcType);
  106.     if (r != NULL)
  107.         r->h = h;
  108.     return r;
  109. }
  110.  
  111. static void
  112. Rsrc_Dealloc(RsrcObject *r)
  113. {
  114.     PyMem_DEL(r);
  115. }
  116.  
  117. static PyObject *
  118. Rsrc_GetResInfo(RsrcObject *r, PyObject *args)
  119. {
  120.     short id;
  121.     ResType type;
  122.     Str255 name;
  123.     if (!PyArg_Parse(args, "()"))
  124.         return NULL;
  125.     GetResInfo(r->h, &id, &type, name);
  126.     return Py_BuildValue("(is#s#)",
  127.         (int)id, (char *)&type, 4, name+1, (int)name[0]);
  128. }
  129.  
  130. static PyMethodDef Rsrc_Methods[] = {
  131.     {"GetResInfo",    (PyCFunction)Rsrc_GetResInfo, 1},
  132.     {NULL,            NULL}         /* Sentinel */
  133. };
  134.  
  135. static PyObject *
  136. Rsrc_GetAttr(PyObject *r, char *name)
  137. {
  138.     return Py_FindMethod(Rsrc_Methods, r, name);
  139. }
  140.  
  141. static PyTypeObject RsrcType = {
  142.     PyObject_HEAD_INIT(&PyType_Type)
  143.     0,
  144.     "Resource",            /*tp_name*/
  145.     sizeof(RsrcObject),    /*tp_basicsize*/
  146.     0,            /*tp_itemsize*/
  147.     /* methods */
  148.     (destructor)Rsrc_Dealloc, /*tp_dealloc*/
  149.     0,            /*tp_print*/
  150.     (getattrfunc)Rsrc_GetAttr, /*tp_getattr*/
  151.     0,            /*tp_setattr*/
  152.     0,            /*tp_compare*/
  153.     0,            /*tp_repr*/
  154.     0,            /*tp_as_number*/
  155.     0,            /*tp_as_sequence*/
  156.     0,            /*tp_as_mapping*/
  157.     0,            /*tp_hash*/
  158. };
  159.  
  160. static PyObject *
  161. MacOS_GetResource(PyObject *self, PyObject *args)
  162. {
  163.     ResType rt;
  164.     int id;
  165.     Handle h;
  166.     if (!PyArg_Parse(args, "(O&i)", GetOSType, &rt, &id))
  167.         return NULL;
  168.     h = GetResource(rt, id);
  169.     return (PyObject *)Rsrc_FromHandle(h);
  170. }
  171.  
  172. static PyObject *
  173. MacOS_GetNamedResource(PyObject *self, PyObject *args)
  174. {
  175.     ResType rt;
  176.     Str255 name;
  177.     Handle h;
  178.     if (!PyArg_Parse(args, "(O&O&)", GetOSType, &rt, GetStr255, &name))
  179.         return NULL;
  180.     h = GetNamedResource(rt, name);
  181.     return (PyObject *)Rsrc_FromHandle(h);
  182. }
  183.  
  184. /*----------------------------------------------------------------------*/
  185. /* SoundChannel objects */
  186.  
  187. /* Convert a SndCommand argument */
  188. static int
  189. GetSndCommand(PyObject *v, SndCommand *pc)
  190. {
  191.     int len;
  192.     pc->param1 = 0;
  193.     pc->param2 = 0;
  194.     if (PyArg_Parse(v, "h", &pc->cmd))
  195.         return 1;
  196.     PyErr_Clear();
  197.     if (PyArg_Parse(v, "(h)", &pc->cmd))
  198.         return 1;
  199.     PyErr_Clear();
  200.     if (PyArg_Parse(v, "(hh)", &pc->cmd, &pc->param1))
  201.         return 1;
  202.     PyErr_Clear();
  203.     if (PyArg_Parse(v, "(hhl)",
  204.             &pc->cmd, &pc->param1, &pc->param2))
  205.         return 1;
  206.     PyErr_Clear();
  207.     if (PyArg_Parse(v, "(hhs#);SndCommand arg must be 1-3 ints or 2 ints + string",
  208.             &pc->cmd, &pc->param1, &pc->param2, &len))
  209.         return 1;
  210.     return 0;
  211. }
  212.  
  213. typedef struct {
  214.     OB_HEAD
  215.     SndChannelPtr chan;
  216. } SndChObject;
  217.  
  218. staticforward PyTypeObject SndChType;
  219.  
  220. #define SndCh_Check(s) ((s)->ob_type == &SndChType)
  221.  
  222. static SndChObject *
  223. SndCh_FromSndChannelPtr(SndChannelPtr chan)
  224. {
  225.     SndChObject *s = PyObject_NEW(SndChObject, &SndChType);
  226.     if (s != NULL)
  227.         s->chan = chan;
  228.     return s;
  229. }
  230.  
  231. static void
  232. SndCh_Cleanup(SndChObject *s, int quitNow)
  233. {
  234.     SndChannelPtr chan = s->chan;
  235.     if (chan != NULL) {
  236.         void *userInfo = (void *)chan->userInfo;
  237.         s->chan = NULL;
  238.         SndDisposeChannel(chan, quitNow);
  239.         if (userInfo != 0)
  240.             DEL(userInfo);
  241.     }
  242. }
  243.  
  244. static void
  245. SndCh_Dealloc(SndChObject *s)
  246. {
  247.     SndCh_Cleanup(s, 1);
  248.     PyMem_DEL(s);
  249. }
  250.  
  251. static PyObject *
  252. SndCh_DisposeChannel(SndChObject *s, PyObject *args)
  253. {
  254.     int quitNow = 1;
  255.     if (PyTuple_Size(args) > 0) {
  256.         if (!PyArg_Parse(args, "(i)", &quitNow))
  257.             return NULL;
  258.     }
  259.     SndCh_Cleanup(s, quitNow);
  260.     Py_INCREF(Py_None);
  261.     return Py_None;
  262. }
  263.  
  264. static int
  265. SndCh_OK(SndChObject *s)
  266. {
  267.     if (s->chan == NULL) {
  268.         PyErr_SetString(MacOS_Error, "channel is closed");
  269.         return 0;
  270.     }
  271.     return 1;
  272. }
  273.  
  274. static PyObject *
  275. SndCh_SndPlay(SndChObject *s, PyObject *args)
  276. {
  277.     RsrcObject *r;
  278.     int async = 0;
  279.     if (!PyArg_Parse(args, "(O!)", RsrcType, &r)) {
  280.         PyErr_Clear();
  281.         if (!PyArg_Parse(args, "(O&i)", RsrcType, &r, &async))
  282.             return NULL;
  283.     }
  284.     if (!SndCh_OK(s))
  285.         return NULL;
  286.     SndPlay(s->chan, r->h, async);
  287.     Py_INCREF(Py_None);
  288.     return Py_None;
  289. }
  290.  
  291. static PyObject *
  292. SndCh_SndDoCommand(SndChObject *s, PyObject *args)
  293. {
  294.     SndCommand c;
  295.     int noWait = 0;
  296.     OSErr err;
  297.     if (!PyArg_Parse(args, "(O&)", GetSndCommand, &c)) {
  298.         PyErr_Clear();
  299.         if (!PyArg_Parse(args, "(O&i)", GetSndCommand, &c, &noWait))
  300.             return NULL;
  301.     }
  302.     if (!SndCh_OK(s))
  303.         return NULL;
  304.     err = SndDoCommand(s->chan, &c, noWait);
  305.     return Err(err);
  306. }
  307.  
  308. static PyObject *
  309. SndCh_SndDoImmediate(SndChObject *s, PyObject *args)
  310. {
  311.     SndCommand c;
  312.     OSErr err;
  313.     if (!PyArg_Parse(args, "(O&)", GetSndCommand, &c))
  314.         return 0;
  315.     if (!SndCh_OK(s))
  316.         return NULL;
  317.     err = SndDoImmediate(s->chan, &c);
  318.     return Err(err);
  319. }
  320.  
  321. static PyMethodDef SndCh_Methods[] = {
  322.     {"close",                (PyCFunction)SndCh_DisposeChannel, 1},
  323.     {"SndDisposeChannel",    (PyCFunction)SndCh_DisposeChannel, 1},
  324.     {"SndPlay",                (PyCFunction)SndCh_SndPlay, 1},
  325.     {"SndDoCommand",        (PyCFunction)SndCh_SndDoCommand, 1},
  326.     {"SndDoImmediate",        (PyCFunction)SndCh_SndDoImmediate, 1},
  327.     {NULL,                    NULL}         /* Sentinel */
  328. };
  329.  
  330. static PyObject *
  331. SndCh_GetAttr(PyObject *s, char *name)
  332. {
  333.     return Py_FindMethod(SndCh_Methods, s, name);
  334. }
  335.  
  336. static PyTypeObject SndChType = {
  337.     PyObject_HEAD_INIT(&PyType_Type)
  338.     0,
  339.     "SoundChannel",            /*tp_name*/
  340.     sizeof(SndChObject),    /*tp_basicsize*/
  341.     0,            /*tp_itemsize*/
  342.     /* methods */
  343.     (destructor)SndCh_Dealloc, /*tp_dealloc*/
  344.     0,            /*tp_print*/
  345.     (getattrfunc)SndCh_GetAttr, /*tp_getattr*/
  346.     0,            /*tp_setattr*/
  347.     0,            /*tp_compare*/
  348.     0,            /*tp_repr*/
  349.     0,            /*tp_as_number*/
  350.     0,            /*tp_as_sequence*/
  351.     0,            /*tp_as_mapping*/
  352.     0,            /*tp_hash*/
  353. };
  354.  
  355. /*----------------------------------------------------------------------*/
  356. /* Module */
  357.  
  358. typedef struct {
  359.     long A5;
  360.     PyObject *callback;
  361.     PyObject *channel;
  362.     SndCommand cmd;
  363. } cbinfo;
  364.  
  365. static int
  366. MySafeCallback(arg)
  367.     void *arg;
  368. {
  369.     cbinfo *p = (cbinfo *)arg;
  370.     PyObject *args;
  371.     PyObject *res;
  372.     args = Py_BuildValue("(O(hhl))",
  373.                          p->channel, p->cmd.cmd, p->cmd.param1, p->cmd.param2);
  374.     res = PyEval_CallObject(p->callback, args);
  375.     Py_DECREF(args);
  376.     if (res == NULL)
  377.         return -1;
  378.     DECREF(res);
  379.     return 0;
  380. }
  381.  
  382. static pascal void
  383. MyUserRoutine(SndChannelPtr chan, SndCommand cmd)
  384. {
  385.     cbinfo *p = (cbinfo *)chan->userInfo;
  386.     long A5 = SetA5(p->A5);
  387.     p->cmd = cmd;
  388.     Py_AddPendingCall(MySafeCallback, (void *)p);
  389.     SetA5(A5);
  390. }
  391.  
  392. static PyObject *
  393. MacOS_SndNewChannel(PyObject *self, PyObject *args)
  394. {
  395.     SndChannelPtr chan;
  396.     short synth;
  397.     long init = 0;
  398.     PyObject *callback = NULL;
  399.     cbinfo *p = NULL;
  400.     SndCallBackProcPtr userroutine = 0;
  401.     OSErr err;
  402.     PyObject *res;
  403.     if (!PyArg_Parse(args, "(h)", &synth)) {
  404.         PyErr_Clear();
  405.         if (!PyArg_Parse(args, "(hl)", &synth, &init)) {
  406.             PyErr_Clear();
  407.             if (!PyArg_Parse(args, "(hlO)", &synth, &init, &callback))
  408.                 return NULL;
  409.         }
  410.     }
  411.     if (callback != NULL) {
  412.         p = NEW(cbinfo, 1);
  413.         if (p == NULL)
  414.             return PyErr_NoMemory();
  415.         p->A5 = SetCurrentA5();
  416.         p->callback = callback;
  417.         userroutine = MyUserRoutine;
  418.     }
  419.     chan = NULL;
  420.     err = SndNewChannel(&chan, synth, init, userroutine);
  421.     if (err) {
  422.         if (p)
  423.             DEL(p);
  424.         return Err(err);
  425.     }
  426.     res = (PyObject *)SndCh_FromSndChannelPtr(chan);
  427.     if (res == NULL) {
  428.         SndDisposeChannel(chan, 1);
  429.         DEL(p);
  430.     }
  431.     else {
  432.         chan->userInfo = (long)p;
  433.         p->channel = res;
  434.     }
  435.     return res;
  436. }
  437.  
  438. static PyObject *
  439. MacOS_SndPlay(PyObject *self, PyObject *args)
  440. {
  441.     RsrcObject *r;
  442.     OSErr err;
  443.     if (!PyArg_Parse(args, "(O!)", &RsrcType, &r))
  444.         return NULL;
  445.     err = SndPlay((SndChannelPtr)NULL, r->h, 0);
  446.     return Err(err);
  447. }
  448.  
  449. static PyObject *
  450. MacOS_SndControl(PyObject *self, PyObject *args)
  451. {
  452.     int id;
  453.     SndCommand c;
  454.     OSErr err;
  455.     if (!PyArg_Parse(args, "(iO&)", &id, GetSndCommand, &c))
  456.         return NULL;
  457.     err = SndControl(id, &c);
  458.     if (err)
  459.         return Err(err);
  460.     return Py_BuildValue("(hhl)", c.cmd, c.param1, c.param2);
  461. }
  462.  
  463. static PyMethodDef MacOS_Methods[] = {
  464.     {"GetResource",            MacOS_GetResource, 1},
  465.     {"GetNamedResource",    MacOS_GetNamedResource, 1},
  466.     {"SndNewChannel",        MacOS_SndNewChannel, 1},
  467.     {"SndPlay",                MacOS_SndPlay, 1},
  468.     {"SndControl",            MacOS_SndControl, 1},
  469.     {NULL,                    NULL}         /* Sentinel */
  470. };
  471.  
  472.  
  473. void
  474. MacOS_Init()
  475. {
  476.     PyObject *m, *d;
  477.     
  478.     m = Py_InitModule("MacOS", MacOS_Methods);
  479.     d = PyModule_GetDict(m);
  480.     
  481.     /* Initialize MacOS.Error exception */
  482.     MacOS_Error = PyString_FromString("MacOS.Error");
  483.     if (MacOS_Error == NULL || PyDict_SetItemString(d, "Error", MacOS_Error) != 0)
  484.         Py_FatalError("can't define MacOS.Error");
  485. }
  486.