-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
75 lines (65 loc) · 2.03 KB
/
main.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
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libevdev/libevdev-uinput.h>
#include "decl.h"
int main(int argc, char **argv) {
if (argc != 2) {
printf("Usage: %s /dev/hidrawN\n", argv[0]);
return 1;
}
char *usbdev_name = argv[1];
int hid = open(usbdev_name, O_RDONLY);
if (hid < 0) {
fprintf(stderr, "unable to open mouse hid device %s: %s\n", usbdev_name, strerror(errno));
return errno;
}
int err;
struct libevdev_uinput *uidev;
err = uinput_init(&uidev);
if (err != 0) {
fprintf(stderr, "unable to init uinput device: %s\n", strerror(-err));
return -err;
}
unsigned int activated = 0, history = 0;
while (true) {
unsigned char buf[7]; // 7-axis "joystick"
int r = read(hid, buf, sizeof(buf));
if (r != sizeof(buf)) {
fprintf(stderr, "read() failed, read %d bytes: %s\n", r, strerror(errno));
if (r == -1) {
return errno;
}
}
const struct sandio_state state = sandio_decode(buf);
unsigned int packed = state.top | (state.right << 8) | (state.left << 16);
if (packed == history) {
continue;
}
history = packed;
bool sync = false;
for (int i = 0;; ++i) {
const struct rule r = rules[i];
if (!r.mask) {
break; // end of rules
}
if (packed & r.mask & ~activated) {
libevdev_uinput_write_event(uidev, EV_KEY, r.code, 1);
activated |= r.mask;
sync = true;
}
if (~packed & r.mask & activated) {
libevdev_uinput_write_event(uidev, EV_KEY, r.code, 0);
activated &= ~r.mask;
sync = true;
}
}
if (sync) {
libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
}
}
libevdev_uinput_destroy(uidev);
}