views:

597

answers:

4

I thought I was a decent programmer until I tried writing gamepad code for OS X. Now I feel deeply useless.

Does anyone know of any code that I can legally use in my (non-free) game?

Is it really this hard to talk to a gamepad on OS X? What am I missing?

A: 

No code, but communicating with gamepads and the like is pretty straightforward with the InputSprocket mechanism. What was the precise problem you had?

Brian Mitchell
Game sprockets don't appear to be supported on OS X. The recommended APIs are here, although I can't use the Leopard-only ones:http://developer.apple.com/referencelibrary/Games/idxHumanInterfaceDeviceForceFeedback-date.html
+3  A: 

Check out the HID Manager, especially the new HID Manager APIs in Leopard. It's somewhat verbose, but the essence of it is that you can get callbacks when devices are attached and detached, and get callbacks when events from those devices are enqueued.

If you're working with Cocoa, Dave Dribin has DDHidLib which provides a nicer Objective-C API atop the HID Manager, and runs on Tiger as well.

Chris Hanson
The HID Manager is what I've been tussling with (although I'm not using the Leopard-specific stuff).All I can say is that if anyone ever wants to discover their limits as a coder, trying to get gamepad data out of HID Manager is a good place for it.
If you have the option, you're best to use the new Leopard-specific API. The old API is very sketchy, and I doubt it will be supported in Snow Leopard. I found a major security flaw (CVE-2007-0724) in it a while back, and it took Apple 11.5 months to come up with a patch.
splicer
A: 

Turns out the answer was Apple's HID_Utilities, which (somewhat) simplifies the job of talking to HID Manager.

John Carmack really hit the nail on the head when he said that Apple don't care about games...

+1  A: 

The quickest way to get gamepad events on OSX is to use SDL, the game library. You don't have to use the whole library, you can just init the joystick subsystem and then poll or wait for SDL_JOYAXISMOTION and SDL_JOYBUTTONUP/DOWN events. SDL has an LGPL license, so you can dynamically link to it in your non-free game.

Easy!

Rhythmic Fistman
I looked at the SDL code originally and reluctantly decided I couldn't use even LGPL code. But as it turns out, the SDL joystick code is a cut'n'paste of Apple's HID_Utilities code which is what I ended up using, so it worked out the same way.