local navigation
[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 >= KEY_F(1) && key <= KEY_F(12)) {
46                 return KB_F1 + (key - KEY_F(1));
47         }
48         if(key < 128) {
49                 return key;
50         }
51         return -1;
52 }
53
54 int poll_input(union event *ev)
55 {
56         ev->type = EV_KEY;
57         if((ev->key.key = convkey(getch())) == -1) {
58                 return 0;
59         }
60         return 1;
61 }
62
63 int have_input(void)
64 {
65         fd_set rdset;
66         struct timeval tv = {0, 0};
67
68         FD_ZERO(&rdset);
69         FD_SET(0, &rdset);
70
71         while(select(1, &rdset, 0, 0, &tv) == -1 && errno == EINTR);
72
73         if(FD_ISSET(0, &rdset)) {
74                 return 1;
75         }
76         return 0;
77 }