|
1 # This script will generate the AppleEvents interface for Python. |
|
2 # It uses the "bgen" package to generate C code. |
|
3 # It execs the file aegen.py which contain the function definitions |
|
4 # (aegen.py was generated by aescan.py, scanning the <AppleEvents.h> header file). |
|
5 |
|
6 |
|
7 from macsupport import * |
|
8 |
|
9 |
|
10 AEArrayType = Type("AEArrayType", "c") |
|
11 AESendMode = Type("AESendMode", "l") |
|
12 AESendPriority = Type("AESendPriority", "h") |
|
13 AEInteractAllowed = Type("AEInteractAllowed", "b") |
|
14 AEReturnID = Type("AEReturnID", "h") |
|
15 AETransactionID = Type("AETransactionID", "l") |
|
16 |
|
17 |
|
18 |
|
19 AEEventClass = OSTypeType('AEEventClass') |
|
20 AEEventID = OSTypeType('AEEventID') |
|
21 AEKeyword = OSTypeType('AEKeyword') |
|
22 DescType = OSTypeType('DescType') |
|
23 |
|
24 |
|
25 AEDesc = OpaqueType('AEDesc') |
|
26 AEDesc_ptr = OpaqueType('AEDesc') |
|
27 |
|
28 AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc') |
|
29 AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc') |
|
30 |
|
31 AEDescList = OpaqueType('AEDescList', 'AEDesc') |
|
32 AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc') |
|
33 |
|
34 AERecord = OpaqueType('AERecord', 'AEDesc') |
|
35 AERecord_ptr = OpaqueType('AERecord', 'AEDesc') |
|
36 |
|
37 AppleEvent = OpaqueType('AppleEvent', 'AEDesc') |
|
38 AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc') |
|
39 |
|
40 |
|
41 class EHType(Type): |
|
42 def __init__(self, name = 'EventHandler', format = ''): |
|
43 Type.__init__(self, name, format) |
|
44 def declare(self, name): |
|
45 Output("AEEventHandlerUPP %s__proc__ = upp_GenericEventHandler;", name) |
|
46 Output("PyObject *%s;", name) |
|
47 def getargsFormat(self): |
|
48 return "O" |
|
49 def getargsArgs(self, name): |
|
50 return "&%s" % name |
|
51 def passInput(self, name): |
|
52 return "%s__proc__, (long)%s" % (name, name) |
|
53 def passOutput(self, name): |
|
54 return "&%s__proc__, (long *)&%s" % (name, name) |
|
55 def mkvalueFormat(self): |
|
56 return "O" |
|
57 def mkvalueArgs(self, name): |
|
58 return name |
|
59 def cleanup(self, name): |
|
60 Output("Py_INCREF(%s); /* XXX leak, but needed */", name) |
|
61 |
|
62 class EHNoRefConType(EHType): |
|
63 def passInput(self, name): |
|
64 return "upp_GenericEventHandler" |
|
65 |
|
66 EventHandler = EHType() |
|
67 EventHandlerNoRefCon = EHNoRefConType() |
|
68 |
|
69 |
|
70 IdleProcPtr = FakeType("upp_AEIdleProc") |
|
71 AEIdleUPP = IdleProcPtr |
|
72 EventFilterProcPtr = FakeType("(AEFilterUPP)0") |
|
73 AEFilterUPP = EventFilterProcPtr |
|
74 NMRecPtr = FakeType("(NMRecPtr)0") |
|
75 EventHandlerProcPtr = FakeType("upp_GenericEventHandler") |
|
76 AEEventHandlerUPP = EventHandlerProcPtr |
|
77 AlwaysFalse = FakeType("0") |
|
78 |
|
79 |
|
80 AEFunction = OSErrWeakLinkFunctionGenerator |
|
81 AEMethod = OSErrWeakLinkMethodGenerator |
|
82 |
|
83 |
|
84 includestuff = includestuff + """ |
|
85 #include <Carbon/Carbon.h> |
|
86 |
|
87 #ifdef USE_TOOLBOX_OBJECT_GLUE |
|
88 extern PyObject *_AEDesc_New(AEDesc *); |
|
89 extern int _AEDesc_Convert(PyObject *, AEDesc *); |
|
90 |
|
91 #define AEDesc_New _AEDesc_New |
|
92 #define AEDesc_NewBorrowed _AEDesc_NewBorrowed |
|
93 #define AEDesc_Convert _AEDesc_Convert |
|
94 #endif |
|
95 |
|
96 typedef long refcontype; |
|
97 |
|
98 static pascal OSErr GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon); /* Forward */ |
|
99 |
|
100 AEEventHandlerUPP upp_GenericEventHandler; |
|
101 |
|
102 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) |
|
103 { |
|
104 if ( PyOS_InterruptOccurred() ) |
|
105 return 1; |
|
106 return 0; |
|
107 } |
|
108 |
|
109 AEIdleUPP upp_AEIdleProc; |
|
110 """ |
|
111 |
|
112 finalstuff = finalstuff + """ |
|
113 static pascal OSErr |
|
114 GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon) |
|
115 { |
|
116 PyObject *handler = (PyObject *)refcon; |
|
117 AEDescObject *requestObject, *replyObject; |
|
118 PyObject *args, *res; |
|
119 if ((requestObject = (AEDescObject *)AEDesc_New((AppleEvent *)request)) == NULL) { |
|
120 return -1; |
|
121 } |
|
122 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) { |
|
123 Py_DECREF(requestObject); |
|
124 return -1; |
|
125 } |
|
126 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) { |
|
127 Py_DECREF(requestObject); |
|
128 Py_DECREF(replyObject); |
|
129 return -1; |
|
130 } |
|
131 res = PyEval_CallObject(handler, args); |
|
132 requestObject->ob_itself.descriptorType = 'null'; |
|
133 requestObject->ob_itself.dataHandle = NULL; |
|
134 replyObject->ob_itself.descriptorType = 'null'; |
|
135 replyObject->ob_itself.dataHandle = NULL; |
|
136 Py_DECREF(args); |
|
137 if (res == NULL) { |
|
138 PySys_WriteStderr("Exception in AE event handler function\\n"); |
|
139 PyErr_Print(); |
|
140 return -1; |
|
141 } |
|
142 Py_DECREF(res); |
|
143 return noErr; |
|
144 } |
|
145 |
|
146 PyObject *AEDesc_NewBorrowed(AEDesc *itself) |
|
147 { |
|
148 PyObject *it; |
|
149 |
|
150 it = AEDesc_New(itself); |
|
151 if (it) |
|
152 ((AEDescObject *)it)->ob_owned = 0; |
|
153 return (PyObject *)it; |
|
154 } |
|
155 |
|
156 """ |
|
157 |
|
158 initstuff = initstuff + """ |
|
159 upp_AEIdleProc = NewAEIdleUPP(AEIdleProc); |
|
160 upp_GenericEventHandler = NewAEEventHandlerUPP(GenericEventHandler); |
|
161 PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_New); |
|
162 PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_NewBorrowed); |
|
163 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(AEDesc, AEDesc_Convert); |
|
164 """ |
|
165 |
|
166 module = MacModule('_AE', 'AE', includestuff, finalstuff, initstuff) |
|
167 |
|
168 class AEDescDefinition(PEP253Mixin, GlobalObjectDefinition): |
|
169 getsetlist = [( |
|
170 'type', |
|
171 'return PyMac_BuildOSType(self->ob_itself.descriptorType);', |
|
172 None, |
|
173 'Type of this AEDesc' |
|
174 ), ( |
|
175 'data', |
|
176 """ |
|
177 PyObject *res; |
|
178 Size size; |
|
179 char *ptr; |
|
180 OSErr err; |
|
181 |
|
182 size = AEGetDescDataSize(&self->ob_itself); |
|
183 if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL ) |
|
184 return NULL; |
|
185 if ( (ptr = PyString_AsString(res)) == NULL ) |
|
186 return NULL; |
|
187 if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 ) |
|
188 return PyMac_Error(err); |
|
189 return res; |
|
190 """, |
|
191 None, |
|
192 'The raw data in this AEDesc' |
|
193 )] |
|
194 |
|
195 def __init__(self, name, prefix = None, itselftype = None): |
|
196 GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name) |
|
197 self.argref = "*" |
|
198 |
|
199 def outputStructMembers(self): |
|
200 GlobalObjectDefinition.outputStructMembers(self) |
|
201 Output("int ob_owned;") |
|
202 |
|
203 def outputInitStructMembers(self): |
|
204 GlobalObjectDefinition.outputInitStructMembers(self) |
|
205 Output("it->ob_owned = 1;") |
|
206 |
|
207 def outputCleanupStructMembers(self): |
|
208 Output("if (self->ob_owned) AEDisposeDesc(&self->ob_itself);") |
|
209 |
|
210 aedescobject = AEDescDefinition('AEDesc') |
|
211 module.addobject(aedescobject) |
|
212 |
|
213 functions = [] |
|
214 aedescmethods = [] |
|
215 |
|
216 execfile('aegen.py') |
|
217 ##execfile('aedatamodelgen.py') |
|
218 |
|
219 # Manual generator |
|
220 AutoDispose_body = """ |
|
221 int onoff, old; |
|
222 if (!PyArg_ParseTuple(_args, "i", &onoff)) |
|
223 return NULL; |
|
224 old = _self->ob_owned; |
|
225 _self->ob_owned = onoff; |
|
226 _res = Py_BuildValue("i", old); |
|
227 return _res; |
|
228 """ |
|
229 f = ManualGenerator("AutoDispose", AutoDispose_body) |
|
230 f.docstring = lambda: "(int)->int. Automatically AEDisposeDesc the object on Python object cleanup" |
|
231 aedescmethods.append(f) |
|
232 |
|
233 for f in functions: module.add(f) |
|
234 for f in aedescmethods: aedescobject.add(f) |
|
235 |
|
236 SetOutputFileName('_AEmodule.c') |
|
237 module.generate() |