-
Notifications
You must be signed in to change notification settings - Fork 5
/
joystick.c
87 lines (72 loc) · 1.57 KB
/
joystick.c
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
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stropts.h>
#include "joystick.h"
int open_joystick(char * path)
{
int fd;
errno = 0;
// if ((fd = open(path, O_RDONLY | O_NONBLOCK)) == -1) {
if ((fd = open(path, O_RDONLY)) == -1) {
perror("open joystick");
exit(1);
}
return fd;
}
int close_joystick(int fd)
{
int res;
errno = 0;
if ((res = close(fd)) == -1) {
perror("close joystick");
exit(1);
}
return res;
}
int get_number_of_axes(int fd)
{
char axes;
errno = 0;
if (ioctl(fd, JSIOCGAXES, &axes)) {
if (errno != 0)
perror("get number of axes");
return -1;
}
return axes;
}
int get_number_of_buttons(int fd)
{
char btns;
errno = 0;
if (ioctl(fd, JSIOCGBUTTONS, &btns)) {
if (errno != 0)
perror("get number of buttons");
return -1;
}
return btns;
}
int get_joystick_event(int fd, struct js_event * event)
{
intmax_t bytes;
errno = 0;
if((bytes = read(fd, event, sizeof(*event))) == -1) {
if (errno != EAGAIN)
perror("read joystick event");
return bytes;
}
if (bytes == sizeof(*event)) {
return 0;
}
fprintf(stderr, "incomplete read from joystick %ld", bytes);
return -1;
}
void debug_print_joystick_event(const struct js_event * const event)
{
fprintf(stderr, "Event: time %8u / value %8hd / type %8u / number %X\n",
event->time, event->value, event->type, event->number);
}