Added function to delete previous set keybindings.
authorDaniel Carl <danielcarl@gmx.de>
Tue, 16 Oct 2012 19:48:18 +0000 (21:48 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Tue, 16 Oct 2012 20:48:27 +0000 (22:48 +0200)
src/keybind.c
src/keybind.h
src/main.c

index 2d02485..270d66b 100644 (file)
@@ -5,6 +5,7 @@
 static GSList* keys = NULL;
 static GString* modkeys = NULL;
 
+static GSList* keybind_find(int mode, guint modkey, guint modmask, guint keyval);
 static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* event);
 
 
@@ -16,7 +17,7 @@ void keybind_init(void)
 
 void keybind_add(int mode, guint modkey, guint modmask, guint keyval, const gchar* command)
 {
-    struct _keybind_key* keybind = g_new0(struct _keybind_key, 1);
+    Keybind* keybind = g_new0(Keybind, 1);
 
     keybind->mode    = mode;
     keybind->modkey  = modkey;
@@ -32,9 +33,34 @@ void keybind_add(int mode, guint modkey, guint modmask, guint keyval, const gcha
     }
 }
 
+void keybind_remove(int mode, guint modkey, guint modmask, guint keyval)
+{
+    GSList* link = keybind_find(mode, modkey, modmask, keyval);
+    if (link) {
+        keys = g_slist_delete_link(keys, link);
+    }
+    /* TODO remove eventually no more used modkeys */
+}
+
+GSList* keybind_find(int mode, guint modkey, guint modmask, guint keyval)
+{
+    GSList* link;
+    for (link = keys; link != NULL; link = link->next) {
+        Keybind* keybind = (Keybind*)link->data;
+
+        if (keybind->mode == mode
+                && keybind->modmask == modmask
+                && keybind->modkey == modkey
+                && keybind->keyval == keyval) {
+            return link;
+        }
+    }
+
+    return NULL;
+}
+
 static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* event)
 {
-    GSList* tmp;
     GdkModifierType irrelevant;
     guint keyval;
     static GdkKeymap *keymap;
@@ -73,25 +99,20 @@ static gboolean keybind_keypress_callback(WebKitWebView* webview, GdkEventKey* e
             }
         }
     }
-    /* TODO move to own function */
+
     /* check for keybinding */
-    for (tmp = keys; tmp != NULL; tmp = tmp->next) {
-        struct _keybind_key* keybind = (struct _keybind_key*)tmp->data;
+    GSList* link = keybind_find(vp.state.mode, vp.state.modkey,
+            (CLEAN(event->state) & ~irrelevant), keyval);
 
-        /* handle key presses */
-        if (keybind->mode == vp.state.mode
-                && keybind->modmask == (CLEAN(event->state) & ~irrelevant)
-                && keybind->modkey == vp.state.modkey
-                && keybind->keyval == keyval
-                && keybind->command) {
-            command_run(keybind->command);
+    if (link) {
+        Keybind* keybind = (Keybind*)link->data;
+        command_run(keybind->command);
 
-            /* if key binding used, remove the modkey */
-            vp.state.modkey = vp.state.count = 0;
-            vp_update_statusbar();
+        /* if key binding used, remove the modkey */
+        vp.state.modkey = vp.state.count = 0;
+        vp_update_statusbar();
 
-            return TRUE;
-        }
+        return TRUE;
     }
 
     return FALSE;
index 1be98de..6866dab 100644 (file)
 #define IS_ESCAPE(event) (IS_ESCAPE_KEY(CLEAN(event->state), event->keyval))
 #define IS_ESCAPE_KEY(s, k) ((s == 0 && k == GDK_Escape) || (s == GDK_CONTROL_MASK && k == GDK_c))
 
-struct _keybind_key {
+typedef struct {
     int    mode;        /* mode maks for allowed browser modes */
     guint  modkey;
     guint  modmask;     /* modemask for the kayval */
     guint  keyval;
     gchar* command;     /* command to run */
-};
+} Keybind;
 
 void keybind_init(void);
 void keybind_add(int mode, guint modkey, guint modmask, guint keyval, const gchar* command);
+void keybind_remove(int mode, guint modkey, guint modmask, guint keyval);
 
 #endif /* end of include guard: KEYBIND_H */
index 2116c1f..9b05ee1 100644 (file)
@@ -136,8 +136,8 @@ gboolean vp_load_uri(const Arg* arg)
     g_free(u);
 
     /* change state to normal mode */
-    vp.state.mode = VP_MODE_NORMAL;
-    vp_update_statusbar();
+    Arg a = {VP_MODE_NORMAL};
+    vp_set_mode(&a);
 
     return TRUE;
 }
@@ -324,6 +324,7 @@ static void vp_init(void)
     /* initialize the keybindings */
     keybind_init();
 
+    /* TODO read the key bindings from config file */
     keybind_add(VP_MODE_NORMAL, GDK_g, 0,                GDK_f,      "source");
     keybind_add(VP_MODE_NORMAL, 0,     0,                GDK_colon,  "input");
     keybind_add(VP_MODE_NORMAL, 0,     0,                GDK_d,      "quit");