From ec41ec2c8789f502a43b6fa08bf3360220572fa9 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Sat, 20 Jul 2019 11:57:06 +0200 Subject: [PATCH] reintroduce focus, as I ran out of keycodes for the input --- ncdc/include/ncdc/keycodes.h | 3 +- ncdc/include/ncdc/mainwindow.h | 3 ++ ncdc/include/ncdc/ncdc.h | 2 +- ncdc/src/keycodes.c | 55 ++++++++++++++++------- ncdc/src/mainwindow.c | 81 ++++++++++++++++++++++------------ ncdc/src/util.c | 21 ++++++--- 6 files changed, 114 insertions(+), 51 deletions(-) diff --git a/ncdc/include/ncdc/keycodes.h b/ncdc/include/ncdc/keycodes.h index 59b813b..1f0aa1a 100644 --- a/ncdc/include/ncdc/keycodes.h +++ b/ncdc/include/ncdc/keycodes.h @@ -17,7 +17,8 @@ typedef struct { extern ncdc_keybinding_t keys_guilds[]; extern ncdc_keybinding_t keys_emacs[]; -extern ncdc_keybinding_t keys_mainwin[]; +extern ncdc_keybinding_t keys_chat[]; +extern ncdc_keybinding_t keys_global[]; ncdc_keybinding_t *ncdc_find_keybinding(ncdc_keybinding_t *keys, wchar_t const *key, size_t l); diff --git a/ncdc/include/ncdc/mainwindow.h b/ncdc/include/ncdc/mainwindow.h index d5f4639..7cfb2fb 100644 --- a/ncdc/include/ncdc/mainwindow.h +++ b/ncdc/include/ncdc/mainwindow.h @@ -23,6 +23,9 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n); void ncdc_mainwindow_rightview(ncdc_mainwindow_t n); void ncdc_mainwindow_leftview(ncdc_mainwindow_t n); +void ncdc_mainwindow_switch_guilds(ncdc_mainwindow_t n); +void ncdc_mainwindow_switch_input(ncdc_mainwindow_t n); +void ncdc_mainwindow_switch_chat(ncdc_mainwindow_t n); void ncdc_mainwindow_update_guilds(ncdc_mainwindow_t n); diff --git a/ncdc/include/ncdc/ncdc.h b/ncdc/include/ncdc/ncdc.h index fa926d3..e3866aa 100644 --- a/ncdc/include/ncdc/ncdc.h +++ b/ncdc/include/ncdc/ncdc.h @@ -56,7 +56,7 @@ extern void *mainwindow; bool is_logged_in(void); -wchar_t *util_readkey(int esc, WINDOW *win); +wchar_t *util_readkey(int esc); void exit_main(void); diff --git a/ncdc/src/keycodes.c b/ncdc/src/keycodes.c index 5eef037..3ef8aa0 100644 --- a/ncdc/src/keycodes.c +++ b/ncdc/src/keycodes.c @@ -22,54 +22,77 @@ ncdc_find_keybinding(ncdc_keybinding_t *keys, wchar_t const *key, size_t l) ncdc_keybinding_t keys_guilds[] = { /* CTRL+KEY_UP */ - NCDC_BINDING(L"\x1B[1;5A", L"previous-item", ncdc_treeview_previous), + NCDC_BINDING(L"\x1B[1;5A", L"previous-item", ncdc_treeview_previous), + NCDC_BINDING(L"\x1BOA", L"previous-item", ncdc_treeview_previous), /* CTRL+KEY_DOWN */ - NCDC_BINDING(L"\x1B[1;5B", L"next-item", ncdc_treeview_next), + NCDC_BINDING(L"\x1B[1;5B", L"next-item", ncdc_treeview_next), + NCDC_BINDING(L"\x1BOB", L"next-item", ncdc_treeview_next), /* CTRL+KEY_RIGHT */ - NCDC_BINDING(L"\x1B[1;5C", L"expand-item", ncdc_treeview_expand), + NCDC_BINDING(L"\x1B[1;5C", L"expand-item", ncdc_treeview_expand), + NCDC_BINDING(L"\x1BOC", L"expand-item", ncdc_treeview_expand), /* CTRL+KEY_LEFT */ - NCDC_BINDING(L"\x1B[1;5D", L"collapse-item", ncdc_treeview_collapse), + NCDC_BINDING(L"\x1B[1;5D", L"collapse-item", ncdc_treeview_collapse), + NCDC_BINDING(L"\x1BOD", L"collapse-item", ncdc_treeview_collapse), NCDC_BINDEND() }; -ncdc_keybinding_t keys_mainwin[] = { +ncdc_keybinding_t keys_chat[] = { /* ALT+KEY_RIGHT */ - NCDC_BINDING(L"\x1B[1;3C", L"right-window", ncdc_mainwindow_rightview), + NCDC_BINDING(L"\x1B[1;3C", L"right-window", ncdc_mainwindow_rightview), + NCDC_BINDING(L"\x1B0C", L"right-window", ncdc_mainwindow_rightview), /* ALT+KEY_LEFT */ - NCDC_BINDING(L"\x1B[1;3D", L"left-window", ncdc_mainwindow_leftview), + NCDC_BINDING(L"\x1B[1;3D", L"left-window", ncdc_mainwindow_leftview), + NCDC_BINDING(L"\x1B0D", L"left-window", ncdc_mainwindow_leftview), + + NCDC_BINDEND() +}; + +ncdc_keybinding_t keys_global[] = { + /* F1 + */ + NCDC_BINDING(L"\x1BOP", L"select-input", ncdc_mainwindow_switch_input), + /* F2 + */ + NCDC_BINDING(L"\x1BOQ", L"select-guilds", ncdc_mainwindow_switch_guilds), + /* F3 + */ + NCDC_BINDING(L"\x1BOR", L"select-chat", ncdc_mainwindow_switch_chat), + NCDC_BINDEND() }; ncdc_keybinding_t keys_emacs[] = { /* key left */ - NCDC_BINDCUR(KEY_LEFT, L"backward", ncdc_input_backward), + NCDC_BINDING(L"\x1BOD", L"backward", ncdc_input_backward), /* key right */ - NCDC_BINDCUR(KEY_RIGHT, L"forward", ncdc_input_forward), + NCDC_BINDING(L"\x1BOC", L"forward", ncdc_input_forward), /* CTRL+F */ - NCDC_BINDING(L"\x06", L"forward", ncdc_input_forward), + NCDC_BINDING(L"\x06", L"forward", ncdc_input_forward), /* CTRL+B */ - NCDC_BINDING(L"\x02", L"backward", ncdc_input_backward), + NCDC_BINDING(L"\x02", L"backward", ncdc_input_backward), /* CTRL+K */ - NCDC_BINDING(L"\x0B", L"kill-right", ncdc_input_kill_right), + NCDC_BINDING(L"\x0B", L"kill-right", ncdc_input_kill_right), /* CTRL+U */ - NCDC_BINDING(L"\x15", L"kill-left", ncdc_input_kill_left), + NCDC_BINDING(L"\x15", L"kill-left", ncdc_input_kill_left), /* CTRL+W */ - NCDC_BINDING(L"\x17", L"kill-word-left", ncdc_input_kill_word_left), + NCDC_BINDING(L"\x17", L"kill-word-left", ncdc_input_kill_word_left), /* CTRL+D */ - NCDC_BINDING(L"\x04", L"delete", ncdc_input_delete), - NCDC_BINDCUR(KEY_BACKSPACE, L"delete", ncdc_input_delete_backward), + NCDC_BINDING(L"\x04", L"delete", ncdc_input_delete), + /* BACKSPACE + */ + NCDC_BINDING(L"\x7F", L"delete", ncdc_input_delete_backward), NCDC_BINDEND() }; diff --git a/ncdc/src/mainwindow.c b/ncdc/src/mainwindow.c index 55cee06..386a476 100644 --- a/ncdc/src/mainwindow.c +++ b/ncdc/src/mainwindow.c @@ -184,14 +184,17 @@ static void ncdc_mainwindow_update_focus(ncdc_mainwindow_t n) switch (n->focus) { case FOCUS_GUILDS: { + curs_set(0); } break; case FOCUS_CHAT: { + curs_set(0); } break; case FOCUS_INPUT: { + curs_set(1); wmove(n->input, 0, ncdc_input_cursor(n->in)); wrefresh(n->input); } break; @@ -343,26 +346,12 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n) wint_t i = 0; wchar_t *key = NULL; size_t keylen = 0; - WINDOW *win = NULL; ncdc_keybinding_t *k = NULL; - switch (n->focus) { - case FOCUS_INPUT: win = n->input; break; - case FOCUS_CHAT: win = n->chat; break; - case FOCUS_GUILDS: win = n->guilds; break; - } - - if (wget_wch(win, &i) == ERR) { - return; - } - - if (i == KEY_RESIZE) { - ncdc_mainwindow_resize(n); - return; - } + i = fgetwc(stdin); if (i == KEY_ESCAPE) { - if ((key = util_readkey(i, n->input)) == NULL) { + if ((key = util_readkey(i)) == NULL) { return; } keylen = wcslen(key); @@ -379,23 +368,38 @@ void ncdc_mainwindow_input_ready(ncdc_mainwindow_t n) fclose(f); if (key != NULL && - (k = ncdc_find_keybinding(keys_mainwin, key, keylen)) != NULL) { + (k = ncdc_find_keybinding(keys_global, key, keylen)) != NULL) { k->handler(n); return; } - if (key != NULL && - (k = ncdc_find_keybinding(keys_guilds, key, keylen)) != NULL) { - k->handler(n->guildview); - return; - } - - if (n->focus == FOCUS_INPUT) { - if (key == NULL) { - ncdc_input_feed(n->in, (wchar_t const *)&i, 1); - } else { - ncdc_input_feed(n->in, key, wcslen(key)); + switch (n->focus) { + case FOCUS_CHAT: + { + if (key != NULL && + (k = ncdc_find_keybinding(keys_chat, key, keylen)) != NULL) { + k->handler(n->guildview); } + } break; + + case FOCUS_GUILDS: + { + if (key != NULL && + (k = ncdc_find_keybinding(keys_guilds, key, keylen)) != NULL) { + k->handler(n->guildview); + } + } break; + + case FOCUS_INPUT: + { + if (n->focus == FOCUS_INPUT) { + if (key == NULL) { + ncdc_input_feed(n->in, (wchar_t const *)&i, 1); + } else { + ncdc_input_feed(n->in, key, wcslen(key)); + } + } + } break; } free(key); @@ -469,6 +473,27 @@ static void ncdc_mainwindow_ack_view(ncdc_mainwindow_t n) #endif } +void ncdc_mainwindow_switch_guilds(ncdc_mainwindow_t n) +{ + return_if_true(n == NULL,); + n->focus = FOCUS_GUILDS; + ncdc_mainwindow_update_focus(n); +} + +void ncdc_mainwindow_switch_input(ncdc_mainwindow_t n) +{ + return_if_true(n == NULL,); + n->focus = FOCUS_INPUT; + ncdc_mainwindow_update_focus(n); +} + +void ncdc_mainwindow_switch_chat(ncdc_mainwindow_t n) +{ + return_if_true(n == NULL,); + n->focus = FOCUS_CHAT; + ncdc_mainwindow_update_focus(n); +} + void ncdc_mainwindow_rightview(ncdc_mainwindow_t n) { return_if_true(n == NULL,); diff --git a/ncdc/src/util.c b/ncdc/src/util.c index 6757d91..2b5e830 100644 --- a/ncdc/src/util.c +++ b/ncdc/src/util.c @@ -6,18 +6,29 @@ bool is_logged_in(void) return dc_session_has_token(current_session); } -wchar_t *util_readkey(int e, WINDOW *win) +wchar_t *util_readkey(int e) { wint_t esc[7] = {0}; int i = 0; + int len = 0; return_if_true(e != KEY_ESCAPE, NULL); esc[0] = e; - for (i = 1; i < 6; i++) { - if (wget_wch(win, esc+i) == ERR) { - return NULL; - } + esc[1] = fgetwc(stdin); + + switch (esc[1]) { + /* here it depends on the next character we read + */ + case L'[': len = 3; break; + /* O codes move the cursor, and have one additional bit set + */ + case L'O': len = 1; break; + default: len = 5; + } + + for (i = 0; i < len; i++) { + esc[i+2] = fgetwc(stdin); } return wcsdup((wchar_t const *)esc);