Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* |
| 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | #include <linux/input.h> |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
Ethan Yonker | c798c9c | 2015-10-09 11:15:26 -0500 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
notsyncing | c7c78d2 | 2018-06-06 20:26:47 +0800 | [diff] [blame] | 29 | #include <fstream> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 30 | |
Mohd Faraz | bc576cd | 2020-05-30 21:06:32 +0530 | [diff] [blame] | 31 | #ifdef USE_QTI_HAPTICS |
| 32 | #include <android/hardware/vibrator/1.2/IVibrator.h> |
| 33 | #endif |
| 34 | |
nebrassy | 76224bd | 2021-04-05 11:52:44 +0200 | [diff] [blame] | 35 | #ifdef USE_QTI_AIDL_HAPTICS |
| 36 | #include <aidl/android/hardware/vibrator/IVibrator.h> |
| 37 | #include <android/binder_manager.h> |
| 38 | using ::aidl::android::hardware::vibrator::IVibrator; |
| 39 | static const std::string kVibratorInstance = std::string(IVibrator::descriptor) + "/default"; |
| 40 | #endif |
| 41 | |
bigbiff | 1f9e484 | 2020-10-31 11:33:15 -0400 | [diff] [blame] | 42 | #include "common.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame] | 44 | #include "minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | |
| 46 | //#define _EVENT_LOGGING |
| 47 | |
Dees_Troy | 3f23d9e | 2013-05-03 21:04:05 +0000 | [diff] [blame] | 48 | #define MAX_DEVICES 32 |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | |
| 50 | #define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable" |
| 51 | #define VIBRATOR_TIME_MS 50 |
| 52 | |
notsyncing | c7c78d2 | 2018-06-06 20:26:47 +0800 | [diff] [blame] | 53 | #define LEDS_HAPTICS_DURATION_FILE "/sys/class/leds/vibrator/duration" |
| 54 | #define LEDS_HAPTICS_ACTIVATE_FILE "/sys/class/leds/vibrator/activate" |
| 55 | |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 56 | #ifndef SYN_REPORT |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | #define SYN_REPORT 0x00 |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 58 | #endif |
| 59 | #ifndef SYN_CONFIG |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 60 | #define SYN_CONFIG 0x01 |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 61 | #endif |
| 62 | #ifndef SYN_MT_REPORT |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | #define SYN_MT_REPORT 0x02 |
Dees_Troy | f759675 | 2012-09-28 13:21:36 -0400 | [diff] [blame] | 64 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | |
| 66 | #define ABS_MT_POSITION 0x2a /* Group a set of X and Y */ |
| 67 | #define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */ |
| 68 | #define ABS_MT_SLOT 0x2f |
| 69 | #define ABS_MT_TOUCH_MAJOR 0x30 |
| 70 | #define ABS_MT_TOUCH_MINOR 0x31 |
| 71 | #define ABS_MT_WIDTH_MAJOR 0x32 |
| 72 | #define ABS_MT_WIDTH_MINOR 0x33 |
| 73 | #define ABS_MT_ORIENTATION 0x34 |
| 74 | #define ABS_MT_POSITION_X 0x35 |
| 75 | #define ABS_MT_POSITION_Y 0x36 |
| 76 | #define ABS_MT_TOOL_TYPE 0x37 |
| 77 | #define ABS_MT_BLOB_ID 0x38 |
| 78 | #define ABS_MT_TRACKING_ID 0x39 |
| 79 | #define ABS_MT_PRESSURE 0x3a |
| 80 | #define ABS_MT_DISTANCE 0x3b |
| 81 | |
| 82 | enum { |
| 83 | DOWN_NOT, |
| 84 | DOWN_SENT, |
| 85 | DOWN_RELEASED, |
| 86 | }; |
| 87 | |
| 88 | struct virtualkey { |
| 89 | int scancode; |
| 90 | int centerx, centery; |
| 91 | int width, height; |
| 92 | }; |
| 93 | |
| 94 | struct position { |
| 95 | int x, y; |
| 96 | int synced; |
| 97 | struct input_absinfo xi, yi; |
| 98 | }; |
| 99 | |
| 100 | struct ev { |
| 101 | struct pollfd *fd; |
| 102 | |
| 103 | struct virtualkey *vks; |
| 104 | int vk_count; |
| 105 | |
| 106 | char deviceName[64]; |
| 107 | |
| 108 | int ignored; |
| 109 | |
| 110 | struct position p, mt_p; |
| 111 | int down; |
| 112 | }; |
| 113 | |
| 114 | static struct pollfd ev_fds[MAX_DEVICES]; |
| 115 | static struct ev evs[MAX_DEVICES]; |
| 116 | static unsigned ev_count = 0; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 117 | static struct timeval lastInputStat; |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 118 | static time_t lastInputMTime; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 119 | static int has_mouse = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 120 | |
| 121 | static inline int ABS(int x) { |
| 122 | return x<0?-x:x; |
| 123 | } |
| 124 | |
notsyncing | c7c78d2 | 2018-06-06 20:26:47 +0800 | [diff] [blame] | 125 | int write_to_file(const std::string& fn, const std::string& line) { |
| 126 | FILE *file; |
| 127 | file = fopen(fn.c_str(), "w"); |
| 128 | if (file != NULL) { |
| 129 | fwrite(line.c_str(), line.size(), 1, file); |
| 130 | fclose(file); |
| 131 | return 0; |
| 132 | } |
| 133 | LOGI("Cannot find file %s\n", fn.c_str()); |
| 134 | return -1; |
| 135 | } |
| 136 | |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 137 | #ifndef TW_NO_HAPTICS |
LameMonster82 | 4ef96a6 | 2020-06-04 13:31:12 +0200 | [diff] [blame] | 138 | #ifndef TW_HAPTICS_TSPDRV |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 139 | int vibrate(int timeout_ms) |
| 140 | { |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 141 | if (timeout_ms > 10000) timeout_ms = 1000; |
Dees Troy | 9a4d740 | 2019-03-21 14:17:28 -0400 | [diff] [blame] | 142 | char tout[6]; |
| 143 | sprintf(tout, "%i", timeout_ms); |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 144 | |
Mohd Faraz | bc576cd | 2020-05-30 21:06:32 +0530 | [diff] [blame] | 145 | #ifdef USE_QTI_HAPTICS |
| 146 | android::sp<android::hardware::vibrator::V1_2::IVibrator> vib = android::hardware::vibrator::V1_2::IVibrator::getService(); |
| 147 | if (vib != nullptr) { |
| 148 | vib->on((uint32_t)timeout_ms); |
| 149 | } |
nebrassy | 76224bd | 2021-04-05 11:52:44 +0200 | [diff] [blame] | 150 | #elif defined(USE_QTI_AIDL_HAPTICS) |
| 151 | std::shared_ptr<IVibrator> vib = IVibrator::fromBinder(ndk::SpAIBinder(AServiceManager_getService(kVibratorInstance.c_str()))); |
| 152 | if (vib != nullptr) { |
| 153 | vib->on((uint32_t)timeout_ms, nullptr); |
| 154 | } |
soulr344 | f831bff | 2021-08-16 09:46:38 +0500 | [diff] [blame] | 155 | #elif defined(USE_SAMSUNG_HAPTICS) |
| 156 | /* Newer Samsung devices have duration file only |
| 157 | 0 in VIBRATOR_TIMEOUT_FILE means no vibration |
| 158 | Anything else is the vibration running for X milliseconds */ |
| 159 | if (std::ifstream(VIBRATOR_TIMEOUT_FILE).good()) { |
| 160 | write_to_file(VIBRATOR_TIMEOUT_FILE, tout); |
| 161 | } |
Mohd Faraz | bc576cd | 2020-05-30 21:06:32 +0530 | [diff] [blame] | 162 | #else |
notsyncing | c7c78d2 | 2018-06-06 20:26:47 +0800 | [diff] [blame] | 163 | if (std::ifstream(LEDS_HAPTICS_ACTIVATE_FILE).good()) { |
Dees Troy | 9a4d740 | 2019-03-21 14:17:28 -0400 | [diff] [blame] | 164 | write_to_file(LEDS_HAPTICS_DURATION_FILE, tout); |
notsyncing | c7c78d2 | 2018-06-06 20:26:47 +0800 | [diff] [blame] | 165 | write_to_file(LEDS_HAPTICS_ACTIVATE_FILE, "1"); |
| 166 | } else |
Dees Troy | 9a4d740 | 2019-03-21 14:17:28 -0400 | [diff] [blame] | 167 | write_to_file(VIBRATOR_TIMEOUT_FILE, tout); |
Mohd Faraz | bc576cd | 2020-05-30 21:06:32 +0530 | [diff] [blame] | 168 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 169 | return 0; |
| 170 | } |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 171 | #endif |
LameMonster82 | 4ef96a6 | 2020-06-04 13:31:12 +0200 | [diff] [blame] | 172 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | |
| 174 | /* Returns empty tokens */ |
| 175 | static char *vk_strtok_r(char *str, const char *delim, char **save_str) |
| 176 | { |
| 177 | if(!str) |
| 178 | { |
| 179 | if(!*save_str) |
| 180 | return NULL; |
| 181 | |
| 182 | str = (*save_str) + 1; |
| 183 | } |
| 184 | *save_str = strpbrk(str, delim); |
| 185 | |
| 186 | if (*save_str) |
| 187 | **save_str = '\0'; |
| 188 | |
| 189 | return str; |
| 190 | } |
| 191 | |
| 192 | static int vk_init(struct ev *e) |
| 193 | { |
| 194 | char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys."; |
| 195 | char vks[2048], *ts = NULL; |
| 196 | ssize_t len; |
| 197 | int vk_fd; |
| 198 | int i; |
| 199 | |
| 200 | e->vk_count = 0; |
| 201 | |
| 202 | len = strlen(vk_path); |
| 203 | len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName); |
| 204 | if (len <= 0) |
| 205 | { |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 206 | LOGE("Unable to query event object.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 207 | return -1; |
| 208 | } |
| 209 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 210 | printf("Event object: %s\n", e->deviceName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 211 | #endif |
| 212 | |
Flemmard | f467461 | 2014-01-10 09:14:44 +0100 | [diff] [blame] | 213 | #ifdef WHITELIST_INPUT |
| 214 | if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0) |
| 215 | { |
| 216 | e->ignored = 1; |
| 217 | } |
| 218 | #else |
Ethan Yonker | 5742a40 | 2014-08-12 14:52:49 -0500 | [diff] [blame] | 219 | #ifndef TW_INPUT_BLACKLIST |
Ethan Yonker | 64dbd0d | 2016-08-10 12:30:10 -0500 | [diff] [blame] | 220 | // Blacklist these "input" devices, use TW_INPUT_BLACKLIST := "accelerometer\x0atest1\x0atest2" using the \x0a as a separator between input devices |
Ethan Yonker | 3c4ac3b | 2014-08-14 11:15:32 -0500 | [diff] [blame] | 221 | if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 222 | { |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 223 | LOGI("Blacklisting input device: %s\n", e->deviceName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 224 | e->ignored = 1; |
| 225 | } |
Ethan Yonker | 5742a40 | 2014-08-12 14:52:49 -0500 | [diff] [blame] | 226 | #else |
| 227 | char* bl = strdup(EXPAND(TW_INPUT_BLACKLIST)); |
| 228 | char* blacklist = strtok(bl, "\n"); |
| 229 | |
| 230 | while (blacklist != NULL) { |
| 231 | if (strcmp(e->deviceName, blacklist) == 0) { |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 232 | LOGI("Blacklisting input device: %s\n", blacklist); |
Ethan Yonker | 5742a40 | 2014-08-12 14:52:49 -0500 | [diff] [blame] | 233 | e->ignored = 1; |
| 234 | } |
| 235 | blacklist = strtok(NULL, "\n"); |
| 236 | } |
| 237 | free(bl); |
| 238 | #endif |
Flemmard | f467461 | 2014-01-10 09:14:44 +0100 | [diff] [blame] | 239 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 240 | |
| 241 | strcat(vk_path, e->deviceName); |
| 242 | |
| 243 | // Some devices split the keys from the touchscreen |
| 244 | e->vk_count = 0; |
| 245 | vk_fd = open(vk_path, O_RDONLY); |
| 246 | if (vk_fd >= 0) |
| 247 | { |
| 248 | len = read(vk_fd, vks, sizeof(vks)-1); |
| 249 | close(vk_fd); |
| 250 | if (len <= 0) |
| 251 | return -1; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 252 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 253 | vks[len] = '\0'; |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 254 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 255 | /* Parse a line like: |
| 256 | keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:... |
| 257 | */ |
| 258 | for (ts = vks, e->vk_count = 1; *ts; ++ts) { |
| 259 | if (*ts == ':') |
| 260 | ++e->vk_count; |
| 261 | } |
| 262 | |
| 263 | if (e->vk_count % 6) { |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 264 | LOGI("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 265 | } |
| 266 | e->vk_count /= 6; |
| 267 | if (e->vk_count <= 0) |
| 268 | return -1; |
| 269 | |
| 270 | e->down = DOWN_NOT; |
| 271 | } |
| 272 | |
| 273 | ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi); |
| 274 | ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi); |
| 275 | e->p.synced = 0; |
| 276 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 277 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 278 | #endif |
| 279 | |
| 280 | ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi); |
| 281 | ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi); |
| 282 | e->mt_p.synced = 0; |
| 283 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 284 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 285 | #endif |
| 286 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 287 | e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 288 | |
| 289 | for (i = 0; i < e->vk_count; ++i) { |
| 290 | char *token[6]; |
| 291 | int j; |
| 292 | |
| 293 | for (j = 0; j < 6; ++j) { |
| 294 | token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts); |
| 295 | } |
| 296 | |
| 297 | if (strcmp(token[0], "0x01") != 0) { |
| 298 | /* Java does string compare, so we do too. */ |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 299 | LOGI("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 300 | continue; |
| 301 | } |
| 302 | |
| 303 | e->vks[i].scancode = strtol(token[1], NULL, 0); |
| 304 | e->vks[i].centerx = strtol(token[2], NULL, 0); |
| 305 | e->vks[i].centery = strtol(token[3], NULL, 0); |
| 306 | e->vks[i].width = strtol(token[4], NULL, 0); |
| 307 | e->vks[i].height = strtol(token[5], NULL, 0); |
| 308 | } |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 313 | #define BITS_PER_LONG (sizeof(long) * 8) |
| 314 | #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1) |
| 315 | #define OFF(x) ((x)%BITS_PER_LONG) |
| 316 | #define LONG(x) ((x)/BITS_PER_LONG) |
| 317 | #define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1) |
Vojtech Bocek | 971d318 | 2014-02-20 21:43:28 +0100 | [diff] [blame] | 318 | |
| 319 | // Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too, |
| 320 | // check also for EV_KEY (BTN_LEFT and BTN_RIGHT) |
Ethan Yonker | 64dbd0d | 2016-08-10 12:30:10 -0500 | [diff] [blame] | 321 | static void check_mouse(int fd, const char* deviceName) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 322 | { |
| 323 | if(has_mouse) |
| 324 | return; |
| 325 | |
| 326 | unsigned long bit[EV_MAX][NBITS(KEY_MAX)]; |
| 327 | memset(bit, 0, sizeof(bit)); |
| 328 | ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]); |
| 329 | |
Vojtech Bocek | 971d318 | 2014-02-20 21:43:28 +0100 | [diff] [blame] | 330 | if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0])) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 331 | return; |
| 332 | |
| 333 | ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]); |
Vojtech Bocek | 971d318 | 2014-02-20 21:43:28 +0100 | [diff] [blame] | 334 | if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL])) |
| 335 | return; |
| 336 | |
| 337 | ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]); |
| 338 | if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY])) |
| 339 | return; |
| 340 | |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 341 | LOGI("Found mouse '%s'\n", deviceName); |
Vojtech Bocek | 971d318 | 2014-02-20 21:43:28 +0100 | [diff] [blame] | 342 | has_mouse = 1; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | int ev_has_mouse(void) |
| 346 | { |
| 347 | return has_mouse; |
| 348 | } |
| 349 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 350 | int ev_init(void) |
| 351 | { |
| 352 | DIR *dir; |
| 353 | struct dirent *de; |
| 354 | int fd; |
| 355 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 356 | has_mouse = 0; |
| 357 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | dir = opendir("/dev/input"); |
| 359 | if(dir != 0) { |
| 360 | while((de = readdir(dir))) { |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 361 | #ifdef _EVENT_LOGGING |
| 362 | fprintf(stderr,"/dev/input/%s\n", de->d_name); |
| 363 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 364 | if(strncmp(de->d_name,"event",5)) continue; |
| 365 | fd = openat(dirfd(dir), de->d_name, O_RDONLY); |
| 366 | if(fd < 0) continue; |
| 367 | |
| 368 | ev_fds[ev_count].fd = fd; |
| 369 | ev_fds[ev_count].events = POLLIN; |
| 370 | evs[ev_count].fd = &ev_fds[ev_count]; |
| 371 | |
| 372 | /* Load virtualkeys if there are any */ |
Ethan Yonker | 64dbd0d | 2016-08-10 12:30:10 -0500 | [diff] [blame] | 373 | vk_init(&evs[ev_count]); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 374 | |
Ethan Yonker | 64dbd0d | 2016-08-10 12:30:10 -0500 | [diff] [blame] | 375 | if (!evs[ev_count].ignored) |
| 376 | check_mouse(fd, evs[ev_count].deviceName); |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 377 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 378 | ev_count++; |
| 379 | if(ev_count == MAX_DEVICES) break; |
| 380 | } |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 381 | closedir(dir); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 382 | } |
| 383 | |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 384 | struct stat st; |
| 385 | if(stat("/dev/input", &st) >= 0) |
| 386 | lastInputMTime = st.st_mtime; |
| 387 | gettimeofday(&lastInputStat, NULL); |
| 388 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | void ev_exit(void) |
| 393 | { |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 394 | while (ev_count-- > 0) { |
| 395 | if (evs[ev_count].vk_count) { |
| 396 | free(evs[ev_count].vks); |
| 397 | evs[ev_count].vk_count = 0; |
| 398 | } |
| 399 | close(ev_fds[ev_count].fd); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 400 | } |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 401 | ev_count = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 402 | } |
| 403 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 404 | /*static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 405 | { |
| 406 | int screen_pos; |
| 407 | |
| 408 | if (info->minimum == info->maximum) |
| 409 | return 0; |
| 410 | |
| 411 | screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum); |
| 412 | return (screen_pos >= 0 && screen_pos < screen_size); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 413 | }*/ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 414 | |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 415 | static int vk_tp_to_screen(struct position *p, int *x, int *y) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 416 | { |
| 417 | if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum) |
| 418 | { |
| 419 | // In this case, we assume the screen dimensions are the same. |
| 420 | *x = p->x; |
| 421 | *y = p->y; |
| 422 | return 0; |
| 423 | } |
| 424 | |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 425 | #ifdef _EVENT_LOGGING |
| 426 | printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width()); |
| 427 | #endif |
| 428 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 429 | #ifndef RECOVERY_TOUCHSCREEN_SWAP_XY |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 430 | int fb_width = gr_fb_width(); |
| 431 | int fb_height = gr_fb_height(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 432 | #else |
| 433 | // We need to swap the scaling sizes, too |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 434 | int fb_width = gr_fb_height(); |
| 435 | int fb_height = gr_fb_width(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 436 | #endif |
| 437 | |
| 438 | *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum); |
| 439 | *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum); |
| 440 | |
| 441 | if (*x >= 0 && *x < fb_width && |
| 442 | *y >= 0 && *y < fb_height) |
| 443 | { |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | return 1; |
| 448 | } |
| 449 | |
| 450 | /* Translate a virtual key in to a real key event, if needed */ |
| 451 | /* Returns non-zero when the event should be consumed */ |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 452 | static int vk_modify(struct ev *e, struct input_event *ev) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 453 | { |
| 454 | static int downX = -1, downY = -1; |
| 455 | static int discard = 0; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 456 | static int last_virt_key = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 457 | static int lastWasSynReport = 0; |
| 458 | static int touchReleaseOnNextSynReport = 0; |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 459 | 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_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 460 | int i; |
| 461 | int x, y; |
| 462 | |
| 463 | // This is used to ditch useless event handlers, like an accelerometer |
| 464 | if (e->ignored) return 1; |
| 465 | |
| 466 | if (ev->type == EV_REL && ev->code == REL_Z) |
| 467 | { |
| 468 | // This appears to be an accelerometer or another strange input device. It's not the touchscreen. |
| 469 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 470 | printf("EV: Device disabled due to non-touchscreen messages.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 471 | #endif |
| 472 | e->ignored = 1; |
| 473 | return 1; |
| 474 | } |
| 475 | |
| 476 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 477 | printf("EV: %s => type: %x code: %x value: %d\n", e->deviceName, ev->type, ev->code, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 478 | #endif |
| 479 | |
| 480 | // Handle keyboard events, value of 1 indicates key down, 0 indicates key up |
| 481 | if (ev->type == EV_KEY) { |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | if (ev->type == EV_ABS) { |
| 486 | switch (ev->code) { |
| 487 | |
| 488 | case ABS_X: //00 |
| 489 | e->p.synced |= 0x01; |
| 490 | e->p.x = ev->value; |
| 491 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 492 | printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 493 | #endif |
| 494 | break; |
| 495 | |
| 496 | case ABS_Y: //01 |
| 497 | e->p.synced |= 0x02; |
| 498 | e->p.y = ev->value; |
| 499 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 500 | printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 501 | #endif |
| 502 | break; |
| 503 | |
| 504 | case ABS_MT_POSITION: //2a |
| 505 | e->mt_p.synced = 0x03; |
| 506 | if (ev->value == (1 << 31)) |
| 507 | { |
Ethan Yonker | 32f68a3 | 2014-12-29 08:13:23 -0600 | [diff] [blame] | 508 | #ifndef TW_IGNORE_MT_POSITION_0 |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 509 | e->mt_p.x = 0; |
| 510 | e->mt_p.y = 0; |
| 511 | lastWasSynReport = 1; |
Ethan Yonker | 32f68a3 | 2014-12-29 08:13:23 -0600 | [diff] [blame] | 512 | #endif |
| 513 | #ifdef _EVENT_LOGGING |
| 514 | #ifndef TW_IGNORE_MT_POSITION_0 |
| 515 | printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value); |
| 516 | #else |
| 517 | printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value); |
| 518 | #endif |
| 519 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 520 | } |
| 521 | else |
| 522 | { |
| 523 | lastWasSynReport = 0; |
| 524 | e->mt_p.x = (ev->value & 0x7FFF0000) >> 16; |
| 525 | e->mt_p.y = (ev->value & 0xFFFF); |
Ethan Yonker | 32f68a3 | 2014-12-29 08:13:23 -0600 | [diff] [blame] | 526 | #ifdef _EVENT_LOGGING |
| 527 | 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)); |
| 528 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 529 | } |
| 530 | break; |
| 531 | |
| 532 | case ABS_MT_TOUCH_MAJOR: //30 |
| 533 | if (ev->value == 0) |
| 534 | { |
Ibrahim Awwal | 2e9cb01 | 2014-01-04 12:38:26 -0800 | [diff] [blame] | 535 | #ifndef TW_IGNORE_MAJOR_AXIS_0 |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 536 | // We're in a touch release, although some devices will still send positions as well |
| 537 | e->mt_p.x = 0; |
| 538 | e->mt_p.y = 0; |
| 539 | touchReleaseOnNextSynReport = 1; |
Ibrahim Awwal | 2e9cb01 | 2014-01-04 12:38:26 -0800 | [diff] [blame] | 540 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 541 | } |
| 542 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 543 | printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 544 | #endif |
| 545 | break; |
| 546 | |
| 547 | case ABS_MT_PRESSURE: //3a |
| 548 | if (ev->value == 0) |
| 549 | { |
| 550 | // We're in a touch release, although some devices will still send positions as well |
| 551 | e->mt_p.x = 0; |
| 552 | e->mt_p.y = 0; |
| 553 | touchReleaseOnNextSynReport = 1; |
| 554 | } |
| 555 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 556 | printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 557 | #endif |
| 558 | break; |
| 559 | |
| 560 | case ABS_MT_POSITION_X: //35 |
| 561 | e->mt_p.synced |= 0x01; |
| 562 | e->mt_p.x = ev->value; |
| 563 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 564 | printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 565 | #endif |
| 566 | break; |
| 567 | |
| 568 | case ABS_MT_POSITION_Y: //36 |
| 569 | e->mt_p.synced |= 0x02; |
| 570 | e->mt_p.y = ev->value; |
| 571 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 572 | printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 573 | #endif |
| 574 | break; |
| 575 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 576 | case ABS_MT_TOUCH_MINOR: //31 |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 577 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 578 | printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value); |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 579 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 580 | break; |
| 581 | |
| 582 | case ABS_MT_WIDTH_MAJOR: //32 |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 583 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 584 | printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value); |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 585 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 586 | break; |
| 587 | |
| 588 | case ABS_MT_WIDTH_MINOR: //33 |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 589 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 590 | printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value); |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 591 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 592 | break; |
| 593 | |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 594 | case ABS_MT_TRACKING_ID: //39 |
Ethan Yonker | d6821a1 | 2015-08-26 15:29:23 -0500 | [diff] [blame] | 595 | #ifdef TW_IGNORE_ABS_MT_TRACKING_ID |
| 596 | #ifdef _EVENT_LOGGING |
| 597 | printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value); |
| 598 | #endif |
| 599 | return 1; |
| 600 | #endif |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 601 | if (ev->value < 0) { |
| 602 | e->mt_p.x = 0; |
| 603 | e->mt_p.y = 0; |
| 604 | touchReleaseOnNextSynReport = 2; |
| 605 | use_tracking_id_negative_as_touch_release = 1; |
| 606 | #ifdef _EVENT_LOGGING |
| 607 | if (use_tracking_id_negative_as_touch_release) |
| 608 | printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n"); |
| 609 | #endif |
| 610 | } |
| 611 | #ifdef _EVENT_LOGGING |
| 612 | printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value); |
| 613 | #endif |
| 614 | break; |
| 615 | |
| 616 | #ifdef _EVENT_LOGGING |
| 617 | // These are for touch logging purposes only |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 618 | case ABS_MT_ORIENTATION: //34 |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 619 | printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 620 | return 1; |
| 621 | break; |
| 622 | |
| 623 | case ABS_MT_TOOL_TYPE: //37 |
| 624 | LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value); |
| 625 | return 1; |
| 626 | break; |
| 627 | |
| 628 | case ABS_MT_BLOB_ID: //38 |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 629 | printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 630 | return 1; |
| 631 | break; |
| 632 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 633 | case ABS_MT_DISTANCE: //3b |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 634 | printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 635 | return 1; |
| 636 | break; |
Ethan Yonker | d6821a1 | 2015-08-26 15:29:23 -0500 | [diff] [blame] | 637 | case ABS_MT_SLOT: |
| 638 | printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value); |
| 639 | return 1; |
| 640 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 641 | #endif |
| 642 | |
| 643 | default: |
| 644 | // This is an unhandled message, just skip it |
| 645 | return 1; |
| 646 | } |
| 647 | |
| 648 | if (ev->code != ABS_MT_POSITION) |
| 649 | { |
| 650 | lastWasSynReport = 0; |
| 651 | return 1; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | // Check if we should ignore the message |
| 656 | if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT))) |
| 657 | { |
| 658 | lastWasSynReport = 0; |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | #ifdef _EVENT_LOGGING |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 663 | if (ev->type == EV_SYN && ev->code == SYN_REPORT) |
| 664 | printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName); |
| 665 | if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT) |
| 666 | printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 667 | #endif |
| 668 | |
| 669 | // Discard the MT versions |
| 670 | if (ev->code == SYN_MT_REPORT) return 0; |
| 671 | |
Dees_Troy | d86400b | 2013-05-13 19:04:35 +0000 | [diff] [blame] | 672 | if (((lastWasSynReport == 1 || touchReleaseOnNextSynReport == 1) && !use_tracking_id_negative_as_touch_release) || (use_tracking_id_negative_as_touch_release && touchReleaseOnNextSynReport == 2)) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 673 | { |
| 674 | // Reset the value |
| 675 | touchReleaseOnNextSynReport = 0; |
| 676 | |
| 677 | // We are a finger-up state |
| 678 | if (!discard) |
| 679 | { |
| 680 | // Report the key up |
| 681 | ev->type = EV_ABS; |
| 682 | ev->code = 0; |
| 683 | ev->value = (downX << 16) | downY; |
| 684 | } |
| 685 | downX = -1; |
| 686 | downY = -1; |
| 687 | if (discard) |
| 688 | { |
| 689 | discard = 0; |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 690 | |
| 691 | // Send the keyUp event |
| 692 | ev->type = EV_KEY; |
| 693 | ev->code = last_virt_key; |
| 694 | ev->value = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 695 | } |
| 696 | return 0; |
| 697 | } |
| 698 | lastWasSynReport = 1; |
| 699 | |
| 700 | // Retrieve where the x,y position is |
| 701 | if (e->p.synced & 0x03) |
| 702 | { |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 703 | vk_tp_to_screen(&e->p, &x, &y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 704 | } |
| 705 | else if (e->mt_p.synced & 0x03) |
| 706 | { |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 707 | vk_tp_to_screen(&e->mt_p, &x, &y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 708 | } |
| 709 | else |
| 710 | { |
| 711 | // We don't have useful information to convey |
| 712 | return 1; |
| 713 | } |
| 714 | |
| 715 | #ifdef RECOVERY_TOUCHSCREEN_SWAP_XY |
| 716 | x ^= y; |
| 717 | y ^= x; |
| 718 | x ^= y; |
| 719 | #endif |
| 720 | #ifdef RECOVERY_TOUCHSCREEN_FLIP_X |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 721 | x = gr_fb_width() - x; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 722 | #endif |
| 723 | #ifdef RECOVERY_TOUCHSCREEN_FLIP_Y |
Dees_Troy | b7ecc09 | 2013-08-24 12:15:41 +0000 | [diff] [blame] | 724 | y = gr_fb_height() - y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 725 | #endif |
| 726 | |
| 727 | #ifdef _EVENT_LOGGING |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 728 | printf("EV: x: %d y: %d\n", x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 729 | #endif |
| 730 | |
| 731 | // Clear the current sync states |
| 732 | e->p.synced = e->mt_p.synced = 0; |
| 733 | |
| 734 | // If we have nothing useful to report, skip it |
| 735 | if (x == -1 || y == -1) return 1; |
| 736 | |
Ethan Yonker | 96bc6dd | 2015-03-18 11:44:34 -0500 | [diff] [blame] | 737 | // Special case, we'll ignore touches on 0,0 because it usually means |
| 738 | // that we received extra data after our last sync and x and y were |
| 739 | // reset to 0. We should not be using 0,0 anyway. |
| 740 | if (x == 0 && y == 0) |
| 741 | return 1; |
| 742 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 743 | // On first touch, see if we're at a virtual key |
| 744 | if (downX == -1) |
| 745 | { |
| 746 | // Attempt mapping to virtual key |
| 747 | for (i = 0; i < e->vk_count; ++i) |
| 748 | { |
| 749 | int xd = ABS(e->vks[i].centerx - x); |
| 750 | int yd = ABS(e->vks[i].centery - y); |
| 751 | |
| 752 | if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2) |
| 753 | { |
| 754 | ev->type = EV_KEY; |
| 755 | ev->code = e->vks[i].scancode; |
| 756 | ev->value = 1; |
| 757 | |
Vojtech Bocek | 0b7fe50 | 2014-03-13 17:36:52 +0100 | [diff] [blame] | 758 | last_virt_key = e->vks[i].scancode; |
| 759 | |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 760 | #ifndef TW_NO_HAPTICS |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 761 | vibrate(VIBRATOR_TIME_MS); |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 762 | #endif |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 763 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 764 | // Mark that all further movement until lift is discard, |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 765 | // and make sure we don't come back into this area |
| 766 | discard = 1; |
| 767 | downX = 0; |
| 768 | return 0; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | // If we were originally a button press, discard this event |
| 774 | if (discard) |
| 775 | { |
| 776 | return 1; |
| 777 | } |
| 778 | |
| 779 | // Record where we started the touch for deciding if this is a key or a scroll |
| 780 | downX = x; |
| 781 | downY = y; |
| 782 | |
| 783 | ev->type = EV_ABS; |
| 784 | ev->code = 1; |
| 785 | ev->value = (x << 16) | y; |
| 786 | return 0; |
| 787 | } |
| 788 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 789 | int ev_get(struct input_event *ev, int timeout_ms) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 790 | { |
| 791 | int r; |
| 792 | unsigned n; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 793 | struct timeval curr; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 794 | |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 795 | gettimeofday(&curr, NULL); |
| 796 | if(curr.tv_sec - lastInputStat.tv_sec >= 2) |
| 797 | { |
| 798 | struct stat st; |
| 799 | stat("/dev/input", &st); |
| 800 | if (st.st_mtime > lastInputMTime) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 801 | { |
Matt Mower | 9b4d8dd | 2017-02-13 16:10:38 -0600 | [diff] [blame] | 802 | LOGI("Reloading input devices\n"); |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 803 | ev_exit(); |
| 804 | ev_init(); |
| 805 | lastInputMTime = st.st_mtime; |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 806 | } |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 807 | lastInputStat = curr; |
| 808 | } |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 809 | |
that | de72b6d | 2015-02-08 08:55:00 +0100 | [diff] [blame] | 810 | r = poll(ev_fds, ev_count, timeout_ms); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 811 | |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 812 | if(r > 0) { |
| 813 | for(n = 0; n < ev_count; n++) { |
| 814 | if(ev_fds[n].revents & POLLIN) { |
| 815 | r = read(ev_fds[n].fd, ev, sizeof(*ev)); |
| 816 | if(r == sizeof(*ev)) { |
| 817 | if (!vk_modify(&evs[n], ev)) |
| 818 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | } |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 822 | return -1; |
Ethan Yonker | e13fa63 | 2015-01-27 11:30:03 -0600 | [diff] [blame] | 823 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 824 | |
that | c5837f3 | 2015-02-01 01:59:43 +0100 | [diff] [blame] | 825 | return -2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 826 | } |
| 827 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 828 | int ev_wait(int timeout __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 829 | { |
| 830 | return -1; |
| 831 | } |
| 832 | |
| 833 | void ev_dispatch(void) |
| 834 | { |
| 835 | return; |
| 836 | } |
| 837 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 838 | int ev_get_input(int fd __unused, short revents __unused, struct input_event *ev __unused) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 839 | { |
| 840 | return -1; |
| 841 | } |