-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysendevent.c
297 lines (260 loc) · 10.4 KB
/
mysendevent.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//[12f23eddde] 20-01-23 a better timer
//[12f23eddde] 20-01-23 add time offset
//[12f23eddde] 20-01-23 touch-sync
//[12f23eddde] 20-01-24 add argprase
//[12f23eddde] 20-01-25 update start cond with press/release
//[12f23eddde] 20-01-25 time correction based on release time
//[12f23eddde] 20-01-26 add support for Huawei devices (experimental)
//[12f23eddde] 20-01-28 fixed timing bug on Huawei devices (not verified)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/ioctl.h>
//#include <linux/input.h> // this does not compile
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <sys/time.h> // Modified
// from <linux/input.h>
typedef uint32_t __u32;
typedef uint16_t __u16;
typedef __signed__ int __s32;
struct input_event {
struct timeval time;
__u16 type;
__u16 code;
__u32 value;
};
#define MICROSEC 1000000
#define TRUNC(x) ( x > 0 ? x : 0 ) // avoid uint overflow
// begin <linux/input.h>
#define EVIOCGVERSION _IOR('E', 0x01, int) /* get driver version */
#define EVIOCGID _IOR('E', 0x02, struct input_id) /* get device ID */
#define EVIOCGKEYCODE _IOR('E', 0x04, int[2]) /* get keycode */
#define EVIOCSKEYCODE _IOW('E', 0x04, int[2]) /* set keycode */
#define EVIOCGNAME(len) _IOC(_IOC_READ, 'E', 0x06, len) /* get device name */
#define EVIOCGPHYS(len) _IOC(_IOC_READ, 'E', 0x07, len) /* get physical location */
#define EVIOCGUNIQ(len) _IOC(_IOC_READ, 'E', 0x08, len) /* get unique identifier */
#define EVIOCGKEY(len) _IOC(_IOC_READ, 'E', 0x18, len) /* get global keystate */
#define EVIOCGLED(len) _IOC(_IOC_READ, 'E', 0x19, len) /* get all LEDs */
#define EVIOCGSND(len) _IOC(_IOC_READ, 'E', 0x1a, len) /* get all sounds status */
#define EVIOCGSW(len) _IOC(_IOC_READ, 'E', 0x1b, len) /* get all switch states */
#define EVIOCGBIT(ev,len) _IOC(_IOC_READ, 'E', 0x20 + ev, len) /* get event bits */
#define EVIOCGABS(abs) _IOR('E', 0x40 + abs, struct input_absinfo) /* get abs value/limits */
#define EVIOCSABS(abs) _IOW('E', 0xc0 + abs, struct input_absinfo) /* set abs value/limits */
#define EVIOCSFF _IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect)) /* send a force effect to a force feedback device */
#define EVIOCRMFF _IOW('E', 0x81, int) /* Erase a force effect */
#define EVIOCGEFFECTS _IOR('E', 0x84, int) /* Report number of effects playable at the same time */
#define EVIOCGRAB _IOW('E', 0x90, int) /* Grab/Release device */
// end <linux/input.h>
long long get_usec(struct timeval *tv_ptr){
gettimeofday(tv_ptr, NULL);
return 1000000 * tv_ptr->tv_sec + tv_ptr->tv_usec;
}
void remove_specific_chars(char* str, char c1, char c2) {
char *pr = str, *pw = str;
while (*pr) {
*pw = *pr++;
pw += (*pw != c1 && *pw != c2);
}
*pw = '\0';
}
int main(int argc, char *argv[])
{
int fd = -1;
int ret;
int version;
// Modified
bool debug = false;
bool wait_for_input = false;
struct input_event event;
struct timeval tv;
long offset = 0;
float release_timeout = 0.2;
char manufacturer = 'u';
FILE *fd_in = NULL;
// parsing arguments
printf("[mysendevent]");
int c;
while ((c = getopt (argc, argv, "t:e:o:r:m:wvh")) != -1) {
switch (c) {
case 't': // trace
fd_in = fopen(optarg, "r");
printf(" trace=%s",optarg);
break;
case 'e': // device_event
fd = open(optarg, O_RDWR);
printf(" dev=%s",optarg);
break;
case 'o': // offset
offset = strtol(optarg, NULL, 10);
printf(" offset=%ld(ms)",offset);
break;
case 'r': // release_timeout
release_timeout = strtof(optarg, NULL);
printf(" release_timeout=%.2f(ms)",release_timeout);
break;
case 'm': // manufacturer
manufacturer = optarg[0];
printf(" manufacturer=%s",optarg);
break;
case 'w': // wait_for_input
wait_for_input = true;
printf(" wait_for_input");
break;
case 'v':
debug = true;
printf(" debug");
break;
case 'h':
case '?':
default:
printf("Usage: -t <trace> -e <device_event> -o <offset> -r <release_timeout> -w -v\n");
printf(" -t trace generated by getevent -t\n");
printf(" -e event in linux IO i.e. /dev/input/event1\n");
printf(" -o offset to sendevent actions (ms)\n");
printf(" -r threshold of separating different actions\n");
printf(" -w wait for input before mysendevent\n");
printf(" -v show debug outputs\n");
return 0;
}
}
printf("\n");
// handle exceptions
if(fd < 0) {
fprintf(stderr, "could not open device event, %s\n", strerror(errno));
return 1;
}
if (ioctl(fd, EVIOCGVERSION, &version)) {
fprintf(stderr, "could not get driver version, %s\n", strerror(errno));
return 1;
}
if (fd_in == NULL) {
fprintf(stderr, "could not open trace file, %s\n", strerror(errno));
return 1;
}
char line[128];
char type[32];
char code[32];
char value[32];
// Modified
unsigned int sleep_time;
double timestamp_init = -1.0;
double timestamp_prev_release = -1.0;
double timestamp_now;
long long usec_init = -1;
int press_cnt = 0;
int release_cnt = 0;
bool is_press = false;
bool is_release = false;
bool is_first_act = true;
long long corr_offset_sum = 0;
if(wait_for_input){
printf("[mysendevent] Waiting for touch event\n");
// received input from touchscreen -> continue
read(fd, &event, sizeof(event));
printf("[mysendevent] Got touch event, waiting for 1st release\n");
}else{
// disable some features
is_first_act = false;
}
while (fgets(line, sizeof(line), fd_in) != NULL) {
// remove the characters [ and ] surrounding the timestamp
remove_specific_chars(line, '[', ']');
sscanf(line, "%lf %s %s %s", ×tamp_now, type, code, value);
// write the event to the appropriate input device
event.type = (int) strtol(type, NULL, 16);
event.code = (int) strtol(code, NULL, 16);
event.value = (uint32_t) strtoll(value, NULL, 16);
long long usec_now = get_usec(&tv);
long long sleep_time_fixed = (long long)((timestamp_now - timestamp_init) * MICROSEC) - (usec_now - usec_init);
sleep_time = TRUNC(sleep_time_fixed); // more accurate
if(manufacturer == 'u') {
// universal
// press/release code val: 0x0039
// release event val: 0xffffffff
if (event.code == (int) 0x0039) {
if (event.value == (uint32_t) 0xffffffff) {
is_release = true;
} else {
is_press = true;
}
}
} else if (manufacturer == 'h') {
// huawei
// thanks to @noricks
if (event.code == (int) 0x014a) {
if (event.value == (uint32_t) 0x00000000) {
is_release = true;
} else if (event.value == (uint32_t) 0x00000001) {
is_press = true;
}
}
}
// set init val
if(usec_init == -1){
usec_init = usec_now;
}
if(timestamp_init == -1.0){
timestamp_init = timestamp_now;
}
if (sleep_time!=0){ // 1st run
// if(debug) printf("[%4d] sleep_time = (%lf-%lf)*1000000 - (%lld - %lld) = %u (us)\n",
// release_cnt,timestamp_now,timestamp_init,usec_now,usec_init,sleep_time);
usleep(sleep_time); // sleep (us)
// set is_first_act = false when:
// flushing IO
// not first release
// > release_timeout from last release
// press_cnt == release_cnt
if(is_first_act && release_cnt > 0 && timestamp_prev_release!=-1.0
&& timestamp_now - timestamp_prev_release > release_timeout && press_cnt == release_cnt){
printf("[mysendevent] Sendevent begin, press ENTER to stop\n");
is_first_act = false;
// time correction
long long offset_final = 1000*offset - corr_offset_sum/(release_cnt);
if(debug) printf("[%4d] %lf: offset_final=%lld(ns)\n",(press_cnt+release_cnt+1)/2,timestamp_now,offset_final);
usec_init += offset_final;
}
// time correction: only press/release time is reliable
if (is_first_act && is_release){
read(fd, &event, sizeof(event));
usec_now = get_usec(&tv);
long long corr_offset = (long long)((timestamp_now - timestamp_init) * MICROSEC) - (usec_now - usec_init);
if(debug) printf("[%4d] %lf: corr_offset=%lld(ns)\n",(press_cnt+release_cnt+1)/2,timestamp_now,corr_offset);
corr_offset_sum += corr_offset;
}
}
if(is_press){
press_cnt++;
if(debug) printf("[%4d] %lf: press_cnt=%d, release_cnt=%d\n",(press_cnt+release_cnt+1)/2,timestamp_now,press_cnt,release_cnt);
is_press = false;
}
if(is_release){
release_cnt++;
if(debug) printf("[%4d] %lf: release_cnt=%d, press_cnt=%d\n",(press_cnt+release_cnt+1)/2,timestamp_now,press_cnt,release_cnt);
is_release = false; //reset later
// set timestamp on 1st release
timestamp_prev_release = timestamp_now;
}
if(!is_first_act) {
ret = write(fd, &event, sizeof(event));
if (ret < sizeof(event)) {
fprintf(stderr, "write event failed, %s\n", strerror(errno));
return -1;
}
}
// Clear temporary buffers
memset(line, 0, sizeof(line));
memset(type, 0, sizeof(type));
memset(code, 0, sizeof(code));
memset(value, 0, sizeof(value));
}
printf("[mysendevent] Sendevent stopped normally\n");
fclose(fd_in);
close(fd);
return 0;
}