blob: e497df62f97708eee5f36df7a27a608d516e11ed [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"
bigbiffeb320712023-12-26 19:35:47 -050053#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040054
bigbiffd81833a2021-01-17 11:06:57 -050055#include "minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040056
57//#define _EVENT_LOGGING
58
Dees_Troy3f23d9e2013-05-03 21:04:05 +000059#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040060
61#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
62#define VIBRATOR_TIME_MS 50
63
notsyncingc7c78d22018-06-06 20:26:47 +080064#define LEDS_HAPTICS_DURATION_FILE "/sys/class/leds/vibrator/duration"
65#define LEDS_HAPTICS_ACTIVATE_FILE "/sys/class/leds/vibrator/activate"
66
Dees_Troyf7596752012-09-28 13:21:36 -040067#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040068#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040069#endif
70#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040071#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040072#endif
73#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040074#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040075#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040076
77#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
78#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
79#define ABS_MT_SLOT 0x2f
80#define ABS_MT_TOUCH_MAJOR 0x30
81#define ABS_MT_TOUCH_MINOR 0x31
82#define ABS_MT_WIDTH_MAJOR 0x32
83#define ABS_MT_WIDTH_MINOR 0x33
84#define ABS_MT_ORIENTATION 0x34
85#define ABS_MT_POSITION_X 0x35
86#define ABS_MT_POSITION_Y 0x36
87#define ABS_MT_TOOL_TYPE 0x37
88#define ABS_MT_BLOB_ID 0x38
89#define ABS_MT_TRACKING_ID 0x39
90#define ABS_MT_PRESSURE 0x3a
91#define ABS_MT_DISTANCE 0x3b
92
93enum {
94 DOWN_NOT,
95 DOWN_SENT,
96 DOWN_RELEASED,
97};
98
99struct virtualkey {
100 int scancode;
101 int centerx, centery;
102 int width, height;
103};
104
105struct position {
106 int x, y;
107 int synced;
108 struct input_absinfo xi, yi;
109};
110
111struct ev {
112 struct pollfd *fd;
113
114 struct virtualkey *vks;
115 int vk_count;
116
117 char deviceName[64];
118
119 int ignored;
120
121 struct position p, mt_p;
122 int down;
123};
124
125static struct pollfd ev_fds[MAX_DEVICES];
126static struct ev evs[MAX_DEVICES];
127static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100128static struct timeval lastInputStat;
Ethan Yonker58f21322018-08-24 11:17:36 -0500129static time_t lastInputMTime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100130static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400131
132static inline int ABS(int x) {
133 return x<0?-x:x;
134}
135
notsyncingc7c78d22018-06-06 20:26:47 +0800136int write_to_file(const std::string& fn, const std::string& line) {
137 FILE *file;
138 file = fopen(fn.c_str(), "w");
139 if (file != NULL) {
140 fwrite(line.c_str(), line.size(), 1, file);
141 fclose(file);
142 return 0;
143 }
144 LOGI("Cannot find file %s\n", fn.c_str());
145 return -1;
146}
147
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400148#ifndef TW_NO_HAPTICS
LameMonster824ef96a62020-06-04 13:31:12 +0200149#ifndef TW_HAPTICS_TSPDRV
Dees_Troy51a0e822012-09-05 15:24:24 -0400150int vibrate(int timeout_ms)
151{
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000152 if (timeout_ms > 10000) timeout_ms = 1000;
Dees Troy9a4d7402019-03-21 14:17:28 -0400153 char tout[6];
154 sprintf(tout, "%i", timeout_ms);
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000155
Mohd Farazbc576cd2020-05-30 21:06:32 +0530156#ifdef USE_QTI_HAPTICS
157 android::sp<android::hardware::vibrator::V1_2::IVibrator> vib = android::hardware::vibrator::V1_2::IVibrator::getService();
158 if (vib != nullptr) {
159 vib->on((uint32_t)timeout_ms);
160 }
nebrassy76224bd2021-04-05 11:52:44 +0200161#elif defined(USE_QTI_AIDL_HAPTICS)
162 std::shared_ptr<IVibrator> vib = IVibrator::fromBinder(ndk::SpAIBinder(AServiceManager_getService(kVibratorInstance.c_str())));
163 if (vib != nullptr) {
sekaiacgacc522f2022-06-25 13:45:17 +0800164#ifdef USE_QTI_AIDL_HAPTICS_FIX_OFF
165 std::thread([vib, timeout_ms] {
166 if (vib->on((uint32_t)timeout_ms, nullptr).isOk()) {
167 vib_on_count++;
168 usleep(timeout_ms * 1000);
169 vib_on_count--;
170 if (!vib_on_count) vib->off();
171 }
172 }).detach();
173#else
nebrassy76224bd2021-04-05 11:52:44 +0200174 vib->on((uint32_t)timeout_ms, nullptr);
sekaiacgacc522f2022-06-25 13:45:17 +0800175#endif
nebrassy76224bd2021-04-05 11:52:44 +0200176 }
soulr344f831bff2021-08-16 09:46:38 +0500177#elif defined(USE_SAMSUNG_HAPTICS)
178 /* Newer Samsung devices have duration file only
179 0 in VIBRATOR_TIMEOUT_FILE means no vibration
180 Anything else is the vibration running for X milliseconds */
181 if (std::ifstream(VIBRATOR_TIMEOUT_FILE).good()) {
182 write_to_file(VIBRATOR_TIMEOUT_FILE, tout);
183 }
Mohd Farazbc576cd2020-05-30 21:06:32 +0530184#else
notsyncingc7c78d22018-06-06 20:26:47 +0800185 if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) {
Dees Troy9a4d7402019-03-21 14:17:28 -0400186 write_to_file(LEDS_HAPTICS_DURATION_FILE, tout);
notsyncingc7c78d22018-06-06 20:26:47 +0800187 write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1");
188 } else
Dees Troy9a4d7402019-03-21 14:17:28 -0400189 write_to_file(VIBRATOR_TIMEOUT_FILE, tout);
Mohd Farazbc576cd2020-05-30 21:06:32 +0530190#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400191 return 0;
192}
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400193#endif
LameMonster824ef96a62020-06-04 13:31:12 +0200194#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400195
196/* Returns empty tokens */
197static char *vk_strtok_r(char *str, const char *delim, char **save_str)
198{
199 if(!str)
200 {
201 if(!*save_str)
202 return NULL;
203
204 str = (*save_str) + 1;
205 }
206 *save_str = strpbrk(str, delim);
207
208 if (*save_str)
209 **save_str = '\0';
210
211 return str;
212}
213
214static int vk_init(struct ev *e)
215{
216 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
217 char vks[2048], *ts = NULL;
218 ssize_t len;
219 int vk_fd;
220 int i;
221
222 e->vk_count = 0;
223
224 len = strlen(vk_path);
225 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
226 if (len <= 0)
227 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600228 LOGE("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400229 return -1;
230 }
231#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000232 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400233#endif
234
Flemmardf4674612014-01-10 09:14:44 +0100235#ifdef WHITELIST_INPUT
236 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
237 {
238 e->ignored = 1;
239 }
240#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500241#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500242 // 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 -0500243 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400244 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600245 LOGI("Blacklisting input device: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400246 e->ignored = 1;
247 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500248#else
249 char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST));
250 char* blacklist = strtok(bl, "\n");
251
252 while (blacklist != NULL) {
253 if (strcmp(e->deviceName, blacklist) == 0) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600254 LOGI("Blacklisting input device: %s\n", blacklist);
Ethan Yonker5742a402014-08-12 14:52:49 -0500255 e->ignored = 1;
256 }
257 blacklist = strtok(NULL, "\n");
258 }
259 free(bl);
260#endif
Flemmardf4674612014-01-10 09:14:44 +0100261#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400262
263 strcat(vk_path, e->deviceName);
264
265 // Some devices split the keys from the touchscreen
266 e->vk_count = 0;
267 vk_fd = open(vk_path, O_RDONLY);
268 if (vk_fd >= 0)
269 {
270 len = read(vk_fd, vks, sizeof(vks)-1);
271 close(vk_fd);
272 if (len <= 0)
273 return -1;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500274
Dees_Troy51a0e822012-09-05 15:24:24 -0400275 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500276
Dees_Troy51a0e822012-09-05 15:24:24 -0400277 /* Parse a line like:
278 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
279 */
280 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
281 if (*ts == ':')
282 ++e->vk_count;
283 }
284
285 if (e->vk_count % 6) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600286 LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400287 }
288 e->vk_count /= 6;
289 if (e->vk_count <= 0)
290 return -1;
291
292 e->down = DOWN_NOT;
293 }
294
295 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
296 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
297 e->p.synced = 0;
298#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000299 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 -0400300#endif
301
302 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
303 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
304 e->mt_p.synced = 0;
305#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000306 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 -0400307#endif
308
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100309 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400310
311 for (i = 0; i < e->vk_count; ++i) {
312 char *token[6];
313 int j;
314
315 for (j = 0; j < 6; ++j) {
316 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
317 }
318
319 if (strcmp(token[0], "0x01") != 0) {
320 /* Java does string compare, so we do too. */
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600321 LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400322 continue;
323 }
324
325 e->vks[i].scancode = strtol(token[1], NULL, 0);
326 e->vks[i].centerx = strtol(token[2], NULL, 0);
327 e->vks[i].centery = strtol(token[3], NULL, 0);
328 e->vks[i].width = strtol(token[4], NULL, 0);
329 e->vks[i].height = strtol(token[5], NULL, 0);
330 }
331
332 return 0;
333}
334
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100335#define BITS_PER_LONG (sizeof(long) * 8)
336#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
337#define OFF(x) ((x)%BITS_PER_LONG)
338#define LONG(x) ((x)/BITS_PER_LONG)
339#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100340
341// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
342// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500343static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100344{
345 if(has_mouse)
346 return;
347
348 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
349 memset(bit, 0, sizeof(bit));
350 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
351
Vojtech Bocek971d3182014-02-20 21:43:28 +0100352 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100353 return;
354
355 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100356 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
357 return;
358
359 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
360 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
361 return;
362
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600363 LOGI("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100364 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100365}
366
367int ev_has_mouse(void)
368{
369 return has_mouse;
370}
371
Dees_Troy51a0e822012-09-05 15:24:24 -0400372int ev_init(void)
373{
374 DIR *dir;
375 struct dirent *de;
376 int fd;
377
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100378 has_mouse = 0;
379
Dees_Troy51a0e822012-09-05 15:24:24 -0400380 dir = opendir("/dev/input");
381 if(dir != 0) {
382 while((de = readdir(dir))) {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600383#ifdef _EVENT_LOGGING
384 fprintf(stderr,"/dev/input/%s\n", de->d_name);
385#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400386 if(strncmp(de->d_name,"event",5)) continue;
387 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
388 if(fd < 0) continue;
389
390 ev_fds[ev_count].fd = fd;
391 ev_fds[ev_count].events = POLLIN;
392 evs[ev_count].fd = &ev_fds[ev_count];
393
394 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500395 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400396
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500397 if (!evs[ev_count].ignored)
398 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100399
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 ev_count++;
401 if(ev_count == MAX_DEVICES) break;
402 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100403 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400404 }
405
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100406 struct stat st;
407 if(stat("/dev/input", &st) >= 0)
408 lastInputMTime = st.st_mtime;
409 gettimeofday(&lastInputStat, NULL);
410
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 return 0;
412}
413
414void ev_exit(void)
415{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100416 while (ev_count-- > 0) {
417 if (evs[ev_count].vk_count) {
418 free(evs[ev_count].vks);
419 evs[ev_count].vk_count = 0;
420 }
421 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400422 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100423 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400424}
425
Ethan Yonker58f21322018-08-24 11:17:36 -0500426/*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
Dees_Troy51a0e822012-09-05 15:24:24 -0400427{
428 int screen_pos;
429
430 if (info->minimum == info->maximum)
431 return 0;
432
433 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
434 return (screen_pos >= 0 && screen_pos < screen_size);
Ethan Yonker58f21322018-08-24 11:17:36 -0500435}*/
Dees_Troy51a0e822012-09-05 15:24:24 -0400436
Dees_Troyb7ecc092013-08-24 12:15:41 +0000437static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400438{
439 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
440 {
441 // In this case, we assume the screen dimensions are the same.
442 *x = p->x;
443 *y = p->y;
444 return 0;
445 }
446
Dees_Troyb7ecc092013-08-24 12:15:41 +0000447#ifdef _EVENT_LOGGING
448 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
449#endif
450
Dees_Troy51a0e822012-09-05 15:24:24 -0400451#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000452 int fb_width = gr_fb_width();
453 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400454#else
455 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000456 int fb_width = gr_fb_height();
457 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400458#endif
459
460 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
461 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
462
463 if (*x >= 0 && *x < fb_width &&
464 *y >= 0 && *y < fb_height)
465 {
466 return 0;
467 }
468
469 return 1;
470}
471
472/* Translate a virtual key in to a real key event, if needed */
473/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000474static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400475{
476 static int downX = -1, downY = -1;
477 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100478 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 static int lastWasSynReport = 0;
480 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000481 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 -0400482 int i;
483 int x, y;
484
485 // This is used to ditch useless event handlers, like an accelerometer
486 if (e->ignored) return 1;
487
488 if (ev->type == EV_REL && ev->code == REL_Z)
489 {
490 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
491#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000492 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400493#endif
494 e->ignored = 1;
495 return 1;
496 }
497
498#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000499 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 -0400500#endif
501
502 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
503 if (ev->type == EV_KEY) {
504 return 0;
505 }
506
507 if (ev->type == EV_ABS) {
508 switch (ev->code) {
509
510 case ABS_X: //00
511 e->p.synced |= 0x01;
512 e->p.x = ev->value;
513#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000514 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400515#endif
516 break;
517
518 case ABS_Y: //01
519 e->p.synced |= 0x02;
520 e->p.y = ev->value;
521#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000522 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400523#endif
524 break;
525
526 case ABS_MT_POSITION: //2a
527 e->mt_p.synced = 0x03;
528 if (ev->value == (1 << 31))
529 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600530#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400531 e->mt_p.x = 0;
532 e->mt_p.y = 0;
533 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600534#endif
535#ifdef _EVENT_LOGGING
536#ifndef TW_IGNORE_MT_POSITION_0
537 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
538#else
539 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
540#endif
541#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400542 }
543 else
544 {
545 lastWasSynReport = 0;
546 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
547 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600548#ifdef _EVENT_LOGGING
549 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));
550#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400551 }
552 break;
553
554 case ABS_MT_TOUCH_MAJOR: //30
555 if (ev->value == 0)
556 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800557#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400558 // We're in a touch release, although some devices will still send positions as well
559 e->mt_p.x = 0;
560 e->mt_p.y = 0;
561 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800562#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400563 }
564#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000565 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400566#endif
567 break;
568
569 case ABS_MT_PRESSURE: //3a
570 if (ev->value == 0)
571 {
572 // We're in a touch release, although some devices will still send positions as well
573 e->mt_p.x = 0;
574 e->mt_p.y = 0;
575 touchReleaseOnNextSynReport = 1;
576 }
577#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000578 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400579#endif
580 break;
581
582 case ABS_MT_POSITION_X: //35
583 e->mt_p.synced |= 0x01;
584 e->mt_p.x = ev->value;
585#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000586 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587#endif
588 break;
589
590 case ABS_MT_POSITION_Y: //36
591 e->mt_p.synced |= 0x02;
592 e->mt_p.y = ev->value;
593#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000594 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400595#endif
596 break;
597
Dees_Troy51a0e822012-09-05 15:24:24 -0400598 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000599#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000600 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000601#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400602 break;
603
604 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000605#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000606 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000607#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400608 break;
609
610 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000611#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000612 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000613#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400614 break;
615
Dees_Troyd86400b2013-05-13 19:04:35 +0000616 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500617#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
618#ifdef _EVENT_LOGGING
619 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
620#endif
621 return 1;
622#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000623 if (ev->value < 0) {
624 e->mt_p.x = 0;
625 e->mt_p.y = 0;
626 touchReleaseOnNextSynReport = 2;
627 use_tracking_id_negative_as_touch_release = 1;
628#ifdef _EVENT_LOGGING
629 if (use_tracking_id_negative_as_touch_release)
630 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
631#endif
632 }
633#ifdef _EVENT_LOGGING
634 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
635#endif
636 break;
637
638#ifdef _EVENT_LOGGING
639 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400640 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000641 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 return 1;
643 break;
644
645 case ABS_MT_TOOL_TYPE: //37
646 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
647 return 1;
648 break;
649
650 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000651 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400652 return 1;
653 break;
654
Dees_Troy51a0e822012-09-05 15:24:24 -0400655 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000656 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400657 return 1;
658 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500659 case ABS_MT_SLOT:
660 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
661 return 1;
662 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400663#endif
664
665 default:
666 // This is an unhandled message, just skip it
667 return 1;
668 }
669
670 if (ev->code != ABS_MT_POSITION)
671 {
672 lastWasSynReport = 0;
673 return 1;
674 }
675 }
676
677 // Check if we should ignore the message
678 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
679 {
680 lastWasSynReport = 0;
681 return 0;
682 }
683
684#ifdef _EVENT_LOGGING
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600685 if (ev->type == EV_SYN && ev->code == SYN_REPORT)
686 printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
687 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT)
688 printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400689#endif
690
691 // Discard the MT versions
692 if (ev->code == SYN_MT_REPORT) return 0;
693
Dees_Troyd86400b2013-05-13 19:04:35 +0000694 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 -0400695 {
696 // Reset the value
697 touchReleaseOnNextSynReport = 0;
698
699 // We are a finger-up state
700 if (!discard)
701 {
702 // Report the key up
703 ev->type = EV_ABS;
704 ev->code = 0;
705 ev->value = (downX << 16) | downY;
706 }
707 downX = -1;
708 downY = -1;
709 if (discard)
710 {
711 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100712
713 // Send the keyUp event
714 ev->type = EV_KEY;
715 ev->code = last_virt_key;
716 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400717 }
718 return 0;
719 }
720 lastWasSynReport = 1;
721
722 // Retrieve where the x,y position is
723 if (e->p.synced & 0x03)
724 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000725 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400726 }
727 else if (e->mt_p.synced & 0x03)
728 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000729 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400730 }
731 else
732 {
733 // We don't have useful information to convey
734 return 1;
735 }
736
737#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
738 x ^= y;
739 y ^= x;
740 x ^= y;
741#endif
742#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000743 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400744#endif
745#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000746 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400747#endif
748
749#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000750 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400751#endif
752
753 // Clear the current sync states
754 e->p.synced = e->mt_p.synced = 0;
755
756 // If we have nothing useful to report, skip it
757 if (x == -1 || y == -1) return 1;
758
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500759 // Special case, we'll ignore touches on 0,0 because it usually means
760 // that we received extra data after our last sync and x and y were
761 // reset to 0. We should not be using 0,0 anyway.
762 if (x == 0 && y == 0)
763 return 1;
764
Dees_Troy51a0e822012-09-05 15:24:24 -0400765 // On first touch, see if we're at a virtual key
766 if (downX == -1)
767 {
768 // Attempt mapping to virtual key
769 for (i = 0; i < e->vk_count; ++i)
770 {
771 int xd = ABS(e->vks[i].centerx - x);
772 int yd = ABS(e->vks[i].centery - y);
773
774 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
775 {
776 ev->type = EV_KEY;
777 ev->code = e->vks[i].scancode;
778 ev->value = 1;
779
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100780 last_virt_key = e->vks[i].scancode;
781
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400782#ifndef TW_NO_HAPTICS
Dees_Troy51a0e822012-09-05 15:24:24 -0400783 vibrate(VIBRATOR_TIME_MS);
bigbiff bigbiff3ed778a2019-03-12 19:28:31 -0400784#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400785
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500786 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400787 // and make sure we don't come back into this area
788 discard = 1;
789 downX = 0;
790 return 0;
791 }
792 }
793 }
794
795 // If we were originally a button press, discard this event
796 if (discard)
797 {
798 return 1;
799 }
800
801 // Record where we started the touch for deciding if this is a key or a scroll
802 downX = x;
803 downY = y;
804
805 ev->type = EV_ABS;
806 ev->code = 1;
807 ev->value = (x << 16) | y;
808 return 0;
809}
810
thatde72b6d2015-02-08 08:55:00 +0100811int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400812{
813 int r;
814 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100815 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400816
Ethan Yonkere13fa632015-01-27 11:30:03 -0600817 gettimeofday(&curr, NULL);
818 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
819 {
820 struct stat st;
821 stat("/dev/input", &st);
822 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100823 {
Matt Mower9b4d8dd2017-02-13 16:10:38 -0600824 LOGI("Reloading input devices\n");
Ethan Yonkere13fa632015-01-27 11:30:03 -0600825 ev_exit();
826 ev_init();
827 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100828 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600829 lastInputStat = curr;
830 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100831
thatde72b6d2015-02-08 08:55:00 +0100832 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400833
Ethan Yonkere13fa632015-01-27 11:30:03 -0600834 if(r > 0) {
835 for(n = 0; n < ev_count; n++) {
836 if(ev_fds[n].revents & POLLIN) {
837 r = read(ev_fds[n].fd, ev, sizeof(*ev));
838 if(r == sizeof(*ev)) {
839 if (!vk_modify(&evs[n], ev))
840 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400841 }
842 }
843 }
thatc5837f32015-02-01 01:59:43 +0100844 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600845 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400846
thatc5837f32015-02-01 01:59:43 +0100847 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400848}
849
Ethan Yonker58f21322018-08-24 11:17:36 -0500850int ev_wait(int timeout __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400851{
852 return -1;
853}
854
855void ev_dispatch(void)
856{
857 return;
858}
859
Ethan Yonker58f21322018-08-24 11:17:36 -0500860int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused)
Dees_Troy51a0e822012-09-05 15:24:24 -0400861{
862 return -1;
863}