mirror of
https://github.com/glfw/glfw.git
synced 2024-11-22 21:14:35 +00:00
Formatting.
This commit is contained in:
parent
2935652f48
commit
e05f0c0f53
@ -47,7 +47,8 @@
|
||||
// Joystick element information
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
IOHIDElementCookie Cookie;
|
||||
|
||||
long Value;
|
||||
@ -57,14 +58,16 @@ typedef struct {
|
||||
|
||||
long MinReport;
|
||||
long MaxReport;
|
||||
} joystick_element_t;
|
||||
|
||||
} _glfwJoystickElement;
|
||||
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// Joystick information & state
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
int Present;
|
||||
char Product[256];
|
||||
|
||||
@ -77,14 +80,20 @@ typedef struct {
|
||||
CFMutableArrayRef Axes;
|
||||
CFMutableArrayRef Buttons;
|
||||
CFMutableArrayRef Hats;
|
||||
} joystick_t;
|
||||
|
||||
joystick_t _glfwJoysticks[GLFW_JOYSTICK_LAST + 1];
|
||||
} _glfwJoystick;
|
||||
|
||||
_glfwJoystick _glfwJoysticks[GLFW_JOYSTICK_LAST + 1];
|
||||
|
||||
|
||||
void GetElementsCFArrayHandler(const void* value, void* parameter);
|
||||
|
||||
static void JoystickAddElemet(joystick_t * joystick, CFTypeRef refElement)
|
||||
|
||||
//========================================================================
|
||||
// Adds an element to the specified joystick
|
||||
//========================================================================
|
||||
|
||||
static void addJoystickElement(_glfwJoystick* joystick, CFTypeRef refElement)
|
||||
{
|
||||
long elementType, usagePage, usage;
|
||||
CFTypeRef refElementType, refUsagePage, refUsage;
|
||||
@ -106,6 +115,7 @@ static void JoystickAddElemet(joystick_t * joystick, CFTypeRef refElement)
|
||||
switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
|
||||
{
|
||||
case kHIDPage_GenericDesktop:
|
||||
{
|
||||
switch (usage)
|
||||
{
|
||||
case kHIDUsage_GD_X:
|
||||
@ -125,7 +135,10 @@ static void JoystickAddElemet(joystick_t * joystick, CFTypeRef refElement)
|
||||
elementsArray = joystick->Hats;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case kHIDPage_Button:
|
||||
joystick->NumButtons++;
|
||||
elementsArray = joystick->Buttons;
|
||||
@ -139,7 +152,7 @@ static void JoystickAddElemet(joystick_t * joystick, CFTypeRef refElement)
|
||||
long number;
|
||||
CFTypeRef refType;
|
||||
|
||||
joystick_element_t * element = (joystick_element_t *) malloc(sizeof(joystick_element_t));
|
||||
_glfwJoystickElement* element = (_glfwJoystickElement*) malloc(sizeof(_glfwJoystickElement));
|
||||
|
||||
CFArrayAppendValue(elementsArray, element);
|
||||
|
||||
@ -167,25 +180,33 @@ static void JoystickAddElemet(joystick_t * joystick, CFTypeRef refElement)
|
||||
CFRange range = {0, CFArrayGetCount (refElementTop)};
|
||||
CFArrayApplyFunction(refElementTop, range, GetElementsCFArrayHandler, joystick);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//========================================================================
|
||||
// Adds an element to the specified joystick
|
||||
//========================================================================
|
||||
|
||||
void GetElementsCFArrayHandler(const void* value, void* parameter)
|
||||
{
|
||||
if (CFGetTypeID(value) == CFDictionaryGetTypeID())
|
||||
JoystickAddElemet((joystick_t *) parameter, (CFTypeRef) value);
|
||||
addJoystickElement((_glfwJoystick*) parameter, (CFTypeRef) value);
|
||||
}
|
||||
|
||||
|
||||
static long GetElementValue(joystick_t * joystick, joystick_element_t * element)
|
||||
//========================================================================
|
||||
// Returns the value of the specified element of the specified joystick
|
||||
//========================================================================
|
||||
|
||||
static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* element)
|
||||
{
|
||||
IOReturn result = kIOReturnSuccess;
|
||||
IOHIDEventStruct hidEvent;
|
||||
hidEvent.value = 0;
|
||||
|
||||
if (NULL != joystick && NULL != element && NULL != joystick->Interface)
|
||||
if (joystick && element && joystick->Interface)
|
||||
{
|
||||
result = (*(joystick->Interface))->getElementValue(joystick->Interface, element->Cookie, &hidEvent);
|
||||
if (kIOReturnSuccess == result)
|
||||
@ -203,7 +224,11 @@ static long GetElementValue(joystick_t * joystick, joystick_element_t * element)
|
||||
}
|
||||
|
||||
|
||||
static void RemoveJoystick(joystick_t * joystick)
|
||||
//========================================================================
|
||||
// Removes the specified joystick
|
||||
//========================================================================
|
||||
|
||||
static void removeJoystick(_glfwJoystick* joystick)
|
||||
{
|
||||
if (joystick->Present)
|
||||
{
|
||||
@ -211,8 +236,8 @@ static void RemoveJoystick(joystick_t * joystick)
|
||||
|
||||
for (int i = 0; i < joystick->NumAxes; i++)
|
||||
{
|
||||
joystick_element_t * axes =
|
||||
(joystick_element_t *) CFArrayGetValueAtIndex(joystick->Axes, i);
|
||||
_glfwJoystickElement* axes =
|
||||
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Axes, i);
|
||||
free(axes);
|
||||
}
|
||||
CFArrayRemoveAllValues(joystick->Axes);
|
||||
@ -220,8 +245,8 @@ static void RemoveJoystick(joystick_t * joystick)
|
||||
|
||||
for (int i = 0; i < joystick->NumButtons; i++)
|
||||
{
|
||||
joystick_element_t * button =
|
||||
(joystick_element_t *) CFArrayGetValueAtIndex(joystick->Buttons, i);
|
||||
_glfwJoystickElement* button =
|
||||
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Buttons, i);
|
||||
free(button);
|
||||
}
|
||||
CFArrayRemoveAllValues(joystick->Buttons);
|
||||
@ -229,8 +254,8 @@ static void RemoveJoystick(joystick_t * joystick)
|
||||
|
||||
for (int i = 0; i < joystick->NumHats; i++)
|
||||
{
|
||||
joystick_element_t * hat =
|
||||
(joystick_element_t *) CFArrayGetValueAtIndex(joystick->Hats, i);
|
||||
_glfwJoystickElement* hat =
|
||||
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Hats, i);
|
||||
free(hat);
|
||||
}
|
||||
CFArrayRemoveAllValues(joystick->Hats);
|
||||
@ -244,32 +269,40 @@ static void RemoveJoystick(joystick_t * joystick)
|
||||
}
|
||||
|
||||
|
||||
static void RemovalCallback(void * target, IOReturn result, void * refcon, void * sender)
|
||||
//========================================================================
|
||||
// Callback for user-initiated joystick removal
|
||||
//========================================================================
|
||||
|
||||
static void removalCallback(void* target, IOReturn result, void* refcon, void* sender)
|
||||
{
|
||||
RemoveJoystick((joystick_t *) refcon);
|
||||
removeJoystick((_glfwJoystick*) refcon);
|
||||
}
|
||||
|
||||
|
||||
static void PollJoystickEvents(void)
|
||||
//========================================================================
|
||||
// Polls for joystick events and updates GFLW state
|
||||
//========================================================================
|
||||
|
||||
static void pollJoystickEvents(void)
|
||||
{
|
||||
for (int i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
|
||||
{
|
||||
joystick_t * joystick = &_glfwJoysticks[i];
|
||||
_glfwJoystick* joystick = &_glfwJoysticks[i];
|
||||
|
||||
if (joystick->Present)
|
||||
{
|
||||
for (CFIndex i = 0; i < joystick->NumButtons; i++)
|
||||
{
|
||||
joystick_element_t * button =
|
||||
(joystick_element_t *) CFArrayGetValueAtIndex(joystick->Buttons, i);
|
||||
button->Value = GetElementValue(joystick, button);
|
||||
_glfwJoystickElement* button =
|
||||
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Buttons, i);
|
||||
button->Value = getElementValue(joystick, button);
|
||||
}
|
||||
|
||||
for (CFIndex i = 0; i < joystick->NumAxes; i++)
|
||||
{
|
||||
joystick_element_t * axes =
|
||||
(joystick_element_t *) CFArrayGetValueAtIndex(joystick->Axes, i);
|
||||
axes->Value = GetElementValue(joystick, axes);
|
||||
_glfwJoystickElement* axes =
|
||||
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->Axes, i);
|
||||
axes->Value = getElementValue(joystick, axes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -317,14 +350,13 @@ void _glfwInitJoysticks(void)
|
||||
|
||||
long usagePage, usage;
|
||||
|
||||
|
||||
result = IORegistryEntryCreateCFProperties(
|
||||
ioHIDDeviceObject,
|
||||
result = IORegistryEntryCreateCFProperties(ioHIDDeviceObject,
|
||||
&hidProperties,
|
||||
kCFAllocatorDefault,
|
||||
kNilOptions);
|
||||
|
||||
if (result != kIOReturnSuccess) continue;
|
||||
if (result != kIOReturnSuccess)
|
||||
continue;
|
||||
|
||||
/* Check device type */
|
||||
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
|
||||
@ -344,14 +376,11 @@ void _glfwInitJoysticks(void)
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
joystick_t * joystick = &_glfwJoysticks[deviceCounter];
|
||||
_glfwJoystick* joystick = &_glfwJoysticks[deviceCounter];
|
||||
|
||||
joystick->Present = GL_TRUE;
|
||||
|
||||
|
||||
result = IOCreatePlugInInterfaceForService(
|
||||
ioHIDDeviceObject,
|
||||
result = IOCreatePlugInInterfaceForService(ioHIDDeviceObject,
|
||||
kIOHIDDeviceUserClientTypeID,
|
||||
kIOCFPlugInInterfaceID,
|
||||
&ppPlugInInterface,
|
||||
@ -365,18 +394,26 @@ void _glfwInitJoysticks(void)
|
||||
CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
|
||||
(void *) &(joystick->Interface));
|
||||
|
||||
if (S_OK != plugInResult)
|
||||
if (plugInResult != S_OK)
|
||||
exit(EXIT_FAILURE);
|
||||
|
||||
(*ppPlugInInterface)->Release(ppPlugInInterface);
|
||||
|
||||
(*(joystick->Interface))->open(joystick->Interface, 0);
|
||||
(*(joystick->Interface))->setRemovalCallback(joystick->Interface, RemovalCallback, joystick, joystick);
|
||||
(*(joystick->Interface))->setRemovalCallback(joystick->Interface,
|
||||
removalCallback,
|
||||
joystick,
|
||||
joystick);
|
||||
|
||||
/* Get product string */
|
||||
refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
|
||||
if (refCF)
|
||||
CFStringGetCString(refCF, (char *) &(joystick->Product), 256, CFStringGetSystemEncoding());
|
||||
{
|
||||
CFStringGetCString(refCF,
|
||||
(char*) &(joystick->Product),
|
||||
256,
|
||||
CFStringGetSystemEncoding());
|
||||
}
|
||||
|
||||
joystick->NumAxes = 0;
|
||||
joystick->NumButtons = 0;
|
||||
@ -385,12 +422,16 @@ void _glfwInitJoysticks(void)
|
||||
joystick->Buttons = CFArrayCreateMutable(NULL, 0, NULL);
|
||||
joystick->Hats = CFArrayCreateMutable(NULL, 0, NULL);
|
||||
|
||||
CFTypeRef refTopElement = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDElementKey));
|
||||
CFTypeRef refTopElement = CFDictionaryGetValue(hidProperties,
|
||||
CFSTR(kIOHIDElementKey));
|
||||
CFTypeID type = CFGetTypeID(refTopElement);
|
||||
if (type == CFArrayGetTypeID())
|
||||
{
|
||||
CFRange range = { 0, CFArrayGetCount(refTopElement) };
|
||||
CFArrayApplyFunction(refTopElement, range, GetElementsCFArrayHandler, (void *) joystick);
|
||||
CFArrayApplyFunction(refTopElement,
|
||||
range,
|
||||
GetElementsCFArrayHandler,
|
||||
(void*) joystick);
|
||||
}
|
||||
|
||||
deviceCounter++;
|
||||
@ -406,8 +447,8 @@ void _glfwTerminateJoysticks(void)
|
||||
{
|
||||
for (int i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
|
||||
{
|
||||
joystick_t * joystick = &_glfwJoysticks[i];
|
||||
RemoveJoystick(joystick);
|
||||
_glfwJoystick* joystick = &_glfwJoysticks[i];
|
||||
removeJoystick(joystick);
|
||||
}
|
||||
}
|
||||
|
||||
@ -456,7 +497,7 @@ int _glfwPlatformGetJoystickPos(int joy, float *pos, int numaxes)
|
||||
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
|
||||
return 0;
|
||||
|
||||
joystick_t joystick = _glfwJoysticks[joy];
|
||||
_glfwJoystick joystick = _glfwJoysticks[joy];
|
||||
|
||||
if (!joystick.Present)
|
||||
{
|
||||
@ -467,12 +508,12 @@ int _glfwPlatformGetJoystickPos(int joy, float *pos, int numaxes)
|
||||
numaxes = numaxes < joystick.NumAxes ? numaxes : joystick.NumAxes;
|
||||
|
||||
// Update joystick state
|
||||
PollJoystickEvents();
|
||||
pollJoystickEvents();
|
||||
|
||||
for (int i = 0; i < numaxes; i++)
|
||||
{
|
||||
joystick_element_t * axes =
|
||||
(joystick_element_t *) CFArrayGetValueAtIndex(joystick.Axes, i);
|
||||
_glfwJoystickElement* axes =
|
||||
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.Axes, i);
|
||||
|
||||
long readScale = axes->MaxReport - axes->MinReport;
|
||||
|
||||
@ -501,7 +542,7 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char *buttons,
|
||||
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
|
||||
return 0;
|
||||
|
||||
joystick_t joystick = _glfwJoysticks[joy];
|
||||
_glfwJoystick joystick = _glfwJoysticks[joy];
|
||||
|
||||
if (!joystick.Present)
|
||||
{
|
||||
@ -512,12 +553,11 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char *buttons,
|
||||
numbuttons = numbuttons < joystick.NumButtons ? numbuttons : joystick.NumButtons;
|
||||
|
||||
// Update joystick state
|
||||
PollJoystickEvents();
|
||||
|
||||
pollJoystickEvents();
|
||||
|
||||
for (int i = 0; i < numbuttons; i++)
|
||||
{
|
||||
joystick_element_t * button = (joystick_element_t *) CFArrayGetValueAtIndex(joystick.Buttons, i);
|
||||
_glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.Buttons, i);
|
||||
buttons[i] = button->Value ? GLFW_PRESS : GLFW_RELEASE;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user