2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2016-08-18 21:42:15 +00:00
|
|
|
// GLFW 3.3 Cocoa - www.glfw.org
|
2010-09-07 15:34:51 +00:00
|
|
|
//------------------------------------------------------------------------
|
2016-06-01 14:17:06 +00:00
|
|
|
// Copyright (c) 2009-2016 Camilla Berglund <elmindreda@glfw.org>
|
2012-08-14 21:34:26 +00:00
|
|
|
// Copyright (c) 2012 Torsten Walluhn <tw@mad-cad.net>
|
2010-09-07 15:34:51 +00: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 19:05:00 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
2016-02-08 19:21:48 +00:00
|
|
|
#include <string.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 <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
|
|
|
|
|
|
|
|
|
|
|
|
// Joystick element information
|
2016-02-08 08:32:48 +00:00
|
|
|
//
|
2015-12-03 17:16:46 +00:00
|
|
|
typedef struct _GLFWjoyelementNS
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
IOHIDElementRef elementRef;
|
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
|
|
|
|
2015-12-03 17:16:46 +00:00
|
|
|
} _GLFWjoyelementNS;
|
2011-09-18 19:05:00 +00:00
|
|
|
|
|
|
|
|
2012-08-14 21:34:26 +00:00
|
|
|
static 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
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2016-06-07 12:11:54 +00:00
|
|
|
static void addJoystickElement(_GLFWjoystickNS* js,
|
2015-06-26 11:31:37 +00:00
|
|
|
IOHIDElementRef elementRef)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
IOHIDElementType elementType;
|
|
|
|
long usagePage, usage;
|
2011-09-18 19:05:00 +00:00
|
|
|
CFMutableArrayRef elementsArray = NULL;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
elementType = IOHIDElementGetType(elementRef);
|
|
|
|
usagePage = IOHIDElementGetUsagePage(elementRef);
|
|
|
|
usage = IOHIDElementGetUsage(elementRef);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-23 20:14:07 +00:00
|
|
|
if ((elementType != kIOHIDElementTypeInput_Axis) &&
|
|
|
|
(elementType != kIOHIDElementTypeInput_Button) &&
|
|
|
|
(elementType != kIOHIDElementTypeInput_Misc))
|
2015-06-26 11:31:37 +00:00
|
|
|
{
|
2015-06-23 20:14:07 +00:00
|
|
|
return;
|
2015-06-26 11:31:37 +00:00
|
|
|
}
|
2015-06-23 20:14:07 +00:00
|
|
|
|
|
|
|
switch (usagePage)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-06-23 20:14:07 +00:00
|
|
|
case kHIDPage_GenericDesktop:
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-06-23 20:14:07 +00:00
|
|
|
switch (usage)
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2015-06-23 20:14:07 +00:00
|
|
|
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:
|
2016-02-08 08:32:48 +00:00
|
|
|
elementsArray = js->axisElements;
|
2015-06-23 20:14:07 +00:00
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Hatswitch:
|
2016-02-08 08:32:48 +00:00
|
|
|
elementsArray = js->hatElements;
|
2015-06-23 20:14:07 +00:00
|
|
|
break;
|
2012-01-29 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 20:14:07 +00:00
|
|
|
break;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-23 20:14:07 +00:00
|
|
|
case kHIDPage_Button:
|
2016-02-08 08:32:48 +00:00
|
|
|
elementsArray = js->buttonElements;
|
2015-06-23 20:14:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-23 20:14:07 +00:00
|
|
|
if (elementsArray)
|
|
|
|
{
|
2015-12-03 17:16:46 +00:00
|
|
|
_GLFWjoyelementNS* element = calloc(1, sizeof(_GLFWjoyelementNS));
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-23 20:14:07 +00:00
|
|
|
CFArrayAppendValue(elementsArray, element);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-23 20:14:07 +00:00
|
|
|
element->elementRef = elementRef;
|
|
|
|
|
|
|
|
element->minReport = IOHIDElementGetLogicalMin(elementRef);
|
|
|
|
element->maxReport = IOHIDElementGetLogicalMax(elementRef);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-29 14:30:01 +00:00
|
|
|
// Adds an element to the specified joystick
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2012-08-14 19:58:22 +00:00
|
|
|
static void getElementsCFArrayHandler(const void* value, void* parameter)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
if (CFGetTypeID(value) == IOHIDElementGetTypeID())
|
2015-06-26 11:31:37 +00:00
|
|
|
{
|
2016-06-07 12:11:54 +00:00
|
|
|
addJoystickElement((_GLFWjoystickNS*) parameter,
|
2015-06-26 11:31:37 +00:00
|
|
|
(IOHIDElementRef) 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
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2016-06-07 12:11:54 +00:00
|
|
|
static long getElementValue(_GLFWjoystickNS* js, _GLFWjoyelementNS* element)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 14:30:01 +00:00
|
|
|
IOReturn result = kIOReturnSuccess;
|
2015-06-20 18:58:26 +00:00
|
|
|
IOHIDValueRef valueRef;
|
|
|
|
long value = 0;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
if (js && element && js->deviceRef)
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2016-02-08 08:32:48 +00:00
|
|
|
result = IOHIDDeviceGetValue(js->deviceRef,
|
2015-06-20 18:58:26 +00:00
|
|
|
element->elementRef,
|
|
|
|
&valueRef);
|
|
|
|
|
2012-01-29 14:30:01 +00:00
|
|
|
if (kIOReturnSuccess == result)
|
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
value = IOHIDValueGetIntegerValue(valueRef);
|
|
|
|
|
2012-08-26 13:38:18 +00:00
|
|
|
// Record min and max for auto calibration
|
2015-06-20 18:58:26 +00:00
|
|
|
if (value < element->minReport)
|
|
|
|
element->minReport = value;
|
|
|
|
if (value > element->maxReport)
|
|
|
|
element->maxReport = value;
|
2012-01-29 14:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-26 13:38:18 +00:00
|
|
|
// Auto user scale
|
2015-06-20 18:58:26 +00:00
|
|
|
return value;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
2012-01-29 14:30:01 +00:00
|
|
|
// Removes the specified joystick
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2016-06-07 12:11:54 +00:00
|
|
|
static void removeJoystick(_GLFWjoystickNS* js)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2012-01-29 23:02:54 +00:00
|
|
|
int i;
|
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
if (!js->present)
|
2013-04-24 17:25:42 +00:00
|
|
|
return;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->axisElements); i++)
|
|
|
|
free((void*) CFArrayGetValueAtIndex(js->axisElements, i));
|
|
|
|
CFArrayRemoveAllValues(js->axisElements);
|
|
|
|
CFRelease(js->axisElements);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->buttonElements); i++)
|
|
|
|
free((void*) CFArrayGetValueAtIndex(js->buttonElements, i));
|
|
|
|
CFArrayRemoveAllValues(js->buttonElements);
|
|
|
|
CFRelease(js->buttonElements);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->hatElements); i++)
|
|
|
|
free((void*) CFArrayGetValueAtIndex(js->hatElements, i));
|
|
|
|
CFArrayRemoveAllValues(js->hatElements);
|
|
|
|
CFRelease(js->hatElements);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
free(js->axes);
|
|
|
|
free(js->buttons);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
memset(js, 0, sizeof(_GLFWjoystickNS));
|
2015-12-13 16:38:50 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
_glfwInputJoystickChange(js - _glfw.ns_js, GLFW_DISCONNECTED);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 22:11:34 +00:00
|
|
|
// Polls for joystick axis events and updates GLFW state
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2016-06-07 12:11:54 +00:00
|
|
|
static GLFWbool pollJoystickAxisEvents(_GLFWjoystickNS* js)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-12-13 14:14:14 +00:00
|
|
|
CFIndex i;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
if (!js->present)
|
2015-12-13 14:14:14 +00:00
|
|
|
return GLFW_FALSE;
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->axisElements); i++)
|
2015-12-13 14:14:14 +00:00
|
|
|
{
|
|
|
|
_GLFWjoyelementNS* axis = (_GLFWjoyelementNS*)
|
2016-02-08 08:32:48 +00:00
|
|
|
CFArrayGetValueAtIndex(js->axisElements, i);
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
long value = getElementValue(js, axis);
|
2015-12-13 14:14:14 +00:00
|
|
|
long readScale = axis->maxReport - axis->minReport;
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2015-12-13 14:14:14 +00:00
|
|
|
if (readScale == 0)
|
2016-02-08 08:32:48 +00:00
|
|
|
js->axes[i] = value;
|
2015-12-13 14:14:14 +00:00
|
|
|
else
|
2016-02-08 08:32:48 +00:00
|
|
|
js->axes[i] = (2.f * (value - axis->minReport) / readScale) - 1.f;
|
2015-12-13 14:14:14 +00:00
|
|
|
}
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2016-03-23 22:11:34 +00:00
|
|
|
return GLFW_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Polls for joystick button events and updates GLFW state
|
|
|
|
//
|
2016-06-07 12:11:54 +00:00
|
|
|
static GLFWbool pollJoystickButtonEvents(_GLFWjoystickNS* js)
|
2016-03-23 22:11:34 +00:00
|
|
|
{
|
|
|
|
CFIndex i;
|
|
|
|
int buttonIndex = 0;
|
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
if (!js->present)
|
2016-03-23 22:11:34 +00:00
|
|
|
return GLFW_FALSE;
|
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->buttonElements); i++)
|
2016-03-23 22:11:34 +00:00
|
|
|
{
|
|
|
|
_GLFWjoyelementNS* button = (_GLFWjoyelementNS*)
|
2016-02-08 08:32:48 +00:00
|
|
|
CFArrayGetValueAtIndex(js->buttonElements, i);
|
2016-03-23 22:11:34 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
if (getElementValue(js, button))
|
|
|
|
js->buttons[buttonIndex++] = GLFW_PRESS;
|
2016-03-23 22:11:34 +00:00
|
|
|
else
|
2016-02-08 08:32:48 +00:00
|
|
|
js->buttons[buttonIndex++] = GLFW_RELEASE;
|
2016-03-23 22:11:34 +00:00
|
|
|
}
|
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->hatElements); i++)
|
2015-12-13 14:14:14 +00:00
|
|
|
{
|
|
|
|
_GLFWjoyelementNS* hat = (_GLFWjoyelementNS*)
|
2016-02-08 08:32:48 +00:00
|
|
|
CFArrayGetValueAtIndex(js->hatElements, i);
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2015-12-13 14:14:14 +00:00
|
|
|
// Bit fields of button presses for each direction, including nil
|
|
|
|
const int directions[9] = { 1, 3, 2, 6, 4, 12, 8, 9, 0 };
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
long j, value = getElementValue(js, hat);
|
2015-12-13 14:14:14 +00:00
|
|
|
if (value < 0 || value > 8)
|
|
|
|
value = 8;
|
2012-03-26 22:24:01 +00:00
|
|
|
|
2015-12-13 14:14:14 +00:00
|
|
|
for (j = 0; j < 4; j++)
|
|
|
|
{
|
|
|
|
if (directions[value] & (1 << j))
|
2016-02-08 08:32:48 +00:00
|
|
|
js->buttons[buttonIndex++] = GLFW_PRESS;
|
2015-12-13 14:14:14 +00:00
|
|
|
else
|
2016-02-08 08:32:48 +00:00
|
|
|
js->buttons[buttonIndex++] = GLFW_RELEASE;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-13 14:14:14 +00:00
|
|
|
|
|
|
|
return GLFW_TRUE;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
// Callback for user-initiated joystick addition
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2015-06-26 11:31:37 +00:00
|
|
|
static void matchCallback(void* context,
|
|
|
|
IOReturn result,
|
|
|
|
void* sender,
|
|
|
|
IOHIDDeviceRef deviceRef)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2016-06-07 12:11:54 +00:00
|
|
|
_GLFWjoystickNS* js;
|
2016-10-10 01:24:07 +00:00
|
|
|
int jid;
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2012-03-26 13:20:31 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
if (_glfw.ns_js[jid].present && _glfw.ns_js[jid].deviceRef == deviceRef)
|
2015-06-20 18:58:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2012-08-14 19:58:22 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
if (!_glfw.ns_js[jid].present)
|
2015-06-20 18:58:26 +00:00
|
|
|
break;
|
2012-08-14 19:58:22 +00:00
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
if (jid > GLFW_JOYSTICK_LAST)
|
2015-06-20 18:58:26 +00:00
|
|
|
return;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
js = _glfw.ns_js + jid;
|
2016-02-08 08:32:48 +00:00
|
|
|
js->present = GLFW_TRUE;
|
|
|
|
js->deviceRef = deviceRef;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
CFStringRef name = IOHIDDeviceGetProperty(deviceRef,
|
|
|
|
CFSTR(kIOHIDProductKey));
|
2016-02-08 19:21:48 +00:00
|
|
|
if (name)
|
|
|
|
{
|
|
|
|
CFStringGetCString(name,
|
2016-02-08 08:32:48 +00:00
|
|
|
js->name,
|
|
|
|
sizeof(js->name),
|
2016-02-08 19:21:48 +00:00
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
}
|
|
|
|
else
|
2016-02-08 08:32:48 +00:00
|
|
|
strncpy(js->name, "Unknown", sizeof(js->name));
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
js->axisElements = CFArrayCreateMutable(NULL, 0, NULL);
|
|
|
|
js->buttonElements = CFArrayCreateMutable(NULL, 0, NULL);
|
|
|
|
js->hatElements = CFArrayCreateMutable(NULL, 0, NULL);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
CFArrayRef arrayRef = IOHIDDeviceCopyMatchingElements(deviceRef,
|
|
|
|
NULL,
|
|
|
|
kIOHIDOptionsTypeNone);
|
2015-06-20 18:58:26 +00:00
|
|
|
CFRange range = { 0, CFArrayGetCount(arrayRef) };
|
|
|
|
CFArrayApplyFunction(arrayRef,
|
|
|
|
range,
|
|
|
|
getElementsCFArrayHandler,
|
2016-02-08 08:32:48 +00:00
|
|
|
(void*) js);
|
2012-08-14 19:58:22 +00:00
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
CFRelease(arrayRef);
|
2014-05-02 05:39:21 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
js->axes = calloc(CFArrayGetCount(js->axisElements), sizeof(float));
|
|
|
|
js->buttons = calloc(CFArrayGetCount(js->buttonElements) +
|
|
|
|
CFArrayGetCount(js->hatElements) * 4, 1);
|
2015-12-13 16:38:50 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
_glfwInputJoystickChange(jid, GLFW_CONNECTED);
|
2015-06-20 18:58:26 +00:00
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
// Callback for user-initiated joystick removal
|
|
|
|
//
|
2015-06-26 11:31:37 +00:00
|
|
|
static void removeCallback(void* context,
|
|
|
|
IOReturn result,
|
|
|
|
void* sender,
|
|
|
|
IOHIDDeviceRef deviceRef)
|
2015-06-20 18:58:26 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
int jid;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2015-06-26 11:31:37 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
if (_glfw.ns_js[jid].deviceRef == deviceRef)
|
2015-06-26 11:31:37 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
removeJoystick(_glfw.ns_js + jid);
|
2015-06-20 18:58:26 +00:00
|
|
|
break;
|
2014-02-06 17:37:37 +00:00
|
|
|
}
|
2015-06-20 18:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
// Creates a dictionary to match against devices with the specified usage page
|
|
|
|
// and usage
|
2015-06-20 18:58:26 +00:00
|
|
|
//
|
2015-06-26 11:31:37 +00:00
|
|
|
static CFMutableDictionaryRef createMatchingDictionary(long usagePage,
|
|
|
|
long usage)
|
2015-06-20 18:58:26 +00:00
|
|
|
{
|
2015-06-26 11:31:37 +00:00
|
|
|
CFMutableDictionaryRef result =
|
|
|
|
CFDictionaryCreateMutable(kCFAllocatorDefault,
|
|
|
|
0,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
if (result)
|
|
|
|
{
|
2015-06-26 11:31:37 +00:00
|
|
|
CFNumberRef pageRef = CFNumberCreate(kCFAllocatorDefault,
|
2015-09-29 02:21:44 +00:00
|
|
|
kCFNumberLongType,
|
2015-06-26 11:31:37 +00:00
|
|
|
&usagePage);
|
2015-06-20 18:58:26 +00:00
|
|
|
if (pageRef)
|
2014-02-06 17:37:37 +00:00
|
|
|
{
|
2015-06-26 11:31:37 +00:00
|
|
|
CFDictionarySetValue(result,
|
|
|
|
CFSTR(kIOHIDDeviceUsagePageKey),
|
|
|
|
pageRef);
|
2015-06-20 18:58:26 +00:00
|
|
|
CFRelease(pageRef);
|
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
CFNumberRef usageRef = CFNumberCreate(kCFAllocatorDefault,
|
2015-09-29 02:21:44 +00:00
|
|
|
kCFNumberLongType,
|
2015-06-26 11:31:37 +00:00
|
|
|
&usage);
|
2015-06-20 18:58:26 +00:00
|
|
|
if (usageRef)
|
|
|
|
{
|
2015-06-26 11:31:37 +00:00
|
|
|
CFDictionarySetValue(result,
|
|
|
|
CFSTR(kIOHIDDeviceUsageKey),
|
|
|
|
usageRef);
|
2015-06-20 18:58:26 +00:00
|
|
|
CFRelease(usageRef);
|
|
|
|
}
|
2014-02-06 17:37:37 +00:00
|
|
|
}
|
2015-06-20 18:58:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
////// GLFW internal API //////
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
// Initialize joystick interface
|
|
|
|
//
|
2015-12-03 17:16:46 +00:00
|
|
|
void _glfwInitJoysticksNS(void)
|
2015-06-20 18:58:26 +00:00
|
|
|
{
|
|
|
|
CFMutableArrayRef matchingCFArrayRef;
|
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
_glfw.ns.hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
|
|
|
|
kIOHIDOptionsTypeNone);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
matchingCFArrayRef = CFArrayCreateMutable(kCFAllocatorDefault,
|
|
|
|
0,
|
|
|
|
&kCFTypeArrayCallBacks);
|
2015-06-20 18:58:26 +00:00
|
|
|
if (matchingCFArrayRef)
|
|
|
|
{
|
2015-06-26 11:31:37 +00:00
|
|
|
CFDictionaryRef matchingCFDictRef =
|
|
|
|
createMatchingDictionary(kHIDPage_GenericDesktop,
|
|
|
|
kHIDUsage_GD_Joystick);
|
2015-06-20 18:58:26 +00:00
|
|
|
if (matchingCFDictRef)
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef);
|
|
|
|
CFRelease(matchingCFDictRef);
|
2012-01-29 14:30:01 +00:00
|
|
|
}
|
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
matchingCFDictRef = createMatchingDictionary(kHIDPage_GenericDesktop,
|
|
|
|
kHIDUsage_GD_GamePad);
|
2015-06-20 18:58:26 +00:00
|
|
|
if (matchingCFDictRef)
|
|
|
|
{
|
|
|
|
CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef);
|
|
|
|
CFRelease(matchingCFDictRef);
|
|
|
|
}
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
matchingCFDictRef =
|
|
|
|
createMatchingDictionary(kHIDPage_GenericDesktop,
|
|
|
|
kHIDUsage_GD_MultiAxisController);
|
2015-06-20 18:58:26 +00:00
|
|
|
if (matchingCFDictRef)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
CFArrayAppendValue(matchingCFArrayRef, matchingCFDictRef);
|
|
|
|
CFRelease(matchingCFDictRef);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2014-05-29 09:28:22 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
IOHIDManagerSetDeviceMatchingMultiple(_glfw.ns.hidManager,
|
2015-09-29 02:21:44 +00:00
|
|
|
matchingCFArrayRef);
|
|
|
|
CFRelease(matchingCFArrayRef);
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
IOHIDManagerRegisterDeviceMatchingCallback(_glfw.ns.hidManager,
|
2015-06-26 11:31:37 +00:00
|
|
|
&matchCallback, NULL);
|
2016-06-07 12:11:54 +00:00
|
|
|
IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns.hidManager,
|
2015-06-26 11:31:37 +00:00
|
|
|
&removeCallback, NULL);
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
IOHIDManagerScheduleWithRunLoop(_glfw.ns.hidManager,
|
2015-06-26 11:31:37 +00:00
|
|
|
CFRunLoopGetMain(),
|
|
|
|
kCFRunLoopDefaultMode);
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
IOHIDManagerOpen(_glfw.ns.hidManager, kIOHIDOptionsTypeNone);
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2015-06-26 11:31:37 +00:00
|
|
|
// Execute the run loop once in order to register any initially-attached
|
|
|
|
// joysticks
|
2015-06-20 18:58:26 +00:00
|
|
|
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close all opened joystick handles
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2015-12-03 17:16:46 +00:00
|
|
|
void _glfwTerminateJoysticksNS(void)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
int jid;
|
2012-01-29 23:02:54 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
for (jid = GLFW_JOYSTICK_1; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
_GLFWjoystickNS* js = _glfw.ns_js + jid;
|
2016-02-08 08:32:48 +00:00
|
|
|
removeJoystick(js);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
CFRelease(_glfw.ns.hidManager);
|
|
|
|
_glfw.ns.hidManager = NULL;
|
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
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
int _glfwPlatformJoystickPresent(int jid)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
_GLFWjoystickNS* js = _glfw.ns_js + jid;
|
2016-02-08 08:32:48 +00:00
|
|
|
return js->present;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
const float* _glfwPlatformGetJoystickAxes(int jid, int* count)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
_GLFWjoystickNS* js = _glfw.ns_js + jid;
|
2016-02-08 08:32:48 +00:00
|
|
|
if (!pollJoystickAxisEvents(js))
|
2013-04-24 17:25:42 +00:00
|
|
|
return NULL;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
*count = (int) CFArrayGetCount(js->axisElements);
|
|
|
|
return js->axes;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
const unsigned char* _glfwPlatformGetJoystickButtons(int jid, int* count)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
_GLFWjoystickNS* js = _glfw.ns_js + jid;
|
2016-02-08 08:32:48 +00:00
|
|
|
if (!pollJoystickButtonEvents(js))
|
2013-04-24 17:25:42 +00:00
|
|
|
return NULL;
|
2012-08-14 19:58:22 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
*count = (int) CFArrayGetCount(js->buttonElements) +
|
|
|
|
(int) CFArrayGetCount(js->hatElements) * 4;
|
|
|
|
return js->buttons;
|
2012-03-26 22:24:01 +00:00
|
|
|
}
|
2012-08-14 19:58:22 +00:00
|
|
|
|
2016-10-10 01:24:07 +00:00
|
|
|
const char* _glfwPlatformGetJoystickName(int jid)
|
2012-09-06 23:01:34 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
_GLFWjoystickNS* js = _glfw.ns_js + jid;
|
2016-02-08 08:32:48 +00:00
|
|
|
if (!js->present)
|
2015-12-13 14:14:14 +00:00
|
|
|
return NULL;
|
2016-03-29 11:42:11 +00:00
|
|
|
|
2016-02-08 08:32:48 +00:00
|
|
|
return js->name;
|
2012-09-06 23:01:34 +00:00
|
|
|
}
|
|
|
|
|