| 1 | n/a | # IBCarbonsupport.py |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | from macsupport import * |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | from CarbonEvtscan import RefObjectTypes |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | # where should this go? macsupport.py? |
|---|
| 8 | n/a | CFStringRef = OpaqueByValueType('CFStringRef') |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | for typ in RefObjectTypes: |
|---|
| 11 | n/a | execstr = "%(name)s = OpaqueByValueType('%(name)s')" % {"name": typ} |
|---|
| 12 | n/a | exec execstr |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | if 0: |
|---|
| 16 | n/a | # these types will have no methods and will merely be opaque blobs |
|---|
| 17 | n/a | # should write getattr and setattr for them? |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | StructObjectTypes = ["EventTypeSpec", |
|---|
| 20 | n/a | "HIPoint", |
|---|
| 21 | n/a | "HICommand", |
|---|
| 22 | n/a | "EventHotKeyID", |
|---|
| 23 | n/a | ] |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | for typ in StructObjectTypes: |
|---|
| 26 | n/a | execstr = "%(name)s = OpaqueType('%(name)s')" % {"name": typ} |
|---|
| 27 | n/a | exec execstr |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | EventHotKeyID = OpaqueByValueType("EventHotKeyID", "EventHotKeyID") |
|---|
| 30 | n/a | EventTypeSpec_ptr = OpaqueType("EventTypeSpec", "EventTypeSpec") |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | # is this the right type for the void * in GetEventParameter |
|---|
| 33 | n/a | #void_ptr = FixedInputBufferType(1024) |
|---|
| 34 | n/a | void_ptr = stringptr |
|---|
| 35 | n/a | # here are some types that are really other types |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | class MyVarInputBufferType(VarInputBufferType): |
|---|
| 38 | n/a | def passInput(self, name): |
|---|
| 39 | n/a | return "%s__len__, %s__in__" % (name, name) |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | MyInBuffer = MyVarInputBufferType('char', 'long', 'l') # (buf, len) |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | EventTime = double |
|---|
| 44 | n/a | EventTimeout = EventTime |
|---|
| 45 | n/a | EventTimerInterval = EventTime |
|---|
| 46 | n/a | EventAttributes = UInt32 |
|---|
| 47 | n/a | EventParamName = OSType |
|---|
| 48 | n/a | EventParamType = OSType |
|---|
| 49 | n/a | EventPriority = SInt16 |
|---|
| 50 | n/a | EventMask = UInt16 |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | EventComparatorUPP = FakeType("(EventComparatorUPP)0") |
|---|
| 53 | n/a | EventLoopTimerUPP = FakeType("(EventLoopTimerUPP)0") |
|---|
| 54 | n/a | EventHandlerUPP = FakeType("(EventHandlerUPP)0") |
|---|
| 55 | n/a | EventHandlerUPP = FakeType("(EventHandlerUPP)0") |
|---|
| 56 | n/a | EventComparatorProcPtr = FakeType("(EventComparatorProcPtr)0") |
|---|
| 57 | n/a | EventLoopTimerProcPtr = FakeType("(EventLoopTimerProcPtr)0") |
|---|
| 58 | n/a | EventHandlerProcPtr = FakeType("(EventHandlerProcPtr)0") |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | CarbonEventsFunction = OSErrFunctionGenerator |
|---|
| 61 | n/a | CarbonEventsMethod = OSErrMethodGenerator |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | class EventHandlerRefMethod(OSErrMethodGenerator): |
|---|
| 64 | n/a | def precheck(self): |
|---|
| 65 | n/a | OutLbrace('if (_self->ob_itself == NULL)') |
|---|
| 66 | n/a | Output('PyErr_SetString(CarbonEvents_Error, "Handler has been removed");') |
|---|
| 67 | n/a | Output('return NULL;') |
|---|
| 68 | n/a | OutRbrace() |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | RgnHandle = OpaqueByValueType("RgnHandle", "ResObj") |
|---|
| 72 | n/a | GrafPtr = OpaqueByValueType("GrafPtr", "GrafObj") |
|---|
| 73 | n/a | MouseTrackingResult = UInt16 |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | includestuff = includestuff + r""" |
|---|
| 77 | n/a | #include <Carbon/Carbon.h> |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | extern int CFStringRef_New(CFStringRef *); |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | extern int CFStringRef_Convert(PyObject *, CFStringRef *); |
|---|
| 82 | n/a | extern int CFBundleRef_Convert(PyObject *, CFBundleRef *); |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | int EventTargetRef_Convert(PyObject *, EventTargetRef *); |
|---|
| 85 | n/a | PyObject *EventHandlerCallRef_New(EventHandlerCallRef itself); |
|---|
| 86 | n/a | PyObject *EventRef_New(EventRef itself); |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | /********** EventTypeSpec *******/ |
|---|
| 89 | n/a | static PyObject* |
|---|
| 90 | n/a | EventTypeSpec_New(EventTypeSpec *in) |
|---|
| 91 | n/a | { |
|---|
| 92 | n/a | return Py_BuildValue("ll", in->eventClass, in->eventKind); |
|---|
| 93 | n/a | } |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | static int |
|---|
| 96 | n/a | EventTypeSpec_Convert(PyObject *v, EventTypeSpec *out) |
|---|
| 97 | n/a | { |
|---|
| 98 | n/a | if (PyArg_Parse(v, "(O&l)", |
|---|
| 99 | n/a | PyMac_GetOSType, &(out->eventClass), |
|---|
| 100 | n/a | &(out->eventKind))) |
|---|
| 101 | n/a | return 1; |
|---|
| 102 | n/a | return NULL; |
|---|
| 103 | n/a | } |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | /********** end EventTypeSpec *******/ |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | /********** HIPoint *******/ |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | #if 0 /* XXX doesn't compile */ |
|---|
| 110 | n/a | static PyObject* |
|---|
| 111 | n/a | HIPoint_New(HIPoint *in) |
|---|
| 112 | n/a | { |
|---|
| 113 | n/a | return Py_BuildValue("ff", in->x, in->y); |
|---|
| 114 | n/a | } |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | static int |
|---|
| 117 | n/a | HIPoint_Convert(PyObject *v, HIPoint *out) |
|---|
| 118 | n/a | { |
|---|
| 119 | n/a | if (PyArg_ParseTuple(v, "ff", &(out->x), &(out->y))) |
|---|
| 120 | n/a | return 1; |
|---|
| 121 | n/a | return NULL; |
|---|
| 122 | n/a | } |
|---|
| 123 | n/a | #endif |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | /********** end HIPoint *******/ |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | /********** EventHotKeyID *******/ |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | static PyObject* |
|---|
| 130 | n/a | EventHotKeyID_New(EventHotKeyID *in) |
|---|
| 131 | n/a | { |
|---|
| 132 | n/a | return Py_BuildValue("ll", in->signature, in->id); |
|---|
| 133 | n/a | } |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | static int |
|---|
| 136 | n/a | EventHotKeyID_Convert(PyObject *v, EventHotKeyID *out) |
|---|
| 137 | n/a | { |
|---|
| 138 | n/a | if (PyArg_ParseTuple(v, "ll", &out->signature, &out->id)) |
|---|
| 139 | n/a | return 1; |
|---|
| 140 | n/a | return NULL; |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | /********** end EventHotKeyID *******/ |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | /******** myEventHandler ***********/ |
|---|
| 146 | n/a | |
|---|
| 147 | n/a | static EventHandlerUPP myEventHandlerUPP; |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | static pascal OSStatus |
|---|
| 150 | n/a | myEventHandler(EventHandlerCallRef handlerRef, EventRef event, void *outPyObject) { |
|---|
| 151 | n/a | PyObject *retValue; |
|---|
| 152 | n/a | int status; |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | retValue = PyObject_CallFunction((PyObject *)outPyObject, "O&O&", |
|---|
| 155 | n/a | EventHandlerCallRef_New, handlerRef, |
|---|
| 156 | n/a | EventRef_New, event); |
|---|
| 157 | n/a | if (retValue == NULL) { |
|---|
| 158 | n/a | PySys_WriteStderr("Error in event handler callback:\n"); |
|---|
| 159 | n/a | PyErr_Print(); /* this also clears the error */ |
|---|
| 160 | n/a | status = noErr; /* complain? how? */ |
|---|
| 161 | n/a | } else { |
|---|
| 162 | n/a | if (retValue == Py_None) |
|---|
| 163 | n/a | status = noErr; |
|---|
| 164 | n/a | else if (PyInt_Check(retValue)) { |
|---|
| 165 | n/a | status = PyInt_AsLong(retValue); |
|---|
| 166 | n/a | } else |
|---|
| 167 | n/a | status = noErr; /* wrong object type, complain? */ |
|---|
| 168 | n/a | Py_DECREF(retValue); |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | return status; |
|---|
| 172 | n/a | } |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | /******** end myEventHandler ***********/ |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | """ |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | initstuff = initstuff + """ |
|---|
| 179 | n/a | myEventHandlerUPP = NewEventHandlerUPP(myEventHandler); |
|---|
| 180 | n/a | """ |
|---|
| 181 | n/a | module = MacModule('_CarbonEvt', 'CarbonEvents', includestuff, finalstuff, initstuff) |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | |
|---|
| 186 | n/a | class EventHandlerRefObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 187 | n/a | def outputStructMembers(self): |
|---|
| 188 | n/a | Output("%s ob_itself;", self.itselftype) |
|---|
| 189 | n/a | Output("PyObject *ob_callback;") |
|---|
| 190 | n/a | def outputInitStructMembers(self): |
|---|
| 191 | n/a | Output("it->ob_itself = %sitself;", self.argref) |
|---|
| 192 | n/a | Output("it->ob_callback = NULL;") |
|---|
| 193 | n/a | def outputFreeIt(self, name): |
|---|
| 194 | n/a | OutLbrace("if (self->ob_itself != NULL)") |
|---|
| 195 | n/a | Output("RemoveEventHandler(self->ob_itself);") |
|---|
| 196 | n/a | Output("Py_DECREF(self->ob_callback);") |
|---|
| 197 | n/a | OutRbrace() |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | class MyGlobalObjectDefinition(PEP253Mixin, GlobalObjectDefinition): |
|---|
| 200 | n/a | pass |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | for typ in RefObjectTypes: |
|---|
| 203 | n/a | if typ == 'EventHandlerRef': |
|---|
| 204 | n/a | EventHandlerRefobject = EventHandlerRefObjectDefinition('EventHandlerRef') |
|---|
| 205 | n/a | else: |
|---|
| 206 | n/a | execstr = typ + 'object = MyGlobalObjectDefinition(typ)' |
|---|
| 207 | n/a | exec execstr |
|---|
| 208 | n/a | module.addobject(eval(typ + 'object')) |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | functions = [] |
|---|
| 212 | n/a | for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEventsscan.py |
|---|
| 213 | n/a | # initialize the lists for carbongen to fill |
|---|
| 214 | n/a | execstr = typ + 'methods = []' |
|---|
| 215 | n/a | exec execstr |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | execfile('CarbonEventsgen.py') |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | for f in functions: module.add(f) # add all the functions carboneventsgen put in the list |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | for typ in RefObjectTypes: ## go thru all ObjectTypes as defined in CarbonEventsscan.py |
|---|
| 224 | n/a | methods = eval(typ + 'methods') ## get a reference to the method list from the main namespace |
|---|
| 225 | n/a | obj = eval(typ + 'object') ## get a reference to the object |
|---|
| 226 | n/a | for m in methods: obj.add(m) ## add each method in the list to the object |
|---|
| 227 | n/a | |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | removeeventhandler = """ |
|---|
| 230 | n/a | OSStatus _err; |
|---|
| 231 | n/a | if (_self->ob_itself == NULL) { |
|---|
| 232 | n/a | PyErr_SetString(CarbonEvents_Error, "Handler has been removed"); |
|---|
| 233 | n/a | return NULL; |
|---|
| 234 | n/a | } |
|---|
| 235 | n/a | if (!PyArg_ParseTuple(_args, "")) |
|---|
| 236 | n/a | return NULL; |
|---|
| 237 | n/a | _err = RemoveEventHandler(_self->ob_itself); |
|---|
| 238 | n/a | if (_err != noErr) return PyMac_Error(_err); |
|---|
| 239 | n/a | _self->ob_itself = NULL; |
|---|
| 240 | n/a | Py_DECREF(_self->ob_callback); |
|---|
| 241 | n/a | _self->ob_callback = NULL; |
|---|
| 242 | n/a | Py_INCREF(Py_None); |
|---|
| 243 | n/a | _res = Py_None; |
|---|
| 244 | n/a | return _res;""" |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | f = ManualGenerator("RemoveEventHandler", removeeventhandler); |
|---|
| 247 | n/a | f.docstring = lambda: "() -> None" |
|---|
| 248 | n/a | EventHandlerRefobject.add(f) |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | installeventhandler = """ |
|---|
| 252 | n/a | EventTypeSpec inSpec; |
|---|
| 253 | n/a | PyObject *callback; |
|---|
| 254 | n/a | EventHandlerRef outRef; |
|---|
| 255 | n/a | OSStatus _err; |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | if (!PyArg_ParseTuple(_args, "O&O", EventTypeSpec_Convert, &inSpec, &callback)) |
|---|
| 258 | n/a | return NULL; |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | _err = InstallEventHandler(_self->ob_itself, myEventHandlerUPP, 1, &inSpec, (void *)callback, &outRef); |
|---|
| 261 | n/a | if (_err != noErr) return PyMac_Error(_err); |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | _res = EventHandlerRef_New(outRef); |
|---|
| 264 | n/a | if (_res != NULL) { |
|---|
| 265 | n/a | ((EventHandlerRefObject*)_res)->ob_callback = callback; |
|---|
| 266 | n/a | Py_INCREF(callback); |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | return _res;""" |
|---|
| 269 | n/a | |
|---|
| 270 | n/a | f = ManualGenerator("InstallEventHandler", installeventhandler); |
|---|
| 271 | n/a | f.docstring = lambda: "(EventTypeSpec inSpec, Method callback) -> (EventHandlerRef outRef)" |
|---|
| 272 | n/a | EventTargetRefobject.add(f) |
|---|
| 273 | n/a | |
|---|
| 274 | n/a | # This may not be the best, but at least it lets you get the raw data back into python as a string. You'll have to cut it up yourself and parse the result. |
|---|
| 275 | n/a | |
|---|
| 276 | n/a | geteventparameter = """ |
|---|
| 277 | n/a | UInt32 bufferSize; |
|---|
| 278 | n/a | EventParamName inName; |
|---|
| 279 | n/a | EventParamType inType; |
|---|
| 280 | n/a | OSErr _err; |
|---|
| 281 | n/a | void * buffer; |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | if (!PyArg_ParseTuple(_args, "O&O&", PyMac_GetOSType, &inName, PyMac_GetOSType, &inType)) |
|---|
| 284 | n/a | return NULL; |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | /* Figure out the size by passing a null buffer to GetEventParameter */ |
|---|
| 287 | n/a | _err = GetEventParameter(_self->ob_itself, inName, inType, NULL, 0, &bufferSize, NULL); |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | if (_err != noErr) |
|---|
| 290 | n/a | return PyMac_Error(_err); |
|---|
| 291 | n/a | buffer = PyMem_NEW(char, bufferSize); |
|---|
| 292 | n/a | if (buffer == NULL) |
|---|
| 293 | n/a | return PyErr_NoMemory(); |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | _err = GetEventParameter(_self->ob_itself, inName, inType, NULL, bufferSize, NULL, buffer); |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | if (_err != noErr) { |
|---|
| 298 | n/a | PyMem_DEL(buffer); |
|---|
| 299 | n/a | return PyMac_Error(_err); |
|---|
| 300 | n/a | } |
|---|
| 301 | n/a | _res = Py_BuildValue("s#", buffer, bufferSize); |
|---|
| 302 | n/a | PyMem_DEL(buffer); |
|---|
| 303 | n/a | return _res; |
|---|
| 304 | n/a | """ |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | f = ManualGenerator("GetEventParameter", geteventparameter); |
|---|
| 307 | n/a | f.docstring = lambda: "(EventParamName eventName, EventParamType eventType) -> (String eventParamData)" |
|---|
| 308 | n/a | EventRefobject.add(f) |
|---|
| 309 | n/a | |
|---|
| 310 | n/a | SetOutputFileName('_CarbonEvtmodule.c') |
|---|
| 311 | n/a | module.generate() |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | ##import os |
|---|
| 314 | n/a | ##os.system("python setup.py build") |
|---|