1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 18:58:52 -05:00
glfw/src/iokit_joystick.m

507 lines
16 KiB
Mathematica
Raw Normal View History

2010-09-07 11:34:51 -04:00
//========================================================================
// GLFW 3.1 IOKit - www.glfw.org
2010-09-07 11:34:51 -04:00
//------------------------------------------------------------------------
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
// Copyright (c) 2012 Torsten Walluhn <tw@mad-cad.net>
2010-09-07 11:34:51 -04:00
//
// 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"
2011-09-18 15:05:00 -04:00
#include <unistd.h>
#include <ctype.h>
2012-01-29 09:38:22 -05:00
2011-09-18 15:05:00 -04:00
#include <mach/mach.h>
#include <mach/mach_error.h>
2012-01-29 09:38:22 -05:00
2011-09-18 15:05:00 -04:00
#include <CoreFoundation/CoreFoundation.h>
#include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
//------------------------------------------------------------------------
// Joystick element information
//------------------------------------------------------------------------
2012-01-29 09:30:01 -05:00
typedef struct
{
2012-01-29 09:37:29 -05:00
IOHIDElementCookie cookie;
2012-01-29 09:30:01 -05:00
2012-01-29 09:37:29 -05:00
long min;
long max;
2012-01-29 09:30:01 -05:00
2012-01-29 09:37:29 -05:00
long minReport;
long maxReport;
2012-01-29 09:30:01 -05:00
} _GLFWjoyelement;
2011-09-18 15:05:00 -04:00
static void getElementsCFArrayHandler(const void* value, void* parameter);
2011-09-18 15:05:00 -04:00
2012-01-29 09:30:01 -05:00
// Adds an element to the specified joystick
2013-02-04 07:22:10 -05:00
//
static void addJoystickElement(_GLFWjoystickIOKit* joystick, CFTypeRef elementRef)
2011-09-18 15:05:00 -04:00
{
long elementType, usagePage, usage;
CFMutableArrayRef elementsArray = NULL;
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
CFNumberGetValue(CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementTypeKey)),
kCFNumberLongType, &elementType);
CFNumberGetValue(CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementUsagePageKey)),
kCFNumberLongType, &usagePage);
CFNumberGetValue(CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementUsageKey)),
kCFNumberLongType, &usage);
2012-01-29 09:30:01 -05:00
if ((elementType == kIOHIDElementTypeInput_Axis) ||
2011-09-18 15:05:00 -04:00
(elementType == kIOHIDElementTypeInput_Button) ||
(elementType == kIOHIDElementTypeInput_Misc))
{
2012-08-14 15:58:22 -04:00
switch (usagePage)
2011-09-18 15:05:00 -04:00
{
case kHIDPage_GenericDesktop:
2012-01-29 09:30:01 -05:00
{
2011-09-18 15:05:00 -04: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:
2013-04-24 13:25:42 -04:00
elementsArray = joystick->axisElements;
2011-09-18 15:05:00 -04:00
break;
case kHIDUsage_GD_Hatswitch:
2013-04-24 13:25:42 -04:00
elementsArray = joystick->hatElements;
2011-09-18 15:05:00 -04:00
break;
}
2012-01-29 09:30:01 -05:00
2011-09-18 15:05:00 -04:00
break;
2012-01-29 09:30:01 -05:00
}
2011-09-18 15:05:00 -04:00
case kHIDPage_Button:
2013-04-24 13:25:42 -04:00
elementsArray = joystick->buttonElements;
2011-09-18 15:05:00 -04:00
break;
default:
break;
}
2012-01-29 09:30:01 -05:00
2011-09-18 15:05:00 -04:00
if (elementsArray)
{
long number;
2013-04-24 13:25:42 -04:00
CFTypeRef numberRef;
_GLFWjoyelement* element = calloc(1, sizeof(_GLFWjoyelement));
2012-01-29 09:30:01 -05:00
2011-09-18 15:05:00 -04:00
CFArrayAppendValue(elementsArray, element);
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
numberRef = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementCookieKey));
if (numberRef && CFNumberGetValue(numberRef, kCFNumberLongType, &number))
2012-01-29 09:37:29 -05:00
element->cookie = (IOHIDElementCookie) number;
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
numberRef = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementMinKey));
if (numberRef && CFNumberGetValue(numberRef, kCFNumberLongType, &number))
2012-01-29 09:37:29 -05:00
element->minReport = element->min = number;
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
numberRef = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementMaxKey));
if (numberRef && CFNumberGetValue(numberRef, kCFNumberLongType, &number))
2012-01-29 09:37:29 -05:00
element->maxReport = element->max = number;
2011-09-18 15:05:00 -04:00
}
}
else
{
2013-04-24 13:25:42 -04:00
CFTypeRef array = CFDictionaryGetValue(elementRef, CFSTR(kIOHIDElementKey));
if (array)
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
if (CFGetTypeID(array) == CFArrayGetTypeID())
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
CFRange range = { 0, CFArrayGetCount(array) };
CFArrayApplyFunction(array, range, getElementsCFArrayHandler, joystick);
2011-09-18 15:05:00 -04:00
}
}
}
}
2012-01-29 09:30:01 -05:00
// Adds an element to the specified joystick
2013-02-04 07:22:10 -05:00
//
2012-08-14 15:58:22 -04:00
static void getElementsCFArrayHandler(const void* value, void* parameter)
2011-09-18 15:05:00 -04:00
{
2012-01-29 09:30:01 -05:00
if (CFGetTypeID(value) == CFDictionaryGetTypeID())
addJoystickElement((_GLFWjoystickIOKit*) parameter, (CFTypeRef) value);
2011-09-18 15:05:00 -04:00
}
2012-01-29 09:30:01 -05:00
// Returns the value of the specified element of the specified joystick
2013-02-04 07:22:10 -05:00
//
static long getElementValue(_GLFWjoystickIOKit* joystick, _GLFWjoyelement* element)
2011-09-18 15:05:00 -04:00
{
2012-01-29 09:30:01 -05:00
IOReturn result = kIOReturnSuccess;
IOHIDEventStruct hidEvent;
hidEvent.value = 0;
2012-01-29 09:37:29 -05:00
if (joystick && element && joystick->interface)
2012-01-29 09:30:01 -05:00
{
2012-01-29 11:08:22 -05:00
result = (*(joystick->interface))->getElementValue(joystick->interface,
element->cookie,
&hidEvent);
2012-01-29 09:30:01 -05:00
if (kIOReturnSuccess == result)
{
2012-08-26 09:38:18 -04:00
// Record min and max for auto calibration
2012-01-29 09:37:29 -05:00
if (hidEvent.value < element->minReport)
2012-01-29 11:08:22 -05:00
element->minReport = hidEvent.value;
2012-01-29 09:37:29 -05:00
if (hidEvent.value > element->maxReport)
2012-01-29 11:08:22 -05:00
element->maxReport = hidEvent.value;
2012-01-29 09:30:01 -05:00
}
}
2012-08-26 09:38:18 -04:00
// Auto user scale
2012-01-29 09:30:01 -05:00
return (long) hidEvent.value;
2011-09-18 15:05:00 -04:00
}
2012-01-29 09:30:01 -05:00
// Removes the specified joystick
2013-02-04 07:22:10 -05:00
//
static void removeJoystick(_GLFWjoystickIOKit* joystick)
2011-09-18 15:05:00 -04:00
{
2012-01-29 18:02:54 -05:00
int i;
2013-04-24 13:25:42 -04:00
if (!joystick->present)
return;
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
for (i = 0; i < CFArrayGetCount(joystick->axisElements); i++)
free((void*) CFArrayGetValueAtIndex(joystick->axisElements, i));
CFArrayRemoveAllValues(joystick->axisElements);
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
for (i = 0; i < CFArrayGetCount(joystick->buttonElements); i++)
free((void*) CFArrayGetValueAtIndex(joystick->buttonElements, i));
CFArrayRemoveAllValues(joystick->buttonElements);
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
for (i = 0; i < CFArrayGetCount(joystick->hatElements); i++)
free((void*) CFArrayGetValueAtIndex(joystick->hatElements, i));
CFArrayRemoveAllValues(joystick->hatElements);
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
free(joystick->axes);
free(joystick->buttons);
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
(*(joystick->interface))->close(joystick->interface);
(*(joystick->interface))->Release(joystick->interface);
memset(joystick, 0, sizeof(_GLFWjoystickIOKit));
2011-09-18 15:05:00 -04:00
}
2012-01-29 09:30:01 -05:00
// Callback for user-initiated joystick removal
2013-02-04 07:22:10 -05:00
//
2012-01-29 09:30:01 -05:00
static void removalCallback(void* target, IOReturn result, void* refcon, void* sender)
2011-09-18 15:05:00 -04:00
{
removeJoystick((_GLFWjoystickIOKit*) refcon);
2011-09-18 15:05:00 -04:00
}
2012-08-26 15:29:39 -04:00
// Polls for joystick events and updates GLFW state
2013-02-04 07:22:10 -05:00
//
2012-01-29 09:30:01 -05:00
static void pollJoystickEvents(void)
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
int joy;
2012-01-29 18:02:54 -05:00
2013-04-24 13:25:42 -04:00
for (joy = 0; joy <= GLFW_JOYSTICK_LAST; joy++)
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
CFIndex i;
int buttonIndex = 0;
2014-04-08 10:42:14 -04:00
_GLFWjoystickIOKit* joystick = _glfw.iokit_js + joy;
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
if (!joystick->present)
continue;
for (i = 0; i < CFArrayGetCount(joystick->buttonElements); i++)
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
_GLFWjoyelement* button =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->buttonElements, i);
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
if (getElementValue(joystick, button))
joystick->buttons[buttonIndex++] = GLFW_PRESS;
else
joystick->buttons[buttonIndex++] = GLFW_RELEASE;
}
for (i = 0; i < CFArrayGetCount(joystick->axisElements); i++)
{
_GLFWjoyelement* axis =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->axisElements, i);
long value = getElementValue(joystick, axis);
long readScale = axis->maxReport - axis->minReport;
if (readScale == 0)
joystick->axes[i] = value;
else
joystick->axes[i] = (2.f * (value - axis->minReport) / readScale) - 1.f;
}
for (i = 0; i < CFArrayGetCount(joystick->hatElements); i++)
{
_GLFWjoyelement* hat =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->hatElements, i);
// Bit fields of button presses for each direction, including nil
const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
long j, value = getElementValue(joystick, hat);
if (value < 0 || value > 8)
value = 8;
2013-04-24 13:25:42 -04:00
for (j = 0; j < 4; j++)
{
2013-04-24 13:25:42 -04:00
if (directions[value] & (1 << j))
joystick->buttons[buttonIndex++] = GLFW_PRESS;
else
joystick->buttons[buttonIndex++] = GLFW_RELEASE;
}
2011-09-18 15:05:00 -04:00
}
}
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
// Initialize joystick interface
2013-02-04 07:22:10 -05:00
//
2011-09-18 15:05:00 -04:00
void _glfwInitJoysticks(void)
{
2013-04-24 13:25:42 -04:00
int joy = 0;
2012-01-29 09:30:01 -05:00
IOReturn result = kIOReturnSuccess;
mach_port_t masterPort = 0;
io_iterator_t objectIterator = 0;
CFMutableDictionaryRef hidMatchDictionary = NULL;
io_object_t ioHIDDeviceObject = 0;
2011-09-18 15:05:00 -04:00
result = IOMasterPort(bootstrap_port, &masterPort);
hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
if (kIOReturnSuccess != result || !hidMatchDictionary)
{
if (hidMatchDictionary)
CFRelease(hidMatchDictionary);
2012-08-14 15:58:22 -04:00
2011-09-18 15:05:00 -04:00
return;
}
2012-01-29 09:30:01 -05:00
2012-01-29 11:08:22 -05:00
result = IOServiceGetMatchingServices(masterPort,
hidMatchDictionary,
&objectIterator);
if (result != kIOReturnSuccess)
2011-09-18 15:05:00 -04:00
return;
2012-01-29 09:30:01 -05:00
2012-08-14 15:58:22 -04:00
if (!objectIterator)
{
// There are no joysticks
2011-09-18 15:05:00 -04:00
return;
2012-08-14 15:58:22 -04:00
}
2012-01-29 09:30:01 -05:00
2011-09-18 15:05:00 -04:00
while ((ioHIDDeviceObject = IOIteratorNext(objectIterator)))
{
CFMutableDictionaryRef propsRef = NULL;
CFTypeRef valueRef = NULL;
2012-01-29 09:30:01 -05:00
kern_return_t result;
IOCFPlugInInterface** ppPlugInInterface = NULL;
2011-09-18 15:05:00 -04:00
HRESULT plugInResult = S_OK;
SInt32 score = 0;
2012-01-29 09:30:01 -05:00
long usagePage = 0;
long usage = 0;
2012-01-29 09:30:01 -05:00
valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject,
CFSTR(kIOHIDPrimaryUsagePageKey),
kCFAllocatorDefault, kNilOptions);
2013-04-24 13:25:42 -04:00
if (valueRef)
{
2013-04-24 13:25:42 -04:00
CFNumberGetValue(valueRef, kCFNumberLongType, &usagePage);
CFRelease(valueRef);
}
2012-01-29 09:30:01 -05:00
valueRef = IORegistryEntryCreateCFProperty(ioHIDDeviceObject,
CFSTR(kIOHIDPrimaryUsageKey),
kCFAllocatorDefault, kNilOptions);
2013-04-24 13:25:42 -04:00
if (valueRef)
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
CFNumberGetValue(valueRef, kCFNumberLongType, &usage);
CFRelease(valueRef);
}
2012-08-14 15:58:22 -04:00
if (usagePage != kHIDPage_GenericDesktop)
{
// This device is not relevant to GLFW
continue;
}
if ((usage != kHIDUsage_GD_Joystick &&
usage != kHIDUsage_GD_GamePad &&
usage != kHIDUsage_GD_MultiAxisController))
{
// This device is not relevant to GLFW
continue;
2013-04-24 13:25:42 -04:00
}
result = IORegistryEntryCreateCFProperties(ioHIDDeviceObject,
&propsRef,
kCFAllocatorDefault,
kNilOptions);
if (result != kIOReturnSuccess)
continue;
2012-01-29 09:30:01 -05:00
2014-04-08 10:42:14 -04:00
_GLFWjoystickIOKit* joystick = _glfw.iokit_js + joy;
2012-01-29 09:37:29 -05:00
joystick->present = GL_TRUE;
2012-01-29 09:30:01 -05:00
result = IOCreatePlugInInterfaceForService(ioHIDDeviceObject,
kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&ppPlugInInterface,
&score);
2011-09-18 15:05:00 -04:00
if (kIOReturnSuccess != result)
{
CFRelease(propsRef);
return;
}
2012-01-29 09:30:01 -05:00
2011-09-18 15:05:00 -04:00
plugInResult = (*ppPlugInInterface)->QueryInterface(
2012-01-29 09:30:01 -05:00
ppPlugInInterface,
CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
2012-01-29 09:37:29 -05:00
(void *) &(joystick->interface));
2012-01-29 09:30:01 -05:00
if (plugInResult != S_OK)
{
CFRelease(propsRef);
return;
}
2012-01-29 09:30:01 -05:00
2011-09-18 15:05:00 -04:00
(*ppPlugInInterface)->Release(ppPlugInInterface);
2012-01-29 09:30:01 -05:00
2012-01-29 09:37:29 -05:00
(*(joystick->interface))->open(joystick->interface, 0);
(*(joystick->interface))->setRemovalCallback(joystick->interface,
2012-01-29 09:30:01 -05:00
removalCallback,
joystick,
joystick);
2012-08-26 09:38:18 -04:00
// Get product string
valueRef = CFDictionaryGetValue(propsRef, CFSTR(kIOHIDProductKey));
2013-04-24 13:25:42 -04:00
if (valueRef)
2012-01-29 09:30:01 -05:00
{
2013-04-24 13:25:42 -04:00
CFStringGetCString(valueRef,
joystick->name,
sizeof(joystick->name),
kCFStringEncodingUTF8);
2012-01-29 09:30:01 -05:00
}
2013-04-24 13:25:42 -04:00
joystick->axisElements = CFArrayCreateMutable(NULL, 0, NULL);
joystick->buttonElements = CFArrayCreateMutable(NULL, 0, NULL);
joystick->hatElements = CFArrayCreateMutable(NULL, 0, NULL);
valueRef = CFDictionaryGetValue(propsRef, CFSTR(kIOHIDElementKey));
2013-04-24 13:25:42 -04:00
if (CFGetTypeID(valueRef) == CFArrayGetTypeID())
2011-09-18 15:05:00 -04:00
{
2013-04-24 13:25:42 -04:00
CFRange range = { 0, CFArrayGetCount(valueRef) };
CFArrayApplyFunction(valueRef,
2012-01-29 09:30:01 -05:00
range,
2012-08-14 15:58:22 -04:00
getElementsCFArrayHandler,
2012-01-29 09:30:01 -05:00
(void*) joystick);
2011-09-18 15:05:00 -04:00
}
CFRelease(propsRef);
2012-01-29 09:30:01 -05:00
joystick->axes = calloc(CFArrayGetCount(joystick->axisElements),
2013-04-24 13:25:42 -04:00
sizeof(float));
joystick->buttons = calloc(CFArrayGetCount(joystick->buttonElements) +
CFArrayGetCount(joystick->hatElements) * 4, 1);
2013-04-24 13:25:42 -04:00
joy++;
if (joy > GLFW_JOYSTICK_LAST)
break;
2011-09-18 15:05:00 -04:00
}
}
// Close all opened joystick handles
2013-02-04 07:22:10 -05:00
//
2011-09-18 15:05:00 -04:00
void _glfwTerminateJoysticks(void)
{
2012-01-29 18:02:54 -05:00
int i;
for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
2011-09-18 15:05:00 -04:00
{
2014-04-08 10:42:14 -04:00
_GLFWjoystickIOKit* joystick = &_glfw.iokit_js[i];
2012-01-29 09:30:01 -05:00
removeJoystick(joystick);
if (joystick->axisElements)
CFRelease(joystick->axisElements);
if (joystick->buttonElements)
CFRelease(joystick->buttonElements);
if (joystick->hatElements)
CFRelease(joystick->hatElements);
2011-09-18 15:05:00 -04:00
}
}
2010-09-15 21:25:36 -04:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 11:34:51 -04:00
2013-04-24 13:25:42 -04:00
int _glfwPlatformJoystickPresent(int joy)
2010-09-07 11:34:51 -04:00
{
2013-04-24 13:25:42 -04:00
pollJoystickEvents();
2012-01-29 09:30:01 -05:00
2014-04-08 10:42:14 -04:00
return _glfw.iokit_js[joy].present;
2010-09-07 11:34:51 -04:00
}
2013-06-04 12:20:38 -04:00
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
2010-09-07 11:34:51 -04:00
{
2014-04-08 10:42:14 -04:00
_GLFWjoystickIOKit* joystick = _glfw.iokit_js + joy;
2012-01-29 09:30:01 -05:00
pollJoystickEvents();
2013-04-24 13:25:42 -04:00
if (!joystick->present)
return NULL;
2012-01-29 09:30:01 -05:00
2013-04-24 13:25:42 -04:00
*count = (int) CFArrayGetCount(joystick->axisElements);
return joystick->axes;
2010-09-07 11:34:51 -04:00
}
2013-06-04 12:20:38 -04:00
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
2010-09-07 11:34:51 -04:00
{
2014-04-08 10:42:14 -04:00
_GLFWjoystickIOKit* joystick = _glfw.iokit_js + joy;
2012-01-29 09:30:01 -05:00
pollJoystickEvents();
2013-04-24 13:25:42 -04:00
if (!joystick->present)
return NULL;
2012-08-14 15:58:22 -04:00
2013-04-24 13:25:42 -04:00
*count = (int) CFArrayGetCount(joystick->buttonElements) +
(int) CFArrayGetCount(joystick->hatElements) * 4;
return joystick->buttons;
}
2012-08-14 15:58:22 -04:00
const char* _glfwPlatformGetJoystickName(int joy)
{
2013-04-24 13:25:42 -04:00
pollJoystickEvents();
2014-04-08 10:42:14 -04:00
return _glfw.iokit_js[joy].name;
}