blob: 54119ebc0211a639ccfb6129e12b583e308af448 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <dirent.h>
21#include <sys/poll.h>
22#include <limits.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040023#include <linux/input.h>
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010024#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
Ethan Yonkerc798c9c2015-10-09 11:15:26 -050027#include <stdio.h>
28#include <string.h>
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010029
Dees_Troy51a0e822012-09-05 15:24:24 -040030
31#include "../common.h"
32
33#include "minui.h"
34
35//#define _EVENT_LOGGING
36
Dees_Troy3f23d9e2013-05-03 21:04:05 +000037#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040038
39#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
40#define VIBRATOR_TIME_MS 50
41
Dees_Troyf7596752012-09-28 13:21:36 -040042#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040043#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040044#endif
45#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040046#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040047#endif
48#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040049#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040050#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040051
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
68enum {
69 DOWN_NOT,
70 DOWN_SENT,
71 DOWN_RELEASED,
72};
73
74struct virtualkey {
75 int scancode;
76 int centerx, centery;
77 int width, height;
78};
79
80struct position {
81 int x, y;
82 int synced;
83 struct input_absinfo xi, yi;
84};
85
86struct 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
100static struct pollfd ev_fds[MAX_DEVICES];
101static struct ev evs[MAX_DEVICES];
102static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100103static struct timeval lastInputStat;
104static unsigned long lastInputMTime;
105static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106
107static inline int ABS(int x) {
108 return x<0?-x:x;
109}
110
111int vibrate(int timeout_ms)
112{
113 char str[20];
114 int fd;
115 int ret;
116
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000117 if (timeout_ms > 10000) timeout_ms = 1000;
118
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 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 */
134static 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
151static 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 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000165 printf("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400166 return -1;
167 }
168#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000169 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400170#endif
171
Flemmardf4674612014-01-10 09:14:44 +0100172#ifdef WHITELIST_INPUT
173 if (strcmp(e->deviceName, EXPAND(WHITELIST_INPUT)) != 0)
174 {
175 e->ignored = 1;
176 }
177#else
Ethan Yonker5742a402014-08-12 14:52:49 -0500178#ifndef TW_INPUT_BLACKLIST
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500179 // Blacklist these "input" devices, use TW_INPUT_BLACKLIST := "accelerometer\x0atest1\x0atest2" using the \x0a as a separator between input devices
Ethan Yonker3c4ac3b2014-08-14 11:15:32 -0500180 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400181 {
Ethan Yonker5742a402014-08-12 14:52:49 -0500182 printf("blacklisting %s input device\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400183 e->ignored = 1;
184 }
Ethan Yonker5742a402014-08-12 14:52:49 -0500185#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) {
191 printf("blacklisting %s input device\n", blacklist);
192 e->ignored = 1;
193 }
194 blacklist = strtok(NULL, "\n");
195 }
196 free(bl);
197#endif
Flemmardf4674612014-01-10 09:14:44 +0100198#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400199
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 Mowerfb1c4ff2014-04-16 13:43:36 -0500211
Dees_Troy51a0e822012-09-05 15:24:24 -0400212 vks[len] = '\0';
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500213
Dees_Troy51a0e822012-09-05 15:24:24 -0400214 /* 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) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000223 printf("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400224 }
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_Troy2673cec2013-04-02 20:22:16 +0000236 printf("EV: ST minX: %d maxX: %d minY: %d maxY: %d\n", e->p.xi.minimum, e->p.xi.maximum, e->p.yi.minimum, e->p.yi.maximum);
Dees_Troy51a0e822012-09-05 15:24:24 -0400237#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_Troy2673cec2013-04-02 20:22:16 +0000243 printf("EV: MT minX: %d maxX: %d minY: %d maxY: %d\n", e->mt_p.xi.minimum, e->mt_p.xi.maximum, e->mt_p.yi.minimum, e->mt_p.yi.maximum);
Dees_Troy51a0e822012-09-05 15:24:24 -0400244#endif
245
Ethan Yonkerfbb43532015-12-28 21:54:50 +0100246 e->vks = (virtualkey *)malloc(sizeof(*e->vks) * e->vk_count);
Dees_Troy51a0e822012-09-05 15:24:24 -0400247
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. */
Dees_Troy2673cec2013-04-02 20:22:16 +0000258 printf("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400259 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 Bocek1fc30fc2014-01-29 18:37:19 +0100272#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 Bocek971d3182014-02-20 21:43:28 +0100277
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 Yonker64dbd0d2016-08-10 12:30:10 -0500280static void check_mouse(int fd, const char* deviceName)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100281{
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 Bocek971d3182014-02-20 21:43:28 +0100289 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100290 return;
291
292 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100293 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
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500300 printf("Found mouse '%s'\n", deviceName);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100301 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100302}
303
304int ev_has_mouse(void)
305{
306 return has_mouse;
307}
308
Dees_Troy51a0e822012-09-05 15:24:24 -0400309int ev_init(void)
310{
311 DIR *dir;
312 struct dirent *de;
313 int fd;
314
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100315 has_mouse = 0;
316
Dees_Troy51a0e822012-09-05 15:24:24 -0400317 dir = opendir("/dev/input");
318 if(dir != 0) {
319 while((de = readdir(dir))) {
320// fprintf(stderr,"/dev/input/%s\n", de->d_name);
321 if(strncmp(de->d_name,"event",5)) continue;
322 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
323 if(fd < 0) continue;
324
325 ev_fds[ev_count].fd = fd;
326 ev_fds[ev_count].events = POLLIN;
327 evs[ev_count].fd = &ev_fds[ev_count];
328
329 /* Load virtualkeys if there are any */
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500330 vk_init(&evs[ev_count]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400331
Ethan Yonker64dbd0d2016-08-10 12:30:10 -0500332 if (!evs[ev_count].ignored)
333 check_mouse(fd, evs[ev_count].deviceName);
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100334
Dees_Troy51a0e822012-09-05 15:24:24 -0400335 ev_count++;
336 if(ev_count == MAX_DEVICES) break;
337 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100338 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400339 }
340
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100341 struct stat st;
342 if(stat("/dev/input", &st) >= 0)
343 lastInputMTime = st.st_mtime;
344 gettimeofday(&lastInputStat, NULL);
345
Dees_Troy51a0e822012-09-05 15:24:24 -0400346 return 0;
347}
348
349void ev_exit(void)
350{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100351 while (ev_count-- > 0) {
352 if (evs[ev_count].vk_count) {
353 free(evs[ev_count].vks);
354 evs[ev_count].vk_count = 0;
355 }
356 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400357 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100358 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400359}
360
361static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
362{
363 int screen_pos;
364
365 if (info->minimum == info->maximum)
366 return 0;
367
368 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
369 return (screen_pos >= 0 && screen_pos < screen_size);
370}
371
Dees_Troyb7ecc092013-08-24 12:15:41 +0000372static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400373{
374 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
375 {
376 // In this case, we assume the screen dimensions are the same.
377 *x = p->x;
378 *y = p->y;
379 return 0;
380 }
381
Dees_Troyb7ecc092013-08-24 12:15:41 +0000382#ifdef _EVENT_LOGGING
383 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
384#endif
385
Dees_Troy51a0e822012-09-05 15:24:24 -0400386#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000387 int fb_width = gr_fb_width();
388 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400389#else
390 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000391 int fb_width = gr_fb_height();
392 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400393#endif
394
395 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
396 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
397
398 if (*x >= 0 && *x < fb_width &&
399 *y >= 0 && *y < fb_height)
400 {
401 return 0;
402 }
403
404 return 1;
405}
406
407/* Translate a virtual key in to a real key event, if needed */
408/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000409static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400410{
411 static int downX = -1, downY = -1;
412 static int discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100413 static int last_virt_key = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 static int lastWasSynReport = 0;
415 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000416 static int use_tracking_id_negative_as_touch_release = 0; // On some devices, type: 3 code: 39 value: -1, aka EV_ABS ABS_MT_TRACKING_ID -1 indicates a true touch release
Dees_Troy51a0e822012-09-05 15:24:24 -0400417 int i;
418 int x, y;
419
420 // This is used to ditch useless event handlers, like an accelerometer
421 if (e->ignored) return 1;
422
423 if (ev->type == EV_REL && ev->code == REL_Z)
424 {
425 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
426#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000427 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400428#endif
429 e->ignored = 1;
430 return 1;
431 }
432
433#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000434 printf("EV: %s => type: %x code: %x value: %d\n", e->deviceName, ev->type, ev->code, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400435#endif
436
437 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
438 if (ev->type == EV_KEY) {
439 return 0;
440 }
441
442 if (ev->type == EV_ABS) {
443 switch (ev->code) {
444
445 case ABS_X: //00
446 e->p.synced |= 0x01;
447 e->p.x = ev->value;
448#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000449 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400450#endif
451 break;
452
453 case ABS_Y: //01
454 e->p.synced |= 0x02;
455 e->p.y = ev->value;
456#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000457 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400458#endif
459 break;
460
461 case ABS_MT_POSITION: //2a
462 e->mt_p.synced = 0x03;
463 if (ev->value == (1 << 31))
464 {
Ethan Yonker32f68a32014-12-29 08:13:23 -0600465#ifndef TW_IGNORE_MT_POSITION_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400466 e->mt_p.x = 0;
467 e->mt_p.y = 0;
468 lastWasSynReport = 1;
Ethan Yonker32f68a32014-12-29 08:13:23 -0600469#endif
470#ifdef _EVENT_LOGGING
471#ifndef TW_IGNORE_MT_POSITION_0
472 printf("EV: %s => EV_ABS ABS_MT_POSITION %d, set x and y to 0 and lastWasSynReport to 1\n", e->deviceName, ev->value);
473#else
474 printf("Ignoring ABS_MT_POSITION 0\n", e->deviceName, ev->value);
475#endif
476#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 }
478 else
479 {
480 lastWasSynReport = 0;
481 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
482 e->mt_p.y = (ev->value & 0xFFFF);
Ethan Yonker32f68a32014-12-29 08:13:23 -0600483#ifdef _EVENT_LOGGING
484 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));
485#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400486 }
487 break;
488
489 case ABS_MT_TOUCH_MAJOR: //30
490 if (ev->value == 0)
491 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800492#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400493 // We're in a touch release, although some devices will still send positions as well
494 e->mt_p.x = 0;
495 e->mt_p.y = 0;
496 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800497#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 }
499#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000500 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400501#endif
502 break;
503
504 case ABS_MT_PRESSURE: //3a
505 if (ev->value == 0)
506 {
507 // We're in a touch release, although some devices will still send positions as well
508 e->mt_p.x = 0;
509 e->mt_p.y = 0;
510 touchReleaseOnNextSynReport = 1;
511 }
512#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000513 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400514#endif
515 break;
516
517 case ABS_MT_POSITION_X: //35
518 e->mt_p.synced |= 0x01;
519 e->mt_p.x = ev->value;
520#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000521 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400522#endif
523 break;
524
525 case ABS_MT_POSITION_Y: //36
526 e->mt_p.synced |= 0x02;
527 e->mt_p.y = ev->value;
528#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000529 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400530#endif
531 break;
532
Dees_Troy51a0e822012-09-05 15:24:24 -0400533 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000534#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000535 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000536#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400537 break;
538
539 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000540#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000541 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000542#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400543 break;
544
545 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000546#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000547 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000548#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400549 break;
550
Dees_Troyd86400b2013-05-13 19:04:35 +0000551 case ABS_MT_TRACKING_ID: //39
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500552#ifdef TW_IGNORE_ABS_MT_TRACKING_ID
553#ifdef _EVENT_LOGGING
554 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d ignored\n", e->deviceName, ev->value);
555#endif
556 return 1;
557#endif
Dees_Troyd86400b2013-05-13 19:04:35 +0000558 if (ev->value < 0) {
559 e->mt_p.x = 0;
560 e->mt_p.y = 0;
561 touchReleaseOnNextSynReport = 2;
562 use_tracking_id_negative_as_touch_release = 1;
563#ifdef _EVENT_LOGGING
564 if (use_tracking_id_negative_as_touch_release)
565 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
566#endif
567 }
568#ifdef _EVENT_LOGGING
569 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
570#endif
571 break;
572
573#ifdef _EVENT_LOGGING
574 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400575 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000576 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400577 return 1;
578 break;
579
580 case ABS_MT_TOOL_TYPE: //37
581 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
582 return 1;
583 break;
584
585 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000586 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400587 return 1;
588 break;
589
Dees_Troy51a0e822012-09-05 15:24:24 -0400590 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000591 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400592 return 1;
593 break;
Ethan Yonkerd6821a12015-08-26 15:29:23 -0500594 case ABS_MT_SLOT:
595 printf("EV: %s => ABS_MT_SLOT %d\n", e->deviceName, ev->value);
596 return 1;
597 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400598#endif
599
600 default:
601 // This is an unhandled message, just skip it
602 return 1;
603 }
604
605 if (ev->code != ABS_MT_POSITION)
606 {
607 lastWasSynReport = 0;
608 return 1;
609 }
610 }
611
612 // Check if we should ignore the message
613 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
614 {
615 lastWasSynReport = 0;
616 return 0;
617 }
618
619#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000620 if (ev->type == EV_SYN && ev->code == SYN_REPORT) printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
621 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT) printf("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400622#endif
623
624 // Discard the MT versions
625 if (ev->code == SYN_MT_REPORT) return 0;
626
Dees_Troyd86400b2013-05-13 19:04:35 +0000627 if (((lastWasSynReport == 1 || touchReleaseOnNextSynReport == 1) && !use_tracking_id_negative_as_touch_release) || (use_tracking_id_negative_as_touch_release && touchReleaseOnNextSynReport == 2))
Dees_Troy51a0e822012-09-05 15:24:24 -0400628 {
629 // Reset the value
630 touchReleaseOnNextSynReport = 0;
631
632 // We are a finger-up state
633 if (!discard)
634 {
635 // Report the key up
636 ev->type = EV_ABS;
637 ev->code = 0;
638 ev->value = (downX << 16) | downY;
639 }
640 downX = -1;
641 downY = -1;
642 if (discard)
643 {
644 discard = 0;
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100645
646 // Send the keyUp event
647 ev->type = EV_KEY;
648 ev->code = last_virt_key;
649 ev->value = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400650 }
651 return 0;
652 }
653 lastWasSynReport = 1;
654
655 // Retrieve where the x,y position is
656 if (e->p.synced & 0x03)
657 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000658 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400659 }
660 else if (e->mt_p.synced & 0x03)
661 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000662 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400663 }
664 else
665 {
666 // We don't have useful information to convey
667 return 1;
668 }
669
670#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
671 x ^= y;
672 y ^= x;
673 x ^= y;
674#endif
675#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000676 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400677#endif
678#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000679 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400680#endif
681
682#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000683 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400684#endif
685
686 // Clear the current sync states
687 e->p.synced = e->mt_p.synced = 0;
688
689 // If we have nothing useful to report, skip it
690 if (x == -1 || y == -1) return 1;
691
Ethan Yonker96bc6dd2015-03-18 11:44:34 -0500692 // Special case, we'll ignore touches on 0,0 because it usually means
693 // that we received extra data after our last sync and x and y were
694 // reset to 0. We should not be using 0,0 anyway.
695 if (x == 0 && y == 0)
696 return 1;
697
Dees_Troy51a0e822012-09-05 15:24:24 -0400698 // On first touch, see if we're at a virtual key
699 if (downX == -1)
700 {
701 // Attempt mapping to virtual key
702 for (i = 0; i < e->vk_count; ++i)
703 {
704 int xd = ABS(e->vks[i].centerx - x);
705 int yd = ABS(e->vks[i].centery - y);
706
707 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
708 {
709 ev->type = EV_KEY;
710 ev->code = e->vks[i].scancode;
711 ev->value = 1;
712
Vojtech Bocek0b7fe502014-03-13 17:36:52 +0100713 last_virt_key = e->vks[i].scancode;
714
Dees_Troy51a0e822012-09-05 15:24:24 -0400715 vibrate(VIBRATOR_TIME_MS);
716
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500717 // Mark that all further movement until lift is discard,
Dees_Troy51a0e822012-09-05 15:24:24 -0400718 // and make sure we don't come back into this area
719 discard = 1;
720 downX = 0;
721 return 0;
722 }
723 }
724 }
725
726 // If we were originally a button press, discard this event
727 if (discard)
728 {
729 return 1;
730 }
731
732 // Record where we started the touch for deciding if this is a key or a scroll
733 downX = x;
734 downY = y;
735
736 ev->type = EV_ABS;
737 ev->code = 1;
738 ev->value = (x << 16) | y;
739 return 0;
740}
741
thatde72b6d2015-02-08 08:55:00 +0100742int ev_get(struct input_event *ev, int timeout_ms)
Dees_Troy51a0e822012-09-05 15:24:24 -0400743{
744 int r;
745 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100746 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400747
Ethan Yonkere13fa632015-01-27 11:30:03 -0600748 gettimeofday(&curr, NULL);
749 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
750 {
751 struct stat st;
752 stat("/dev/input", &st);
753 if (st.st_mtime > lastInputMTime)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100754 {
Ethan Yonkere13fa632015-01-27 11:30:03 -0600755 printf("Reloading input devices\n");
756 ev_exit();
757 ev_init();
758 lastInputMTime = st.st_mtime;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100759 }
Ethan Yonkere13fa632015-01-27 11:30:03 -0600760 lastInputStat = curr;
761 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100762
thatde72b6d2015-02-08 08:55:00 +0100763 r = poll(ev_fds, ev_count, timeout_ms);
Dees_Troy51a0e822012-09-05 15:24:24 -0400764
Ethan Yonkere13fa632015-01-27 11:30:03 -0600765 if(r > 0) {
766 for(n = 0; n < ev_count; n++) {
767 if(ev_fds[n].revents & POLLIN) {
768 r = read(ev_fds[n].fd, ev, sizeof(*ev));
769 if(r == sizeof(*ev)) {
770 if (!vk_modify(&evs[n], ev))
771 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400772 }
773 }
774 }
thatc5837f32015-02-01 01:59:43 +0100775 return -1;
Ethan Yonkere13fa632015-01-27 11:30:03 -0600776 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400777
thatc5837f32015-02-01 01:59:43 +0100778 return -2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400779}
780
781int ev_wait(int timeout)
782{
783 return -1;
784}
785
786void ev_dispatch(void)
787{
788 return;
789}
790
791int ev_get_input(int fd, short revents, struct input_event *ev)
792{
793 return -1;
794}