[300] | 1 | #include "Python.h"
|
---|
| 2 | #include "HID_Utilities_External.h"
|
---|
| 3 | #include <IOKit/hid/IOHIDUsageTables.h>
|
---|
| 4 | #import <Cocoa/Cocoa.h>
|
---|
| 5 |
|
---|
| 6 | static hu_device_t *device = NULL;
|
---|
| 7 |
|
---|
| 8 | static void callback(void *target, IOReturn result, void *refcon, void *sender) {
|
---|
| 9 | IOHIDEventStruct event;
|
---|
| 10 | while (HIDGetEvent(device, &event)) {
|
---|
| 11 | if (event.type != kIOHIDElementTypeInput_Button ||
|
---|
| 12 | event.elementCookie != (void *)4) continue;
|
---|
| 13 | Nanoseconds nanoTimestamp = AbsoluteToNanoseconds(event.timestamp);
|
---|
| 14 | UInt64 realTimestamp = UnsignedWideToUInt64(nanoTimestamp);
|
---|
| 15 | NSTimeInterval timestamp = ((NSTimeInterval)realTimestamp)/1000000000;
|
---|
| 16 | [NSApp sendEvent: [NSEvent otherEventWithType: NSApplicationDefined
|
---|
| 17 | location: NSZeroPoint
|
---|
| 18 | modifierFlags: 0
|
---|
| 19 | timestamp: timestamp
|
---|
| 20 | windowNumber: 0
|
---|
| 21 | context: nil
|
---|
| 22 | subtype: 0
|
---|
| 23 | data1: event.value
|
---|
| 24 | data2: 0]];
|
---|
| 25 | }
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | static PyObject
|
---|
| 29 | *HIDRemote_connect(PyObject *self, PyObject *args) {
|
---|
| 30 | if (device != NULL) {
|
---|
| 31 | PyErr_SetString(PyExc_OSError, "already connected");
|
---|
| 32 | return NULL;
|
---|
| 33 | }
|
---|
| 34 | if (!HIDBuildDeviceList(kHIDPage_Consumer, kHIDUsage_Csmr_ConsumerControl)) {
|
---|
| 35 | PyErr_SetString(PyExc_OSError, "can't get HID device list");
|
---|
| 36 | return NULL;
|
---|
| 37 | }
|
---|
| 38 | device = HIDGetFirstDevice();
|
---|
| 39 | if (device == NULL) {
|
---|
| 40 | HIDReleaseDeviceList();
|
---|
| 41 | PyErr_SetString(PyExc_OSError, "no HID consumer control devices");
|
---|
| 42 | return NULL;
|
---|
| 43 | }
|
---|
| 44 | if (HIDQueueDevice(device) != kIOReturnSuccess) {
|
---|
| 45 | HIDDequeueDevice(device);
|
---|
| 46 | HIDReleaseDeviceList();
|
---|
| 47 | PyErr_SetString(PyExc_OSError, "can't queue HID consumer control device");
|
---|
| 48 | return NULL;
|
---|
| 49 | }
|
---|
| 50 | if (HIDSetQueueCallback(device, callback, NULL, NULL) != kIOReturnSuccess) {
|
---|
| 51 | HIDDequeueDevice(device);
|
---|
| 52 | HIDReleaseDeviceList();
|
---|
| 53 | PyErr_SetString(PyExc_OSError, "can't register queue callback");
|
---|
| 54 | return NULL;
|
---|
| 55 | }
|
---|
| 56 | Py_INCREF(Py_None);
|
---|
| 57 | return Py_None;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | static PyObject
|
---|
| 61 | *HIDRemote_disconnect(PyObject *self, PyObject *args) {
|
---|
| 62 | if (device == NULL) {
|
---|
| 63 | PyErr_SetString(PyExc_OSError, "not connected");
|
---|
| 64 | return NULL;
|
---|
| 65 | }
|
---|
| 66 | HIDDequeueDevice(device);
|
---|
| 67 | HIDReleaseDeviceList();
|
---|
| 68 | Py_INCREF(Py_None);
|
---|
| 69 | return Py_None;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | static PyMethodDef HIDRemotemodule_methods[] = {
|
---|
| 73 | {"connect", HIDRemote_connect, METH_NOARGS,
|
---|
| 74 | "connect()\n\n"
|
---|
| 75 | "Connect to the first consumer control USB HID device and begin receiving events."},
|
---|
| 76 | {"disconnect", HIDRemote_disconnect, METH_NOARGS,
|
---|
| 77 | "disconnect()\n\n"
|
---|
| 78 | "Disconnect from the attached consumer control USB HID device and stop receiving events."},
|
---|
| 79 | {NULL, NULL, 0, NULL}
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | PyMODINIT_FUNC
|
---|
| 83 | initHIDRemote(void) {
|
---|
| 84 | (void)Py_InitModule("HIDRemote", HIDRemotemodule_methods);
|
---|
| 85 | }
|
---|