blob: 955acf31f1d3920482a160f73078753031f0a5a6 [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
Mohd Farazbc576cd2020-05-30 21:06:32 +053031#ifdef USE_QTI_HAPTICS
32#include <android/hardware/vibrator/1.2/IVibrator.h>
33#endif
34
Dees_Troy51a0e822012-09-05 15:24:24 -040035#include "../common.h"
36
37#include "minui.h"
38
39//#define _EVENT_LOGGING
40
Dees_Troy3f23d9e2013-05-03 21:04:05 +000041#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040042
43#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
44#define VIBRATOR_TIME_MS 50
45
notsyncingc7c78d22018-06-06 20:26:47 +080046#define LEDS_HAPTICS_DURATION_FILE "/sys/class/leds/vibrator/duration"
47#define LEDS_HAPTICS_ACTIVATE_FILE "/sys/class/leds/vibrator/activate"
48
Dees_Troyf7596752012-09-28 13:21:36 -040049#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040050#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040051#endif
52#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040053#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040054#endif
55#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040056#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040057#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040058
59#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
60#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
61#define ABS_MT_SLOT 0x2f
62#define ABS_MT_TOUCH_MAJOR 0x30
63#define ABS_MT_TOUCH_MINOR 0x31
64#define ABS_MT_WIDTH_MAJOR 0x32
65#define ABS_MT_WIDTH_MINOR 0x33
66#define ABS_MT_ORIENTATION 0x34
67#define ABS_MT_POSITION_X 0x35
68#define ABS_MT_POSITION_Y 0x36
69#define ABS_MT_TOOL_TYPE 0x37
70#define ABS_MT_BLOB_ID 0x38
71#define ABS_MT_TRACKING_ID 0x39
72#define ABS_MT_PRESSURE 0x3a
73#define ABS_MT_DISTANCE 0x3b
74
75enum {
76 DOWN_NOT,
77 DOWN_SENT,
78 DOWN_RELEASED,
79};
80
81struct virtualkey {
82 int scancode;
83 int centerx, centery;
84 int width, height;
85};
86
87struct position {
88 int x, y;
89 int synced;
90 struct input_absinfo xi, yi;
91};
92
93struct ev {
94 struct pollfd *fd;
95
96 struct virtualkey *vks;
97 int vk_count;
98
99 char deviceName[64];
100
101 int ignored;
102
103 struct position p, mt_p;
104 int down;
105};
106
107static struct pollfd ev_fds[MAX_DEVICES];
108static struct ev evs[MAX_DEVICES];
109static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100110static struct timeval lastInputStat;
Ethan Yonker58f21322018-08-24 11:17:36 -0500111static time_t lastInputMTime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100112static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400113
114static inline int ABS(int x) {
115 return x<0?-x:x;
116}
117
notsyncingc7c78d22018-06-06 20:26:47 +0800118int write_to_file(const std::string& fn, const std::string& line) {
119 FILE *file;
120 file = fopen(fn.c_str(), "w");
121 if (file != NULL) {
122 fwrite(line.c_str(), line.size(), 1, file);
123 fclose(file);
124 return 0;
125 }
126 LOGI("Cannot find file %s\n", fn.c_str());
127 return -1;
128}
129
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400130#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400131int vibrate(int timeout_ms)
132{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000133 if (timeout_ms > 10000) timeout_ms = 1000;
Dees Troy9a4d7402019-03-21 14:17:28 -0400134 char tout[6];
135 sprintf(tout, "%i", timeout_ms);
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000136
Mohd Farazbc576cd2020-05-30 21:06:32 +0530137#ifdef USE_QTI_HAPTICS
138 android::sp<android::hardware::vibrator::V1_2::IVibrator> vib = android::hardware::vibrator::V1_2::IVibrator::getService();
139 if (vib != nullptr) {
140 vib->on((uint32_t)timeout_ms);
141 }
142#else
notsyncingc7c78d22018-06-06 20:26:47 +0800143 if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) {
Dees Troy9a4d7402019-03-21 14:17:28 -0400144 write_to_file(LEDS_HAPTICS_DURATION_FILE, tout);
notsyncingc7c78d22018-06-06 20:26:47 +0800145 write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1");
146 } else
Dees Troy9a4d7402019-03-21 14:17:28 -0400147 write_to_file(VIBRATOR_TIMEOUT_FILE, tout);
Mohd Farazbc576cd2020-05-30 21:06:32 +0530148#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400149 return 0;
150}
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400151#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400152
153/* Returns empty tokens */
154static char *vk_strtok_r(char *str, const char *delim, char **save_str)
155{
156 if(!str)
157 {
158 if(!*save_str)
159 return NULL;
160
161 str = (*save_str) + 1;
162 }
163 *save_str = strpbrk(str, delim);
164
165 if (*save_str)
166 **save_str = '\0';
167
168 return str;
169}
170
171static int vk_init(struct ev *e)
172{
173 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
174 char vks[2048], *ts = NULL;
175 ssize_t len;
176 int vk_fd;
177 int i;
178
179 e->vk_count = 0;
180
181 len = strlen(vk_path);
182 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
183 if (len <= 0)
184 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600185 LOGE("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400186 return -1;
187 }
188#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000189 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400190#endif
191
Flemmardf4674612014-01-10 09:14:44 +0100192#ifdef WHITELIST_INPUT
193 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
194 {
195 e->ignored = 1;
196 }
197#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500198#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500199 // 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 -0500200 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400201 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600202 LOGI("Blacklisting input device: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 e->ignored = 1;
204 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500205#else
206 char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST));
207 char* blacklist = strtok(bl, "\n");
208
209 while (blacklist != NULL) {
210 if (strcmp(e->deviceName, blacklist) == 0) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600211 LOGI("Blacklisting input device: %s\n", blacklist);
Ethan Yonker5742a402014-08-12 14:52:49 -0500212 e->ignored = 1;
213 }
214 blacklist = strtok(NULL, "\n");
215 }
216 free(bl);
217#endif
Flemmardf4674612014-01-10 09:14:44 +0100218#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400219
220 strcat(vk_path, e->deviceName);
221
222 // Some devices split the keys from the touchscreen
223 e->vk_count = 0;
224 vk_fd = open(vk_path, O_RDONLY);
225 if (vk_fd >= 0)
226 {
227 len = read(vk_fd, vks, sizeof(vks)-1);
228 close(vk_fd);
229 if (len <= 0)
230 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500231
Dees_Troy51a0e822012-09-05 15:24:24 -0400232 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500233
Dees_Troy51a0e822012-09-05 15:24:24 -0400234 /* Parse a line like:
235 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
236 */
237 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
238 if (*ts == ':')
239 ++e->vk_count;
240 }
241
242 if (e->vk_count % 6) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600243 LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400244 }
245 e->vk_count /= 6;
246 if (e->vk_count <= 0)
247 return -1;
248
249 e->down = DOWN_NOT;
250 }
251
252 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
253 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
254 e->p.synced = 0;
255#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000256 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 -0400257#endif
258
259 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
260 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
261 e->mt_p.synced = 0;
262#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000263 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 -0400264#endif
265
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100266 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400267
268 for (i = 0; i < e->vk_count; ++i) {
269 char *token[6];
270 int j;
271
272 for (j = 0; j < 6; ++j) {
273 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
274 }
275
276 if (strcmp(token[0], "0x01") != 0) {
277 /* Java does string compare, so we do too. */
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600278 LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400279 continue;
280 }
281
282 e->vks[i].scancode = strtol(token[1], NULL, 0);
283 e->vks[i].centerx = strtol(token[2], NULL, 0);
284 e->vks[i].centery = strtol(token[3], NULL, 0);
285 e->vks[i].width = strtol(token[4], NULL, 0);
286 e->vks[i].height = strtol(token[5], NULL, 0);
287 }
288
289 return 0;
290}
291
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100292#define BITS_PER_LONG (sizeof(long) * 8)
293#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
294#define OFF(x) ((x)%BITS_PER_LONG)
295#define LONG(x) ((x)/BITS_PER_LONG)
296#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100297
298// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
299// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500300static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100301{
302 if(has_mouse)
303 return;
304
305 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
306 memset(bit, 0, sizeof(bit));
307 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
308
Vojtech Bocek971d3182014-02-20 21:43:28 +0100309 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100310 return;
311
312 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100313 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
314 return;
315
316 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
317 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
318 return;
319
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600320 LOGI("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100321 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100322}
323
324int ev_has_mouse(void)
325{
326 return has_mouse;
327}
328
Dees_Troy51a0e822012-09-05 15:24:24 -0400329int ev_init(void)
330{
331 DIR *dir;
332 struct dirent *de;
333 int fd;
334
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100335 has_mouse = 0;
336
Dees_Troy51a0e822012-09-05 15:24:24 -0400337 dir = opendir("/dev/input");
338 if(dir != 0) {
339 while((de = readdir(dir))) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600340#ifdef _EVENT_LOGGING
341 fprintf(stderr,"/dev/input/%s\n", de->d_name);
342#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400343 if(strncmp(de->d_name,"event",5)) continue;
344 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
345 if(fd < 0) continue;
346
347 ev_fds[ev_count].fd = fd;
348 ev_fds[ev_count].events = POLLIN;
349 evs[ev_count].fd = &ev_fds[ev_count];
350
351 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500352 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400353
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500354 if (!evs[ev_count].ignored)
355 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100356
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 ev_count++;
358 if(ev_count == MAX_DEVICES) break;
359 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100360 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400361 }
362
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100363 struct stat st;
364 if(stat("/dev/input", &st) >= 0)
365 lastInputMTime = st.st_mtime;
366 gettimeofday(&lastInputStat, NULL);
367
Dees_Troy51a0e822012-09-05 15:24:24 -0400368 return 0;
369}
370
371void ev_exit(void)
372{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100373 while (ev_count-- > 0) {
374 if (evs[ev_count].vk_count) {
375 free(evs[ev_count].vks);
376 evs[ev_count].vk_count = 0;
377 }
378 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100380 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400381}
382
Ethan Yonker58f21322018-08-24 11:17:36 -0500383/*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
Dees_Troy51a0e822012-09-05 15:24:24 -0400384{
385 int screen_pos;
386
387 if (info->minimum == info->maximum)
388 return 0;
389
390 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
391 return (screen_pos >= 0 && screen_pos < screen_size);
Ethan Yonker58f21322018-08-24 11:17:36 -0500392}*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400393
Dees_Troyb7ecc092013-08-24 12:15:41 +0000394static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400395{
396 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
397 {
398 // In this case, we assume the screen dimensions are the same.
399 *x = p->x;
400 *y = p->y;
401 return 0;
402 }
403
Dees_Troyb7ecc092013-08-24 12:15:41 +0000404#ifdef _EVENT_LOGGING
405 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
406#endif
407
Dees_Troy51a0e822012-09-05 15:24:24 -0400408#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000409 int fb_width = gr_fb_width();
410 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400411#else
412 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000413 int fb_width = gr_fb_height();
414 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400415#endif
416
417 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
418 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
419
420 if (*x >= 0 && *x < fb_width &&
421 *y >= 0 && *y < fb_height)
422 {
423 return 0;
424 }
425
426 return 1;
427}
428
429/* Translate a virtual key in to a real key event, if needed */
430/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000431static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400432{
433 static int downX = -1, downY = -1;
434 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100435 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400436 static int lastWasSynReport = 0;
437 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000438 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 -0400439 int i;
440 int x, y;
441
442 // This is used to ditch useless event handlers, like an accelerometer
443 if (e->ignored) return 1;
444
445 if (ev->type == EV_REL && ev->code == REL_Z)
446 {
447 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
448#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000449 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400450#endif
451 e->ignored = 1;
452 return 1;
453 }
454
455#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000456 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 -0400457#endif
458
459 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
460 if (ev->type == EV_KEY) {
461 return 0;
462 }
463
464 if (ev->type == EV_ABS) {
465 switch (ev->code) {
466
467 case ABS_X: //00
468 e->p.synced |= 0x01;
469 e->p.x = ev->value;
470#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000471 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400472#endif
473 break;
474
475 case ABS_Y: //01
476 e->p.synced |= 0x02;
477 e->p.y = ev->value;
478#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000479 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400480#endif
481 break;
482
483 case ABS_MT_POSITION: //2a
484 e->mt_p.synced = 0x03;
485 if (ev->value == (1 << 31))
486 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600487#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400488 e->mt_p.x = 0;
489 e->mt_p.y = 0;
490 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600491#endif
492#ifdef _EVENT_LOGGING
493#ifndef TW_IGNORE_MT_POSITION_0
494 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
495#else
496 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
497#endif
498#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400499 }
500 else
501 {
502 lastWasSynReport = 0;
503 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
504 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600505#ifdef _EVENT_LOGGING
506 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));
507#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 }
509 break;
510
511 case ABS_MT_TOUCH_MAJOR: //30
512 if (ev->value == 0)
513 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800514#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400515 // 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;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800519#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400520 }
521#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400523#endif
524 break;
525
526 case ABS_MT_PRESSURE: //3a
527 if (ev->value == 0)
528 {
529 // We're in a touch release, although some devices will still send positions as well
530 e->mt_p.x = 0;
531 e->mt_p.y = 0;
532 touchReleaseOnNextSynReport = 1;
533 }
534#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000535 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400536#endif
537 break;
538
539 case ABS_MT_POSITION_X: //35
540 e->mt_p.synced |= 0x01;
541 e->mt_p.x = ev->value;
542#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000543 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400544#endif
545 break;
546
547 case ABS_MT_POSITION_Y: //36
548 e->mt_p.synced |= 0x02;
549 e->mt_p.y = ev->value;
550#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000551 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400552#endif
553 break;
554
Dees_Troy51a0e822012-09-05 15:24:24 -0400555 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000556#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000557 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000558#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400559 break;
560
561 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000562#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000563 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000564#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400565 break;
566
567 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000568#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000569 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000570#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400571 break;
572
Dees_Troyd86400b2013-05-13 19:04:35 +0000573 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500574#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
575#ifdef _EVENT_LOGGING
576 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
577#endif
578 return 1;
579#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000580 if (ev->value < 0) {
581 e->mt_p.x = 0;
582 e->mt_p.y = 0;
583 touchReleaseOnNextSynReport = 2;
584 use_tracking_id_negative_as_touch_release = 1;
585#ifdef _EVENT_LOGGING
586 if (use_tracking_id_negative_as_touch_release)
587 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
588#endif
589 }
590#ifdef _EVENT_LOGGING
591 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
592#endif
593 break;
594
595#ifdef _EVENT_LOGGING
596 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000598 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 return 1;
600 break;
601
602 case ABS_MT_TOOL_TYPE: //37
603 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
604 return 1;
605 break;
606
607 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400609 return 1;
610 break;
611
Dees_Troy51a0e822012-09-05 15:24:24 -0400612 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000613 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 return 1;
615 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500616 case ABS_MT_SLOT:
617 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
618 return 1;
619 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400620#endif
621
622 default:
623 // This is an unhandled message, just skip it
624 return 1;
625 }
626
627 if (ev->code != ABS_MT_POSITION)
628 {
629 lastWasSynReport = 0;
630 return 1;
631 }
632 }
633
634 // Check if we should ignore the message
635 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
636 {
637 lastWasSynReport = 0;
638 return 0;
639 }
640
641#ifdef _EVENT_LOGGING
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600642 if (ev->type == EV_SYN && ev->code == SYN_REPORT)
643 printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
644 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT)
645 printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400646#endif
647
648 // Discard the MT versions
649 if (ev->code == SYN_MT_REPORT) return 0;
650
Dees_Troyd86400b2013-05-13 19:04:35 +0000651 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 -0400652 {
653 // Reset the value
654 touchReleaseOnNextSynReport = 0;
655
656 // We are a finger-up state
657 if (!discard)
658 {
659 // Report the key up
660 ev->type = EV_ABS;
661 ev->code = 0;
662 ev->value = (downX << 16) | downY;
663 }
664 downX = -1;
665 downY = -1;
666 if (discard)
667 {
668 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100669
670 // Send the keyUp event
671 ev->type = EV_KEY;
672 ev->code = last_virt_key;
673 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400674 }
675 return 0;
676 }
677 lastWasSynReport = 1;
678
679 // Retrieve where the x,y position is
680 if (e->p.synced & 0x03)
681 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000682 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400683 }
684 else if (e->mt_p.synced & 0x03)
685 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000686 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400687 }
688 else
689 {
690 // We don't have useful information to convey
691 return 1;
692 }
693
694#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
695 x ^= y;
696 y ^= x;
697 x ^= y;
698#endif
699#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000700 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400701#endif
702#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000703 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400704#endif
705
706#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000707 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400708#endif
709
710 // Clear the current sync states
711 e->p.synced = e->mt_p.synced = 0;
712
713 // If we have nothing useful to report, skip it
714 if (x == -1 || y == -1) return 1;
715
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500716 // Special case, we'll ignore touches on 0,0 because it usually means
717 // that we received extra data after our last sync and x and y were
718 // reset to 0. We should not be using 0,0 anyway.
719 if (x == 0 && y == 0)
720 return 1;
721
Dees_Troy51a0e822012-09-05 15:24:24 -0400722 // On first touch, see if we're at a virtual key
723 if (downX == -1)
724 {
725 // Attempt mapping to virtual key
726 for (i = 0; i < e->vk_count; ++i)
727 {
728 int xd = ABS(e->vks[i].centerx - x);
729 int yd = ABS(e->vks[i].centery - y);
730
731 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
732 {
733 ev->type = EV_KEY;
734 ev->code = e->vks[i].scancode;
735 ev->value = 1;
736
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100737 last_virt_key = e->vks[i].scancode;
738
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400739#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400740 vibrate(VIBRATOR_TIME_MS);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400741#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400742
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500743 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400744 // and make sure we don't come back into this area
745 discard = 1;
746 downX = 0;
747 return 0;
748 }
749 }
750 }
751
752 // If we were originally a button press, discard this event
753 if (discard)
754 {
755 return 1;
756 }
757
758 // Record where we started the touch for deciding if this is a key or a scroll
759 downX = x;
760 downY = y;
761
762 ev->type = EV_ABS;
763 ev->code = 1;
764 ev->value = (x << 16) | y;
765 return 0;
766}
767
thatde72b6d2015-02-08 08:55:00 +0100768int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400769{
770 int r;
771 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100772 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400773
Ethan Yonkere13fa632015-01-27 11:30:03 -0600774 gettimeofday(&curr, NULL);
775 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
776 {
777 struct stat st;
778 stat("/dev/input", &st);
779 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100780 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600781 LOGI("Reloading input devices\n");
Ethan Yonkere13fa632015-01-27 11:30:03 -0600782 ev_exit();
783 ev_init();
784 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100785 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600786 lastInputStat = curr;
787 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100788
thatde72b6d2015-02-08 08:55:00 +0100789 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400790
Ethan Yonkere13fa632015-01-27 11:30:03 -0600791 if(r > 0) {
792 for(n = 0; n < ev_count; n++) {
793 if(ev_fds[n].revents & POLLIN) {
794 r = read(ev_fds[n].fd, ev, sizeof(*ev));
795 if(r == sizeof(*ev)) {
796 if (!vk_modify(&evs[n], ev))
797 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400798 }
799 }
800 }
thatc5837f32015-02-01 01:59:43 +0100801 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600802 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400803
thatc5837f32015-02-01 01:59:43 +0100804 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400805}
806
Ethan Yonker58f21322018-08-24 11:17:36 -0500807int ev_wait(int timeout __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400808{
809 return -1;
810}
811
812void ev_dispatch(void)
813{
814 return;
815}
816
Ethan Yonker58f21322018-08-24 11:17:36 -0500817int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400818{
819 return -1;
820}