Whitespace normalization.
[python/dscho.git] / Mac / Modules / ae / aesupport.py
blob17184db47357c37f66886fb052da910f1af9fb36
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 #include <Carbon/Carbon.h>
87 #ifdef USE_TOOLBOX_OBJECT_GLUE
88 extern PyObject *_AEDesc_New(AEDesc *);
89 extern int _AEDesc_Convert(PyObject *, AEDesc *);
91 #define AEDesc_New _AEDesc_New
92 #define AEDesc_NewBorrowed _AEDesc_NewBorrowed
93 #define AEDesc_Convert _AEDesc_Convert
94 #endif
96 typedef long refcontype;
98 static pascal OSErr GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon); /* Forward */
100 AEEventHandlerUPP upp_GenericEventHandler;
102 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
104 if ( PyOS_InterruptOccurred() )
105 return 1;
106 return 0;
109 AEIdleUPP upp_AEIdleProc;
112 finalstuff = finalstuff + """
113 static pascal OSErr
114 GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon)
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;
122 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
123 Py_DECREF(requestObject);
124 return -1;
126 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
127 Py_DECREF(requestObject);
128 Py_DECREF(replyObject);
129 return -1;
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;
142 Py_DECREF(res);
143 return noErr;
146 PyObject *AEDesc_NewBorrowed(AEDesc *itself)
148 PyObject *it;
150 it = AEDesc_New(itself);
151 if (it)
152 ((AEDescObject *)it)->ob_owned = 0;
153 return (PyObject *)it;
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);
166 module = MacModule('_AE', 'AE', includestuff, finalstuff, initstuff)
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',
177 PyObject *res;
178 Size size;
179 char *ptr;
180 OSErr err;
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'
195 def __init__(self, name, prefix = None, itselftype = None):
196 GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
197 self.argref = "*"
199 def outputStructMembers(self):
200 GlobalObjectDefinition.outputStructMembers(self)
201 Output("int ob_owned;")
203 def outputInitStructMembers(self):
204 GlobalObjectDefinition.outputInitStructMembers(self)
205 Output("it->ob_owned = 1;")
207 def outputCleanupStructMembers(self):
208 Output("if (self->ob_owned) AEDisposeDesc(&self->ob_itself);")
210 aedescobject = AEDescDefinition('AEDesc')
211 module.addobject(aedescobject)
213 functions = []
214 aedescmethods = []
216 execfile('aegen.py')
217 ##execfile('aedatamodelgen.py')
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;
229 f = ManualGenerator("AutoDispose", AutoDispose_body)
230 f.docstring = lambda: "(int)->int. Automatically AEDisposeDesc the object on Python object cleanup"
231 aedescmethods.append(f)
233 for f in functions: module.add(f)
234 for f in aedescmethods: aedescobject.add(f)
236 SetOutputFileName('_AEmodule.c')
237 module.generate()