aboutsummaryrefslogtreecommitdiffstats
path: root/libgamepad_next_event.c
blob: b9d8986790cfdd0920579706dfb29890f7c141b1 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* See LICENSE file for copyright and license details. */
#include "common.h"


static int
update_button(struct libgamepad_device *device, uint16_t code, int32_t value)
{
	int16_t index;
	if (code > ELEMSOF(device->button_map))
		return 0;
	index = device->button_map[code];
	if (index < 0 || GETBIT(device->internals->buttons, (size_t)index) == value)
		return 1;
	XORBIT(device->internals->buttons, (size_t)index);
	return 1;
}


static int
update_absolute_axis(struct libgamepad_device *device, uint16_t code, int32_t value)
{
	int16_t index;
	if (code > ELEMSOF(device->absolute_axis_map))
		return 0;
	index = device->absolute_axis_map[code];
	if (index < 0 || device->internals->absinfo[index].value == value)
		return 0;
	device->internals->absinfo[index].value = value;
	return 1;
}


ssize_t
libgamepad_next_event(struct libgamepad_device *device, struct libgamepad_input_event *events, size_t max)
{
	size_t i, dequeued = 0;
	void *new;
	ssize_t r;
	struct input_event *ev;

	if (device->internals->deferred_error) {
		errno = device->internals->deferred_error;
		device->internals->deferred_error = 0;
		return -1;
	}

	if (device->internals->require_sync)
		if (libgamepad_drain_events(device))
			return -1;

	if (max > device->internals->evbuf_size) {
		if (max > SIZE_MAX / sizeof(*device->internals->evbuf)) {
			errno = ENOMEM;
			return -1;
		}
		new = realloc(device->internals->evbuf, max * sizeof(*device->internals->evbuf));
		if (!new)
			return -1;
		device->internals->evbuf = new;
		device->internals->evbuf_size = max;
	}

	dequeued = device->internals->ev_queued < max ? device->internals->ev_queued : max;
	if (!dequeued || dequeued < max) {
		r = read(device->fd, &device->internals->evbuf[dequeued], (max - dequeued) * sizeof(*device->internals->evbuf));
		if (r <= 0) {
			if (!r && (max - dequeued))
				errno = ENODEV;
			return -1;
		} else if ((size_t)r % sizeof(*device->internals->evbuf)) {
			errno = EBADF;
			return -1;
		}
	} else {
		r = 0;
	}
	r += (ssize_t)(dequeued * sizeof(*device->internals->evbuf));

	i = 0;
	for (ev = device->internals->evbuf; r; r -= (ssize_t)sizeof(*device->internals->evbuf), ev++) {
		events[i].code = ev->code;
		events[i].value = ev->value;
		events[i].time.tv_sec = ev->input_event_sec;
		events[i].time.tv_usec = ev->input_event_usec;

		switch (ev->type) {
		case EV_SYN:
			events[i].code = 0;
			events[i].value = 0;
			if (ev->code == SYN_REPORT) {
				events[i++].type = LIBGAMEPAD_EVENT_END;
			} else if (ev->code == SYN_DROPPED) {
				device->internals->require_sync = device->auto_sync;
				events[i++].type = LIBGAMEPAD_EVENT_DROP;
				goto out;
			}
			break;

		case EV_KEY:
			ev->value = !!ev->value;
			if (update_button(device, ev->code, ev->value))
				events[i++].type = LIBGAMEPAD_BUTTON;
			break;

		case EV_REL:
			events[i++].type = LIBGAMEPAD_RELATIVE_AXIS;
			break;

		case EV_ABS:
			if (update_absolute_axis(device, ev->code, ev->value))
				events[i++].type = LIBGAMEPAD_ABSOLUTE_AXIS;
			break;

		default:
			break;
		}
	}

out:
	if (dequeued) {
		device->internals->ev_queued -= dequeued;
		memmove(&device->internals->evbuf[0], &device->internals->evbuf[dequeued],
		        device->internals->ev_queued * sizeof(*device->internals->evbuf));
	}
	return (ssize_t)i;
}