Game input in Cocoa

Member
Posts: 28
Joined: 2003.10
Post: #1
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.

-- Giles Williams
Oolite - retro space-sim agogo
Quote this message in a reply
Member
Posts: 304
Joined: 2002.04
Post: #2
Quote:Originally posted by aegidian
AmelioGIL is mentioned in the FAQ but the links there are borken.
<http://doomlegacy.idevgames.com/amelio/> works for me.
Quote this message in a reply
Member
Posts: 28
Joined: 2003.10
Post: #3
Aha - the links in the faq are capitalised and the server case-sensitive!

me go fix now!

Thank you!

-- Giles Williams
Oolite - retro space-sim agogo
Quote this message in a reply
Moderator
Posts: 434
Joined: 2002.09
Post: #4
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?

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
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...
Quote this message in a reply
Member
Posts: 104
Joined: 2002.04
Post: #6
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
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
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
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.01
Post: #8
HID Utilities: <http://developer.apple.com/samplecode/Sa...dware/HID_
Manager/HID_Utilities_Source.htm>.

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
element.

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 <geowar@apple.com>.

--
Enjoy,
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: <http://developer.apple.com/samplecode/index.html>
Technical Docs: <http://developer.apple.com/techpubs/index.html>
Mailing Lists: <http://www.lists.apple.com/>
Tech Info Lib: <http://kbase.info.apple.com>
Quote this message in a reply
Member
Posts: 28
Joined: 2003.10
Post: #9
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:
Code:
    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)

Code:
    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 );
                }
                else
                {
                    //handle error
                }
            }
            else
            {
                if ( HIDIsValidDevice( deviceCandidate ) )
                {
                    //store the device in a list of devices we're interested in
                    stored_device[foundDevices] =  deviceCandidate;
                    deviceCandidate = HIDGetNextDevice( deviceCandidate );
                    foundDevices++;
                }
                else
                {
                    //handle error
                }
        
                
            }
            
            i++;
        }
    }
8) Now that you've got the device, go through its elements and find the axis or button you're interested in:
Code:
    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;
                                break;
                        case kHIDUsage_GD_Y:
                                y_axis = elRec;
                                break;
                        case kHIDUsage_GD_GamePad:
                                pad = elRec;
                                break;
                        //etc
                    }
            }

        //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:
Code:
    if ( HIDIsValidElement( devicePtr, elementPtr ) )
    {        
        value = HIDGetElementValue(devicePtr, elementPtr);
    }
I hope this helps.

Wade

[edited for line wrap - w_reade]

-- Giles Williams
Oolite - retro space-sim agogo
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cocoa only input/event mechanism? Mr. Yuk 7 4,624 Aug 21, 2005 05:42 AM
Last Post: reubert
  [ANN] Amelio Game Input Library (G.I.L) v0.9 calumr 27 7,862 Feb 2, 2003 12:24 PM
Last Post: OneSadCookie