Another batch of updates...
[python/dscho.git] / Mac / Modules / ae / aesupport.py
blob8e4c7a3a44842fef94c628f3c02c954bf9503c10
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).
6 import addpack
7 addpack.addpack(':Tools:bgen:bgen')
9 from macsupport import *
12 AEArrayType = Type("AEArrayType", "c")
13 AESendMode = Type("AESendMode", "l")
14 AESendPriority = Type("AESendPriority", "h")
15 AEInteractAllowed = Type("AEInteractAllowed", "b")
18 AEEventClass = OSTypeType('AEEventClass')
19 AEEventID = OSTypeType('AEEventID')
20 AEKeyword = OSTypeType('AEKeyword')
21 DescType = OSTypeType('DescType')
24 AEDesc = OpaqueType('AEDesc')
25 AEDesc_ptr = OpaqueType('AEDesc')
27 AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc')
28 AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc')
30 AEDescList = OpaqueType('AEDescList', 'AEDesc')
31 AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc')
33 AERecord = OpaqueType('AERecord', 'AEDesc')
34 AERecord_ptr = OpaqueType('AERecord', 'AEDesc')
36 AppleEvent = OpaqueType('AppleEvent', 'AEDesc')
37 AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc')
40 class EHType(Type):
41 def __init__(self, name = 'EventHandler', format = ''):
42 Type.__init__(self, name, format)
43 def declare(self, name):
44 Output("AEEventHandlerUPP %s__proc__ = upp_GenericEventHandler;", name)
45 Output("PyObject *%s;", name)
46 def getargsFormat(self):
47 return "O"
48 def getargsArgs(self, name):
49 return "&%s" % name
50 def passInput(self, name):
51 return "%s__proc__, (long)%s" % (name, name)
52 def passOutput(self, name):
53 return "&%s__proc__, (long *)&%s" % (name, name)
54 def mkvalueFormat(self):
55 return "O"
56 def mkvalueArgs(self, name):
57 return name
58 def cleanup(self, name):
59 Output("Py_INCREF(%s); /* XXX leak, but needed */", name)
61 class EHNoRefConType(EHType):
62 def passInput(self, name):
63 return "upp_GenericEventHandler"
65 EventHandler = EHType()
66 EventHandlerNoRefCon = EHNoRefConType()
69 IdleProcPtr = FakeType("upp_AEIdleProc")
70 AEIdleUPP = IdleProcPtr
71 EventFilterProcPtr = FakeType("(AEFilterUPP)0")
72 AEFilterUPP = EventFilterProcPtr
73 NMRecPtr = FakeType("(NMRecPtr)0")
74 EventHandlerProcPtr = FakeType("upp_GenericEventHandler")
75 AEEventHandlerUPP = EventHandlerProcPtr
76 AlwaysFalse = FakeType("0")
79 AEFunction = OSErrFunctionGenerator
80 AEMethod = OSErrMethodGenerator
83 includestuff = includestuff + """
84 #include <AppleEvents.h>
86 #ifndef HAVE_UNIVERSAL_HEADERS
87 #define AEIdleProcPtr IdleProcPtr
88 #define AEFilterProcPtr EventFilterProcPtr
89 #define AEEventHandlerProcPtr EventHandlerProcPtr
90 #endif
92 #ifndef HAVE_UNIVERSAL_HEADERS
93 /* I'm trying to setup the code here so that is easily automated,
94 ** as follows:
95 ** - Use the UPP in the source
96 ** - for pre-universal headers, #define each UPP as the corresponding ProcPtr
97 ** - for each routine we pass we declare a upp_xxx that
98 ** we initialize to the correct value in the init routine.
100 #define AEIdleUPP AEIdleProcPtr
101 #define AEFilterUPP AEFilterProcPtr
102 #define AEEventHandlerUPP AEEventHandlerProcPtr
103 #define NewAEIdleProc(x) (x)
104 #define NewAEFilterProc(x) (x)
105 #define NewAEEventHandlerProc(x) (x)
106 #endif
108 static pascal OSErr GenericEventHandler(); /* Forward */
110 AEEventHandlerUPP upp_GenericEventHandler;
112 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
114 PyMac_Yield();
115 return 0;
118 AEIdleUPP upp_AEIdleProc;
121 finalstuff = finalstuff + """
122 static pascal OSErr
123 GenericEventHandler(AppleEvent *request, AppleEvent *reply, long refcon)
125 PyObject *handler = (PyObject *)refcon;
126 AEDescObject *requestObject, *replyObject;
127 PyObject *args, *res;
128 if ((requestObject = (AEDescObject *)AEDesc_New(request)) == NULL) {
129 return -1;
131 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
132 Py_DECREF(requestObject);
133 return -1;
135 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
136 Py_DECREF(requestObject);
137 Py_DECREF(replyObject);
138 return -1;
140 res = PyEval_CallObject(handler, args);
141 requestObject->ob_itself.descriptorType = 'null';
142 requestObject->ob_itself.dataHandle = NULL;
143 replyObject->ob_itself.descriptorType = 'null';
144 replyObject->ob_itself.dataHandle = NULL;
145 Py_DECREF(args);
146 if (res == NULL)
147 return -1;
148 Py_DECREF(res);
149 return noErr;
153 initstuff = initstuff + """
154 upp_AEIdleProc = NewAEIdleProc(AEIdleProc);
155 upp_GenericEventHandler = NewAEEventHandlerProc(GenericEventHandler);
158 module = MacModule('AE', 'AE', includestuff, finalstuff, initstuff)
160 class AEDescDefiniton(ObjectDefinition):
162 def __init__(self, name, prefix = None, itselftype = None):
163 ObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
164 self.argref = "*"
166 def outputFreeIt(self, name):
167 Output("AEDisposeDesc(&%s);", name)
169 def outputGetattrHook(self):
170 Output("""
171 if (strcmp(name, "type") == 0)
172 return PyMac_BuildOSType(self->ob_itself.descriptorType);
173 if (strcmp(name, "data") == 0) {
174 PyObject *res;
175 char state;
176 state = HGetState(self->ob_itself.dataHandle);
177 HLock(self->ob_itself.dataHandle);
178 res = PyString_FromStringAndSize(
179 *self->ob_itself.dataHandle,
180 GetHandleSize(self->ob_itself.dataHandle));
181 HUnlock(self->ob_itself.dataHandle);
182 HSetState(self->ob_itself.dataHandle, state);
183 return res;
185 if (strcmp(name, "__members__") == 0)
186 return Py_BuildValue("[ss]", "data", "type");
187 """)
190 aedescobject = AEDescDefiniton('AEDesc')
191 module.addobject(aedescobject)
193 functions = []
194 aedescmethods = []
196 execfile('aegen.py')
198 for f in functions: module.add(f)
199 for f in aedescmethods: aedescobject.add(f)
201 SetOutputFileName('AEmodule.c')
202 module.generate()