]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_sim.c
Compile our version of libwebsockets if not found in the system
[coffee/mt-apps.git] / mt_sim.c
1 #include <unistd.h>
2 #include <termios.h>
3
4 #include "mt_sim.h"
5 #include "json_helpers.h"
6
7 static ev_io w;
8 static int pipefd;
9 static struct termios saved_term;
10
11 static void stdin_cb(EV_P_ ev_io *w, int revents)
12 {
13     char ch;
14
15     read(w->fd, &ch, sizeof(ch));
16
17     char uid[15];
18
19     switch (ch) {
20     case 'A' ... 'F':
21         keys_json_print(pipefd, ch);
22         break;
23     case 'a' ... 'f':
24         keys_json_print(pipefd, ch + 'A' - 'a');
25         break;
26     case '0' ... '9':
27         strcpy(uid, "?ABC0123456789");
28         uid[0] = ch;
29         rfid_json_print(pipefd, 42, 32, uid, (strlen(uid)+1)/2);
30         break;
31     }
32 }
33
34 int mt_sim_init(struct ev_loop *loop, int fd)
35 {
36     pipefd = fd;
37
38     if (!isatty(STDIN_FILENO)) {
39         fprintf(stderr, "stdin is not a terminal\n");
40         return -1;
41     }
42
43     struct termios term;
44     tcgetattr(STDIN_FILENO, &term);
45     saved_term = term;
46     term.c_lflag &= ~ICANON;
47     tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
48
49     ev_io_init(&w, stdin_cb, STDIN_FILENO, EV_READ);
50     ev_io_start(loop, &w);
51
52     return 0;
53 }
54
55 void mt_sim_deinit(void)
56 {
57     tcsetattr(STDIN_FILENO, TCSADRAIN, &saved_term);
58 }