Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Modules / waste / wastesupport.py
blobe983b154ce6914ae5145c374eb1ad9f24831b026
1 # This script generates a Python interface for an Apple Macintosh Manager.
2 # It uses the "bgen" package to generate C code.
3 # The function specifications are generated by scanning the mamager's header file,
4 # using the "scantools" package (customized for this particular manager).
6 import string
8 # Declarations that change for each manager
9 MACHEADERFILE = 'WASTE.h' # The Apple header file
10 MODNAME = 'waste' # The name of the module
11 OBJECTNAME = 'waste' # The basic name of the objects used here
12 KIND = 'Ptr' # Usually 'Ptr' or 'Handle'
14 # The following is *usually* unchanged but may still require tuning
15 MODPREFIX = MODNAME # The prefix for module-wide routines
16 OBJECTTYPE = "WEReference" # The C type used to represent them
17 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods
18 INPUTFILE = 'wastegen.py' # The file generated by the scanner
19 TYPETESTFILE = 'wastetypetest.py' # Another file generated by the scanner
20 OUTPUTFILE = "wastemodule.c" # The file generated by this program
22 from macsupport import *
24 # Create the type objects
25 WEReference = OpaqueByValueType("WEReference", "wasteObj")
26 ExistingWEReference = OpaqueByValueType("WEReference", "ExistingwasteObj")
27 WEObjectReference = OpaqueByValueType("WEObjectReference", "WEOObj")
28 StScrpHandle = OpaqueByValueType("StScrpHandle", "ResObj")
29 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
30 EventModifiers = Type("EventModifiers", "H")
31 FlavorType = OSTypeType("FlavorType")
32 WESelector = OSTypeType("WESelector")
34 OptHandle = OpaqueByValueType("Handle", "OptResObj")
35 OptSoupHandle = OpaqueByValueType("WESoupHandle", "OptResObj")
36 OptStScrpHandle = OpaqueByValueType("StScrpHandle", "OptResObj")
38 WEStyleMode = Type("WEStyleMode", "H")
39 WERulerMode = Type("WERulerMode", "l")
40 WEActionKind = Type("WEActionKind", "h")
41 WEAlignment = Type("WEAlignment", "B")
42 WEEdge = Type("WEEdge", "B")
43 WEDirection = Type("WEDirection", "h")
44 WESoupHandle = OpaqueByValueType("WESoupHandle", "ResObj")
45 WEFontTableHandle = OpaqueByValueType("WEFontTableHandle", "ResObj")
46 WEFontTableHandle
47 WERunInfo = OpaqueType("WERunInfo", "RunInfo")
49 AppleEvent = OpaqueType('AppleEvent', 'AEDesc')
50 AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc')
52 TextStyle = OpaqueType("TextStyle", "TextStyle")
53 TextStyle_ptr = TextStyle
54 LongPt = OpaqueType("LongPt", "LongPt")
55 LongPt_ptr = LongPt
56 LongRect = OpaqueType("LongRect", "LongRect")
57 LongRect_ptr = LongRect
59 TextEncodingVariant = Type("TextEncodingVariant", "l")
60 TextEncodingFormat = Type("TextEncodingFormat", "l")
62 includestuff = includestuff + """
63 #include <%s>""" % MACHEADERFILE + """
64 #include <WEObjectHandlers.h>
65 #include <WETabs.h>
66 #ifndef PyDoc_STR
67 #define PyDoc_STR(x) (x)
68 #endif
70 /* Exported by Qdmodule.c: */
71 extern PyObject *QdRGB_New(RGBColor *);
72 extern int QdRGB_Convert(PyObject *, RGBColor *);
74 /* Exported by AEModule.c: */
75 extern PyObject *AEDesc_New(AppleEvent *);
76 extern int AEDesc_Convert(PyObject *, AppleEvent *);
78 /* Forward declaration */
79 static PyObject *WEOObj_New(WEObjectReference);
80 static PyObject *ExistingwasteObj_New(WEReference);
83 ** Parse/generate TextStyle records
85 static PyObject *
86 TextStyle_New(TextStylePtr itself)
89 return Py_BuildValue("lllO&", (long)itself->tsFont, (long)itself->tsFace, (long)itself->tsSize, QdRGB_New,
90 &itself->tsColor);
93 static int
94 TextStyle_Convert(PyObject *v, TextStylePtr p_itself)
96 long font, face, size;
98 if( !PyArg_ParseTuple(v, "lllO&", &font, &face, &size, QdRGB_Convert, &p_itself->tsColor) )
99 return 0;
100 p_itself->tsFont = (short)font;
101 p_itself->tsFace = (Style)face;
102 p_itself->tsSize = (short)size;
103 return 1;
107 ** Parse/generate RunInfo records
109 static PyObject *
110 RunInfo_New(WERunInfo *itself)
113 return Py_BuildValue("llhhO&O&", itself->runStart, itself->runEnd, itself->runHeight,
114 itself->runAscent, TextStyle_New, &itself->runStyle, WEOObj_New, itself->runObject);
117 /* Conversion of long points and rects */
119 LongRect_Convert(PyObject *v, LongRect *r)
121 return PyArg_Parse(v, "(llll)", &r->left, &r->top, &r->right, &r->bottom);
124 PyObject *
125 LongRect_New(LongRect *r)
127 return Py_BuildValue("(llll)", r->left, r->top, r->right, r->bottom);
131 LongPt_Convert(PyObject *v, LongPt *p)
133 return PyArg_Parse(v, "(ll)", &p->h, &p->v);
136 PyObject *
137 LongPt_New(LongPt *p)
139 return Py_BuildValue("(ll)", p->h, p->v);
142 /* Stuff for the callbacks: */
143 static PyObject *callbackdict;
144 WENewObjectUPP upp_new_handler;
145 WEDisposeObjectUPP upp_dispose_handler;
146 WEDrawObjectUPP upp_draw_handler;
147 WEClickObjectUPP upp_click_handler;
149 static OSErr
150 any_handler(WESelector what, WEObjectReference who, PyObject *args, PyObject **rv)
152 FlavorType tp;
153 PyObject *key, *func;
155 if ( args == NULL ) return errAECorruptData;
157 tp = WEGetObjectType(who);
159 if( (key=Py_BuildValue("O&O&", PyMac_BuildOSType, tp, PyMac_BuildOSType, what)) == NULL)
160 return errAECorruptData;
161 if( (func = PyDict_GetItem(callbackdict, key)) == NULL ) {
162 Py_DECREF(key);
163 return errAEHandlerNotFound;
165 Py_INCREF(func);
166 *rv = PyEval_CallObject(func, args);
167 Py_DECREF(func);
168 Py_DECREF(key);
169 if ( *rv == NULL ) {
170 PySys_WriteStderr("--Exception in callback: ");
171 PyErr_Print();
172 return errAEReplyNotArrived;
174 return 0;
177 static pascal OSErr
178 my_new_handler(Point *objectSize, WEObjectReference objref)
180 PyObject *args=NULL, *rv=NULL;
181 OSErr err;
183 args=Py_BuildValue("(O&)", WEOObj_New, objref);
184 err = any_handler(weNewHandler, objref, args, &rv);
185 if (!err) {
186 if (!PyMac_GetPoint(rv, objectSize) )
187 err = errAECoercionFail;
189 if ( args ) {
190 Py_DECREF(args);
192 if ( rv ) {
193 Py_DECREF(rv);
195 return err;
198 static pascal OSErr
199 my_dispose_handler(WEObjectReference objref)
201 PyObject *args=NULL, *rv=NULL;
202 OSErr err;
204 args=Py_BuildValue("(O&)", WEOObj_New, objref);
205 err = any_handler(weDisposeHandler, objref, args, &rv);
206 if ( args ) {
207 Py_DECREF(args);
209 if ( rv ) {
210 Py_DECREF(rv);
212 return err;
215 static pascal OSErr
216 my_draw_handler(const Rect *destRect, WEObjectReference objref)
218 PyObject *args=NULL, *rv=NULL;
219 OSErr err;
221 args=Py_BuildValue("O&O&", PyMac_BuildRect, destRect, WEOObj_New, objref);
222 err = any_handler(weDrawHandler, objref, args, &rv);
223 if ( args ) {
224 Py_DECREF(args);
226 if ( rv ) {
227 Py_DECREF(rv);
229 return err;
232 static pascal Boolean
233 my_click_handler(Point hitPt, EventModifiers modifiers,
234 unsigned long clickTime, WEObjectReference objref)
236 PyObject *args=NULL, *rv=NULL;
237 int retvalue;
238 OSErr err;
240 args=Py_BuildValue("O&llO&", PyMac_BuildPoint, hitPt,
241 (long)modifiers, (long)clickTime, WEOObj_New, objref);
242 err = any_handler(weClickHandler, objref, args, &rv);
243 if (!err)
244 retvalue = PyInt_AsLong(rv);
245 else
246 retvalue = 0;
247 if ( args ) {
248 Py_DECREF(args);
250 if ( rv ) {
251 Py_DECREF(rv);
253 return retvalue;
258 finalstuff = finalstuff + """
259 /* Return the object corresponding to the window, or NULL */
261 PyObject *
262 ExistingwasteObj_New(w)
263 WEReference w;
265 PyObject *it = NULL;
267 if (w == NULL)
268 it = NULL;
269 else
270 WEGetInfo(weRefCon, (void *)&it, w);
271 if (it == NULL || ((wasteObject *)it)->ob_itself != w)
272 it = Py_None;
273 Py_INCREF(it);
274 return it;
278 class WEMethodGenerator(OSErrMethodGenerator):
279 """Similar to MethodGenerator, but has self as last argument"""
281 def parseArgumentList(self, args):
282 args, a0 = args[:-1], args[-1]
283 t0, n0, m0 = a0
284 if m0 != InMode:
285 raise ValueError, "method's 'self' must be 'InMode'"
286 self.itself = Variable(t0, "_self->ob_itself", SelfMode)
287 FunctionGenerator.parseArgumentList(self, args)
288 self.argumentList.append(self.itself)
292 class WEObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
293 def outputCheckNewArg(self):
294 Output("""if (itself == NULL) {
295 PyErr_SetString(waste_Error,"Cannot create null WE");
296 return NULL;
297 }""")
298 def outputInitStructMembers(self):
299 GlobalObjectDefinition.outputInitStructMembers(self)
300 Output("WESetInfo(weRefCon, (void *)&it, itself);")
301 def outputFreeIt(self, itselfname):
302 Output("WEDispose(%s);", itselfname)
304 class WEOObjectDefinition(PEP253Mixin, GlobalObjectDefinition):
305 def outputCheckNewArg(self):
306 Output("""if (itself == NULL) {
307 Py_INCREF(Py_None);
308 return Py_None;
309 }""")
311 variablestuff = """
312 callbackdict = PyDict_New();
313 if (callbackdict == NULL || PyDict_SetItemString(d, "callbacks", callbackdict) != 0)
314 return;
315 upp_new_handler = NewWENewObjectProc(my_new_handler);
316 upp_dispose_handler = NewWEDisposeObjectProc(my_dispose_handler);
317 upp_draw_handler = NewWEDrawObjectProc(my_draw_handler);
318 upp_click_handler = NewWEClickObjectProc(my_click_handler);
322 # From here on it's basically all boiler plate...
324 # Test types used for existence
325 ## execfile(TYPETESTFILE)
327 # Create the generator groups and link them
328 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff)
329 object = WEObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
330 object2 = WEOObjectDefinition("WEO", "WEOObj", "WEObjectReference")
331 module.addobject(object2)
332 module.addobject(object)
334 # Create the generator classes used to populate the lists
335 Function = OSErrFunctionGenerator
336 Method = WEMethodGenerator
337 Method2 = OSErrMethodGenerator
339 # Create and populate the lists
340 functions = []
341 methods = []
342 methods2 = []
343 execfile(INPUTFILE)
345 # A function written by hand:
346 stdhandlers_body = """
347 OSErr err;
348 // install the sample object handlers for pictures and sounds
349 #define kTypePicture 'PICT'
350 #define kTypeSound 'snd '
352 if ( !PyArg_ParseTuple(_args, "") ) return NULL;
354 if ((err = WEInstallObjectHandler(kTypePicture, weNewHandler,
355 (UniversalProcPtr) NewWENewObjectProc(HandleNewPicture), NULL)) != noErr)
356 goto cleanup;
358 if ((err = WEInstallObjectHandler(kTypePicture, weDisposeHandler,
359 (UniversalProcPtr) NewWEDisposeObjectProc(HandleDisposePicture), NULL)) != noErr)
360 goto cleanup;
362 if ((err = WEInstallObjectHandler(kTypePicture, weDrawHandler,
363 (UniversalProcPtr) NewWEDrawObjectProc(HandleDrawPicture), NULL)) != noErr)
364 goto cleanup;
366 if ((err = WEInstallObjectHandler(kTypeSound, weNewHandler,
367 (UniversalProcPtr) NewWENewObjectProc(HandleNewSound), NULL)) != noErr)
368 goto cleanup;
370 if ((err = WEInstallObjectHandler(kTypeSound, weDrawHandler,
371 (UniversalProcPtr) NewWEDrawObjectProc(HandleDrawSound), NULL)) != noErr)
372 goto cleanup;
374 if ((err = WEInstallObjectHandler(kTypeSound, weClickHandler,
375 (UniversalProcPtr) NewWEClickObjectProc(HandleClickSound), NULL)) != noErr)
376 goto cleanup;
377 Py_INCREF(Py_None);
378 _res = Py_None;
379 return _res;
381 cleanup:
382 return PyMac_Error(err);
385 inshandler_body = """
386 OSErr err;
387 FlavorType objectType;
388 WESelector selector;
389 PyObject *py_handler;
390 UniversalProcPtr handler;
391 WEReference we = NULL;
392 PyObject *key;
395 if ( !PyArg_ParseTuple(_args, "O&O&O|O&",
396 PyMac_GetOSType, &objectType,
397 PyMac_GetOSType, &selector,
398 &py_handler,
399 WEOObj_Convert, &we) ) return NULL;
401 if ( selector == weNewHandler ) handler = (UniversalProcPtr)upp_new_handler;
402 else if ( selector == weDisposeHandler ) handler = (UniversalProcPtr)upp_dispose_handler;
403 else if ( selector == weDrawHandler ) handler = (UniversalProcPtr)upp_draw_handler;
404 else if ( selector == weClickHandler ) handler = (UniversalProcPtr)upp_click_handler;
405 else return PyMac_Error(weUndefinedSelectorErr);
407 if ((key = Py_BuildValue("O&O&",
408 PyMac_BuildOSType, objectType,
409 PyMac_BuildOSType, selector)) == NULL )
410 return NULL;
412 PyDict_SetItem(callbackdict, key, py_handler);
414 err = WEInstallObjectHandler(objectType, selector, handler, we);
415 if ( err ) return PyMac_Error(err);
416 Py_INCREF(Py_None);
417 _res = Py_None;
418 return _res;
421 stdhand = ManualGenerator("STDObjectHandlers", stdhandlers_body)
422 inshand = ManualGenerator("WEInstallObjectHandler", inshandler_body)
425 # Tab hook handlers. Could be parsed from WETabs.h, but this is just as simple.
426 f = Method(OSErr, 'WEInstallTabHooks', (WEReference, 'we', InMode))
427 methods.append(f)
428 f = Method(OSErr, 'WERemoveTabHooks', (WEReference, 'we', InMode))
429 methods.append(f)
430 f = Method(Boolean, 'WEIsTabHooks', (WEReference, 'we', InMode))
431 methods.append(f)
432 f = Method(SInt16, 'WEGetTabSize', (WEReference, 'we', InMode))
433 methods.append(f)
434 f = Method(OSErr, 'WESetTabSize', (SInt16, 'tabWidth', InMode), (WEReference, 'we', InMode))
435 methods.append(f)
437 # add the populated lists to the generator groups
438 # (in a different wordl the scan program would generate this)
439 for f in functions: module.add(f)
440 module.add(stdhand)
441 module.add(inshand)
442 for f in methods: object.add(f)
443 for f in methods2: object2.add(f)
445 # generate output (open the output file as late as possible)
446 SetOutputFileName(OUTPUTFILE)
447 module.generate()