aboutsummaryrefslogtreecommitdiffstats
path: root/test-attachments.c
blob: 9f8e63c0e4d14f2ee32870eb81e7334cb9787971 (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
/* 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);
	}
}