1 # This script generates the Sound interface for Python.
2 # It uses the "bgen" package to generate C code.
3 # It execs the file sndgen.py which contain the function definitions
4 # (sndgen.py was generated by sndscan.py, scanning the <Sound.h> header file).
6 from macsupport
import *
9 # define our own function and module generators
13 class SndFunction(SndMixIn
, OSErrFunctionGenerator
): pass
14 class SndMethod(SndMixIn
, OSErrMethodGenerator
): pass
17 # includestuff etc. are imported from macsupport
19 includestuff
= includestuff
+ """
22 #ifndef HAVE_UNIVERSAL_HEADERS
23 #define SndCallBackUPP ProcPtr
24 #define NewSndCallBackProc(x) ((SndCallBackProcPtr)(x))
25 #define SndListHandle Handle
29 initstuff
= initstuff
+ """
33 # define types used for arguments (in addition to standard and macsupport types)
35 class SndChannelPtrType(OpaqueByValueType
):
36 def declare(self
, name
):
37 # Initializing all SndChannelPtr objects to 0 saves
38 # special-casing NewSndChannel(), where it is formally an
39 # input-output parameter but we treat it as output-only
40 # (since Python users are not supposed to allocate memory)
41 Output("SndChannelPtr %s = 0;", name
)
43 SndChannelPtr
= SndChannelPtrType('SndChannelPtr', 'SndCh')
45 SndCommand
= OpaqueType('SndCommand', 'SndCmd')
46 SndCommand_ptr
= OpaqueType('SndCommand', 'SndCmd')
47 SndListHandle
= OpaqueByValueType("SndListHandle", "ResObj")
49 class SndCallBackType(InputOnlyType
):
51 Type
.__init
__(self
, 'PyObject*', 'O')
52 def getargsCheck(self
, name
):
53 Output("if (%s != Py_None && !PyCallable_Check(%s))", name
, name
)
55 Output('PyErr_SetString(PyExc_TypeError, "callback must be callable");')
56 Output("goto %s__error__;", name
)
58 def passInput(self
, name
):
59 return "NewSndCallBackProc(SndCh_UserRoutine)"
60 def cleanup(self
, name
):
61 # XXX This knows it is executing inside the SndNewChannel wrapper
62 Output("if (_res != NULL && %s != Py_None)", name
)
64 Output("SndChannelObject *p = (SndChannelObject *)_res;")
65 Output("p->ob_itself->userInfo = (long)p;")
66 Output("Py_INCREF(%s);", name
)
67 Output("p->ob_callback = %s;", name
)
70 Output(" %s__error__: ;", name
)
73 SndCallBackProcPtr
= SndCallBackType()
75 SndCompletionProcPtr
= FakeType('(SndCompletionProcPtr)0') # XXX
77 NumVersion
= OpaqueByValueType('NumVersion', 'NumVer')
79 InOutBuf128
= FixedInputOutputBufferType(128)
81 AudioSelectionPtr
= FakeType('0') # XXX
83 ProcPtr
= FakeType('0') # XXX
85 SCStatus
= StructOutputBufferType('SCStatus')
86 SMStatus
= StructOutputBufferType('SMStatus')
88 includestuff
= includestuff
+ """
89 #include <OSUtils.h> /* for Set(Current)A5 */
91 /* Create a SndCommand object (an (int, int, int) tuple) */
93 SndCmd_New(SndCommand *pc)
95 return Py_BuildValue("hhl", pc->cmd, pc->param1, pc->param2);
98 /* Convert a SndCommand argument */
100 SndCmd_Convert(PyObject *v, SndCommand *pc)
105 if (PyTuple_Check(v)) {
106 if (PyArg_ParseTuple(v, "h|hl", &pc->cmd, &pc->param1, &pc->param2))
109 return PyArg_ParseTuple(v, "hhs#", &pc->cmd, &pc->param1, &pc->param2, &len);
111 return PyArg_Parse(v, "h", &pc->cmd);
114 /* Create a NumVersion object (a quintuple of integers) */
116 NumVer_New(NumVersion nv)
118 return Py_BuildValue("iiiii",
124 (nv.minorAndBugRev>>4) & 0xf,
125 nv.minorAndBugRev & 0xf,
131 static pascal void SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd); /* Forward */
135 finalstuff
= finalstuff
+ """
136 /* Routine passed to Py_AddPendingCall -- call the Python callback */
138 SndCh_CallCallBack(arg)
141 SndChannelObject *p = (SndChannelObject *)arg;
144 args = Py_BuildValue("(O(hhl))",
145 p, p->ob_cmd.cmd, p->ob_cmd.param1, p->ob_cmd.param2);
146 res = PyEval_CallObject(p->ob_callback, args);
154 /* Routine passed to NewSndChannel -- schedule a call to SndCh_CallCallBack */
156 SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd)
158 SndChannelObject *p = (SndChannelObject *)(chan->userInfo);
159 if (p->ob_callback != NULL) {
160 long A5 = SetA5(p->ob_A5);
162 Py_AddPendingCall(SndCh_CallCallBack, (void *)p);
169 # create the module and object definition and link them
171 class SndObjectDefinition(ObjectDefinition
):
173 def outputStructMembers(self
):
174 ObjectDefinition
.outputStructMembers(self
)
175 Output("/* Members used to implement callbacks: */")
176 Output("PyObject *ob_callback;")
177 Output("long ob_A5;");
178 Output("SndCommand ob_cmd;")
180 def outputInitStructMembers(self
):
181 ObjectDefinition
.outputInitStructMembers(self
)
182 Output("it->ob_callback = NULL;")
183 Output("it->ob_A5 = SetCurrentA5();");
185 def outputCleanupStructMembers(self
):
186 ObjectDefinition
.outputCleanupStructMembers(self
)
187 Output("Py_XDECREF(self->ob_callback);")
189 def outputFreeIt(self
, itselfname
):
190 Output("SndDisposeChannel(%s, 1);", itselfname
)
193 sndobject
= SndObjectDefinition('SndChannel', 'SndCh', 'SndChannelPtr')
194 module
= MacModule('Snd', 'Snd', includestuff
, finalstuff
, initstuff
)
195 module
.addobject(sndobject
)
198 # create lists of functions and object methods
206 execfile('sndgen.py')
209 # add the functions and methods to the module and object, respectively
211 for f
in functions
: module
.add(f
)
212 for f
in sndmethods
: sndobject
.add(f
)
217 SetOutputFileName('Sndmodule.c')