summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/agptek/button-agptek.c
blob: 2d2deda6f66cc9155f754d042d850326c332bf0d (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
/***************************************************************************
 *             __________               __   ___
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2017 Marcin Bukat
 *
 * 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"

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

static int button_map(int keycode)
{
    switch(keycode)
    {
        case KEY_LEFT:
            return BUTTON_LEFT;

        case KEY_RIGHT:
            return BUTTON_RIGHT;

        case KEY_UP:
            return BUTTON_UP;

        case KEY_DOWN:
            return BUTTON_DOWN;

        case KEY_PLAYPAUSE:
            return BUTTON_SELECT;

        case KEY_VOLUMEUP:
            return BUTTON_VOLUP;

        case KEY_VOLUMEDOWN:
            return BUTTON_VOLDOWN;

        case KEY_POWER:
            return BUTTON_POWER;

        default:
            return 0;
    }
}

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;
    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))
                {
                    int keycode = event.code;
                    /* event.value == 0x10000 means press
                     * event.value == 0 means release
                     */
                    bool press = event.value ? true : false;

                    /* map linux event code to rockbox button bitmap */
                    if(press)
                    {
                        button_bitmap |= button_map(keycode);
                    }
                    else
                    {
                        button_bitmap &= ~button_map(keycode);
                    }
                }
            }
        }
    }

    return button_bitmap;
}

bool headphones_inserted(void)
{
    int status = 0;
    const char * const sysfs_hp_switch = "/sys/class/switch/headset/state";
    sysfs_get_int(sysfs_hp_switch, &status);

    return status ? true : false;
}

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