blob: 7b4655d8e5430f3c59efecb881f14e149b84c89c [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <dirent.h>
21#include <sys/poll.h>
22#include <limits.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040023#include <linux/input.h>
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010024#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
27
Dees_Troy51a0e822012-09-05 15:24:24 -040028
29#include "../common.h"
30
31#include "minui.h"
32
33//#define _EVENT_LOGGING
34
Dees_Troy3f23d9e2013-05-03 21:04:05 +000035#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
38#define VIBRATOR_TIME_MS 50
39
Dees_Troyf7596752012-09-28 13:21:36 -040040#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040041#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040042#endif
43#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040044#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040045#endif
46#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040047#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040048#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040049
50#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
51#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
52#define ABS_MT_SLOT 0x2f
53#define ABS_MT_TOUCH_MAJOR 0x30
54#define ABS_MT_TOUCH_MINOR 0x31
55#define ABS_MT_WIDTH_MAJOR 0x32
56#define ABS_MT_WIDTH_MINOR 0x33
57#define ABS_MT_ORIENTATION 0x34
58#define ABS_MT_POSITION_X 0x35
59#define ABS_MT_POSITION_Y 0x36
60#define ABS_MT_TOOL_TYPE 0x37
61#define ABS_MT_BLOB_ID 0x38
62#define ABS_MT_TRACKING_ID 0x39
63#define ABS_MT_PRESSURE 0x3a
64#define ABS_MT_DISTANCE 0x3b
65
66enum {
67 DOWN_NOT,
68 DOWN_SENT,
69 DOWN_RELEASED,
70};
71
72struct virtualkey {
73 int scancode;
74 int centerx, centery;
75 int width, height;
76};
77
78struct position {
79 int x, y;
80 int synced;
81 struct input_absinfo xi, yi;
82};
83
84struct ev {
85 struct pollfd *fd;
86
87 struct virtualkey *vks;
88 int vk_count;
89
90 char deviceName[64];
91
92 int ignored;
93
94 struct position p, mt_p;
95 int down;
96};
97
98static struct pollfd ev_fds[MAX_DEVICES];
99static struct ev evs[MAX_DEVICES];
100static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100101static struct timeval lastInputStat;
102static unsigned long lastInputMTime;
103static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
105static inline int ABS(int x) {
106 return x<0?-x:x;
107}
108
109int vibrate(int timeout_ms)
110{
111 char str[20];
112 int fd;
113 int ret;
114
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000115 if (timeout_ms > 10000) timeout_ms = 1000;
116
Dees_Troy51a0e822012-09-05 15:24:24 -0400117 fd = open(VIBRATOR_TIMEOUT_FILE, O_WRONLY);
118 if (fd < 0)
119 return -1;
120
121 ret = snprintf(str, sizeof(str), "%d", timeout_ms);
122 ret = write(fd, str, ret);
123 close(fd);
124
125 if (ret < 0)
126 return -1;
127
128 return 0;
129}
130
131/* Returns empty tokens */
132static char *vk_strtok_r(char *str, const char *delim, char **save_str)
133{
134 if(!str)
135 {
136 if(!*save_str)
137 return NULL;
138
139 str = (*save_str) + 1;
140 }
141 *save_str = strpbrk(str, delim);
142
143 if (*save_str)
144 **save_str = '\0';
145
146 return str;
147}
148
149static int vk_init(struct ev *e)
150{
151 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
152 char vks[2048], *ts = NULL;
153 ssize_t len;
154 int vk_fd;
155 int i;
156
157 e->vk_count = 0;
158
159 len = strlen(vk_path);
160 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
161 if (len <= 0)
162 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000163 printf("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400164 return -1;
165 }
166#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000167 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400168#endif
169
Flemmardf4674612014-01-10 09:14:44 +0100170#ifdef WHITELIST_INPUT
171 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
172 {
173 e->ignored = 1;
174 }
175#else
Dees_Troy51a0e822012-09-05 15:24:24 -0400176 // Blacklist these "input" devices
Ethan Yonker4ee5ad72014-02-18 18:41:17 -0600177 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0 || strcmp(e->deviceName, "accelerometer") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400178 {
179 e->ignored = 1;
180 }
Flemmardf4674612014-01-10 09:14:44 +0100181#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400182
183 strcat(vk_path, e->deviceName);
184
185 // Some devices split the keys from the touchscreen
186 e->vk_count = 0;
187 vk_fd = open(vk_path, O_RDONLY);
188 if (vk_fd >= 0)
189 {
190 len = read(vk_fd, vks, sizeof(vks)-1);
191 close(vk_fd);
192 if (len <= 0)
193 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500194
Dees_Troy51a0e822012-09-05 15:24:24 -0400195 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500196
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 /* Parse a line like:
198 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
199 */
200 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
201 if (*ts == ':')
202 ++e->vk_count;
203 }
204
205 if (e->vk_count % 6) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000206 printf("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400207 }
208 e->vk_count /= 6;
209 if (e->vk_count <= 0)
210 return -1;
211
212 e->down = DOWN_NOT;
213 }
214
215 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
216 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
217 e->p.synced = 0;
218#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 printf("EV: ST minX: %d maxX: %d minY: %d maxY: %d\n", e->p.xi.minimum, e->p.xi.maximum, e->p.yi.minimum, e->p.yi.maximum);
Dees_Troy51a0e822012-09-05 15:24:24 -0400220#endif
221
222 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
223 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
224 e->mt_p.synced = 0;
225#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000226 printf("EV: MT minX: %d maxX: %d minY: %d maxY: %d\n", e->mt_p.xi.minimum, e->mt_p.xi.maximum, e->mt_p.yi.minimum, e->mt_p.yi.maximum);
Dees_Troy51a0e822012-09-05 15:24:24 -0400227#endif
228
229 e->vks = malloc(sizeof(*e->vks) * e->vk_count);
230
231 for (i = 0; i < e->vk_count; ++i) {
232 char *token[6];
233 int j;
234
235 for (j = 0; j < 6; ++j) {
236 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
237 }
238
239 if (strcmp(token[0], "0x01") != 0) {
240 /* Java does string compare, so we do too. */
Dees_Troy2673cec2013-04-02 20:22:16 +0000241 printf("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400242 continue;
243 }
244
245 e->vks[i].scancode = strtol(token[1], NULL, 0);
246 e->vks[i].centerx = strtol(token[2], NULL, 0);
247 e->vks[i].centery = strtol(token[3], NULL, 0);
248 e->vks[i].width = strtol(token[4], NULL, 0);
249 e->vks[i].height = strtol(token[5], NULL, 0);
250 }
251
252 return 0;
253}
254
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100255#define BITS_PER_LONG (sizeof(long) * 8)
256#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
257#define OFF(x) ((x)%BITS_PER_LONG)
258#define LONG(x) ((x)/BITS_PER_LONG)
259#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100260
261// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
262// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100263static void check_mouse(int fd)
264{
265 if(has_mouse)
266 return;
267
268 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
269 memset(bit, 0, sizeof(bit));
270 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
271
Vojtech Bocek971d3182014-02-20 21:43:28 +0100272 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100273 return;
274
275 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100276 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
277 return;
278
279 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
280 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
281 return;
282
283 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100284}
285
286int ev_has_mouse(void)
287{
288 return has_mouse;
289}
290
Dees_Troy51a0e822012-09-05 15:24:24 -0400291int ev_init(void)
292{
293 DIR *dir;
294 struct dirent *de;
295 int fd;
296
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100297 has_mouse = 0;
298
Dees_Troy51a0e822012-09-05 15:24:24 -0400299 dir = opendir("/dev/input");
300 if(dir != 0) {
301 while((de = readdir(dir))) {
302// fprintf(stderr,"/dev/input/%s\n", de->d_name);
303 if(strncmp(de->d_name,"event",5)) continue;
304 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
305 if(fd < 0) continue;
306
307 ev_fds[ev_count].fd = fd;
308 ev_fds[ev_count].events = POLLIN;
309 evs[ev_count].fd = &ev_fds[ev_count];
310
311 /* Load virtualkeys if there are any */
312 vk_init(&evs[ev_count]);
313
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100314 check_mouse(fd);
315
Dees_Troy51a0e822012-09-05 15:24:24 -0400316 ev_count++;
317 if(ev_count == MAX_DEVICES) break;
318 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100319 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400320 }
321
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100322 struct stat st;
323 if(stat("/dev/input", &st) >= 0)
324 lastInputMTime = st.st_mtime;
325 gettimeofday(&lastInputStat, NULL);
326
Dees_Troy51a0e822012-09-05 15:24:24 -0400327 return 0;
328}
329
330void ev_exit(void)
331{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100332 while (ev_count-- > 0) {
333 if (evs[ev_count].vk_count) {
334 free(evs[ev_count].vks);
335 evs[ev_count].vk_count = 0;
336 }
337 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400338 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100339 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400340}
341
342static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
343{
344 int screen_pos;
345
346 if (info->minimum == info->maximum)
347 return 0;
348
349 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
350 return (screen_pos >= 0 && screen_pos < screen_size);
351}
352
Dees_Troyb7ecc092013-08-24 12:15:41 +0000353static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400354{
355 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
356 {
357 // In this case, we assume the screen dimensions are the same.
358 *x = p->x;
359 *y = p->y;
360 return 0;
361 }
362
Dees_Troyb7ecc092013-08-24 12:15:41 +0000363#ifdef _EVENT_LOGGING
364 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
365#endif
366
Dees_Troy51a0e822012-09-05 15:24:24 -0400367#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000368 int fb_width = gr_fb_width();
369 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400370#else
371 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000372 int fb_width = gr_fb_height();
373 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400374#endif
375
376 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
377 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
378
379 if (*x >= 0 && *x < fb_width &&
380 *y >= 0 && *y < fb_height)
381 {
382 return 0;
383 }
384
385 return 1;
386}
387
388/* Translate a virtual key in to a real key event, if needed */
389/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000390static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400391{
392 static int downX = -1, downY = -1;
393 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100394 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400395 static int lastWasSynReport = 0;
396 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000397 static int use_tracking_id_negative_as_touch_release = 0; // On some devices, type: 3 code: 39 value: -1, aka EV_ABS ABS_MT_TRACKING_ID -1 indicates a true touch release
Dees_Troy51a0e822012-09-05 15:24:24 -0400398 int i;
399 int x, y;
400
401 // This is used to ditch useless event handlers, like an accelerometer
402 if (e->ignored) return 1;
403
404 if (ev->type == EV_REL && ev->code == REL_Z)
405 {
406 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
407#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400409#endif
410 e->ignored = 1;
411 return 1;
412 }
413
414#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000415 printf("EV: %s => type: %x code: %x value: %d\n", e->deviceName, ev->type, ev->code, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400416#endif
417
418 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
419 if (ev->type == EV_KEY) {
420 return 0;
421 }
422
423 if (ev->type == EV_ABS) {
424 switch (ev->code) {
425
426 case ABS_X: //00
427 e->p.synced |= 0x01;
428 e->p.x = ev->value;
429#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000430 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400431#endif
432 break;
433
434 case ABS_Y: //01
435 e->p.synced |= 0x02;
436 e->p.y = ev->value;
437#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000438 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400439#endif
440 break;
441
442 case ABS_MT_POSITION: //2a
443 e->mt_p.synced = 0x03;
444 if (ev->value == (1 << 31))
445 {
446 e->mt_p.x = 0;
447 e->mt_p.y = 0;
448 lastWasSynReport = 1;
449 }
450 else
451 {
452 lastWasSynReport = 0;
453 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
454 e->mt_p.y = (ev->value & 0xFFFF);
455 }
456 break;
457
458 case ABS_MT_TOUCH_MAJOR: //30
459 if (ev->value == 0)
460 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800461#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400462 // We're in a touch release, although some devices will still send positions as well
463 e->mt_p.x = 0;
464 e->mt_p.y = 0;
465 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800466#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400467 }
468#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000469 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400470#endif
471 break;
472
473 case ABS_MT_PRESSURE: //3a
474 if (ev->value == 0)
475 {
476 // We're in a touch release, although some devices will still send positions as well
477 e->mt_p.x = 0;
478 e->mt_p.y = 0;
479 touchReleaseOnNextSynReport = 1;
480 }
481#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483#endif
484 break;
485
486 case ABS_MT_POSITION_X: //35
487 e->mt_p.synced |= 0x01;
488 e->mt_p.x = ev->value;
489#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000490 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491#endif
492 break;
493
494 case ABS_MT_POSITION_Y: //36
495 e->mt_p.synced |= 0x02;
496 e->mt_p.y = ev->value;
497#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000498 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400499#endif
500 break;
501
Dees_Troy51a0e822012-09-05 15:24:24 -0400502 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000503#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000504 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000505#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 break;
507
508 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000509#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000511#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400512 break;
513
514 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000515#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000516 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000517#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400518 break;
519
Dees_Troyd86400b2013-05-13 19:04:35 +0000520 case ABS_MT_TRACKING_ID: //39
521 if (ev->value < 0) {
522 e->mt_p.x = 0;
523 e->mt_p.y = 0;
524 touchReleaseOnNextSynReport = 2;
525 use_tracking_id_negative_as_touch_release = 1;
526#ifdef _EVENT_LOGGING
527 if (use_tracking_id_negative_as_touch_release)
528 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
529#endif
530 }
531#ifdef _EVENT_LOGGING
532 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
533#endif
534 break;
535
536#ifdef _EVENT_LOGGING
537 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400538 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400540 return 1;
541 break;
542
543 case ABS_MT_TOOL_TYPE: //37
544 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
545 return 1;
546 break;
547
548 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000549 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400550 return 1;
551 break;
552
Dees_Troy51a0e822012-09-05 15:24:24 -0400553 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000554 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400555 return 1;
556 break;
557#endif
558
559 default:
560 // This is an unhandled message, just skip it
561 return 1;
562 }
563
564 if (ev->code != ABS_MT_POSITION)
565 {
566 lastWasSynReport = 0;
567 return 1;
568 }
569 }
570
571 // Check if we should ignore the message
572 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
573 {
574 lastWasSynReport = 0;
575 return 0;
576 }
577
578#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000579 if (ev->type == EV_SYN && ev->code == SYN_REPORT) printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
580 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT) printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400581#endif
582
583 // Discard the MT versions
584 if (ev->code == SYN_MT_REPORT) return 0;
585
Dees_Troyd86400b2013-05-13 19:04:35 +0000586 if (((lastWasSynReport == 1 || touchReleaseOnNextSynReport == 1) && !use_tracking_id_negative_as_touch_release) || (use_tracking_id_negative_as_touch_release && touchReleaseOnNextSynReport == 2))
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 {
588 // Reset the value
589 touchReleaseOnNextSynReport = 0;
590
591 // We are a finger-up state
592 if (!discard)
593 {
594 // Report the key up
595 ev->type = EV_ABS;
596 ev->code = 0;
597 ev->value = (downX << 16) | downY;
598 }
599 downX = -1;
600 downY = -1;
601 if (discard)
602 {
603 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100604
605 // Send the keyUp event
606 ev->type = EV_KEY;
607 ev->code = last_virt_key;
608 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 }
610 return 0;
611 }
612 lastWasSynReport = 1;
613
614 // Retrieve where the x,y position is
615 if (e->p.synced & 0x03)
616 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000617 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400618 }
619 else if (e->mt_p.synced & 0x03)
620 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000621 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400622 }
623 else
624 {
625 // We don't have useful information to convey
626 return 1;
627 }
628
629#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
630 x ^= y;
631 y ^= x;
632 x ^= y;
633#endif
634#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000635 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400636#endif
637#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000638 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400639#endif
640
641#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000642 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400643#endif
644
645 // Clear the current sync states
646 e->p.synced = e->mt_p.synced = 0;
647
648 // If we have nothing useful to report, skip it
649 if (x == -1 || y == -1) return 1;
650
651 // On first touch, see if we're at a virtual key
652 if (downX == -1)
653 {
654 // Attempt mapping to virtual key
655 for (i = 0; i < e->vk_count; ++i)
656 {
657 int xd = ABS(e->vks[i].centerx - x);
658 int yd = ABS(e->vks[i].centery - y);
659
660 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
661 {
662 ev->type = EV_KEY;
663 ev->code = e->vks[i].scancode;
664 ev->value = 1;
665
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100666 last_virt_key = e->vks[i].scancode;
667
Dees_Troy51a0e822012-09-05 15:24:24 -0400668 vibrate(VIBRATOR_TIME_MS);
669
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500670 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400671 // and make sure we don't come back into this area
672 discard = 1;
673 downX = 0;
674 return 0;
675 }
676 }
677 }
678
679 // If we were originally a button press, discard this event
680 if (discard)
681 {
682 return 1;
683 }
684
685 // Record where we started the touch for deciding if this is a key or a scroll
686 downX = x;
687 downY = y;
688
689 ev->type = EV_ABS;
690 ev->code = 1;
691 ev->value = (x << 16) | y;
692 return 0;
693}
694
Dees_Troyb7ecc092013-08-24 12:15:41 +0000695int ev_get(struct input_event *ev, unsigned dont_wait)
Dees_Troy51a0e822012-09-05 15:24:24 -0400696{
697 int r;
698 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100699 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400700
701 do {
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100702 gettimeofday(&curr, NULL);
703 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
704 {
705 struct stat st;
706 stat("/dev/input", &st);
707 if (st.st_mtime > lastInputMTime)
708 {
709 LOGI("Reloading input devices\n");
710 ev_exit();
711 ev_init();
712 lastInputMTime = st.st_mtime;
713 }
714 lastInputStat = curr;
715 }
716
717 r = poll(ev_fds, ev_count, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400718
719 if(r > 0) {
720 for(n = 0; n < ev_count; n++) {
721 if(ev_fds[n].revents & POLLIN) {
722 r = read(ev_fds[n].fd, ev, sizeof(*ev));
723 if(r == sizeof(*ev)) {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000724 if (!vk_modify(&evs[n], ev))
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 return 0;
726 }
727 }
728 }
729 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100730
731 usleep(1000);
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 } while(dont_wait == 0);
733
734 return -1;
735}
736
737int ev_wait(int timeout)
738{
739 return -1;
740}
741
742void ev_dispatch(void)
743{
744 return;
745}
746
747int ev_get_input(int fd, short revents, struct input_event *ev)
748{
749 return -1;
750}