Jump to content

  • Log In with Google      Sign In   
  • Create Account


hr_rabe

Member Since 25 Feb 2012
Offline Last Active Jun 11 2012 04:48 AM
-----

Topics I've Started

[SOLVED] Mapping problem with OsX Gamepad controls via HIDManager

25 February 2012 - 07:05 PM

I am currently trying to map gamepad controls in a Cocoa application, using Apple's HIDManager api.

However, I ran into a problem, and can't find an easy solution to it.

From examination, it seems to me, that the only way I can identify distinct control updates (button presses and stick movement) is via an integer identifier, I get back from the HIDElementGetCookie(element) function. But this identifier is completely vendor specific as far as I know.

For example, on my simple logitech Gamepad, the Cookie 18 describes the X-Axis of the left analog stick, whereas on the PS3 controller the same Cookie value describes the upper-right shoulder button.

Is there any technique or reliable other method, how I can identify specific elements of a controller?

To add some code to my question, here is the HID callback function, that handles the control update:
void gamepadAction(void* inContext, IOReturn inResult,
				   void* inSender, IOHIDValueRef value) {
  
	IOHIDElementRef element = IOHIDValueGetElement(value);
	Boolean isElement = CFGetTypeID(element) == IOHIDElementGetTypeID();
	if (!isElement)
		return;
	IOHIDElementCookie cookie = IOHIDElementGetCookie(element);
	IOHIDElementType type = IOHIDElementGetType(element);
	if (18 != cookie && 19 != cookie && 21 != cookie){
		NSLog(@"Gamepad talked rubbish at %d of type %d", cookie, type);
		return;
	}
  
	IOHIDElementCollectionType ctype = IOHIDElementGetCollectionType(element);
	CFStringRef name = IOHIDElementGetName(element);
  
	long elementValue = IOHIDValueGetIntegerValue(value);
	NSLog(@"Gamepad talked: %d / %d - %@ [%i] = %ld", type, ctype, name,
		  cookie, elementValue);
  
	if (18 == cookie || 19 == cookie || 21 == cookie){
		MyGameController* obj = (__bridge MyGameController*) inContext;
		float axisScale = 128;
	  
		float axisvalue = ((float)(elementValue-axisScale)/axisScale);
		if (elementValue <= axisScale +1 &&
			elementValue >= axisScale -1)
			axisvalue = 0.0;
		if (18 == cookie)
			[obj setLatitude:axisvalue];
		else if (21 == cookie)
			[obj setCenterDistance:axisvalue];
		else if (19 == cookie)
			[obj setLongitude:axisvalue];
	}  
}

HowTo start writing games for Mac Os X using ObjC

25 February 2012 - 05:29 PM

Hello Everyone,

I am an experienced developer in "the serious business", and am currently trying my hand at game development.

Since I am starting my journey on a Macintosh, I also started to write for that platform and with the tools available here. This means Objective C, Cocoa and OpenGL/OpenAL.

I haven't found any good tutorials on that topic yet, and so I started to document my work in a step by step diary/tutorial as I am learning the ropes.

Maybe that stuff is also of interest to someone here. To entries about basic OpenGL wiring and OpenAL processing are already done. More will follow as I am progressing. I am currently writing the third entry about Gamepad control via the HIDManager.

Sorry, if that is the wrong place for it (it forums are quite big) or if posts like this are not welcome at all, in which case I can delete it (hopefully). But I thought, that maybe of use to someone here:

Part 1: http://dragonsandbyt...pengl-and-coco/
Part 2: https://dragonsandby...ound-of-openal/


Regards,

Mr. Raven

PARTNERS