mirror of
https://github.com/glfw/glfw.git
synced 2026-09-19 07:05:49 +00:00
Add glfwSetJoystickCallback
This commit is contained in:
+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 //////
|
||||
|
||||
Reference in New Issue
Block a user