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