summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/xduoo/button-xduoo.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/xduoo/button-xduoo.c')
-rw-r--r--firmware/target/hosted/xduoo/button-xduoo.c202
1 files changed, 202 insertions, 0 deletions
diff --git a/firmware/target/hosted/xduoo/button-xduoo.c b/firmware/target/hosted/xduoo/button-xduoo.c
new file mode 100644
index 0000000000..9fd1392b89
--- /dev/null
+++ b/firmware/target/hosted/xduoo/button-xduoo.c
@@ -0,0 +1,202 @@
1/***************************************************************************
2 * __________ __ ___
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2017 Marcin Bukat
10 * Copyright (C) 2018 Roman Stolyarov
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
40static bool soft_hold = false;
41#ifndef BOOTLOADER
42static unsigned soft_hold_counter = 0;
43#define SOFT_HOLD_BUTTON BUTTON_POWER
44#define SOFT_HOLD_CNTMAX_1 (HZ)
45#define SOFT_HOLD_CNTMAX_2 (HZ*2)
46#endif
47
48#define NR_POLL_DESC 3
49static struct pollfd poll_fds[NR_POLL_DESC];
50
51static int button_map(int keycode)
52{
53 switch(keycode)
54 {
55 case KEY_BACK:
56 return BUTTON_HOME;
57
58 case KEY_MENU:
59 return BUTTON_OPTION;
60
61 case KEY_UP:
62 return BUTTON_PREV;
63
64 case KEY_DOWN:
65 return BUTTON_NEXT;
66
67 case KEY_ENTER:
68 return BUTTON_PLAY;
69
70 case KEY_VOLUMEUP:
71 return BUTTON_VOL_UP;
72
73 case KEY_VOLUMEDOWN:
74 return BUTTON_VOL_DOWN;
75
76 case KEY_POWER:
77 return BUTTON_POWER;
78
79 default:
80 return 0;
81 }
82}
83
84void button_init_device(void)
85{
86 const char * const input_devs[] = {
87 "/dev/input/event0",
88 "/dev/input/event1",
89 "/dev/input/event2"
90 };
91
92 for(int i = 0; i < NR_POLL_DESC; i++)
93 {
94 int fd = open(input_devs[i], O_RDWR);
95
96 if(fd < 0)
97 {
98 panicf("Cannot open input device: %s\n", input_devs[i]);
99 }
100
101 poll_fds[i].fd = fd;
102 poll_fds[i].events = POLLIN;
103 poll_fds[i].revents = 0;
104 }
105}
106
107int button_read_device(void)
108{
109 static int button_bitmap = 0;
110 struct input_event event;
111
112 /* check if there are any events pending and process them */
113 while(poll(poll_fds, NR_POLL_DESC, 0))
114 {
115 for(int i = 0; i < NR_POLL_DESC; i++)
116 {
117 /* read only if non-blocking */
118 if(poll_fds[i].revents & POLLIN)
119 {
120 int size = read(poll_fds[i].fd, &event, sizeof(event));
121 if(size == (int)sizeof(event))
122 {
123 int keycode = event.code;
124 /* event.value == 1 means press
125 * event.value == 0 means release
126 */
127 bool press = event.value ? true : false;
128
129 /* map linux event code to rockbox button bitmap */
130 if(press)
131 {
132 button_bitmap |= button_map(keycode);
133 }
134 else
135 {
136 button_bitmap &= ~button_map(keycode);
137 }
138 }
139 }
140 }
141 }
142
143#ifndef BOOTLOADER
144 if (button_bitmap == SOFT_HOLD_BUTTON) {
145 soft_hold_counter++;
146 if (soft_hold_counter == SOFT_HOLD_CNTMAX_1) {
147 soft_hold = !soft_hold;
148 backlight_hold_changed(soft_hold);
149 }
150 else
151 if (soft_hold_counter == SOFT_HOLD_CNTMAX_2) {
152 soft_hold = false;
153 backlight_hold_changed(soft_hold);
154 }
155 } else {
156 soft_hold_counter = 0;
157 }
158
159 if((soft_hold) && (button_bitmap != SOFT_HOLD_BUTTON)) {
160 return BUTTON_NONE;
161 }
162#endif
163
164 return button_bitmap;
165}
166
167bool headphones_inserted(void)
168{
169 int status = 0;
170 const char * const sysfs_lo_switch = "/sys/class/switch/lineout/state";
171 const char * const sysfs_hs_switch = "/sys/class/switch/headset/state";
172#ifdef XDUOO_X20
173 const char * const sysfs_bal_switch = "/sys/class/switch/balance/state";
174#endif
175
176 sysfs_get_int(sysfs_lo_switch, &status);
177 if (status) return true;
178
179 sysfs_get_int(sysfs_hs_switch, &status);
180 if (status) return true;
181
182#ifdef XDUOO_X20
183 sysfs_get_int(sysfs_bal_switch, &status);
184 if (status) return true;
185#endif
186
187 return false;
188}
189
190void button_close_device(void)
191{
192 /* close descriptors */
193 for(int i = 0; i < NR_POLL_DESC; i++)
194 {
195 close(poll_fds[i].fd);
196 }
197}
198
199bool button_hold(void)
200{
201 return soft_hold;
202}