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
+ """
86 #define PyDoc_STR(x) (x)
88 #ifdef WITHOUT_FRAMEWORKS
89 #include <AppleEvents.h>
90 #include <AEObjects.h>
92 #include <Carbon/Carbon.h>
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
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() )
114 #if !TARGET_API_MAC_OSX
115 if ( PyMac_HandleEvent(theEvent) < 0 ) {
116 PySys_WriteStderr("Exception in user event handler during AE processing\\n");
123 AEIdleUPP upp_AEIdleProc;
126 finalstuff
= finalstuff
+ """
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");
160 PyObject *AEDesc_NewBorrowed(AEDesc *itself)
164 it = AEDesc_New(itself);
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
):
185 'return PyMac_BuildOSType(self->ob_itself.descriptorType);',
187 'Type of this AEDesc'
196 size = AEGetDescDataSize(&self->ob_itself);
197 if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL )
199 if ( (ptr = PyString_AsString(res)) == NULL )
201 if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 )
202 return PyMac_Error(err);
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
)
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
)
231 ##execfile('aedatamodelgen.py')
234 AutoDispose_body
= """
236 if (!PyArg_ParseTuple(_args, "i", &onoff))
238 old = _self->ob_owned;
239 _self->ob_owned = onoff;
240 _res = Py_BuildValue("i", old);
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')