blob: aecaf8f12dd96fff838d4050230944eec3b17ea8 [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>
23
24#include <linux/input.h>
25
26#include "../common.h"
27
28#include "minui.h"
29
30//#define _EVENT_LOGGING
31
Dees_Troy3f23d9e2013-05-03 21:04:05 +000032#define MAX_DEVICES 32
Dees_Troy51a0e822012-09-05 15:24:24 -040033
34#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
35#define VIBRATOR_TIME_MS 50
36
Dees_Troyf7596752012-09-28 13:21:36 -040037#ifndef SYN_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040038#define SYN_REPORT 0x00
Dees_Troyf7596752012-09-28 13:21:36 -040039#endif
40#ifndef SYN_CONFIG
Dees_Troy51a0e822012-09-05 15:24:24 -040041#define SYN_CONFIG 0x01
Dees_Troyf7596752012-09-28 13:21:36 -040042#endif
43#ifndef SYN_MT_REPORT
Dees_Troy51a0e822012-09-05 15:24:24 -040044#define SYN_MT_REPORT 0x02
Dees_Troyf7596752012-09-28 13:21:36 -040045#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040046
47#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
48#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
49#define ABS_MT_SLOT 0x2f
50#define ABS_MT_TOUCH_MAJOR 0x30
51#define ABS_MT_TOUCH_MINOR 0x31
52#define ABS_MT_WIDTH_MAJOR 0x32
53#define ABS_MT_WIDTH_MINOR 0x33
54#define ABS_MT_ORIENTATION 0x34
55#define ABS_MT_POSITION_X 0x35
56#define ABS_MT_POSITION_Y 0x36
57#define ABS_MT_TOOL_TYPE 0x37
58#define ABS_MT_BLOB_ID 0x38
59#define ABS_MT_TRACKING_ID 0x39
60#define ABS_MT_PRESSURE 0x3a
61#define ABS_MT_DISTANCE 0x3b
62
63enum {
64 DOWN_NOT,
65 DOWN_SENT,
66 DOWN_RELEASED,
67};
68
69struct virtualkey {
70 int scancode;
71 int centerx, centery;
72 int width, height;
73};
74
75struct position {
76 int x, y;
77 int synced;
78 struct input_absinfo xi, yi;
79};
80
81struct ev {
82 struct pollfd *fd;
83
84 struct virtualkey *vks;
85 int vk_count;
86
87 char deviceName[64];
88
89 int ignored;
90
91 struct position p, mt_p;
92 int down;
93};
94
95static struct pollfd ev_fds[MAX_DEVICES];
96static struct ev evs[MAX_DEVICES];
97static unsigned ev_count = 0;
98
99static inline int ABS(int x) {
100 return x<0?-x:x;
101}
102
103int vibrate(int timeout_ms)
104{
105 char str[20];
106 int fd;
107 int ret;
108
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000109 if (timeout_ms > 10000) timeout_ms = 1000;
110
Dees_Troy51a0e822012-09-05 15:24:24 -0400111 fd = open(VIBRATOR_TIMEOUT_FILE, O_WRONLY);
112 if (fd < 0)
113 return -1;
114
115 ret = snprintf(str, sizeof(str), "%d", timeout_ms);
116 ret = write(fd, str, ret);
117 close(fd);
118
119 if (ret < 0)
120 return -1;
121
122 return 0;
123}
124
125/* Returns empty tokens */
126static char *vk_strtok_r(char *str, const char *delim, char **save_str)
127{
128 if(!str)
129 {
130 if(!*save_str)
131 return NULL;
132
133 str = (*save_str) + 1;
134 }
135 *save_str = strpbrk(str, delim);
136
137 if (*save_str)
138 **save_str = '\0';
139
140 return str;
141}
142
143static int vk_init(struct ev *e)
144{
145 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
146 char vks[2048], *ts = NULL;
147 ssize_t len;
148 int vk_fd;
149 int i;
150
151 e->vk_count = 0;
152
153 len = strlen(vk_path);
154 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
155 if (len <= 0)
156 {
Dees_Troy2673cec2013-04-02 20:22:16 +0000157 printf("Unable to query event object.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400158 return -1;
159 }
160#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000161 printf("Event object: %s\n", e->deviceName);
Dees_Troy51a0e822012-09-05 15:24:24 -0400162#endif
163
164 // Blacklist these "input" devices
Dees_Troy07a3a742012-12-11 15:13:21 +0000165 if (strcmp(e->deviceName, "bma250") == 0 || strcmp(e->deviceName, "bma150") == 0)
Dees_Troy51a0e822012-09-05 15:24:24 -0400166 {
167 e->ignored = 1;
168 }
169
170 strcat(vk_path, e->deviceName);
171
172 // Some devices split the keys from the touchscreen
173 e->vk_count = 0;
174 vk_fd = open(vk_path, O_RDONLY);
175 if (vk_fd >= 0)
176 {
177 len = read(vk_fd, vks, sizeof(vks)-1);
178 close(vk_fd);
179 if (len <= 0)
180 return -1;
181
182 vks[len] = '\0';
183
184 /* Parse a line like:
185 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
186 */
187 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
188 if (*ts == ':')
189 ++e->vk_count;
190 }
191
192 if (e->vk_count % 6) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000193 printf("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 }
195 e->vk_count /= 6;
196 if (e->vk_count <= 0)
197 return -1;
198
199 e->down = DOWN_NOT;
200 }
201
202 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
203 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
204 e->p.synced = 0;
205#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000206 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 -0400207#endif
208
209 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
210 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
211 e->mt_p.synced = 0;
212#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000213 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 -0400214#endif
215
216 e->vks = malloc(sizeof(*e->vks) * e->vk_count);
217
218 for (i = 0; i < e->vk_count; ++i) {
219 char *token[6];
220 int j;
221
222 for (j = 0; j < 6; ++j) {
223 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
224 }
225
226 if (strcmp(token[0], "0x01") != 0) {
227 /* Java does string compare, so we do too. */
Dees_Troy2673cec2013-04-02 20:22:16 +0000228 printf("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
Dees_Troy51a0e822012-09-05 15:24:24 -0400229 continue;
230 }
231
232 e->vks[i].scancode = strtol(token[1], NULL, 0);
233 e->vks[i].centerx = strtol(token[2], NULL, 0);
234 e->vks[i].centery = strtol(token[3], NULL, 0);
235 e->vks[i].width = strtol(token[4], NULL, 0);
236 e->vks[i].height = strtol(token[5], NULL, 0);
237 }
238
239 return 0;
240}
241
242int ev_init(void)
243{
244 DIR *dir;
245 struct dirent *de;
246 int fd;
247
248 dir = opendir("/dev/input");
249 if(dir != 0) {
250 while((de = readdir(dir))) {
251// fprintf(stderr,"/dev/input/%s\n", de->d_name);
252 if(strncmp(de->d_name,"event",5)) continue;
253 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
254 if(fd < 0) continue;
255
256 ev_fds[ev_count].fd = fd;
257 ev_fds[ev_count].events = POLLIN;
258 evs[ev_count].fd = &ev_fds[ev_count];
259
260 /* Load virtualkeys if there are any */
261 vk_init(&evs[ev_count]);
262
263 ev_count++;
264 if(ev_count == MAX_DEVICES) break;
265 }
266 }
267
268 return 0;
269}
270
271void ev_exit(void)
272{
273 while (ev_count-- > 0) {
274 if (evs[ev_count].vk_count) {
275 free(evs[ev_count].vks);
276 evs[ev_count].vk_count = 0;
277 }
278 close(ev_fds[ev_count].fd);
279 }
280}
281
282static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
283{
284 int screen_pos;
285
286 if (info->minimum == info->maximum)
287 return 0;
288
289 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
290 return (screen_pos >= 0 && screen_pos < screen_size);
291}
292
Dees_Troyb7ecc092013-08-24 12:15:41 +0000293static int vk_tp_to_screen(struct position *p, int *x, int *y)
Dees_Troy51a0e822012-09-05 15:24:24 -0400294{
295 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
296 {
297 // In this case, we assume the screen dimensions are the same.
298 *x = p->x;
299 *y = p->y;
300 return 0;
301 }
302
Dees_Troyb7ecc092013-08-24 12:15:41 +0000303#ifdef _EVENT_LOGGING
304 printf("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
305#endif
306
Dees_Troy51a0e822012-09-05 15:24:24 -0400307#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
Dees_Troyb7ecc092013-08-24 12:15:41 +0000308 int fb_width = gr_fb_width();
309 int fb_height = gr_fb_height();
Dees_Troy51a0e822012-09-05 15:24:24 -0400310#else
311 // We need to swap the scaling sizes, too
Dees_Troyb7ecc092013-08-24 12:15:41 +0000312 int fb_width = gr_fb_height();
313 int fb_height = gr_fb_width();
Dees_Troy51a0e822012-09-05 15:24:24 -0400314#endif
315
316 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
317 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
318
319 if (*x >= 0 && *x < fb_width &&
320 *y >= 0 && *y < fb_height)
321 {
322 return 0;
323 }
324
325 return 1;
326}
327
328/* Translate a virtual key in to a real key event, if needed */
329/* Returns non-zero when the event should be consumed */
Dees_Troyb7ecc092013-08-24 12:15:41 +0000330static int vk_modify(struct ev *e, struct input_event *ev)
Dees_Troy51a0e822012-09-05 15:24:24 -0400331{
332 static int downX = -1, downY = -1;
333 static int discard = 0;
334 static int lastWasSynReport = 0;
335 static int touchReleaseOnNextSynReport = 0;
Dees_Troyd86400b2013-05-13 19:04:35 +0000336 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 -0400337 int i;
338 int x, y;
339
340 // This is used to ditch useless event handlers, like an accelerometer
341 if (e->ignored) return 1;
342
343 if (ev->type == EV_REL && ev->code == REL_Z)
344 {
345 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
346#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000347 printf("EV: Device disabled due to non-touchscreen messages.\n");
Dees_Troy51a0e822012-09-05 15:24:24 -0400348#endif
349 e->ignored = 1;
350 return 1;
351 }
352
353#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000354 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 -0400355#endif
356
357 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
358 if (ev->type == EV_KEY) {
359 return 0;
360 }
361
362 if (ev->type == EV_ABS) {
363 switch (ev->code) {
364
365 case ABS_X: //00
366 e->p.synced |= 0x01;
367 e->p.x = ev->value;
368#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000369 printf("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400370#endif
371 break;
372
373 case ABS_Y: //01
374 e->p.synced |= 0x02;
375 e->p.y = ev->value;
376#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000377 printf("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400378#endif
379 break;
380
381 case ABS_MT_POSITION: //2a
382 e->mt_p.synced = 0x03;
383 if (ev->value == (1 << 31))
384 {
385 e->mt_p.x = 0;
386 e->mt_p.y = 0;
387 lastWasSynReport = 1;
388 }
389 else
390 {
391 lastWasSynReport = 0;
392 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
393 e->mt_p.y = (ev->value & 0xFFFF);
394 }
395 break;
396
397 case ABS_MT_TOUCH_MAJOR: //30
398 if (ev->value == 0)
399 {
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800400#ifndef TW_IGNORE_MAJOR_AXIS_0
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 // We're in a touch release, although some devices will still send positions as well
402 e->mt_p.x = 0;
403 e->mt_p.y = 0;
404 touchReleaseOnNextSynReport = 1;
Ibrahim Awwal2e9cb012014-01-04 12:38:26 -0800405#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 }
407#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000408 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400409#endif
410 break;
411
412 case ABS_MT_PRESSURE: //3a
413 if (ev->value == 0)
414 {
415 // We're in a touch release, although some devices will still send positions as well
416 e->mt_p.x = 0;
417 e->mt_p.y = 0;
418 touchReleaseOnNextSynReport = 1;
419 }
420#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000421 printf("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400422#endif
423 break;
424
425 case ABS_MT_POSITION_X: //35
426 e->mt_p.synced |= 0x01;
427 e->mt_p.x = ev->value;
428#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000429 printf("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400430#endif
431 break;
432
433 case ABS_MT_POSITION_Y: //36
434 e->mt_p.synced |= 0x02;
435 e->mt_p.y = ev->value;
436#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000437 printf("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400438#endif
439 break;
440
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 case ABS_MT_TOUCH_MINOR: //31
Dees_Troyd86400b2013-05-13 19:04:35 +0000442#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000443 printf("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000444#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 break;
446
447 case ABS_MT_WIDTH_MAJOR: //32
Dees_Troyd86400b2013-05-13 19:04:35 +0000448#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000449 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000450#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 break;
452
453 case ABS_MT_WIDTH_MINOR: //33
Dees_Troyd86400b2013-05-13 19:04:35 +0000454#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000455 printf("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
Dees_Troyd86400b2013-05-13 19:04:35 +0000456#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400457 break;
458
Dees_Troyd86400b2013-05-13 19:04:35 +0000459 case ABS_MT_TRACKING_ID: //39
460 if (ev->value < 0) {
461 e->mt_p.x = 0;
462 e->mt_p.y = 0;
463 touchReleaseOnNextSynReport = 2;
464 use_tracking_id_negative_as_touch_release = 1;
465#ifdef _EVENT_LOGGING
466 if (use_tracking_id_negative_as_touch_release)
467 printf("using ABS_MT_TRACKING_ID value -1 to indicate touch releases\n");
468#endif
469 }
470#ifdef _EVENT_LOGGING
471 printf("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
472#endif
473 break;
474
475#ifdef _EVENT_LOGGING
476 // These are for touch logging purposes only
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 case ABS_MT_ORIENTATION: //34
Dees_Troy2673cec2013-04-02 20:22:16 +0000478 printf("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400479 return 1;
480 break;
481
482 case ABS_MT_TOOL_TYPE: //37
483 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
484 return 1;
485 break;
486
487 case ABS_MT_BLOB_ID: //38
Dees_Troy2673cec2013-04-02 20:22:16 +0000488 printf("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400489 return 1;
490 break;
491
Dees_Troy51a0e822012-09-05 15:24:24 -0400492 case ABS_MT_DISTANCE: //3b
Dees_Troy2673cec2013-04-02 20:22:16 +0000493 printf("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
Dees_Troy51a0e822012-09-05 15:24:24 -0400494 return 1;
495 break;
496#endif
497
498 default:
499 // This is an unhandled message, just skip it
500 return 1;
501 }
502
503 if (ev->code != ABS_MT_POSITION)
504 {
505 lastWasSynReport = 0;
506 return 1;
507 }
508 }
509
510 // Check if we should ignore the message
511 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
512 {
513 lastWasSynReport = 0;
514 return 0;
515 }
516
517#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000518 if (ev->type == EV_SYN && ev->code == SYN_REPORT) printf("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
519 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 -0400520#endif
521
522 // Discard the MT versions
523 if (ev->code == SYN_MT_REPORT) return 0;
524
Dees_Troyd86400b2013-05-13 19:04:35 +0000525 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 -0400526 {
527 // Reset the value
528 touchReleaseOnNextSynReport = 0;
529
530 // We are a finger-up state
531 if (!discard)
532 {
533 // Report the key up
534 ev->type = EV_ABS;
535 ev->code = 0;
536 ev->value = (downX << 16) | downY;
537 }
538 downX = -1;
539 downY = -1;
540 if (discard)
541 {
542 discard = 0;
543 return 1;
544 }
545 return 0;
546 }
547 lastWasSynReport = 1;
548
549 // Retrieve where the x,y position is
550 if (e->p.synced & 0x03)
551 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000552 vk_tp_to_screen(&e->p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400553 }
554 else if (e->mt_p.synced & 0x03)
555 {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000556 vk_tp_to_screen(&e->mt_p, &x, &y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400557 }
558 else
559 {
560 // We don't have useful information to convey
561 return 1;
562 }
563
564#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
565 x ^= y;
566 y ^= x;
567 x ^= y;
568#endif
569#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
Dees_Troyb7ecc092013-08-24 12:15:41 +0000570 x = gr_fb_width() - x;
Dees_Troy51a0e822012-09-05 15:24:24 -0400571#endif
572#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
Dees_Troyb7ecc092013-08-24 12:15:41 +0000573 y = gr_fb_height() - y;
Dees_Troy51a0e822012-09-05 15:24:24 -0400574#endif
575
576#ifdef _EVENT_LOGGING
Dees_Troy2673cec2013-04-02 20:22:16 +0000577 printf("EV: x: %d y: %d\n", x, y);
Dees_Troy51a0e822012-09-05 15:24:24 -0400578#endif
579
580 // Clear the current sync states
581 e->p.synced = e->mt_p.synced = 0;
582
583 // If we have nothing useful to report, skip it
584 if (x == -1 || y == -1) return 1;
585
586 // On first touch, see if we're at a virtual key
587 if (downX == -1)
588 {
589 // Attempt mapping to virtual key
590 for (i = 0; i < e->vk_count; ++i)
591 {
592 int xd = ABS(e->vks[i].centerx - x);
593 int yd = ABS(e->vks[i].centery - y);
594
595 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
596 {
597 ev->type = EV_KEY;
598 ev->code = e->vks[i].scancode;
599 ev->value = 1;
600
601 vibrate(VIBRATOR_TIME_MS);
602
603 // Mark that all further movement until lift is discard,
604 // and make sure we don't come back into this area
605 discard = 1;
606 downX = 0;
607 return 0;
608 }
609 }
610 }
611
612 // If we were originally a button press, discard this event
613 if (discard)
614 {
615 return 1;
616 }
617
618 // Record where we started the touch for deciding if this is a key or a scroll
619 downX = x;
620 downY = y;
621
622 ev->type = EV_ABS;
623 ev->code = 1;
624 ev->value = (x << 16) | y;
625 return 0;
626}
627
Dees_Troyb7ecc092013-08-24 12:15:41 +0000628int ev_get(struct input_event *ev, unsigned dont_wait)
Dees_Troy51a0e822012-09-05 15:24:24 -0400629{
630 int r;
631 unsigned n;
632
633 do {
634 r = poll(ev_fds, ev_count, dont_wait ? 0 : -1);
635
636 if(r > 0) {
637 for(n = 0; n < ev_count; n++) {
638 if(ev_fds[n].revents & POLLIN) {
639 r = read(ev_fds[n].fd, ev, sizeof(*ev));
640 if(r == sizeof(*ev)) {
Dees_Troyb7ecc092013-08-24 12:15:41 +0000641 if (!vk_modify(&evs[n], ev))
Dees_Troy51a0e822012-09-05 15:24:24 -0400642 return 0;
643 }
644 }
645 }
646 }
647 } while(dont_wait == 0);
648
649 return -1;
650}
651
652int ev_wait(int timeout)
653{
654 return -1;
655}
656
657void ev_dispatch(void)
658{
659 return;
660}
661
662int ev_get_input(int fd, short revents, struct input_event *ev)
663{
664 return -1;
665}