-
Notifications
You must be signed in to change notification settings - Fork 0
/
w2w.c
87 lines (76 loc) · 2.34 KB
/
w2w.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 <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <unistd.h>
void print_usage(FILE *stream, const char *program) {
fprintf(stream,
"w2w - Transform a tablet wheel (or ring) in a mouse wheel\n"
"\n"
"%s [-h | [-s steps]]\n"
"Mainly to use with the ring of my HUION\n"
"options:\n"
" -h show this message and exit\n",
program);
}
bool read_event(struct input_event *event) {
return fread(event, sizeof(struct input_event), 1, stdin) == 1;
}
void write_event(const struct input_event *event) {
if (fwrite(event, sizeof(struct input_event), 1, stdout) != 1)
exit(EXIT_FAILURE);
}
void write_scroll(const int steps) {
const struct input_event low_res_event = {.type = EV_REL,
.code = REL_WHEEL,
.value = steps},
high_res_event = {.type = EV_REL,
.code = REL_WHEEL_HI_RES,
.value = steps * 120};
// write_event(&low_res_event);
write_event(&high_res_event);
}
const int RING_SIZE = 12;
int main(int argc, char *argv[]) {
for (int opt; (opt = getopt(argc, argv, "h:")) != -1;) {
switch (opt) {
case 'h':
return print_usage(stdout, argv[0]), EXIT_SUCCESS;
}
return print_usage(stderr, argv[0]), EXIT_FAILURE;
}
setbuf(stdin, NULL);
setbuf(stdout, NULL);
struct input_event input;
bool scrolling = false;
int last_position = 0;
int low_res;
while (read_event(&input)) {
// Unhandled event, just bypass it
if (input.type != EV_ABS) {
write_event(&input);
continue;
}
if (input.code == ABS_MISC && input.value) {
last_position = 0;
scrolling = false;
}
if (input.code == ABS_WHEEL) {
if (scrolling) {
low_res = (input.value - last_position);
last_position = input.value;
int sign = (low_res > 0) - (low_res < 0);
low_res = abs(low_res) * 2 < RING_SIZE
? low_res
: -sign * (RING_SIZE - abs(low_res));
if (low_res) {
write_scroll(low_res);
}
} else {
last_position = input.value;
scrolling = true;
}
}
}
}