2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2011-03-06 00:46:39 +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"
|
|
|
|
|
2011-09-18 19:05:00 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
2012-01-29 14:38:22 +00:00
|
|
|
|
2011-09-18 19:05:00 +00:00
|
|
|
#include <mach/mach.h>
|
|
|
|
#include <mach/mach_error.h>
|
2012-01-29 14:38:22 +00:00
|
|
|
|
2011-09-18 19:05:00 +00: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 14:30:01 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2012-01-29 14:37:29 +00:00
|
|
|
IOHIDElementCookie cookie;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
long value;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
long min;
|
|
|
|
long max;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +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
|
|
|
|
{
|
2012-01-29 14:37:29 +00:00
|
|
|
int present;
|
|
|
|
char product[256];
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
IOHIDDeviceInterface** interface;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
int numAxes;
|
|
|
|
int numButtons;
|
|
|
|
int numHats;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
CFMutableArrayRef axes;
|
|
|
|
CFMutableArrayRef buttons;
|
|
|
|
CFMutableArrayRef hats;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
|
|
|
} _glfwJoystick;
|
|
|
|
|
2012-01-29 14:51:09 +00:00
|
|
|
static _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:
|
2012-01-29 14:37:29 +00:00
|
|
|
joystick->numAxes++;
|
|
|
|
elementsArray = joystick->axes;
|
2011-09-18 19:05:00 +00:00
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Hatswitch:
|
2012-01-29 14:37:29 +00:00
|
|
|
joystick->numHats++;
|
|
|
|
elementsArray = joystick->hats;
|
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
|
|
|
break;
|
2012-01-29 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2011-09-18 19:05:00 +00:00
|
|
|
case kHIDPage_Button:
|
2012-01-29 14:37:29 +00:00
|
|
|
joystick->numButtons++;
|
|
|
|
elementsArray = joystick->buttons;
|
2011-09-18 19:05:00 +00:00
|
|
|
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
|
|
|
|
2012-02-07 13:58:58 +00:00
|
|
|
_glfwJoystickElement* element = (_glfwJoystickElement*) malloc(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))
|
2012-01-29 14:37:29 +00:00
|
|
|
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))
|
2012-01-29 14:37:29 +00:00
|
|
|
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))
|
2012-01-29 14:37:29 +00:00
|
|
|
element->maxReport = element->max = number;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
if (joystick && element && joystick->interface)
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2012-01-29 16:08:22 +00:00
|
|
|
result = (*(joystick->interface))->getElementValue(joystick->interface,
|
|
|
|
element->cookie,
|
|
|
|
&hidEvent);
|
2012-01-29 14:30:01 +00:00
|
|
|
if (kIOReturnSuccess == result)
|
|
|
|
{
|
2011-09-18 19:05:00 +00:00
|
|
|
/* record min and max for auto calibration */
|
2012-01-29 14:37:29 +00:00
|
|
|
if (hidEvent.value < element->minReport)
|
2012-01-29 16:08:22 +00:00
|
|
|
element->minReport = hidEvent.value;
|
2012-01-29 14:37:29 +00:00
|
|
|
if (hidEvent.value > element->maxReport)
|
2012-01-29 16:08:22 +00:00
|
|
|
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
|
|
|
{
|
2012-01-29 23:02:54 +00:00
|
|
|
int i;
|
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
if (joystick->present)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:37:29 +00:00
|
|
|
joystick->present = GL_FALSE;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 23:02:54 +00:00
|
|
|
for (i = 0; i < joystick->numAxes; i++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
_glfwJoystickElement* axes =
|
2012-01-29 14:37:29 +00:00
|
|
|
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i);
|
2012-02-07 13:58:58 +00:00
|
|
|
free(axes);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2012-01-29 14:37:29 +00:00
|
|
|
CFArrayRemoveAllValues(joystick->axes);
|
|
|
|
joystick->numAxes = 0;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 23:02:54 +00:00
|
|
|
for (i = 0; i < joystick->numButtons; i++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
_glfwJoystickElement* button =
|
2012-01-29 14:37:29 +00:00
|
|
|
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i);
|
2012-02-07 13:58:58 +00:00
|
|
|
free(button);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2012-01-29 14:37:29 +00:00
|
|
|
CFArrayRemoveAllValues(joystick->buttons);
|
|
|
|
joystick->numButtons = 0;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 23:02:54 +00:00
|
|
|
for (i = 0; i < joystick->numHats; i++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
_glfwJoystickElement* hat =
|
2012-01-29 14:37:29 +00:00
|
|
|
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, i);
|
2012-02-07 13:58:58 +00:00
|
|
|
free(hat);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2012-01-29 14:37:29 +00:00
|
|
|
CFArrayRemoveAllValues(joystick->hats);
|
|
|
|
joystick->hats = 0;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
(*(joystick->interface))->close(joystick->interface);
|
|
|
|
(*(joystick->interface))->Release(joystick->interface);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
joystick->interface = NULL;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 23:02:54 +00:00
|
|
|
int i;
|
|
|
|
CFIndex j;
|
|
|
|
|
|
|
|
for (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];
|
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
if (joystick->present)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 23:02:54 +00:00
|
|
|
for (j = 0; j < joystick->numButtons; j++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
_glfwJoystickElement* button =
|
2012-01-29 23:02:54 +00:00
|
|
|
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, j);
|
2012-01-29 14:37:29 +00:00
|
|
|
button->value = getElementValue(joystick, button);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 23:02:54 +00:00
|
|
|
for (j = 0; j < joystick->numAxes; j++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
_glfwJoystickElement* axes =
|
2012-01-29 23:02:54 +00:00
|
|
|
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, j);
|
2012-01-29 14:37:29 +00:00
|
|
|
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
|
|
|
|
2012-01-29 16:08:22 +00:00
|
|
|
result = IOServiceGetMatchingServices(masterPort,
|
|
|
|
hidMatchDictionary,
|
|
|
|
&objectIterator);
|
|
|
|
if (result != kIOReturnSuccess)
|
2011-09-18 19:05:00 +00:00
|
|
|
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)
|
2012-01-29 14:37:29 +00:00
|
|
|
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];
|
|
|
|
|
2012-01-29 14:37:29 +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)
|
2012-01-29 14:32:16 +00:00
|
|
|
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),
|
2012-01-29 14:37:29 +00:00
|
|
|
(void *) &(joystick->interface));
|
2012-01-29 14:30:01 +00:00
|
|
|
|
|
|
|
if (plugInResult != S_OK)
|
2012-01-29 14:32:16 +00:00
|
|
|
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
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
(*(joystick->interface))->open(joystick->interface, 0);
|
|
|
|
(*(joystick->interface))->setRemovalCallback(joystick->interface,
|
2012-01-29 14:30:01 +00:00
|
|
|
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,
|
2012-01-29 14:37:29 +00:00
|
|
|
(char*) &(joystick->product),
|
2012-01-29 14:30:01 +00:00
|
|
|
256,
|
|
|
|
CFStringGetSystemEncoding());
|
|
|
|
}
|
|
|
|
|
2012-01-29 14:37:29 +00: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 14:30:01 +00:00
|
|
|
|
|
|
|
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 23:02:54 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (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
|
|
|
{
|
2012-01-29 14:37:29 +00:00
|
|
|
if (!_glfwJoysticks[joy].present)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
|
|
|
// TODO: Figure out if this is an error
|
2012-01-29 14:41:06 +00:00
|
|
|
return GL_FALSE;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
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:
|
2012-01-29 14:37:29 +00:00
|
|
|
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:
|
2012-01-29 14:37:29 +00:00
|
|
|
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
|
|
|
|
2012-01-29 14:41:06 +00:00
|
|
|
return GL_FALSE;
|
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 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
|
|
|
{
|
2012-01-29 23:02:54 +00:00
|
|
|
int i;
|
|
|
|
|
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];
|
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
if (!joystick.present)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
|
|
|
// TODO: Figure out if this is an error
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +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();
|
|
|
|
|
2012-01-29 23:02:54 +00:00
|
|
|
for (i = 0; i < numaxes; i++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
_glfwJoystickElement* axes =
|
2012-01-29 14:37:29 +00:00
|
|
|
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.axes, i);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
long readScale = axes->maxReport - axes->minReport;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
|
|
|
if (readScale == 0)
|
2012-01-29 14:37:29 +00:00
|
|
|
pos[i] = axes->value;
|
2012-01-29 14:30:01 +00:00
|
|
|
else
|
2012-01-29 14:37:29 +00:00
|
|
|
pos[i] = (2.0f * (axes->value - axes->minReport) / readScale) - 1.0f;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:39:35 +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
|
|
|
{
|
2012-01-29 23:02:54 +00:00
|
|
|
int i;
|
|
|
|
|
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];
|
|
|
|
|
2012-01-29 14:37:29 +00:00
|
|
|
if (!joystick.present)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
|
|
|
// TODO: Figure out if this is an error
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2012-01-29 14:37:29 +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();
|
|
|
|
|
2012-01-29 23:02:54 +00:00
|
|
|
for (i = 0; i < numbuttons; i++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:37:29 +00:00
|
|
|
_glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.buttons, i);
|
|
|
|
buttons[i] = button->value ? GLFW_PRESS : GLFW_RELEASE;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|