blob: f07fc14b43154b8916462f4c8ea5bc55864c58e4 [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>
27
Dees_Troy51a0e822012-09-05 15:24:24 -040028
29#include "../common.h"
30
31#include "minui.h"
32
33//#define _EVENT_LOGGING
34
Dees_Troy3f23d9e2013-05-03 21:04:05 +000035#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040036
37#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
38#define VIBRATOR_TIME_MS 50
39
Dees_Troyf7596752012-09-28 13:21:36 -040040#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040041#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040042#endif
43#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040044#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040045#endif
46#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040047#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040048#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040049
50#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
51#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
52#define ABS_MT_SLOT 0x2f
53#define ABS_MT_TOUCH_MAJOR 0x30
54#define ABS_MT_TOUCH_MINOR 0x31
55#define ABS_MT_WIDTH_MAJOR 0x32
56#define ABS_MT_WIDTH_MINOR 0x33
57#define ABS_MT_ORIENTATION 0x34
58#define ABS_MT_POSITION_X 0x35
59#define ABS_MT_POSITION_Y 0x36
60#define ABS_MT_TOOL_TYPE 0x37
61#define ABS_MT_BLOB_ID 0x38
62#define ABS_MT_TRACKING_ID 0x39
63#define ABS_MT_PRESSURE 0x3a
64#define ABS_MT_DISTANCE 0x3b
65
66enum {
67 DOWN_NOT,
68 DOWN_SENT,
69 DOWN_RELEASED,
70};
71
72struct virtualkey {
73 int scancode;
74 int centerx, centery;
75 int width, height;
76};
77
78struct position {
79 int x, y;
80 int synced;
81 struct input_absinfo xi, yi;
82};
83
84struct ev {
85 struct pollfd *fd;
86
87 struct virtualkey *vks;
88 int vk_count;
89
90 char deviceName[64];
91
92 int ignored;
93
94 struct position p, mt_p;
95 int down;
96};
97
98static struct pollfd ev_fds[MAX_DEVICES];
99static struct ev evs[MAX_DEVICES];
100static unsigned ev_count = 0;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100101static struct timeval lastInputStat;
102static unsigned long lastInputMTime;
103static int has_mouse = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
105static inline int ABS(int x) {
106 return x<0?-x:x;
107}
108
109int vibrate(int timeout_ms)
110{
111 char str[20];
112 int fd;
113 int ret;
114
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000115 if (timeout_ms > 10000) timeout_ms = 1000;
116
Dees_Troy51a0e822012-09-05 15:24:24 -0400117 fd = open(VIBRATOR_TIMEOUT_FILE, O_WRONLY);
118 if (fd < 0)
119 return -1;
120
121 ret = snprintf(str, sizeof(str), "%d", timeout_ms);
122 ret = write(fd, str, ret);
123 close(fd);
124
125 if (ret < 0)
126 return -1;
127
128 return 0;
129}
130
131/* Returns empty tokens */
132static char *vk_strtok_r(char *str, const char *delim, char **save_str)
133{
134 if(!str)
135 {
136 if(!*save_str)
137 return NULL;
138
139 str = (*save_str) + 1;
140 }
141 *save_str = strpbrk(str, delim);
142
143 if (*save_str)
144 **save_str = '\0';
145
146 return str;
147}
148
149static int vk_init(struct ev *e)
150{
151 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
152 char vks[2048], *ts = NULL;
153 ssize_t len;
154 int vk_fd;
155 int i;
156
157 e->vk_count = 0;
158
159 len = strlen(vk_path);
160 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
161 if (len <= 0)
162 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000163 printf("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400164 return -1;
165 }
166#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000167 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400168#endif
169
170 // Blacklist these "input" devices
Dees_Troy07a3a742012-12-11 15:13:21 +0000171 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400172 {
173 e->ignored = 1;
174 }
175
176 strcat(vk_path, e->deviceName);
177
178 // Some devices split the keys from the touchscreen
179 e->vk_count = 0;
180 vk_fd = open(vk_path, O_RDONLY);
181 if (vk_fd >= 0)
182 {
183 len = read(vk_fd, vks, sizeof(vks)-1);
184 close(vk_fd);
185 if (len <= 0)
186 return -1;
187
188 vks[len] = '\0';
189
190 /* Parse a line like:
191 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
192 */
193 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
194 if (*ts == ':')
195 ++e->vk_count;
196 }
197
198 if (e->vk_count % 6) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000199 printf("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400200 }
201 e->vk_count /= 6;
202 if (e->vk_count <= 0)
203 return -1;
204
205 e->down = DOWN_NOT;
206 }
207
208 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
209 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
210 e->p.synced = 0;
211#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000212 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 -0400213#endif
214
215 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
216 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
217 e->mt_p.synced = 0;
218#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000219 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 -0400220#endif
221
222 e->vks = malloc(sizeof(*e->vks) * e->vk_count);
223
224 for (i = 0; i < e->vk_count; ++i) {
225 char *token[6];
226 int j;
227
228 for (j = 0; j < 6; ++j) {
229 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
230 }
231
232 if (strcmp(token[0], "0x01") != 0) {
233 /* Java does string compare, so we do too. */
Dees_Troy2673cec2013-04-02 20:22:16 +0000234 printf("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400235 continue;
236 }
237
238 e->vks[i].scancode = strtol(token[1], NULL, 0);
239 e->vks[i].centerx = strtol(token[2], NULL, 0);
240 e->vks[i].centery = strtol(token[3], NULL, 0);
241 e->vks[i].width = strtol(token[4], NULL, 0);
242 e->vks[i].height = strtol(token[5], NULL, 0);
243 }
244
245 return 0;
246}
247
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100248#define BITS_PER_LONG (sizeof(long) * 8)
249#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
250#define OFF(x) ((x)%BITS_PER_LONG)
251#define LONG(x) ((x)/BITS_PER_LONG)
252#define test_bit(bit, array) ((array[LONG(bit)] >> OFF(bit)) & 1)
Vojtech Bocek971d3182014-02-20 21:43:28 +0100253
254// Check for EV_REL (REL_X and REL_Y) and, because touchscreens can have those too,
255// check also for EV_KEY (BTN_LEFT and BTN_RIGHT)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100256static void check_mouse(int fd)
257{
258 if(has_mouse)
259 return;
260
261 unsigned long bit[EV_MAX][NBITS(KEY_MAX)];
262 memset(bit, 0, sizeof(bit));
263 ioctl(fd, EVIOCGBIT(0, EV_MAX), bit[0]);
264
Vojtech Bocek971d3182014-02-20 21:43:28 +0100265 if(!test_bit(EV_REL, bit[0]) || !test_bit(EV_KEY, bit[0]))
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100266 return;
267
268 ioctl(fd, EVIOCGBIT(EV_REL, KEY_MAX), bit[EV_REL]);
Vojtech Bocek971d3182014-02-20 21:43:28 +0100269 if(!test_bit(REL_X, bit[EV_REL]) || !test_bit(REL_Y, bit[EV_REL]))
270 return;
271
272 ioctl(fd, EVIOCGBIT(EV_KEY, KEY_MAX), bit[EV_KEY]);
273 if(!test_bit(BTN_LEFT, bit[EV_KEY]) || !test_bit(BTN_RIGHT, bit[EV_KEY]))
274 return;
275
276 has_mouse = 1;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100277}
278
279int ev_has_mouse(void)
280{
281 return has_mouse;
282}
283
Dees_Troy51a0e822012-09-05 15:24:24 -0400284int ev_init(void)
285{
286 DIR *dir;
287 struct dirent *de;
288 int fd;
289
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100290 has_mouse = 0;
291
Dees_Troy51a0e822012-09-05 15:24:24 -0400292 dir = opendir("/dev/input");
293 if(dir != 0) {
294 while((de = readdir(dir))) {
295// fprintf(stderr,"/dev/input/%s\n", de->d_name);
296 if(strncmp(de->d_name,"event",5)) continue;
297 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
298 if(fd < 0) continue;
299
300 ev_fds[ev_count].fd = fd;
301 ev_fds[ev_count].events = POLLIN;
302 evs[ev_count].fd = &ev_fds[ev_count];
303
304 /* Load virtualkeys if there are any */
305 vk_init(&evs[ev_count]);
306
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100307 check_mouse(fd);
308
Dees_Troy51a0e822012-09-05 15:24:24 -0400309 ev_count++;
310 if(ev_count == MAX_DEVICES) break;
311 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100312 closedir(dir);
Dees_Troy51a0e822012-09-05 15:24:24 -0400313 }
314
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100315 struct stat st;
316 if(stat("/dev/input", &st) >= 0)
317 lastInputMTime = st.st_mtime;
318 gettimeofday(&lastInputStat, NULL);
319
Dees_Troy51a0e822012-09-05 15:24:24 -0400320 return 0;
321}
322
323void ev_exit(void)
324{
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100325 while (ev_count-- > 0) {
326 if (evs[ev_count].vk_count) {
327 free(evs[ev_count].vks);
328 evs[ev_count].vk_count = 0;
329 }
330 close(ev_fds[ev_count].fd);
Dees_Troy51a0e822012-09-05 15:24:24 -0400331 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100332 ev_count = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400333}
334
335static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
336{
337 int screen_pos;
338
339 if (info->minimum == info->maximum)
340 return 0;
341
342 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
343 return (screen_pos >= 0 && screen_pos < screen_size);
344}
345
Dees_Troyb7ecc092013-08-24 12:15:41 +0000346static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400347{
348 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
349 {
350 // In this case, we assume the screen dimensions are the same.
351 *x = p->x;
352 *y = p->y;
353 return 0;
354 }
355
Dees_Troyb7ecc092013-08-24 12:15:41 +0000356#ifdef _EVENT_LOGGING
357 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
358#endif
359
Dees_Troy51a0e822012-09-05 15:24:24 -0400360#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000361 int fb_width = gr_fb_width();
362 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400363#else
364 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000365 int fb_width = gr_fb_height();
366 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400367#endif
368
369 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
370 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
371
372 if (*x >= 0 && *x < fb_width &&
373 *y >= 0 && *y < fb_height)
374 {
375 return 0;
376 }
377
378 return 1;
379}
380
381/* Translate a virtual key in to a real key event, if needed */
382/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000383static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400384{
385 static int downX = -1, downY = -1;
386 static int discard = 0;
387 static int lastWasSynReport = 0;
388 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000389 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 -0400390 int i;
391 int x, y;
392
393 // This is used to ditch useless event handlers, like an accelerometer
394 if (e->ignored) return 1;
395
396 if (ev->type == EV_REL && ev->code == REL_Z)
397 {
398 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
399#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000400 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400401#endif
402 e->ignored = 1;
403 return 1;
404 }
405
406#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000407 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 -0400408#endif
409
410 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
411 if (ev->type == EV_KEY) {
412 return 0;
413 }
414
415 if (ev->type == EV_ABS) {
416 switch (ev->code) {
417
418 case ABS_X: //00
419 e->p.synced |= 0x01;
420 e->p.x = ev->value;
421#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000422 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400423#endif
424 break;
425
426 case ABS_Y: //01
427 e->p.synced |= 0x02;
428 e->p.y = ev->value;
429#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000430 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400431#endif
432 break;
433
434 case ABS_MT_POSITION: //2a
435 e->mt_p.synced = 0x03;
436 if (ev->value == (1 << 31))
437 {
438 e->mt_p.x = 0;
439 e->mt_p.y = 0;
440 lastWasSynReport = 1;
441 }
442 else
443 {
444 lastWasSynReport = 0;
445 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
446 e->mt_p.y = (ev->value & 0xFFFF);
447 }
448 break;
449
450 case ABS_MT_TOUCH_MAJOR: //30
451 if (ev->value == 0)
452 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800453#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 // We're in a touch release, although some devices will still send positions as well
455 e->mt_p.x = 0;
456 e->mt_p.y = 0;
457 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800458#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400459 }
460#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000461 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400462#endif
463 break;
464
465 case ABS_MT_PRESSURE: //3a
466 if (ev->value == 0)
467 {
468 // We're in a touch release, although some devices will still send positions as well
469 e->mt_p.x = 0;
470 e->mt_p.y = 0;
471 touchReleaseOnNextSynReport = 1;
472 }
473#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000474 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400475#endif
476 break;
477
478 case ABS_MT_POSITION_X: //35
479 e->mt_p.synced |= 0x01;
480 e->mt_p.x = ev->value;
481#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000482 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400483#endif
484 break;
485
486 case ABS_MT_POSITION_Y: //36
487 e->mt_p.synced |= 0x02;
488 e->mt_p.y = ev->value;
489#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000490 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400491#endif
492 break;
493
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000495#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000496 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000497#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400498 break;
499
500 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000501#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000502 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000503#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400504 break;
505
506 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000507#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000508 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000509#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400510 break;
511
Dees_Troyd86400b2013-05-13 19:04:35 +0000512 case ABS_MT_TRACKING_ID: //39
513 if (ev->value < 0) {
514 e->mt_p.x = 0;
515 e->mt_p.y = 0;
516 touchReleaseOnNextSynReport = 2;
517 use_tracking_id_negative_as_touch_release = 1;
518#ifdef _EVENT_LOGGING
519 if (use_tracking_id_negative_as_touch_release)
520 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
521#endif
522 }
523#ifdef _EVENT_LOGGING
524 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
525#endif
526 break;
527
528#ifdef _EVENT_LOGGING
529 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400530 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000531 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400532 return 1;
533 break;
534
535 case ABS_MT_TOOL_TYPE: //37
536 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
537 return 1;
538 break;
539
540 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000541 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400542 return 1;
543 break;
544
Dees_Troy51a0e822012-09-05 15:24:24 -0400545 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000546 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400547 return 1;
548 break;
549#endif
550
551 default:
552 // This is an unhandled message, just skip it
553 return 1;
554 }
555
556 if (ev->code != ABS_MT_POSITION)
557 {
558 lastWasSynReport = 0;
559 return 1;
560 }
561 }
562
563 // Check if we should ignore the message
564 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
565 {
566 lastWasSynReport = 0;
567 return 0;
568 }
569
570#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000571 if (ev->type == EV_SYN && ev->code == SYN_REPORT) printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
572 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 -0400573#endif
574
575 // Discard the MT versions
576 if (ev->code == SYN_MT_REPORT) return 0;
577
Dees_Troyd86400b2013-05-13 19:04:35 +0000578 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 -0400579 {
580 // Reset the value
581 touchReleaseOnNextSynReport = 0;
582
583 // We are a finger-up state
584 if (!discard)
585 {
586 // Report the key up
587 ev->type = EV_ABS;
588 ev->code = 0;
589 ev->value = (downX << 16) | downY;
590 }
591 downX = -1;
592 downY = -1;
593 if (discard)
594 {
595 discard = 0;
596 return 1;
597 }
598 return 0;
599 }
600 lastWasSynReport = 1;
601
602 // Retrieve where the x,y position is
603 if (e->p.synced & 0x03)
604 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000605 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400606 }
607 else if (e->mt_p.synced & 0x03)
608 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000609 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400610 }
611 else
612 {
613 // We don't have useful information to convey
614 return 1;
615 }
616
617#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
618 x ^= y;
619 y ^= x;
620 x ^= y;
621#endif
622#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000623 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400624#endif
625#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000626 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400627#endif
628
629#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000630 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400631#endif
632
633 // Clear the current sync states
634 e->p.synced = e->mt_p.synced = 0;
635
636 // If we have nothing useful to report, skip it
637 if (x == -1 || y == -1) return 1;
638
639 // On first touch, see if we're at a virtual key
640 if (downX == -1)
641 {
642 // Attempt mapping to virtual key
643 for (i = 0; i < e->vk_count; ++i)
644 {
645 int xd = ABS(e->vks[i].centerx - x);
646 int yd = ABS(e->vks[i].centery - y);
647
648 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
649 {
650 ev->type = EV_KEY;
651 ev->code = e->vks[i].scancode;
652 ev->value = 1;
653
654 vibrate(VIBRATOR_TIME_MS);
655
656 // Mark that all further movement until lift is discard,
657 // and make sure we don't come back into this area
658 discard = 1;
659 downX = 0;
660 return 0;
661 }
662 }
663 }
664
665 // If we were originally a button press, discard this event
666 if (discard)
667 {
668 return 1;
669 }
670
671 // Record where we started the touch for deciding if this is a key or a scroll
672 downX = x;
673 downY = y;
674
675 ev->type = EV_ABS;
676 ev->code = 1;
677 ev->value = (x << 16) | y;
678 return 0;
679}
680
Dees_Troyb7ecc092013-08-24 12:15:41 +0000681int ev_get(struct input_event *ev, unsigned dont_wait)
Dees_Troy51a0e822012-09-05 15:24:24 -0400682{
683 int r;
684 unsigned n;
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100685 struct timeval curr;
Dees_Troy51a0e822012-09-05 15:24:24 -0400686
687 do {
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100688 gettimeofday(&curr, NULL);
689 if(curr.tv_sec - lastInputStat.tv_sec >= 2)
690 {
691 struct stat st;
692 stat("/dev/input", &st);
693 if (st.st_mtime > lastInputMTime)
694 {
695 LOGI("Reloading input devices\n");
696 ev_exit();
697 ev_init();
698 lastInputMTime = st.st_mtime;
699 }
700 lastInputStat = curr;
701 }
702
703 r = poll(ev_fds, ev_count, 0);
Dees_Troy51a0e822012-09-05 15:24:24 -0400704
705 if(r > 0) {
706 for(n = 0; n < ev_count; n++) {
707 if(ev_fds[n].revents & POLLIN) {
708 r = read(ev_fds[n].fd, ev, sizeof(*ev));
709 if(r == sizeof(*ev)) {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000710 if (!vk_modify(&evs[n], ev))
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 return 0;
712 }
713 }
714 }
715 }
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +0100716
717 usleep(1000);
Dees_Troy51a0e822012-09-05 15:24:24 -0400718 } while(dont_wait == 0);
719
720 return -1;
721}
722
723int ev_wait(int timeout)
724{
725 return -1;
726}
727
728void ev_dispatch(void)
729{
730 return;
731}
732
733int ev_get_input(int fd, short revents, struct input_event *ev)
734{
735 return -1;
736}