6 #include <linux/input.h>
7 #include <linux/input-event-codes.h>
10 #include "signal_exit.h"
11 #include "json_helpers.h"
13 void keys_json_print(int fd, char key)
15 static const char *type = "keys";
24 static void keys_cb(EV_P_ ev_io *w_, int revents)
26 ev_io_keys *w = (ev_io_keys *)w_;
28 struct input_event ev;
30 ssize_t n = read(w->w.fd, &ev, sizeof(ev));
31 if (n != sizeof(ev)) {
35 if (ev.type == 1 && ev.value == 1) {
37 case BTN_TRIGGER_HAPPY1:
40 case BTN_TRIGGER_HAPPY2:
43 case BTN_TRIGGER_HAPPY3:
46 case BTN_TRIGGER_HAPPY4:
49 case BTN_TRIGGER_HAPPY5:
52 case BTN_TRIGGER_HAPPY6:
55 case BTN_TRIGGER_HAPPY7:
58 case BTN_TRIGGER_HAPPY8:
61 case BTN_TRIGGER_HAPPY9:
64 case BTN_TRIGGER_HAPPY10:
67 case BTN_TRIGGER_HAPPY11:
70 case BTN_TRIGGER_HAPPY12:
73 case BTN_TRIGGER_HAPPY13:
76 case BTN_TRIGGER_HAPPY14:
79 case BTN_TRIGGER_HAPPY15:
82 case BTN_TRIGGER_HAPPY16:
85 case BTN_TRIGGER_HAPPY17:
88 case BTN_TRIGGER_HAPPY18:
91 case BTN_TRIGGER_HAPPY19:
94 case BTN_TRIGGER_HAPPY20:
98 fprintf(stderr, "unsupported event code: %d\n", ev.code);
103 keys_json_print(fd, key);
107 int mt_keys_init(mt_keys_t *self, struct ev_loop *loop, int fd)
109 char *dev = getenv("INPUT");
112 dev = "/dev/input/by-path/platform-gpio-keys-event";
114 int ev = open(dev, O_RDONLY);
121 ev_io_keys *w = &(self->w);
123 ev_io_init(&(w->w), keys_cb, ev, EV_READ);
124 ev_io_start(loop, (ev_io *)w);
129 void mt_keys_deinit(mt_keys_t *self)
131 if (close(self->fd) == 0) {
132 fprintf(stderr, "closed %d\n", self->fd);
139 int main(int argc, char **argv)
141 struct ev_loop *loop = EV_DEFAULT;
144 set_signal_exit(loop);
146 if (mt_keys_init(&keys, loop, STDOUT_FILENO) != 0) {
152 mt_keys_deinit(&keys);
153 ev_loop_destroy(loop);