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