6 #include <linux/input.h>
7 #include <linux/input-event-codes.h>
10 #include "signal_exit.h"
11 #include "json_helpers.h"
13 static void keys_cb(EV_P_ ev_io *w_, int revents)
15 static char *type = "keys";
17 ev_io_keys *w = (ev_io_keys *)w_;
19 struct input_event ev;
21 ssize_t n = read(w->w.fd, &ev, sizeof(ev));
22 if (n != sizeof(ev)) {
26 if (ev.type == 1 && ev.value == 1) {
28 case BTN_TRIGGER_HAPPY1:
31 case BTN_TRIGGER_HAPPY2:
34 case BTN_TRIGGER_HAPPY3:
37 case BTN_TRIGGER_HAPPY4:
40 case BTN_TRIGGER_HAPPY5:
43 case BTN_TRIGGER_HAPPY6:
46 case BTN_TRIGGER_HAPPY7:
49 case BTN_TRIGGER_HAPPY8:
52 case BTN_TRIGGER_HAPPY9:
55 case BTN_TRIGGER_HAPPY10:
58 case BTN_TRIGGER_HAPPY11:
61 case BTN_TRIGGER_HAPPY12:
64 case BTN_TRIGGER_HAPPY13:
67 case BTN_TRIGGER_HAPPY14:
70 case BTN_TRIGGER_HAPPY15:
73 case BTN_TRIGGER_HAPPY16:
76 case BTN_TRIGGER_HAPPY17:
79 case BTN_TRIGGER_HAPPY18:
82 case BTN_TRIGGER_HAPPY19:
85 case BTN_TRIGGER_HAPPY20:
89 fprintf(stderr, "unsupported event code: %d\n", ev.code);
101 int mt_keys_init(mt_keys_t *self, struct ev_loop *loop, int fd)
103 int ev = open("/dev/input/by-path/platform-gpio-keys-event", O_RDONLY);
105 perror("/dev/input/by-path/platform-gpio-keys-event");
110 ev_io_keys *w = &(self->w);
112 ev_io_init(&(w->w), keys_cb, ev, EV_READ);
113 ev_io_start(loop, (ev_io *)w);
118 void mt_keys_deinit(mt_keys_t *self)
120 if (close(self->fd) == 0) {
121 fprintf(stderr, "closed %d\n", self->fd);
128 int main(int argc, char **argv)
130 struct ev_loop *loop = EV_DEFAULT;
133 set_signal_exit(loop);
135 if (mt_keys_init(&keys, loop, STDOUT_FILENO) != 0) {
141 mt_keys_deinit(&keys);
142 ev_loop_destroy(loop);