aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_next_event.c
blob: c74d64113926f78a50a706453aff1b7839c9814a (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
/* 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->internals->require_sync) {
		r = libevdev_next_event(device->internals->dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);
		if (r < 0) {
			errno = -r;
			return -1;
		}
	} else {
		r = libevdev_next_event(device->internals->dev, LIBEVDEV_READ_FLAG_SYNC, &ev);
		if (r == -EAGAIN) {
			device->internals->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->internals->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->internals->require_sync = 1;
		return 0;

	default:
		return 0;
	}
}