glfw/src/cocoa_joystick.m

638 lines
20 KiB
Mathematica
Raw Normal View History

2010-09-07 17:34:51 +02:00
//========================================================================
// GLFW - An OpenGL library
2012-08-28 15:03:57 +02:00
// Platform: Cocoa
2010-09-07 17:41:26 +02:00
// API Version: 3.0
2010-09-07 17:34:51 +02:00
// WWW: http://www.glfw.org/
//------------------------------------------------------------------------
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
// Copyright (c) 2012 Torsten Walluhn <tw@mad-cad.net>
2010-09-07 17:34:51 +02: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 21:05:00 +02:00
#include <unistd.h>
#include <ctype.h>
2012-01-29 15:38:22 +01:00
2011-09-18 21:05:00 +02:00
#include <mach/mach.h>
#include <mach/mach_error.h>
2012-01-29 15:38:22 +01:00
2011-09-18 21:05:00 +02:00
#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 15:30:01 +01:00
typedef struct
{
2012-01-29 15:37:29 +01:00
IOHIDElementCookie cookie;
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
long value;
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
long min;
long max;
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
long minReport;
long maxReport;
2012-01-29 15:30:01 +01:00
} _glfwJoystickElement;
2011-09-18 21:05:00 +02:00
//------------------------------------------------------------------------
// Joystick information & state
//------------------------------------------------------------------------
2012-01-29 15:30:01 +01:00
typedef struct
{
2012-01-29 15:37:29 +01:00
int present;
char name[256];
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
IOHIDDeviceInterface** interface;
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
int numAxes;
int numButtons;
int numHats;
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
CFMutableArrayRef axes;
CFMutableArrayRef buttons;
CFMutableArrayRef hats;
2012-01-29 15:30:01 +01:00
} _glfwJoystick;
2012-01-29 15:51:09 +01:00
static _glfwJoystick _glfwJoysticks[GLFW_JOYSTICK_LAST + 1];
2011-09-18 21:05:00 +02:00
static void getElementsCFArrayHandler(const void* value, void* parameter);
2011-09-18 21:05:00 +02:00
2012-01-29 15:30:01 +01:00
//========================================================================
// Adds an element to the specified joystick
//========================================================================
static void addJoystickElement(_glfwJoystick* joystick, CFTypeRef refElement)
2011-09-18 21:05:00 +02:00
{
long elementType, usagePage, usage;
CFTypeRef refElementType, refUsagePage, refUsage;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
refElementType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementTypeKey));
2012-01-29 15:30:01 +01:00
refUsagePage = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsagePageKey));
refUsage = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsageKey));
2011-09-18 21:05:00 +02:00
CFMutableArrayRef elementsArray = NULL;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
CFNumberGetValue(refElementType, kCFNumberLongType, &elementType);
CFNumberGetValue(refUsagePage, kCFNumberLongType, &usagePage);
CFNumberGetValue(refUsage, kCFNumberLongType, &usage);
2012-01-29 15:30:01 +01:00
if ((elementType == kIOHIDElementTypeInput_Axis) ||
2011-09-18 21:05:00 +02:00
(elementType == kIOHIDElementTypeInput_Button) ||
(elementType == kIOHIDElementTypeInput_Misc))
{
2012-08-14 21:58:22 +02:00
switch (usagePage)
2011-09-18 21:05:00 +02:00
{
case kHIDPage_GenericDesktop:
2012-01-29 15:30:01 +01:00
{
2011-09-18 21:05:00 +02: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:
2012-01-29 15:37:29 +01:00
joystick->numAxes++;
elementsArray = joystick->axes;
2011-09-18 21:05:00 +02:00
break;
case kHIDUsage_GD_Hatswitch:
2012-01-29 15:37:29 +01:00
joystick->numHats++;
elementsArray = joystick->hats;
2011-09-18 21:05:00 +02:00
break;
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
break;
2012-01-29 15:30:01 +01:00
}
2011-09-18 21:05:00 +02:00
case kHIDPage_Button:
2012-01-29 15:37:29 +01:00
joystick->numButtons++;
elementsArray = joystick->buttons;
2011-09-18 21:05:00 +02:00
break;
default:
break;
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
if (elementsArray)
{
long number;
CFTypeRef refType;
2012-01-29 15:30:01 +01:00
2012-02-07 14:58:58 +01:00
_glfwJoystickElement* element = (_glfwJoystickElement*) malloc(sizeof(_glfwJoystickElement));
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
CFArrayAppendValue(elementsArray, element);
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementCookieKey));
if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
2012-01-29 15:37:29 +01:00
element->cookie = (IOHIDElementCookie) number;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMinKey));
if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
2012-01-29 15:37:29 +01:00
element->minReport = element->min = number;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMaxKey));
if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
2012-01-29 15:37:29 +01:00
element->maxReport = element->max = number;
2011-09-18 21:05:00 +02:00
}
}
else
{
CFTypeRef refElementTop = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementKey));
if (refElementTop)
{
CFTypeID type = CFGetTypeID (refElementTop);
2012-08-14 21:58:22 +02:00
if (type == CFArrayGetTypeID())
2011-09-18 21:05:00 +02:00
{
CFRange range = {0, CFArrayGetCount (refElementTop)};
2012-08-14 21:58:22 +02:00
CFArrayApplyFunction(refElementTop, range, getElementsCFArrayHandler, joystick);
2011-09-18 21:05:00 +02:00
}
}
}
}
2012-01-29 15:30:01 +01:00
//========================================================================
// Adds an element to the specified joystick
//========================================================================
2012-08-14 21:58:22 +02:00
static void getElementsCFArrayHandler(const void* value, void* parameter)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
if (CFGetTypeID(value) == CFDictionaryGetTypeID())
addJoystickElement((_glfwJoystick*) parameter, (CFTypeRef) value);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
//========================================================================
// Returns the value of the specified element of the specified joystick
//========================================================================
static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* element)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
IOReturn result = kIOReturnSuccess;
IOHIDEventStruct hidEvent;
hidEvent.value = 0;
2012-01-29 15:37:29 +01:00
if (joystick && element && joystick->interface)
2012-01-29 15:30:01 +01:00
{
2012-01-29 17:08:22 +01:00
result = (*(joystick->interface))->getElementValue(joystick->interface,
element->cookie,
&hidEvent);
2012-01-29 15:30:01 +01:00
if (kIOReturnSuccess == result)
{
2012-08-26 15:38:18 +02:00
// Record min and max for auto calibration
2012-01-29 15:37:29 +01:00
if (hidEvent.value < element->minReport)
2012-01-29 17:08:22 +01:00
element->minReport = hidEvent.value;
2012-01-29 15:37:29 +01:00
if (hidEvent.value > element->maxReport)
2012-01-29 17:08:22 +01:00
element->maxReport = hidEvent.value;
2012-01-29 15:30:01 +01:00
}
}
2012-08-26 15:38:18 +02:00
// Auto user scale
2012-01-29 15:30:01 +01:00
return (long) hidEvent.value;
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
//========================================================================
// Removes the specified joystick
//========================================================================
static void removeJoystick(_glfwJoystick* joystick)
2011-09-18 21:05:00 +02:00
{
2012-01-30 00:02:54 +01:00
int i;
2012-01-29 15:37:29 +01:00
if (joystick->present)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:37:29 +01:00
joystick->present = GL_FALSE;
2012-01-29 15:30:01 +01:00
2012-01-30 00:02:54 +01:00
for (i = 0; i < joystick->numAxes; i++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystickElement* axes =
2012-01-29 15:37:29 +01:00
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i);
2012-02-07 14:58:58 +01:00
free(axes);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:37:29 +01:00
CFArrayRemoveAllValues(joystick->axes);
joystick->numAxes = 0;
2012-01-29 15:30:01 +01:00
2012-01-30 00:02:54 +01:00
for (i = 0; i < joystick->numButtons; i++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystickElement* button =
2012-01-29 15:37:29 +01:00
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i);
2012-02-07 14:58:58 +01:00
free(button);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:37:29 +01:00
CFArrayRemoveAllValues(joystick->buttons);
joystick->numButtons = 0;
2012-01-29 15:30:01 +01:00
2012-01-30 00:02:54 +01:00
for (i = 0; i < joystick->numHats; i++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystickElement* hat =
2012-01-29 15:37:29 +01:00
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, i);
2012-02-07 14:58:58 +01:00
free(hat);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:37:29 +01:00
CFArrayRemoveAllValues(joystick->hats);
joystick->hats = 0;
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
(*(joystick->interface))->close(joystick->interface);
(*(joystick->interface))->Release(joystick->interface);
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
joystick->interface = NULL;
2011-09-18 21:05:00 +02:00
}
}
2012-01-29 15:30:01 +01:00
//========================================================================
// Callback for user-initiated joystick removal
//========================================================================
static void removalCallback(void* target, IOReturn result, void* refcon, void* sender)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
removeJoystick((_glfwJoystick*) refcon);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
//========================================================================
2012-08-26 21:29:39 +02:00
// Polls for joystick events and updates GLFW state
2012-01-29 15:30:01 +01:00
//========================================================================
static void pollJoystickEvents(void)
2011-09-18 21:05:00 +02:00
{
2012-01-30 00:02:54 +01:00
int i;
CFIndex j;
for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystick* joystick = &_glfwJoysticks[i];
2012-01-29 15:37:29 +01:00
if (joystick->present)
2011-09-18 21:05:00 +02:00
{
2012-01-30 00:02:54 +01:00
for (j = 0; j < joystick->numButtons; j++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystickElement* button =
2012-01-30 00:02:54 +01:00
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, j);
2012-01-29 15:37:29 +01:00
button->value = getElementValue(joystick, button);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
2012-01-30 00:02:54 +01:00
for (j = 0; j < joystick->numAxes; j++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystickElement* axes =
2012-01-30 00:02:54 +01:00
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, j);
2012-01-29 15:37:29 +01:00
axes->value = getElementValue(joystick, axes);
2011-09-18 21:05:00 +02:00
}
for (j = 0; j < joystick->numHats; j++)
{
_glfwJoystickElement* hat =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, j);
hat->value = getElementValue(joystick, hat);
}
2011-09-18 21:05:00 +02:00
}
}
}
//////////////////////////////////////////////////////////////////////////
////// GLFW internal API //////
//////////////////////////////////////////////////////////////////////////
//========================================================================
// Initialize joystick interface
//========================================================================
void _glfwInitJoysticks(void)
{
2012-01-29 15:30:01 +01: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;
memset(&_glfwJoysticks, 0, sizeof(_glfwJoysticks));
2011-09-18 21:05:00 +02:00
result = IOMasterPort(bootstrap_port, &masterPort);
hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
if (kIOReturnSuccess != result || !hidMatchDictionary)
{
if (hidMatchDictionary)
CFRelease(hidMatchDictionary);
2012-08-14 21:58:22 +02:00
2011-09-18 21:05:00 +02:00
return;
}
2012-01-29 15:30:01 +01:00
2012-01-29 17:08:22 +01:00
result = IOServiceGetMatchingServices(masterPort,
hidMatchDictionary,
&objectIterator);
if (result != kIOReturnSuccess)
2011-09-18 21:05:00 +02:00
return;
2012-01-29 15:30:01 +01:00
2012-08-14 21:58:22 +02:00
if (!objectIterator)
{
// There are no joysticks
2011-09-18 21:05:00 +02:00
return;
2012-08-14 21:58:22 +02:00
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
while ((ioHIDDeviceObject = IOIteratorNext(objectIterator)))
{
2012-01-29 15:30:01 +01:00
CFMutableDictionaryRef hidProperties = 0;
kern_return_t result;
CFTypeRef refCF = 0;
IOCFPlugInInterface** ppPlugInInterface = NULL;
2011-09-18 21:05:00 +02:00
HRESULT plugInResult = S_OK;
SInt32 score = 0;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
long usagePage, usage;
2012-01-29 15:30:01 +01:00
result = IORegistryEntryCreateCFProperties(ioHIDDeviceObject,
2011-09-18 21:05:00 +02:00
&hidProperties,
kCFAllocatorDefault,
kNilOptions);
2012-01-29 15:30:01 +01:00
if (result != kIOReturnSuccess)
continue;
2012-08-26 15:38:18 +02:00
// Check device type
2011-09-18 21:05:00 +02:00
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
if (refCF)
{
2011-09-18 21:05:00 +02:00
CFNumberGetValue(refCF, kCFNumberLongType, &usagePage);
if (usagePage != kHIDPage_GenericDesktop)
{
2012-08-26 15:38:18 +02:00
// This device is not relevant to GLFW
continue;
}
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
if (refCF)
{
CFNumberGetValue(refCF, kCFNumberLongType, &usage);
2012-08-14 21:58:22 +02:00
if ((usage != kHIDUsage_GD_Joystick &&
usage != kHIDUsage_GD_GamePad &&
usage != kHIDUsage_GD_MultiAxisController))
{
2012-08-26 15:38:18 +02:00
// This device is not relevant to GLFW
continue;
}
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
_glfwJoystick* joystick = &_glfwJoysticks[deviceCounter];
2012-01-29 15:37:29 +01:00
joystick->present = GL_TRUE;
2012-01-29 15:30:01 +01:00
result = IOCreatePlugInInterfaceForService(ioHIDDeviceObject,
kIOHIDDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&ppPlugInInterface,
&score);
2011-09-18 21:05:00 +02:00
if (kIOReturnSuccess != result)
return;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
plugInResult = (*ppPlugInInterface)->QueryInterface(
2012-01-29 15:30:01 +01:00
ppPlugInInterface,
CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
2012-01-29 15:37:29 +01:00
(void *) &(joystick->interface));
2012-01-29 15:30:01 +01:00
if (plugInResult != S_OK)
return;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
(*ppPlugInInterface)->Release(ppPlugInInterface);
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
(*(joystick->interface))->open(joystick->interface, 0);
(*(joystick->interface))->setRemovalCallback(joystick->interface,
2012-01-29 15:30:01 +01:00
removalCallback,
joystick,
joystick);
2012-08-26 15:38:18 +02:00
// Get product string
2011-09-18 21:05:00 +02:00
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
if (refCF)
2012-01-29 15:30:01 +01:00
{
CFStringGetCString(refCF,
joystick->name,
sizeof(joystick->name),
kCFStringEncodingUTF8);
2012-01-29 15:30:01 +01:00
}
2012-01-29 15:37:29 +01:00
joystick->numAxes = 0;
joystick->numButtons = 0;
joystick->numHats = 0;
joystick->axes = CFArrayCreateMutable(NULL, 0, NULL);
joystick->buttons = CFArrayCreateMutable(NULL, 0, NULL);
joystick->hats = CFArrayCreateMutable(NULL, 0, NULL);
2012-01-29 15:30:01 +01:00
CFTypeRef refTopElement = CFDictionaryGetValue(hidProperties,
CFSTR(kIOHIDElementKey));
2011-09-18 21:05:00 +02:00
CFTypeID type = CFGetTypeID(refTopElement);
if (type == CFArrayGetTypeID())
{
2012-01-29 15:30:01 +01:00
CFRange range = { 0, CFArrayGetCount(refTopElement) };
CFArrayApplyFunction(refTopElement,
range,
2012-08-14 21:58:22 +02:00
getElementsCFArrayHandler,
2012-01-29 15:30:01 +01:00
(void*) joystick);
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
deviceCounter++;
}
}
//========================================================================
// Close all opened joystick handles
//========================================================================
void _glfwTerminateJoysticks(void)
{
2012-01-30 00:02:54 +01:00
int i;
for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
2011-09-18 21:05:00 +02:00
{
2012-01-29 15:30:01 +01:00
_glfwJoystick* joystick = &_glfwJoysticks[i];
removeJoystick(joystick);
2011-09-18 21:05:00 +02:00
}
}
2010-09-16 03:25:36 +02:00
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
2010-09-07 17:34:51 +02:00
//========================================================================
// Determine joystick capabilities
//========================================================================
2011-09-18 21:05:00 +02:00
int _glfwPlatformGetJoystickParam(int joy, int param)
2010-09-07 17:34:51 +02:00
{
2012-01-29 15:37:29 +01:00
if (!_glfwJoysticks[joy].present)
2011-09-18 21:05:00 +02:00
{
// TODO: Figure out if this is an error
2012-01-29 15:41:06 +01:00
return GL_FALSE;
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
switch (param)
{
case GLFW_PRESENT:
return GL_TRUE;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
case GLFW_AXES:
2012-01-29 15:37:29 +01:00
return (int) CFArrayGetCount(_glfwJoysticks[joy].axes);
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
case GLFW_BUTTONS:
2012-08-14 21:58:22 +02:00
return (int) CFArrayGetCount(_glfwJoysticks[joy].buttons) +
(int) CFArrayGetCount(_glfwJoysticks[joy].hats) * 4;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
default:
break;
}
2012-01-29 15:30:01 +01:00
2012-01-29 15:41:06 +01:00
return GL_FALSE;
2010-09-07 17:34:51 +02:00
}
2011-09-18 21:05:00 +02:00
2010-09-07 17:34:51 +02:00
//========================================================================
// Get joystick axis positions
//========================================================================
int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes)
2010-09-07 17:34:51 +02:00
{
2012-01-30 00:02:54 +01:00
int i;
2011-09-18 21:05:00 +02:00
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0;
2012-01-29 15:30:01 +01:00
_glfwJoystick joystick = _glfwJoysticks[joy];
2012-01-29 15:37:29 +01:00
if (!joystick.present)
2011-09-18 21:05:00 +02:00
{
// TODO: Figure out if this is an error
return 0;
}
2012-01-29 15:30:01 +01:00
2012-01-29 15:37:29 +01:00
numaxes = numaxes < joystick.numAxes ? numaxes : joystick.numAxes;
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
// Update joystick state
2012-01-29 15:30:01 +01:00
pollJoystickEvents();
2012-01-30 00:02:54 +01:00
for (i = 0; i < numaxes; i++)
2011-09-18 21:05:00 +02:00
{
_glfwJoystickElement* elements =
2012-01-29 15:37:29 +01:00
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.axes, i);
2012-01-29 15:30:01 +01:00
long readScale = elements->maxReport - elements->minReport;
2012-01-29 15:30:01 +01:00
if (readScale == 0)
axes[i] = elements->value;
2012-01-29 15:30:01 +01:00
else
axes[i] = (2.0f * (elements->value - elements->minReport) / readScale) - 1.0f;
2012-01-29 15:30:01 +01:00
if (i & 1)
axes[i] = -axes[i];
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
return numaxes;
2010-09-07 17:34:51 +02:00
}
2011-09-18 21:05:00 +02:00
2010-09-07 17:34:51 +02:00
//========================================================================
// Get joystick button states
//========================================================================
2012-01-29 15:30:01 +01:00
int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
2011-09-18 21:05:00 +02:00
int numbuttons)
2010-09-07 17:34:51 +02:00
{
2012-04-09 23:56:14 +02:00
int i, j, button;
2012-01-30 00:02:54 +01:00
2011-09-18 21:05:00 +02:00
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0;
2012-01-29 15:30:01 +01:00
_glfwJoystick joystick = _glfwJoysticks[joy];
2012-01-29 15:37:29 +01:00
if (!joystick.present)
2011-09-18 21:05:00 +02:00
{
// TODO: Figure out if this is an error
return 0;
}
2012-01-29 15:30:01 +01:00
2011-09-18 21:05:00 +02:00
// Update joystick state
2012-01-29 15:30:01 +01:00
pollJoystickEvents();
for (button = 0; button < numbuttons && button < joystick.numButtons; button++)
2011-09-18 21:05:00 +02:00
{
_glfwJoystickElement* element = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.buttons, button);
buttons[button] = element->value ? GLFW_PRESS : GLFW_RELEASE;
2011-09-18 21:05:00 +02:00
}
2012-01-29 15:30:01 +01:00
// Virtual buttons - Inject data from hats
// Each hat is exposed as 4 buttons which exposes 8 directions with concurrent button presses
2012-08-14 21:58:22 +02:00
// Bit fields of button presses for each direction, including nil
const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
2010-09-07 17:34:51 +02:00
2012-04-09 23:56:14 +02:00
for (i = 0; i < joystick.numHats; i++)
{
_glfwJoystickElement* hat = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.hats, i);
int value = hat->value;
2012-08-14 21:58:22 +02:00
if (value < 0 || value > 8)
value = 8;
for (j = 0; j < 4 && button < numbuttons; j++)
{
2012-08-14 21:58:22 +02:00
if (directions[value] & (1 << j))
buttons[button] = GLFW_PRESS;
2012-08-14 21:58:22 +02:00
else
buttons[button] = GLFW_RELEASE;
2012-08-14 21:58:22 +02:00
button++;
}
}
return button;
}
2012-08-14 21:58:22 +02:00
//========================================================================
// Get joystick name
//========================================================================
const char* _glfwPlatformGetJoystickName(int joy)
{
return _glfwJoysticks[joy].name;
}