Moved Cocoa joystick state to library struct.

This commit is contained in:
Camilla Berglund 2013-02-14 19:07:17 +01:00
parent 94f5b28152
commit ec8273f249
2 changed files with 61 additions and 62 deletions

View File

@ -37,10 +37,6 @@
#include <mach/mach_error.h>
#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>
@ -59,30 +55,7 @@ typedef struct
long minReport;
long maxReport;
} _glfwJoystickElement;
//------------------------------------------------------------------------
// Joystick information & state
//------------------------------------------------------------------------
typedef struct
{
int present;
char name[256];
IOHIDDeviceInterface** interface;
int numAxes;
int numButtons;
int numHats;
CFMutableArrayRef axes;
CFMutableArrayRef buttons;
CFMutableArrayRef hats;
} _glfwJoystick;
static _glfwJoystick _glfwJoysticks[GLFW_JOYSTICK_LAST + 1];
} _GLFWjoyelement;
static void getElementsCFArrayHandler(const void* value, void* parameter);
@ -90,7 +63,7 @@ static void getElementsCFArrayHandler(const void* value, void* parameter);
// Adds an element to the specified joystick
//
static void addJoystickElement(_glfwJoystick* joystick, CFTypeRef refElement)
static void addJoystickElement(_GLFWjoy* joystick, CFTypeRef refElement)
{
long elementType, usagePage, usage;
CFTypeRef refElementType, refUsagePage, refUsage;
@ -149,7 +122,7 @@ static void addJoystickElement(_glfwJoystick* joystick, CFTypeRef refElement)
long number;
CFTypeRef refType;
_glfwJoystickElement* element = (_glfwJoystickElement*) malloc(sizeof(_glfwJoystickElement));
_GLFWjoyelement* element = (_GLFWjoyelement*) malloc(sizeof(_GLFWjoyelement));
CFArrayAppendValue(elementsArray, element);
@ -186,12 +159,12 @@ static void addJoystickElement(_glfwJoystick* joystick, CFTypeRef refElement)
static void getElementsCFArrayHandler(const void* value, void* parameter)
{
if (CFGetTypeID(value) == CFDictionaryGetTypeID())
addJoystickElement((_glfwJoystick*) parameter, (CFTypeRef) value);
addJoystickElement((_GLFWjoy*) parameter, (CFTypeRef) value);
}
// Returns the value of the specified element of the specified joystick
//
static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* element)
static long getElementValue(_GLFWjoy* joystick, _GLFWjoyelement* element)
{
IOReturn result = kIOReturnSuccess;
IOHIDEventStruct hidEvent;
@ -218,7 +191,7 @@ static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* eleme
// Removes the specified joystick
//
static void removeJoystick(_glfwJoystick* joystick)
static void removeJoystick(_GLFWjoy* joystick)
{
int i;
@ -228,8 +201,8 @@ static void removeJoystick(_glfwJoystick* joystick)
for (i = 0; i < joystick->numAxes; i++)
{
_glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i);
_GLFWjoyelement* axes =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->axes, i);
free(axes);
}
CFArrayRemoveAllValues(joystick->axes);
@ -237,8 +210,8 @@ static void removeJoystick(_glfwJoystick* joystick)
for (i = 0; i < joystick->numButtons; i++)
{
_glfwJoystickElement* button =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i);
_GLFWjoyelement* button =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->buttons, i);
free(button);
}
CFArrayRemoveAllValues(joystick->buttons);
@ -246,8 +219,8 @@ static void removeJoystick(_glfwJoystick* joystick)
for (i = 0; i < joystick->numHats; i++)
{
_glfwJoystickElement* hat =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, i);
_GLFWjoyelement* hat =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->hats, i);
free(hat);
}
CFArrayRemoveAllValues(joystick->hats);
@ -264,7 +237,7 @@ static void removeJoystick(_glfwJoystick* joystick)
//
static void removalCallback(void* target, IOReturn result, void* refcon, void* sender)
{
removeJoystick((_glfwJoystick*) refcon);
removeJoystick((_GLFWjoy*) refcon);
}
// Polls for joystick events and updates GLFW state
@ -276,28 +249,28 @@ static void pollJoystickEvents(void)
for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
{
_glfwJoystick* joystick = &_glfwJoysticks[i];
_GLFWjoy* joystick = &_glfw.ns.joysticks[i];
if (joystick->present)
{
for (j = 0; j < joystick->numButtons; j++)
{
_glfwJoystickElement* button =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, j);
_GLFWjoyelement* button =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->buttons, j);
button->value = getElementValue(joystick, button);
}
for (j = 0; j < joystick->numAxes; j++)
{
_glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, j);
_GLFWjoyelement* axes =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->axes, j);
axes->value = getElementValue(joystick, axes);
}
for (j = 0; j < joystick->numHats; j++)
{
_glfwJoystickElement* hat =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, j);
_GLFWjoyelement* hat =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick->hats, j);
hat->value = getElementValue(joystick, hat);
}
}
@ -320,8 +293,6 @@ void _glfwInitJoysticks(void)
CFMutableDictionaryRef hidMatchDictionary = NULL;
io_object_t ioHIDDeviceObject = 0;
memset(&_glfwJoysticks, 0, sizeof(_glfwJoysticks));
result = IOMasterPort(bootstrap_port, &masterPort);
hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
if (kIOReturnSuccess != result || !hidMatchDictionary)
@ -390,7 +361,7 @@ void _glfwInitJoysticks(void)
}
}
_glfwJoystick* joystick = &_glfwJoysticks[deviceCounter];
_GLFWjoy* joystick = &_glfw.ns.joysticks[deviceCounter];
joystick->present = GL_TRUE;
@ -460,7 +431,7 @@ void _glfwTerminateJoysticks(void)
for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
{
_glfwJoystick* joystick = &_glfwJoysticks[i];
_GLFWjoy* joystick = &_glfw.ns.joysticks[i];
removeJoystick(joystick);
}
}
@ -472,7 +443,7 @@ void _glfwTerminateJoysticks(void)
int _glfwPlatformGetJoystickParam(int joy, int param)
{
if (!_glfwJoysticks[joy].present)
if (!_glfw.ns.joysticks[joy].present)
{
// TODO: Figure out if this is an error
return GL_FALSE;
@ -484,11 +455,11 @@ int _glfwPlatformGetJoystickParam(int joy, int param)
return GL_TRUE;
case GLFW_AXES:
return (int) CFArrayGetCount(_glfwJoysticks[joy].axes);
return (int) CFArrayGetCount(_glfw.ns.joysticks[joy].axes);
case GLFW_BUTTONS:
return (int) CFArrayGetCount(_glfwJoysticks[joy].buttons) +
(int) CFArrayGetCount(_glfwJoysticks[joy].hats) * 4;
return (int) CFArrayGetCount(_glfw.ns.joysticks[joy].buttons) +
(int) CFArrayGetCount(_glfw.ns.joysticks[joy].hats) * 4;
default:
break;
@ -504,7 +475,7 @@ int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes)
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0;
_glfwJoystick joystick = _glfwJoysticks[joy];
_GLFWjoy joystick = _glfw.ns.joysticks[joy];
if (!joystick.present)
{
@ -519,8 +490,8 @@ int _glfwPlatformGetJoystickAxes(int joy, float* axes, int numaxes)
for (i = 0; i < numaxes; i++)
{
_glfwJoystickElement* elements =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.axes, i);
_GLFWjoyelement* elements =
(_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick.axes, i);
long readScale = elements->maxReport - elements->minReport;
@ -544,7 +515,7 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0;
_glfwJoystick joystick = _glfwJoysticks[joy];
_GLFWjoy joystick = _glfw.ns.joysticks[joy];
if (!joystick.present)
{
@ -557,7 +528,7 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
for (button = 0; button < numbuttons && button < joystick.numButtons; button++)
{
_glfwJoystickElement* element = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.buttons, button);
_GLFWjoyelement* element = (_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick.buttons, button);
buttons[button] = element->value ? GLFW_PRESS : GLFW_RELEASE;
}
@ -569,7 +540,7 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
for (i = 0; i < joystick.numHats; i++)
{
_glfwJoystickElement* hat = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.hats, i);
_GLFWjoyelement* hat = (_GLFWjoyelement*) CFArrayGetValueAtIndex(joystick.hats, i);
int value = hat->value;
if (value < 0 || value > 8)
@ -591,6 +562,6 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
const char* _glfwPlatformGetJoystickName(int joy)
{
return _glfwJoysticks[joy].name;
return _glfw.ns.joysticks[joy].name;
}

View File

@ -46,6 +46,11 @@ typedef void* id;
#error "No supported context creation API selected"
#endif
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNS ns
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryNS ns
#define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorNS ns
@ -73,6 +78,27 @@ typedef struct _GLFWwindowNS
} _GLFWwindowNS;
//------------------------------------------------------------------------
// Joystick information & state
//------------------------------------------------------------------------
typedef struct
{
int present;
char name[256];
IOHIDDeviceInterface** interface;
int numAxes;
int numButtons;
int numHats;
CFMutableArrayRef axes;
CFMutableArrayRef buttons;
CFMutableArrayRef hats;
} _GLFWjoy;
//------------------------------------------------------------------------
// Platform-specific library global data for Cocoa
//------------------------------------------------------------------------
@ -88,6 +114,8 @@ typedef struct _GLFWlibraryNS
id autoreleasePool;
char* clipboardString;
_GLFWjoy joysticks[GLFW_JOYSTICK_LAST + 1];
} _GLFWlibraryNS;