|
| 1 | +# IBCarbonsupport.py |
| 2 | + |
| 3 | +from macsupport import * |
| 4 | + |
| 5 | +from CarbonEventsscan import RefObjectTypes |
| 6 | + |
| 7 | +# where should this go? macsupport.py? |
| 8 | +CFStringRef = OpaqueByValueType('CFStringRef') |
| 9 | + |
| 10 | +for typ in RefObjectTypes: |
| 11 | + execstr = "%(name)s = OpaqueByValueType('%(name)s')" % {"name": typ} |
| 12 | + exec execstr |
| 13 | + |
| 14 | +# these types will have no methods and will merely be opaque blobs |
| 15 | +# should write getattr and setattr for them? |
| 16 | + |
| 17 | +StructObjectTypes = ["EventTypeSpec", |
| 18 | + "HIPoint", |
| 19 | + "HICommand", |
| 20 | + "EventHotKeyID", |
| 21 | + ] |
| 22 | + |
| 23 | +for typ in StructObjectTypes: |
| 24 | + execstr = "%(name)s = OpaqueType('%(name)s')" % {"name": typ} |
| 25 | + exec execstr |
| 26 | + |
| 27 | +EventTypeSpec_ptr = OpaqueType("EventTypeSpec *", "EventTypeSpec") |
| 28 | + |
| 29 | +# is this the right type for the void * in GetEventParameter |
| 30 | +#void_ptr = FixedInputBufferType(1024) |
| 31 | +void_ptr = stringptr |
| 32 | +# here are some types that are really other types |
| 33 | + |
| 34 | +EventTime = double |
| 35 | +EventTimeout = EventTime |
| 36 | +EventTimerInterval = EventTime |
| 37 | +EventAttributes = UInt32 |
| 38 | +EventParamName = OSType |
| 39 | +EventParamType = OSType |
| 40 | +EventPriority = SInt16 |
| 41 | +EventMask = UInt16 |
| 42 | + |
| 43 | +EventComparatorUPP = FakeType("(EventComparatorUPP)0") |
| 44 | +EventLoopTimerUPP = FakeType("(EventLoopTimerUPP)0") |
| 45 | +EventHandlerUPP = FakeType("(EventHandlerUPP)0") |
| 46 | +EventHandlerUPP = FakeType("(EventHandlerUPP)0") |
| 47 | +EventComparatorProcPtr = FakeType("(EventComparatorProcPtr)0") |
| 48 | +EventLoopTimerProcPtr = FakeType("(EventLoopTimerProcPtr)0") |
| 49 | +EventHandlerProcPtr = FakeType("(EventHandlerProcPtr)0") |
| 50 | + |
| 51 | +CarbonEventsFunction = OSErrFunctionGenerator |
| 52 | +CarbonEventsMethod = OSErrMethodGenerator |
| 53 | + |
| 54 | +includestuff = """ |
| 55 | +#include <Carbon/Carbon.h> |
| 56 | +#include "macglue.h" |
| 57 | +
|
| 58 | +#define USE_MAC_MP_MULTITHREADING 1 |
| 59 | +
|
| 60 | +#if USE_MAC_MP_MULTITHREADING |
| 61 | +static PyThreadState *_save; |
| 62 | +static MPCriticalRegionID reentrantLock; |
| 63 | +#endif /* USE_MAC_MP_MULTITHREADING */ |
| 64 | +
|
| 65 | +extern int CFStringRef_New(CFStringRef *); |
| 66 | +
|
| 67 | +extern int CFStringRef_Convert(PyObject *, CFStringRef *); |
| 68 | +extern int CFBundleRef_Convert(PyObject *, CFBundleRef *); |
| 69 | +
|
| 70 | +int EventTargetRef_Convert(PyObject *, EventTargetRef *); |
| 71 | +PyObject *EventHandlerCallRef_New(EventHandlerCallRef itself); |
| 72 | +PyObject *EventRef_New(EventRef itself); |
| 73 | +
|
| 74 | +/********** EventTypeSpec *******/ |
| 75 | +static PyObject* |
| 76 | +EventTypeSpec_New(EventTypeSpec *in) |
| 77 | +{ |
| 78 | + return Py_BuildValue("ll", in->eventClass, in->eventKind); |
| 79 | +} |
| 80 | +
|
| 81 | +static int |
| 82 | +EventTypeSpec_Convert(PyObject *v, EventTypeSpec *out) |
| 83 | +{ |
| 84 | + if (PyArg_ParseTuple(v, "ll", &(out->eventClass), &(out->eventKind))) |
| 85 | + return 1; |
| 86 | + return NULL; |
| 87 | +} |
| 88 | +
|
| 89 | +/********** end EventTypeSpec *******/ |
| 90 | +
|
| 91 | +/********** HIPoint *******/ |
| 92 | +
|
| 93 | +static PyObject* |
| 94 | +HIPoint_New(HIPoint *in) |
| 95 | +{ |
| 96 | + return Py_BuildValue("ff", in->x, in->y); |
| 97 | +} |
| 98 | +
|
| 99 | +static int |
| 100 | +HIPoint_Convert(PyObject *v, HIPoint *out) |
| 101 | +{ |
| 102 | + if (PyArg_ParseTuple(v, "ff", &(out->x), &(out->y))) |
| 103 | + return 1; |
| 104 | + return NULL; |
| 105 | +} |
| 106 | +
|
| 107 | +/********** end HIPoint *******/ |
| 108 | +
|
| 109 | +/********** EventHotKeyID *******/ |
| 110 | +
|
| 111 | +static PyObject* |
| 112 | +EventHotKeyID_New(EventHotKeyID *in) |
| 113 | +{ |
| 114 | + return Py_BuildValue("ll", in->signature, in->id); |
| 115 | +} |
| 116 | +
|
| 117 | +static int |
| 118 | +EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out) |
| 119 | +{ |
| 120 | + if (PyArg_ParseTuple(v, "ll", &out->signature, &out->id)) |
| 121 | + return 1; |
| 122 | + return NULL; |
| 123 | +} |
| 124 | +
|
| 125 | +/********** end EventHotKeyID *******/ |
| 126 | +
|
| 127 | +/******** handlecommand ***********/ |
| 128 | +
|
| 129 | +pascal OSStatus CarbonEvents_HandleCommand(EventHandlerCallRef handlerRef, Even |
| 130 | +tRef event, void *outPyObject) { |
| 131 | + PyObject *retValue; |
| 132 | + int status; |
| 133 | +
|
| 134 | +#if USE_MAC_MP_MULTITHREADING |
| 135 | + MPEnterCriticalRegion(reentrantLock, kDurationForever); |
| 136 | + PyEval_RestoreThread(_save); |
| 137 | +#endif /* USE_MAC_MP_MULTITHREADING */ |
| 138 | +
|
| 139 | + retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", EventHand |
| 140 | +lerCallRef_New, handlerRef, EventRef_New, event); |
| 141 | + status = PyInt_AsLong(retValue); |
| 142 | +
|
| 143 | +#if USE_MAC_MP_MULTITHREADING |
| 144 | + _save = PyEval_SaveThread(); |
| 145 | + MPExitCriticalRegion(reentrantLock); |
| 146 | +#endif /* USE_MAC_MP_MULTITHREADING */ |
| 147 | +
|
| 148 | + return status; |
| 149 | +} |
| 150 | +
|
| 151 | +/******** end handlecommand ***********/ |
| 152 | +
|
| 153 | +""" |
| 154 | + |
| 155 | +module = MacModule('CarbonEvents', 'CarbonEvents', includestuff, finalstuff, in |
| 156 | +itstuff) |
| 157 | + |
| 158 | +#class CFReleaserObj(GlobalObjectDefinition): |
| 159 | +# def outputFreeIt(self, name): |
| 160 | +# Output("CFRelease(%s);" % name) |
| 161 | + |
| 162 | +for typ in RefObjectTypes: |
| 163 | + execstr = typ + 'object = GlobalObjectDefinition(typ)' |
| 164 | + exec execstr |
| 165 | + module.addobject(eval(typ + 'object')) |
| 166 | + |
| 167 | +functions = [] |
| 168 | +for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEvent |
| 169 | +sscan.py |
| 170 | + # initialize the lists for carbongen to fill |
| 171 | + execstr = typ + 'methods = []' |
| 172 | + exec execstr |
| 173 | + |
| 174 | +execfile('CarbonEventsgen.py') |
| 175 | + |
| 176 | +for f in functions: module.add(f) # add all the functions carboneventsgen |
| 177 | + put in the list |
| 178 | + |
| 179 | +for typ in RefObjectTypes: ## go thru all ObjectT |
| 180 | +ypes as defined in CarbonEventsscan.py |
| 181 | + methods = eval(typ + 'methods') ## get a reference to the method list |
| 182 | +from the main namespace |
| 183 | + obj = eval(typ + 'object') ## get a reference to the obj |
| 184 | +ect |
| 185 | + for m in methods: obj.add(m) ## add each method in the list to the o |
| 186 | +bject |
| 187 | + |
| 188 | +installeventhandler = """ |
| 189 | +EventTypeSpec inSpec; |
| 190 | +PyObject *callback; |
| 191 | +EventHandlerRef outRef; |
| 192 | +OSStatus _err; |
| 193 | +EventHandlerUPP event; |
| 194 | +
|
| 195 | +if (!PyArg_ParseTuple(_args, "O&O", EventTypeSpec_Convert, &inSpec, &callback)) |
| 196 | + return NULL; |
| 197 | +
|
| 198 | +event = NewEventHandlerUPP(CarbonEvents_HandleCommand); |
| 199 | +_err = InstallEventHandler(_self->ob_itself, event, 1, &inSpec, (void *)callbac |
| 200 | +k, &outRef); |
| 201 | +if (_err != noErr) return PyMac_Error(_err); |
| 202 | +
|
| 203 | +return Py_BuildValue("l", outRef); |
| 204 | +""" |
| 205 | + |
| 206 | +f = ManualGenerator("InstallEventHandler", installeventhandler); |
| 207 | +f.docstring = lambda: "(EventTargetRef inTarget, EventTypeSpec inSpec, Method c |
| 208 | +allback) -> (EventHandlerRef outRef)" |
| 209 | +EventTargetRefobject.add(f) |
| 210 | + |
| 211 | +runappeventloop = """ |
| 212 | +#if USE_MAC_MP_MULTITHREADING |
| 213 | +if (MPCreateCriticalRegion(&reentrantLock) != noErr) { |
| 214 | + printf("lock failure\n"); |
| 215 | + return NULL; |
| 216 | +} |
| 217 | +_save = PyEval_SaveThread(); |
| 218 | +#endif /* USE_MAC_MP_MULTITHREADING */ |
| 219 | +
|
| 220 | +RunApplicationEventLoop(); |
| 221 | +
|
| 222 | +#if USE_MAC_MP_MULTITHREADING |
| 223 | +PyEval_RestoreThread(_save); |
| 224 | +
|
| 225 | +MPDeleteCriticalRegion(reentrantLock); |
| 226 | +#endif /* USE_MAC_MP_MULTITHREADING */ |
| 227 | +
|
| 228 | +Py_INCREF(Py_None); |
| 229 | +
|
| 230 | +return Py_None; |
| 231 | +""" |
| 232 | + |
| 233 | +f = ManualGenerator("RunApplicationEventLoop", runappeventloop); |
| 234 | +f.docstring = lambda: "() -> ()" |
| 235 | +module.add(f) |
| 236 | + |
| 237 | +SetOutputFileName('_CarbonEvents.c') |
| 238 | +module.generate() |
| 239 | + |
| 240 | +import os |
| 241 | +os.system("python setup.py build") |
0 commit comments