aboutsummaryrefslogtreecommitdiffstats
path: root/test-input.c
blob: 9e2dc37909f21dd08156c3388eb7f523e0af4473 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* See LICENSE file for copyright and license details. */
#include "libgamepad.h"

#include <assert.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#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;
	ssize_t 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, 1);
		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.type == LIBGAMEPAD_BUTTON)
			printf("%s %ji\n", libgamepad_get_button_name(NULL, event.code), (intmax_t)event.value);
		else if (event.type == LIBGAMEPAD_ABSOLUTE_AXIS)
			printf("%s %ji\n", libgamepad_get_absolute_axis_name(NULL, event.code), (intmax_t)event.value);
		else if (event.type == LIBGAMEPAD_RELATIVE_AXIS)
			printf("%s %ji\n", libgamepad_get_relative_axis_name(NULL, event.code), (intmax_t)event.value);
		else if (event.type == LIBGAMEPAD_EVENT_DROP)
			printf("event drop\n");
		else if (event.type == LIBGAMEPAD_EVENT_END)
			printf("event conclusion\n");
		else
			printf("unknown event\n");
	}
}