summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/fiio/button-fiio.c
blob: fcc7480e117b634af46e48ba6843c1d0cc19005d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/***************************************************************************
 *             __________               __   ___
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2017 Marcin Bukat
 * Copyright (C) 2019 Roman Stolyarov
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <poll.h>
//#include <dir.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <linux/input.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>

#include "sysfs.h"
#include "button.h"
#include "button-target.h"
#include "panic.h"

#include "kernel.h"
#include "backlight.h"
#include "backlight-target.h"

static int key_enter_delay = 0;
static int key_right_delay = 0;
static int key_left_delay = 0;
static int key_power_delay = 0;
static int key_home_delay = 0;
static int key_backspace_delay = 0;
static int key_leftbrace_delay = 0;
static int key_rightbrace_delay = 0;
static int key_up_delay = 0;
static int key_down_delay = 0;
static int key_f12_delay = 0;

#define NR_POLL_DESC	2
static struct pollfd poll_fds[NR_POLL_DESC];

#define DEF_DELAY	5

static int button_map_on(int keycode)
{
    switch(keycode)
    {
        case KEY_ENTER:
            key_enter_delay = DEF_DELAY;
            return BUTTON_PLAY;
        case KEY_F10:
            key_enter_delay = 0;
            return BUTTON_PLAY;

        case KEY_RIGHT:
            key_right_delay = DEF_DELAY;
            return BUTTON_VOL_DOWN;
        case KEY_F7:
            key_right_delay = 0;
            return BUTTON_VOL_DOWN;

        case KEY_LEFT:
            key_left_delay = DEF_DELAY;
            return BUTTON_VOL_UP;
        case KEY_F6:
            key_left_delay = 0;
            return BUTTON_VOL_UP;

        case KEY_POWER:
            key_power_delay = DEF_DELAY;
            return BUTTON_POWER;
        case KEY_F8:
            key_power_delay = 0;
            return BUTTON_POWER;
        
        case KEY_HOME:
            key_home_delay = DEF_DELAY;
            return BUTTON_OPTION;
        case KEY_F9:
            key_home_delay = 0;
            return BUTTON_OPTION;

        case KEY_BACKSPACE:
            key_backspace_delay = DEF_DELAY;
            return BUTTON_HOME;
        case KEY_NUMLOCK:
            key_backspace_delay = 0;
            return BUTTON_HOME;

        case KEY_LEFTBRACE:
            key_leftbrace_delay = DEF_DELAY;
            return BUTTON_PREV;
        case KEY_F5:
            key_leftbrace_delay = 0;
            return BUTTON_PREV;

        case KEY_RIGHTBRACE:
            key_rightbrace_delay = DEF_DELAY;
            return BUTTON_NEXT;
        case KEY_F4:
            key_rightbrace_delay = 0;
            return BUTTON_NEXT;

        case KEY_UP:
            if (!key_up_delay) key_up_delay = DEF_DELAY;
            return BUTTON_UP;

        case KEY_DOWN:
            if (!key_down_delay) key_down_delay = DEF_DELAY;
            return BUTTON_DOWN;

        case KEY_F12:
            key_f12_delay = DEF_DELAY;
            //return BUTTON_UNLOCK;
            return 0;

        default:
            return 0;
    }
}

static int button_map_off(int keycode)
{
    switch(keycode)
    {
        case KEY_F10:
            return BUTTON_PLAY;

        case KEY_F7:
            return BUTTON_VOL_DOWN;

        case KEY_F6:
            return BUTTON_VOL_UP;

        case KEY_F8:
            return BUTTON_POWER;
        
        case KEY_F9:
            return BUTTON_OPTION;

        case KEY_NUMLOCK:
            return BUTTON_HOME;

        case KEY_F5:
            return BUTTON_PREV;

        case KEY_F4:
            return BUTTON_NEXT;

        default:
            return 0;
    }
}

static int button_map_timer(void)
{
  int map = 0;
  
  if (key_enter_delay)
  {
    if (--key_enter_delay == 0) map |= BUTTON_PLAY;
  }
  if (key_right_delay)
  {
    if (--key_right_delay == 0) map |= BUTTON_VOL_DOWN;
  }
  if (key_left_delay)
  {
    if (--key_left_delay == 0) map |= BUTTON_VOL_UP;
  }
  if (key_power_delay)
  {
    if (--key_power_delay == 0) map |= BUTTON_POWER;
  }
  if (key_home_delay)
  {
    if (--key_home_delay == 0) map |= BUTTON_OPTION;
  }
  if (key_backspace_delay)
  {
    if (--key_backspace_delay == 0) map |= BUTTON_HOME;
  }
  if (key_leftbrace_delay)
  {
    if (--key_leftbrace_delay == 0) map |= BUTTON_PREV;
  }
  if (key_rightbrace_delay)
  {
    if (--key_rightbrace_delay == 0) map |= BUTTON_NEXT;
  }
  if (key_up_delay)
  {
    if (--key_up_delay == 0) map |= BUTTON_UP;
  }
  if (key_down_delay)
  {
    if (--key_down_delay == 0) map |= BUTTON_DOWN;
  }
  if (key_f12_delay)
  {
    if (--key_f12_delay == 0) map |= 0; //BUTTON_UNLOCK
  }

  return map;
}

void button_init_device(void)
{
    const char * const input_devs[] = {
        "/dev/input/event0",
        "/dev/input/event1",
    };

    for(int i = 0; i < NR_POLL_DESC; i++)
    {
        int fd = open(input_devs[i], O_RDWR);

        if(fd < 0)
        {
            panicf("Cannot open input device: %s\n", input_devs[i]);
        }

        poll_fds[i].fd = fd;
        poll_fds[i].events = POLLIN;
        poll_fds[i].revents = 0;
    }
}

int button_read_device(void)
{
    static int button_bitmap = 0;
    static int map;
    struct input_event event;

    /* check if there are any events pending and process them */
    while(poll(poll_fds, NR_POLL_DESC, 0))
    {
        for(int i = 0; i < NR_POLL_DESC; i++)
        {
            /* read only if non-blocking */
            if(poll_fds[i].revents & POLLIN)
            {
                int size = read(poll_fds[i].fd, &event, sizeof(event));
                if(size == (int)sizeof(event))
                {
                    if(event.type == EV_KEY)
                    {
                        int keycode = event.code;

                        /* event.value == 1 means press
                         * event.value == 0 means release
                         */
                        bool press = event.value ? true : false;

                        if(press)
                        {
                            map = button_map_on(keycode);
                            if(map) button_bitmap |= map;
                        }
                        else
                        {
                            map = button_map_off(keycode);
                            if(map) button_bitmap &= ~map;
                        }
                    }
                }
            }
        }
    }

    map = button_map_timer();
    if(map) button_bitmap &= ~map;

    return button_bitmap;
}

bool headphones_inserted(void)
{
    int status = 0;
    const char * const sysfs_hs_switch = "/sys/class/misc/axp173/headset_state";

    sysfs_get_int(sysfs_hs_switch, &status);
    if (status) return true;

    return false;
}

void button_close_device(void)
{
    /* close descriptors */
    for(int i = 0; i < NR_POLL_DESC; i++)
    {
        close(poll_fds[i].fd);
    }
}