-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoystick.c
101 lines (84 loc) · 2.1 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <memory.h>
#include "joystick.h"
const char* type2str(__u8 type) {
type &= ~JS_EVENT_INIT;
switch(type) {
case JS_EVENT_BUTTON:
return "JS_EVENT_BUTTON";
break;
case JS_EVENT_AXIS:
return "JS_EVENT_AXIS";
break;
case JS_EVENT_INIT:
return "JS_EVENT_INIT";
break;
default:
return "UNKNOWN";
}
}
void js_event_print(struct js_event* e) {
printf("%s: time=%d value=%d number=%d\n", type2str(e->type),
e->time, e->value, e->number);
}
int joystick_maybe_read(int fileno, struct js_event* e) {
if(read(fileno, e, sizeof(struct js_event)) <= 0) {
if(errno != EAGAIN) {
fprintf(stderr, "error reading from joystick\n");
exit(-1);
}
return 0;
} else {
e->type &= ~JS_EVENT_INIT;
return 1;
}
}
js_state joystick_open(const char* device) {
int fd = open ("/dev/input/js0", O_RDONLY|O_NONBLOCK);
int capacity = 32;
js_state state = malloc(sizeof(struct js_state_));
state->num_values = 0;
state->capacity = capacity;
state->fileno = fd;
state->values = malloc(capacity * sizeof(struct js_event));
memset(state->values, 0, capacity * sizeof(struct js_event));
joystick_update_state(state);
return state;
}
void joystick_close(js_state state) {
close(state->fileno);
free(state->values);
free(state);
}
void joystick_update_state(js_state state) {
struct js_event e;
while(joystick_maybe_read(state->fileno, &e)) {
int slot = e.number * 2 + e.type - 1;
if(slot >= state->num_values) {
if(slot >= state->capacity) {
int new_cap = slot + 1;
state->values = realloc(state->values, new_cap * sizeof(struct js_event));
state->capacity = new_cap;
}
state->num_values = slot + 1;
}
state->values[slot] = e;
}
}
void joystick_print_state(js_state state) {
int ii;
for(ii = 0; ii < state->num_values; ++ii) {
js_event_print(state->values + ii);
}
}
/*
int main(int argc, char** argv) {
int ii;
js_state state = joystick_open("/dev/input/js0");
joystick_print_state(state);
joystick_close(state);
return 1;
}
*/