list widget selection
[oftp] / src / unix / input.c
index 50eb23e..92c1dcc 100644 (file)
@@ -1,3 +1,5 @@
+#include <errno.h>
+#include <sys/select.h>
 #include <curses.h>
 #include "input.h"
 
@@ -10,9 +12,59 @@ void cleanup_input(void)
 {
 }
 
-int wait_input(union event *ev)
+static int convkey(int key)
+{
+       switch(key) {
+       case KEY_DC:
+               return KB_DEL;
+       case KEY_IC:
+               return KB_INS;
+       case KEY_UP:
+               return KB_UP;
+       case KEY_DOWN:
+               return KB_DOWN;
+       case KEY_LEFT:
+               return KB_LEFT;
+       case KEY_RIGHT:
+               return KB_RIGHT;
+       case KEY_HOME:
+               return KB_HOME;
+       case KEY_END:
+               return KB_END;
+       case KEY_PPAGE:
+               return KB_PGUP;
+       case KEY_NPAGE:
+               return KB_PGDN;
+       default:
+               break;
+       }
+       if(key < 128) {
+               return key;
+       }
+       return -1;
+}
+
+int poll_input(union event *ev)
 {
        ev->type = EV_KEY;
-       ev->key.key = getch();
+       if((ev->key.key = convkey(getch())) == -1) {
+               return 0;
+       }
        return 1;
 }
+
+int have_input(void)
+{
+       fd_set rdset;
+       struct timeval tv = {0, 0};
+
+       FD_ZERO(&rdset);
+       FD_SET(0, &rdset);
+
+       while(select(1, &rdset, 0, 0, &tv) == -1 && errno == EINTR);
+
+       if(FD_ISSET(0, &rdset)) {
+               return 1;
+       }
+       return 0;
+}