mirror of
https://github.com/glfw/glfw.git
synced 2026-09-17 22:32:50 +00:00
Add glfwSetJoystickCallback
This commit is contained in:
@@ -190,6 +190,8 @@ static void removeJoystick(_GLFWjoydeviceNS* joystick)
|
||||
free(joystick->buttons);
|
||||
|
||||
memset(joystick, 0, sizeof(_GLFWjoydeviceNS));
|
||||
|
||||
_glfwInputJoystickChange(joystick - _glfw.ns_js.devices, GLFW_DISCONNECTED);
|
||||
}
|
||||
|
||||
// Polls for joystick axis events and updates GLFW state
|
||||
@@ -329,6 +331,8 @@ static void matchCallback(void* context,
|
||||
sizeof(float));
|
||||
joystick->buttons = calloc(CFArrayGetCount(joystick->buttonElements) +
|
||||
CFArrayGetCount(joystick->hatElements) * 4, 1);
|
||||
|
||||
_glfwInputJoystickChange(joy, GLFW_CONNECTED);
|
||||
}
|
||||
|
||||
// Callback for user-initiated joystick removal
|
||||
|
||||
+13
@@ -220,6 +220,12 @@ void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths)
|
||||
window->callbacks.drop((GLFWwindow*) window, count, paths);
|
||||
}
|
||||
|
||||
void _glfwInputJoystickChange(int joy, int event)
|
||||
{
|
||||
if (_glfw.callbacks.joystick)
|
||||
_glfw.callbacks.joystick(joy, event);
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW internal API //////
|
||||
@@ -621,6 +627,13 @@ GLFWAPI const char* glfwGetJoystickName(int joy)
|
||||
return _glfwPlatformGetJoystickName(joy);
|
||||
}
|
||||
|
||||
GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
|
||||
{
|
||||
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
|
||||
_GLFW_SWAP_POINTERS(_glfw.callbacks.joystick, cbfun);
|
||||
return cbfun;
|
||||
}
|
||||
|
||||
GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string)
|
||||
{
|
||||
_GLFWwindow* window = (_GLFWwindow*) handle;
|
||||
|
||||
@@ -448,6 +448,7 @@ struct _GLFWlibrary
|
||||
|
||||
struct {
|
||||
GLFWmonitorfun monitor;
|
||||
GLFWjoystickfun joystick;
|
||||
} callbacks;
|
||||
|
||||
// This is defined in the window API's platform.h
|
||||
@@ -947,6 +948,13 @@ void _glfwInputError(int error, const char* format, ...);
|
||||
*/
|
||||
void _glfwInputDrop(_GLFWwindow* window, int count, const char** names);
|
||||
|
||||
/*! @brief Notifies shared code of a joystick connection/disconnection event.
|
||||
* @param[in] joy The joystick that was connected or disconnected.
|
||||
* @param[in] event One of `GLFW_CONNECTED` or `GLFW_DISCONNECTED`.
|
||||
* @ingroup event
|
||||
*/
|
||||
void _glfwInputJoystickChange(int joy, int event);
|
||||
|
||||
|
||||
//========================================================================
|
||||
// Utility functions
|
||||
|
||||
+29
-19
@@ -100,6 +100,8 @@ static GLFWbool openJoystickDevice(const char* path)
|
||||
ioctl(fd, JSIOCGBUTTONS, &buttonCount);
|
||||
js->buttonCount = (int) buttonCount;
|
||||
js->buttons = calloc(buttonCount, 1);
|
||||
|
||||
_glfwInputJoystickChange(joy, GLFW_CONNECTED);
|
||||
#endif // __linux__
|
||||
return GLFW_TRUE;
|
||||
}
|
||||
@@ -109,25 +111,7 @@ static GLFWbool openJoystickDevice(const char* path)
|
||||
static GLFWbool pollJoystickEvents(_GLFWjoystickLinux* js)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
ssize_t offset = 0;
|
||||
char buffer[16384];
|
||||
|
||||
const ssize_t size = read(_glfw.linux_js.inotify, buffer, sizeof(buffer));
|
||||
|
||||
while (size > offset)
|
||||
{
|
||||
regmatch_t match;
|
||||
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
|
||||
|
||||
if (regexec(&_glfw.linux_js.regex, e->name, 1, &match, 0) == 0)
|
||||
{
|
||||
char path[20];
|
||||
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
|
||||
openJoystickDevice(path);
|
||||
}
|
||||
|
||||
offset += sizeof(struct inotify_event) + e->len;
|
||||
}
|
||||
_glfwPollJoystickEvents();
|
||||
|
||||
if (!js->present)
|
||||
return GLFW_FALSE;
|
||||
@@ -149,6 +133,9 @@ static GLFWbool pollJoystickEvents(_GLFWjoystickLinux* js)
|
||||
free(js->path);
|
||||
|
||||
memset(js, 0, sizeof(_GLFWjoystickLinux));
|
||||
|
||||
_glfwInputJoystickChange(js - _glfw.linux_js.js,
|
||||
GLFW_DISCONNECTED);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -285,6 +272,29 @@ void _glfwTerminateJoysticksLinux(void)
|
||||
#endif // __linux__
|
||||
}
|
||||
|
||||
void _glfwPollJoystickEvents(void)
|
||||
{
|
||||
ssize_t offset = 0;
|
||||
char buffer[16384];
|
||||
|
||||
const ssize_t size = read(_glfw.linux_js.inotify, buffer, sizeof(buffer));
|
||||
|
||||
while (size > offset)
|
||||
{
|
||||
regmatch_t match;
|
||||
const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
|
||||
|
||||
if (regexec(&_glfw.linux_js.regex, e->name, 1, &match, 0) == 0)
|
||||
{
|
||||
char path[20];
|
||||
snprintf(path, sizeof(path), "/dev/input/%s", e->name);
|
||||
openJoystickDevice(path);
|
||||
}
|
||||
|
||||
offset += sizeof(struct inotify_event) + e->len;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
////// GLFW platform API //////
|
||||
|
||||
@@ -64,4 +64,6 @@ typedef struct _GLFWjoylistLinux
|
||||
GLFWbool _glfwInitJoysticksLinux(void);
|
||||
void _glfwTerminateJoysticksLinux(void);
|
||||
|
||||
void _glfwPollJoystickEvents(void);
|
||||
|
||||
#endif // _glfw3_linux_joystick_h_
|
||||
|
||||
@@ -97,6 +97,8 @@ static GLFWbool openJoystickDevice(DWORD index)
|
||||
js->name = strdup(getDeviceDescription(&xic));
|
||||
js->index = index;
|
||||
|
||||
_glfwInputJoystickChange(joy, GLFW_CONNECTED);
|
||||
|
||||
return GLFW_TRUE;
|
||||
}
|
||||
|
||||
@@ -120,6 +122,7 @@ static GLFWbool pollJoystickEvents(_GLFWjoystickWin32* js, int flags)
|
||||
{
|
||||
free(js->name);
|
||||
memset(js, 0, sizeof(_GLFWjoystickWin32));
|
||||
_glfwInputJoystickChange((int) (js - _glfw.win32_js), GLFW_DISCONNECTED);
|
||||
}
|
||||
|
||||
return GLFW_FALSE;
|
||||
|
||||
+12
-2
@@ -54,11 +54,19 @@
|
||||
void selectDisplayConnection(struct timeval* timeout)
|
||||
{
|
||||
fd_set fds;
|
||||
int result;
|
||||
int result, count;
|
||||
const int fd = ConnectionNumber(_glfw.x11.display);
|
||||
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
#if defined(__linux__)
|
||||
FD_SET(_glfw.linux_js.inotify, &fds);
|
||||
#endif
|
||||
|
||||
if (fd > _glfw.linux_js.inotify)
|
||||
count = fd + 1;
|
||||
else
|
||||
count = _glfw.linux_js.inotify + 1;
|
||||
|
||||
// NOTE: We use select instead of an X function like XNextEvent, as the
|
||||
// wait inside those are guarded by the mutex protecting the display
|
||||
@@ -68,7 +76,7 @@ void selectDisplayConnection(struct timeval* timeout)
|
||||
// TODO: Update timeout value manually
|
||||
do
|
||||
{
|
||||
result = select(fd + 1, &fds, NULL, NULL, timeout);
|
||||
result = select(count, &fds, NULL, NULL, timeout);
|
||||
}
|
||||
while (result == -1 && errno == EINTR && timeout == NULL);
|
||||
}
|
||||
@@ -1999,6 +2007,8 @@ int _glfwPlatformWindowMaximized(_GLFWwindow* window)
|
||||
|
||||
void _glfwPlatformPollEvents(void)
|
||||
{
|
||||
_glfwPollJoystickEvents();
|
||||
|
||||
int count = XPending(_glfw.x11.display);
|
||||
while (count--)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user