glfw/src/cocoa_joystick.m

567 lines
18 KiB
Mathematica
Raw Normal View History

2010-09-07 15:34:51 +00:00
//========================================================================
// GLFW - An OpenGL library
2010-09-07 15:34:51 +00:00
// Platform: Cocoa/NSOpenGL
2010-09-07 15:41:26 +00:00
// API Version: 3.0
2010-09-07 15:34:51 +00:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would
// be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not
// be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
//========================================================================
#include "internal.h"
2010-09-16 01:25:36 +00:00
2011-09-18 19:05:00 +00:00
#include <unistd.h>
#include <ctype.h>
#include <mach/mach.h>
#include <mach/mach_error.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
#include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
//------------------------------------------------------------------------
// Joystick element information
//------------------------------------------------------------------------
2012-01-29 14:30:01 +00:00
typedef struct
{
2011-09-18 19:05:00 +00:00
IOHIDElementCookie Cookie;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
long Value;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
long Min;
long Max;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
long MinReport;
long MaxReport;
2012-01-29 14:30:01 +00:00
} _glfwJoystickElement;
2011-09-18 19:05:00 +00:00
//------------------------------------------------------------------------
// Joystick information & state
//------------------------------------------------------------------------
2012-01-29 14:30:01 +00:00
typedef struct
{
2011-09-18 19:05:00 +00:00
int Present;
char Product[256];
2012-01-29 14:30:01 +00:00
IOHIDDeviceInterface** Interface;
2011-09-18 19:05:00 +00:00
int NumAxes;
int NumButtons;
int NumHats;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
CFMutableArrayRef Axes;
2012-01-29 14:30:01 +00:00
CFMutableArrayRef Buttons;
CFMutableArrayRef Hats;
} _glfwJoystick;
_glfwJoystick _glfwJoysticks[GLFW_JOYSTICK_LAST + 1];
2011-09-18 19:05:00 +00:00
2012-01-29 14:30:01 +00:00
void GetElementsCFArrayHandler(const void* value, void* parameter);
2011-09-18 19:05:00 +00:00
2012-01-29 14:30:01 +00:00
//========================================================================
// Adds an element to the specified joystick
//========================================================================
static void addJoystickElement(_glfwJoystick* joystick, CFTypeRef refElement)
2011-09-18 19:05:00 +00:00
{
long elementType, usagePage, usage;
CFTypeRef refElementType, refUsagePage, refUsage;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
refElementType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementTypeKey));
2012-01-29 14:30:01 +00:00
refUsagePage = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsagePageKey));
refUsage = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsageKey));
2011-09-18 19:05:00 +00:00
CFMutableArrayRef elementsArray = NULL;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
CFNumberGetValue(refElementType, kCFNumberLongType, &elementType);
CFNumberGetValue(refUsagePage, kCFNumberLongType, &usagePage);
CFNumberGetValue(refUsage, kCFNumberLongType, &usage);
2012-01-29 14:30:01 +00:00
if ((elementType == kIOHIDElementTypeInput_Axis) ||
2011-09-18 19:05:00 +00:00
(elementType == kIOHIDElementTypeInput_Button) ||
(elementType == kIOHIDElementTypeInput_Misc))
{
switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
{
case kHIDPage_GenericDesktop:
2012-01-29 14:30:01 +00:00
{
2011-09-18 19:05:00 +00:00
switch (usage)
{
case kHIDUsage_GD_X:
case kHIDUsage_GD_Y:
case kHIDUsage_GD_Z:
case kHIDUsage_GD_Rx:
case kHIDUsage_GD_Ry:
case kHIDUsage_GD_Rz:
case kHIDUsage_GD_Slider:
case kHIDUsage_GD_Dial:
case kHIDUsage_GD_Wheel:
joystick->NumAxes++;
elementsArray = joystick->Axes;
break;
case kHIDUsage_GD_Hatswitch:
joystick->NumHats++;
elementsArray = joystick->Hats;
break;
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
break;
2012-01-29 14:30:01 +00:00
}
2011-09-18 19:05:00 +00:00
case kHIDPage_Button:
joystick->NumButtons++;
elementsArray = joystick->Buttons;
break;
default:
break;
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
if (elementsArray)
{
long number;
CFTypeRef refType;
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* element = (_glfwJoystickElement*) _glfwMalloc(sizeof(_glfwJoystickElement));
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
CFArrayAppendValue(elementsArray, element);
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementCookieKey));
if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
element->Cookie = (IOHIDElementCookie) number;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMinKey));
if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
element->MinReport = element->Min = number;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMaxKey));
if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
element->MaxReport = element->Max = number;
}
}
else
{
CFTypeRef refElementTop = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementKey));
if (refElementTop)
{
CFTypeID type = CFGetTypeID (refElementTop);
if (type == CFArrayGetTypeID()) /* if element is an array */
{
CFRange range = {0, CFArrayGetCount (refElementTop)};
CFArrayApplyFunction(refElementTop, range, GetElementsCFArrayHandler, joystick);
}
}
}
}
2012-01-29 14:30:01 +00:00
//========================================================================
// Adds an element to the specified joystick
//========================================================================
void GetElementsCFArrayHandler(const void* value, void* parameter)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
if (CFGetTypeID(value) == CFDictionaryGetTypeID())
addJoystickElement((_glfwJoystick*) parameter, (CFTypeRef) value);
2011-09-18 19:05:00 +00:00
}
2012-01-29 14:30:01 +00:00
//========================================================================
// Returns the value of the specified element of the specified joystick
//========================================================================
static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* element)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
IOReturn result = kIOReturnSuccess;
IOHIDEventStruct hidEvent;
hidEvent.value = 0;
if (joystick && element && joystick->Interface)
{
result = (*(joystick->Interface))->getElementValue(joystick->Interface, element->Cookie, &hidEvent);
if (kIOReturnSuccess == result)
{
2011-09-18 19:05:00 +00:00
/* record min and max for auto calibration */
if (hidEvent.value < element->MinReport)
element->MinReport = hidEvent.value;
if (hidEvent.value > element->MaxReport)
element->MaxReport = hidEvent.value;
2012-01-29 14:30:01 +00:00
}
}
/* auto user scale */
return (long) hidEvent.value;
2011-09-18 19:05:00 +00:00
}
2012-01-29 14:30:01 +00:00
//========================================================================
// Removes the specified joystick
//========================================================================
static void removeJoystick(_glfwJoystick* joystick)
2011-09-18 19:05:00 +00:00
{
if (joystick->Present)
{
joystick->Present = GL_FALSE;
2012-01-29 14:30:01 +00:00
for (int i = 0; i < joystick->NumAxes; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Axes, i);
_glfwFree(axes);
2011-09-18 19:05:00 +00:00
}
CFArrayRemoveAllValues(joystick->Axes);
joystick->NumAxes = 0;
2012-01-29 14:30:01 +00:00
for (int i = 0; i < joystick->NumButtons; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* button =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Buttons, i);
_glfwFree(button);
2011-09-18 19:05:00 +00:00
}
CFArrayRemoveAllValues(joystick->Buttons);
joystick->NumButtons = 0;
2012-01-29 14:30:01 +00:00
for (int i = 0; i < joystick->NumHats; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* hat =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Hats, i);
_glfwFree(hat);
2011-09-18 19:05:00 +00:00
}
CFArrayRemoveAllValues(joystick->Hats);
joystick->Hats = 0;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
(*(joystick->Interface))->close(joystick->Interface);
(*(joystick->Interface))->Release(joystick->Interface);
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
joystick->Interface = NULL;
}
}
2012-01-29 14:30:01 +00:00
//========================================================================
// Callback for user-initiated joystick removal
//========================================================================
static void removalCallback(void* target, IOReturn result, void* refcon, void* sender)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
removeJoystick((_glfwJoystick*) refcon);
2011-09-18 19:05:00 +00:00
}
2012-01-29 14:30:01 +00:00
//========================================================================
// Polls for joystick events and updates GFLW state
//========================================================================
static void pollJoystickEvents(void)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
for (int i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystick* joystick = &_glfwJoysticks[i];
2011-09-18 19:05:00 +00:00
if (joystick->Present)
{
2012-01-29 14:30:01 +00:00
for (CFIndex i = 0; i < joystick->NumButtons; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* button =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Buttons, i);
button->Value = getElementValue(joystick, button);
2011-09-18 19:05:00 +00:00
}
2012-01-29 14:30:01 +00:00
for (CFIndex i = 0; i < joystick->NumAxes; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Axes, i);
axes->Value = getElementValue(joystick, axes);
2011-09-18 19:05:00 +00:00
}
}
}
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
//========================================================================
// Initialize joystick interface
//========================================================================
void _glfwInitJoysticks(void)
{
2012-01-29 14:30:01 +00:00
int deviceCounter = 0;
IOReturn result = kIOReturnSuccess;
mach_port_t masterPort = 0;
io_iterator_t objectIterator = 0;
CFMutableDictionaryRef hidMatchDictionary = NULL;
io_object_t ioHIDDeviceObject = 0;
2011-09-18 19:05:00 +00:00
result = IOMasterPort(bootstrap_port, &masterPort);
hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
if (kIOReturnSuccess != result || !hidMatchDictionary)
return;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
result = IOServiceGetMatchingServices(masterPort, hidMatchDictionary, &objectIterator);
if (kIOReturnSuccess != result)
return;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
if (!objectIterator) /* there are no joysticks */
return;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
while ((ioHIDDeviceObject = IOIteratorNext(objectIterator)))
{
2012-01-29 14:30:01 +00:00
CFMutableDictionaryRef hidProperties = 0;
kern_return_t result;
CFTypeRef refCF = 0;
IOCFPlugInInterface** ppPlugInInterface = NULL;
2011-09-18 19:05:00 +00:00
HRESULT plugInResult = S_OK;
SInt32 score = 0;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
long usagePage, usage;
2012-01-29 14:30:01 +00:00
result = IORegistryEntryCreateCFProperties(ioHIDDeviceObject,
2011-09-18 19:05:00 +00:00
&hidProperties,
kCFAllocatorDefault,
kNilOptions);
2012-01-29 14:30:01 +00:00
if (result != kIOReturnSuccess)
continue;
2011-09-18 19:05:00 +00:00
/* Check device type */
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
if (refCF)
CFNumberGetValue(refCF, kCFNumberLongType, &usagePage);
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
if (refCF)
CFNumberGetValue (refCF, kCFNumberLongType, &usage);
2012-01-29 14:30:01 +00:00
if ((usagePage != kHIDPage_GenericDesktop) ||
(usage != kHIDUsage_GD_Joystick &&
usage != kHIDUsage_GD_GamePad &&
usage != kHIDUsage_GD_MultiAxisController))
2011-09-18 19:05:00 +00:00
{
/* We don't interested in this device */
continue;
}
2012-01-29 14:30:01 +00:00
_glfwJoystick* joystick = &_glfwJoysticks[deviceCounter];
2011-09-18 19:05:00 +00:00
joystick->Present = GL_TRUE;
2012-01-29 14:30:01 +00:00
result = IOCreatePlugInInterfaceForService(ioHIDDeviceObject,
kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&ppPlugInInterface,
&score);
2011-09-18 19:05:00 +00:00
if (kIOReturnSuccess != result)
return;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
plugInResult = (*ppPlugInInterface)->QueryInterface(
2012-01-29 14:30:01 +00:00
ppPlugInInterface,
CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
2011-09-18 19:05:00 +00:00
(void *) &(joystick->Interface));
2012-01-29 14:30:01 +00:00
if (plugInResult != S_OK)
return;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
(*ppPlugInInterface)->Release(ppPlugInInterface);
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
(*(joystick->Interface))->open(joystick->Interface, 0);
2012-01-29 14:30:01 +00:00
(*(joystick->Interface))->setRemovalCallback(joystick->Interface,
removalCallback,
joystick,
joystick);
2011-09-18 19:05:00 +00:00
/* Get product string */
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
if (refCF)
2012-01-29 14:30:01 +00:00
{
CFStringGetCString(refCF,
(char*) &(joystick->Product),
256,
CFStringGetSystemEncoding());
}
joystick->NumAxes = 0;
2011-09-18 19:05:00 +00:00
joystick->NumButtons = 0;
2012-01-29 14:30:01 +00:00
joystick->NumHats = 0;
joystick->Axes = CFArrayCreateMutable(NULL, 0, NULL);
2011-09-18 19:05:00 +00:00
joystick->Buttons = CFArrayCreateMutable(NULL, 0, NULL);
2012-01-29 14:30:01 +00:00
joystick->Hats = CFArrayCreateMutable(NULL, 0, NULL);
CFTypeRef refTopElement = CFDictionaryGetValue(hidProperties,
CFSTR(kIOHIDElementKey));
2011-09-18 19:05:00 +00:00
CFTypeID type = CFGetTypeID(refTopElement);
if (type == CFArrayGetTypeID())
{
2012-01-29 14:30:01 +00:00
CFRange range = { 0, CFArrayGetCount(refTopElement) };
CFArrayApplyFunction(refTopElement,
range,
GetElementsCFArrayHandler,
(void*) joystick);
2011-09-18 19:05:00 +00:00
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
deviceCounter++;
}
}
//========================================================================
// Close all opened joystick handles
//========================================================================
void _glfwTerminateJoysticks(void)
{
2012-01-29 14:30:01 +00:00
for (int i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystick* joystick = &_glfwJoysticks[i];
removeJoystick(joystick);
2011-09-18 19:05:00 +00:00
}
}
2010-09-16 01:25:36 +00:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 15:34:51 +00:00
//========================================================================
// Determine joystick capabilities
//========================================================================
2011-09-18 19:05:00 +00:00
int _glfwPlatformGetJoystickParam(int joy, int param)
2010-09-07 15:34:51 +00:00
{
2011-09-18 19:05:00 +00:00
if (!_glfwJoysticks[joy].Present)
{
// TODO: Figure out if this is an error
return 0;
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
switch (param)
{
case GLFW_PRESENT:
return GL_TRUE;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
case GLFW_AXES:
return (int) CFArrayGetCount(_glfwJoysticks[joy].Axes);
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
case GLFW_BUTTONS:
return (int) CFArrayGetCount(_glfwJoysticks[joy].Buttons);
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
default:
break;
}
2012-01-29 14:30:01 +00:00
2010-09-07 15:34:51 +00:00
return 0;
}
2011-09-18 19:05:00 +00:00
2010-09-07 15:34:51 +00:00
//========================================================================
// Get joystick axis positions
//========================================================================
2012-01-29 14:30:01 +00:00
int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes)
2010-09-07 15:34:51 +00:00
{
2011-09-18 19:05:00 +00:00
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0;
2012-01-29 14:30:01 +00:00
_glfwJoystick joystick = _glfwJoysticks[joy];
2011-09-18 19:05:00 +00:00
if (!joystick.Present)
{
// TODO: Figure out if this is an error
return 0;
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
numaxes = numaxes < joystick.NumAxes ? numaxes : joystick.NumAxes;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
// Update joystick state
2012-01-29 14:30:01 +00:00
pollJoystickEvents();
for (int i = 0; i < numaxes; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.Axes, i);
long readScale = axes->MaxReport - axes->MinReport;
if (readScale == 0)
pos[i] = axes->Value;
else
pos[i] = (2.0f * (axes->Value - axes->MinReport) / readScale) - 1.0f;
2011-09-18 19:05:00 +00:00
printf("%ld, %ld, %ld\n", axes->Value, axes->MinReport, axes->MaxReport);
2012-01-29 14:30:01 +00:00
if (i & 1)
2011-09-18 19:05:00 +00:00
pos[i] = -pos[i];
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
return numaxes;
2010-09-07 15:34:51 +00:00
}
2011-09-18 19:05:00 +00:00
2010-09-07 15:34:51 +00:00
//========================================================================
// Get joystick button states
//========================================================================
2012-01-29 14:30:01 +00:00
int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
2011-09-18 19:05:00 +00:00
int numbuttons)
2010-09-07 15:34:51 +00:00
{
2011-09-18 19:05:00 +00:00
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0;
2012-01-29 14:30:01 +00:00
_glfwJoystick joystick = _glfwJoysticks[joy];
2011-09-18 19:05:00 +00:00
if (!joystick.Present)
{
// TODO: Figure out if this is an error
return 0;
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
numbuttons = numbuttons < joystick.NumButtons ? numbuttons : joystick.NumButtons;
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
// Update joystick state
2012-01-29 14:30:01 +00:00
pollJoystickEvents();
for (int i = 0; i < numbuttons; i++)
2011-09-18 19:05:00 +00:00
{
2012-01-29 14:30:01 +00:00
_glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.Buttons, i);
2011-09-18 19:05:00 +00:00
buttons[i] = button->Value ? GLFW_PRESS : GLFW_RELEASE;
}
2012-01-29 14:30:01 +00:00
2011-09-18 19:05:00 +00:00
return numbuttons;
2010-09-07 15:34:51 +00:00
}
2011-09-18 19:05:00 +00:00