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
= OSErrFunctionGenerator
81 AEMethod
= OSErrMethodGenerator
84 includestuff
= includestuff
+ """
85 #include <AppleEvents.h>
86 #include <AEObjects.h>
88 static pascal OSErr GenericEventHandler(); /* Forward */
90 AEEventHandlerUPP upp_GenericEventHandler;
92 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
94 if ( PyOS_InterruptOccurred() )
96 if ( PyMac_HandleEvent(theEvent) < 0 ) {
97 PySys_WriteStderr("Exception in user event handler during AE processing\\n");
103 AEIdleUPP upp_AEIdleProc;
106 finalstuff
= finalstuff
+ """
108 GenericEventHandler(const AppleEvent *request, AppleEvent *reply, unsigned long refcon)
110 PyObject *handler = (PyObject *)refcon;
111 AEDescObject *requestObject, *replyObject;
112 PyObject *args, *res;
113 if ((requestObject = (AEDescObject *)AEDesc_New((AppleEvent *)request)) == NULL) {
116 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
117 Py_DECREF(requestObject);
120 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
121 Py_DECREF(requestObject);
122 Py_DECREF(replyObject);
125 res = PyEval_CallObject(handler, args);
126 requestObject->ob_itself.descriptorType = 'null';
127 requestObject->ob_itself.dataHandle = NULL;
128 replyObject->ob_itself.descriptorType = 'null';
129 replyObject->ob_itself.dataHandle = NULL;
138 initstuff
= initstuff
+ """
139 upp_AEIdleProc = NewAEIdleProc(AEIdleProc);
140 upp_GenericEventHandler = NewAEEventHandlerProc(GenericEventHandler);
143 module
= MacModule('AE', 'AE', includestuff
, finalstuff
, initstuff
)
145 class AEDescDefinition(GlobalObjectDefinition
):
147 def __init__(self
, name
, prefix
= None, itselftype
= None):
148 GlobalObjectDefinition
.__init
__(self
, name
, prefix
or name
, itselftype
or name
)
151 def outputFreeIt(self
, name
):
152 Output("AEDisposeDesc(&%s);", name
)
154 def outputGetattrHook(self
):
156 if (strcmp(name, "type") == 0)
157 return PyMac_BuildOSType(self->ob_itself.descriptorType);
158 if (strcmp(name, "data") == 0) {
160 #if !TARGET_API_MAC_CARBON
162 state = HGetState(self->ob_itself.dataHandle);
163 HLock(self->ob_itself.dataHandle);
164 res = PyString_FromStringAndSize(
165 *self->ob_itself.dataHandle,
166 GetHandleSize(self->ob_itself.dataHandle));
167 HUnlock(self->ob_itself.dataHandle);
168 HSetState(self->ob_itself.dataHandle, state);
174 size = AEGetDescDataSize(&self->ob_itself);
175 if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL )
177 if ( (ptr = PyString_AsString(res)) == NULL )
179 if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 )
180 return PyMac_Error(err);
184 if (strcmp(name, "__members__") == 0)
185 return Py_BuildValue("[ss]", "data", "type");
189 aedescobject
= AEDescDefinition('AEDesc')
190 module
.addobject(aedescobject
)
196 ##execfile('aedatamodelgen.py')
198 for f
in functions
: module
.add(f
)
199 for f
in aedescmethods
: aedescobject
.add(f
)
201 SetOutputFileName('AEmodule.c')