blob: bd29f718d64ca6d8a45b8b540d7ada0f8d702c77 [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>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050027#include <stdio.h>
28#include <string.h>
notsyncingc7c78d22018-06-06 20:26:47 +080029#include <fstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040030
31#include "../common.h"
32
33#include "minui.h"
34
35//#define _EVENT_LOGGING
36
Dees_Troy3f23d9e2013-05-03 21:04:05 +000037#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040038
39#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
40#define VIBRATOR_TIME_MS 50
41
notsyncingc7c78d22018-06-06 20:26:47 +080042#define LEDS_HAPTICS_DURATION_FILE "/sys/class/leds/vibrator/duration"
43#define LEDS_HAPTICS_ACTIVATE_FILE "/sys/class/leds/vibrator/activate"
44
Dees_Troyf7596752012-09-28 13:21:36 -040045#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040046#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040047#endif
48#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040049#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040050#endif
51#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040052#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040053#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040054
55#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
56#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
57#define ABS_MT_SLOT 0x2f
58#define ABS_MT_TOUCH_MAJOR 0x30
59#define ABS_MT_TOUCH_MINOR 0x31
60#define ABS_MT_WIDTH_MAJOR 0x32
61#define ABS_MT_WIDTH_MINOR 0x33
62#define ABS_MT_ORIENTATION 0x34
63#define ABS_MT_POSITION_X 0x35
64#define ABS_MT_POSITION_Y 0x36
65#define ABS_MT_TOOL_TYPE 0x37
66#define ABS_MT_BLOB_ID 0x38
67#define ABS_MT_TRACKING_ID 0x39
68#define ABS_MT_PRESSURE 0x3a
69#define ABS_MT_DISTANCE 0x3b
70
71enum {
72 DOWN_NOT,
73 DOWN_SENT,
74 DOWN_RELEASED,
75};
76
77struct virtualkey {
78 int scancode;
79 int centerx, centery;
80 int width, height;
81};
82
83struct position {
84 int x, y;
85 int synced;
86 struct input_absinfo xi, yi;
87};
88
89struct ev {
90 struct pollfd *fd;
91
92 struct virtualkey *vks;
93 int vk_count;
94
95 char deviceName[64];
96
97 int ignored;
98
99 struct position p, mt_p;
100 int down;
101};
102
103static struct pollfd ev_fds[MAX_DEVICES];
104static struct ev evs[MAX_DEVICES];
105static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100106static struct timeval lastInputStat;
Ethan Yonker58f21322018-08-24 11:17:36 -0500107static time_t lastInputMTime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100108static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400109
110static inline int ABS(int x) {
111 return x<0?-x:x;
112}
113
notsyncingc7c78d22018-06-06 20:26:47 +0800114int write_to_file(const std::string& fn, const std::string& line) {
115 FILE *file;
116 file = fopen(fn.c_str(), "w");
117 if (file != NULL) {
118 fwrite(line.c_str(), line.size(), 1, file);
119 fclose(file);
120 return 0;
121 }
122 LOGI("Cannot find file %s\n", fn.c_str());
123 return -1;
124}
125
Dees_Troy51a0e822012-09-05 15:24:24 -0400126int vibrate(int timeout_ms)
127{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000128 if (timeout_ms > 10000) timeout_ms = 1000;
129
notsyncingc7c78d22018-06-06 20:26:47 +0800130 if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) {
131 write_to_file(LEDS_HAPTICS_DURATION_FILE, std::to_string(timeout_ms));
132 write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1");
133 } else
134 write_to_file(VIBRATOR_TIMEOUT_FILE, std::to_string(timeout_ms));
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
136 return 0;
137}
138
139/* Returns empty tokens */
140static char *vk_strtok_r(char *str, const char *delim, char **save_str)
141{
142 if(!str)
143 {
144 if(!*save_str)
145 return NULL;
146
147 str = (*save_str) + 1;
148 }
149 *save_str = strpbrk(str, delim);
150
151 if (*save_str)
152 **save_str = '\0';
153
154 return str;
155}
156
157static int vk_init(struct ev *e)
158{
159 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
160 char vks[2048], *ts = NULL;
161 ssize_t len;
162 int vk_fd;
163 int i;
164
165 e->vk_count = 0;
166
167 len = strlen(vk_path);
168 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
169 if (len <= 0)
170 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600171 LOGE("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400172 return -1;
173 }
174#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000175 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400176#endif
177
Flemmardf4674612014-01-10 09:14:44 +0100178#ifdef WHITELIST_INPUT
179 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
180 {
181 e->ignored = 1;
182 }
183#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500184#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500185 // Blacklist these "input" devices, use TW_INPUT_BLACKLIST := "accelerometer\x0atest1\x0atest2" using the \x0a as a separator between input devices
Ethan Yonker3c4ac3b2014-08-14 11:15:32 -0500186 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400187 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600188 LOGI("Blacklisting input device: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 e->ignored = 1;
190 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500191#else
192 char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST));
193 char* blacklist = strtok(bl, "\n");
194
195 while (blacklist != NULL) {
196 if (strcmp(e->deviceName, blacklist) == 0) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600197 LOGI("Blacklisting input device: %s\n", blacklist);
Ethan Yonker5742a402014-08-12 14:52:49 -0500198 e->ignored = 1;
199 }
200 blacklist = strtok(NULL, "\n");
201 }
202 free(bl);
203#endif
Flemmardf4674612014-01-10 09:14:44 +0100204#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
206 strcat(vk_path, e->deviceName);
207
208 // Some devices split the keys from the touchscreen
209 e->vk_count = 0;
210 vk_fd = open(vk_path, O_RDONLY);
211 if (vk_fd >= 0)
212 {
213 len = read(vk_fd, vks, sizeof(vks)-1);
214 close(vk_fd);
215 if (len <= 0)
216 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500217
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500219
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 /* Parse a line like:
221 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
222 */
223 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
224 if (*ts == ':')
225 ++e->vk_count;
226 }
227
228 if (e->vk_count % 6) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600229 LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400230 }
231 e->vk_count /= 6;
232 if (e->vk_count <= 0)
233 return -1;
234
235 e->down = DOWN_NOT;
236 }
237
238 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
239 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
240 e->p.synced = 0;
241#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000242 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 -0400243#endif
244
245 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
246 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
247 e->mt_p.synced = 0;
248#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000249 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 -0400250#endif
251
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100252 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400253
254 for (i = 0; i < e->vk_count; ++i) {
255 char *token[6];
256 int j;
257
258 for (j = 0; j < 6; ++j) {
259 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
260 }
261
262 if (strcmp(token[0], "0x01") != 0) {
263 /* Java does string compare, so we do too. */
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600264 LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400265 continue;
266 }
267
268 e->vks[i].scancode = strtol(token[1], NULL, 0);
269 e->vks[i].centerx = strtol(token[2], NULL, 0);
270 e->vks[i].centery = strtol(token[3], NULL, 0);
271 e->vks[i].width = strtol(token[4], NULL, 0);
272 e->vks[i].height = strtol(token[5], NULL, 0);
273 }
274
275 return 0;
276}
277
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100278#define BITS_PER_LONG (sizeof(long) * 8)
279#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
280#define OFF(x) ((x)%BITS_PER_LONG)
281#define LONG(x) ((x)/BITS_PER_LONG)
282#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100283
284// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
285// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500286static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100287{
288 if(has_mouse)
289 return;
290
291 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
292 memset(bit, 0, sizeof(bit));
293 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
294
Vojtech Bocek971d3182014-02-20 21:43:28 +0100295 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100296 return;
297
298 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100299 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
300 return;
301
302 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
303 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
304 return;
305
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600306 LOGI("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100307 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100308}
309
310int ev_has_mouse(void)
311{
312 return has_mouse;
313}
314
Dees_Troy51a0e822012-09-05 15:24:24 -0400315int ev_init(void)
316{
317 DIR *dir;
318 struct dirent *de;
319 int fd;
320
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100321 has_mouse = 0;
322
Dees_Troy51a0e822012-09-05 15:24:24 -0400323 dir = opendir("/dev/input");
324 if(dir != 0) {
325 while((de = readdir(dir))) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600326#ifdef _EVENT_LOGGING
327 fprintf(stderr,"/dev/input/%s\n", de->d_name);
328#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400329 if(strncmp(de->d_name,"event",5)) continue;
330 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
331 if(fd < 0) continue;
332
333 ev_fds[ev_count].fd = fd;
334 ev_fds[ev_count].events = POLLIN;
335 evs[ev_count].fd = &ev_fds[ev_count];
336
337 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500338 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400339
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500340 if (!evs[ev_count].ignored)
341 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100342
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 ev_count++;
344 if(ev_count == MAX_DEVICES) break;
345 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100346 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400347 }
348
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100349 struct stat st;
350 if(stat("/dev/input", &st) >= 0)
351 lastInputMTime = st.st_mtime;
352 gettimeofday(&lastInputStat, NULL);
353
Dees_Troy51a0e822012-09-05 15:24:24 -0400354 return 0;
355}
356
357void ev_exit(void)
358{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100359 while (ev_count-- > 0) {
360 if (evs[ev_count].vk_count) {
361 free(evs[ev_count].vks);
362 evs[ev_count].vk_count = 0;
363 }
364 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400365 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100366 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367}
368
Ethan Yonker58f21322018-08-24 11:17:36 -0500369/*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
Dees_Troy51a0e822012-09-05 15:24:24 -0400370{
371 int screen_pos;
372
373 if (info->minimum == info->maximum)
374 return 0;
375
376 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
377 return (screen_pos >= 0 && screen_pos < screen_size);
Ethan Yonker58f21322018-08-24 11:17:36 -0500378}*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400379
Dees_Troyb7ecc092013-08-24 12:15:41 +0000380static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400381{
382 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
383 {
384 // In this case, we assume the screen dimensions are the same.
385 *x = p->x;
386 *y = p->y;
387 return 0;
388 }
389
Dees_Troyb7ecc092013-08-24 12:15:41 +0000390#ifdef _EVENT_LOGGING
391 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
392#endif
393
Dees_Troy51a0e822012-09-05 15:24:24 -0400394#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000395 int fb_width = gr_fb_width();
396 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400397#else
398 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000399 int fb_width = gr_fb_height();
400 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400401#endif
402
403 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
404 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
405
406 if (*x >= 0 && *x < fb_width &&
407 *y >= 0 && *y < fb_height)
408 {
409 return 0;
410 }
411
412 return 1;
413}
414
415/* Translate a virtual key in to a real key event, if needed */
416/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000417static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400418{
419 static int downX = -1, downY = -1;
420 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100421 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 static int lastWasSynReport = 0;
423 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000424 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 -0400425 int i;
426 int x, y;
427
428 // This is used to ditch useless event handlers, like an accelerometer
429 if (e->ignored) return 1;
430
431 if (ev->type == EV_REL && ev->code == REL_Z)
432 {
433 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
434#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000435 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400436#endif
437 e->ignored = 1;
438 return 1;
439 }
440
441#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000442 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 -0400443#endif
444
445 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
446 if (ev->type == EV_KEY) {
447 return 0;
448 }
449
450 if (ev->type == EV_ABS) {
451 switch (ev->code) {
452
453 case ABS_X: //00
454 e->p.synced |= 0x01;
455 e->p.x = ev->value;
456#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000457 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458#endif
459 break;
460
461 case ABS_Y: //01
462 e->p.synced |= 0x02;
463 e->p.y = ev->value;
464#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000465 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400466#endif
467 break;
468
469 case ABS_MT_POSITION: //2a
470 e->mt_p.synced = 0x03;
471 if (ev->value == (1 << 31))
472 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600473#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 e->mt_p.x = 0;
475 e->mt_p.y = 0;
476 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600477#endif
478#ifdef _EVENT_LOGGING
479#ifndef TW_IGNORE_MT_POSITION_0
480 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
481#else
482 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
483#endif
484#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400485 }
486 else
487 {
488 lastWasSynReport = 0;
489 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
490 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600491#ifdef _EVENT_LOGGING
492 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x: %d and y: %d and lastWasSynReport to 0\n", e->deviceName, ev->value, (ev->value & 0x7FFF0000) >> 16, (ev->value & 0xFFFF));
493#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 }
495 break;
496
497 case ABS_MT_TOUCH_MAJOR: //30
498 if (ev->value == 0)
499 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800500#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 // We're in a touch release, although some devices will still send positions as well
502 e->mt_p.x = 0;
503 e->mt_p.y = 0;
504 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800505#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400506 }
507#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000508 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400509#endif
510 break;
511
512 case ABS_MT_PRESSURE: //3a
513 if (ev->value == 0)
514 {
515 // We're in a touch release, although some devices will still send positions as well
516 e->mt_p.x = 0;
517 e->mt_p.y = 0;
518 touchReleaseOnNextSynReport = 1;
519 }
520#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000521 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522#endif
523 break;
524
525 case ABS_MT_POSITION_X: //35
526 e->mt_p.synced |= 0x01;
527 e->mt_p.x = ev->value;
528#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530#endif
531 break;
532
533 case ABS_MT_POSITION_Y: //36
534 e->mt_p.synced |= 0x02;
535 e->mt_p.y = ev->value;
536#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000537 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400538#endif
539 break;
540
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000542#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000544#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400545 break;
546
547 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000548#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000549 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000550#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400551 break;
552
553 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000554#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000555 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000556#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 break;
558
Dees_Troyd86400b2013-05-13 19:04:35 +0000559 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500560#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
561#ifdef _EVENT_LOGGING
562 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
563#endif
564 return 1;
565#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000566 if (ev->value < 0) {
567 e->mt_p.x = 0;
568 e->mt_p.y = 0;
569 touchReleaseOnNextSynReport = 2;
570 use_tracking_id_negative_as_touch_release = 1;
571#ifdef _EVENT_LOGGING
572 if (use_tracking_id_negative_as_touch_release)
573 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
574#endif
575 }
576#ifdef _EVENT_LOGGING
577 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
578#endif
579 break;
580
581#ifdef _EVENT_LOGGING
582 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400583 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000584 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400585 return 1;
586 break;
587
588 case ABS_MT_TOOL_TYPE: //37
589 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
590 return 1;
591 break;
592
593 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400595 return 1;
596 break;
597
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000599 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 return 1;
601 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500602 case ABS_MT_SLOT:
603 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
604 return 1;
605 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400606#endif
607
608 default:
609 // This is an unhandled message, just skip it
610 return 1;
611 }
612
613 if (ev->code != ABS_MT_POSITION)
614 {
615 lastWasSynReport = 0;
616 return 1;
617 }
618 }
619
620 // Check if we should ignore the message
621 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
622 {
623 lastWasSynReport = 0;
624 return 0;
625 }
626
627#ifdef _EVENT_LOGGING
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600628 if (ev->type == EV_SYN && ev->code == SYN_REPORT)
629 printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
630 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT)
631 printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400632#endif
633
634 // Discard the MT versions
635 if (ev->code == SYN_MT_REPORT) return 0;
636
Dees_Troyd86400b2013-05-13 19:04:35 +0000637 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 -0400638 {
639 // Reset the value
640 touchReleaseOnNextSynReport = 0;
641
642 // We are a finger-up state
643 if (!discard)
644 {
645 // Report the key up
646 ev->type = EV_ABS;
647 ev->code = 0;
648 ev->value = (downX << 16) | downY;
649 }
650 downX = -1;
651 downY = -1;
652 if (discard)
653 {
654 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100655
656 // Send the keyUp event
657 ev->type = EV_KEY;
658 ev->code = last_virt_key;
659 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400660 }
661 return 0;
662 }
663 lastWasSynReport = 1;
664
665 // Retrieve where the x,y position is
666 if (e->p.synced & 0x03)
667 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000668 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400669 }
670 else if (e->mt_p.synced & 0x03)
671 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000672 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400673 }
674 else
675 {
676 // We don't have useful information to convey
677 return 1;
678 }
679
680#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
681 x ^= y;
682 y ^= x;
683 x ^= y;
684#endif
685#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000686 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400687#endif
688#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000689 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400690#endif
691
692#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000693 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400694#endif
695
696 // Clear the current sync states
697 e->p.synced = e->mt_p.synced = 0;
698
699 // If we have nothing useful to report, skip it
700 if (x == -1 || y == -1) return 1;
701
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500702 // Special case, we'll ignore touches on 0,0 because it usually means
703 // that we received extra data after our last sync and x and y were
704 // reset to 0. We should not be using 0,0 anyway.
705 if (x == 0 && y == 0)
706 return 1;
707
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 // On first touch, see if we're at a virtual key
709 if (downX == -1)
710 {
711 // Attempt mapping to virtual key
712 for (i = 0; i < e->vk_count; ++i)
713 {
714 int xd = ABS(e->vks[i].centerx - x);
715 int yd = ABS(e->vks[i].centery - y);
716
717 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
718 {
719 ev->type = EV_KEY;
720 ev->code = e->vks[i].scancode;
721 ev->value = 1;
722
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100723 last_virt_key = e->vks[i].scancode;
724
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 vibrate(VIBRATOR_TIME_MS);
726
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500727 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 // and make sure we don't come back into this area
729 discard = 1;
730 downX = 0;
731 return 0;
732 }
733 }
734 }
735
736 // If we were originally a button press, discard this event
737 if (discard)
738 {
739 return 1;
740 }
741
742 // Record where we started the touch for deciding if this is a key or a scroll
743 downX = x;
744 downY = y;
745
746 ev->type = EV_ABS;
747 ev->code = 1;
748 ev->value = (x << 16) | y;
749 return 0;
750}
751
thatde72b6d2015-02-08 08:55:00 +0100752int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400753{
754 int r;
755 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100756 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400757
Ethan Yonkere13fa632015-01-27 11:30:03 -0600758 gettimeofday(&curr, NULL);
759 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
760 {
761 struct stat st;
762 stat("/dev/input", &st);
763 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100764 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600765 LOGI("Reloading input devices\n");
Ethan Yonkere13fa632015-01-27 11:30:03 -0600766 ev_exit();
767 ev_init();
768 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100769 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600770 lastInputStat = curr;
771 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100772
thatde72b6d2015-02-08 08:55:00 +0100773 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400774
Ethan Yonkere13fa632015-01-27 11:30:03 -0600775 if(r > 0) {
776 for(n = 0; n < ev_count; n++) {
777 if(ev_fds[n].revents & POLLIN) {
778 r = read(ev_fds[n].fd, ev, sizeof(*ev));
779 if(r == sizeof(*ev)) {
780 if (!vk_modify(&evs[n], ev))
781 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 }
783 }
784 }
thatc5837f32015-02-01 01:59:43 +0100785 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600786 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400787
thatc5837f32015-02-01 01:59:43 +0100788 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400789}
790
Ethan Yonker58f21322018-08-24 11:17:36 -0500791int ev_wait(int timeout __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400792{
793 return -1;
794}
795
796void ev_dispatch(void)
797{
798 return;
799}
800
Ethan Yonker58f21322018-08-24 11:17:36 -0500801int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400802{
803 return -1;
804}