summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSolomon Peachy <pizza@shaftnet.org>2020-10-29 09:37:57 -0400
committerSolomon Peachy <pizza@shaftnet.org>2020-10-31 14:17:22 +0000
commit125e97b0bf979e1d03b84b17e72fc655b74015fb (patch)
tree693b24abb83fad6dbc9e3da6b3b0fb22c0e20bd6
parentdd82f13fa1241266576b508180fcf90b8d9bda2c (diff)
downloadrockbox-125e97b0bf979e1d03b84b17e72fc655b74015fb.tar.gz
rockbox-125e97b0bf979e1d03b84b17e72fc655b74015fb.zip
hosted: Consolidate common /dev/input code shared by the hiby targets
* Enable dynamic number of input devices, needed for bluetooth support Change-Id: Ic2425834eb9b0fff298899ab45f3115ce1d95e91
-rw-r--r--firmware/SOURCES1
-rw-r--r--firmware/target/hosted/agptek/button-agptek.c84
-rw-r--r--firmware/target/hosted/agptek/button-target.h2
-rw-r--r--firmware/target/hosted/aigo/button-erosq.c106
-rw-r--r--firmware/target/hosted/aigo/button-target.h2
-rw-r--r--firmware/target/hosted/button-devinput.c130
-rw-r--r--firmware/target/hosted/fiio/button-fiio.c3
-rw-r--r--firmware/target/hosted/xduoo/button-target.h2
-rw-r--r--firmware/target/hosted/xduoo/button-xduoo.c88
9 files changed, 144 insertions, 274 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index 7f08e4870f..11929d8253 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -145,6 +145,7 @@ target/hosted/xduoo/powermgmt-xduoo.c
145 145
146#if defined(HIBY_LINUX) && !defined(SIMULATOR) 146#if defined(HIBY_LINUX) && !defined(SIMULATOR)
147target/hosted/usb-hiby.c 147target/hosted/usb-hiby.c
148target/hosted/button-devinput.c
148#endif 149#endif
149 150
150#if (defined(FIIO_M3K)) && !defined(SIMULATOR) 151#if (defined(FIIO_M3K)) && !defined(SIMULATOR)
diff --git a/firmware/target/hosted/agptek/button-agptek.c b/firmware/target/hosted/agptek/button-agptek.c
index 2c84a50405..8fc676acec 100644
--- a/firmware/target/hosted/agptek/button-agptek.c
+++ b/firmware/target/hosted/agptek/button-agptek.c
@@ -17,25 +17,13 @@
17 * KIND, either express or implied. 17 * KIND, either express or implied.
18 * 18 *
19 ****************************************************************************/ 19 ****************************************************************************/
20#include <poll.h>
21//#include <dir.h>
22#include <errno.h>
23#include <unistd.h>
24#include <sys/types.h>
25#include <linux/input.h> 20#include <linux/input.h>
26#include <fcntl.h>
27#include <string.h>
28#include <stdlib.h>
29 21
30#include "sysfs.h" 22#include "sysfs.h"
31#include "button.h" 23#include "button.h"
32#include "button-target.h" 24#include "button-target.h"
33#include "panic.h"
34 25
35#define NR_POLL_DESC 2 26int button_map(int keycode)
36static struct pollfd poll_fds[NR_POLL_DESC];
37
38static int button_map(int keycode)
39{ 27{
40 switch(keycode) 28 switch(keycode)
41 { 29 {
@@ -68,67 +56,6 @@ static int button_map(int keycode)
68 } 56 }
69} 57}
70 58
71void button_init_device(void)
72{
73 const char * const input_devs[NR_POLL_DESC] = {
74 "/dev/input/event0",
75 "/dev/input/event1"
76 };
77
78 for(int i = 0; i < NR_POLL_DESC; i++)
79 {
80 int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC);
81
82 if(fd < 0)
83 {
84 panicf("Cannot open input device: %s\n", input_devs[i]);
85 }
86
87 poll_fds[i].fd = fd;
88 poll_fds[i].events = POLLIN;
89 poll_fds[i].revents = 0;
90 }
91}
92
93int button_read_device(void)
94{
95 static int button_bitmap = 0;
96 struct input_event event;
97
98 /* check if there are any events pending and process them */
99 while(poll(poll_fds, NR_POLL_DESC, 0))
100 {
101 for(int i = 0; i < NR_POLL_DESC; i++)
102 {
103 /* read only if non-blocking */
104 if(poll_fds[i].revents & POLLIN)
105 {
106 int size = read(poll_fds[i].fd, &event, sizeof(event));
107 if(size == (int)sizeof(event))
108 {
109 int keycode = event.code;
110 /* event.value == 0x10000 means press
111 * event.value == 0 means release
112 */
113 bool press = event.value ? true : false;
114
115 /* map linux event code to rockbox button bitmap */
116 if(press)
117 {
118 button_bitmap |= button_map(keycode);
119 }
120 else
121 {
122 button_bitmap &= ~button_map(keycode);
123 }
124 }
125 }
126 }
127 }
128
129 return button_bitmap;
130}
131
132bool headphones_inserted(void) 59bool headphones_inserted(void)
133{ 60{
134 int status = 0; 61 int status = 0;
@@ -137,12 +64,3 @@ bool headphones_inserted(void)
137 64
138 return status ? true : false; 65 return status ? true : false;
139} 66}
140
141void button_close_device(void)
142{
143 /* close descriptors */
144 for(int i = 0; i < NR_POLL_DESC; i++)
145 {
146 close(poll_fds[i].fd);
147 }
148}
diff --git a/firmware/target/hosted/agptek/button-target.h b/firmware/target/hosted/agptek/button-target.h
index e13891a554..66a29f60f7 100644
--- a/firmware/target/hosted/agptek/button-target.h
+++ b/firmware/target/hosted/agptek/button-target.h
@@ -39,5 +39,7 @@
39#define POWEROFF_BUTTON BUTTON_POWER 39#define POWEROFF_BUTTON BUTTON_POWER
40#define POWEROFF_COUNT 25 40#define POWEROFF_COUNT 25
41 41
42int button_map(int keycode);
43
42#endif /* _BUTTON_TARGET_H_ */ 44#endif /* _BUTTON_TARGET_H_ */
43 45
diff --git a/firmware/target/hosted/aigo/button-erosq.c b/firmware/target/hosted/aigo/button-erosq.c
index db7f7e24e5..1336442370 100644
--- a/firmware/target/hosted/aigo/button-erosq.c
+++ b/firmware/target/hosted/aigo/button-erosq.c
@@ -6,7 +6,6 @@
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/ 7 * \/ \/ \/ \/ \/
8 * 8 *
9 * Copyright (C) 2017 Marcin Bukat
10 * Copyright (C) 2020 Solomon Peachy 9 * Copyright (C) 2020 Solomon Peachy
11 * 10 *
12 * This program is free software; you can redistribute it and/or 11 * This program is free software; you can redistribute it and/or
@@ -18,30 +17,20 @@
18 * KIND, either express or implied. 17 * KIND, either express or implied.
19 * 18 *
20 ****************************************************************************/ 19 ****************************************************************************/
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> 20#include <linux/input.h>
27#include <fcntl.h>
28#include <string.h>
29#include <stdlib.h>
30 21
31#include "sysfs.h"
32#include "button.h" 22#include "button.h"
33#include "button-target.h" 23#include "button-target.h"
34#include "panic.h"
35 24
36#include "kernel.h" 25#include "kernel.h"
37#include "backlight.h"
38#include "backlight-target.h"
39#include "erosqlinux_codec.h" 26#include "erosqlinux_codec.h"
40 27
41#define NR_POLL_DESC 2 28/*
42static struct pollfd poll_fds[NR_POLL_DESC]; 29 /dev/input/event0: rotary encoder
30 /dev/input/event1: all keys
31*/
43 32
44static int button_map(int keycode) 33int button_map(int keycode)
45{ 34{
46 switch(keycode) 35 switch(keycode)
47 { 36 {
@@ -80,82 +69,6 @@ static int button_map(int keycode)
80 } 69 }
81} 70}
82 71
83void button_init_device(void)
84{
85 const char * const input_devs[NR_POLL_DESC] = {
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_RDONLY | O_CLOEXEC);
93
94 if(fd < 0)
95 {
96 panicf("Cannot open input device: %s (%d)\n", input_devs[i], errno);
97 }
98
99 poll_fds[i].fd = fd;
100 poll_fds[i].events = POLLIN;
101 poll_fds[i].revents = 0;
102 }
103}
104
105int 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
159bool headphones_inserted(void) 72bool headphones_inserted(void)
160{ 73{
161#ifdef BOOTLOADER 74#ifdef BOOTLOADER
@@ -177,12 +90,3 @@ bool lineout_inserted(void)
177 90
178 return (ps == 1); 91 return (ps == 1);
179} 92}
180
181void 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}
diff --git a/firmware/target/hosted/aigo/button-target.h b/firmware/target/hosted/aigo/button-target.h
index f59f491d2f..44bd914778 100644
--- a/firmware/target/hosted/aigo/button-target.h
+++ b/firmware/target/hosted/aigo/button-target.h
@@ -42,4 +42,6 @@
42#define POWEROFF_BUTTON BUTTON_POWER 42#define POWEROFF_BUTTON BUTTON_POWER
43#define POWEROFF_COUNT 25 43#define POWEROFF_COUNT 25
44 44
45int button_map(int keycode);
46
45#endif /* _BUTTON_TARGET_H_ */ 47#endif /* _BUTTON_TARGET_H_ */
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
39static int num_devices = 0;
40static struct pollfd poll_fds[NR_POLL_DESC];
41
42void 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
65void 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
75int 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}
diff --git a/firmware/target/hosted/fiio/button-fiio.c b/firmware/target/hosted/fiio/button-fiio.c
index 134ec5a426..4acbfb0ea7 100644
--- a/firmware/target/hosted/fiio/button-fiio.c
+++ b/firmware/target/hosted/fiio/button-fiio.c
@@ -19,7 +19,6 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#include <poll.h> 21#include <poll.h>
22//#include <dir.h>
23#include <errno.h> 22#include <errno.h>
24#include <unistd.h> 23#include <unistd.h>
25#include <sys/types.h> 24#include <sys/types.h>
@@ -34,8 +33,6 @@
34#include "panic.h" 33#include "panic.h"
35 34
36#include "kernel.h" 35#include "kernel.h"
37#include "backlight.h"
38#include "backlight-target.h"
39 36
40static int key_enter_delay = 0; 37static int key_enter_delay = 0;
41static int key_right_delay = 0; 38static int key_right_delay = 0;
diff --git a/firmware/target/hosted/xduoo/button-target.h b/firmware/target/hosted/xduoo/button-target.h
index 3240d3df8e..20bae1c6f6 100644
--- a/firmware/target/hosted/xduoo/button-target.h
+++ b/firmware/target/hosted/xduoo/button-target.h
@@ -40,4 +40,6 @@
40#define POWEROFF_BUTTON BUTTON_POWER 40#define POWEROFF_BUTTON BUTTON_POWER
41#define POWEROFF_COUNT 25 41#define POWEROFF_COUNT 25
42 42
43int button_map(int keycode);
44
43#endif /* _BUTTON_TARGET_H_ */ 45#endif /* _BUTTON_TARGET_H_ */
diff --git a/firmware/target/hosted/xduoo/button-xduoo.c b/firmware/target/hosted/xduoo/button-xduoo.c
index 67cb9a6c46..f5b6c6be62 100644
--- a/firmware/target/hosted/xduoo/button-xduoo.c
+++ b/firmware/target/hosted/xduoo/button-xduoo.c
@@ -18,30 +18,15 @@
18 * KIND, either express or implied. 18 * KIND, either express or implied.
19 * 19 *
20 ****************************************************************************/ 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> 21#include <linux/input.h>
27#include <fcntl.h>
28#include <string.h>
29#include <stdlib.h>
30 22
31#include "sysfs.h"
32#include "button.h" 23#include "button.h"
33#include "button-target.h" 24#include "button-target.h"
34#include "panic.h"
35 25
36#include "kernel.h" 26#include "kernel.h"
37#include "backlight.h"
38#include "backlight-target.h"
39#include "xduoolinux_codec.h" 27#include "xduoolinux_codec.h"
40 28
41#define NR_POLL_DESC 3 29int button_map(int keycode)
42static struct pollfd poll_fds[NR_POLL_DESC];
43
44static int button_map(int keycode)
45{ 30{
46 switch(keycode) 31 switch(keycode)
47 { 32 {
@@ -74,68 +59,6 @@ static int button_map(int keycode)
74 } 59 }
75} 60}
76 61
77void button_init_device(void)
78{
79 const char * const input_devs[NR_POLL_DESC] = {
80 "/dev/input/event0",
81 "/dev/input/event1",
82 "/dev/input/event2"
83 };
84
85 for(int i = 0; i < NR_POLL_DESC; i++)
86 {
87 int fd = open(input_devs[i], O_RDONLY | O_CLOEXEC);
88
89 if(fd < 0)
90 {
91 panicf("Cannot open input device: %s\n", input_devs[i]);
92 }
93
94 poll_fds[i].fd = fd;
95 poll_fds[i].events = POLLIN;
96 poll_fds[i].revents = 0;
97 }
98}
99
100int button_read_device(void)
101{
102 static int button_bitmap = 0;
103 struct input_event event;
104
105 /* check if there are any events pending and process them */
106 while(poll(poll_fds, NR_POLL_DESC, 0))
107 {
108 for(int i = 0; i < NR_POLL_DESC; i++)
109 {
110 /* read only if non-blocking */
111 if(poll_fds[i].revents & POLLIN)
112 {
113 int size = read(poll_fds[i].fd, &event, sizeof(event));
114 if(size == (int)sizeof(event))
115 {
116 int keycode = event.code;
117 /* event.value == 1 means press
118 * event.value == 0 means release
119 */
120 bool press = event.value ? true : false;
121
122 /* map linux event code to rockbox button bitmap */
123 if(press)
124 {
125 button_bitmap |= button_map(keycode);
126 }
127 else
128 {
129 button_bitmap &= ~button_map(keycode);
130 }
131 }
132 }
133 }
134 }
135
136 return button_bitmap;
137}
138
139bool headphones_inserted(void) 62bool headphones_inserted(void)
140{ 63{
141#ifdef BOOTLOADER 64#ifdef BOOTLOADER
@@ -156,12 +79,3 @@ bool lineout_inserted(void)
156#endif 79#endif
157 return (ps == 1); 80 return (ps == 1);
158} 81}
159
160void button_close_device(void)
161{
162 /* close descriptors */
163 for(int i = 0; i < NR_POLL_DESC; i++)
164 {
165 close(poll_fds[i].fd);
166 }
167}