aboutsummaryrefslogtreecommitdiffstats
path: root/test-attachments.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2022-07-23 14:39:44 +0200
committerMattias Andrée <maandree@kth.se>2022-07-23 14:40:11 +0200
commitd49393b339c47733c2eda207b8ba03d61db0a5a3 (patch)
treebc609622c3cabe1dc6b59970cb8a9a8364a561a7 /test-attachments.c
downloadlibgamepad-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 'test-attachments.c')
-rw-r--r--test-attachments.c50
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);
+ }
+}