From 0e472ee74dff60528356f0453fb8f1f05c24afc4 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Fri, 18 Jun 2010 12:58:20 +0200 Subject: [PATCH] Bindings for special keys This commit makes it possible to define and use special keys like on their own or in combination with a modifier: >. In addition the evaluation of the keypress has been changed, so that the order of the bindings do not matter any longer. --- config.def.h | 29 +++++++++++++++++ zathura.c | 88 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 91 insertions(+), 26 deletions(-) diff --git a/config.def.h b/config.def.h index 82ed8b3..230fdf8 100644 --- a/config.def.h +++ b/config.def.h @@ -286,3 +286,32 @@ ModeName mode_names[] = { {"normal", NORMAL}, {"visual", VISUAL}, }; + +/* special keys */ +GDKKey gdk_keys[] = { + {"", GDK_BackSpace}, + {"", GDK_Caps_Lock}, + {"", GDK_Down}, + {"", GDK_Escape}, + {"", GDK_F10}, + {"", GDK_F11}, + {"", GDK_F12}, + {"", GDK_F1}, + {"", GDK_F2}, + {"", GDK_F3}, + {"", GDK_F4}, + {"", GDK_F5}, + {"", GDK_F6}, + {"", GDK_F7}, + {"", GDK_F8}, + {"", GDK_F9}, + {"", GDK_Left}, + {"", GDK_Page_Down}, + {"", GDK_Page_Up}, + {"", GDK_Return}, + {"", GDK_Right}, + {"", GDK_space}, + {"", GDK_Super_L}, + {"", GDK_Tab}, + {"", GDK_Up}, +}; diff --git a/zathura.c b/zathura.c index 40efce2..317f517 100644 --- a/zathura.c +++ b/zathura.c @@ -143,6 +143,12 @@ struct SCList typedef struct SCList ShortcutList; +typedef struct +{ + char* identifier; + int key; +} GDKKey; + typedef struct { PopplerPage *page; @@ -2820,35 +2826,69 @@ cmd_map(int argc, char** argv) /* parse modifier and key */ int mask = 0; int key = 0; + int keyl = strlen(ks); int mode = NORMAL; // single key (e.g.: g) - if(strlen(ks) == 1) + if(keyl == 1) key = ks[0]; // modifier and key (e.g.: - if(strlen(ks) == 5 && ks[0] == '<' && ks[2] == '-' && ks[4] == '>') + // special key or modifier and key/special key (e.g.: , ) + + else if(keyl >= 3 && ks[0] == '<' && ks[keyl-1] == '>') { - /* evaluate modifier */ - switch(ks[1]) + char* specialkey = NULL; + + /* check for modifier */ + if(keyl >= 5 && ks[2] == '-') { - case 'S': - mask = GDK_SHIFT_MASK; - break; - case 'C': - mask = GDK_CONTROL_MASK; + /* evaluate modifier */ + switch(ks[1]) + { + case 'S': + mask = GDK_SHIFT_MASK; + break; + case 'C': + mask = GDK_CONTROL_MASK; + break; + } + + /* no valid modifier */ + if(!mask) + { + notify(WARNING, "No valid modifier given."); + return FALSE; + } + + /* modifier and special key */ + if(keyl > 5) + specialkey = g_strndup(ks + 3, keyl - 4); + else + key = ks[3]; + } + else + specialkey = ks; + + /* search special key */ + int g_c; + for(g_c = 0; specialkey && g_c < LENGTH(gdk_keys); g_c++) + { + if(!strcmp(specialkey, gdk_keys[g_c].identifier)) + { + key = gdk_keys[g_c].key; break; + } } - /* get key */ - key = ks[3]; + if(specialkey) + g_free(specialkey); + } - /* no valid modifier */ - if(!mask) - { - notify(WARNING, "No valid modifier given."); - return FALSE; - } + if(!key) + { + notify(WARNING, "No valid key binding given."); + return FALSE; } /* parse argument */ @@ -2890,12 +2930,6 @@ cmd_map(int argc, char** argv) } } - if(!key) - { - notify(WARNING, "No valid key binding given."); - return FALSE; - } - /* search for existing binding to overwrite it */ ShortcutList* sc = Zathura.Bindings.sclist; while(sc && sc->next != NULL) @@ -3836,9 +3870,11 @@ cb_view_kb_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data) ShortcutList* sc = Zathura.Bindings.sclist; while(sc) { - if (event->keyval == sc->element.key && - (((event->state & sc->element.mask) == sc->element.mask) || sc->element.mask == 0) - && (Zathura.Global.mode == sc->element.mode || sc->element.mode == -1)) + if( event->state == sc->element.mask + && event->keyval == sc->element.key + && (Zathura.Global.mode == sc->element.mode || sc->element.mode == -1) + && sc->element.function + ) { sc->element.function(&(sc->element.argument)); return TRUE;