Gamepad and joystick support on Mac OS X in user space
Asked Answered
Z

1

7

I have been searching through how to do gamepad and joystick support on Mac for some days and all resources that I found seems to suggest a pre-installed driver along with using Apple's HID API, which works.

The drawback about this approach is that each joystick and gamepad will require another kernel extension to be loaded, so it can be recognized by HID manager, or at least a code less Info.plist saying it conforms to the earlier installed driver. For instance, when I have an 360 Xbox driver KEXT in house, the Xbox controller from Microsoft will work, but not the Logitech one (I tried F710).

As Apple suggests the application that uses a gamepad or joystick should be able to do themselves at user space without introducing any KEXT stuff. Is there a way to do it?

The thing I had in mind was something like using IORegistry or IOUSB API to get the device when they get plugged in (USB Prober shows it at least). Then somehow get the description of the device, then use that description to register the device as a HID one. Then the whole HID manager can be used.

Am I on the right track? Or is there any other way to do this?

Since IOKit API actually provided keywords like kHIDUsage_GD_Joystick, and there's an ForceFeedback.h library, I suppose Apple designed their HID API with joystick and force feedback in mind. That's the slim hope I had that this might work.

Some reference documentation and open source project:

Zischke answered 10/10, 2012 at 22:18 Comment(3)
I don't have relevant hardware to hand, but have you tried looking at the IOService nodes for a variety of joysticks/gamepads in the IORegistryExplorer (or ioreg) to see what they have in common? Then, filter the devices in your app/library accordingly.Barone
Sorry for late responds. I have two gamepads at hand, one 360 Controller from Microsoft and one from powerA. Some values are shared in common but I didn't see how to actually determine the gamepad configureation (buttons number, joystick number etc.).Zischke
One thing that two controller shares in common is probably the "USB Prodcut Name" is both "Controller". I'm a little suspicious to trust blindly on string value though. Anyway, it seems that I will need some more knowledge from the IOService and IOUsb space to see if this is doable. Will update this question when I get more time to work on this. Right now, the HID manager works with driver :)Zischke
Z
2

After revisiting this, I found out the solution to be operating directly on the file descriptors of the device. libusb is an excellent library which greatly simplify your life on this and they have Mac supported.

xboxdrv link is a great example on how to operate on file socket using libusb.

In pseudo code, it should look like this:

  1. enumerate device
  2. detect kernel driver and detach it if possible
  3. open device
  4. file off a initial transfer
  5. wait on the callback function to handle msg and error properly
  6. start run loop or select on fd to call libusb_event_handle

Check libusb for more info link.

Zischke answered 24/1, 2013 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.