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