Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a gamepad button press on OSX 10.5 and higher?

Tags:

cocoa

How do I detect a button press on a USB gamepad on OSX 10.5 and higher?

I can't wrap my head around the ridiculously complex HID Manager (even though apparently it was simplified with 10.5), and the code samples at Apple have thousands of lines of code that would take days to understand and isolate what I need, so I'd appreciate if someone posts a simple, and fully coded solution for this isolated problem.


EDIT: so far all answers are links to source code or semi obscure libraries for all kinds of HID devices, which will require more research time than what I'd like to invest on this. I am starting a bounty to get an actual snippet of code that solves this simple problem (using an external library or not).


EDIT POS BOUNTY: thanks to all for you help; but unfortunately the answer that has been automatically selected by the system is not working for me, can't figure out why; and the author has not yet replied to my comments. Any insight would be appreciated, but until a fix is found, anyone looking for resources on this topic should take this answer with a pinch of salt.

like image 506
Steph Thirion Avatar asked Feb 10 '10 04:02

Steph Thirion


1 Answers

First, import the IOKit Framework and include it like this in your header and implementation files:

#import <IOKit/hid/IOHIDLib.h>

In your header file you might want to have this variable:

IOHIDManagerRef hidManager;

Then add this to your implementation:

void gamepadWasAdded(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef device) {
  NSLog(@"Gamepad was plugged in");
}

void gamepadWasRemoved(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef device) {
  NSLog(@"Gamepad was unplugged");
}

void gamepadAction(void* inContext, IOReturn inResult, void* inSender, IOHIDValueRef value) {
  NSLog(@"Gamepad talked!");
  IOHIDElementRef element = IOHIDValueGetElement(value);
  NSLog(@"Element: %@", element);
  int elementValue = IOHIDValueGetIntegerValue(value);
  NSLog(@"Element value: %i", elementValue);
}

-(void) setupGamepad {
  hidManager = IOHIDManagerCreate( kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  NSMutableDictionary* criterion = [[NSMutableDictionary alloc] init];
  [criterion setObject: [NSNumber numberWithInt: kHIDPage_GenericDesktop] forKey: (NSString*)CFSTR(kIOHIDDeviceUsagePageKey)];
  [criterion setObject: [NSNumber numberWithInt: kHIDUsage_GD_GamePad] forKey: (NSString*)CFSTR(kIOHIDDeviceUsageKey)];
  IOHIDManagerSetDeviceMatching(hidManager, criterion);
  IOHIDManagerRegisterDeviceMatchingCallback(hidManager, gamepadWasAdded, (void*)self);
  IOHIDManagerRegisterDeviceRemovalCallback(hidManager, gamepadWasRemoved, (void*)self);
  IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  IOReturn tIOReturn = IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone);
  IOHIDManagerRegisterInputValueCallback(hidManager, gamepadAction, (void*)self);
}

And call setupGamepad from your awakeFromNib. You can modify the variable "kHIDUsage_GD_GamePad" to register other devices as well. I coded the stuff above by reading the http://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/HID/hid.pdf document examples step by step and comparing it to the "real" source code at http://abstractable.net/enjoy

Good luck.

like image 102
funkensturm Avatar answered Jan 12 '23 01:01

funkensturm