forked from ddvk/remarkable-touchgestures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyinjector.c
264 lines (224 loc) · 7.64 KB
/
keyinjector.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <time.h>
#include "ui.h"
#include "keyinjector.h"
#include "gestures.h"
#include "gesture_definition.h"
#include "config.h"
enum Key {Left=105, Right=106, Home=102,Power=116};
enum TouchStatus {SwipeOnly, SwipeAndTouch, Disabled};
static enum TouchStatus touch_status = SwipeOnly;
int f;
int w;
void injector_init() {
f = open(BUTTONS,O_WRONLY);
if (!f){
fprintf(stderr, "cannot open buttons");
exit(1);
}
/* w = open(WACOM, O_WRONLY); */
/* if (!w){ */
/* fprintf(stderr, "cannot open digitizer"); */
/* exit(1); */
/* } */
}
extern bool verbose;
void press_button(int code);
void press_pen(int x, int y, long time);
void move_pen(int x, int y, long time);
void interpret_gesture(struct Gesture *g){
if (g->type == TwoTapWide) {
touch_status = ((int)touch_status + 1) % 3; //cycle the states
switch(touch_status) {
case SwipeOnly:
show("swipe only");
break;
case Disabled:
show("touch disabled");
break;
case SwipeAndTouch:
show("swipe and touch");
break;
}
return;
}
time_t rawtime;
time(&rawtime);
switch(g->type){
case SwipeDownLong:
{
struct tm* timeinfo = localtime(&rawtime);
char buffer[100];
strftime(buffer,sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
show(buffer);
}
break;
default:
break;
}
if (touch_status == Disabled)
return;
//require touch enabled
switch(g->type){
case TapLeft:
if(touch_status != SwipeAndTouch)
break;
// fall through
case SwipeRight:
press_button(Left);
break;
case TapRight:
if(touch_status != SwipeAndTouch)
break;
// fall through
case SwipeLeft:
press_button(Right);
break;
case SwipeUpLong:
if (verbose)
printf("execute custom script\n");
system("~/scripts/swipeup.sh");
break;
default:
break;
}
}
//todo: wip
void move_pen(int x, int y, long time)
{
struct input_event key_input_event;
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_X;
key_input_event.value = x;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_Y;
key_input_event.value = y;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_DISTANCE;
key_input_event.value = 16;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = 24;
key_input_event.value = 3000;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
}
void press_pen(int x, int y, long time){
struct input_event key_input_event;
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = 1;
key_input_event.code = 320;
key_input_event.value = 1;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_X;
key_input_event.value = x;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_Y;
key_input_event.value = y;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_DISTANCE;
key_input_event.value = 16;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = 1; //touch
key_input_event.code = 330;
key_input_event.value = 1;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_X;
key_input_event.value = x;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = ABS_Y;
key_input_event.value = y;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = 24;
key_input_event.value = 3300;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = 25;
key_input_event.value = 21;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = 26;
key_input_event.value = 800;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_ABS;
key_input_event.code = 27;
key_input_event.value = -100;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
key_input_event.time.tv_sec = time;
write(w, &key_input_event,sizeof(key_input_event));
}
void press_button(int code){
struct input_event key_input_event;
memset(&key_input_event, 0, sizeof(key_input_event));
debug_print("inject button %d \n", code);
key_input_event.type = EV_KEY;
key_input_event.code = code;
key_input_event.value = 1;
write(f, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
write(f, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_KEY;
key_input_event.code = code;
write(f, &key_input_event,sizeof(key_input_event));
memset(&key_input_event, 0, sizeof(key_input_event));
key_input_event.type = EV_SYN;
key_input_event.code = SYN_REPORT;
write(f, &key_input_event,sizeof(key_input_event));
}