1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 02:38:52 -05:00

Separated joystick state from window system state.

This is partially in preparation for pending support for additional
joystick APIs like XInput, DirectInput and IOHID.
This commit is contained in:
Camilla Berglund 2014-03-19 15:27:49 +01:00
parent 6acbfe863d
commit 660034332d
13 changed files with 214 additions and 114 deletions

View File

@ -12,15 +12,17 @@ set(common_SOURCES clipboard.c context.c gamma.c init.c input.c joystick.c
monitor.c time.c window.c)
if (_GLFW_COCOA)
set(glfw_HEADERS ${common_HEADERS} cocoa_platform.h posix_tls.h)
set(glfw_HEADERS ${common_HEADERS} cocoa_platform.h iokit_joystick.h
posix_tls.h)
set(glfw_SOURCES ${common_SOURCES} cocoa_clipboard.m cocoa_gamma.c
cocoa_init.m cocoa_joystick.m cocoa_monitor.m cocoa_time.c
cocoa_window.m posix_tls.c)
cocoa_init.m cocoa_monitor.m cocoa_time.c cocoa_window.m
iokit_joystick.m posix_tls.c)
elseif (_GLFW_WIN32)
set(glfw_HEADERS ${common_HEADERS} win32_platform.h win32_tls.h)
set(glfw_HEADERS ${common_HEADERS} win32_platform.h win32_tls.h
winmm_joystick.h)
set(glfw_SOURCES ${common_SOURCES} win32_clipboard.c win32_gamma.c
win32_init.c win32_joystick.c win32_monitor.c win32_time.c
win32_tls.c win32_window.c)
win32_init.c win32_monitor.c win32_time.c win32_tls.c
win32_window.c winmm_joystick.c)
elseif (_GLFW_X11)
set(glfw_HEADERS ${common_HEADERS} x11_platform.h posix_tls.h)
set(glfw_SOURCES ${common_SOURCES} x11_clipboard.c x11_gamma.c x11_init.c

View File

@ -45,10 +45,7 @@ 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>
#include "iokit_joystick.h"
#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNS ns
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryNS ns
@ -74,26 +71,6 @@ typedef struct _GLFWwindowNS
} _GLFWwindowNS;
//------------------------------------------------------------------------
// Joystick information & state
//------------------------------------------------------------------------
typedef struct
{
int present;
char name[256];
IOHIDDeviceInterface** interface;
CFMutableArrayRef axisElements;
CFMutableArrayRef buttonElements;
CFMutableArrayRef hatElements;
float* axes;
unsigned char* buttons;
} _GLFWjoy;
//------------------------------------------------------------------------
// Platform-specific library global data for Cocoa
//------------------------------------------------------------------------
@ -111,7 +88,6 @@ typedef struct _GLFWlibraryNS
char* clipboardString;
_GLFWjoy joysticks[GLFW_JOYSTICK_LAST + 1];
} _GLFWlibraryNS;
@ -143,10 +119,6 @@ typedef struct _GLFWcursorNS
// Time
void _glfwInitTimer(void);
// Joystick input
void _glfwInitJoysticks(void);
void _glfwTerminateJoysticks(void);
// Fullscreen
GLboolean _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* desired);
void _glfwRestoreVideoMode(_GLFWmonitor* monitor);

View File

@ -349,6 +349,8 @@ struct _GLFWlibrary
_GLFW_PLATFORM_LIBRARY_WINDOW_STATE;
// This is defined in the context API's platform.h
_GLFW_PLATFORM_LIBRARY_OPENGL_STATE;
// This is defined in the platform's joystick.h
_GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE;
// This is defined in the platform's tls.h
_GLFW_PLATFORM_TLS_STATE;
};

69
src/iokit_joystick.h Normal file
View File

@ -0,0 +1,69 @@
//========================================================================
// GLFW 3.1 IOKit - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2006-2014 Camilla Berglund <elmindreda@elmindreda.org>
//
// 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.
//
//========================================================================
#ifndef _iokit_joystick_h_
#define _iokit_joystick_h_
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \
_GLFWjoystickIOKit joystick[GLFW_JOYSTICK_LAST + 1]
//========================================================================
// GLFW platform specific types
//========================================================================
//------------------------------------------------------------------------
// Platform-specific joystick structure
//------------------------------------------------------------------------
typedef struct _GLFWjoystickIOKit
{
int present;
char name[256];
IOHIDDeviceInterface** interface;
CFMutableArrayRef axisElements;
CFMutableArrayRef buttonElements;
CFMutableArrayRef hatElements;
float* axes;
unsigned char* buttons;
} _GLFWjoystickIOKit;
//========================================================================
// Prototypes for platform specific internal functions
//========================================================================
void _glfwInitJoysticks(void);
void _glfwTerminateJoysticks(void);
#endif // _iokit_joystick_h_

View File

@ -1,5 +1,5 @@
//========================================================================
// GLFW 3.1 OS X - www.glfw.org
// GLFW 3.1 IOKit - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2009-2010 Camilla Berglund <elmindreda@elmindreda.org>
// Copyright (c) 2012 Torsten Walluhn <tw@mad-cad.net>
@ -58,7 +58,7 @@ static void getElementsCFArrayHandler(const void* value, void* parameter);
// Adds an element to the specified joystick
//
static void addJoystickElement(_GLFWjoy* joystick, CFTypeRef elementRef)
static void addJoystickElement(_GLFWjoystickIOKit* joystick, CFTypeRef elementRef)
{
long elementType, usagePage, usage;
CFMutableArrayRef elementsArray = NULL;
@ -146,12 +146,12 @@ static void addJoystickElement(_GLFWjoy* joystick, CFTypeRef elementRef)
static void getElementsCFArrayHandler(const void* value, void* parameter)
{
if (CFGetTypeID(value) == CFDictionaryGetTypeID())
addJoystickElement((_GLFWjoy*) parameter, (CFTypeRef) value);
addJoystickElement((_GLFWjoystickIOKit*) parameter, (CFTypeRef) value);
}
// Returns the value of the specified element of the specified joystick
//
static long getElementValue(_GLFWjoy* joystick, _GLFWjoyelement* element)
static long getElementValue(_GLFWjoystickIOKit* joystick, _GLFWjoyelement* element)
{
IOReturn result = kIOReturnSuccess;
IOHIDEventStruct hidEvent;
@ -178,7 +178,7 @@ static long getElementValue(_GLFWjoy* joystick, _GLFWjoyelement* element)
// Removes the specified joystick
//
static void removeJoystick(_GLFWjoy* joystick)
static void removeJoystick(_GLFWjoystickIOKit* joystick)
{
int i;
@ -203,14 +203,14 @@ static void removeJoystick(_GLFWjoy* joystick)
(*(joystick->interface))->close(joystick->interface);
(*(joystick->interface))->Release(joystick->interface);
memset(joystick, 0, sizeof(_GLFWjoy));
memset(joystick, 0, sizeof(_GLFWjoystickIOKit));
}
// Callback for user-initiated joystick removal
//
static void removalCallback(void* target, IOReturn result, void* refcon, void* sender)
{
removeJoystick((_GLFWjoy*) refcon);
removeJoystick((_GLFWjoystickIOKit*) refcon);
}
// Polls for joystick events and updates GLFW state
@ -223,7 +223,7 @@ static void pollJoystickEvents(void)
{
CFIndex i;
int buttonIndex = 0;
_GLFWjoy* joystick = _glfw.ns.joysticks + joy;
_GLFWjoystickIOKit* joystick = _glfw.joystick + joy;
if (!joystick->present)
continue;
@ -368,7 +368,7 @@ void _glfwInitJoysticks(void)
CFRelease(valueRef);
}
_GLFWjoy* joystick = _glfw.ns.joysticks + joy;
_GLFWjoystickIOKit* joystick = _glfw.joystick + joy;
joystick->present = GL_TRUE;
result = IOCreatePlugInInterfaceForService(ioHIDDeviceObject,
@ -450,7 +450,7 @@ void _glfwTerminateJoysticks(void)
for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++)
{
_GLFWjoy* joystick = &_glfw.ns.joysticks[i];
_GLFWjoystickIOKit* joystick = &_glfw.joystick[i];
removeJoystick(joystick);
}
}
@ -464,12 +464,12 @@ int _glfwPlatformJoystickPresent(int joy)
{
pollJoystickEvents();
return _glfw.ns.joysticks[joy].present;
return _glfw.joystick[joy].present;
}
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{
_GLFWjoy* joystick = _glfw.ns.joysticks + joy;
_GLFWjoystickIOKit* joystick = _glfw.joystick + joy;
pollJoystickEvents();
@ -482,7 +482,7 @@ const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{
_GLFWjoy* joystick = _glfw.ns.joysticks + joy;
_GLFWjoystickIOKit* joystick = _glfw.joystick + joy;
pollJoystickEvents();
@ -498,6 +498,6 @@ const char* _glfwPlatformGetJoystickName(int joy)
{
pollJoystickEvents();
return _glfw.ns.joysticks[joy].name;
return _glfw.joystick[joy].name;
}

View File

@ -56,7 +56,7 @@ static int openJoystickDevice(int joy, const char* path)
if (fd == -1)
return GL_FALSE;
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].fd = fd;
_glfw.joystick[joy].fd = fd;
// Verify that the joystick driver version is at least 1.0
ioctl(fd, JSIOCGVERSION, &version);
@ -70,20 +70,18 @@ static int openJoystickDevice(int joy, const char* path)
if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
strncpy(name, "Unknown", sizeof(name));
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].name = strdup(name);
_glfw.joystick[joy].name = strdup(name);
ioctl(fd, JSIOCGAXES, &axisCount);
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axisCount = (int) axisCount;
_glfw.joystick[joy].axisCount = (int) axisCount;
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttonCount = (int) buttonCount;
_glfw.joystick[joy].buttonCount = (int) buttonCount;
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axes =
calloc(axisCount, sizeof(float));
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttons =
calloc(buttonCount, 1);
_glfw.joystick[joy].axes = calloc(axisCount, sizeof(float));
_glfw.joystick[joy].buttons = calloc(buttonCount, 1);
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present = GL_TRUE;
_glfw.joystick[joy].present = GL_TRUE;
#endif // __linux__
return GL_TRUE;
@ -100,23 +98,21 @@ static void pollJoystickEvents(void)
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
{
if (!_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present)
if (!_glfw.joystick[i].present)
continue;
// Read all queued events (non-blocking)
for (;;)
{
errno = 0;
result = read(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].fd,
&e,
sizeof(e));
result = read(_glfw.joystick[i].fd, &e, sizeof(e));
if (errno == ENODEV)
{
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].axes);
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].buttons);
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].name);
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present = GL_FALSE;
free(_glfw.joystick[i].axes);
free(_glfw.joystick[i].buttons);
free(_glfw.joystick[i].name);
_glfw.joystick[i].present = GL_FALSE;
}
if (result == -1)
@ -128,12 +124,12 @@ static void pollJoystickEvents(void)
switch (e.type)
{
case JS_EVENT_AXIS:
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].axes[e.number] =
_glfw.joystick[i].axes[e.number] =
(float) e.value / 32767.0f;
break;
case JS_EVENT_BUTTON:
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].buttons[e.number] =
_glfw.joystick[i].buttons[e.number] =
e.value ? GLFW_PRESS : GLFW_RELEASE;
break;
@ -208,14 +204,14 @@ void _glfwTerminateJoysticks(void)
for (i = 0; i <= GLFW_JOYSTICK_LAST; i++)
{
if (_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present)
if (_glfw.joystick[i].present)
{
close(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].fd);
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].axes);
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].buttons);
free(_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].name);
close(_glfw.joystick[i].fd);
free(_glfw.joystick[i].axes);
free(_glfw.joystick[i].buttons);
free(_glfw.joystick[i].name);
_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[i].present = GL_FALSE;
_glfw.joystick[i].present = GL_FALSE;
}
}
#endif // __linux__
@ -230,35 +226,35 @@ int _glfwPlatformJoystickPresent(int joy)
{
pollJoystickEvents();
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present;
return _glfw.joystick[joy].present;
}
const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{
pollJoystickEvents();
if (!_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present)
if (!_glfw.joystick[joy].present)
return NULL;
*count = _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axisCount;
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].axes;
*count = _glfw.joystick[joy].axisCount;
return _glfw.joystick[joy].axes;
}
const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{
pollJoystickEvents();
if (!_GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].present)
if (!_glfw.joystick[joy].present)
return NULL;
*count = _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttonCount;
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].buttons;
*count = _glfw.joystick[joy].buttonCount;
return _glfw.joystick[joy].buttons;
}
const char* _glfwPlatformGetJoystickName(int joy)
{
pollJoystickEvents();
return _GLFW_LINUX_JOYSTICK_CONTEXT.joystick[joy].name;
return _glfw.joystick[joy].name;
}

View File

@ -27,19 +27,33 @@
#ifndef _linux_joystick_h_
#define _linux_joystick_h_
typedef struct _GLFWjoystickLinux {
struct {
int present;
int fd;
float* axes;
int axisCount;
unsigned char* buttons;
int buttonCount;
char* name;
} joystick[GLFW_JOYSTICK_LAST + 1];
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \
_GLFWjoystickLinux joystick[GLFW_JOYSTICK_LAST + 1]
//========================================================================
// GLFW platform specific types
//========================================================================
//------------------------------------------------------------------------
// Platform-specific joystick structure
//------------------------------------------------------------------------
typedef struct _GLFWjoystickLinux
{
int present;
int fd;
float* axes;
int axisCount;
unsigned char* buttons;
int buttonCount;
char* name;
} _GLFWjoystickLinux;
// Joystick input
//========================================================================
// Prototypes for platform specific internal functions
//========================================================================
void _glfwInitJoysticks(void);
void _glfwTerminateJoysticks(void);

View File

@ -94,3 +94,4 @@ void _glfwPlatformSetTime(double time)
_GLFW_UNIX_TIME_CONTEXT.base = getRawTime() -
(uint64_t) (time / _GLFW_UNIX_TIME_CONTEXT.resolution);
}

View File

@ -36,7 +36,6 @@
#error "The Wayland backend depends on EGL platform support"
#endif
#define _GLFW_LINUX_JOYSTICK_CONTEXT _glfw.wayland.joystick
#include "linux_joystick.h"
#define _GLFW_UNIX_TIME_CONTEXT _glfw.wayland.timer
@ -76,7 +75,6 @@ typedef struct _GLFWlibraryWayland
int monitorsSize;
_GLFWtimeUNIX timer;
_GLFWjoystickLinux joystick;
} _GLFWlibraryWayland;
typedef struct _GLFWmonitorWayland

View File

@ -163,6 +163,8 @@ typedef HRESULT (WINAPI * DWMISCOMPOSITIONENABLED_T)(BOOL*);
#error "No supported context creation API selected"
#endif
#include "winmm_joystick.h"
#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowWin32 win32
#define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryWin32 win32
#define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorWin32 win32
@ -232,11 +234,6 @@ typedef struct _GLFWlibraryWin32
DWMISCOMPOSITIONENABLED_T DwmIsCompositionEnabled;
} dwmapi;
struct {
float axes[6];
unsigned char buttons[36]; // 32 buttons plus one hat
char* name;
} joystick[GLFW_JOYSTICK_LAST + 1];
} _GLFWlibraryWin32;
@ -276,10 +273,6 @@ char* _glfwCreateUTF8FromWideString(const WCHAR* source);
// Time
void _glfwInitTimer(void);
// Joystick input
void _glfwInitJoysticks(void);
void _glfwTerminateJoysticks(void);
// Fullscreen support
GLboolean _glfwSetVideoMode(_GLFWmonitor* monitor, const GLFWvidmode* desired);
void _glfwRestoreVideoMode(_GLFWmonitor* monitor);

View File

@ -1,5 +1,5 @@
//========================================================================
// GLFW 3.1 Win32 - www.glfw.org
// GLFW 3.1 WinMM - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2002-2006 Marcus Geelnard
// Copyright (c) 2006-2010 Camilla Berglund <elmindreda@elmindreda.org>
@ -63,7 +63,7 @@ void _glfwTerminateJoysticks(void)
int i;
for (i = 0; i < GLFW_JOYSTICK_LAST; i++)
free(_glfw.win32.joystick[i].name);
free(_glfw.joystick[i].name);
}
@ -85,7 +85,7 @@ const float* _glfwPlatformGetJoystickAxes(int joy, int* count)
{
JOYCAPS jc;
JOYINFOEX ji;
float* axes = _glfw.win32.joystick[joy].axes;
float* axes = _glfw.joystick[joy].axes;
if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
return NULL;
@ -118,7 +118,7 @@ const unsigned char* _glfwPlatformGetJoystickButtons(int joy, int* count)
{
JOYCAPS jc;
JOYINFOEX ji;
unsigned char* buttons = _glfw.win32.joystick[joy].buttons;
unsigned char* buttons = _glfw.joystick[joy].buttons;
if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
return NULL;
@ -169,9 +169,9 @@ const char* _glfwPlatformGetJoystickName(int joy)
if (_glfw_joyGetDevCaps(joy, &jc, sizeof(JOYCAPS)) != JOYERR_NOERROR)
return NULL;
free(_glfw.win32.joystick[joy].name);
_glfw.win32.joystick[joy].name = _glfwCreateUTF8FromWideString(jc.szPname);
free(_glfw.joystick[joy].name);
_glfw.joystick[joy].name = _glfwCreateUTF8FromWideString(jc.szPname);
return _glfw.win32.joystick[joy].name;
return _glfw.joystick[joy].name;
}

56
src/winmm_joystick.h Normal file
View File

@ -0,0 +1,56 @@
//========================================================================
// GLFW 3.1 WinMM - www.glfw.org
//------------------------------------------------------------------------
// Copyright (c) 2006-2014 Camilla Berglund <elmindreda@elmindreda.org>
//
// 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.
//
//========================================================================
#ifndef _winmm_joystick_h_
#define _winmm_joystick_h_
#define _GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \
_GLFWjoystickWinMM joystick[GLFW_JOYSTICK_LAST + 1]
//========================================================================
// GLFW platform specific types
//========================================================================
//------------------------------------------------------------------------
// Platform-specific joystick structure
//------------------------------------------------------------------------
typedef struct _GLFWjoystickWinMM
{
float axes[6];
unsigned char buttons[36]; // 32 buttons plus one hat
char* name;
} _GLFWjoystickWinMM;
//========================================================================
// Prototypes for platform specific internal functions
//========================================================================
void _glfwInitJoysticks(void);
void _glfwTerminateJoysticks(void);
#endif // _winmm_joystick_h_

View File

@ -65,7 +65,6 @@
#define _GLFW_UNIX_TIME_CONTEXT _glfw.x11.timer
#include "unix_time.h"
#define _GLFW_LINUX_JOYSTICK_CONTEXT _glfw.x11.joystick
#include "linux_joystick.h"
#define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowX11 x11
@ -215,8 +214,6 @@ typedef struct _GLFWlibraryX11
Window source;
} xdnd;
_GLFWjoystickLinux joystick;
} _GLFWlibraryX11;