aboutsummaryrefslogblamecommitdiffstats
path: root/libgamepad_next_event.c
blob: acc23eae34a2c259562ec4b9944be8e3c05ba059 (plain) (tree)
























































                                                                                              
/* 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;
	}
}