6fba0fc77bc88643fee8077a8f8076f5882072bb
[oftp] / src / unix / input.c
1 #include <errno.h>
2 #include <sys/select.h>
3 #include <curses.h>
4 #include "input.h"
5
6 int init_input(void)
7 {
8         return 0;
9 }
10
11 void cleanup_input(void)
12 {
13 }
14
15 static int convkey(int key)
16 {
17         switch(key) {
18         case KEY_DC:
19                 return KB_DEL;
20         case KEY_IC:
21                 return KB_INS;
22         case KEY_UP:
23                 return KB_UP;
24         case KEY_DOWN:
25                 return KB_DOWN;
26         case KEY_LEFT:
27                 return KB_LEFT;
28         case KEY_RIGHT:
29                 return KB_RIGHT;
30         case KEY_HOME:
31                 return KB_HOME;
32         case KEY_END:
33                 return KB_END;
34         case KEY_PPAGE:
35                 return KB_PGUP;
36         case KEY_NPAGE:
37                 return KB_PGDN;
38         case KEY_ENTER:
39                 return '\n';
40         case KEY_BACKSPACE:
41                 return '\b';
42         default:
43                 break;
44         }
45         if(key < 128) {
46                 return key;
47         }
48         return -1;
49 }
50
51 int poll_input(union event *ev)
52 {
53         ev->type = EV_KEY;
54         if((ev->key.key = convkey(getch())) == -1) {
55                 return 0;
56         }
57         return 1;
58 }
59
60 int have_input(void)
61 {
62         fd_set rdset;
63         struct timeval tv = {0, 0};
64
65         FD_ZERO(&rdset);
66         FD_SET(0, &rdset);
67
68         while(select(1, &rdset, 0, 0, &tv) == -1 && errno == EINTR);
69
70         if(FD_ISSET(0, &rdset)) {
71                 return 1;
72         }
73         return 0;
74 }