|
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). |
|
5 |
|
6 import string |
|
7 |
|
8 # Declarations that change for each manager |
|
9 MACHEADERFILE = 'Drag.h' # The Apple header file |
|
10 MODNAME = '_Drag' # The name of the module |
|
11 OBJECTNAME = 'DragObj' # The basic name of the objects used here |
|
12 |
|
13 # The following is *usually* unchanged but may still require tuning |
|
14 MODPREFIX = 'Drag' # The prefix for module-wide routines |
|
15 OBJECTTYPE = 'DragRef' # The C type used to represent them |
|
16 OBJECTPREFIX = MODPREFIX + 'Obj' # The prefix for object methods |
|
17 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner |
|
18 OUTPUTFILE = MODNAME + "module.c" # The file generated by this program |
|
19 |
|
20 from macsupport import * |
|
21 |
|
22 # Create the type objects |
|
23 |
|
24 DragRef = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX) |
|
25 DragItemRef = Type("ItemReference", "l") |
|
26 # Old names |
|
27 DragReference = DragRef |
|
28 ItemReference = DragItemRef |
|
29 |
|
30 PixMapHandle = OpaqueByValueType("PixMapHandle", "ResObj") |
|
31 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|
32 AEDesc = OpaqueType('AEDesc') |
|
33 AEDesc_ptr = AEDesc |
|
34 RGBColor = OpaqueType("RGBColor", "QdRGB") |
|
35 |
|
36 FlavorType = OSTypeType("FlavorType") |
|
37 DragAttributes = Type("DragAttributes", "l") |
|
38 DragBehaviors = Type("DragBehaviors", "l") |
|
39 DragImageFlags = Type("DragImageFlags", "l") |
|
40 DragImageTranslucency = Type("DragImageTranslucency", "l") |
|
41 DragRegionMessage = Type("DragRegionMessage", "h") |
|
42 ZoomAcceleration = Type("ZoomAcceleration", "h") |
|
43 FlavorFlags = Type("FlavorFlags", "l") |
|
44 DragTrackingMessage = Type("DragTrackingMessage", "h") |
|
45 |
|
46 includestuff = includestuff + """ |
|
47 #include <Carbon/Carbon.h> |
|
48 |
|
49 /* Callback glue routines */ |
|
50 DragTrackingHandlerUPP dragglue_TrackingHandlerUPP; |
|
51 DragReceiveHandlerUPP dragglue_ReceiveHandlerUPP; |
|
52 DragSendDataUPP dragglue_SendDataUPP; |
|
53 #if 0 |
|
54 DragInputUPP dragglue_InputUPP; |
|
55 DragDrawingUPP dragglue_DrawingUPP; |
|
56 #endif |
|
57 |
|
58 #ifdef USE_TOOLBOX_OBJECT_GLUE |
|
59 extern PyObject *_DragObj_New(DragRef); |
|
60 extern int _DragObj_Convert(PyObject *, DragRef *); |
|
61 |
|
62 #define DragObj_New _DragObj_New |
|
63 #define DragObj_Convert _DragObj_Convert |
|
64 #endif |
|
65 """ |
|
66 |
|
67 finalstuff = finalstuff + """ |
|
68 static pascal OSErr |
|
69 dragglue_TrackingHandler(DragTrackingMessage theMessage, WindowPtr theWindow, |
|
70 void *handlerRefCon, DragReference theDrag) |
|
71 { |
|
72 PyObject *args, *rv; |
|
73 int i; |
|
74 |
|
75 args = Py_BuildValue("hO&O&", theMessage, DragObj_New, theDrag, WinObj_WhichWindow, theWindow); |
|
76 if ( args == NULL ) |
|
77 return -1; |
|
78 rv = PyEval_CallObject((PyObject *)handlerRefCon, args); |
|
79 Py_DECREF(args); |
|
80 if ( rv == NULL ) { |
|
81 PySys_WriteStderr("Drag: Exception in TrackingHandler\\n"); |
|
82 PyErr_Print(); |
|
83 return -1; |
|
84 } |
|
85 i = -1; |
|
86 if ( rv == Py_None ) |
|
87 i = 0; |
|
88 else |
|
89 PyArg_Parse(rv, "l", &i); |
|
90 Py_DECREF(rv); |
|
91 return i; |
|
92 } |
|
93 |
|
94 static pascal OSErr |
|
95 dragglue_ReceiveHandler(WindowPtr theWindow, void *handlerRefCon, |
|
96 DragReference theDrag) |
|
97 { |
|
98 PyObject *args, *rv; |
|
99 int i; |
|
100 |
|
101 args = Py_BuildValue("O&O&", DragObj_New, theDrag, WinObj_WhichWindow, theWindow); |
|
102 if ( args == NULL ) |
|
103 return -1; |
|
104 rv = PyEval_CallObject((PyObject *)handlerRefCon, args); |
|
105 Py_DECREF(args); |
|
106 if ( rv == NULL ) { |
|
107 PySys_WriteStderr("Drag: Exception in ReceiveHandler\\n"); |
|
108 PyErr_Print(); |
|
109 return -1; |
|
110 } |
|
111 i = -1; |
|
112 if ( rv == Py_None ) |
|
113 i = 0; |
|
114 else |
|
115 PyArg_Parse(rv, "l", &i); |
|
116 Py_DECREF(rv); |
|
117 return i; |
|
118 } |
|
119 |
|
120 static pascal OSErr |
|
121 dragglue_SendData(FlavorType theType, void *dragSendRefCon, |
|
122 ItemReference theItem, DragReference theDrag) |
|
123 { |
|
124 DragObjObject *self = (DragObjObject *)dragSendRefCon; |
|
125 PyObject *args, *rv; |
|
126 int i; |
|
127 |
|
128 if ( self->sendproc == NULL ) |
|
129 return -1; |
|
130 args = Py_BuildValue("O&l", PyMac_BuildOSType, theType, theItem); |
|
131 if ( args == NULL ) |
|
132 return -1; |
|
133 rv = PyEval_CallObject(self->sendproc, args); |
|
134 Py_DECREF(args); |
|
135 if ( rv == NULL ) { |
|
136 PySys_WriteStderr("Drag: Exception in SendDataHandler\\n"); |
|
137 PyErr_Print(); |
|
138 return -1; |
|
139 } |
|
140 i = -1; |
|
141 if ( rv == Py_None ) |
|
142 i = 0; |
|
143 else |
|
144 PyArg_Parse(rv, "l", &i); |
|
145 Py_DECREF(rv); |
|
146 return i; |
|
147 } |
|
148 |
|
149 #if 0 |
|
150 static pascal OSErr |
|
151 dragglue_Input(Point *mouse, short *modifiers, |
|
152 void *dragSendRefCon, DragReference theDrag) |
|
153 { |
|
154 return 0; |
|
155 } |
|
156 |
|
157 static pascal OSErr |
|
158 dragglue_Drawing(xxxx |
|
159 void *dragSendRefCon, DragReference theDrag) |
|
160 { |
|
161 return 0; |
|
162 } |
|
163 #endif |
|
164 |
|
165 """ |
|
166 |
|
167 initstuff = initstuff + """ |
|
168 PyMac_INIT_TOOLBOX_OBJECT_NEW(DragRef, DragObj_New); |
|
169 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(DragRef, DragObj_Convert); |
|
170 """ |
|
171 |
|
172 variablestuff = """ |
|
173 dragglue_TrackingHandlerUPP = NewDragTrackingHandlerUPP(dragglue_TrackingHandler); |
|
174 dragglue_ReceiveHandlerUPP = NewDragReceiveHandlerUPP(dragglue_ReceiveHandler); |
|
175 dragglue_SendDataUPP = NewDragSendDataUPP(dragglue_SendData); |
|
176 #if 0 |
|
177 dragglue_InputUPP = NewDragInputUPP(dragglue_Input); |
|
178 dragglue_DrawingUPP = NewDragDrawingUPP(dragglue_Drawing); |
|
179 #endif |
|
180 """ |
|
181 |
|
182 class MyObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|
183 def outputCheckNewArg(self): |
|
184 Output("""if (itself == NULL) { |
|
185 PyErr_SetString(Drag_Error,"Cannot create null Drag"); |
|
186 return NULL; |
|
187 }""") |
|
188 def outputFreeIt(self, itselfname): |
|
189 ## Output("DisposeDrag(%s);", itselfname) |
|
190 Output("Py_XDECREF(self->sendproc);") |
|
191 ## Output("Py_XDECREF(self->inputproc);") |
|
192 ## Output("Py_XDECREF(self->drawingproc);") |
|
193 |
|
194 def outputStructMembers(self): |
|
195 GlobalObjectDefinition.outputStructMembers(self) |
|
196 Output("PyObject *sendproc;") |
|
197 ## Output("PyObject *inputproc;") |
|
198 ## Output("PyObject *drawingproc;") |
|
199 |
|
200 def outputInitStructMembers(self): |
|
201 GlobalObjectDefinition.outputInitStructMembers(self) |
|
202 Output("it->sendproc = NULL;") |
|
203 ## Output("it->inputproc = NULL;") |
|
204 ## Output("it->drawingproc = NULL;") |
|
205 |
|
206 |
|
207 # Create the generator groups and link them |
|
208 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff, variablestuff) |
|
209 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE) |
|
210 module.addobject(object) |
|
211 |
|
212 # Create the generator classes used to populate the lists |
|
213 Function = OSErrWeakLinkFunctionGenerator |
|
214 Method = OSErrWeakLinkMethodGenerator |
|
215 |
|
216 # Create and populate the lists |
|
217 functions = [] |
|
218 methods = [] |
|
219 execfile(INPUTFILE) |
|
220 |
|
221 # add the populated lists to the generator groups |
|
222 for f in functions: module.add(f) |
|
223 for f in methods: object.add(f) |
|
224 |
|
225 # Manual generators for the callbacks |
|
226 |
|
227 installtracking_body = """ |
|
228 PyObject *callback; |
|
229 WindowPtr theWindow = NULL; |
|
230 OSErr _err; |
|
231 |
|
232 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) ) |
|
233 return NULL; |
|
234 Py_INCREF(callback); /* Cannot decref later, too bad */ |
|
235 _err = InstallTrackingHandler(dragglue_TrackingHandlerUPP, theWindow, (void *)callback); |
|
236 if (_err != noErr) return PyMac_Error(_err); |
|
237 Py_INCREF(Py_None); |
|
238 _res = Py_None; |
|
239 return _res; |
|
240 """ |
|
241 installtracking = ManualGenerator("InstallTrackingHandler", installtracking_body) |
|
242 module.add(installtracking) |
|
243 |
|
244 installreceive_body = """ |
|
245 PyObject *callback; |
|
246 WindowPtr theWindow = NULL; |
|
247 OSErr _err; |
|
248 |
|
249 if ( !PyArg_ParseTuple(_args, "O|O&", &callback, WinObj_Convert, &theWindow) ) |
|
250 return NULL; |
|
251 Py_INCREF(callback); /* Cannot decref later, too bad */ |
|
252 _err = InstallReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow, (void *)callback); |
|
253 if (_err != noErr) return PyMac_Error(_err); |
|
254 Py_INCREF(Py_None); |
|
255 _res = Py_None; |
|
256 return _res; |
|
257 """ |
|
258 installreceive = ManualGenerator("InstallReceiveHandler", installreceive_body) |
|
259 module.add(installreceive) |
|
260 |
|
261 removetracking_body = """ |
|
262 WindowPtr theWindow = NULL; |
|
263 OSErr _err; |
|
264 |
|
265 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) ) |
|
266 return NULL; |
|
267 _err = RemoveTrackingHandler(dragglue_TrackingHandlerUPP, theWindow); |
|
268 if (_err != noErr) return PyMac_Error(_err); |
|
269 Py_INCREF(Py_None); |
|
270 _res = Py_None; |
|
271 return _res; |
|
272 """ |
|
273 removetracking = ManualGenerator("RemoveTrackingHandler", removetracking_body) |
|
274 module.add(removetracking) |
|
275 |
|
276 removereceive_body = """ |
|
277 WindowPtr theWindow = NULL; |
|
278 OSErr _err; |
|
279 |
|
280 if ( !PyArg_ParseTuple(_args, "|O&", WinObj_Convert, &theWindow) ) |
|
281 return NULL; |
|
282 _err = RemoveReceiveHandler(dragglue_ReceiveHandlerUPP, theWindow); |
|
283 if (_err != noErr) return PyMac_Error(_err); |
|
284 Py_INCREF(Py_None); |
|
285 _res = Py_None; |
|
286 return _res; |
|
287 """ |
|
288 removereceive = ManualGenerator("RemoveReceiveHandler", removereceive_body) |
|
289 module.add(removereceive) |
|
290 |
|
291 # generate output (open the output file as late as possible) |
|
292 SetOutputFileName(OUTPUTFILE) |
|
293 module.generate() |