aboutsummaryrefslogblamecommitdiffstats
path: root/test-attachments.c
blob: 9f8e63c0e4d14f2ee32870eb81e7334cb9787971 (plain) (tree)

















































                                                                                             
/* See LICENSE file for copyright and license details. */
#include "libgamepad.h"

#include <sys/select.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>


int
main(void)
{
	LIBGAMEPAD_ATTACHMENT_MONITOR *monitor;
	int fd, r;
	fd_set fds;
	char *syspath = NULL;
	size_t size = 0;
	enum libgamepad_attachment_event_type action;

	fd = libgamepad_create_attachment_monitor(&monitor);
	if (fd < 0) {
		perror("libgamepad_create_attachment_monitor");
		return 1;
	}

	FD_ZERO(&fds);
	for (;;) {
		FD_SET(fd, &fds);
		if (select(fd + 1, &fds, NULL, NULL, NULL) < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			libgamepad_destroy_attachment_monitor(monitor);
			free(syspath);
			return 1;
		}

		r = libgamepad_get_attachment_event(monitor, &syspath, &size, &action);
		if (r <= 0) {
			if (!r || errno == EINTR || errno == EAGAIN)
				continue;
			perror("libgamepad_get_attachment_event");
			libgamepad_destroy_attachment_monitor(monitor);
			free(syspath);
			return 1;
		}

		printf("%s %s\n", action == LIBGAMEPAD_ADDED ? "added" : "removed", syspath);
	}
}