Add glfwSetJoystickCallback

This commit is contained in:
Camilla Berglund
2015-12-13 17:38:50 +01:00
parent bdd17c337f
commit 8a7fa306ce
12 changed files with 223 additions and 104 deletions
+29 -19
View File
@@ -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 //////