blob: 6d576c6732fb5edee8f51f81220d1b51d5d27767 [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
bigbiff1f9e4842020-10-31 11:33:15 -040035#include "common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040036
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
LameMonster824ef96a62020-06-04 13:31:12 +0200131#ifndef TW_HAPTICS_TSPDRV
Dees_Troy51a0e822012-09-05 15:24:24 -0400132int vibrate(int timeout_ms)
133{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000134 if (timeout_ms > 10000) timeout_ms = 1000;
Dees Troy9a4d7402019-03-21 14:17:28 -0400135 char tout[6];
136 sprintf(tout, "%i", timeout_ms);
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000137
Mohd Farazbc576cd2020-05-30 21:06:32 +0530138#ifdef USE_QTI_HAPTICS
139 android::sp<android::hardware::vibrator::V1_2::IVibrator> vib = android::hardware::vibrator::V1_2::IVibrator::getService();
140 if (vib != nullptr) {
141 vib->on((uint32_t)timeout_ms);
142 }
143#else
notsyncingc7c78d22018-06-06 20:26:47 +0800144 if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) {
Dees Troy9a4d7402019-03-21 14:17:28 -0400145 write_to_file(LEDS_HAPTICS_DURATION_FILE, tout);
notsyncingc7c78d22018-06-06 20:26:47 +0800146 write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1");
147 } else
Dees Troy9a4d7402019-03-21 14:17:28 -0400148 write_to_file(VIBRATOR_TIMEOUT_FILE, tout);
Mohd Farazbc576cd2020-05-30 21:06:32 +0530149#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400150 return 0;
151}
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400152#endif
LameMonster824ef96a62020-06-04 13:31:12 +0200153#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400154
155/* Returns empty tokens */
156static char *vk_strtok_r(char *str, const char *delim, char **save_str)
157{
158 if(!str)
159 {
160 if(!*save_str)
161 return NULL;
162
163 str = (*save_str) + 1;
164 }
165 *save_str = strpbrk(str, delim);
166
167 if (*save_str)
168 **save_str = '\0';
169
170 return str;
171}
172
173static int vk_init(struct ev *e)
174{
175 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
176 char vks[2048], *ts = NULL;
177 ssize_t len;
178 int vk_fd;
179 int i;
180
181 e->vk_count = 0;
182
183 len = strlen(vk_path);
184 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
185 if (len <= 0)
186 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600187 LOGE("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400188 return -1;
189 }
190#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000191 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400192#endif
193
Flemmardf4674612014-01-10 09:14:44 +0100194#ifdef WHITELIST_INPUT
195 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
196 {
197 e->ignored = 1;
198 }
199#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500200#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500201 // 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 -0500202 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400203 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600204 LOGI("Blacklisting input device: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400205 e->ignored = 1;
206 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500207#else
208 char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST));
209 char* blacklist = strtok(bl, "\n");
210
211 while (blacklist != NULL) {
212 if (strcmp(e->deviceName, blacklist) == 0) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600213 LOGI("Blacklisting input device: %s\n", blacklist);
Ethan Yonker5742a402014-08-12 14:52:49 -0500214 e->ignored = 1;
215 }
216 blacklist = strtok(NULL, "\n");
217 }
218 free(bl);
219#endif
Flemmardf4674612014-01-10 09:14:44 +0100220#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400221
222 strcat(vk_path, e->deviceName);
223
224 // Some devices split the keys from the touchscreen
225 e->vk_count = 0;
226 vk_fd = open(vk_path, O_RDONLY);
227 if (vk_fd >= 0)
228 {
229 len = read(vk_fd, vks, sizeof(vks)-1);
230 close(vk_fd);
231 if (len <= 0)
232 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500233
Dees_Troy51a0e822012-09-05 15:24:24 -0400234 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500235
Dees_Troy51a0e822012-09-05 15:24:24 -0400236 /* Parse a line like:
237 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
238 */
239 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
240 if (*ts == ':')
241 ++e->vk_count;
242 }
243
244 if (e->vk_count % 6) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600245 LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400246 }
247 e->vk_count /= 6;
248 if (e->vk_count <= 0)
249 return -1;
250
251 e->down = DOWN_NOT;
252 }
253
254 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
255 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
256 e->p.synced = 0;
257#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 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 -0400259#endif
260
261 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
262 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
263 e->mt_p.synced = 0;
264#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000265 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 -0400266#endif
267
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100268 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400269
270 for (i = 0; i < e->vk_count; ++i) {
271 char *token[6];
272 int j;
273
274 for (j = 0; j < 6; ++j) {
275 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
276 }
277
278 if (strcmp(token[0], "0x01") != 0) {
279 /* Java does string compare, so we do too. */
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600280 LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400281 continue;
282 }
283
284 e->vks[i].scancode = strtol(token[1], NULL, 0);
285 e->vks[i].centerx = strtol(token[2], NULL, 0);
286 e->vks[i].centery = strtol(token[3], NULL, 0);
287 e->vks[i].width = strtol(token[4], NULL, 0);
288 e->vks[i].height = strtol(token[5], NULL, 0);
289 }
290
291 return 0;
292}
293
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100294#define BITS_PER_LONG (sizeof(long) * 8)
295#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
296#define OFF(x) ((x)%BITS_PER_LONG)
297#define LONG(x) ((x)/BITS_PER_LONG)
298#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100299
300// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
301// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500302static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100303{
304 if(has_mouse)
305 return;
306
307 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
308 memset(bit, 0, sizeof(bit));
309 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
310
Vojtech Bocek971d3182014-02-20 21:43:28 +0100311 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100312 return;
313
314 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100315 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
316 return;
317
318 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
319 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
320 return;
321
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600322 LOGI("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100323 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100324}
325
326int ev_has_mouse(void)
327{
328 return has_mouse;
329}
330
Dees_Troy51a0e822012-09-05 15:24:24 -0400331int ev_init(void)
332{
333 DIR *dir;
334 struct dirent *de;
335 int fd;
336
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100337 has_mouse = 0;
338
Dees_Troy51a0e822012-09-05 15:24:24 -0400339 dir = opendir("/dev/input");
340 if(dir != 0) {
341 while((de = readdir(dir))) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600342#ifdef _EVENT_LOGGING
343 fprintf(stderr,"/dev/input/%s\n", de->d_name);
344#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 if(strncmp(de->d_name,"event",5)) continue;
346 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
347 if(fd < 0) continue;
348
349 ev_fds[ev_count].fd = fd;
350 ev_fds[ev_count].events = POLLIN;
351 evs[ev_count].fd = &ev_fds[ev_count];
352
353 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500354 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400355
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500356 if (!evs[ev_count].ignored)
357 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100358
Dees_Troy51a0e822012-09-05 15:24:24 -0400359 ev_count++;
360 if(ev_count == MAX_DEVICES) break;
361 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100362 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400363 }
364
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100365 struct stat st;
366 if(stat("/dev/input", &st) >= 0)
367 lastInputMTime = st.st_mtime;
368 gettimeofday(&lastInputStat, NULL);
369
Dees_Troy51a0e822012-09-05 15:24:24 -0400370 return 0;
371}
372
373void ev_exit(void)
374{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100375 while (ev_count-- > 0) {
376 if (evs[ev_count].vk_count) {
377 free(evs[ev_count].vks);
378 evs[ev_count].vk_count = 0;
379 }
380 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400381 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100382 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
Ethan Yonker58f21322018-08-24 11:17:36 -0500385/*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
Dees_Troy51a0e822012-09-05 15:24:24 -0400386{
387 int screen_pos;
388
389 if (info->minimum == info->maximum)
390 return 0;
391
392 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
393 return (screen_pos >= 0 && screen_pos < screen_size);
Ethan Yonker58f21322018-08-24 11:17:36 -0500394}*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400395
Dees_Troyb7ecc092013-08-24 12:15:41 +0000396static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400397{
398 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
399 {
400 // In this case, we assume the screen dimensions are the same.
401 *x = p->x;
402 *y = p->y;
403 return 0;
404 }
405
Dees_Troyb7ecc092013-08-24 12:15:41 +0000406#ifdef _EVENT_LOGGING
407 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
408#endif
409
Dees_Troy51a0e822012-09-05 15:24:24 -0400410#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000411 int fb_width = gr_fb_width();
412 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400413#else
414 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000415 int fb_width = gr_fb_height();
416 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400417#endif
418
419 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
420 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
421
422 if (*x >= 0 && *x < fb_width &&
423 *y >= 0 && *y < fb_height)
424 {
425 return 0;
426 }
427
428 return 1;
429}
430
431/* Translate a virtual key in to a real key event, if needed */
432/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000433static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400434{
435 static int downX = -1, downY = -1;
436 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100437 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400438 static int lastWasSynReport = 0;
439 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000440 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 -0400441 int i;
442 int x, y;
443
444 // This is used to ditch useless event handlers, like an accelerometer
445 if (e->ignored) return 1;
446
447 if (ev->type == EV_REL && ev->code == REL_Z)
448 {
449 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
450#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000451 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400452#endif
453 e->ignored = 1;
454 return 1;
455 }
456
457#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000458 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 -0400459#endif
460
461 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
462 if (ev->type == EV_KEY) {
463 return 0;
464 }
465
466 if (ev->type == EV_ABS) {
467 switch (ev->code) {
468
469 case ABS_X: //00
470 e->p.synced |= 0x01;
471 e->p.x = ev->value;
472#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000473 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400474#endif
475 break;
476
477 case ABS_Y: //01
478 e->p.synced |= 0x02;
479 e->p.y = ev->value;
480#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000481 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400482#endif
483 break;
484
485 case ABS_MT_POSITION: //2a
486 e->mt_p.synced = 0x03;
487 if (ev->value == (1 << 31))
488 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600489#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400490 e->mt_p.x = 0;
491 e->mt_p.y = 0;
492 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600493#endif
494#ifdef _EVENT_LOGGING
495#ifndef TW_IGNORE_MT_POSITION_0
496 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
497#else
498 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
499#endif
500#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400501 }
502 else
503 {
504 lastWasSynReport = 0;
505 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
506 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600507#ifdef _EVENT_LOGGING
508 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));
509#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 }
511 break;
512
513 case ABS_MT_TOUCH_MAJOR: //30
514 if (ev->value == 0)
515 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800516#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400517 // We're in a touch release, although some devices will still send positions as well
518 e->mt_p.x = 0;
519 e->mt_p.y = 0;
520 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800521#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400522 }
523#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000524 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400525#endif
526 break;
527
528 case ABS_MT_PRESSURE: //3a
529 if (ev->value == 0)
530 {
531 // We're in a touch release, although some devices will still send positions as well
532 e->mt_p.x = 0;
533 e->mt_p.y = 0;
534 touchReleaseOnNextSynReport = 1;
535 }
536#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000537 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400538#endif
539 break;
540
541 case ABS_MT_POSITION_X: //35
542 e->mt_p.synced |= 0x01;
543 e->mt_p.x = ev->value;
544#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000545 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400546#endif
547 break;
548
549 case ABS_MT_POSITION_Y: //36
550 e->mt_p.synced |= 0x02;
551 e->mt_p.y = ev->value;
552#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000553 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400554#endif
555 break;
556
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000558#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000559 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000560#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400561 break;
562
563 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000564#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000566#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400567 break;
568
569 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000570#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000571 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000572#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400573 break;
574
Dees_Troyd86400b2013-05-13 19:04:35 +0000575 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500576#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
577#ifdef _EVENT_LOGGING
578 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
579#endif
580 return 1;
581#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000582 if (ev->value < 0) {
583 e->mt_p.x = 0;
584 e->mt_p.y = 0;
585 touchReleaseOnNextSynReport = 2;
586 use_tracking_id_negative_as_touch_release = 1;
587#ifdef _EVENT_LOGGING
588 if (use_tracking_id_negative_as_touch_release)
589 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
590#endif
591 }
592#ifdef _EVENT_LOGGING
593 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
594#endif
595 break;
596
597#ifdef _EVENT_LOGGING
598 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400599 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000600 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 return 1;
602 break;
603
604 case ABS_MT_TOOL_TYPE: //37
605 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
606 return 1;
607 break;
608
609 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000610 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400611 return 1;
612 break;
613
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000615 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400616 return 1;
617 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500618 case ABS_MT_SLOT:
619 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
620 return 1;
621 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400622#endif
623
624 default:
625 // This is an unhandled message, just skip it
626 return 1;
627 }
628
629 if (ev->code != ABS_MT_POSITION)
630 {
631 lastWasSynReport = 0;
632 return 1;
633 }
634 }
635
636 // Check if we should ignore the message
637 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
638 {
639 lastWasSynReport = 0;
640 return 0;
641 }
642
643#ifdef _EVENT_LOGGING
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600644 if (ev->type == EV_SYN && ev->code == SYN_REPORT)
645 printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
646 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT)
647 printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400648#endif
649
650 // Discard the MT versions
651 if (ev->code == SYN_MT_REPORT) return 0;
652
Dees_Troyd86400b2013-05-13 19:04:35 +0000653 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 -0400654 {
655 // Reset the value
656 touchReleaseOnNextSynReport = 0;
657
658 // We are a finger-up state
659 if (!discard)
660 {
661 // Report the key up
662 ev->type = EV_ABS;
663 ev->code = 0;
664 ev->value = (downX << 16) | downY;
665 }
666 downX = -1;
667 downY = -1;
668 if (discard)
669 {
670 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100671
672 // Send the keyUp event
673 ev->type = EV_KEY;
674 ev->code = last_virt_key;
675 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400676 }
677 return 0;
678 }
679 lastWasSynReport = 1;
680
681 // Retrieve where the x,y position is
682 if (e->p.synced & 0x03)
683 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000684 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400685 }
686 else if (e->mt_p.synced & 0x03)
687 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000688 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400689 }
690 else
691 {
692 // We don't have useful information to convey
693 return 1;
694 }
695
696#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
697 x ^= y;
698 y ^= x;
699 x ^= y;
700#endif
701#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000702 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400703#endif
704#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000705 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400706#endif
707
708#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000709 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400710#endif
711
712 // Clear the current sync states
713 e->p.synced = e->mt_p.synced = 0;
714
715 // If we have nothing useful to report, skip it
716 if (x == -1 || y == -1) return 1;
717
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500718 // Special case, we'll ignore touches on 0,0 because it usually means
719 // that we received extra data after our last sync and x and y were
720 // reset to 0. We should not be using 0,0 anyway.
721 if (x == 0 && y == 0)
722 return 1;
723
Dees_Troy51a0e822012-09-05 15:24:24 -0400724 // On first touch, see if we're at a virtual key
725 if (downX == -1)
726 {
727 // Attempt mapping to virtual key
728 for (i = 0; i < e->vk_count; ++i)
729 {
730 int xd = ABS(e->vks[i].centerx - x);
731 int yd = ABS(e->vks[i].centery - y);
732
733 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
734 {
735 ev->type = EV_KEY;
736 ev->code = e->vks[i].scancode;
737 ev->value = 1;
738
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100739 last_virt_key = e->vks[i].scancode;
740
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400741#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400742 vibrate(VIBRATOR_TIME_MS);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400743#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400744
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500745 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400746 // and make sure we don't come back into this area
747 discard = 1;
748 downX = 0;
749 return 0;
750 }
751 }
752 }
753
754 // If we were originally a button press, discard this event
755 if (discard)
756 {
757 return 1;
758 }
759
760 // Record where we started the touch for deciding if this is a key or a scroll
761 downX = x;
762 downY = y;
763
764 ev->type = EV_ABS;
765 ev->code = 1;
766 ev->value = (x << 16) | y;
767 return 0;
768}
769
thatde72b6d2015-02-08 08:55:00 +0100770int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400771{
772 int r;
773 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100774 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400775
Ethan Yonkere13fa632015-01-27 11:30:03 -0600776 gettimeofday(&curr, NULL);
777 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
778 {
779 struct stat st;
780 stat("/dev/input", &st);
781 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100782 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600783 LOGI("Reloading input devices\n");
Ethan Yonkere13fa632015-01-27 11:30:03 -0600784 ev_exit();
785 ev_init();
786 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100787 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600788 lastInputStat = curr;
789 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100790
thatde72b6d2015-02-08 08:55:00 +0100791 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400792
Ethan Yonkere13fa632015-01-27 11:30:03 -0600793 if(r > 0) {
794 for(n = 0; n < ev_count; n++) {
795 if(ev_fds[n].revents & POLLIN) {
796 r = read(ev_fds[n].fd, ev, sizeof(*ev));
797 if(r == sizeof(*ev)) {
798 if (!vk_modify(&evs[n], ev))
799 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400800 }
801 }
802 }
thatc5837f32015-02-01 01:59:43 +0100803 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600804 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400805
thatc5837f32015-02-01 01:59:43 +0100806 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400807}
808
Ethan Yonker58f21322018-08-24 11:17:36 -0500809int ev_wait(int timeout __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400810{
811 return -1;
812}
813
814void ev_dispatch(void)
815{
816 return;
817}
818
Ethan Yonker58f21322018-08-24 11:17:36 -0500819int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400820{
821 return -1;
822}