]> rtime.felk.cvut.cz Git - coffee/mt-apps.git/blob - mt_sim.c
Make it easier to call showOfflineQueue() from remote scripts
[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         memset(uid, 0, sizeof(uid));
28         for (int i = 0; i < 14; i++)
29             uid[i] = ch;
30         rfid_json_print(pipefd, 42, 32, uid, (strlen(uid)+1)/2);
31         break;
32     }
33 }
34
35 int mt_sim_init(struct ev_loop *loop, int fd)
36 {
37     pipefd = fd;
38
39     if (!isatty(STDIN_FILENO)) {
40         fprintf(stderr, "stdin is not a terminal\n");
41         return -1;
42     }
43
44     struct termios term;
45     tcgetattr(STDIN_FILENO, &term);
46     saved_term = term;
47     term.c_lflag &= ~ICANON;
48     tcsetattr(STDIN_FILENO, TCSADRAIN, &term);
49
50     ev_io_init(&w, stdin_cb, STDIN_FILENO, EV_READ);
51     ev_io_start(loop, &w);
52
53     return 0;
54 }
55
56 void mt_sim_deinit(void)
57 {
58     tcsetattr(STDIN_FILENO, TCSADRAIN, &saved_term);
59 }