]> git.mdlowis.com Git - archive/afm.git/commitdiff
Input handling works mostly
authorMichael D. Lowis <mike.lowis@gentex.com>
Fri, 25 Jul 2014 14:09:11 +0000 (10:09 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Fri, 25 Jul 2014 14:09:11 +0000 (10:09 -0400)
source/input.c
source/input.h
source/main.c

index 520cbe36d1f2b2f1678af7436f0c70d9c5bfbf5b..5fa9fe7f0c92ad59661c9b05d39cf5490349ef8e 100644 (file)
@@ -3,9 +3,7 @@
 #include "workdir.h"
 #include "screen.h"
 #include <stdlib.h>
-
-static vec_t* States;
-static vec_t* Curr_State;
+#include <string.h>
 
 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';
+        }
     }
 }
 
index 530215c1dd612b8df917cc9e721f2fc797152919..75c96ab658ac38aac24a8a7d2cef3cf3a8da259b 100644 (file)
@@ -7,8 +7,6 @@
 #ifndef INPUT_H
 #define INPUT_H
 
-void input_init(void);
-
 void input_handle_key(char ch);
 
 #endif /* INPUT_H */
index 4bb3dcdf83e33ccfada3f0de72114d50979db052..665f9a713dc24ef9f2c1fffa45a0a0bedb12013d 100644 (file)
@@ -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;
 }