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).
8 # Declarations that change for each manager
9 MACHEADERFILE
= 'QDOffscreen.h' # The Apple header file
10 MODNAME
= '_Qdoffs' # The name of the module
11 OBJECTNAME
= 'GWorld' # The basic name of the objects used here
13 # The following is *usually* unchanged but may still require tuning
14 MODPREFIX
= 'Qdoffs' # The prefix for module-wide routines
15 OBJECTTYPE
= OBJECTNAME
+ 'Ptr' # The C type used to represent them
16 OBJECTPREFIX
= OBJECTNAME
+ 'Obj' # The prefix for object methods
17 INPUTFILE
= string
.lower(MODPREFIX
) + 'gen.py' # The file generated by the scanner
18 #EDITFILE = string.lower(MODPREFIX) + 'edit.py' # The manual definitions
19 OUTPUTFILE
= MODNAME
+ "module.c" # The file generated by this program
21 from macsupport
import *
23 # Create the type objects
25 GWorldPtr
= OpaqueByValueType(OBJECTTYPE
, OBJECTPREFIX
)
26 GWorldFlags
= Type("GWorldFlags", "l")
27 GDHandle
= OpaqueByValueType("GDHandle", "ResObj")
28 OptGDHandle
= OpaqueByValueType("GDHandle", "OptResObj")
29 CTabHandle
= OpaqueByValueType("CTabHandle", "OptResObj")
30 PixPatHandle
= OpaqueByValueType("PixPatHandle", "ResObj")
31 PixMapHandle
= OpaqueByValueType("PixMapHandle", "ResObj")
32 CGrafPtr
= OpaqueByValueType("CGrafPtr", "GrafObj")
33 GrafPtr
= OpaqueByValueType("GrafPtr", "GrafObj")
34 QDErr
= OSErrType("QDErr", 'h')
36 includestuff
= includestuff
+ """
37 #include <Carbon/Carbon.h>
39 #ifdef USE_TOOLBOX_OBJECT_GLUE
40 extern PyObject *_GWorldObj_New(GWorldPtr);
41 extern int _GWorldObj_Convert(PyObject *, GWorldPtr *);
43 #define GWorldObj_New _GWorldObj_New
44 #define GWorldObj_Convert _GWorldObj_Convert
47 #define as_GrafPtr(gworld) ((GrafPtr)(gworld))
51 initstuff
= initstuff
+ """
52 PyMac_INIT_TOOLBOX_OBJECT_NEW(GWorldPtr, GWorldObj_New);
53 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(GWorldPtr, GWorldObj_Convert);
56 class MyObjectDefinition(PEP253Mixin
, GlobalObjectDefinition
):
57 # XXXX Should inherit from GrafPtr?
58 def outputCheckNewArg(self
):
59 Output("if (itself == NULL) return PyMac_Error(resNotFound);")
60 ## def outputInitStructMembers(self):
61 ## GlobalObjectDefinition.outputInitStructMembers(self)
62 ## Output("SetWRefCon(itself, (long)it);")
63 ## def outputCheckConvertArg(self):
64 ## OutLbrace("if (DlgObj_Check(v))")
65 ## Output("*p_itself = ((WindowObject *)v)->ob_itself;")
66 ## Output("return 1;")
69 ## if (v == Py_None) { *p_itself = NULL; return 1; }
70 ## if (PyInt_Check(v)) { *p_itself = (WindowPtr)PyInt_AsLong(v); return 1; }
72 def outputFreeIt(self
, itselfname
):
73 Output("DisposeGWorld(%s);", itselfname
)
74 # From here on it's basically all boiler plate...
76 # Create the generator groups and link them
77 module
= MacModule(MODNAME
, MODPREFIX
, includestuff
, finalstuff
, initstuff
)
78 object = MyObjectDefinition(OBJECTNAME
, OBJECTPREFIX
, OBJECTTYPE
)
79 module
.addobject(object)
82 # Create the generator classes used to populate the lists
83 Function
= OSErrWeakLinkFunctionGenerator
84 Method
= OSErrWeakLinkMethodGenerator
86 # Create and populate the lists
91 # A method to convert a GWorldPtr to a GrafPtr
92 f
= Method(GrafPtr
, 'as_GrafPtr', (GWorldPtr
, 'p', InMode
))
96 # Manual generator: get data out of a pixmap
97 pixmapgetbytes_body
= """
102 if ( !PyArg_ParseTuple(_args, "O&ii", ResObj_Convert, &pm, &from, &length) )
104 cp = GetPixBaseAddr(pm)+from;
105 _res = PyString_FromStringAndSize(cp, length);
108 f
= ManualGenerator("GetPixMapBytes", pixmapgetbytes_body
)
109 f
.docstring
= lambda: """(pixmap, int start, int size) -> string. Return bytes from the pixmap"""
112 # Manual generator: store data in a pixmap
113 pixmapputbytes_body
= """
118 if ( !PyArg_ParseTuple(_args, "O&is#", ResObj_Convert, &pm, &from, &icp, &length) )
120 cp = GetPixBaseAddr(pm)+from;
121 memcpy(cp, icp, length);
126 f
= ManualGenerator("PutPixMapBytes", pixmapputbytes_body
)
127 f
.docstring
= lambda: """(pixmap, int start, string data). Store bytes into the pixmap"""
130 # add the populated lists to the generator groups
131 # (in a different wordl the scan program would generate this)
132 for f
in functions
: module
.add(f
)
133 for f
in methods
: object.add(f
)
137 # generate output (open the output file as late as possible)
138 SetOutputFileName(OUTPUTFILE
)