Guido van Rossum | 17448e2 | 1995-01-30 11:53:55 +0000 | [diff] [blame^] | 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 | |
| 7 | from macsupport import * |
| 8 | |
| 9 | |
| 10 | AEArrayType = Type("AEArrayType", "c") |
| 11 | AESendMode = Type("AESendMode", "l") |
| 12 | AESendPriority = Type("AESendPriority", "h") |
| 13 | AEInteractAllowed = Type("AEInteractAllowed", "b") |
| 14 | |
| 15 | |
| 16 | AEEventClass = OSTypeType('AEEventClass') |
| 17 | AEEventID = OSTypeType('AEEventID') |
| 18 | AEKeyword = OSTypeType('AEKeyword') |
| 19 | DescType = OSTypeType('DescType') |
| 20 | |
| 21 | |
| 22 | AEDesc = OpaqueType('AEDesc') |
| 23 | AEDesc_ptr = OpaqueType('AEDesc') |
| 24 | |
| 25 | AEAddressDesc = OpaqueType('AEAddressDesc', 'AEDesc') |
| 26 | AEAddressDesc_ptr = OpaqueType('AEAddressDesc', 'AEDesc') |
| 27 | |
| 28 | AEDescList = OpaqueType('AEDescList', 'AEDesc') |
| 29 | AEDescList_ptr = OpaqueType('AEDescList', 'AEDesc') |
| 30 | |
| 31 | AERecord = OpaqueType('AERecord', 'AEDesc') |
| 32 | AERecord_ptr = OpaqueType('AERecord', 'AEDesc') |
| 33 | |
| 34 | AppleEvent = OpaqueType('AppleEvent', 'AEDesc') |
| 35 | AppleEvent_ptr = OpaqueType('AppleEvent', 'AEDesc') |
| 36 | |
| 37 | |
| 38 | class EHType(Type): |
| 39 | def __init__(self, name = 'EventHandler', format = ''): |
| 40 | Type.__init__(self, name, format) |
| 41 | def declare(self, name): |
| 42 | Output("AEEventHandlerProcPtr %s__proc__ = GenericEventHandler;", name) |
| 43 | Output("PyObject *%s;", name) |
| 44 | def getargsFormat(self): |
| 45 | return "O" |
| 46 | def getargsArgs(self, name): |
| 47 | return "&%s" % name |
| 48 | def passInput(self, name): |
| 49 | return "%s__proc__, (long)%s" % (name, name) |
| 50 | def passOutput(self, name): |
| 51 | return "&%s__proc__, (long *)&%s" % (name, name) |
| 52 | def mkvalueFormat(self): |
| 53 | return "O" |
| 54 | def mkvalueArgs(self, name): |
| 55 | return name |
| 56 | |
| 57 | class EHNoRefConType(EHType): |
| 58 | def passInput(self, name): |
| 59 | return "GenericEventHandler" |
| 60 | |
| 61 | EventHandler = EHType() |
| 62 | EventHandlerNoRefCon = EHNoRefConType() |
| 63 | |
| 64 | |
| 65 | IdleProcPtr = FakeType("AEIdleProc") |
| 66 | EventFilterProcPtr = FakeType("(AEFilterProcPtr)0") |
| 67 | NMRecPtr = FakeType("(NMRecPtr)0") |
| 68 | EventHandlerProcPtr = FakeType("GenericEventHandler") |
| 69 | AlwaysFalse = FakeType("0") |
| 70 | |
| 71 | |
| 72 | AEFunction = OSErrFunctionGenerator |
| 73 | AEMethod = OSErrMethodGenerator |
| 74 | |
| 75 | |
| 76 | includestuff = includestuff + """ |
| 77 | #include <AppleEvents.h> |
| 78 | |
| 79 | #ifdef THINK_C |
| 80 | #define AEFilterProcPtr EventFilterProcPtr |
| 81 | #define AEEventHandlerProcPtr EventHandlerProcPtr |
| 82 | #endif |
| 83 | |
| 84 | static pascal OSErr GenericEventHandler(); /* Forward */ |
| 85 | |
| 86 | static pascal Boolean AEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn) |
| 87 | { |
| 88 | return !PyMac_Idle(); |
| 89 | } |
| 90 | """ |
| 91 | |
| 92 | finalstuff = finalstuff + """ |
| 93 | static pascal OSErr |
| 94 | GenericEventHandler(const AppleEvent *request, AppleEvent *reply, long refcon) |
| 95 | { |
| 96 | PyObject *handler = (PyObject *)refcon; |
| 97 | AEDescObject *requestObject, *replyObject; |
| 98 | PyObject *args, *res; |
| 99 | if ((requestObject = (AEDescObject *)AEDesc_New(request)) == NULL) { |
| 100 | return -1; |
| 101 | } |
| 102 | if ((replyObject = (AEDescObject *)AEDesc_New(reply)) == NULL) { |
| 103 | Py_DECREF(requestObject); |
| 104 | return -1; |
| 105 | } |
| 106 | if ((args = Py_BuildValue("OO", requestObject, replyObject)) == NULL) { |
| 107 | Py_DECREF(requestObject); |
| 108 | Py_DECREF(replyObject); |
| 109 | return -1; |
| 110 | } |
| 111 | res = PyEval_CallObject(handler, args); |
| 112 | requestObject->ob_itself.descriptorType = 'null'; |
| 113 | requestObject->ob_itself.dataHandle = NULL; |
| 114 | replyObject->ob_itself.descriptorType = 'null'; |
| 115 | replyObject->ob_itself.dataHandle = NULL; |
| 116 | Py_DECREF(args); |
| 117 | if (res == NULL) |
| 118 | return -1; |
| 119 | Py_DECREF(res); |
| 120 | return noErr; |
| 121 | } |
| 122 | """ |
| 123 | |
| 124 | module = MacModule('AE', 'AE', includestuff, finalstuff, initstuff) |
| 125 | |
| 126 | class AEDescDefiniton(ObjectDefinition): |
| 127 | |
| 128 | def __init__(self, name, prefix = None, itselftype = None): |
| 129 | ObjectDefinition.__init__(self, name, prefix or name, itselftype or name) |
| 130 | self.argref = "*" |
| 131 | |
| 132 | def outputFreeIt(self, name): |
| 133 | Output("AEDisposeDesc(&%s);", name) |
| 134 | |
| 135 | def outputGetattrHook(self): |
| 136 | Output(""" |
| 137 | if (strcmp(name, "type") == 0) |
| 138 | return PyMac_BuildOSType(self->ob_itself.descriptorType); |
| 139 | if (strcmp(name, "data") == 0) { |
| 140 | PyObject *res; |
| 141 | char state; |
| 142 | state = HGetState(self->ob_itself.dataHandle); |
| 143 | HLock(self->ob_itself.dataHandle); |
| 144 | res = PyString_FromStringAndSize( |
| 145 | *self->ob_itself.dataHandle, |
| 146 | GetHandleSize(self->ob_itself.dataHandle)); |
| 147 | HUnlock(self->ob_itself.dataHandle); |
| 148 | HSetState(self->ob_itself.dataHandle, state); |
| 149 | return res; |
| 150 | } |
| 151 | if (strcmp(name, "__members__") == 0) |
| 152 | return Py_BuildValue("[ss]", "data", "type"); |
| 153 | """) |
| 154 | |
| 155 | |
| 156 | aedescobject = AEDescDefiniton('AEDesc') |
| 157 | module.addobject(aedescobject) |
| 158 | |
| 159 | functions = [] |
| 160 | aedescmethods = [] |
| 161 | |
| 162 | execfile('aegen.py') |
| 163 | |
| 164 | for f in functions: module.add(f) |
| 165 | for f in aedescmethods: aedescobject.add(f) |
| 166 | |
| 167 | SetOutputFileName('AEmodule.c') |
| 168 | module.generate() |