From 083e6c7fa6c9ba7ed6318a6e9f1d11a033031f96 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Fri, 25 Jul 2014 10:09:11 -0400 Subject: [PATCH] Input handling works mostly --- source/input.c | 87 +++++++++++++++++++++++++++++--------------------- source/input.h | 2 -- source/main.c | 7 ++-- 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/source/input.c b/source/input.c index 520cbe3..5fa9fe7 100644 --- a/source/input.c +++ b/source/input.c @@ -3,9 +3,7 @@ #include "workdir.h" #include "screen.h" #include - -static vec_t* States; -static vec_t* Curr_State; +#include typedef void (*key_cb_t)(void); @@ -34,51 +32,68 @@ static void handle_cd(void) { workdir_cd(state_get_focused_frame()->workdir); } -binding_t Default_Bindings[] = { +static binding_t Default_Bindings[] = { { "a", &handle_aardvark }, { "q", &handle_quit }, { "j", &handle_next }, { "k", &handle_prev }, { "\n", &handle_cd }, { "wn", &screen_open }, - { "wc", &screen_open }, + { "wc", &screen_close }, + //{ "wj", NULL }, + //{ "wk", NULL }, }; -void input_init(void) { +static char Key_Buffer[16] = {0}; + +void input_handle_key(char ch) { + bool more_matches = false; + bool match_found = false; size_t num_entries = (sizeof(Default_Bindings) / sizeof(binding_t)); - for(int i = 0; i < num_entries; i++) { - input_add_binding(Default_Bindings[i].sequence, Default_Bindings[i].callback); + int len = strlen(Key_Buffer); + + /* If no more room then reset the buffer */ + if (len+1 >= 16) { + beep(); + len = 0; + Key_Buffer[0] = '\0'; } - exit(0); -} -void input_handle_key(char ch) { - ///* Assume screen is dirty by default */ - //bool is_screen_dirty = true; - //switch(ch){ - // case 'a': state_set_aardvark_mode(!state_get_aardvark_mode()); - // break; - // case 'q': state_set_running(false); - // break; - // case 'j': workdir_next(state_get_focused_frame()->workdir); break; - // case 'k': workdir_prev(state_get_focused_frame()->workdir); break; - // case 'e': workdir_cd(state_get_focused_frame()->workdir); break; - // case 'n': screen_open(); - // break; - // case 'c': screen_close(); - // break; - // default: is_screen_dirty = false; - // break; - //} - ///* Update the screen if we need to */ - //state_set_screen_dirty(state_get_screen_dirty() || is_screen_dirty); -} + /* If we got a valid key then process it */ + if(ERR != ch) { + /* Put the key in the buffer */ + len++; + Key_Buffer[len-1] = ch; + Key_Buffer[len] = '\0'; + + /* Loop over the bindings */ + for(int i = 0; i < num_entries; i++) { + binding_t binding = Default_Bindings[i]; + char* seq = binding.sequence; + + /* If the binding we're looking at matches a substring but has more chars + * make note of it so we can wait for the next char */ + if((strlen(seq) > len) && (0 == strncmp(seq, Key_Buffer, len))) + { + more_matches = true; + } + /* If the current string matches exactly then execute it's handler */ + else if (0 == strcmp(Key_Buffer, seq)) + { + binding.callback(); + Key_Buffer[0] = '\0'; + match_found = true; + break; + } + } -void input_add_binding(char* seq, key_cb_t p_cb_fn) { - vec_t* p_state = States; - size_t len = strlen(seq); - for(int i = 0; i < len; i++) { - key_t* p_key = input_lookup_or_add(p_state, seq[i]); + /* If we did not find a match and we don't have any possibility of + * finding a longer match, then throw out the buffer and start over */ + if(!match_found && !more_matches) { + beep(); + len = 0; + Key_Buffer[0] = '\0'; + } } } diff --git a/source/input.h b/source/input.h index 530215c..75c96ab 100644 --- a/source/input.h +++ b/source/input.h @@ -7,8 +7,6 @@ #ifndef INPUT_H #define INPUT_H -void input_init(void); - void input_handle_key(char ch); #endif /* INPUT_H */ diff --git a/source/main.c b/source/main.c index 4bb3dcd..665f9a7 100644 --- a/source/main.c +++ b/source/main.c @@ -28,16 +28,15 @@ int main(int argc, char** argv) { noecho(); timeout(25); refresh(); - //screen_init(); - input_init(); + screen_init(); while(state_get_running()) { - //if(state_get_screen_dirty()) screen_update(); + if(state_get_screen_dirty()) screen_update(); input_handle_key(getch()); } erase(); refresh(); endwin(); - //screen_deinit(); + screen_deinit(); return 0; } -- 2.54.0