2010-09-07 15:34:51 +00:00
|
|
|
//========================================================================
|
2019-04-16 12:43:29 +00:00
|
|
|
// GLFW 3.4 Cocoa - www.glfw.org
|
2010-09-07 15:34:51 +00:00
|
|
|
//------------------------------------------------------------------------
|
2019-04-15 18:50:00 +00:00
|
|
|
// Copyright (c) 2009-2019 Camilla Löwy <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.
|
|
|
|
//
|
|
|
|
//========================================================================
|
2019-05-20 13:24:14 +00:00
|
|
|
// It is fine to use C99 in this file because it will not be built with VS
|
|
|
|
//========================================================================
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
#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
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
IOHIDElementRef native;
|
2017-06-18 13:13:25 +00:00
|
|
|
uint32_t usage;
|
|
|
|
int index;
|
2017-01-05 18:44:15 +00:00
|
|
|
long minimum;
|
|
|
|
long maximum;
|
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-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
|
|
|
//
|
2017-01-05 18:44:15 +00:00
|
|
|
static long getElementValue(_GLFWjoystick* js, _GLFWjoyelementNS* element)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
IOHIDValueRef valueRef;
|
|
|
|
long value = 0;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-03-23 14:54:34 +00:00
|
|
|
if (js->ns.device)
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2017-03-23 14:54:34 +00:00
|
|
|
if (IOHIDDeviceGetValue(js->ns.device,
|
|
|
|
element->native,
|
|
|
|
&valueRef) == kIOReturnSuccess)
|
2012-01-29 14:30:01 +00:00
|
|
|
{
|
2015-06-20 18:58:26 +00:00
|
|
|
value = IOHIDValueGetIntegerValue(valueRef);
|
2012-01-29 14:30:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
return value;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 13:13:25 +00:00
|
|
|
// Comparison function for matching the SDL element order
|
|
|
|
//
|
2017-09-17 12:39:17 +00:00
|
|
|
static CFComparisonResult compareElements(const void* fp,
|
|
|
|
const void* sp,
|
|
|
|
void* user)
|
2017-06-18 13:13:25 +00:00
|
|
|
{
|
|
|
|
const _GLFWjoyelementNS* fe = fp;
|
|
|
|
const _GLFWjoyelementNS* se = sp;
|
|
|
|
if (fe->usage < se->usage)
|
|
|
|
return kCFCompareLessThan;
|
|
|
|
if (fe->usage > se->usage)
|
|
|
|
return kCFCompareGreaterThan;
|
|
|
|
if (fe->index < se->index)
|
|
|
|
return kCFCompareLessThan;
|
|
|
|
if (fe->index > se->index)
|
|
|
|
return kCFCompareGreaterThan;
|
|
|
|
return kCFCompareEqualTo;
|
|
|
|
}
|
|
|
|
|
2012-01-29 14:30:01 +00:00
|
|
|
// Removes the specified joystick
|
2013-02-04 12:22:10 +00:00
|
|
|
//
|
2017-01-05 18:44:15 +00:00
|
|
|
static void closeJoystick(_GLFWjoystick* 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
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->ns.axes); i++)
|
2021-08-03 18:53:48 +00:00
|
|
|
_glfw_free((void*) CFArrayGetValueAtIndex(js->ns.axes, i));
|
2017-01-05 18:44:15 +00:00
|
|
|
CFRelease(js->ns.axes);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->ns.buttons); i++)
|
2021-08-03 18:53:48 +00:00
|
|
|
_glfw_free((void*) CFArrayGetValueAtIndex(js->ns.buttons, i));
|
2017-01-05 18:44:15 +00:00
|
|
|
CFRelease(js->ns.buttons);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->ns.hats); i++)
|
2021-08-03 18:53:48 +00:00
|
|
|
_glfw_free((void*) CFArrayGetValueAtIndex(js->ns.hats, i));
|
2017-01-05 18:44:15 +00:00
|
|
|
CFRelease(js->ns.hats);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
_glfwFreeJoystick(js);
|
2017-07-09 12:01:48 +00:00
|
|
|
_glfwInputJoystick(js, GLFW_DISCONNECTED);
|
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,
|
2017-01-05 18:44:15 +00:00
|
|
|
IOHIDDeviceRef device)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2016-10-10 01:24:07 +00:00
|
|
|
int jid;
|
2017-01-05 18:44:15 +00:00
|
|
|
char name[256];
|
2017-06-18 13:13:25 +00:00
|
|
|
char guid[33];
|
2017-01-05 18:44:15 +00:00
|
|
|
CFIndex i;
|
2017-06-18 13:13:25 +00:00
|
|
|
CFTypeRef property;
|
|
|
|
uint32_t vendor = 0, product = 0, version = 0;
|
2017-01-05 18:44:15 +00:00
|
|
|
_GLFWjoystick* js;
|
|
|
|
CFMutableArrayRef axes, buttons, hats;
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2017-01-12 04:30:56 +00:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2012-03-26 13:20:31 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
if (_glfw.joysticks[jid].ns.device == device)
|
2015-06-20 18:58:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
axes = CFArrayCreateMutable(NULL, 0, NULL);
|
|
|
|
buttons = CFArrayCreateMutable(NULL, 0, NULL);
|
|
|
|
hats = CFArrayCreateMutable(NULL, 0, NULL);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-06-18 13:13:25 +00:00
|
|
|
property = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey));
|
|
|
|
if (property)
|
2016-02-08 19:21:48 +00:00
|
|
|
{
|
2017-06-18 13:13:25 +00:00
|
|
|
CFStringGetCString(property,
|
2017-01-05 18:44:15 +00:00
|
|
|
name,
|
|
|
|
sizeof(name),
|
2016-02-08 19:21:48 +00:00
|
|
|
kCFStringEncodingUTF8);
|
|
|
|
}
|
|
|
|
else
|
2017-01-05 18:44:15 +00:00
|
|
|
strncpy(name, "Unknown", sizeof(name));
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-06-18 13:13:25 +00:00
|
|
|
property = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey));
|
|
|
|
if (property)
|
|
|
|
CFNumberGetValue(property, kCFNumberSInt32Type, &vendor);
|
|
|
|
|
|
|
|
property = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey));
|
|
|
|
if (property)
|
|
|
|
CFNumberGetValue(property, kCFNumberSInt32Type, &product);
|
|
|
|
|
|
|
|
property = IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVersionNumberKey));
|
|
|
|
if (property)
|
|
|
|
CFNumberGetValue(property, kCFNumberSInt32Type, &version);
|
|
|
|
|
|
|
|
// Generate a joystick GUID that matches the SDL 2.0.5+ one
|
|
|
|
if (vendor && product)
|
|
|
|
{
|
|
|
|
sprintf(guid, "03000000%02x%02x0000%02x%02x0000%02x%02x0000",
|
|
|
|
(uint8_t) vendor, (uint8_t) (vendor >> 8),
|
|
|
|
(uint8_t) product, (uint8_t) (product >> 8),
|
|
|
|
(uint8_t) version, (uint8_t) (version >> 8));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprintf(guid, "05000000%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x00",
|
|
|
|
name[0], name[1], name[2], name[3],
|
|
|
|
name[4], name[5], name[6], name[7],
|
|
|
|
name[8], name[9], name[10]);
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
CFArrayRef elements =
|
|
|
|
IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone);
|
|
|
|
|
|
|
|
for (i = 0; i < CFArrayGetCount(elements); i++)
|
|
|
|
{
|
2017-09-17 12:39:17 +00:00
|
|
|
IOHIDElementRef native = (IOHIDElementRef)
|
|
|
|
CFArrayGetValueAtIndex(elements, i);
|
2017-01-05 18:44:15 +00:00
|
|
|
if (CFGetTypeID(native) != IOHIDElementGetTypeID())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const IOHIDElementType type = IOHIDElementGetType(native);
|
|
|
|
if ((type != kIOHIDElementTypeInput_Axis) &&
|
|
|
|
(type != kIOHIDElementTypeInput_Button) &&
|
|
|
|
(type != kIOHIDElementTypeInput_Misc))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
CFMutableArrayRef target = NULL;
|
2012-08-14 19:58:22 +00:00
|
|
|
|
2017-07-04 15:22:29 +00:00
|
|
|
const uint32_t usage = IOHIDElementGetUsage(native);
|
|
|
|
const uint32_t page = IOHIDElementGetUsagePage(native);
|
|
|
|
if (page == kHIDPage_GenericDesktop)
|
2017-01-05 18:44:15 +00:00
|
|
|
{
|
2017-07-04 15:22:29 +00:00
|
|
|
switch (usage)
|
2017-01-05 18:44:15 +00:00
|
|
|
{
|
2017-07-04 15:22:29 +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:
|
|
|
|
target = axes;
|
|
|
|
break;
|
|
|
|
case kHIDUsage_GD_Hatswitch:
|
|
|
|
target = hats;
|
|
|
|
break;
|
2019-01-30 23:44:10 +00:00
|
|
|
case kHIDUsage_GD_DPadUp:
|
|
|
|
case kHIDUsage_GD_DPadRight:
|
|
|
|
case kHIDUsage_GD_DPadDown:
|
|
|
|
case kHIDUsage_GD_DPadLeft:
|
|
|
|
case kHIDUsage_GD_SystemMainMenu:
|
|
|
|
case kHIDUsage_GD_Select:
|
|
|
|
case kHIDUsage_GD_Start:
|
|
|
|
target = buttons;
|
|
|
|
break;
|
2017-01-05 18:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 21:58:47 +00:00
|
|
|
else if (page == kHIDPage_Simulation)
|
|
|
|
{
|
|
|
|
switch (usage)
|
|
|
|
{
|
|
|
|
case kHIDUsage_Sim_Accelerator:
|
|
|
|
case kHIDUsage_Sim_Brake:
|
|
|
|
case kHIDUsage_Sim_Throttle:
|
|
|
|
case kHIDUsage_Sim_Rudder:
|
|
|
|
case kHIDUsage_Sim_Steering:
|
|
|
|
target = axes;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 23:44:10 +00:00
|
|
|
else if (page == kHIDPage_Button || page == kHIDPage_Consumer)
|
2017-07-04 15:22:29 +00:00
|
|
|
target = buttons;
|
2017-01-05 18:44:15 +00:00
|
|
|
|
|
|
|
if (target)
|
|
|
|
{
|
2021-08-03 18:53:48 +00:00
|
|
|
_GLFWjoyelementNS* element = _glfw_calloc(1, sizeof(_GLFWjoyelementNS));
|
2017-01-05 18:44:15 +00:00
|
|
|
element->native = native;
|
2017-06-18 13:13:25 +00:00
|
|
|
element->usage = usage;
|
|
|
|
element->index = (int) CFArrayGetCount(target);
|
2017-01-05 18:44:15 +00:00
|
|
|
element->minimum = IOHIDElementGetLogicalMin(native);
|
|
|
|
element->maximum = IOHIDElementGetLogicalMax(native);
|
|
|
|
CFArrayAppendValue(target, element);
|
|
|
|
}
|
|
|
|
}
|
2014-05-02 05:39:21 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
CFRelease(elements);
|
2015-12-13 16:38:50 +00:00
|
|
|
|
2017-06-18 13:13:25 +00:00
|
|
|
CFArraySortValues(axes, CFRangeMake(0, CFArrayGetCount(axes)),
|
|
|
|
compareElements, NULL);
|
|
|
|
CFArraySortValues(buttons, CFRangeMake(0, CFArrayGetCount(buttons)),
|
|
|
|
compareElements, NULL);
|
|
|
|
CFArraySortValues(hats, CFRangeMake(0, CFArrayGetCount(hats)),
|
|
|
|
compareElements, NULL);
|
|
|
|
|
|
|
|
js = _glfwAllocJoystick(name, guid,
|
2017-11-16 19:10:51 +00:00
|
|
|
(int) CFArrayGetCount(axes),
|
|
|
|
(int) CFArrayGetCount(buttons),
|
|
|
|
(int) CFArrayGetCount(hats));
|
2017-01-05 18:44:15 +00:00
|
|
|
|
|
|
|
js->ns.device = device;
|
|
|
|
js->ns.axes = axes;
|
|
|
|
js->ns.buttons = buttons;
|
|
|
|
js->ns.hats = hats;
|
|
|
|
|
2017-07-09 12:01:48 +00:00
|
|
|
_glfwInputJoystick(js, 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,
|
2017-01-05 18:44:15 +00:00
|
|
|
IOHIDDeviceRef device)
|
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
|
|
|
|
2017-01-12 04:30:56 +00:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2015-06-26 11:31:37 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
if (_glfw.joysticks[jid].ns.device == device)
|
2015-06-26 11:31:37 +00:00
|
|
|
{
|
2017-01-05 18:44:15 +00:00
|
|
|
closeJoystick(_glfw.joysticks + 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
|
|
|
|
2015-06-20 18:58:26 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2020-07-21 15:46:48 +00:00
|
|
|
////// GLFW platform API //////
|
2015-06-20 18:58:26 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2020-07-12 16:31:29 +00:00
|
|
|
GLFWbool _glfwPlatformInitJoysticks(void)
|
2015-06-20 18:58:26 +00:00
|
|
|
{
|
2017-02-26 21:18:40 +00:00
|
|
|
CFMutableArrayRef matching;
|
|
|
|
const long usages[] =
|
|
|
|
{
|
|
|
|
kHIDUsage_GD_Joystick,
|
|
|
|
kHIDUsage_GD_GamePad,
|
|
|
|
kHIDUsage_GD_MultiAxisController
|
|
|
|
};
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2016-06-07 12:11:54 +00:00
|
|
|
_glfw.ns.hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
|
|
|
|
kIOHIDOptionsTypeNone);
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-02-26 21:18:40 +00:00
|
|
|
matching = CFArrayCreateMutable(kCFAllocatorDefault,
|
|
|
|
0,
|
|
|
|
&kCFTypeArrayCallBacks);
|
|
|
|
if (!matching)
|
2015-06-20 18:58:26 +00:00
|
|
|
{
|
2017-02-26 21:18:40 +00:00
|
|
|
_glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to create array");
|
2020-07-12 16:31:29 +00:00
|
|
|
return GLFW_FALSE;
|
2017-02-26 21:18:40 +00:00
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2019-10-06 20:53:42 +00:00
|
|
|
for (size_t i = 0; i < sizeof(usages) / sizeof(long); i++)
|
2017-02-26 21:18:40 +00:00
|
|
|
{
|
|
|
|
const long page = kHIDPage_GenericDesktop;
|
|
|
|
|
|
|
|
CFMutableDictionaryRef dict =
|
|
|
|
CFDictionaryCreateMutable(kCFAllocatorDefault,
|
|
|
|
0,
|
|
|
|
&kCFTypeDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (!dict)
|
|
|
|
continue;
|
2013-04-24 17:25:42 +00:00
|
|
|
|
2017-02-26 21:18:40 +00:00
|
|
|
CFNumberRef pageRef = CFNumberCreate(kCFAllocatorDefault,
|
|
|
|
kCFNumberLongType,
|
|
|
|
&page);
|
|
|
|
CFNumberRef usageRef = CFNumberCreate(kCFAllocatorDefault,
|
|
|
|
kCFNumberLongType,
|
|
|
|
&usages[i]);
|
|
|
|
if (pageRef && usageRef)
|
2011-09-18 19:05:00 +00:00
|
|
|
{
|
2017-02-26 21:18:40 +00:00
|
|
|
CFDictionarySetValue(dict,
|
|
|
|
CFSTR(kIOHIDDeviceUsagePageKey),
|
|
|
|
pageRef);
|
|
|
|
CFDictionarySetValue(dict,
|
|
|
|
CFSTR(kIOHIDDeviceUsageKey),
|
|
|
|
usageRef);
|
|
|
|
CFArrayAppendValue(matching, dict);
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
2014-05-29 09:28:22 +00:00
|
|
|
|
2017-02-26 21:18:40 +00:00
|
|
|
if (pageRef)
|
|
|
|
CFRelease(pageRef);
|
|
|
|
if (usageRef)
|
|
|
|
CFRelease(usageRef);
|
|
|
|
|
|
|
|
CFRelease(dict);
|
2015-09-29 02:21:44 +00:00
|
|
|
}
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-02-26 21:18:40 +00:00
|
|
|
IOHIDManagerSetDeviceMatchingMultiple(_glfw.ns.hidManager, matching);
|
|
|
|
CFRelease(matching);
|
|
|
|
|
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);
|
2016-06-07 12:11:54 +00:00
|
|
|
IOHIDManagerScheduleWithRunLoop(_glfw.ns.hidManager,
|
2015-06-26 11:31:37 +00:00
|
|
|
CFRunLoopGetMain(),
|
|
|
|
kCFRunLoopDefaultMode);
|
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);
|
2020-07-12 16:31:29 +00:00
|
|
|
return GLFW_TRUE;
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
2020-07-12 16:31:29 +00:00
|
|
|
void _glfwPlatformTerminateJoysticks(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
|
|
|
|
2017-01-12 04:30:56 +00:00
|
|
|
for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
|
2017-01-05 18:44:15 +00:00
|
|
|
closeJoystick(_glfw.joysticks + jid);
|
2015-06-20 18:58:26 +00:00
|
|
|
|
2020-07-12 16:31:29 +00:00
|
|
|
if (_glfw.ns.hidManager)
|
|
|
|
{
|
|
|
|
CFRelease(_glfw.ns.hidManager);
|
|
|
|
_glfw.ns.hidManager = NULL;
|
|
|
|
}
|
2011-09-18 19:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-09 12:01:48 +00:00
|
|
|
int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2017-06-18 13:13:25 +00:00
|
|
|
if (mode & _GLFW_POLL_AXES)
|
2017-01-05 18:44:15 +00:00
|
|
|
{
|
|
|
|
CFIndex i;
|
2012-01-29 14:30:01 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->ns.axes); i++)
|
|
|
|
{
|
|
|
|
_GLFWjoyelementNS* axis = (_GLFWjoyelementNS*)
|
|
|
|
CFArrayGetValueAtIndex(js->ns.axes, i);
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2017-03-23 16:30:39 +00:00
|
|
|
const long raw = getElementValue(js, axis);
|
2017-03-23 14:54:34 +00:00
|
|
|
// Perform auto calibration
|
2017-03-23 16:30:39 +00:00
|
|
|
if (raw < axis->minimum)
|
|
|
|
axis->minimum = raw;
|
|
|
|
if (raw > axis->maximum)
|
|
|
|
axis->maximum = raw;
|
2012-08-14 19:58:22 +00:00
|
|
|
|
2019-01-31 00:58:04 +00:00
|
|
|
const long size = axis->maximum - axis->minimum;
|
|
|
|
if (size == 0)
|
2017-11-16 19:10:51 +00:00
|
|
|
_glfwInputJoystickAxis(js, (int) i, 0.f);
|
2017-01-05 18:44:15 +00:00
|
|
|
else
|
2017-03-23 16:30:39 +00:00
|
|
|
{
|
2019-01-31 00:58:04 +00:00
|
|
|
const float value = (2.f * (raw - axis->minimum) / size) - 1.f;
|
2017-11-16 19:10:51 +00:00
|
|
|
_glfwInputJoystickAxis(js, (int) i, value);
|
2017-03-23 16:30:39 +00:00
|
|
|
}
|
2017-01-05 18:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-18 13:13:25 +00:00
|
|
|
|
|
|
|
if (mode & _GLFW_POLL_BUTTONS)
|
2017-01-05 18:44:15 +00:00
|
|
|
{
|
2017-03-01 22:27:20 +00:00
|
|
|
CFIndex i;
|
2012-08-14 19:58:22 +00:00
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
for (i = 0; i < CFArrayGetCount(js->ns.buttons); i++)
|
|
|
|
{
|
|
|
|
_GLFWjoyelementNS* button = (_GLFWjoyelementNS*)
|
|
|
|
CFArrayGetValueAtIndex(js->ns.buttons, i);
|
2017-03-23 14:54:34 +00:00
|
|
|
const char value = getElementValue(js, button) - button->minimum;
|
2019-01-31 00:54:06 +00:00
|
|
|
const int state = (value > 0) ? GLFW_PRESS : GLFW_RELEASE;
|
|
|
|
_glfwInputJoystickButton(js, (int) i, state);
|
2017-01-05 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < CFArrayGetCount(js->ns.hats); i++)
|
|
|
|
{
|
2017-03-01 22:27:20 +00:00
|
|
|
const int states[9] =
|
|
|
|
{
|
|
|
|
GLFW_HAT_UP,
|
|
|
|
GLFW_HAT_RIGHT_UP,
|
|
|
|
GLFW_HAT_RIGHT,
|
|
|
|
GLFW_HAT_RIGHT_DOWN,
|
|
|
|
GLFW_HAT_DOWN,
|
|
|
|
GLFW_HAT_LEFT_DOWN,
|
|
|
|
GLFW_HAT_LEFT,
|
|
|
|
GLFW_HAT_LEFT_UP,
|
|
|
|
GLFW_HAT_CENTERED
|
|
|
|
};
|
|
|
|
|
2017-01-05 18:44:15 +00:00
|
|
|
_GLFWjoyelementNS* hat = (_GLFWjoyelementNS*)
|
|
|
|
CFArrayGetValueAtIndex(js->ns.hats, i);
|
2017-03-23 14:54:34 +00:00
|
|
|
long state = getElementValue(js, hat) - hat->minimum;
|
2017-01-05 18:44:15 +00:00
|
|
|
if (state < 0 || state > 8)
|
|
|
|
state = 8;
|
|
|
|
|
2017-11-16 19:10:51 +00:00
|
|
|
_glfwInputJoystickHat(js, (int) i, states[state]);
|
2017-01-05 18:44:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return js->present;
|
2012-09-06 23:01:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 20:12:21 +00:00
|
|
|
const char* _glfwPlatformGetMappingName(void)
|
|
|
|
{
|
|
|
|
return "Mac OS X";
|
|
|
|
}
|
|
|
|
|
2017-06-18 13:13:25 +00:00
|
|
|
void _glfwPlatformUpdateGamepadGUID(char* guid)
|
|
|
|
{
|
|
|
|
if ((strncmp(guid + 4, "000000000000", 12) == 0) &&
|
|
|
|
(strncmp(guid + 20, "000000000000", 12) == 0))
|
|
|
|
{
|
|
|
|
char original[33];
|
2019-03-04 17:52:56 +00:00
|
|
|
strncpy(original, guid, sizeof(original) - 1);
|
2017-06-18 13:13:25 +00:00
|
|
|
sprintf(guid, "03000000%.4s0000%.4s000000000000",
|
|
|
|
original, original + 16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|