summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/agptek/button-agptek.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/agptek/button-agptek.c')
-rw-r--r--firmware/target/hosted/agptek/button-agptek.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/firmware/target/hosted/agptek/button-agptek.c b/firmware/target/hosted/agptek/button-agptek.c
new file mode 100644
index 0000000000..a8b5debee5
--- /dev/null
+++ b/firmware/target/hosted/agptek/button-agptek.c
@@ -0,0 +1,149 @@
1/***************************************************************************
2 * __________ __ ___
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2017 Marcin Bukat
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
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>
26#include <fcntl.h>
27#include <string.h>
28#include <stdlib.h>
29
30#include "sysfs.h"
31#include "button.h"
32#include "button-target.h"
33#include "panic.h"
34
35#define NR_POLL_DESC 2
36static struct pollfd poll_fds[NR_POLL_DESC];
37
38static int button_map(int keycode)
39{
40 switch(keycode)
41 {
42 case KEY_LEFT:
43 return BUTTON_LEFT;
44
45 case KEY_RIGHT:
46 return BUTTON_RIGHT;
47
48 case KEY_UP:
49 return BUTTON_UP;
50
51 case KEY_DOWN:
52 return BUTTON_DOWN;
53
54 case KEY_PLAYPAUSE:
55 return BUTTON_SELECT;
56
57 case KEY_VOLUMEUP:
58 return BUTTON_VOLUP;
59
60 case KEY_VOLUMEDOWN:
61 return BUTTON_VOLDOWN;
62
63 case KEY_POWER:
64 return BUTTON_POWER;
65
66 default:
67 return 0;
68 }
69}
70
71void button_init_device(void)
72{
73 const char * const input_devs[] = {
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_RDWR);
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)
133{
134 int status = 0;
135 const char * const sysfs_hp_switch = "/sys/devices/switch/headset/status";
136 sysfs_get_int(sysfs_hp_switch, &status);
137
138 return status ? true : false;
139}
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}
149