aboutsummaryrefslogblamecommitdiffstats
path: root/libgamepad_next_event.c
blob: b9d8986790cfdd0920579706dfb29890f7c141b1 (plain) (tree)
1
2
3
4
5
6
7



                                                         

                                                                             
 









































                                                                                                          
 


                                                                         

                                  















                                                                                                                                

                                  

                      
         
                                                                     
 





                                                                                                       
 











                                                                                    
 




                                                                       
 


                                                                    
 



                                                                              
 









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