• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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).
5
6
7from macsupport import *
8
9
10AEArrayType = Type("AEArrayType", "c")
11AESendMode = Type("AESendMode", "l")
12AESendPriority = Type("AESendPriority", "h")
13AEInteractAllowed = Type("AEInteractAllowed", "b")
14AEReturnID = Type("AEReturnID", "h")
15AETransactionID = Type("AETransactionID", "l")
16
17
18
19AEEventClass = OSTypeType('AEEventClass')
20AEEventID = OSTypeType('AEEventID')
21AEKeyword = OSTypeType('AEKeyword')
22DescType = OSTypeType('DescType')
23
24
25AEDesc = OpaqueType('AEDesc')
26AEDesc_ptr = OpaqueType('AEDesc')
27
28AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc')
29AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc')
30
31AEDescList = OpaqueType('AEDescList', 'AEDesc')
32AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc')
33
34AERecord = OpaqueType('AERecord', 'AEDesc')
35AERecord_ptr = OpaqueType('AERecord', 'AEDesc')
36
37AppleEvent = OpaqueType('AppleEvent', 'AEDesc')
38AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc')
39
40
41class 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)
61
62class EHNoRefConType(EHType):
63    def passInput(self, name):
64        return "upp_GenericEventHandler"
65
66EventHandler = EHType()
67EventHandlerNoRefCon = EHNoRefConType()
68
69
70IdleProcPtr = FakeType("upp_AEIdleProc")
71AEIdleUPP = IdleProcPtr
72EventFilterProcPtr = FakeType("(AEFilterUPP)0")
73AEFilterUPP = EventFilterProcPtr
74NMRecPtr = FakeType("(NMRecPtr)0")
75EventHandlerProcPtr = FakeType("upp_GenericEventHandler")
76AEEventHandlerUPP = EventHandlerProcPtr
77AlwaysFalse = FakeType("0")
78
79
80AEFunction = OSErrWeakLinkFunctionGenerator
81AEMethod = OSErrWeakLinkMethodGenerator
82
83
84includestuff = includestuff + """
85#include <Carbon/Carbon.h>
86
87#ifdef USE_TOOLBOX_OBJECT_GLUE
88extern PyObject *_AEDesc_New(AEDesc *);
89extern int _AEDesc_Convert(PyObject *, AEDesc *);
90
91#define AEDesc_New _AEDesc_New
92#define AEDesc_NewBorrowed _AEDesc_NewBorrowed
93#define AEDesc_Convert _AEDesc_Convert
94#endif
95
96typedef long refcontype;
97
98static pascal OSErr GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon); /* Forward */
99
100AEEventHandlerUPP upp_GenericEventHandler;
101
102static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
103{
104        if ( PyOS_InterruptOccurred() )
105                return 1;
106        return 0;
107}
108
109AEIdleUPP upp_AEIdleProc;
110"""
111
112finalstuff = finalstuff + """
113static pascal OSErr
114GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon)
115{
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;
121        }
122        if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
123                Py_DECREF(requestObject);
124                return -1;
125        }
126        if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
127                Py_DECREF(requestObject);
128                Py_DECREF(replyObject);
129                return -1;
130        }
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;
141        }
142        Py_DECREF(res);
143        return noErr;
144}
145
146PyObject *AEDesc_NewBorrowed(AEDesc *itself)
147{
148        PyObject *it;
149
150        it = AEDesc_New(itself);
151        if (it)
152                ((AEDescObject *)it)->ob_owned = 0;
153        return (PyObject *)it;
154}
155
156"""
157
158initstuff = 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);
164"""
165
166module = MacModule('_AE', 'AE', includestuff, finalstuff, initstuff)
167
168class 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',
176            """
177            PyObject *res;
178            Size size;
179            char *ptr;
180            OSErr err;
181
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'
193            )]
194
195    def __init__(self, name, prefix = None, itselftype = None):
196        GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
197        self.argref = "*"
198
199    def outputStructMembers(self):
200        GlobalObjectDefinition.outputStructMembers(self)
201        Output("int ob_owned;")
202
203    def outputInitStructMembers(self):
204        GlobalObjectDefinition.outputInitStructMembers(self)
205        Output("it->ob_owned = 1;")
206
207    def outputCleanupStructMembers(self):
208        Output("if (self->ob_owned) AEDisposeDesc(&self->ob_itself);")
209
210aedescobject = AEDescDefinition('AEDesc')
211module.addobject(aedescobject)
212
213functions = []
214aedescmethods = []
215
216execfile('aegen.py')
217##execfile('aedatamodelgen.py')
218
219# Manual generator
220AutoDispose_body = """
221int onoff, old;
222if (!PyArg_ParseTuple(_args, "i", &onoff))
223        return NULL;
224old = _self->ob_owned;
225_self->ob_owned = onoff;
226_res = Py_BuildValue("i", old);
227return _res;
228"""
229f = ManualGenerator("AutoDispose", AutoDispose_body)
230f.docstring = lambda: "(int)->int. Automatically AEDisposeDesc the object on Python object cleanup"
231aedescmethods.append(f)
232
233for f in functions: module.add(f)
234for f in aedescmethods: aedescobject.add(f)
235
236SetOutputFileName('_AEmodule.c')
237module.generate()
238