list widget selection
[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         default:
39                 break;
40         }
41         if(key < 128) {
42                 return key;
43         }
44         return -1;
45 }
46
47 int poll_input(union event *ev)
48 {
49         ev->type = EV_KEY;
50         if((ev->key.key = convkey(getch())) == -1) {
51                 return 0;
52         }
53         return 1;
54 }
55
56 int have_input(void)
57 {
58         fd_set rdset;
59         struct timeval tv = {0, 0};
60
61         FD_ZERO(&rdset);
62         FD_SET(0, &rdset);
63
64         while(select(1, &rdset, 0, 0, &tv) == -1 && errno == EINTR);
65
66         if(FD_ISSET(0, &rdset)) {
67                 return 1;
68         }
69         return 0;
70 }