blob: b5e5a1453a5d49136102b686b32891e6f400b364 [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>
sekaiacgacc522f2022-06-25 13:45:17 +080030#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
31#include <thread>
32#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Mohd Farazbc576cd2020-05-30 21:06:32 +053034#ifdef USE_QTI_HAPTICS
35#include <android/hardware/vibrator/1.2/IVibrator.h>
36#endif
37
nebrassy76224bd2021-04-05 11:52:44 +020038#ifdef USE_QTI_AIDL_HAPTICS
39#include <aidl/android/hardware/vibrator/IVibrator.h>
40#include <android/binder_manager.h>
41using ::aidl::android::hardware::vibrator::IVibrator;
sekaiacg38d6efd2022-06-25 13:41:54 +080042#ifdef USE_QTI_AIDL_HAPTICS_FQNAME
43static const std::string kVibratorInstance = std::string("android.hardware.vibrator.") + USE_QTI_AIDL_HAPTICS_FQNAME;
44#else
nebrassy76224bd2021-04-05 11:52:44 +020045static const std::string kVibratorInstance = std::string(IVibrator::descriptor) + "/default";
46#endif
sekaiacgacc522f2022-06-25 13:45:17 +080047#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
48static std::atomic_int vib_on_count = 0;
49#endif
sekaiacg38d6efd2022-06-25 13:41:54 +080050#endif
nebrassy76224bd2021-04-05 11:52:44 +020051
bigbiff1f9e4842020-10-31 11:33:15 -040052#include "common.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040053
bigbiffd81833a2021-01-17 11:06:57 -050054#include "minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040055
56//#define _EVENT_LOGGING
57
Dees_Troy3f23d9e2013-05-03 21:04:05 +000058#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
61#define VIBRATOR_TIME_MS 50
62
notsyncingc7c78d22018-06-06 20:26:47 +080063#define LEDS_HAPTICS_DURATION_FILE "/sys/class/leds/vibrator/duration"
64#define LEDS_HAPTICS_ACTIVATE_FILE "/sys/class/leds/vibrator/activate"
65
Dees_Troyf7596752012-09-28 13:21:36 -040066#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040067#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040068#endif
69#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040070#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040071#endif
72#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040073#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040074#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040075
76#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
77#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
78#define ABS_MT_SLOT 0x2f
79#define ABS_MT_TOUCH_MAJOR 0x30
80#define ABS_MT_TOUCH_MINOR 0x31
81#define ABS_MT_WIDTH_MAJOR 0x32
82#define ABS_MT_WIDTH_MINOR 0x33
83#define ABS_MT_ORIENTATION 0x34
84#define ABS_MT_POSITION_X 0x35
85#define ABS_MT_POSITION_Y 0x36
86#define ABS_MT_TOOL_TYPE 0x37
87#define ABS_MT_BLOB_ID 0x38
88#define ABS_MT_TRACKING_ID 0x39
89#define ABS_MT_PRESSURE 0x3a
90#define ABS_MT_DISTANCE 0x3b
91
92enum {
93 DOWN_NOT,
94 DOWN_SENT,
95 DOWN_RELEASED,
96};
97
98struct virtualkey {
99 int scancode;
100 int centerx, centery;
101 int width, height;
102};
103
104struct position {
105 int x, y;
106 int synced;
107 struct input_absinfo xi, yi;
108};
109
110struct ev {
111 struct pollfd *fd;
112
113 struct virtualkey *vks;
114 int vk_count;
115
116 char deviceName[64];
117
118 int ignored;
119
120 struct position p, mt_p;
121 int down;
122};
123
124static struct pollfd ev_fds[MAX_DEVICES];
125static struct ev evs[MAX_DEVICES];
126static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100127static struct timeval lastInputStat;
Ethan Yonker58f21322018-08-24 11:17:36 -0500128static time_t lastInputMTime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100129static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400130
131static inline int ABS(int x) {
132 return x<0?-x:x;
133}
134
notsyncingc7c78d22018-06-06 20:26:47 +0800135int write_to_file(const std::string& fn, const std::string& line) {
136 FILE *file;
137 file = fopen(fn.c_str(), "w");
138 if (file != NULL) {
139 fwrite(line.c_str(), line.size(), 1, file);
140 fclose(file);
141 return 0;
142 }
143 LOGI("Cannot find file %s\n", fn.c_str());
144 return -1;
145}
146
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400147#ifndef TW_NO_HAPTICS
LameMonster824ef96a62020-06-04 13:31:12 +0200148#ifndef TW_HAPTICS_TSPDRV
Dees_Troy51a0e822012-09-05 15:24:24 -0400149int vibrate(int timeout_ms)
150{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000151 if (timeout_ms > 10000) timeout_ms = 1000;
Dees Troy9a4d7402019-03-21 14:17:28 -0400152 char tout[6];
153 sprintf(tout, "%i", timeout_ms);
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000154
Mohd Farazbc576cd2020-05-30 21:06:32 +0530155#ifdef USE_QTI_HAPTICS
156 android::sp<android::hardware::vibrator::V1_2::IVibrator> vib = android::hardware::vibrator::V1_2::IVibrator::getService();
157 if (vib != nullptr) {
158 vib->on((uint32_t)timeout_ms);
159 }
nebrassy76224bd2021-04-05 11:52:44 +0200160#elif defined(USE_QTI_AIDL_HAPTICS)
161 std::shared_ptr<IVibrator> vib = IVibrator::fromBinder(ndk::SpAIBinder(AServiceManager_getService(kVibratorInstance.c_str())));
162 if (vib != nullptr) {
sekaiacgacc522f2022-06-25 13:45:17 +0800163#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
164 std::thread([vib, timeout_ms] {
165 if (vib->on((uint32_t)timeout_ms, nullptr).isOk()) {
166 vib_on_count++;
167 usleep(timeout_ms * 1000);
168 vib_on_count--;
169 if (!vib_on_count) vib->off();
170 }
171 }).detach();
172#else
nebrassy76224bd2021-04-05 11:52:44 +0200173 vib->on((uint32_t)timeout_ms, nullptr);
sekaiacgacc522f2022-06-25 13:45:17 +0800174#endif
nebrassy76224bd2021-04-05 11:52:44 +0200175 }
soulr344f831bff2021-08-16 09:46:38 +0500176#elif defined(USE_SAMSUNG_HAPTICS)
177 /* Newer Samsung devices have duration file only
178 0 in VIBRATOR_TIMEOUT_FILE means no vibration
179 Anything else is the vibration running for X milliseconds */
180 if (std::ifstream(VIBRATOR_TIMEOUT_FILE).good()) {
181 write_to_file(VIBRATOR_TIMEOUT_FILE, tout);
182 }
Mohd Farazbc576cd2020-05-30 21:06:32 +0530183#else
notsyncingc7c78d22018-06-06 20:26:47 +0800184 if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) {
Dees Troy9a4d7402019-03-21 14:17:28 -0400185 write_to_file(LEDS_HAPTICS_DURATION_FILE, tout);
notsyncingc7c78d22018-06-06 20:26:47 +0800186 write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1");
187 } else
Dees Troy9a4d7402019-03-21 14:17:28 -0400188 write_to_file(VIBRATOR_TIMEOUT_FILE, tout);
Mohd Farazbc576cd2020-05-30 21:06:32 +0530189#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400190 return 0;
191}
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400192#endif
LameMonster824ef96a62020-06-04 13:31:12 +0200193#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400194
195/* Returns empty tokens */
196static char *vk_strtok_r(char *str, const char *delim, char **save_str)
197{
198 if(!str)
199 {
200 if(!*save_str)
201 return NULL;
202
203 str = (*save_str) + 1;
204 }
205 *save_str = strpbrk(str, delim);
206
207 if (*save_str)
208 **save_str = '\0';
209
210 return str;
211}
212
213static int vk_init(struct ev *e)
214{
215 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
216 char vks[2048], *ts = NULL;
217 ssize_t len;
218 int vk_fd;
219 int i;
220
221 e->vk_count = 0;
222
223 len = strlen(vk_path);
224 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
225 if (len <= 0)
226 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600227 LOGE("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400228 return -1;
229 }
230#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000231 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400232#endif
233
Flemmardf4674612014-01-10 09:14:44 +0100234#ifdef WHITELIST_INPUT
235 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
236 {
237 e->ignored = 1;
238 }
239#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500240#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500241 // 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 -0500242 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400243 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600244 LOGI("Blacklisting input device: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400245 e->ignored = 1;
246 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500247#else
248 char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST));
249 char* blacklist = strtok(bl, "\n");
250
251 while (blacklist != NULL) {
252 if (strcmp(e->deviceName, blacklist) == 0) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600253 LOGI("Blacklisting input device: %s\n", blacklist);
Ethan Yonker5742a402014-08-12 14:52:49 -0500254 e->ignored = 1;
255 }
256 blacklist = strtok(NULL, "\n");
257 }
258 free(bl);
259#endif
Flemmardf4674612014-01-10 09:14:44 +0100260#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400261
262 strcat(vk_path, e->deviceName);
263
264 // Some devices split the keys from the touchscreen
265 e->vk_count = 0;
266 vk_fd = open(vk_path, O_RDONLY);
267 if (vk_fd >= 0)
268 {
269 len = read(vk_fd, vks, sizeof(vks)-1);
270 close(vk_fd);
271 if (len <= 0)
272 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500273
Dees_Troy51a0e822012-09-05 15:24:24 -0400274 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500275
Dees_Troy51a0e822012-09-05 15:24:24 -0400276 /* Parse a line like:
277 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
278 */
279 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
280 if (*ts == ':')
281 ++e->vk_count;
282 }
283
284 if (e->vk_count % 6) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600285 LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400286 }
287 e->vk_count /= 6;
288 if (e->vk_count <= 0)
289 return -1;
290
291 e->down = DOWN_NOT;
292 }
293
294 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
295 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
296 e->p.synced = 0;
297#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000298 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 -0400299#endif
300
301 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
302 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
303 e->mt_p.synced = 0;
304#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000305 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 -0400306#endif
307
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100308 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400309
310 for (i = 0; i < e->vk_count; ++i) {
311 char *token[6];
312 int j;
313
314 for (j = 0; j < 6; ++j) {
315 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
316 }
317
318 if (strcmp(token[0], "0x01") != 0) {
319 /* Java does string compare, so we do too. */
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600320 LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400321 continue;
322 }
323
324 e->vks[i].scancode = strtol(token[1], NULL, 0);
325 e->vks[i].centerx = strtol(token[2], NULL, 0);
326 e->vks[i].centery = strtol(token[3], NULL, 0);
327 e->vks[i].width = strtol(token[4], NULL, 0);
328 e->vks[i].height = strtol(token[5], NULL, 0);
329 }
330
331 return 0;
332}
333
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100334#define BITS_PER_LONG (sizeof(long) * 8)
335#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
336#define OFF(x) ((x)%BITS_PER_LONG)
337#define LONG(x) ((x)/BITS_PER_LONG)
338#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100339
340// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
341// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500342static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100343{
344 if(has_mouse)
345 return;
346
347 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
348 memset(bit, 0, sizeof(bit));
349 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
350
Vojtech Bocek971d3182014-02-20 21:43:28 +0100351 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100352 return;
353
354 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100355 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
356 return;
357
358 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
359 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
360 return;
361
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600362 LOGI("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100363 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100364}
365
366int ev_has_mouse(void)
367{
368 return has_mouse;
369}
370
Dees_Troy51a0e822012-09-05 15:24:24 -0400371int ev_init(void)
372{
373 DIR *dir;
374 struct dirent *de;
375 int fd;
376
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100377 has_mouse = 0;
378
Dees_Troy51a0e822012-09-05 15:24:24 -0400379 dir = opendir("/dev/input");
380 if(dir != 0) {
381 while((de = readdir(dir))) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600382#ifdef _EVENT_LOGGING
383 fprintf(stderr,"/dev/input/%s\n", de->d_name);
384#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400385 if(strncmp(de->d_name,"event",5)) continue;
386 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
387 if(fd < 0) continue;
388
389 ev_fds[ev_count].fd = fd;
390 ev_fds[ev_count].events = POLLIN;
391 evs[ev_count].fd = &ev_fds[ev_count];
392
393 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500394 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400395
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500396 if (!evs[ev_count].ignored)
397 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100398
Dees_Troy51a0e822012-09-05 15:24:24 -0400399 ev_count++;
400 if(ev_count == MAX_DEVICES) break;
401 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100402 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400403 }
404
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100405 struct stat st;
406 if(stat("/dev/input", &st) >= 0)
407 lastInputMTime = st.st_mtime;
408 gettimeofday(&lastInputStat, NULL);
409
Dees_Troy51a0e822012-09-05 15:24:24 -0400410 return 0;
411}
412
413void ev_exit(void)
414{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100415 while (ev_count-- > 0) {
416 if (evs[ev_count].vk_count) {
417 free(evs[ev_count].vks);
418 evs[ev_count].vk_count = 0;
419 }
420 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400421 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100422 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400423}
424
Ethan Yonker58f21322018-08-24 11:17:36 -0500425/*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
Dees_Troy51a0e822012-09-05 15:24:24 -0400426{
427 int screen_pos;
428
429 if (info->minimum == info->maximum)
430 return 0;
431
432 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
433 return (screen_pos >= 0 && screen_pos < screen_size);
Ethan Yonker58f21322018-08-24 11:17:36 -0500434}*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400435
Dees_Troyb7ecc092013-08-24 12:15:41 +0000436static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400437{
438 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
439 {
440 // In this case, we assume the screen dimensions are the same.
441 *x = p->x;
442 *y = p->y;
443 return 0;
444 }
445
Dees_Troyb7ecc092013-08-24 12:15:41 +0000446#ifdef _EVENT_LOGGING
447 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
448#endif
449
Dees_Troy51a0e822012-09-05 15:24:24 -0400450#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000451 int fb_width = gr_fb_width();
452 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400453#else
454 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000455 int fb_width = gr_fb_height();
456 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400457#endif
458
459 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
460 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
461
462 if (*x >= 0 && *x < fb_width &&
463 *y >= 0 && *y < fb_height)
464 {
465 return 0;
466 }
467
468 return 1;
469}
470
471/* Translate a virtual key in to a real key event, if needed */
472/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000473static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400474{
475 static int downX = -1, downY = -1;
476 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100477 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400478 static int lastWasSynReport = 0;
479 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000480 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 -0400481 int i;
482 int x, y;
483
484 // This is used to ditch useless event handlers, like an accelerometer
485 if (e->ignored) return 1;
486
487 if (ev->type == EV_REL && ev->code == REL_Z)
488 {
489 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
490#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000491 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400492#endif
493 e->ignored = 1;
494 return 1;
495 }
496
497#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000498 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 -0400499#endif
500
501 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
502 if (ev->type == EV_KEY) {
503 return 0;
504 }
505
506 if (ev->type == EV_ABS) {
507 switch (ev->code) {
508
509 case ABS_X: //00
510 e->p.synced |= 0x01;
511 e->p.x = ev->value;
512#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000513 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400514#endif
515 break;
516
517 case ABS_Y: //01
518 e->p.synced |= 0x02;
519 e->p.y = ev->value;
520#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000521 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522#endif
523 break;
524
525 case ABS_MT_POSITION: //2a
526 e->mt_p.synced = 0x03;
527 if (ev->value == (1 << 31))
528 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600529#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 e->mt_p.x = 0;
531 e->mt_p.y = 0;
532 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600533#endif
534#ifdef _EVENT_LOGGING
535#ifndef TW_IGNORE_MT_POSITION_0
536 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
537#else
538 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
539#endif
540#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400541 }
542 else
543 {
544 lastWasSynReport = 0;
545 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
546 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600547#ifdef _EVENT_LOGGING
548 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));
549#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400550 }
551 break;
552
553 case ABS_MT_TOUCH_MAJOR: //30
554 if (ev->value == 0)
555 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800556#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 // We're in a touch release, although some devices will still send positions as well
558 e->mt_p.x = 0;
559 e->mt_p.y = 0;
560 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800561#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400562 }
563#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000564 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400565#endif
566 break;
567
568 case ABS_MT_PRESSURE: //3a
569 if (ev->value == 0)
570 {
571 // We're in a touch release, although some devices will still send positions as well
572 e->mt_p.x = 0;
573 e->mt_p.y = 0;
574 touchReleaseOnNextSynReport = 1;
575 }
576#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400578#endif
579 break;
580
581 case ABS_MT_POSITION_X: //35
582 e->mt_p.synced |= 0x01;
583 e->mt_p.x = ev->value;
584#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000585 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400586#endif
587 break;
588
589 case ABS_MT_POSITION_Y: //36
590 e->mt_p.synced |= 0x02;
591 e->mt_p.y = ev->value;
592#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000593 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400594#endif
595 break;
596
Dees_Troy51a0e822012-09-05 15:24:24 -0400597 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000598#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000599 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000600#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400601 break;
602
603 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000604#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000605 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000606#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400607 break;
608
609 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000610#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000611 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000612#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400613 break;
614
Dees_Troyd86400b2013-05-13 19:04:35 +0000615 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500616#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
617#ifdef _EVENT_LOGGING
618 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
619#endif
620 return 1;
621#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000622 if (ev->value < 0) {
623 e->mt_p.x = 0;
624 e->mt_p.y = 0;
625 touchReleaseOnNextSynReport = 2;
626 use_tracking_id_negative_as_touch_release = 1;
627#ifdef _EVENT_LOGGING
628 if (use_tracking_id_negative_as_touch_release)
629 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
630#endif
631 }
632#ifdef _EVENT_LOGGING
633 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
634#endif
635 break;
636
637#ifdef _EVENT_LOGGING
638 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400639 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000640 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400641 return 1;
642 break;
643
644 case ABS_MT_TOOL_TYPE: //37
645 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
646 return 1;
647 break;
648
649 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000650 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400651 return 1;
652 break;
653
Dees_Troy51a0e822012-09-05 15:24:24 -0400654 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000655 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400656 return 1;
657 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500658 case ABS_MT_SLOT:
659 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
660 return 1;
661 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400662#endif
663
664 default:
665 // This is an unhandled message, just skip it
666 return 1;
667 }
668
669 if (ev->code != ABS_MT_POSITION)
670 {
671 lastWasSynReport = 0;
672 return 1;
673 }
674 }
675
676 // Check if we should ignore the message
677 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
678 {
679 lastWasSynReport = 0;
680 return 0;
681 }
682
683#ifdef _EVENT_LOGGING
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600684 if (ev->type == EV_SYN && ev->code == SYN_REPORT)
685 printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
686 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT)
687 printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400688#endif
689
690 // Discard the MT versions
691 if (ev->code == SYN_MT_REPORT) return 0;
692
Dees_Troyd86400b2013-05-13 19:04:35 +0000693 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 -0400694 {
695 // Reset the value
696 touchReleaseOnNextSynReport = 0;
697
698 // We are a finger-up state
699 if (!discard)
700 {
701 // Report the key up
702 ev->type = EV_ABS;
703 ev->code = 0;
704 ev->value = (downX << 16) | downY;
705 }
706 downX = -1;
707 downY = -1;
708 if (discard)
709 {
710 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100711
712 // Send the keyUp event
713 ev->type = EV_KEY;
714 ev->code = last_virt_key;
715 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400716 }
717 return 0;
718 }
719 lastWasSynReport = 1;
720
721 // Retrieve where the x,y position is
722 if (e->p.synced & 0x03)
723 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000724 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400725 }
726 else if (e->mt_p.synced & 0x03)
727 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000728 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400729 }
730 else
731 {
732 // We don't have useful information to convey
733 return 1;
734 }
735
736#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
737 x ^= y;
738 y ^= x;
739 x ^= y;
740#endif
741#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000742 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400743#endif
744#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000745 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400746#endif
747
748#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000749 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400750#endif
751
752 // Clear the current sync states
753 e->p.synced = e->mt_p.synced = 0;
754
755 // If we have nothing useful to report, skip it
756 if (x == -1 || y == -1) return 1;
757
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500758 // Special case, we'll ignore touches on 0,0 because it usually means
759 // that we received extra data after our last sync and x and y were
760 // reset to 0. We should not be using 0,0 anyway.
761 if (x == 0 && y == 0)
762 return 1;
763
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 // On first touch, see if we're at a virtual key
765 if (downX == -1)
766 {
767 // Attempt mapping to virtual key
768 for (i = 0; i < e->vk_count; ++i)
769 {
770 int xd = ABS(e->vks[i].centerx - x);
771 int yd = ABS(e->vks[i].centery - y);
772
773 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
774 {
775 ev->type = EV_KEY;
776 ev->code = e->vks[i].scancode;
777 ev->value = 1;
778
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100779 last_virt_key = e->vks[i].scancode;
780
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400781#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400782 vibrate(VIBRATOR_TIME_MS);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400783#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400784
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500785 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400786 // and make sure we don't come back into this area
787 discard = 1;
788 downX = 0;
789 return 0;
790 }
791 }
792 }
793
794 // If we were originally a button press, discard this event
795 if (discard)
796 {
797 return 1;
798 }
799
800 // Record where we started the touch for deciding if this is a key or a scroll
801 downX = x;
802 downY = y;
803
804 ev->type = EV_ABS;
805 ev->code = 1;
806 ev->value = (x << 16) | y;
807 return 0;
808}
809
thatde72b6d2015-02-08 08:55:00 +0100810int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400811{
812 int r;
813 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100814 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400815
Ethan Yonkere13fa632015-01-27 11:30:03 -0600816 gettimeofday(&curr, NULL);
817 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
818 {
819 struct stat st;
820 stat("/dev/input", &st);
821 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100822 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600823 LOGI("Reloading input devices\n");
Ethan Yonkere13fa632015-01-27 11:30:03 -0600824 ev_exit();
825 ev_init();
826 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100827 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600828 lastInputStat = curr;
829 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100830
thatde72b6d2015-02-08 08:55:00 +0100831 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400832
Ethan Yonkere13fa632015-01-27 11:30:03 -0600833 if(r > 0) {
834 for(n = 0; n < ev_count; n++) {
835 if(ev_fds[n].revents & POLLIN) {
836 r = read(ev_fds[n].fd, ev, sizeof(*ev));
837 if(r == sizeof(*ev)) {
838 if (!vk_modify(&evs[n], ev))
839 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400840 }
841 }
842 }
thatc5837f32015-02-01 01:59:43 +0100843 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600844 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400845
thatc5837f32015-02-01 01:59:43 +0100846 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400847}
848
Ethan Yonker58f21322018-08-24 11:17:36 -0500849int ev_wait(int timeout __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400850{
851 return -1;
852}
853
854void ev_dispatch(void)
855{
856 return;
857}
858
Ethan Yonker58f21322018-08-24 11:17:36 -0500859int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400860{
861 return -1;
862}