Cocoa: Clean up joystick globals

This commit is contained in:
Camilla Berglund 2016-06-07 14:11:54 +02:00
parent b6b8ff591d
commit 76801973e1
3 changed files with 33 additions and 39 deletions

View File

@ -32,12 +32,13 @@
#include <IOKit/hid/IOHIDLib.h> #include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h> #include <IOKit/hid/IOHIDKeys.h>
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE _GLFWjoystickNS ns_js #define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \
_GLFWjoystickNS ns_js[GLFW_JOYSTICK_LAST + 1]
// Cocoa-specific per-joystick data // Cocoa-specific per-joystick data
// //
typedef struct _GLFWjoydeviceNS typedef struct _GLFWjoystickNS
{ {
GLFWbool present; GLFWbool present;
char name[256]; char name[256];
@ -50,17 +51,9 @@ typedef struct _GLFWjoydeviceNS
float* axes; float* axes;
unsigned char* buttons; unsigned char* buttons;
} _GLFWjoydeviceNS;
// Cocoa-specific joystick API data
//
typedef struct _GLFWjoystickNS
{
_GLFWjoydeviceNS js[GLFW_JOYSTICK_LAST + 1];
IOHIDManagerRef managerRef;
} _GLFWjoystickNS; } _GLFWjoystickNS;
void _glfwInitJoysticksNS(void); void _glfwInitJoysticksNS(void);
void _glfwTerminateJoysticksNS(void); void _glfwTerminateJoysticksNS(void);

View File

@ -57,7 +57,7 @@ static void getElementsCFArrayHandler(const void* value, void* parameter);
// Adds an element to the specified joystick // Adds an element to the specified joystick
// //
static void addJoystickElement(_GLFWjoydeviceNS* js, static void addJoystickElement(_GLFWjoystickNS* js,
IOHIDElementRef elementRef) IOHIDElementRef elementRef)
{ {
IOHIDElementType elementType; IOHIDElementType elementType;
@ -126,14 +126,14 @@ static void getElementsCFArrayHandler(const void* value, void* parameter)
{ {
if (CFGetTypeID(value) == IOHIDElementGetTypeID()) if (CFGetTypeID(value) == IOHIDElementGetTypeID())
{ {
addJoystickElement((_GLFWjoydeviceNS*) parameter, addJoystickElement((_GLFWjoystickNS*) parameter,
(IOHIDElementRef) value); (IOHIDElementRef) value);
} }
} }
// Returns the value of the specified element of the specified joystick // Returns the value of the specified element of the specified joystick
// //
static long getElementValue(_GLFWjoydeviceNS* js, _GLFWjoyelementNS* element) static long getElementValue(_GLFWjoystickNS* js, _GLFWjoyelementNS* element)
{ {
IOReturn result = kIOReturnSuccess; IOReturn result = kIOReturnSuccess;
IOHIDValueRef valueRef; IOHIDValueRef valueRef;
@ -163,7 +163,7 @@ static long getElementValue(_GLFWjoydeviceNS* js, _GLFWjoyelementNS* element)
// Removes the specified joystick // Removes the specified joystick
// //
static void removeJoystick(_GLFWjoydeviceNS* js) static void removeJoystick(_GLFWjoystickNS* js)
{ {
int i; int i;
@ -188,14 +188,14 @@ static void removeJoystick(_GLFWjoydeviceNS* js)
free(js->axes); free(js->axes);
free(js->buttons); free(js->buttons);
memset(js, 0, sizeof(_GLFWjoydeviceNS)); memset(js, 0, sizeof(_GLFWjoystickNS));
_glfwInputJoystickChange(js - _glfw.ns_js.js, GLFW_DISCONNECTED); _glfwInputJoystickChange(js - _glfw.ns_js, GLFW_DISCONNECTED);
} }
// Polls for joystick axis events and updates GLFW state // Polls for joystick axis events and updates GLFW state
// //
static GLFWbool pollJoystickAxisEvents(_GLFWjoydeviceNS* js) static GLFWbool pollJoystickAxisEvents(_GLFWjoystickNS* js)
{ {
CFIndex i; CFIndex i;
@ -221,7 +221,7 @@ static GLFWbool pollJoystickAxisEvents(_GLFWjoydeviceNS* js)
// Polls for joystick button events and updates GLFW state // Polls for joystick button events and updates GLFW state
// //
static GLFWbool pollJoystickButtonEvents(_GLFWjoydeviceNS* js) static GLFWbool pollJoystickButtonEvents(_GLFWjoystickNS* js)
{ {
CFIndex i; CFIndex i;
int buttonIndex = 0; int buttonIndex = 0;
@ -271,25 +271,25 @@ static void matchCallback(void* context,
void* sender, void* sender,
IOHIDDeviceRef deviceRef) IOHIDDeviceRef deviceRef)
{ {
_GLFWjoydeviceNS* js; _GLFWjoystickNS* js;
int joy; int joy;
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
if (_glfw.ns_js.js[joy].present && _glfw.ns_js.js[joy].deviceRef == deviceRef) if (_glfw.ns_js[joy].present && _glfw.ns_js[joy].deviceRef == deviceRef)
return; return;
} }
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
if (!_glfw.ns_js.js[joy].present) if (!_glfw.ns_js[joy].present)
break; break;
} }
if (joy > GLFW_JOYSTICK_LAST) if (joy > GLFW_JOYSTICK_LAST)
return; return;
js = _glfw.ns_js.js + joy; js = _glfw.ns_js + joy;
js->present = GLFW_TRUE; js->present = GLFW_TRUE;
js->deviceRef = deviceRef; js->deviceRef = deviceRef;
@ -338,9 +338,9 @@ static void removeCallback(void* context,
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
if (_glfw.ns_js.js[joy].deviceRef == deviceRef) if (_glfw.ns_js[joy].deviceRef == deviceRef)
{ {
removeJoystick(_glfw.ns_js.js + joy); removeJoystick(_glfw.ns_js + joy);
break; break;
} }
} }
@ -397,8 +397,8 @@ void _glfwInitJoysticksNS(void)
{ {
CFMutableArrayRef matchingCFArrayRef; CFMutableArrayRef matchingCFArrayRef;
_glfw.ns_js.managerRef = IOHIDManagerCreate(kCFAllocatorDefault, _glfw.ns.hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
kIOHIDOptionsTypeNone); kIOHIDOptionsTypeNone);
matchingCFArrayRef = CFArrayCreateMutable(kCFAllocatorDefault, matchingCFArrayRef = CFArrayCreateMutable(kCFAllocatorDefault,
0, 0,
@ -431,21 +431,21 @@ void _glfwInitJoysticksNS(void)
CFRelease(matchingCFDictRef); CFRelease(matchingCFDictRef);
} }
IOHIDManagerSetDeviceMatchingMultiple(_glfw.ns_js.managerRef, IOHIDManagerSetDeviceMatchingMultiple(_glfw.ns.hidManager,
matchingCFArrayRef); matchingCFArrayRef);
CFRelease(matchingCFArrayRef); CFRelease(matchingCFArrayRef);
} }
IOHIDManagerRegisterDeviceMatchingCallback(_glfw.ns_js.managerRef, IOHIDManagerRegisterDeviceMatchingCallback(_glfw.ns.hidManager,
&matchCallback, NULL); &matchCallback, NULL);
IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns_js.managerRef, IOHIDManagerRegisterDeviceRemovalCallback(_glfw.ns.hidManager,
&removeCallback, NULL); &removeCallback, NULL);
IOHIDManagerScheduleWithRunLoop(_glfw.ns_js.managerRef, IOHIDManagerScheduleWithRunLoop(_glfw.ns.hidManager,
CFRunLoopGetMain(), CFRunLoopGetMain(),
kCFRunLoopDefaultMode); kCFRunLoopDefaultMode);
IOHIDManagerOpen(_glfw.ns_js.managerRef, kIOHIDOptionsTypeNone); IOHIDManagerOpen(_glfw.ns.hidManager, kIOHIDOptionsTypeNone);
// Execute the run loop once in order to register any initially-attached // Execute the run loop once in order to register any initially-attached
// joysticks // joysticks
@ -460,12 +460,12 @@ void _glfwTerminateJoysticksNS(void)
for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) for (joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++)
{ {
_GLFWjoydeviceNS* js = _glfw.ns_js.js + joy; _GLFWjoystickNS* js = _glfw.ns_js + joy;
removeJoystick(js); removeJoystick(js);
} }
CFRelease(_glfw.ns_js.managerRef); CFRelease(_glfw.ns.hidManager);
_glfw.ns_js.managerRef = NULL; _glfw.ns.hidManager = NULL;
} }
@ -475,13 +475,13 @@ void _glfwTerminateJoysticksNS(void)
int _glfwPlatformJoystickPresent(int joy) int _glfwPlatformJoystickPresent(int joy)
{ {
_GLFWjoydeviceNS* js = _glfw.ns_js.js + joy; _GLFWjoystickNS* js = _glfw.ns_js + joy;
return js->present; return js->present;
} }
const float* _glfwPlatformGetJoystickAxes(int joy, int* count) const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{ {
_GLFWjoydeviceNS* js = _glfw.ns_js.js + joy; _GLFWjoystickNS* js = _glfw.ns_js + joy;
if (!pollJoystickAxisEvents(js)) if (!pollJoystickAxisEvents(js))
return NULL; return NULL;
@ -491,7 +491,7 @@ const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count) const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{ {
_GLFWjoydeviceNS* js = _glfw.ns_js.js + joy; _GLFWjoystickNS* js = _glfw.ns_js + joy;
if (!pollJoystickButtonEvents(js)) if (!pollJoystickButtonEvents(js))
return NULL; return NULL;
@ -502,7 +502,7 @@ const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
const char* _glfwPlatformGetJoystickName(int joy) const char* _glfwPlatformGetJoystickName(int joy)
{ {
_GLFWjoydeviceNS* js = _glfw.ns_js.js + joy; _GLFWjoystickNS* js = _glfw.ns_js + joy;
if (!js->present) if (!js->present)
return NULL; return NULL;

View File

@ -91,6 +91,7 @@ typedef struct _GLFWlibraryNS
id autoreleasePool; id autoreleasePool;
id cursor; id cursor;
TISInputSourceRef inputSource; TISInputSourceRef inputSource;
IOHIDManagerRef hidManager;
id unicodeData; id unicodeData;
id listener; id listener;