-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.c
164 lines (146 loc) · 3.66 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <libinput.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#define INTERVAL 100 // in ms
#define THRESHOLD 10
#define LEFT_SWIPE "xdotool key alt+ctrl+Right"
#define RIGHT_SWIPE "xdotool key alt+ctrl+Left"
int find (char *src, char *tgt) {
int pos = 0;
char tmp;
int slen = strlen(src);
int tlen = strlen(tgt);
for (; pos <= slen-tlen+1; pos++){
tmp = src[pos+tlen];
src[pos+tlen] = 0;
if (strcmp(src+pos, tgt) == 0) {
src[pos+tlen] = tmp;
return pos;
}
src[pos+tlen] = tmp;
}
return -1;
}
int scan_device (int ev) {
// check the access permission
if (access("/proc/bus/input/devices", R_OK) != 0) {
printf("permission denied\n");
exit(-1);
}
FILE *f = fopen("/proc/bus/input/devices", "r");
char s[1024];
int x, pos, y;
while (fgets(s, 1024, f) != NULL) {
if (s[0] == 'H') {
pos = find(s, "event");
sscanf(s+pos, "event%d", &x);
} else if (s[0] == 'B') {
sscanf(s+3, "EV=%x", &y);
if (y == ev) {
return x;
}
}
}
return -1;
}
int dispatch (struct libinput *li) {
struct epoll_event ep[32];
epoll_wait(libinput_get_fd(li), ep, 32, -1);
libinput_dispatch(li);
}
unsigned long timestamp () {
struct timeval t;
gettimeofday(&t, 0);
return t.tv_sec*1000 + t.tv_usec/1000;
}
int open_restricted (const char *path, int flags, void *user_data) {
int fd = open(path, O_RDONLY | O_NONBLOCK);
return fd;
}
int close_restricted (int fd, void *user_data) {
close(fd);
}
void do_left() {
system(LEFT_SWIPE);
}
void do_right() {
system(RIGHT_SWIPE);
}
void clean_old_events (struct libinput *li, int direction) {
dispatch(li);
while (1) {
struct libinput_event *ev = libinput_get_event(li);
if (ev == NULL || libinput_event_get_type(ev) != LIBINPUT_EVENT_POINTER_AXIS ||
libinput_event_pointer_get_axis_value(ev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL)*direction < THRESHOLD){
libinput_event_destroy(ev);
break;
}
libinput_event_destroy(ev);
dispatch(li);
}
}
inline void handle_pointer_motion (struct libinput_event *);
void handle_pointer_motion (struct libinput_event *ev) {
enum libinput_pointer_axis_source src = libinput_event_pointer_get_axis_source(ev);
double axis_value = libinput_event_pointer_get_axis_value(ev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
static unsigned long last_time = 0, t = 0;
t = timestamp();
if (axis_value > THRESHOLD) {
if (t > last_time+INTERVAL) {
do_right();
last_time = timestamp();
clean_old_events(libinput_event_get_context(ev), 1);
}
} else if (axis_value < -THRESHOLD) {
if (t > last_time+INTERVAL) {
do_left();
last_time = timestamp();
clean_old_events(libinput_event_get_context(ev), -1);
}
}
libinput_event_destroy(ev);
}
int main () {
int dev = scan_device(0x0b);
if (dev == -1) {
exit(-1);
}
char dev_str[50];
sprintf(dev_str, "/dev/input/event%d", dev);
struct libinput_interface interface = {
open_restricted,
close_restricted
};
struct libinput *li = libinput_path_create_context(&interface, NULL);
struct libinput_device *device = libinput_path_add_device(li, dev_str);
if (device == NULL) {
printf("get device failed, please check your permissions\n");
return -1;
}
struct libinput_event *ev;
unsigned long last_left = 0, last_right = 0, t = 0;
libinput_device_ref(device);
libinput_ref(li);
dispatch(li);
while (1) {
ev = libinput_get_event(li);
if (ev == NULL) {
libinput_event_destroy(ev);
dispatch(li);
continue;
}
switch (libinput_event_get_type(ev)) {
case LIBINPUT_EVENT_POINTER_AXIS:
handle_pointer_motion(ev);
break;
}
dispatch(li);
}
}