aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_next_event.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 /libgamepad_next_event.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 'libgamepad_next_event.c')
-rw-r--r--libgamepad_next_event.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/libgamepad_next_event.c b/libgamepad_next_event.c
new file mode 100644
index 0000000..acc23ea
--- /dev/null
+++ b/libgamepad_next_event.c
@@ -0,0 +1,57 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+int
+libgamepad_next_event(struct libgamepad_device *device, struct libgamepad_input_event *eventp)
+{
+ int r;
+ struct input_event ev;
+
+ if (!device->require_sync) {
+ r = libevdev_next_event(device->dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
+ if (r < 0) {
+ errno = -r;
+ return -1;
+ }
+ } else {
+ r = libevdev_next_event(device->dev, LIBEVDEV_READ_FLAG_SYNC, &ev);
+ if (r == -EAGAIN) {
+ device->require_sync = 0; /* yes, this is how it is document */
+ return 0;
+ } else if (r < 0) {
+ errno = -r;
+ return -1;
+ }
+ if (r != LIBEVDEV_READ_STATUS_SYNC) /* just to be safe */
+ device->require_sync = 0;
+ }
+
+ eventp->sync_event = (r == LIBEVDEV_READ_STATUS_SYNC);
+ eventp->code = ev.code;
+ eventp->value = ev.value;
+ eventp->time.tv_sec = ev.input_event_sec;
+ eventp->time.tv_usec = ev.input_event_usec;
+
+ switch (ev.type) {
+ case EV_KEY:
+ eventp->type = LIBGAMEPAD_BUTTON;
+ return 1;
+
+ case EV_ABS:
+ eventp->type = LIBGAMEPAD_ABSOLUTE_AXIS;
+ return 1;
+
+ case EV_REL:
+ eventp->type = LIBGAMEPAD_RELATIVE_AXIS;
+ return 1;
+
+ case EV_SYN:
+ if (ev.code == SYN_DROPPED)
+ device->require_sync = 1;
+ return 0;
+
+ default:
+ return 0;
+ }
+}