blob: 52c5a7d501b501737fb9a259b2754367b4920327 [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
32#define MAX_DEVICES 16
33
34#define VIBRATOR_TIMEOUT_FILE "/sys/class/timed_output/vibrator/enable"
35#define VIBRATOR_TIME_MS 50
36
37#define SYN_REPORT 0x00
38#define SYN_CONFIG 0x01
39#define SYN_MT_REPORT 0x02
40
41#define ABS_MT_POSITION 0x2a /* Group a set of X and Y */
42#define ABS_MT_AMPLITUDE 0x2b /* Group a set of Z and W */
43#define ABS_MT_SLOT 0x2f
44#define ABS_MT_TOUCH_MAJOR 0x30
45#define ABS_MT_TOUCH_MINOR 0x31
46#define ABS_MT_WIDTH_MAJOR 0x32
47#define ABS_MT_WIDTH_MINOR 0x33
48#define ABS_MT_ORIENTATION 0x34
49#define ABS_MT_POSITION_X 0x35
50#define ABS_MT_POSITION_Y 0x36
51#define ABS_MT_TOOL_TYPE 0x37
52#define ABS_MT_BLOB_ID 0x38
53#define ABS_MT_TRACKING_ID 0x39
54#define ABS_MT_PRESSURE 0x3a
55#define ABS_MT_DISTANCE 0x3b
56
57enum {
58 DOWN_NOT,
59 DOWN_SENT,
60 DOWN_RELEASED,
61};
62
63struct virtualkey {
64 int scancode;
65 int centerx, centery;
66 int width, height;
67};
68
69struct position {
70 int x, y;
71 int synced;
72 struct input_absinfo xi, yi;
73};
74
75struct ev {
76 struct pollfd *fd;
77
78 struct virtualkey *vks;
79 int vk_count;
80
81 char deviceName[64];
82
83 int ignored;
84
85 struct position p, mt_p;
86 int down;
87};
88
89static struct pollfd ev_fds[MAX_DEVICES];
90static struct ev evs[MAX_DEVICES];
91static unsigned ev_count = 0;
92
93static inline int ABS(int x) {
94 return x<0?-x:x;
95}
96
97int vibrate(int timeout_ms)
98{
99 char str[20];
100 int fd;
101 int ret;
102
103 fd = open(VIBRATOR_TIMEOUT_FILE, O_WRONLY);
104 if (fd < 0)
105 return -1;
106
107 ret = snprintf(str, sizeof(str), "%d", timeout_ms);
108 ret = write(fd, str, ret);
109 close(fd);
110
111 if (ret < 0)
112 return -1;
113
114 return 0;
115}
116
117/* Returns empty tokens */
118static char *vk_strtok_r(char *str, const char *delim, char **save_str)
119{
120 if(!str)
121 {
122 if(!*save_str)
123 return NULL;
124
125 str = (*save_str) + 1;
126 }
127 *save_str = strpbrk(str, delim);
128
129 if (*save_str)
130 **save_str = '\0';
131
132 return str;
133}
134
135static int vk_init(struct ev *e)
136{
137 char vk_path[PATH_MAX] = "/sys/board_properties/virtualkeys.";
138 char vks[2048], *ts = NULL;
139 ssize_t len;
140 int vk_fd;
141 int i;
142
143 e->vk_count = 0;
144
145 len = strlen(vk_path);
146 len = ioctl(e->fd->fd, EVIOCGNAME(sizeof(e->deviceName)), e->deviceName);
147 if (len <= 0)
148 {
149 LOGE("Unable to query event object.\n");
150 return -1;
151 }
152#ifdef _EVENT_LOGGING
153 LOGI("Event object: %s\n", e->deviceName);
154#endif
155
156 // Blacklist these "input" devices
157 if (strcmp(e->deviceName, "bma250") == 0)
158 {
159 e->ignored = 1;
160 }
161
162 strcat(vk_path, e->deviceName);
163
164 // Some devices split the keys from the touchscreen
165 e->vk_count = 0;
166 vk_fd = open(vk_path, O_RDONLY);
167 if (vk_fd >= 0)
168 {
169 len = read(vk_fd, vks, sizeof(vks)-1);
170 close(vk_fd);
171 if (len <= 0)
172 return -1;
173
174 vks[len] = '\0';
175
176 /* Parse a line like:
177 keytype:keycode:centerx:centery:width:height:keytype2:keycode2:centerx2:...
178 */
179 for (ts = vks, e->vk_count = 1; *ts; ++ts) {
180 if (*ts == ':')
181 ++e->vk_count;
182 }
183
184 if (e->vk_count % 6) {
185 LOGW("minui: %s is %d %% 6\n", vk_path, e->vk_count % 6);
186 }
187 e->vk_count /= 6;
188 if (e->vk_count <= 0)
189 return -1;
190
191 e->down = DOWN_NOT;
192 }
193
194 ioctl(e->fd->fd, EVIOCGABS(ABS_X), &e->p.xi);
195 ioctl(e->fd->fd, EVIOCGABS(ABS_Y), &e->p.yi);
196 e->p.synced = 0;
197#ifdef _EVENT_LOGGING
198 LOGI("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);
199#endif
200
201 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_X), &e->mt_p.xi);
202 ioctl(e->fd->fd, EVIOCGABS(ABS_MT_POSITION_Y), &e->mt_p.yi);
203 e->mt_p.synced = 0;
204#ifdef _EVENT_LOGGING
205 LOGI("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);
206#endif
207
208 e->vks = malloc(sizeof(*e->vks) * e->vk_count);
209
210 for (i = 0; i < e->vk_count; ++i) {
211 char *token[6];
212 int j;
213
214 for (j = 0; j < 6; ++j) {
215 token[j] = vk_strtok_r((i||j)?NULL:vks, ":", &ts);
216 }
217
218 if (strcmp(token[0], "0x01") != 0) {
219 /* Java does string compare, so we do too. */
220 LOGW("minui: %s: ignoring unknown virtual key type %s\n", vk_path, token[0]);
221 continue;
222 }
223
224 e->vks[i].scancode = strtol(token[1], NULL, 0);
225 e->vks[i].centerx = strtol(token[2], NULL, 0);
226 e->vks[i].centery = strtol(token[3], NULL, 0);
227 e->vks[i].width = strtol(token[4], NULL, 0);
228 e->vks[i].height = strtol(token[5], NULL, 0);
229 }
230
231 return 0;
232}
233
234int ev_init(void)
235{
236 DIR *dir;
237 struct dirent *de;
238 int fd;
239
240 dir = opendir("/dev/input");
241 if(dir != 0) {
242 while((de = readdir(dir))) {
243// fprintf(stderr,"/dev/input/%s\n", de->d_name);
244 if(strncmp(de->d_name,"event",5)) continue;
245 fd = openat(dirfd(dir), de->d_name, O_RDONLY);
246 if(fd < 0) continue;
247
248 ev_fds[ev_count].fd = fd;
249 ev_fds[ev_count].events = POLLIN;
250 evs[ev_count].fd = &ev_fds[ev_count];
251
252 /* Load virtualkeys if there are any */
253 vk_init(&evs[ev_count]);
254
255 ev_count++;
256 if(ev_count == MAX_DEVICES) break;
257 }
258 }
259
260 return 0;
261}
262
263void ev_exit(void)
264{
265 while (ev_count-- > 0) {
266 if (evs[ev_count].vk_count) {
267 free(evs[ev_count].vks);
268 evs[ev_count].vk_count = 0;
269 }
270 close(ev_fds[ev_count].fd);
271 }
272}
273
274static int vk_inside_display(__s32 value, struct input_absinfo *info, int screen_size)
275{
276 int screen_pos;
277
278 if (info->minimum == info->maximum)
279 return 0;
280
281 screen_pos = (value - info->minimum) * (screen_size - 1) / (info->maximum - info->minimum);
282 return (screen_pos >= 0 && screen_pos < screen_size);
283}
284
285static int vk_tp_to_screen(struct position *p, int *x, int *y)
286{
287 if (p->xi.minimum == p->xi.maximum || p->yi.minimum == p->yi.maximum)
288 {
289 // In this case, we assume the screen dimensions are the same.
290 *x = p->x;
291 *y = p->y;
292 return 0;
293 }
294
295#ifdef _EVENT_LOGGING
296 LOGI("EV: p->x=%d x-range=%d,%d fb-width=%d\n", p->x, p->xi.minimum, p->xi.maximum, gr_fb_width());
297#endif
298
299#ifndef RECOVERY_TOUCHSCREEN_SWAP_XY
300 int fb_width = gr_fb_width();
301 int fb_height = gr_fb_height();
302#else
303 // We need to swap the scaling sizes, too
304 int fb_width = gr_fb_height();
305 int fb_height = gr_fb_width();
306#endif
307
308 *x = (p->x - p->xi.minimum) * (fb_width - 1) / (p->xi.maximum - p->xi.minimum);
309 *y = (p->y - p->yi.minimum) * (fb_height - 1) / (p->yi.maximum - p->yi.minimum);
310
311 if (*x >= 0 && *x < fb_width &&
312 *y >= 0 && *y < fb_height)
313 {
314 return 0;
315 }
316
317 return 1;
318}
319
320/* Translate a virtual key in to a real key event, if needed */
321/* Returns non-zero when the event should be consumed */
322static int vk_modify(struct ev *e, struct input_event *ev)
323{
324 static int downX = -1, downY = -1;
325 static int discard = 0;
326 static int lastWasSynReport = 0;
327 static int touchReleaseOnNextSynReport = 0;
328 int i;
329 int x, y;
330
331 // This is used to ditch useless event handlers, like an accelerometer
332 if (e->ignored) return 1;
333
334 if (ev->type == EV_REL && ev->code == REL_Z)
335 {
336 // This appears to be an accelerometer or another strange input device. It's not the touchscreen.
337#ifdef _EVENT_LOGGING
338 LOGI("EV: Device disabled due to non-touchscreen messages.\n");
339#endif
340 e->ignored = 1;
341 return 1;
342 }
343
344#ifdef _EVENT_LOGGING
345 LOGI("EV: %s => type: %x code: %x value: %d\n", e->deviceName, ev->type, ev->code, ev->value);
346#endif
347
348 // Handle keyboard events, value of 1 indicates key down, 0 indicates key up
349 if (ev->type == EV_KEY) {
350 return 0;
351 }
352
353 if (ev->type == EV_ABS) {
354 switch (ev->code) {
355
356 case ABS_X: //00
357 e->p.synced |= 0x01;
358 e->p.x = ev->value;
359#ifdef _EVENT_LOGGING
360 LOGI("EV: %s => EV_ABS ABS_X %d\n", e->deviceName, ev->value);
361#endif
362 break;
363
364 case ABS_Y: //01
365 e->p.synced |= 0x02;
366 e->p.y = ev->value;
367#ifdef _EVENT_LOGGING
368 LOGI("EV: %s => EV_ABS ABS_Y %d\n", e->deviceName, ev->value);
369#endif
370 break;
371
372 case ABS_MT_POSITION: //2a
373 e->mt_p.synced = 0x03;
374 if (ev->value == (1 << 31))
375 {
376 e->mt_p.x = 0;
377 e->mt_p.y = 0;
378 lastWasSynReport = 1;
379 }
380 else
381 {
382 lastWasSynReport = 0;
383 e->mt_p.x = (ev->value & 0x7FFF0000) >> 16;
384 e->mt_p.y = (ev->value & 0xFFFF);
385 }
386 break;
387
388 case ABS_MT_TOUCH_MAJOR: //30
389 if (ev->value == 0)
390 {
391 // We're in a touch release, although some devices will still send positions as well
392 e->mt_p.x = 0;
393 e->mt_p.y = 0;
394 touchReleaseOnNextSynReport = 1;
395 }
396#ifdef _EVENT_LOGGING
397 LOGI("EV: %s => EV_ABS ABS_MT_TOUCH_MAJOR %d\n", e->deviceName, ev->value);
398#endif
399 break;
400
401 case ABS_MT_PRESSURE: //3a
402 if (ev->value == 0)
403 {
404 // We're in a touch release, although some devices will still send positions as well
405 e->mt_p.x = 0;
406 e->mt_p.y = 0;
407 touchReleaseOnNextSynReport = 1;
408 }
409#ifdef _EVENT_LOGGING
410 LOGI("EV: %s => EV_ABS ABS_MT_PRESSURE %d\n", e->deviceName, ev->value);
411#endif
412 break;
413
414 case ABS_MT_POSITION_X: //35
415 e->mt_p.synced |= 0x01;
416 e->mt_p.x = ev->value;
417#ifdef _EVENT_LOGGING
418 LOGI("EV: %s => EV_ABS ABS_MT_POSITION_X %d\n", e->deviceName, ev->value);
419#endif
420 break;
421
422 case ABS_MT_POSITION_Y: //36
423 e->mt_p.synced |= 0x02;
424 e->mt_p.y = ev->value;
425#ifdef _EVENT_LOGGING
426 LOGI("EV: %s => EV_ABS ABS_MT_POSITION_Y %d\n", e->deviceName, ev->value);
427#endif
428 break;
429
430#ifdef _EVENT_LOGGING
431 // All of these items are strictly for logging purposes only. Return 1 because they don't need to be handled.
432 case ABS_MT_TOUCH_MINOR: //31
433 LOGI("EV: %s => EV_ABS ABS_MT_TOUCH_MINOR %d\n", e->deviceName, ev->value);
434 return 1;
435 break;
436
437 case ABS_MT_WIDTH_MAJOR: //32
438 LOGI("EV: %s => EV_ABS ABS_MT_WIDTH_MAJOR %d\n", e->deviceName, ev->value);
439 return 1;
440 break;
441
442 case ABS_MT_WIDTH_MINOR: //33
443 LOGI("EV: %s => EV_ABS ABS_MT_WIDTH_MINOR %d\n", e->deviceName, ev->value);
444 return 1;
445 break;
446
447 case ABS_MT_ORIENTATION: //34
448 LOGI("EV: %s => EV_ABS ABS_MT_ORIENTATION %d\n", e->deviceName, ev->value);
449 return 1;
450 break;
451
452 case ABS_MT_TOOL_TYPE: //37
453 LOGI("EV: %s => EV_ABS ABS_MT_TOOL_TYPE %d\n", e->deviceName, ev->value);
454 return 1;
455 break;
456
457 case ABS_MT_BLOB_ID: //38
458 LOGI("EV: %s => EV_ABS ABS_MT_BLOB_ID %d\n", e->deviceName, ev->value);
459 return 1;
460 break;
461
462 case ABS_MT_TRACKING_ID: //39
463 LOGI("EV: %s => EV_ABS ABS_MT_TRACKING_ID %d\n", e->deviceName, ev->value);
464 return 1;
465 break;
466
467 case ABS_MT_DISTANCE: //3b
468 LOGI("EV: %s => EV_ABS ABS_MT_DISTANCE %d\n", e->deviceName, ev->value);
469 return 1;
470 break;
471#endif
472
473 default:
474 // This is an unhandled message, just skip it
475 return 1;
476 }
477
478 if (ev->code != ABS_MT_POSITION)
479 {
480 lastWasSynReport = 0;
481 return 1;
482 }
483 }
484
485 // Check if we should ignore the message
486 if (ev->code != ABS_MT_POSITION && (ev->type != EV_SYN || (ev->code != SYN_REPORT && ev->code != SYN_MT_REPORT)))
487 {
488 lastWasSynReport = 0;
489 return 0;
490 }
491
492#ifdef _EVENT_LOGGING
493 if (ev->type == EV_SYN && ev->code == SYN_REPORT) LOGI("EV: %s => EV_SYN SYN_REPORT\n", e->deviceName);
494 if (ev->type == EV_SYN && ev->code == SYN_MT_REPORT) LOGI("EV: %s => EV_SYN SYN_MT_REPORT\n", e->deviceName);
495#endif
496
497 // Discard the MT versions
498 if (ev->code == SYN_MT_REPORT) return 0;
499
500 if (lastWasSynReport == 1 || touchReleaseOnNextSynReport == 1)
501 {
502 // Reset the value
503 touchReleaseOnNextSynReport = 0;
504
505 // We are a finger-up state
506 if (!discard)
507 {
508 // Report the key up
509 ev->type = EV_ABS;
510 ev->code = 0;
511 ev->value = (downX << 16) | downY;
512 }
513 downX = -1;
514 downY = -1;
515 if (discard)
516 {
517 discard = 0;
518 return 1;
519 }
520 return 0;
521 }
522 lastWasSynReport = 1;
523
524 // Retrieve where the x,y position is
525 if (e->p.synced & 0x03)
526 {
527 vk_tp_to_screen(&e->p, &x, &y);
528 }
529 else if (e->mt_p.synced & 0x03)
530 {
531 vk_tp_to_screen(&e->mt_p, &x, &y);
532 }
533 else
534 {
535 // We don't have useful information to convey
536 return 1;
537 }
538
539#ifdef RECOVERY_TOUCHSCREEN_SWAP_XY
540 x ^= y;
541 y ^= x;
542 x ^= y;
543#endif
544#ifdef RECOVERY_TOUCHSCREEN_FLIP_X
545 x = gr_fb_width() - x;
546#endif
547#ifdef RECOVERY_TOUCHSCREEN_FLIP_Y
548 y = gr_fb_height() - y;
549#endif
550
551#ifdef _EVENT_LOGGING
552 LOGI("EV: x: %d y: %d\n", x, y);
553#endif
554
555 // Clear the current sync states
556 e->p.synced = e->mt_p.synced = 0;
557
558 // If we have nothing useful to report, skip it
559 if (x == -1 || y == -1) return 1;
560
561 // On first touch, see if we're at a virtual key
562 if (downX == -1)
563 {
564 // Attempt mapping to virtual key
565 for (i = 0; i < e->vk_count; ++i)
566 {
567 int xd = ABS(e->vks[i].centerx - x);
568 int yd = ABS(e->vks[i].centery - y);
569
570 if (xd < e->vks[i].width/2 && yd < e->vks[i].height/2)
571 {
572 ev->type = EV_KEY;
573 ev->code = e->vks[i].scancode;
574 ev->value = 1;
575
576 vibrate(VIBRATOR_TIME_MS);
577
578 // Mark that all further movement until lift is discard,
579 // and make sure we don't come back into this area
580 discard = 1;
581 downX = 0;
582 return 0;
583 }
584 }
585 }
586
587 // If we were originally a button press, discard this event
588 if (discard)
589 {
590 return 1;
591 }
592
593 // Record where we started the touch for deciding if this is a key or a scroll
594 downX = x;
595 downY = y;
596
597 ev->type = EV_ABS;
598 ev->code = 1;
599 ev->value = (x << 16) | y;
600 return 0;
601}
602
603int ev_get(struct input_event *ev, unsigned dont_wait)
604{
605 int r;
606 unsigned n;
607
608 do {
609 r = poll(ev_fds, ev_count, dont_wait ? 0 : -1);
610
611 if(r > 0) {
612 for(n = 0; n < ev_count; n++) {
613 if(ev_fds[n].revents & POLLIN) {
614 r = read(ev_fds[n].fd, ev, sizeof(*ev));
615 if(r == sizeof(*ev)) {
616 if (!vk_modify(&evs[n], ev))
617 return 0;
618 }
619 }
620 }
621 }
622 } while(dont_wait == 0);
623
624 return -1;
625}
626
627int ev_wait(int timeout)
628{
629 return -1;
630}
631
632void ev_dispatch(void)
633{
634 return;
635}
636
637int ev_get_input(int fd, short revents, struct input_event *ev)
638{
639 return -1;
640}