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')
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
):
49 def getargsArgs(self
, 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
):
57 def mkvalueArgs(self
, 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>
89 #include <Carbon/Carbon.h>
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
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() )
108 #if !TARGET_API_MAC_OSX
109 if ( PyMac_HandleEvent(theEvent) < 0 ) {
110 PySys_WriteStderr("Exception in user event handler during AE processing\\n");
117 AEIdleUPP upp_AEIdleProc;
120 finalstuff
= finalstuff
+ """
121 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
122 typedef long refcontype;
124 typedef unsigned long refcontype;
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) {
136 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
137 Py_DECREF(requestObject);
140 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
141 Py_DECREF(requestObject);
142 Py_DECREF(replyObject);
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;
152 PySys_WriteStderr("Exception in AE event handler function\\n");
161 initstuff
= initstuff
+ """
162 upp_AEIdleProc = NewAEIdleUPP(AEIdleProc);
163 #if UNIVERSAL_INTERFACES_VERSION >= 0x03400
164 upp_GenericEventHandler = NewAEEventHandlerUPP(&GenericEventHandler);
166 upp_GenericEventHandler = NewAEEventHandlerUPP(GenericEventHandler);
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
)
180 def outputFreeIt(self
, name
):
181 Output("AEDisposeDesc(&%s);", name
)
183 def outputGetattrHook(self
):
185 if (strcmp(name, "type") == 0)
186 return PyMac_BuildOSType(self->ob_itself.descriptorType);
187 if (strcmp(name, "data") == 0) {
189 #if !TARGET_API_MAC_CARBON
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);
203 size = AEGetDescDataSize(&self->ob_itself);
204 if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL )
206 if ( (ptr = PyString_AsString(res)) == NULL )
208 if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 )
209 return PyMac_Error(err);
213 if (strcmp(name, "__members__") == 0)
214 return Py_BuildValue("[ss]", "data", "type");
218 aedescobject
= AEDescDefinition('AEDesc')
219 module
.addobject(aedescobject
)
225 ##execfile('aedatamodelgen.py')
227 for f
in functions
: module
.add(f
)
228 for f
in aedescmethods
: aedescobject
.add(f
)
230 SetOutputFileName('_AEmodule.c')