blob: 9374ef1741e5e3905af14a186d8ebb91428f6112 [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
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400126#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400127int vibrate(int timeout_ms)
128{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000129 if (timeout_ms > 10000) timeout_ms = 1000;
130
notsyncingc7c78d22018-06-06 20:26:47 +0800131 if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) {
132 write_to_file(LEDS_HAPTICS_DURATION_FILE, std::to_string(timeout_ms));
133 write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1");
134 } else
135 write_to_file(VIBRATOR_TIMEOUT_FILE, std::to_string(timeout_ms));
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
137 return 0;
138}
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400139#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
141/* Returns empty tokens */
142static char *vk_strtok_r(char *str, const char *delim, char **save_str)
143{
144 if(!str)
145 {
146 if(!*save_str)
147 return NULL;
148
149 str = (*save_str) + 1;
150 }
151 *save_str = strpbrk(str, delim);
152
153 if (*save_str)
154 **save_str = '\0';
155
156 return str;
157}
158
159static int vk_init(struct ev *e)
160{
161 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
162 char vks[2048], *ts = NULL;
163 ssize_t len;
164 int vk_fd;
165 int i;
166
167 e->vk_count = 0;
168
169 len = strlen(vk_path);
170 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
171 if (len <= 0)
172 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600173 LOGE("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400174 return -1;
175 }
176#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000177 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400178#endif
179
Flemmardf4674612014-01-10 09:14:44 +0100180#ifdef WHITELIST_INPUT
181 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
182 {
183 e->ignored = 1;
184 }
185#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500186#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500187 // 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 -0500188 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400189 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600190 LOGI("Blacklisting input device: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400191 e->ignored = 1;
192 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500193#else
194 char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST));
195 char* blacklist = strtok(bl, "\n");
196
197 while (blacklist != NULL) {
198 if (strcmp(e->deviceName, blacklist) == 0) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600199 LOGI("Blacklisting input device: %s\n", blacklist);
Ethan Yonker5742a402014-08-12 14:52:49 -0500200 e->ignored = 1;
201 }
202 blacklist = strtok(NULL, "\n");
203 }
204 free(bl);
205#endif
Flemmardf4674612014-01-10 09:14:44 +0100206#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400207
208 strcat(vk_path, e->deviceName);
209
210 // Some devices split the keys from the touchscreen
211 e->vk_count = 0;
212 vk_fd = open(vk_path, O_RDONLY);
213 if (vk_fd >= 0)
214 {
215 len = read(vk_fd, vks, sizeof(vks)-1);
216 close(vk_fd);
217 if (len <= 0)
218 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500219
Dees_Troy51a0e822012-09-05 15:24:24 -0400220 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500221
Dees_Troy51a0e822012-09-05 15:24:24 -0400222 /* Parse a line like:
223 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
224 */
225 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
226 if (*ts == ':')
227 ++e->vk_count;
228 }
229
230 if (e->vk_count % 6) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600231 LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400232 }
233 e->vk_count /= 6;
234 if (e->vk_count <= 0)
235 return -1;
236
237 e->down = DOWN_NOT;
238 }
239
240 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
241 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
242 e->p.synced = 0;
243#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000244 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 -0400245#endif
246
247 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
248 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
249 e->mt_p.synced = 0;
250#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000251 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 -0400252#endif
253
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100254 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400255
256 for (i = 0; i < e->vk_count; ++i) {
257 char *token[6];
258 int j;
259
260 for (j = 0; j < 6; ++j) {
261 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
262 }
263
264 if (strcmp(token[0], "0x01") != 0) {
265 /* Java does string compare, so we do too. */
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600266 LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400267 continue;
268 }
269
270 e->vks[i].scancode = strtol(token[1], NULL, 0);
271 e->vks[i].centerx = strtol(token[2], NULL, 0);
272 e->vks[i].centery = strtol(token[3], NULL, 0);
273 e->vks[i].width = strtol(token[4], NULL, 0);
274 e->vks[i].height = strtol(token[5], NULL, 0);
275 }
276
277 return 0;
278}
279
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100280#define BITS_PER_LONG (sizeof(long) * 8)
281#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
282#define OFF(x) ((x)%BITS_PER_LONG)
283#define LONG(x) ((x)/BITS_PER_LONG)
284#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100285
286// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
287// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500288static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100289{
290 if(has_mouse)
291 return;
292
293 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
294 memset(bit, 0, sizeof(bit));
295 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
296
Vojtech Bocek971d3182014-02-20 21:43:28 +0100297 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100298 return;
299
300 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100301 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
302 return;
303
304 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
305 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
306 return;
307
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600308 LOGI("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100309 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100310}
311
312int ev_has_mouse(void)
313{
314 return has_mouse;
315}
316
Dees_Troy51a0e822012-09-05 15:24:24 -0400317int ev_init(void)
318{
319 DIR *dir;
320 struct dirent *de;
321 int fd;
322
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100323 has_mouse = 0;
324
Dees_Troy51a0e822012-09-05 15:24:24 -0400325 dir = opendir("/dev/input");
326 if(dir != 0) {
327 while((de = readdir(dir))) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600328#ifdef _EVENT_LOGGING
329 fprintf(stderr,"/dev/input/%s\n", de->d_name);
330#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400331 if(strncmp(de->d_name,"event",5)) continue;
332 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
333 if(fd < 0) continue;
334
335 ev_fds[ev_count].fd = fd;
336 ev_fds[ev_count].events = POLLIN;
337 evs[ev_count].fd = &ev_fds[ev_count];
338
339 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500340 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400341
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500342 if (!evs[ev_count].ignored)
343 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100344
Dees_Troy51a0e822012-09-05 15:24:24 -0400345 ev_count++;
346 if(ev_count == MAX_DEVICES) break;
347 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100348 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400349 }
350
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100351 struct stat st;
352 if(stat("/dev/input", &st) >= 0)
353 lastInputMTime = st.st_mtime;
354 gettimeofday(&lastInputStat, NULL);
355
Dees_Troy51a0e822012-09-05 15:24:24 -0400356 return 0;
357}
358
359void ev_exit(void)
360{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100361 while (ev_count-- > 0) {
362 if (evs[ev_count].vk_count) {
363 free(evs[ev_count].vks);
364 evs[ev_count].vk_count = 0;
365 }
366 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400367 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100368 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400369}
370
Ethan Yonker58f21322018-08-24 11:17:36 -0500371/*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
Dees_Troy51a0e822012-09-05 15:24:24 -0400372{
373 int screen_pos;
374
375 if (info->minimum == info->maximum)
376 return 0;
377
378 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
379 return (screen_pos >= 0 && screen_pos < screen_size);
Ethan Yonker58f21322018-08-24 11:17:36 -0500380}*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400381
Dees_Troyb7ecc092013-08-24 12:15:41 +0000382static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400383{
384 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
385 {
386 // In this case, we assume the screen dimensions are the same.
387 *x = p->x;
388 *y = p->y;
389 return 0;
390 }
391
Dees_Troyb7ecc092013-08-24 12:15:41 +0000392#ifdef _EVENT_LOGGING
393 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
394#endif
395
Dees_Troy51a0e822012-09-05 15:24:24 -0400396#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000397 int fb_width = gr_fb_width();
398 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400399#else
400 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000401 int fb_width = gr_fb_height();
402 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400403#endif
404
405 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
406 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
407
408 if (*x >= 0 && *x < fb_width &&
409 *y >= 0 && *y < fb_height)
410 {
411 return 0;
412 }
413
414 return 1;
415}
416
417/* Translate a virtual key in to a real key event, if needed */
418/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000419static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400420{
421 static int downX = -1, downY = -1;
422 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100423 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400424 static int lastWasSynReport = 0;
425 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000426 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 -0400427 int i;
428 int x, y;
429
430 // This is used to ditch useless event handlers, like an accelerometer
431 if (e->ignored) return 1;
432
433 if (ev->type == EV_REL && ev->code == REL_Z)
434 {
435 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
436#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000437 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400438#endif
439 e->ignored = 1;
440 return 1;
441 }
442
443#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000444 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 -0400445#endif
446
447 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
448 if (ev->type == EV_KEY) {
449 return 0;
450 }
451
452 if (ev->type == EV_ABS) {
453 switch (ev->code) {
454
455 case ABS_X: //00
456 e->p.synced |= 0x01;
457 e->p.x = ev->value;
458#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000459 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400460#endif
461 break;
462
463 case ABS_Y: //01
464 e->p.synced |= 0x02;
465 e->p.y = ev->value;
466#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000467 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400468#endif
469 break;
470
471 case ABS_MT_POSITION: //2a
472 e->mt_p.synced = 0x03;
473 if (ev->value == (1 << 31))
474 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600475#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400476 e->mt_p.x = 0;
477 e->mt_p.y = 0;
478 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600479#endif
480#ifdef _EVENT_LOGGING
481#ifndef TW_IGNORE_MT_POSITION_0
482 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
483#else
484 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
485#endif
486#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400487 }
488 else
489 {
490 lastWasSynReport = 0;
491 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
492 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600493#ifdef _EVENT_LOGGING
494 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));
495#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400496 }
497 break;
498
499 case ABS_MT_TOUCH_MAJOR: //30
500 if (ev->value == 0)
501 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800502#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 // We're in a touch release, although some devices will still send positions as well
504 e->mt_p.x = 0;
505 e->mt_p.y = 0;
506 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800507#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400508 }
509#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000510 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400511#endif
512 break;
513
514 case ABS_MT_PRESSURE: //3a
515 if (ev->value == 0)
516 {
517 // 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;
521 }
522#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000523 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400524#endif
525 break;
526
527 case ABS_MT_POSITION_X: //35
528 e->mt_p.synced |= 0x01;
529 e->mt_p.x = ev->value;
530#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400532#endif
533 break;
534
535 case ABS_MT_POSITION_Y: //36
536 e->mt_p.synced |= 0x02;
537 e->mt_p.y = ev->value;
538#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000539 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400540#endif
541 break;
542
Dees_Troy51a0e822012-09-05 15:24:24 -0400543 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000544#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000545 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000546#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400547 break;
548
549 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000550#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000551 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000552#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400553 break;
554
555 case ABS_MT_WIDTH_MINOR: //33
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_WIDTH_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
Dees_Troyd86400b2013-05-13 19:04:35 +0000561 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500562#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
563#ifdef _EVENT_LOGGING
564 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
565#endif
566 return 1;
567#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000568 if (ev->value < 0) {
569 e->mt_p.x = 0;
570 e->mt_p.y = 0;
571 touchReleaseOnNextSynReport = 2;
572 use_tracking_id_negative_as_touch_release = 1;
573#ifdef _EVENT_LOGGING
574 if (use_tracking_id_negative_as_touch_release)
575 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
576#endif
577 }
578#ifdef _EVENT_LOGGING
579 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
580#endif
581 break;
582
583#ifdef _EVENT_LOGGING
584 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400585 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000586 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 return 1;
588 break;
589
590 case ABS_MT_TOOL_TYPE: //37
591 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
592 return 1;
593 break;
594
595 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000596 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 return 1;
598 break;
599
Dees_Troy51a0e822012-09-05 15:24:24 -0400600 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000601 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400602 return 1;
603 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500604 case ABS_MT_SLOT:
605 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
606 return 1;
607 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400608#endif
609
610 default:
611 // This is an unhandled message, just skip it
612 return 1;
613 }
614
615 if (ev->code != ABS_MT_POSITION)
616 {
617 lastWasSynReport = 0;
618 return 1;
619 }
620 }
621
622 // Check if we should ignore the message
623 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
624 {
625 lastWasSynReport = 0;
626 return 0;
627 }
628
629#ifdef _EVENT_LOGGING
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600630 if (ev->type == EV_SYN && ev->code == SYN_REPORT)
631 printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
632 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT)
633 printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400634#endif
635
636 // Discard the MT versions
637 if (ev->code == SYN_MT_REPORT) return 0;
638
Dees_Troyd86400b2013-05-13 19:04:35 +0000639 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 -0400640 {
641 // Reset the value
642 touchReleaseOnNextSynReport = 0;
643
644 // We are a finger-up state
645 if (!discard)
646 {
647 // Report the key up
648 ev->type = EV_ABS;
649 ev->code = 0;
650 ev->value = (downX << 16) | downY;
651 }
652 downX = -1;
653 downY = -1;
654 if (discard)
655 {
656 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100657
658 // Send the keyUp event
659 ev->type = EV_KEY;
660 ev->code = last_virt_key;
661 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400662 }
663 return 0;
664 }
665 lastWasSynReport = 1;
666
667 // Retrieve where the x,y position is
668 if (e->p.synced & 0x03)
669 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000670 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400671 }
672 else if (e->mt_p.synced & 0x03)
673 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000674 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400675 }
676 else
677 {
678 // We don't have useful information to convey
679 return 1;
680 }
681
682#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
683 x ^= y;
684 y ^= x;
685 x ^= y;
686#endif
687#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000688 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400689#endif
690#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000691 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400692#endif
693
694#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000695 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400696#endif
697
698 // Clear the current sync states
699 e->p.synced = e->mt_p.synced = 0;
700
701 // If we have nothing useful to report, skip it
702 if (x == -1 || y == -1) return 1;
703
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500704 // Special case, we'll ignore touches on 0,0 because it usually means
705 // that we received extra data after our last sync and x and y were
706 // reset to 0. We should not be using 0,0 anyway.
707 if (x == 0 && y == 0)
708 return 1;
709
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 // On first touch, see if we're at a virtual key
711 if (downX == -1)
712 {
713 // Attempt mapping to virtual key
714 for (i = 0; i < e->vk_count; ++i)
715 {
716 int xd = ABS(e->vks[i].centerx - x);
717 int yd = ABS(e->vks[i].centery - y);
718
719 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
720 {
721 ev->type = EV_KEY;
722 ev->code = e->vks[i].scancode;
723 ev->value = 1;
724
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100725 last_virt_key = e->vks[i].scancode;
726
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400727#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 vibrate(VIBRATOR_TIME_MS);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400729#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400730
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500731 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400732 // and make sure we don't come back into this area
733 discard = 1;
734 downX = 0;
735 return 0;
736 }
737 }
738 }
739
740 // If we were originally a button press, discard this event
741 if (discard)
742 {
743 return 1;
744 }
745
746 // Record where we started the touch for deciding if this is a key or a scroll
747 downX = x;
748 downY = y;
749
750 ev->type = EV_ABS;
751 ev->code = 1;
752 ev->value = (x << 16) | y;
753 return 0;
754}
755
thatde72b6d2015-02-08 08:55:00 +0100756int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400757{
758 int r;
759 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100760 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400761
Ethan Yonkere13fa632015-01-27 11:30:03 -0600762 gettimeofday(&curr, NULL);
763 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
764 {
765 struct stat st;
766 stat("/dev/input", &st);
767 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100768 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600769 LOGI("Reloading input devices\n");
Ethan Yonkere13fa632015-01-27 11:30:03 -0600770 ev_exit();
771 ev_init();
772 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100773 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600774 lastInputStat = curr;
775 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100776
thatde72b6d2015-02-08 08:55:00 +0100777 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400778
Ethan Yonkere13fa632015-01-27 11:30:03 -0600779 if(r > 0) {
780 for(n = 0; n < ev_count; n++) {
781 if(ev_fds[n].revents & POLLIN) {
782 r = read(ev_fds[n].fd, ev, sizeof(*ev));
783 if(r == sizeof(*ev)) {
784 if (!vk_modify(&evs[n], ev))
785 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 }
787 }
788 }
thatc5837f32015-02-01 01:59:43 +0100789 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600790 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400791
thatc5837f32015-02-01 01:59:43 +0100792 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400793}
794
Ethan Yonker58f21322018-08-24 11:17:36 -0500795int ev_wait(int timeout __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400796{
797 return -1;
798}
799
800void ev_dispatch(void)
801{
802 return;
803}
804
Ethan Yonker58f21322018-08-24 11:17:36 -0500805int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400806{
807 return -1;
808}