1 //========================================================================
2 // GLFW 3.4 Linux - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2002-2006 Marcus Geelnard
5 // Copyright (c) 2006-2017 Camilla Löwy <elmindreda@glfw.org>
7 // This software is provided 'as-is', without any express or implied
8 // warranty. In no event will the authors be held liable for any damages
9 // arising from the use of this software.
11 // Permission is granted to anyone to use this software for any purpose,
12 // including commercial applications, and to alter it and redistribute it
13 // freely, subject to the following restrictions:
15 // 1. The origin of this software must not be misrepresented; you must not
16 // claim that you wrote the original software. If you use this software
17 // in a product, an acknowledgment in the product documentation would
18 // be appreciated but is not required.
20 // 2. Altered source versions must be plainly marked as such, and must not
21 // be misrepresented as being the original software.
23 // 3. This notice may not be removed or altered from any source
26 //========================================================================
27 // It is fine to use C99 in this file because it will not be built with VS
28 //========================================================================
32 #include <sys/types.h>
34 #include <sys/inotify.h>
43 #ifndef SYN_DROPPED // < v2.6.39 kernel headers
44 // Workaround for CentOS-6, which is supported till 2020-11-30, but still on v2.6.32
48 // Apply an EV_KEY event to the specified joystick
50 static void handleKeyEvent(_GLFWjoystick* js, int code, int value)
52 _glfwInputJoystickButton(js,
53 js->linjs.keyMap[code - BTN_MISC],
54 value ? GLFW_PRESS : GLFW_RELEASE);
57 // Apply an EV_ABS event to the specified joystick
59 static void handleAbsEvent(_GLFWjoystick* js, int code, int value)
61 const int index = js->linjs.absMap[code];
63 if (code >= ABS_HAT0X && code <= ABS_HAT3Y)
65 static const char stateMap[3][3] =
67 { GLFW_HAT_CENTERED, GLFW_HAT_UP, GLFW_HAT_DOWN },
68 { GLFW_HAT_LEFT, GLFW_HAT_LEFT_UP, GLFW_HAT_LEFT_DOWN },
69 { GLFW_HAT_RIGHT, GLFW_HAT_RIGHT_UP, GLFW_HAT_RIGHT_DOWN },
72 const int hat = (code - ABS_HAT0X) / 2;
73 const int axis = (code - ABS_HAT0X) % 2;
74 int* state = js->linjs.hats[hat];
76 // NOTE: Looking at several input drivers, it seems all hat events use
77 // -1 for left / up, 0 for centered and 1 for right / down
85 _glfwInputJoystickHat(js, index, stateMap[state[0]][state[1]]);
89 const struct input_absinfo* info = &js->linjs.absInfo[code];
90 float normalized = value;
92 const int range = info->maximum - info->minimum;
95 // Normalize to 0.0 -> 1.0
96 normalized = (normalized - info->minimum) / range;
97 // Normalize to -1.0 -> 1.0
98 normalized = normalized * 2.0f - 1.0f;
101 _glfwInputJoystickAxis(js, index, normalized);
105 // Poll state of absolute axes
107 static void pollAbsState(_GLFWjoystick* js)
109 for (int code = 0; code < ABS_CNT; code++)
111 if (js->linjs.absMap[code] < 0)
114 struct input_absinfo* info = &js->linjs.absInfo[code];
116 if (ioctl(js->linjs.fd, EVIOCGABS(code), info) < 0)
119 handleAbsEvent(js, code, info->value);
123 #define isBitSet(bit, arr) (arr[(bit) / 8] & (1 << ((bit) % 8)))
125 // Attempt to open the specified joystick device
127 static GLFWbool openJoystickDevice(const char* path)
129 for (int jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
131 if (!_glfw.joysticks[jid].present)
133 if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
137 _GLFWjoystickLinux linjs = {0};
138 linjs.fd = open(path, O_RDONLY | O_NONBLOCK);
142 char evBits[(EV_CNT + 7) / 8] = {0};
143 char keyBits[(KEY_CNT + 7) / 8] = {0};
144 char absBits[(ABS_CNT + 7) / 8] = {0};
147 if (ioctl(linjs.fd, EVIOCGBIT(0, sizeof(evBits)), evBits) < 0 ||
148 ioctl(linjs.fd, EVIOCGBIT(EV_KEY, sizeof(keyBits)), keyBits) < 0 ||
149 ioctl(linjs.fd, EVIOCGBIT(EV_ABS, sizeof(absBits)), absBits) < 0 ||
150 ioctl(linjs.fd, EVIOCGID, &id) < 0)
152 _glfwInputError(GLFW_PLATFORM_ERROR,
153 "Linux: Failed to query input device: %s",
159 // Ensure this device supports the events expected of a joystick
160 if (!isBitSet(EV_KEY, evBits) || !isBitSet(EV_ABS, evBits))
168 if (ioctl(linjs.fd, EVIOCGNAME(sizeof(name)), name) < 0)
169 strncpy(name, "Unknown", sizeof(name));
173 // Generate a joystick GUID that matches the SDL 2.0.5+ one
174 if (id.vendor && id.product && id.version)
176 sprintf(guid, "%02x%02x0000%02x%02x0000%02x%02x0000%02x%02x0000",
177 id.bustype & 0xff, id.bustype >> 8,
178 id.vendor & 0xff, id.vendor >> 8,
179 id.product & 0xff, id.product >> 8,
180 id.version & 0xff, id.version >> 8);
184 sprintf(guid, "%02x%02x0000%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x00",
185 id.bustype & 0xff, id.bustype >> 8,
186 name[0], name[1], name[2], name[3],
187 name[4], name[5], name[6], name[7],
188 name[8], name[9], name[10]);
191 int axisCount = 0, buttonCount = 0, hatCount = 0;
193 for (int code = BTN_MISC; code < KEY_CNT; code++)
195 if (!isBitSet(code, keyBits))
198 linjs.keyMap[code - BTN_MISC] = buttonCount;
202 for (int code = 0; code < ABS_CNT; code++)
204 linjs.absMap[code] = -1;
205 if (!isBitSet(code, absBits))
208 if (code >= ABS_HAT0X && code <= ABS_HAT3Y)
210 linjs.absMap[code] = hatCount;
217 if (ioctl(linjs.fd, EVIOCGABS(code), &linjs.absInfo[code]) < 0)
220 linjs.absMap[code] = axisCount;
226 _glfwAllocJoystick(name, guid, axisCount, buttonCount, hatCount);
233 strncpy(linjs.path, path, sizeof(linjs.path) - 1);
234 memcpy(&js->linjs, &linjs, sizeof(linjs));
238 _glfwInputJoystick(js, GLFW_CONNECTED);
244 // Frees all resources associated with the specified joystick
246 static void closeJoystick(_GLFWjoystick* js)
249 _glfwFreeJoystick(js);
250 _glfwInputJoystick(js, GLFW_DISCONNECTED);
253 // Lexically compare joysticks by name; used by qsort
255 static int compareJoysticks(const void* fp, const void* sp)
257 const _GLFWjoystick* fj = fp;
258 const _GLFWjoystick* sj = sp;
259 return strcmp(fj->linjs.path, sj->linjs.path);
263 //////////////////////////////////////////////////////////////////////////
264 ////// GLFW internal API //////
265 //////////////////////////////////////////////////////////////////////////
267 void _glfwDetectJoystickConnectionLinux(void)
269 if (_glfw.linjs.inotify <= 0)
274 const ssize_t size = read(_glfw.linjs.inotify, buffer, sizeof(buffer));
276 while (size > offset)
279 const struct inotify_event* e = (struct inotify_event*) (buffer + offset);
281 offset += sizeof(struct inotify_event) + e->len;
283 if (regexec(&_glfw.linjs.regex, e->name, 1, &match, 0) != 0)
287 snprintf(path, sizeof(path), "/dev/input/%s", e->name);
289 if (e->mask & (IN_CREATE | IN_ATTRIB))
290 openJoystickDevice(path);
291 else if (e->mask & IN_DELETE)
293 for (int jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
295 if (strcmp(_glfw.joysticks[jid].linjs.path, path) == 0)
297 closeJoystick(_glfw.joysticks + jid);
306 //////////////////////////////////////////////////////////////////////////
307 ////// GLFW platform API //////
308 //////////////////////////////////////////////////////////////////////////
310 GLFWbool _glfwPlatformInitJoysticks(void)
312 const char* dirname = "/dev/input";
314 _glfw.linjs.inotify = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
315 if (_glfw.linjs.inotify > 0)
317 // HACK: Register for IN_ATTRIB to get notified when udev is done
318 // This works well in practice but the true way is libudev
320 _glfw.linjs.watch = inotify_add_watch(_glfw.linjs.inotify,
322 IN_CREATE | IN_ATTRIB | IN_DELETE);
325 // Continue without device connection notifications if inotify fails
327 if (regcomp(&_glfw.linjs.regex, "^event[0-9]\\+$", 0) != 0)
329 _glfwInputError(GLFW_PLATFORM_ERROR, "Linux: Failed to compile regex");
335 DIR* dir = opendir(dirname);
338 struct dirent* entry;
340 while ((entry = readdir(dir)))
344 if (regexec(&_glfw.linjs.regex, entry->d_name, 1, &match, 0) != 0)
349 snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
351 if (openJoystickDevice(path))
358 // Continue with no joysticks if enumeration fails
360 qsort(_glfw.joysticks, count, sizeof(_GLFWjoystick), compareJoysticks);
364 void _glfwPlatformTerminateJoysticks(void)
368 for (jid = 0; jid <= GLFW_JOYSTICK_LAST; jid++)
370 _GLFWjoystick* js = _glfw.joysticks + jid;
375 if (_glfw.linjs.inotify > 0)
377 if (_glfw.linjs.watch > 0)
378 inotify_rm_watch(_glfw.linjs.inotify, _glfw.linjs.watch);
380 close(_glfw.linjs.inotify);
381 regfree(&_glfw.linjs.regex);
385 int _glfwPlatformPollJoystick(_GLFWjoystick* js, int mode)
387 // Read all queued events (non-blocking)
390 struct input_event e;
393 if (read(js->linjs.fd, &e, sizeof(e)) < 0)
395 // Reset the joystick slot if the device was disconnected
402 if (e.type == EV_SYN)
404 if (e.code == SYN_DROPPED)
405 _glfw.linjs.dropped = GLFW_TRUE;
406 else if (e.code == SYN_REPORT)
408 _glfw.linjs.dropped = GLFW_FALSE;
413 if (_glfw.linjs.dropped)
416 if (e.type == EV_KEY)
417 handleKeyEvent(js, e.code, e.value);
418 else if (e.type == EV_ABS)
419 handleAbsEvent(js, e.code, e.value);
425 void _glfwPlatformUpdateGamepadGUID(char* guid)