iDevGames Forums
Game input in Cocoa - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Game input in Cocoa (/thread-6663.html)

Game input in Cocoa - aegidian - Oct 24, 2003 03:49 AM

I'm looking for some way to grab joystick/controller input for a Cocoa based game.

I'm pretty uncertain about how difficult wrapping HID manager functions with obj C classes might be, or even if it's possible.

AmelioGIL is mentioned in the FAQ but the links there are borken.

Has anyone any experiences or source-code I could lean on? Please.

Game input in Cocoa - codemattic - Oct 24, 2003 04:45 AM

Quote:Originally posted by aegidian
AmelioGIL is mentioned in the FAQ but the links there are borken.
<> works for me.

Game input in Cocoa - aegidian - Oct 24, 2003 05:03 AM

Aha - the links in the faq are capitalised and the server case-sensitive!

me go fix now!

Thank you!

Game input in Cocoa - MattDiamond - Oct 24, 2003 11:39 AM

I'm considering using Amelio myself. The coding side seems really sweet. But the config screen is not ideal. Also, one of the other uDG programmers mentioned that it wasn't working for all his users, though that could just be his problem.

Any alternatives to Amelio I should know about? Does Amelio have enough hooks to allow one to write their own config screen? Or would I just be using HID utilities at that point?

Game input in Cocoa - OneSadCookie - Oct 24, 2003 03:15 PM

Amelio does come with the source, so if you don't like something, you can always go in and fix it... I'd say it's got to be easier than working straight with the HID utilities...

Game input in Cocoa - GoodDoug - Oct 27, 2003 05:32 PM

I just had to rip Amelio out of my uDG project. It worked on my machine, but not on others... I have wrapped the HID Utilities stuff from the Apple Developer Examples to do what I want. It's not the easiest thing to grok, but once you do, it's pretty straightforward.

my old uDG entry ZedNought used the wrappers I wrote. I'll be updating them for this years entry.

Sorry i don't have much more information other than "Caveat emptor" with Amelio

Game input in Cocoa - OneSadCookie - Oct 27, 2003 05:49 PM

I wrote my own HID stuff (straight, without even the HID utilities) over the weekend. The state of the documentation and sample code was a bit frustrating, but I'm quite happy with what I ended up with.

Hopefully I'll be able to wrap this up as sample code or a library at some point, but I think I should get more than just the keyboard working before I do Rasp

Game input in Cocoa - geowar - Nov 4, 2003 12:27 PM

HID Utilities: <

I'm currently the DRI for the HID Utilities. I almost have a new version ready for posting. It sounds like I also need to include some documentation to explain how to use it. For the existing code read thru the <HID_Utilities_External.h> file.

The "interesting" API's are:

HIDBuildDeviceList - This creates a list of HID devices.
HIDReleaseDeviceList - call this when you're done with the list.
HIDGetFirst/NextDevice - used to iterate the devices
HIDGetFirst/NextDeviceElement - used to iterate elements of a device.
HIDGetElementNameFromVendorProduct[Cookie|Usage] - Get text name of an

HIDQueueDevice - Add all device elements to event queue
HIDDequeueDevice - remove all device elements from event queue
HIDQueueElement - Add element to event queue
HIDDequeueElement - remove element from event queue
HIDGetEvent - poll queue for events

You may incorporate the entire HID Utilities into your project as-is or cut & paste the pieces that you need.

If you have any specific questions or suggestions about the HID Utilities you may email me at <>.

George Warner, (408)974-0668
Schizophrenic Optimization Scientists
Apple Developer Technical Support (DTS)

As an extra service to our developers, we in Apple's Developer Technical
Support (DTS), are encouraging all of our developers to check out our cool
*NEW* web-sites for access to lots of new sample code and to see if any of
your questions have already been answered.

Sample code: <>
Technical Docs: <>
Mailing Lists: <>
Tech Info Lib: <>

Game input in Cocoa - aegidian - Nov 4, 2003 03:47 PM

Wade Williams posted the following response to the recent cries for help:-

In a general sense, there's nothing wrong with the HID Utilities code. (I say in a general sense because I know those with complex needs have things they'd like it to do different/better)

What is wrong from your point of view is that there's no real documentation, so to read the headers and sample code and figure it out assumes you have some experience.

So, with that said, let's try to make it as easy as possible.

1) Download the HID Utilities sample code.

2) Double-click the HID Utilities slib.pbproj file to open it in Project Builder.

3) Click build. This will create a build folder that will contain libHIDUtilities.a once it's finished building. (Actually building it may not be necessary, but it doesn't hurt.)

4) Add libHIDUtilities.a to your project.

5) Add #include "HID_Utilities_External.h" and #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
to your joystick code file.

6) Build a list of devices by doing:
    HIDBuildDeviceList( NULL, NULL );
    if (! HIDHaveDeviceList() )
        return NULL;
    num_devices = HIDCountDevices();
7) Loop through the devices, looking for the one you're interested in. You find it by matching its usage and usagePage. For example, for a game pad, you'd look for a usagePage of kHIDPage_GenericDesktop and a usage of kHIDUsage_GD_GamePad. (All this stuff is found in IOHIDUsageTables.h)

    pRecDevice    deviceCandidate = NULL, *stored_device[100 ];
    int i, foundDevices = 0;

    deviceCandidate = HIDGetFirstDevice();

    i = 0;
    while ( i < num_devices )
        if ( deviceCandidate )
            //here I filter out keyboards, mice and totally unrelated devices
            if ( (deviceCandidate->usage == kHIDUsage_GD_Keyboard)
                    || (deviceCandidate->usagePage != kHIDPage_GenericDesktop) ||
                        (deviceCandidate->usage == kHIDUsage_GD_Mouse) )
                if ( HIDIsValidDevice( deviceCandidate ) )
                    //skip this one and just go on to the next one
                    deviceCandidate = HIDGetNextDevice( deviceCandidate );
                    //handle error
                if ( HIDIsValidDevice( deviceCandidate ) )
                    //store the device in a list of devices we're interested in
                    stored_device[foundDevices] =  deviceCandidate;
                    deviceCandidate = HIDGetNextDevice( deviceCandidate );
                    //handle error
8) Now that you've got the device, go through its elements and find the axis or button you're interested in:
    if ( HIDIsValidDevice( device ) )
        elRec=HIDGetFirstDeviceElement( js_devices[id].device, kHIDElementTypeInput );
        if ( elRec )
            switch (elRec->usagePage)
                case kHIDPage_GenericDesktop:
                    switch ( elRec->usage )
                        case kHIDUsage_GD_X:
                                x_axis = elRec;
                        case kHIDUsage_GD_Y:
                                y_axis = elRec;
                        case kHIDUsage_GD_GamePad:
                                pad = elRec;

        //if the right element was not found, continue
        //looping through the rest of the elements by
        //calling HIDGetNextDeviceElement() until you
        //find the right one or it returns NULL
9) Now that you've got the actual element, you can query it's value:
    if ( HIDIsValidElement( devicePtr, elementPtr ) )
        value = HIDGetElementValue(devicePtr, elementPtr);
I hope this helps.


[edited for line wrap - w_reade]