forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
420 lines (342 loc) · 9.42 KB
/
input.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <pthread.h>
#include <dirent.h>
#include "input.h"
#include "framebuffer.h"
#include "util.h"
#include "log.h"
#define MAX_DEVICES 16
// for touch calculation
static const int screen_res[] = { 800, 1280 };
static struct pollfd ev_fds[MAX_DEVICES];
static unsigned ev_count = 0;
static volatile int input_run = 0;
static int key_queue[10];
static int8_t key_itr = 10;
static pthread_mutex_t key_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t touch_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_t input_thread;
static touch_event mt_events[10];
static int mt_slot = 0;
static int switch_xy = 0;
static int mt_range_x[2] = { 0 };
static int mt_range_y[2] = { 0 };
struct handler_list_it
{
touch_handler *handler;
struct handler_list_it *prev;
struct handler_list_it *next;
};
typedef struct handler_list_it handler_list_it;
typedef struct
{
int handlers_mode;
handler_list_it *handlers;
} handlers_ctx;
static handler_list_it *mt_handlers = NULL;
static handlers_ctx **inactive_ctx = NULL;
static int mt_handlers_mode = HANDLERS_FIRST;
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
#define BIT(nr) (1UL << (nr))
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
#define BITS_PER_BYTE 8
#define BITS_PER_LONG (sizeof(long) * BITS_PER_BYTE)
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
static void get_abs_min_max(int fd)
{
int abs[5];
if(ioctl(fd, EVIOCGABS(ABS_MT_POSITION_X), abs) >= 0)
memcpy(mt_range_x, abs+1, 2*sizeof(int));
if(ioctl(fd, EVIOCGABS(ABS_MT_POSITION_Y), abs) >= 0)
memcpy(mt_range_y, abs+1, 2*sizeof(int));
switch_xy = (mt_range_x[1] > mt_range_y[1]);
if(switch_xy)
{
memcpy(abs, mt_range_x, 2*sizeof(int));
memcpy(mt_range_x, mt_range_y, 2*sizeof(int));
memcpy(mt_range_y, abs, 2*sizeof(int));
}
}
static int ev_init(void)
{
DIR *dir;
struct dirent *de;
int fd;
long absbit[BITS_TO_LONGS(ABS_CNT)];
ev_count = 0;
dir = opendir("/dev/input");
if(!dir)
return -1;
while((de = readdir(dir)))
{
if(strncmp(de->d_name,"event",5))
continue;
fd = openat(dirfd(dir), de->d_name, O_RDONLY);
if(fd < 0)
continue;
ev_fds[ev_count].fd = fd;
ev_fds[ev_count].events = POLLIN;
if (ioctl(fd, EVIOCGBIT(EV_ABS, ABS_CNT), absbit) >= 0)
{
if ((absbit[BIT_WORD(ABS_MT_POSITION_X)] & BIT_MASK(ABS_MT_POSITION_X)) &&
(absbit[BIT_WORD(ABS_MT_POSITION_Y)] & BIT_MASK(ABS_MT_POSITION_Y)))
{
get_abs_min_max(fd);
}
}
ev_count++;
if(ev_count == MAX_DEVICES) break;
}
closedir(dir);
return 0;
}
static void ev_exit(void)
{
while (ev_count > 0) {
close(ev_fds[--ev_count].fd);
}
}
static int ev_get(struct input_event *ev, unsigned dont_wait)
{
int r;
unsigned n;
do {
r = poll(ev_fds, ev_count, dont_wait ? 0 : -1);
if(r > 0) {
for(n = 0; n < ev_count; n++) {
if(ev_fds[n].revents & POLLIN) {
r = read(ev_fds[n].fd, ev, sizeof(*ev));
if(r == sizeof(*ev)) return 0;
}
}
}
} while(dont_wait == 0);
return -1;
}
#define IS_KEY_HANDLED(key) (key >= KEY_VOLUMEDOWN && key <= KEY_POWER)
static void handle_key_event(struct input_event *ev)
{
if(ev->value != 0 || !IS_KEY_HANDLED(ev->code))
return;
pthread_mutex_lock(&key_mutex);
if(key_itr > 0)
key_queue[--key_itr] = ev->code;
pthread_mutex_unlock(&key_mutex);
}
static int calc_mt_pos(int val, int *range, int d_max)
{
int res = ((val-range[0])*100);
res /= (range[1]-range[0]);
return (res*d_max)/100;
}
static inline int64_t get_us_diff(struct timeval now, struct timeval prev)
{
return ((int64_t)(now.tv_sec - prev.tv_sec))*1000000+
(now.tv_usec - prev.tv_usec);
}
static void handle_touch_event(struct input_event *ev)
{
// SYN_REPORT, send events to handlers
if(ev->type == EV_SYN && ev->code == SYN_REPORT)
{
pthread_mutex_lock(&touch_mutex);
int has_handlers = (mt_handlers != NULL);
pthread_mutex_unlock(&touch_mutex);
if(!has_handlers)
return;
uint32_t i;
touch_handler *h;
handler_list_it *it;
for(i = 0; i < ARRAY_SIZE(mt_events); ++i)
{
mt_events[i].us_diff = get_us_diff(ev->time, mt_events[i].time);
mt_events[i].time = ev->time;
if(!mt_events[i].changed)
continue;
it = mt_handlers;
while(it)
{
h = it->handler;
if((*h->callback)(&mt_events[i], h->data) == 0 && mt_handlers_mode == HANDLERS_FIRST)
break;
it = it->next;
}
mt_events[i].changed = 0;
}
return;
}
// ABS events
switch(ev->code)
{
case ABS_MT_SLOT:
if(ev->value < (int)ARRAY_SIZE(mt_events))
mt_slot = ev->value;
break;
case ABS_MT_TRACKING_ID:
{
if(ev->value != -1)
{
mt_events[mt_slot].id = ev->value;
mt_events[mt_slot].changed |= TCHNG_ADDED;
}
else
mt_events[mt_slot].changed |= TCHNG_REMOVED;
break;
}
case ABS_MT_POSITION_X:
case ABS_MT_POSITION_Y:
{
if((ev->code == ABS_MT_POSITION_X) ^ (switch_xy != 0))
{
mt_events[mt_slot].x = calc_mt_pos(ev->value, mt_range_x, screen_res[0]);
if(switch_xy)
mt_events[mt_slot].x = screen_res[0] - mt_events[mt_slot].x;
}
else
mt_events[mt_slot].y = calc_mt_pos(ev->value, mt_range_y, screen_res[1]);
mt_events[mt_slot].changed |= TCHNG_POS;
break;
}
}
}
void add_touch_handler(touch_callback callback, void *data)
{
touch_handler *handler = malloc(sizeof(touch_handler));
handler->data = data;
handler->callback = callback;
handler_list_it *new_it = malloc(sizeof(handler_list_it));
memset(new_it, 0, sizeof(handler_list_it));
new_it->handler = handler;
pthread_mutex_lock(&touch_mutex);
handler_list_it **it = &mt_handlers;
while(*it)
{
if(!(*it)->next)
new_it->prev = *it;
it = &((*it)->next);
}
*it = new_it;
pthread_mutex_unlock(&touch_mutex);
}
void rm_touch_handler(touch_callback callback, void *data)
{
pthread_mutex_lock(&touch_mutex);
handler_list_it *it = mt_handlers;
while(it)
{
if(it->handler->callback != callback || it->handler->data != data)
{
it = it->next;
continue;
}
if(it->prev)
it->prev->next = it->next;
if(it->next)
it->next->prev = it->prev;
if(it == mt_handlers)
mt_handlers = it->next;
free(it->handler);
free(it);
break;
}
pthread_mutex_unlock(&touch_mutex);
}
void set_touch_handlers_mode(int mode)
{
mt_handlers_mode = mode;
}
static void *input_thread_work(void *cookie)
{
ev_init();
struct input_event ev;
memset(mt_events, 0, sizeof(mt_events));
key_itr = 10;
mt_slot = 0;
int res;
while(input_run)
{
while(ev_get(&ev, 1) == 0)
{
switch(ev.type)
{
case EV_KEY:
handle_key_event(&ev);
break;
case EV_ABS:
case EV_SYN:
handle_touch_event(&ev);
break;
}
}
usleep(10000);
}
ev_exit();
pthread_exit(NULL);
return NULL;
}
int get_last_key(void)
{
int res = -1;
pthread_mutex_lock(&key_mutex);
if(key_itr != 10)
res = key_queue[key_itr++];
pthread_mutex_unlock(&key_mutex);
return res;
}
int wait_for_key(void)
{
int res = -1;
while(res == -1)
{
res = get_last_key();
usleep(10000);
}
return res;
}
void start_input_thread(void)
{
if(input_run)
return;
input_run = 1;
pthread_create(&input_thread, NULL, input_thread_work, NULL);
}
void stop_input_thread(void)
{
if(!input_run)
return;
input_run = 0;
pthread_join(input_thread, NULL);
}
void input_push_context(void)
{
handlers_ctx *ctx = malloc(sizeof(handlers_ctx));
memset(ctx, 0, sizeof(handlers_ctx));
pthread_mutex_lock(&touch_mutex);
ctx->handlers_mode = mt_handlers_mode;
ctx->handlers = mt_handlers;
mt_handlers_mode = HANDLERS_FIRST;
mt_handlers = NULL;
pthread_mutex_unlock(&touch_mutex);
list_add(ctx, &inactive_ctx);
}
void input_pop_context(void)
{
if(!inactive_ctx)
return;
int idx = list_item_count(inactive_ctx)-1;
handlers_ctx *ctx = inactive_ctx[idx];
pthread_mutex_lock(&touch_mutex);
mt_handlers_mode = ctx->handlers_mode;
mt_handlers = ctx->handlers;
pthread_mutex_unlock(&touch_mutex);
list_rm(ctx, &inactive_ctx, &free);
}