Bump version number to 2.4.2 to pick up the latest minor bug fixes.
[python/dscho.git] / Mac / Modules / ae / aesupport.py
blob8ecaeda3cb97de2acc80f24abc8821a8067775d5
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 #ifdef WITHOUT_FRAMEWORKS
86 #include <AppleEvents.h>
87 #include <AEObjects.h>
88 #else
89 #include <Carbon/Carbon.h>
90 #endif
92 #ifdef USE_TOOLBOX_OBJECT_GLUE
93 extern PyObject *_AEDesc_New(AEDesc *);
94 extern int _AEDesc_Convert(PyObject *, AEDesc *);
96 #define AEDesc_New _AEDesc_New
97 #define AEDesc_Convert _AEDesc_Convert
98 #endif
100 static pascal OSErr GenericEventHandler(); /* Forward */
102 AEEventHandlerUPP upp_GenericEventHandler;
104 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
106 if ( PyOS_InterruptOccurred() )
107 return 1;
108 #if !TARGET_API_MAC_OSX
109 if ( PyMac_HandleEvent(theEvent) < 0 ) {
110 PySys_WriteStderr("Exception in user event handler during AE processing\\n");
111 PyErr_Clear();
113 #endif
114 return 0;
117 AEIdleUPP upp_AEIdleProc;
120 finalstuff = finalstuff + """
121 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
122 typedef long refcontype;
123 #else
124 typedef unsigned long refcontype;
125 #endif
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;
161 initstuff = initstuff + """
162 upp_AEIdleProc = NewAEIdleUPP(AEIdleProc);
163 #if UNIVERSAL_INTERFACES_VERSION >= 0x03400
164 upp_GenericEventHandler = NewAEEventHandlerUPP(&GenericEventHandler);
165 #else
166 upp_GenericEventHandler = NewAEEventHandlerUPP(GenericEventHandler);
167 #endif
168 PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_New);
169 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(AEDesc, AEDesc_Convert);
172 module = MacModule('_AE', 'AE', includestuff, finalstuff, initstuff)
174 class AEDescDefinition(GlobalObjectDefinition):
176 def __init__(self, name, prefix = None, itselftype = None):
177 GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
178 self.argref = "*"
180 def outputFreeIt(self, name):
181 Output("AEDisposeDesc(&%s);", name)
183 def outputGetattrHook(self):
184 Output("""
185 if (strcmp(name, "type") == 0)
186 return PyMac_BuildOSType(self->ob_itself.descriptorType);
187 if (strcmp(name, "data") == 0) {
188 PyObject *res;
189 #if !TARGET_API_MAC_CARBON
190 char state;
191 state = HGetState(self->ob_itself.dataHandle);
192 HLock(self->ob_itself.dataHandle);
193 res = PyString_FromStringAndSize(
194 *self->ob_itself.dataHandle,
195 GetHandleSize(self->ob_itself.dataHandle));
196 HUnlock(self->ob_itself.dataHandle);
197 HSetState(self->ob_itself.dataHandle, state);
198 #else
199 Size size;
200 char *ptr;
201 OSErr err;
203 size = AEGetDescDataSize(&self->ob_itself);
204 if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL )
205 return NULL;
206 if ( (ptr = PyString_AsString(res)) == NULL )
207 return NULL;
208 if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 )
209 return PyMac_Error(err);
210 #endif
211 return res;
213 if (strcmp(name, "__members__") == 0)
214 return Py_BuildValue("[ss]", "data", "type");
215 """)
218 aedescobject = AEDescDefinition('AEDesc')
219 module.addobject(aedescobject)
221 functions = []
222 aedescmethods = []
224 execfile('aegen.py')
225 ##execfile('aedatamodelgen.py')
227 for f in functions: module.add(f)
228 for f in aedescmethods: aedescobject.add(f)
230 SetOutputFileName('_AEmodule.c')
231 module.generate()