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