]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_keys.c
Allow using different input event source
[coffee/mt-apps.git] / mt_keys.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <linux/input.h>
7 #include <linux/input-event-codes.h>
8
9 #include "mt_keys.h"
10 #include "signal_exit.h"
11 #include "json_helpers.h"
12
13 static void keys_cb(EV_P_ ev_io *w_, int revents)
14 {
15     static char *type = "keys";
16
17     ev_io_keys *w = (ev_io_keys *)w_;
18     int fd = w->fd;
19     struct input_event ev;
20     char key;
21     ssize_t n = read(w->w.fd, &ev, sizeof(ev));
22     if (n != sizeof(ev)) {
23         perror("read");
24         return;
25     }
26     if (ev.type == 1 && ev.value == 1) {
27         switch (ev.code) {
28             case BTN_TRIGGER_HAPPY1:
29                 key = 'A';
30                 break;
31             case BTN_TRIGGER_HAPPY2:
32                 key = 'B';
33                 break;
34             case BTN_TRIGGER_HAPPY3:
35                 key = 'C';
36                 break;
37             case BTN_TRIGGER_HAPPY4:
38                 key = 'D';
39                 break;
40             case BTN_TRIGGER_HAPPY5:
41                 key = 'E';
42                 break;
43             case BTN_TRIGGER_HAPPY6:
44                 key = 'F';
45                 break;
46             case BTN_TRIGGER_HAPPY7:
47                 key = 'G';
48                 break;
49             case BTN_TRIGGER_HAPPY8:
50                 key = 'H';
51                 break;
52             case BTN_TRIGGER_HAPPY9:
53                 key = 'I';
54                 break;
55             case BTN_TRIGGER_HAPPY10:
56                 key = 'J';
57                 break;
58             case BTN_TRIGGER_HAPPY11:
59                 key = 'K';
60                 break;
61             case BTN_TRIGGER_HAPPY12:
62                 key = 'L';
63                 break;
64             case BTN_TRIGGER_HAPPY13:
65                 key = 'M';
66                 break;
67             case BTN_TRIGGER_HAPPY14:
68                 key = 'N';
69                 break;
70             case BTN_TRIGGER_HAPPY15:
71                 key = 'O';
72                 break;
73             case BTN_TRIGGER_HAPPY16:
74                 key = 'P';
75                 break;
76             case BTN_TRIGGER_HAPPY17:
77                 key = 'Q';
78                 break;
79             case BTN_TRIGGER_HAPPY18:
80                 key = 'R';
81                 break;
82             case BTN_TRIGGER_HAPPY19:
83                 key = 'S';
84                 break;
85             case BTN_TRIGGER_HAPPY20:
86                 key = 'T';
87                 break;
88             default:
89                 fprintf(stderr, "unsupported event code: %d\n", ev.code);
90                 return;
91         }
92
93         JSON_START();
94         JSON_STR(type);
95         JSON_NEXT();
96         JSON_CHAR(key);
97         JSON_END();
98     }
99 }
100
101 int mt_keys_init(mt_keys_t *self, struct ev_loop *loop, int fd)
102 {
103     char *dev = getenv("INPUT");
104
105     if (!dev)
106         dev = "/dev/input/by-path/platform-gpio-keys-event";
107
108     int ev = open(dev, O_RDONLY);
109     if (ev == -1) {
110         perror(dev);
111         return -1;
112     }
113
114     self->fd = ev;
115     ev_io_keys *w = &(self->w);
116     w->fd = fd;
117     ev_io_init(&(w->w), keys_cb, ev, EV_READ);
118     ev_io_start(loop, (ev_io *)w);
119
120     return 0;
121 }
122
123 void mt_keys_deinit(mt_keys_t *self)
124 {
125     if (close(self->fd) == 0) {
126         fprintf(stderr, "closed %d\n", self->fd);
127     } else {
128         perror("close");
129     }
130 }
131
132 #ifndef NO_MAIN
133 int main(int argc, char **argv)
134 {
135     struct ev_loop *loop = EV_DEFAULT;
136     mt_keys_t keys;
137
138     set_signal_exit(loop);
139
140     if (mt_keys_init(&keys, loop, STDOUT_FILENO) != 0) {
141         return -1;
142     }
143
144     ev_run(loop, 0);
145
146     mt_keys_deinit(&keys);
147     ev_loop_destroy(loop);
148
149     return 0;
150 }
151 #endif