diff options
Diffstat (limited to 'firmware/target/hosted/button-devinput.c')
-rw-r--r-- | firmware/target/hosted/button-devinput.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/firmware/target/hosted/button-devinput.c b/firmware/target/hosted/button-devinput.c new file mode 100644 index 0000000000..8c469b8308 --- /dev/null +++ b/firmware/target/hosted/button-devinput.c | |||
@@ -0,0 +1,130 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___ | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2017 Marcin Bukat | ||
10 | * Copyright (C) 2020 Solomon Peachy | ||
11 | |||
12 | * | ||
13 | * This program is free software; you can redistribute it and/or | ||
14 | * modify it under the terms of the GNU General Public License | ||
15 | * as published by the Free Software Foundation; either version 2 | ||
16 | * of the License, or (at your option) any later version. | ||
17 | * | ||
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
19 | * KIND, either express or implied. | ||
20 | * | ||
21 | ****************************************************************************/ | ||
22 | |||
23 | #include <poll.h> | ||
24 | #include <errno.h> | ||
25 | #include <unistd.h> | ||
26 | #include <sys/types.h> | ||
27 | #include <linux/input.h> | ||
28 | #include <fcntl.h> | ||
29 | #include <string.h> | ||
30 | #include <stdlib.h> | ||
31 | |||
32 | #include "kernel.h" | ||
33 | #include "sysfs.h" | ||
34 | #include "button.h" | ||
35 | #include "panic.h" | ||
36 | |||
37 | #define NR_POLL_DESC 4 | ||
38 | |||
39 | static int num_devices = 0; | ||
40 | static struct pollfd poll_fds[NR_POLL_DESC]; | ||
41 | |||
42 | void button_init_device(void) | ||
43 | { | ||
44 | const char * const input_devs[NR_POLL_DESC] = { | ||
45 | "/dev/input/event0", | ||
46 | "/dev/input/event1", | ||
47 | "/dev/input/event2", | ||
48 | "/dev/input/event3", | ||
49 | }; | ||
50 | |||
51 | for(int i = 0; i < NR_POLL_DESC; i++) | ||
52 | { | ||
53 | int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC); | ||
54 | |||
55 | if(fd >= 0) | ||
56 | { | ||
57 | poll_fds[num_devices].fd = fd; | ||
58 | poll_fds[num_devices].events = POLLIN; | ||
59 | poll_fds[num_devices].revents = 0; | ||
60 | num_devices++; | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | void button_close_device(void) | ||
66 | { | ||
67 | /* close descriptors */ | ||
68 | for(int i = 0; i < num_devices; i++) | ||
69 | { | ||
70 | close(poll_fds[i].fd); | ||
71 | } | ||
72 | num_devices = 0; | ||
73 | } | ||
74 | |||
75 | int button_read_device(void) | ||
76 | { | ||
77 | static int button_bitmap = 0; | ||
78 | struct input_event event; | ||
79 | |||
80 | #if defined(BUTTON_SCROLL_BACK) | ||
81 | // FIXME TODO: Make this work via HAVE_SCROLL_WHEEL instead | ||
82 | |||
83 | /* Wheel gives us press+release back to back, clear them after time elapses */ | ||
84 | static long last_tick = 0; | ||
85 | if (button_bitmap & (BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD) && | ||
86 | current_tick - last_tick >= 2) | ||
87 | { | ||
88 | button_bitmap &= ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); | ||
89 | } | ||
90 | #endif | ||
91 | |||
92 | /* check if there are any events pending and process them */ | ||
93 | while(poll(poll_fds, num_devices, 0)) | ||
94 | { | ||
95 | for(int i = 0; i < num_devices; i++) | ||
96 | { | ||
97 | /* read only if non-blocking */ | ||
98 | if(poll_fds[i].revents & POLLIN) | ||
99 | { | ||
100 | int size = read(poll_fds[i].fd, &event, sizeof(event)); | ||
101 | if(size == (int)sizeof(event)) | ||
102 | { | ||
103 | int keycode = event.code; | ||
104 | /* event.value == 0x10000 means press | ||
105 | * event.value == 0 means release | ||
106 | */ | ||
107 | bool press = event.value ? true : false; | ||
108 | |||
109 | /* map linux event code to rockbox button bitmap */ | ||
110 | if(press) | ||
111 | { | ||
112 | button_bitmap |= button_map(keycode); | ||
113 | } | ||
114 | else | ||
115 | { | ||
116 | #if defined(BUTTON_SCROLL_BACK) | ||
117 | /* Wheel gives us press+release back to back; ignore the release */ | ||
118 | int bmap = button_map(keycode) & ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); | ||
119 | button_bitmap &= ~bmap; | ||
120 | #else | ||
121 | button_bitmap &= ~button_map(keycode); | ||
122 | #endif | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | return button_bitmap; | ||
130 | } | ||