diff options
Diffstat (limited to 'firmware/target/hosted/aigo/button-erosq.c')
-rw-r--r-- | firmware/target/hosted/aigo/button-erosq.c | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/firmware/target/hosted/aigo/button-erosq.c b/firmware/target/hosted/aigo/button-erosq.c new file mode 100644 index 0000000000..2735c48c71 --- /dev/null +++ b/firmware/target/hosted/aigo/button-erosq.c | |||
@@ -0,0 +1,188 @@ | |||
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 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | #include <poll.h> | ||
22 | //#include <dir.h> | ||
23 | #include <errno.h> | ||
24 | #include <unistd.h> | ||
25 | #include <sys/types.h> | ||
26 | #include <linux/input.h> | ||
27 | #include <fcntl.h> | ||
28 | #include <string.h> | ||
29 | #include <stdlib.h> | ||
30 | |||
31 | #include "sysfs.h" | ||
32 | #include "button.h" | ||
33 | #include "button-target.h" | ||
34 | #include "panic.h" | ||
35 | |||
36 | #include "kernel.h" | ||
37 | #include "backlight.h" | ||
38 | #include "backlight-target.h" | ||
39 | #include "erosqlinux_codec.h" | ||
40 | |||
41 | #define NR_POLL_DESC 3 | ||
42 | static struct pollfd poll_fds[NR_POLL_DESC]; | ||
43 | |||
44 | static int button_map(int keycode) | ||
45 | { | ||
46 | switch(keycode) | ||
47 | { | ||
48 | case KEY_POWER: | ||
49 | return BUTTON_POWER; | ||
50 | |||
51 | case KEY_MENU: | ||
52 | return BUTTON_MENU; | ||
53 | |||
54 | case KEY_BACK: | ||
55 | return BUTTON_BACK; | ||
56 | |||
57 | case KEY_NEXTSONG: | ||
58 | return BUTTON_PREV; | ||
59 | |||
60 | case KEY_PREVIOUSSONG: | ||
61 | return BUTTON_NEXT; // Yes, backwards! | ||
62 | |||
63 | case KEY_PLAYPAUSE: | ||
64 | return BUTTON_PLAY; | ||
65 | |||
66 | case KEY_LEFT: | ||
67 | return BUTTON_SCROLL_BACK; | ||
68 | |||
69 | case KEY_RIGHT: | ||
70 | return BUTTON_SCROLL_FWD; | ||
71 | |||
72 | case KEY_VOLUMEUP: | ||
73 | return BUTTON_VOL_UP; | ||
74 | |||
75 | case KEY_VOLUMEDOWN: | ||
76 | return BUTTON_VOL_DOWN; | ||
77 | |||
78 | default: | ||
79 | return 0; | ||
80 | } | ||
81 | } | ||
82 | |||
83 | void button_init_device(void) | ||
84 | { | ||
85 | const char * const input_devs[] = { | ||
86 | "/dev/input/event0", // Rotary encoder | ||
87 | "/dev/input/event1" // Keys | ||
88 | }; | ||
89 | |||
90 | for(int i = 0; i < NR_POLL_DESC; i++) | ||
91 | { | ||
92 | int fd = open(input_devs[i], O_RDWR | O_CLOEXEC); | ||
93 | |||
94 | if(fd < 0) | ||
95 | { | ||
96 | panicf("Cannot open input device: %s\n", input_devs[i]); | ||
97 | } | ||
98 | |||
99 | poll_fds[i].fd = fd; | ||
100 | poll_fds[i].events = POLLIN; | ||
101 | poll_fds[i].revents = 0; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | int button_read_device(void) | ||
106 | { | ||
107 | static int button_bitmap = 0; | ||
108 | struct input_event event; | ||
109 | |||
110 | // FIXME TODO: Make this work via HAVE_SCROLL_WHEEL instead | ||
111 | |||
112 | /* Wheel gives us press+release back to back, clear them after time elapses */ | ||
113 | static long last_tick = 0; | ||
114 | if (button_bitmap & (BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD) && | ||
115 | current_tick - last_tick >= 2) | ||
116 | { | ||
117 | button_bitmap &= ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); | ||
118 | } | ||
119 | |||
120 | /* check if there are any events pending and process them */ | ||
121 | while(poll(poll_fds, NR_POLL_DESC, 0)) | ||
122 | { | ||
123 | for(int i = 0; i < NR_POLL_DESC; i++) | ||
124 | { | ||
125 | /* read only if non-blocking */ | ||
126 | if(poll_fds[i].revents & POLLIN) | ||
127 | { | ||
128 | int size = read(poll_fds[i].fd, &event, sizeof(event)); | ||
129 | if(size == (int)sizeof(event)) | ||
130 | { | ||
131 | int keycode = event.code; | ||
132 | /* event.value == 1 means press | ||
133 | * event.value == 0 means release | ||
134 | */ | ||
135 | bool press = event.value ? true : false; | ||
136 | |||
137 | /* map linux event code to rockbox button bitmap */ | ||
138 | if(press) | ||
139 | { | ||
140 | int bmap = button_map(keycode); | ||
141 | if (bmap & (BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD)) | ||
142 | last_tick = current_tick; | ||
143 | button_bitmap |= bmap; | ||
144 | } | ||
145 | else | ||
146 | { | ||
147 | /* Wheel gives us press+release back to back; ignore the release */ | ||
148 | int bmap = button_map(keycode) & ~(BUTTON_SCROLL_BACK|BUTTON_SCROLL_FWD); | ||
149 | button_bitmap &= ~bmap; | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | return button_bitmap; | ||
157 | } | ||
158 | |||
159 | bool headphones_inserted(void) | ||
160 | { | ||
161 | #ifdef BOOTLOADER | ||
162 | int ps = 0; | ||
163 | #else | ||
164 | int ps = erosq_get_outputs(); | ||
165 | #endif | ||
166 | |||
167 | return (ps == 2); | ||
168 | } | ||
169 | |||
170 | bool lineout_inserted(void) | ||
171 | { | ||
172 | #ifdef BOOTLOADER | ||
173 | int ps = 0; | ||
174 | #else | ||
175 | int ps = erosq_get_outputs(); | ||
176 | #endif | ||
177 | |||
178 | return (ps == 1); | ||
179 | } | ||
180 | |||
181 | void button_close_device(void) | ||
182 | { | ||
183 | /* close descriptors */ | ||
184 | for(int i = 0; i < NR_POLL_DESC; i++) | ||
185 | { | ||
186 | close(poll_fds[i].fd); | ||
187 | } | ||
188 | } | ||