aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-05-22 15:47:47 -0700
committerBruce Hill <bruce@bruce-hill.com>2019-05-22 15:47:47 -0700
commitac69e52faaaf34a407727b493e4f636cf78fb1ca (patch)
tree00ea36be1ae52663e9b5fc4b512f0552441e38b3
parent512cbbdae42b3313bc7debb3efbcf50c3276982f (diff)
Added polling for blocking key reads
-rw-r--r--bb.c5
-rw-r--r--keys.h51
2 files changed, 32 insertions, 24 deletions
diff --git a/bb.c b/bb.c
index 40febba..7e4c6cb 100644
--- a/bb.c
+++ b/bb.c
@@ -28,6 +28,7 @@
#define writez(fd, str) write(fd, str, strlen(str))
#define IS_SELECTED(p) ((p)->atme)
+#define KEY_DELAY -1
static struct termios orig_termios;
static int termfd;
@@ -544,7 +545,7 @@ static void explore(char *path, int print_dir, int print_selection, char sep)
render(&state);
skip_redraw:
scrolloff = MIN(SCROLLOFF, (height-4)/2);
- int key = term_getkey(termfd, &mouse_x, &mouse_y);
+ int key = term_getkey(termfd, &mouse_x, &mouse_y, KEY_DELAY);
switch (key) {
case KEY_MOUSE_LEFT: {
struct timespec clicktime;
@@ -754,7 +755,7 @@ static void explore(char *path, int print_dir, int print_selection, char sep)
term_move(0, height-1);
writez(termfd, "\e[K\e[1mSort by (a)lphabetic (s)ize (t)ime (p)ermissions:\e[0m \e[?25h");
try_sort_again:
- switch (term_getkey(termfd, &mouse_x, &mouse_y)) {
+ switch (term_getkey(termfd, &mouse_x, &mouse_y, -1)) {
case 'a': case 'A':
sort_alpha:
if (state.sortmethod == SORT_ALPHA)
diff --git a/keys.h b/keys.h
index fc1e8b2..702d367 100644
--- a/keys.h
+++ b/keys.h
@@ -2,8 +2,10 @@
* Definitions of some key character constants
*/
-#ifndef KEYS_DEFINED
-#define KEYS_DEFINED
+#ifndef _KEYS_DEFINED
+#define _KEYS_DEFINED
+
+#include <poll.h>
#define KEY_F1 (0xFFFF-0)
#define KEY_F2 (0xFFFF-1)
@@ -82,22 +84,27 @@
#define KEY_CTRL_8 0x7F /* clash with 'BACKSPACE2' */
-static inline int nextchar(int fd)
+#define ESC_DELAY 10
+
+static inline int nextchar(int fd, int timeout)
{
char c;
- return read(fd, &c, 1) == 1 ? c : -1;
+ struct pollfd pfd = {fd, POLLIN, 0};
+ if (poll(&pfd, 1, timeout) > 0)
+ return read(fd, &c, 1) == 1 ? c : -1;
+ return -1;
}
-int term_getkey(int fd, int *mouse_x, int *mouse_y)
+int term_getkey(int fd, int *mouse_x, int *mouse_y, int timeout)
{
- int c = nextchar(fd);
+ int c = nextchar(fd, timeout);
if (c == '\x1b')
goto escape;
return c;
escape:
- c = nextchar(fd);
+ c = nextchar(fd, ESC_DELAY);
// Actual escape key:
if (c == -1)
return KEY_ESC;
@@ -110,7 +117,7 @@ int term_getkey(int fd, int *mouse_x, int *mouse_y)
}
CSI:
- c = nextchar(fd);
+ c = nextchar(fd, ESC_DELAY);
if (c == -1)
return -1;
@@ -122,37 +129,37 @@ int term_getkey(int fd, int *mouse_x, int *mouse_y)
case 'F': return KEY_END;
case 'H': return KEY_HOME;
case '1':
- switch (nextchar(fd)) {
+ switch (nextchar(fd, ESC_DELAY)) {
case '5':
- return nextchar(fd) == '~' ? KEY_F5 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F5 : -1;
case '7':
- return nextchar(fd) == '~' ? KEY_F6 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F6 : -1;
case '8':
- return nextchar(fd) == '~' ? KEY_F7 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F7 : -1;
case '9':
- return nextchar(fd) == '~' ? KEY_F8 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F8 : -1;
default: return -1;
}
break;
case '2':
- switch (nextchar(fd)) {
+ switch (nextchar(fd, ESC_DELAY)) {
case '0':
- return nextchar(fd) == '~' ? KEY_F9 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F9 : -1;
case '1':
- return nextchar(fd) == '~' ? KEY_F10 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F10 : -1;
case '3':
- return nextchar(fd) == '~' ? KEY_F11 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F11 : -1;
case '4':
- return nextchar(fd) == '~' ? KEY_F12 : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_F12 : -1;
default: return -1;
}
break;
case '3':
- return nextchar(fd) == '~' ? KEY_DELETE : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_DELETE : -1;
case '5':
- return nextchar(fd) == '~' ? KEY_PGUP : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_PGUP : -1;
case '6':
- return nextchar(fd) == '~' ? KEY_PGDN : -1;
+ return nextchar(fd, ESC_DELAY) == '~' ? KEY_PGDN : -1;
case '<': { // Mouse clicks
int buttons = 0, x = 0, y = 0;
char buf;
@@ -187,7 +194,7 @@ int term_getkey(int fd, int *mouse_x, int *mouse_y)
return -1;
SS3:
- switch (nextchar(fd)) {
+ switch (nextchar(fd, ESC_DELAY)) {
case 'P': return KEY_F1;
case 'Q': return KEY_F2;
case 'R': return KEY_F3;