diff options
Diffstat (limited to '')
-rw-r--r-- | test-attachments.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test-attachments.c b/test-attachments.c new file mode 100644 index 0000000..9f8e63c --- /dev/null +++ b/test-attachments.c @@ -0,0 +1,50 @@ +/* See LICENSE file for copyright and license details. */ +#include "libgamepad.h" + +#include <sys/select.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + + +int +main(void) +{ + LIBGAMEPAD_ATTACHMENT_MONITOR *monitor; + int fd, r; + fd_set fds; + char *syspath = NULL; + size_t size = 0; + enum libgamepad_attachment_event_type action; + + fd = libgamepad_create_attachment_monitor(&monitor); + if (fd < 0) { + perror("libgamepad_create_attachment_monitor"); + return 1; + } + + FD_ZERO(&fds); + for (;;) { + FD_SET(fd, &fds); + if (select(fd + 1, &fds, NULL, NULL, NULL) < 0) { + if (errno == EINTR) + continue; + perror("select"); + libgamepad_destroy_attachment_monitor(monitor); + free(syspath); + return 1; + } + + r = libgamepad_get_attachment_event(monitor, &syspath, &size, &action); + if (r <= 0) { + if (!r || errno == EINTR || errno == EAGAIN) + continue; + perror("libgamepad_get_attachment_event"); + libgamepad_destroy_attachment_monitor(monitor); + free(syspath); + return 1; + } + + printf("%s %s\n", action == LIBGAMEPAD_ADDED ? "added" : "removed", syspath); + } +} |