Skip to content

Instantly share code, notes, and snippets.

@badsectoracula
Created July 9, 2016 02:33
Show Gist options
  • Save badsectoracula/f72f42ec1dd50da97b6b92022f72d3c4 to your computer and use it in GitHub Desktop.
Save badsectoracula/f72f42ec1dd50da97b6b92022f72d3c4 to your computer and use it in GitHub Desktop.
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hidsystem/event_status_driver.h>
static int hid_mousex, hid_mousey;
static void hidCallback(void* context, IOReturn result, void* sender, IOHIDValueRef valueRef)
{
IOHIDElementRef element = IOHIDValueGetElement(valueRef);
if (mouse_capture > 0 && IOHIDElementGetUsagePage(element) == kHIDPage_GenericDesktop) {
int value = (int)IOHIDValueGetIntegerValue(valueRef);
switch (IOHIDElementGetUsage(element)) {
case kHIDUsage_GD_X:
hid_mousex += value;
break;
case kHIDUsage_GD_Y:
hid_mousey += value;
break;
}
}
}
... init ...
sys_log(SYSLOG_NONE, "Initializing the HID manager");
hidManager = IOHIDManagerCreate(kCFAllocatorSystemDefault, kIOHIDOptionsTypeNone);
if (!hidManager) {
sys_log(SYSLOG_FATAL, "Failed to initialize the HID manager");
eng_shutdown();
return 0;
}
sys_log(SYSLOG_NONE, "Initializing the HID callbacks");
IOHIDManagerSetDeviceMatching(hidManager, NULL);
IOHIDManagerRegisterInputValueCallback(hidManager, hidCallback, NULL);
IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
if (IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
sys_log(SYSLOG_FATAL, "Failed to open the HID manager");
eng_shutdown();
return 0;
}
... shutdown ...
IOHIDManagerUnscheduleFromRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
CFRelease(hidManager);
... main loop ...
if (hid_mousex) {
eng_key(KEY_MOUSE_X, hid_mousex);
hid_mousex = 0;
}
if (hid_mousey) {
eng_key(KEY_MOUSE_Y, hid_mousey);
hid_mousey = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment