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