Skip to content

Instantly share code, notes, and snippets.

@yamaya
Forked from badsectoracula/hidcode.c
Created April 27, 2017 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaya/7dcb831bb54072bab14821d163793d7c to your computer and use it in GitHub Desktop.
Save yamaya/7dcb831bb54072bab14821d163793d7c 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