| 1 | n/a | # This script will generate the AppleEvents interface for Python. |
|---|
| 2 | n/a | # It uses the "bgen" package to generate C code. |
|---|
| 3 | n/a | # It execs the file aegen.py which contain the function definitions |
|---|
| 4 | n/a | # (aegen.py was generated by aescan.py, scanning the <AppleEvents.h> header file). |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | from macsupport import * |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | AEArrayType = Type("AEArrayType", "c") |
|---|
| 11 | n/a | AESendMode = Type("AESendMode", "l") |
|---|
| 12 | n/a | AESendPriority = Type("AESendPriority", "h") |
|---|
| 13 | n/a | AEInteractAllowed = Type("AEInteractAllowed", "b") |
|---|
| 14 | n/a | AEReturnID = Type("AEReturnID", "h") |
|---|
| 15 | n/a | AETransactionID = Type("AETransactionID", "l") |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | AEEventClass = OSTypeType('AEEventClass') |
|---|
| 20 | n/a | AEEventID = OSTypeType('AEEventID') |
|---|
| 21 | n/a | AEKeyword = OSTypeType('AEKeyword') |
|---|
| 22 | n/a | DescType = OSTypeType('DescType') |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | AEDesc = OpaqueType('AEDesc') |
|---|
| 26 | n/a | AEDesc_ptr = OpaqueType('AEDesc') |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc') |
|---|
| 29 | n/a | AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc') |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | AEDescList = OpaqueType('AEDescList', 'AEDesc') |
|---|
| 32 | n/a | AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc') |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | AERecord = OpaqueType('AERecord', 'AEDesc') |
|---|
| 35 | n/a | AERecord_ptr = OpaqueType('AERecord', 'AEDesc') |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | AppleEvent = OpaqueType('AppleEvent', 'AEDesc') |
|---|
| 38 | n/a | AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc') |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | class EHType(Type): |
|---|
| 42 | n/a | def __init__(self, name = 'EventHandler', format = ''): |
|---|
| 43 | n/a | Type.__init__(self, name, format) |
|---|
| 44 | n/a | def declare(self, name): |
|---|
| 45 | n/a | Output("AEEventHandlerUPP %s__proc__ = upp_GenericEventHandler;", name) |
|---|
| 46 | n/a | Output("PyObject *%s;", name) |
|---|
| 47 | n/a | def getargsFormat(self): |
|---|
| 48 | n/a | return "O" |
|---|
| 49 | n/a | def getargsArgs(self, name): |
|---|
| 50 | n/a | return "&%s" % name |
|---|
| 51 | n/a | def passInput(self, name): |
|---|
| 52 | n/a | return "%s__proc__, (long)%s" % (name, name) |
|---|
| 53 | n/a | def passOutput(self, name): |
|---|
| 54 | n/a | return "&%s__proc__, (long *)&%s" % (name, name) |
|---|
| 55 | n/a | def mkvalueFormat(self): |
|---|
| 56 | n/a | return "O" |
|---|
| 57 | n/a | def mkvalueArgs(self, name): |
|---|
| 58 | n/a | return name |
|---|
| 59 | n/a | def cleanup(self, name): |
|---|
| 60 | n/a | Output("Py_INCREF(%s); /* XXX leak, but needed */", name) |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | class EHNoRefConType(EHType): |
|---|
| 63 | n/a | def passInput(self, name): |
|---|
| 64 | n/a | return "upp_GenericEventHandler" |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | EventHandler = EHType() |
|---|
| 67 | n/a | EventHandlerNoRefCon = EHNoRefConType() |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | IdleProcPtr = FakeType("upp_AEIdleProc") |
|---|
| 71 | n/a | AEIdleUPP = IdleProcPtr |
|---|
| 72 | n/a | EventFilterProcPtr = FakeType("(AEFilterUPP)0") |
|---|
| 73 | n/a | AEFilterUPP = EventFilterProcPtr |
|---|
| 74 | n/a | NMRecPtr = FakeType("(NMRecPtr)0") |
|---|
| 75 | n/a | EventHandlerProcPtr = FakeType("upp_GenericEventHandler") |
|---|
| 76 | n/a | AEEventHandlerUPP = EventHandlerProcPtr |
|---|
| 77 | n/a | AlwaysFalse = FakeType("0") |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | AEFunction = OSErrWeakLinkFunctionGenerator |
|---|
| 81 | n/a | AEMethod = OSErrWeakLinkMethodGenerator |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | includestuff = includestuff + """ |
|---|
| 85 | n/a | #include <Carbon/Carbon.h> |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | #ifdef USE_TOOLBOX_OBJECT_GLUE |
|---|
| 88 | n/a | extern PyObject *_AEDesc_New(AEDesc *); |
|---|
| 89 | n/a | extern int _AEDesc_Convert(PyObject *, AEDesc *); |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | #define AEDesc_New _AEDesc_New |
|---|
| 92 | n/a | #define AEDesc_NewBorrowed _AEDesc_NewBorrowed |
|---|
| 93 | n/a | #define AEDesc_Convert _AEDesc_Convert |
|---|
| 94 | n/a | #endif |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | typedef long refcontype; |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | static pascal OSErr GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon); /* Forward */ |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | AEEventHandlerUPP upp_GenericEventHandler; |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) |
|---|
| 103 | n/a | { |
|---|
| 104 | n/a | if ( PyOS_InterruptOccurred() ) |
|---|
| 105 | n/a | return 1; |
|---|
| 106 | n/a | return 0; |
|---|
| 107 | n/a | } |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | AEIdleUPP upp_AEIdleProc; |
|---|
| 110 | n/a | """ |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | finalstuff = finalstuff + """ |
|---|
| 113 | n/a | static pascal OSErr |
|---|
| 114 | n/a | GenericEventHandler(const AppleEvent *request, AppleEvent *reply, refcontype refcon) |
|---|
| 115 | n/a | { |
|---|
| 116 | n/a | PyObject *handler = (PyObject *)refcon; |
|---|
| 117 | n/a | AEDescObject *requestObject, *replyObject; |
|---|
| 118 | n/a | PyObject *args, *res; |
|---|
| 119 | n/a | if ((requestObject = (AEDescObject *)AEDesc_New((AppleEvent *)request)) == NULL) { |
|---|
| 120 | n/a | return -1; |
|---|
| 121 | n/a | } |
|---|
| 122 | n/a | if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) { |
|---|
| 123 | n/a | Py_DECREF(requestObject); |
|---|
| 124 | n/a | return -1; |
|---|
| 125 | n/a | } |
|---|
| 126 | n/a | if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) { |
|---|
| 127 | n/a | Py_DECREF(requestObject); |
|---|
| 128 | n/a | Py_DECREF(replyObject); |
|---|
| 129 | n/a | return -1; |
|---|
| 130 | n/a | } |
|---|
| 131 | n/a | res = PyEval_CallObject(handler, args); |
|---|
| 132 | n/a | requestObject->ob_itself.descriptorType = 'null'; |
|---|
| 133 | n/a | requestObject->ob_itself.dataHandle = NULL; |
|---|
| 134 | n/a | replyObject->ob_itself.descriptorType = 'null'; |
|---|
| 135 | n/a | replyObject->ob_itself.dataHandle = NULL; |
|---|
| 136 | n/a | Py_DECREF(args); |
|---|
| 137 | n/a | if (res == NULL) { |
|---|
| 138 | n/a | PySys_WriteStderr("Exception in AE event handler function\\n"); |
|---|
| 139 | n/a | PyErr_Print(); |
|---|
| 140 | n/a | return -1; |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | Py_DECREF(res); |
|---|
| 143 | n/a | return noErr; |
|---|
| 144 | n/a | } |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | PyObject *AEDesc_NewBorrowed(AEDesc *itself) |
|---|
| 147 | n/a | { |
|---|
| 148 | n/a | PyObject *it; |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | it = AEDesc_New(itself); |
|---|
| 151 | n/a | if (it) |
|---|
| 152 | n/a | ((AEDescObject *)it)->ob_owned = 0; |
|---|
| 153 | n/a | return (PyObject *)it; |
|---|
| 154 | n/a | } |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | """ |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | initstuff = initstuff + """ |
|---|
| 159 | n/a | upp_AEIdleProc = NewAEIdleUPP(AEIdleProc); |
|---|
| 160 | n/a | upp_GenericEventHandler = NewAEEventHandlerUPP(GenericEventHandler); |
|---|
| 161 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_New); |
|---|
| 162 | n/a | PyMac_INIT_TOOLBOX_OBJECT_NEW(AEDesc *, AEDesc_NewBorrowed); |
|---|
| 163 | n/a | PyMac_INIT_TOOLBOX_OBJECT_CONVERT(AEDesc, AEDesc_Convert); |
|---|
| 164 | n/a | """ |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | module = MacModule('_AE', 'AE', includestuff, finalstuff, initstuff) |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | class AEDescDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 169 | n/a | getsetlist = [( |
|---|
| 170 | n/a | 'type', |
|---|
| 171 | n/a | 'return PyMac_BuildOSType(self->ob_itself.descriptorType);', |
|---|
| 172 | n/a | None, |
|---|
| 173 | n/a | 'Type of this AEDesc' |
|---|
| 174 | n/a | ), ( |
|---|
| 175 | n/a | 'data', |
|---|
| 176 | n/a | """ |
|---|
| 177 | n/a | PyObject *res; |
|---|
| 178 | n/a | Size size; |
|---|
| 179 | n/a | char *ptr; |
|---|
| 180 | n/a | OSErr err; |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | size = AEGetDescDataSize(&self->ob_itself); |
|---|
| 183 | n/a | if ( (res = PyString_FromStringAndSize(NULL, size)) == NULL ) |
|---|
| 184 | n/a | return NULL; |
|---|
| 185 | n/a | if ( (ptr = PyString_AsString(res)) == NULL ) |
|---|
| 186 | n/a | return NULL; |
|---|
| 187 | n/a | if ( (err=AEGetDescData(&self->ob_itself, ptr, size)) < 0 ) |
|---|
| 188 | n/a | return PyMac_Error(err); |
|---|
| 189 | n/a | return res; |
|---|
| 190 | n/a | """, |
|---|
| 191 | n/a | None, |
|---|
| 192 | n/a | 'The raw data in this AEDesc' |
|---|
| 193 | n/a | )] |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | def __init__(self, name, prefix = None, itselftype = None): |
|---|
| 196 | n/a | GlobalObjectDefinition.__init__(self, name, prefix or name, itselftype or name) |
|---|
| 197 | n/a | self.argref = "*" |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | def outputStructMembers(self): |
|---|
| 200 | n/a | GlobalObjectDefinition.outputStructMembers(self) |
|---|
| 201 | n/a | Output("int ob_owned;") |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | def outputInitStructMembers(self): |
|---|
| 204 | n/a | GlobalObjectDefinition.outputInitStructMembers(self) |
|---|
| 205 | n/a | Output("it->ob_owned = 1;") |
|---|
| 206 | n/a | |
|---|
| 207 | n/a | def outputCleanupStructMembers(self): |
|---|
| 208 | n/a | Output("if (self->ob_owned) AEDisposeDesc(&self->ob_itself);") |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | aedescobject = AEDescDefinition('AEDesc') |
|---|
| 211 | n/a | module.addobject(aedescobject) |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | functions = [] |
|---|
| 214 | n/a | aedescmethods = [] |
|---|
| 215 | n/a | |
|---|
| 216 | n/a | execfile('aegen.py') |
|---|
| 217 | n/a | ##execfile('aedatamodelgen.py') |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | # Manual generator |
|---|
| 220 | n/a | AutoDispose_body = """ |
|---|
| 221 | n/a | int onoff, old; |
|---|
| 222 | n/a | if (!PyArg_ParseTuple(_args, "i", &onoff)) |
|---|
| 223 | n/a | return NULL; |
|---|
| 224 | n/a | old = _self->ob_owned; |
|---|
| 225 | n/a | _self->ob_owned = onoff; |
|---|
| 226 | n/a | _res = Py_BuildValue("i", old); |
|---|
| 227 | n/a | return _res; |
|---|
| 228 | n/a | """ |
|---|
| 229 | n/a | f = ManualGenerator("AutoDispose", AutoDispose_body) |
|---|
| 230 | n/a | f.docstring = lambda: "(int)->int. Automatically AEDisposeDesc the object on Python object cleanup" |
|---|
| 231 | n/a | aedescmethods.append(f) |
|---|
| 232 | n/a | |
|---|
| 233 | n/a | for f in functions: module.add(f) |
|---|
| 234 | n/a | for f in aedescmethods: aedescobject.add(f) |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | SetOutputFileName('_AEmodule.c') |
|---|
| 237 | n/a | module.generate() |
|---|