From d49393b339c47733c2eda207b8ba03d61db0a5a3 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 23 Jul 2022 14:39:44 +0200 Subject: First commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- test-input.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 test-input.c (limited to 'test-input.c') diff --git a/test-input.c b/test-input.c new file mode 100644 index 0000000..1fd5c4c --- /dev/null +++ b/test-input.c @@ -0,0 +1,77 @@ +/* See LICENSE file for copyright and license details. */ +#include "libgamepad.h" + +#include +#include +#include +#include + +#define GRAB_DEVICE 0 + + +static struct libgamepad_device gamepad; + + +static void +sigint_handler(int signo) +{ + (void) signo; + +#if GRAB_DEVICE + if (libgamepad_ungrab(&gamepad)) + perror("libgamepad_ungrab"); +#endif + + libgamepad_close_device(&gamepad); + exit(0); +} + + +int +main(int argc, char *argv[]) +{ + struct libgamepad_input_event event; + int r; + + if (argc != 2) { + fprintf(stderr, "Please provide the path to the subdevice as the only command line argument\n"); + return 1; + } + + if (libgamepad_open_device(&gamepad, AT_FDCWD, argv[1], O_RDONLY)) { + perror("libgamepad_open_device"); + return 1; + } + +#if GRAB_DEVICE + if (libgamepad_grab(&gamepad)) + perror("libgamepad_grab"); +#endif + + if (signal(SIGINT, sigint_handler) == SIG_ERR) { + perror("signal"); + return 1; + } + + for (;;) { + r = libgamepad_next_event(&gamepad, &event); + if (r <= 0) { + if (!r || errno == EINTR) + continue; + perror("libgamepad_next_event"); + return 1; + } + printf("[%lli.%06li] ", (long long int)event.time.tv_sec, event.time.tv_usec); + if (event.sync_event) + printf("[sync] "); + if (event.type == LIBGAMEPAD_BUTTON) { + printf("%s ", libgamepad_get_button_name(event.code)); + } else if (event.type == LIBGAMEPAD_ABSOLUTE_AXIS) { + printf("%s ", libgamepad_get_absolute_axis_name(event.code)); + } else { + assert(event.type == LIBGAMEPAD_RELATIVE_AXIS); + printf("%s ", libgamepad_get_relative_axis_name(event.code)); + } + printf("%lli\n", (long long int)event.value); + } +} -- cgit v1.2.3-70-g09d2