Clarify portability and main program.
[python/dscho.git] / Mac / Modules / ae / aesupport.py
blobd82496bfd403aaeedd023dd7da07673ecc70921f
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", "h")
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')
41 class 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)
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>
87 #ifndef HAVE_UNIVERSAL_HEADERS
88 #define AEIdleProcPtr IdleProcPtr
89 #define AEFilterProcPtr EventFilterProcPtr
90 #define AEEventHandlerProcPtr EventHandlerProcPtr
91 #endif
93 #ifndef HAVE_UNIVERSAL_HEADERS
94 /* I'm trying to setup the code here so that is easily automated,
95 ** as follows:
96 ** - Use the UPP in the source
97 ** - for pre-universal headers, #define each UPP as the corresponding ProcPtr
98 ** - for each routine we pass we declare a upp_xxx that
99 ** we initialize to the correct value in the init routine.
101 #define AEIdleUPP AEIdleProcPtr
102 #define AEFilterUPP AEFilterProcPtr
103 #define AEEventHandlerUPP AEEventHandlerProcPtr
104 #define NewAEIdleProc(x) (x)
105 #define NewAEFilterProc(x) (x)
106 #define NewAEEventHandlerProc(x) (x)
107 #endif
109 static pascal OSErr GenericEventHandler(); /* Forward */
111 AEEventHandlerUPP upp_GenericEventHandler;
113 static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn)
115 if ( PyOS_InterruptOccurred() )
116 return 1;
117 if ( PyMac_HandleEvent(theEvent) < 0 ) {
118 PySys_WriteStderr("Exception in user event handler during AE processing\\n");
119 PyErr_Clear();
121 return 0;
124 AEIdleUPP upp_AEIdleProc;
127 finalstuff = finalstuff + """
128 static pascal OSErr
129 GenericEventHandler(AppleEvent *request, AppleEvent *reply, long refcon)
131 PyObject *handler = (PyObject *)refcon;
132 AEDescObject *requestObject, *replyObject;
133 PyObject *args, *res;
134 if ((requestObject = (AEDescObject *)AEDesc_New(request)) == NULL) {
135 return -1;
137 if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) {
138 Py_DECREF(requestObject);
139 return -1;
141 if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) {
142 Py_DECREF(requestObject);
143 Py_DECREF(replyObject);
144 return -1;
146 res = PyEval_CallObject(handler, args);
147 requestObject->ob_itself.descriptorType = 'null';
148 requestObject->ob_itself.dataHandle = NULL;
149 replyObject->ob_itself.descriptorType = 'null';
150 replyObject->ob_itself.dataHandle = NULL;
151 Py_DECREF(args);
152 if (res == NULL)
153 return -1;
154 Py_DECREF(res);
155 return noErr;
159 initstuff = initstuff + """
160 upp_AEIdleProc = NewAEIdleProc(AEIdleProc);
161 upp_GenericEventHandler = NewAEEventHandlerProc(GenericEventHandler);
164 module = MacModule('AE', 'AE', includestuff, finalstuff, initstuff)
166 class AEDescDefinition(GlobalObjectDefinition):
168 def __init__(self, name, prefix = None, itselftype = None):
169 GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name)
170 self.argref = "*"
172 def outputFreeIt(self, name):
173 Output("AEDisposeDesc(&%s);", name)
175 def outputGetattrHook(self):
176 Output("""
177 if (strcmp(name, "type") == 0)
178 return PyMac_BuildOSType(self->ob_itself.descriptorType);
179 if (strcmp(name, "data") == 0) {
180 PyObject *res;
181 char state;
182 state = HGetState(self->ob_itself.dataHandle);
183 HLock(self->ob_itself.dataHandle);
184 res = PyString_FromStringAndSize(
185 *self->ob_itself.dataHandle,
186 GetHandleSize(self->ob_itself.dataHandle));
187 HUnlock(self->ob_itself.dataHandle);
188 HSetState(self->ob_itself.dataHandle, state);
189 return res;
191 if (strcmp(name, "__members__") == 0)
192 return Py_BuildValue("[ss]", "data", "type");
193 """)
196 aedescobject = AEDescDefinition('AEDesc')
197 module.addobject(aedescobject)
199 functions = []
200 aedescmethods = []
202 execfile('aegen.py')
203 execfile('aedatamodelgen.py')
205 for f in functions: module.add(f)
206 for f in aedescmethods: aedescobject.add(f)
208 SetOutputFileName('AEmodule.c')
209 module.generate()