| 1 | n/a | # This script generates the Sound interface for Python. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # It execs the file sndgen.py which contain the function definitions |
|---|
| 4 | n/a | # (sndgen.py was generated by sndscan.py, scanning the <Sound.h> header file). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | from macsupport import * |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | # define our own function and module generators |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | class SndMixIn: pass |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | class SndFunction(SndMixIn, OSErrFunctionGenerator): pass |
|---|
| 14 | n/a | class SndMethod(SndMixIn, OSErrMethodGenerator): pass |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | # includestuff etc. are imported from macsupport |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | includestuff = includestuff + """ |
|---|
| 20 | n/a | #include <Carbon/Carbon.h> |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | initstuff = initstuff + """ |
|---|
| 24 | n/a | """ |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | # define types used for arguments (in addition to standard and macsupport types) |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | class SndChannelPtrType(OpaqueByValueType): |
|---|
| 30 | n/a | def declare(self, name): |
|---|
| 31 | n/a | # Initializing all SndChannelPtr objects to 0 saves |
|---|
| 32 | n/a | # special-casing NewSndChannel(), where it is formally an |
|---|
| 33 | n/a | # input-output parameter but we treat it as output-only |
|---|
| 34 | n/a | # (since Python users are not supposed to allocate memory) |
|---|
| 35 | n/a | Output("SndChannelPtr %s = 0;", name) |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | SndChannelPtr = SndChannelPtrType('SndChannelPtr', 'SndCh') |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | SndCommand = OpaqueType('SndCommand', 'SndCmd') |
|---|
| 40 | n/a | SndCommand_ptr = OpaqueType('SndCommand', 'SndCmd') |
|---|
| 41 | n/a | SndListHandle = OpaqueByValueType("SndListHandle", "ResObj") |
|---|
| 42 | n/a | SPBPtr = OpaqueByValueType("SPBPtr", "SPBObj") |
|---|
| 43 | n/a | ModalFilterUPP = FakeType("(ModalFilterUPP)0") |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | # |
|---|
| 46 | n/a | # NOTE: the following is pretty dangerous. For void pointers we pass buffer addresses |
|---|
| 47 | n/a | # but we have no way to check that the buffer is big enough. This is the same problem |
|---|
| 48 | n/a | # as in C, though (but Pythoneers may not be suspecting this...) |
|---|
| 49 | n/a | void_ptr = Type("void *", "w") |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | class SndCallBackType(InputOnlyType): |
|---|
| 52 | n/a | def __init__(self): |
|---|
| 53 | n/a | Type.__init__(self, 'PyObject*', 'O') |
|---|
| 54 | n/a | def getargsCheck(self, name): |
|---|
| 55 | n/a | Output("if (%s != Py_None && !PyCallable_Check(%s))", name, name) |
|---|
| 56 | n/a | OutLbrace() |
|---|
| 57 | n/a | Output('PyErr_SetString(PyExc_TypeError, "callback must be callable");') |
|---|
| 58 | n/a | Output("goto %s__error__;", name) |
|---|
| 59 | n/a | OutRbrace() |
|---|
| 60 | n/a | def passInput(self, name): |
|---|
| 61 | n/a | return "NewSndCallBackUPP(SndCh_UserRoutine)" |
|---|
| 62 | n/a | def cleanup(self, name): |
|---|
| 63 | n/a | # XXX This knows it is executing inside the SndNewChannel wrapper |
|---|
| 64 | n/a | Output("if (_res != NULL && %s != Py_None)", name) |
|---|
| 65 | n/a | OutLbrace() |
|---|
| 66 | n/a | Output("SndChannelObject *p = (SndChannelObject *)_res;") |
|---|
| 67 | n/a | Output("p->ob_itself->userInfo = (long)p;") |
|---|
| 68 | n/a | Output("Py_INCREF(%s);", name) |
|---|
| 69 | n/a | Output("p->ob_callback = %s;", name) |
|---|
| 70 | n/a | OutRbrace() |
|---|
| 71 | n/a | DedentLevel() |
|---|
| 72 | n/a | Output(" %s__error__: ;", name) |
|---|
| 73 | n/a | IndentLevel() |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | SndCallBackProcPtr = SndCallBackType() |
|---|
| 76 | n/a | SndCallBackUPP = SndCallBackProcPtr |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | SndCompletionProcPtr = FakeType('(SndCompletionProcPtr)0') # XXX |
|---|
| 79 | n/a | SndCompletionUPP = SndCompletionProcPtr |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | ##InOutBuf128 = FixedInputOutputBufferType(128) |
|---|
| 82 | n/a | StateBlock = StructInputOutputBufferType('StateBlock') |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | AudioSelectionPtr = FakeType('0') # XXX |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | ProcPtr = FakeType('0') # XXX |
|---|
| 87 | n/a | FilePlayCompletionUPP = FakeType('0') # XXX |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | SCStatus = StructOutputBufferType('SCStatus') |
|---|
| 90 | n/a | SMStatus = StructOutputBufferType('SMStatus') |
|---|
| 91 | n/a | CompressionInfo = StructOutputBufferType('CompressionInfo') |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | includestuff = includestuff + """ |
|---|
| 94 | n/a | /* Convert a SndCommand argument */ |
|---|
| 95 | n/a | static int |
|---|
| 96 | n/a | SndCmd_Convert(PyObject *v, SndCommand *pc) |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | int len; |
|---|
| 99 | n/a | pc->param1 = 0; |
|---|
| 100 | n/a | pc->param2 = 0; |
|---|
| 101 | n/a | if (PyTuple_Check(v)) { |
|---|
| 102 | n/a | if (PyArg_ParseTuple(v, "h|hl", &pc->cmd, &pc->param1, &pc->param2)) |
|---|
| 103 | n/a | return 1; |
|---|
| 104 | n/a | PyErr_Clear(); |
|---|
| 105 | n/a | return PyArg_ParseTuple(v, "Hhs#", &pc->cmd, &pc->param1, &pc->param2, &len); |
|---|
| 106 | n/a | } |
|---|
| 107 | n/a | return PyArg_Parse(v, "H", &pc->cmd); |
|---|
| 108 | n/a | } |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | static pascal void SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd); /* Forward */ |
|---|
| 111 | n/a | static pascal void SPB_completion(SPBPtr my_spb); /* Forward */ |
|---|
| 112 | n/a | """ |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | finalstuff = finalstuff + """ |
|---|
| 116 | n/a | /* Routine passed to Py_AddPendingCall -- call the Python callback */ |
|---|
| 117 | n/a | static int |
|---|
| 118 | n/a | SndCh_CallCallBack(void *arg) |
|---|
| 119 | n/a | { |
|---|
| 120 | n/a | SndChannelObject *p = (SndChannelObject *)arg; |
|---|
| 121 | n/a | PyObject *args; |
|---|
| 122 | n/a | PyObject *res; |
|---|
| 123 | n/a | args = Py_BuildValue("(O(hhl))", |
|---|
| 124 | n/a | p, p->ob_cmd.cmd, p->ob_cmd.param1, p->ob_cmd.param2); |
|---|
| 125 | n/a | res = PyEval_CallObject(p->ob_callback, args); |
|---|
| 126 | n/a | Py_DECREF(args); |
|---|
| 127 | n/a | if (res == NULL) |
|---|
| 128 | n/a | return -1; |
|---|
| 129 | n/a | Py_DECREF(res); |
|---|
| 130 | n/a | return 0; |
|---|
| 131 | n/a | } |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | /* Routine passed to NewSndChannel -- schedule a call to SndCh_CallCallBack */ |
|---|
| 134 | n/a | static pascal void |
|---|
| 135 | n/a | SndCh_UserRoutine(SndChannelPtr chan, SndCommand *cmd) |
|---|
| 136 | n/a | { |
|---|
| 137 | n/a | SndChannelObject *p = (SndChannelObject *)(chan->userInfo); |
|---|
| 138 | n/a | if (p->ob_callback != NULL) { |
|---|
| 139 | n/a | long A5 = SetA5(p->ob_A5); |
|---|
| 140 | n/a | p->ob_cmd = *cmd; |
|---|
| 141 | n/a | Py_AddPendingCall(SndCh_CallCallBack, (void *)p); |
|---|
| 142 | n/a | SetA5(A5); |
|---|
| 143 | n/a | } |
|---|
| 144 | n/a | } |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | /* SPB callbacks - Schedule callbacks to Python */ |
|---|
| 147 | n/a | static int |
|---|
| 148 | n/a | SPB_CallCallBack(void *arg) |
|---|
| 149 | n/a | { |
|---|
| 150 | n/a | SPBObject *p = (SPBObject *)arg; |
|---|
| 151 | n/a | PyObject *args; |
|---|
| 152 | n/a | PyObject *res; |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | if ( p->ob_thiscallback == 0 ) return 0; |
|---|
| 155 | n/a | args = Py_BuildValue("(O)", p); |
|---|
| 156 | n/a | res = PyEval_CallObject(p->ob_thiscallback, args); |
|---|
| 157 | n/a | p->ob_thiscallback = 0; |
|---|
| 158 | n/a | Py_DECREF(args); |
|---|
| 159 | n/a | if (res == NULL) |
|---|
| 160 | n/a | return -1; |
|---|
| 161 | n/a | Py_DECREF(res); |
|---|
| 162 | n/a | return 0; |
|---|
| 163 | n/a | } |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | static pascal void |
|---|
| 166 | n/a | SPB_completion(SPBPtr my_spb) |
|---|
| 167 | n/a | { |
|---|
| 168 | n/a | SPBObject *p = (SPBObject *)(my_spb->userLong); |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | if (p && p->ob_completion) { |
|---|
| 171 | n/a | long A5 = SetA5(p->ob_A5); |
|---|
| 172 | n/a | p->ob_thiscallback = p->ob_completion; /* Hope we cannot get two at the same time */ |
|---|
| 173 | n/a | Py_AddPendingCall(SPB_CallCallBack, (void *)p); |
|---|
| 174 | n/a | SetA5(A5); |
|---|
| 175 | n/a | } |
|---|
| 176 | n/a | } |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | """ |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | # create the module and object definition and link them |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | class SndObjectDefinition(PEP252Mixin, ObjectDefinition): |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | def outputStructMembers(self): |
|---|
| 186 | n/a | ObjectDefinition.outputStructMembers(self) |
|---|
| 187 | n/a | Output("/* Members used to implement callbacks: */") |
|---|
| 188 | n/a | Output("PyObject *ob_callback;") |
|---|
| 189 | n/a | Output("long ob_A5;"); |
|---|
| 190 | n/a | Output("SndCommand ob_cmd;") |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | def outputInitStructMembers(self): |
|---|
| 193 | n/a | ObjectDefinition.outputInitStructMembers(self) |
|---|
| 194 | n/a | Output("it->ob_callback = NULL;") |
|---|
| 195 | n/a | Output("it->ob_A5 = SetCurrentA5();"); |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | def outputCleanupStructMembers(self): |
|---|
| 198 | n/a | ObjectDefinition.outputCleanupStructMembers(self) |
|---|
| 199 | n/a | Output("Py_XDECREF(self->ob_callback);") |
|---|
| 200 | n/a | |
|---|
| 201 | n/a | def outputFreeIt(self, itselfname): |
|---|
| 202 | n/a | Output("SndDisposeChannel(%s, 1);", itselfname) |
|---|
| 203 | n/a | |
|---|
| 204 | n/a | def outputConvert(self): |
|---|
| 205 | n/a | pass # Not needed |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | # |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | class SpbObjectDefinition(PEP252Mixin, ObjectDefinition): |
|---|
| 210 | n/a | getsetlist = [ |
|---|
| 211 | n/a | ( |
|---|
| 212 | n/a | 'inRefNum', |
|---|
| 213 | n/a | 'return Py_BuildValue("l", self->ob_spb.inRefNum);', |
|---|
| 214 | n/a | 'return -1 + PyArg_Parse(v, "l", &self->ob_spb.inRefNum);', |
|---|
| 215 | n/a | None, |
|---|
| 216 | n/a | ), ( |
|---|
| 217 | n/a | 'count', |
|---|
| 218 | n/a | 'return Py_BuildValue("l", self->ob_spb.count);', |
|---|
| 219 | n/a | 'return -1 + PyArg_Parse(v, "l", &self->ob_spb.count);', |
|---|
| 220 | n/a | None |
|---|
| 221 | n/a | ), ( |
|---|
| 222 | n/a | 'milliseconds', |
|---|
| 223 | n/a | 'return Py_BuildValue("l", self->ob_spb.milliseconds);', |
|---|
| 224 | n/a | 'return -1 + PyArg_Parse(v, "l", &self->ob_spb.milliseconds);', |
|---|
| 225 | n/a | None, |
|---|
| 226 | n/a | ), ( |
|---|
| 227 | n/a | 'error', |
|---|
| 228 | n/a | 'return Py_BuildValue("h", self->ob_spb.error);', |
|---|
| 229 | n/a | None, |
|---|
| 230 | n/a | None |
|---|
| 231 | n/a | ), ( |
|---|
| 232 | n/a | 'completionRoutine', |
|---|
| 233 | n/a | None, |
|---|
| 234 | n/a | """self->ob_spb.completionRoutine = NewSICompletionUPP(SPB_completion); |
|---|
| 235 | n/a | self->ob_completion = v; |
|---|
| 236 | n/a | Py_INCREF(v); |
|---|
| 237 | n/a | return 0;""", |
|---|
| 238 | n/a | None, |
|---|
| 239 | n/a | )] |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | def outputStructMembers(self): |
|---|
| 242 | n/a | Output("/* Members used to implement callbacks: */") |
|---|
| 243 | n/a | Output("PyObject *ob_completion;") |
|---|
| 244 | n/a | Output("PyObject *ob_interrupt;") |
|---|
| 245 | n/a | Output("PyObject *ob_thiscallback;"); |
|---|
| 246 | n/a | Output("long ob_A5;") |
|---|
| 247 | n/a | Output("SPB ob_spb;") |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | def outputNew(self): |
|---|
| 250 | n/a | Output() |
|---|
| 251 | n/a | Output("%sPyObject *%s_New(void)", self.static, self.prefix) |
|---|
| 252 | n/a | OutLbrace() |
|---|
| 253 | n/a | Output("%s *it;", self.objecttype) |
|---|
| 254 | n/a | self.outputCheckNewArg() |
|---|
| 255 | n/a | Output("it = PyObject_NEW(%s, &%s);", self.objecttype, self.typename) |
|---|
| 256 | n/a | Output("if (it == NULL) return NULL;") |
|---|
| 257 | n/a | self.outputInitStructMembers() |
|---|
| 258 | n/a | Output("return (PyObject *)it;") |
|---|
| 259 | n/a | OutRbrace() |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | def outputInitStructMembers(self): |
|---|
| 262 | n/a | Output("it->ob_completion = NULL;") |
|---|
| 263 | n/a | Output("it->ob_interrupt = NULL;") |
|---|
| 264 | n/a | Output("it->ob_thiscallback = NULL;") |
|---|
| 265 | n/a | Output("it->ob_A5 = SetCurrentA5();") |
|---|
| 266 | n/a | Output("memset((char *)&it->ob_spb, 0, sizeof(it->ob_spb));") |
|---|
| 267 | n/a | Output("it->ob_spb.userLong = (long)it;") |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | def outputCleanupStructMembers(self): |
|---|
| 270 | n/a | ObjectDefinition.outputCleanupStructMembers(self) |
|---|
| 271 | n/a | Output("self->ob_spb.userLong = 0;") |
|---|
| 272 | n/a | Output("self->ob_thiscallback = 0;") |
|---|
| 273 | n/a | Output("Py_XDECREF(self->ob_completion);") |
|---|
| 274 | n/a | Output("Py_XDECREF(self->ob_interrupt);") |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | def outputConvert(self): |
|---|
| 277 | n/a | Output("%sint %s_Convert(PyObject *v, %s *p_itself)", self.static, self.prefix, self.itselftype) |
|---|
| 278 | n/a | OutLbrace() |
|---|
| 279 | n/a | self.outputCheckConvertArg() |
|---|
| 280 | n/a | Output("if (!%s_Check(v))", self.prefix) |
|---|
| 281 | n/a | OutLbrace() |
|---|
| 282 | n/a | Output('PyErr_SetString(PyExc_TypeError, "%s required");', self.name) |
|---|
| 283 | n/a | Output("return 0;") |
|---|
| 284 | n/a | OutRbrace() |
|---|
| 285 | n/a | Output("*p_itself = &((%s *)v)->ob_spb;", self.objecttype) |
|---|
| 286 | n/a | Output("return 1;") |
|---|
| 287 | n/a | OutRbrace() |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | sndobject = SndObjectDefinition('SndChannel', 'SndCh', 'SndChannelPtr') |
|---|
| 291 | n/a | spbobject = SpbObjectDefinition('SPB', 'SPBObj', 'SPBPtr') |
|---|
| 292 | n/a | spbgenerator = ManualGenerator("SPB", "_res = SPBObj_New(); return _res;") |
|---|
| 293 | n/a | module = MacModule('_Snd', 'Snd', includestuff, finalstuff, initstuff) |
|---|
| 294 | n/a | module.addobject(sndobject) |
|---|
| 295 | n/a | module.addobject(spbobject) |
|---|
| 296 | n/a | module.add(spbgenerator) |
|---|
| 297 | n/a | |
|---|
| 298 | n/a | |
|---|
| 299 | n/a | # create lists of functions and object methods |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | functions = [] |
|---|
| 302 | n/a | sndmethods = [] |
|---|
| 303 | n/a | |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | # populate the lists |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | execfile('sndgen.py') |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | # add the functions and methods to the module and object, respectively |
|---|
| 311 | n/a | |
|---|
| 312 | n/a | for f in functions: module.add(f) |
|---|
| 313 | n/a | for f in sndmethods: sndobject.add(f) |
|---|
| 314 | n/a | |
|---|
| 315 | n/a | |
|---|
| 316 | n/a | # generate output |
|---|
| 317 | n/a | |
|---|
| 318 | n/a | SetOutputFileName('_Sndmodule.c') |
|---|
| 319 | n/a | module.generate() |
|---|