diff options
author | Mattias Andrée <maandree@kth.se> | 2022-07-23 14:39:44 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2022-07-23 14:40:11 +0200 |
commit | d49393b339c47733c2eda207b8ba03d61db0a5a3 (patch) | |
tree | bc609622c3cabe1dc6b59970cb8a9a8364a561a7 /libgamepad_get_attachment_event.c | |
download | libgamepad-d49393b339c47733c2eda207b8ba03d61db0a5a3.tar.gz libgamepad-d49393b339c47733c2eda207b8ba03d61db0a5a3.tar.bz2 libgamepad-d49393b339c47733c2eda207b8ba03d61db0a5a3.tar.xz |
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r-- | libgamepad_get_attachment_event.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/libgamepad_get_attachment_event.c b/libgamepad_get_attachment_event.c new file mode 100644 index 0000000..4462356 --- /dev/null +++ b/libgamepad_get_attachment_event.c @@ -0,0 +1,60 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +int +libgamepad_get_attachment_event(LIBGAMEPAD_ATTACHMENT_MONITOR *monitor, char **syspathp, size_t *sizep, + enum libgamepad_attachment_event_type *typep) +{ + struct udev_device *device; + const char *action; + const char *syspath; + size_t len; + void *new; + char *syspath_end; + int saved_errno; + + device = udev_monitor_receive_device(monitor->monitor); + if (!device) + return -1; + + action = udev_device_get_action(device); + if (!strcmp(action, "bind")) + *typep = LIBGAMEPAD_ADDED; + else if (!strcmp(action, "unbind")) + *typep = LIBGAMEPAD_REMOVED; + else + goto suppress; + + syspath = udev_device_get_syspath(device); + if (!syspath || !*syspath) + goto suppress; + len = strlen(syspath) + sizeof("/hidraw"); + if (len > *sizep) { + new = realloc(*syspathp, len); + if (!new) { + udev_device_unref(device); + return -1; + } + *syspathp = new; + *sizep = len; + } + syspath_end = stpcpy(*syspathp, syspath); + + if (*typep == LIBGAMEPAD_ADDED) { + stpcpy(syspath_end, "/hidraw"); + saved_errno = errno; + if (access(*syspathp, F_OK)) { + errno = saved_errno; + goto suppress; + } + *syspath_end = '\0'; + } + + udev_device_unref(device); + return 1; + +suppress: + udev_device_unref(device); + return 0; +} |