diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2015-07-09 14:54:23 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2015-07-09 16:41:45 +0200 |
| commit | 9e7cb40b1f527c1bf431d4ca6c9b9ca4e70b5459 (patch) | |
| tree | 9a6665f3a757d4c91bb3bfac210e16ed2bacf6dc | |
| parent | 66f5700d201c9456f917507db77a88115f38961c (diff) | |
| download | vis-9e7cb40b1f527c1bf431d4ca6c9b9ca4e70b5459.tar.gz vis-9e7cb40b1f527c1bf431d4ca6c9b9ca4e70b5459.tar.xz | |
ui: move input handling code to ui specific files
| -rw-r--r-- | editor.h | 5 | ||||
| -rw-r--r-- | ui-curses.c | 42 | ||||
| -rw-r--r-- | ui.h | 11 | ||||
| -rw-r--r-- | vis.c | 29 |
4 files changed, 50 insertions, 37 deletions
@@ -26,11 +26,6 @@ typedef union { void (*f)(Editor*); /* generic editor commands */ } Arg; -typedef struct { - char str[6]; /* UTF8 character or terminal escape code */ - int code; /* curses KEY_* constant */ -} Key; - #define MAX_KEYS 2 typedef Key KeyCombo[MAX_KEYS]; diff --git a/ui-curses.c b/ui-curses.c index 619524b..f2f6899 100644 --- a/ui-curses.c +++ b/ui-curses.c @@ -528,6 +528,46 @@ static void ui_suspend(Ui *ui) { raise(SIGSTOP); } +static bool ui_haskey(Ui *ui) { + nodelay(stdscr, TRUE); + int c = getch(); + if (c != ERR) + ungetch(c); + nodelay(stdscr, FALSE); + return c != ERR; +} + +static Key ui_getkey(Ui *ui) { + Key key = { .str = "", .code = 0 }; + int keycode = getch(), cur = 0; + if (keycode == ERR) + return key; + + if (keycode >= KEY_MIN) { + key.code = keycode; + } else { + key.str[cur++] = keycode; + int len = 1; + unsigned char keychar = keycode; + if (ISASCII(keychar)) len = 1; + else if (keychar == 0x1B || keychar >= 0xFC) len = 6; + else if (keychar >= 0xF8) len = 5; + else if (keychar >= 0xF0) len = 4; + else if (keychar >= 0xE0) len = 3; + else if (keychar >= 0xC0) len = 2; + len = MIN(len, LENGTH(key.str)); + + if (cur < len) { + nodelay(stdscr, TRUE); + for (int t; cur < len && (t = getch()) != ERR; cur++) + key.str[cur] = t; + nodelay(stdscr, FALSE); + } + } + + return key; +} + Ui *ui_curses_new(void) { setlocale(LC_CTYPE, ""); if (!getenv("ESCDELAY")) @@ -571,6 +611,8 @@ Ui *ui_curses_new(void) { .info = info, .info_hide = info_hide, .color_get = color_get, + .haskey = ui_haskey, + .getkey = ui_getkey, }; struct sigaction sa; @@ -15,6 +15,11 @@ enum UiOption { UI_OPTION_LINE_NUMBERS_RELATIVE = 1 << 1, }; +typedef struct { + char str[6]; /* UTF8 character or terminal escape code */ + int code; /* curses KEY_* constant */ +} Key; + #include <stdbool.h> #include <stdarg.h> #include "text.h" @@ -40,10 +45,8 @@ struct Ui { void (*update)(Ui*); void (*suspend)(Ui*); void (*resume)(Ui*); -/* TODO main loop integration, signal handling - Key getkey(void); - bool haskey(void); -*/ + Key (*getkey)(Ui*); + bool (*haskey)(Ui*); }; struct UiWin { @@ -2268,36 +2268,9 @@ static void keypress(Key *key) { } static Key getkey(void) { - Key key = { .str = "", .code = 0 }; - int keycode = getch(), cur = 0; - if (keycode == ERR) - return key; - - if (keycode >= KEY_MIN) { - key.code = keycode; - } else { - key.str[cur++] = keycode; - int len = 1; - unsigned char keychar = keycode; - if (ISASCII(keychar)) len = 1; - else if (keychar == 0x1B || keychar >= 0xFC) len = 6; - else if (keychar >= 0xF8) len = 5; - else if (keychar >= 0xF0) len = 4; - else if (keychar >= 0xE0) len = 3; - else if (keychar >= 0xC0) len = 2; - len = MIN(len, LENGTH(key.str)); - - if (cur < len) { - nodelay(stdscr, TRUE); - for (int t; cur < len && (t = getch()) != ERR; cur++) - key.str[cur] = t; - nodelay(stdscr, FALSE); - } - } - + Key key = vis->ui->getkey(vis->ui); if (config->keypress && !config->keypress(&key)) return (Key){ .str = "", .code = 0 }; - return key; } |
