Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Mac / Modules / ae / aesupport.py
blob89a571baa502aeefac1254c4b7eaf0b95f831861
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).
7 from macsupport import *
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")
19 AEEventClass = OSTypeType('AEEventClass')
20 AEEventID = OSTypeType('AEEventID')
21 AEKeyword = OSTypeType('AEKeyword')
22 DescType = OSTypeType('DescType')
25 AEDesc = OpaqueType('AEDesc')
26 AEDesc_ptr = OpaqueType('AEDesc')
28 AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc')
29 AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc')
31 AEDescList = OpaqueType('AEDescList', 'AEDesc')
32 AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc')
34 AERecord = OpaqueType('AERecord', 'AEDesc')
35 AERecord_ptr = OpaqueType('AERecord', 'AEDesc')
37 AppleEvent = OpaqueType('AppleEvent', 'AEDesc')
38 AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc')
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)
62 class EHNoRefConType(EHType):
63 def passInput(self, name):
64 return "upp_GenericEventHandler"
66 EventHandler = EHType()
67 EventHandlerNoRefCon = EHNoRefConType()
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")
80 AEFunction = OSErrWeakLinkFunctionGenerator
81 AEMethod = OSErrWeakLinkMethodGenerator
84 includestuff = includestuff + """
85 #ifndef PyDoc_STR
86 #define PyDoc_STR(x) (x)
87 #endif
88 #ifdef WITHOUT_FRAMEWORKS
89 #include <AppleEvents.h>
90 #include <AEObjects.h>
91 #else
92 #include <Carbon/Carbon.h>
93 #endif
95 #ifdef USE_TOOLBOX_OBJECT_GLUE
96 extern PyObject *_AEDesc_New(AEDesc *);
97 extern int _AEDesc_Convert(PyObject *, AEDesc *);
99 #define AEDesc_New _AEDesc_New
100 #define AEDesc_NewBorrowed _AEDesc_NewBorrowed
101 #define AEDesc_Convert _AEDesc_Convert
102 #endif
104 typedef long refcontype;
106 static pascal OSErr GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon); /* Forward */
108 AEEventHandlerUPP upp_GenericEventHandler;
110 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
112 if ( PyOS_InterruptOccurred() )
113 return 1;
114 #if !TARGET_API_MAC_OSX
115 if ( PyMac_HandleEvent(theEvent) < 0 ) {
116 PySys_WriteStderr("Exception in user event handler during AE processing\\n");
117 PyErr_Clear();
119 #endif
120 return 0;
123 AEIdleUPP upp_AEIdleProc;
126 finalstuff = finalstuff + """
127 static pascal OSErr
128 GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon)
130 PyObject *handler = (PyObject *)refcon;
131 AEDescObject *requestObject, *replyObject;
132 PyObject *args, *res;
133 if ((requestObject = (AEDescObject *)AEDesc_New((AppleEvent *)request)) == NULL) {
134 return -1;
136 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
137 Py_DECREF(requestObject);
138 return -1;
140 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
141 Py_DECREF(requestObject);
142 Py_DECREF(replyObject);
143 return -1;
145 res = PyEval_CallObject(handler, args);
146 requestObject->ob_itself.descriptorType = 'null';
147 requestObject->ob_itself.dataHandle = NULL;
148 replyObject->ob_itself.descriptorType = 'null';
149 replyObject->ob_itself.dataHandle = NULL;
150 Py_DECREF(args);
151 if (res == NULL) {
152 PySys_WriteStderr("Exception in AE event handler function\\n");
153 PyErr_Print();
154 return -1;
156 Py_DECREF(res);
157 return noErr;
160 PyObject *AEDesc_NewBorrowed(AEDesc *itself)
162 PyObject *it;
164 it = AEDesc_New(itself);
165 if (it)
166 ((AEDescObject *)it)->ob_owned = 0;
167 return (PyObject *)it;
172 initstuff = initstuff + """
173 upp_AEIdleProc = NewAEIdleUPP(AEIdleProc);
174 upp_GenericEventHandler = NewAEEventHandlerUPP(GenericEventHandler);
175 PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_New);
176 PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_NewBorrowed);
177 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(AEDesc, AEDesc_Convert);
180 module = MacModule('_AE', 'AE', includestuff, finalstuff, initstuff)
182 class AEDescDefinition(PEP253Mixin, GlobalObjectDefinition):
183 getsetlist = [(
184 'type',
185 'return PyMac_BuildOSType(self->ob_itself.descriptorType);',
186 None,
187 'Type of this AEDesc'
188 ), (
189 'data',
191 PyObject *res;
192 Size size;
193 char *ptr;
194 OSErr err;
196 size = AEGetDescDataSize(&self->ob_itself);
197 if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL )
198 return NULL;
199 if ( (ptr = PyString_AsString(res)) == NULL )
200 return NULL;
201 if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 )
202 return PyMac_Error(err);
203 return res;
204 """,
205 None,
206 'The raw data in this AEDesc'
209 def __init__(self, name, prefix = None, itselftype = None):
210 GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
211 self.argref = "*"
213 def outputStructMembers(self):
214 GlobalObjectDefinition.outputStructMembers(self)
215 Output("int ob_owned;")
217 def outputInitStructMembers(self):
218 GlobalObjectDefinition.outputInitStructMembers(self)
219 Output("it->ob_owned = 1;")
221 def outputCleanupStructMembers(self):
222 Output("if (self->ob_owned) AEDisposeDesc(&self->ob_itself);")
224 aedescobject = AEDescDefinition('AEDesc')
225 module.addobject(aedescobject)
227 functions = []
228 aedescmethods = []
230 execfile('aegen.py')
231 ##execfile('aedatamodelgen.py')
233 # Manual generator
234 AutoDispose_body = """
235 int onoff, old;
236 if (!PyArg_ParseTuple(_args, "i", &onoff))
237 return NULL;
238 old = _self->ob_owned;
239 _self->ob_owned = onoff;
240 _res = Py_BuildValue("i", old);
241 return _res;
243 f = ManualGenerator("AutoDispose", AutoDispose_body)
244 f.docstring = lambda: "(int)->int. Automatically AEDisposeDesc the object on Python object cleanup"
245 aedescmethods.append(f)
247 for f in functions: module.add(f)
248 for f in aedescmethods: aedescobject.add(f)
250 SetOutputFileName('_AEmodule.c')
251 module.generate()