From 09d2be260b067f49e00f5039ff603701ac1d0043 Mon Sep 17 00:00:00 2001 From: QC Date: Thu, 30 Apr 2015 22:42:04 +0200 Subject: [PATCH] Allow keybindings to be set. --- Makefile.am | 6 +++++- include/keyb.h | 7 +++++++ include/textbox.h | 12 ++++++------ source/keyb.c | 47 ++++++++++++++++++++++++++++++++++++++++----- source/rofi.c | 11 +++++++++-- source/textbox.c | 7 +++---- test/textbox-test.c | 3 +++ 7 files changed, 75 insertions(+), 18 deletions(-) diff --git a/Makefile.am b/Makefile.am index 8e9d5961..6329d9b2 100644 --- a/Makefile.am +++ b/Makefile.am @@ -128,10 +128,14 @@ textbox_test_SOURCES=\ config/config.c\ source/keyb.c\ source/x11-helper.c\ + source/xrmoptions.c\ + source/helper.c\ include/keyb.h\ include/rofi.h\ include/textbox.h\ - source/x11-helper.h\ + include/x11-helper.h\ + include/xrmoptions.h\ + include/helper.h\ test/textbox-test.c helper_test_SOURCES=\ diff --git a/include/keyb.h b/include/keyb.h index fce2b540..1edd0bd6 100644 --- a/include/keyb.h +++ b/include/keyb.h @@ -11,6 +11,9 @@ typedef enum _KeyBindingAction REMOVE_WORD_BACK, REMOVE_WORD_FORWARD, REMOVE_CHAR_FORWARD, + REMOVE_CHAR_BACK, + MOVE_WORD_BACK, + MOVE_WORD_FORWARD, NUM_ABE } KeyBindingAction; @@ -24,13 +27,17 @@ typedef struct _KeyBinding typedef struct _ActionBindingEntry { const char *name; + char *keystr; int num_bindings; KeyBinding *kb; } ActionBindingEntry; +void parse_keys_abe ( void ); void setup_abe ( void ); +void cleanup_abe ( void ); + extern ActionBindingEntry abe[NUM_ABE]; int abe_test_action ( KeyBindingAction action, unsigned int mask, KeySym key ); #endif // __KEYB_H__ diff --git a/include/textbox.h b/include/textbox.h index 6ec1cf7b..1fd2dc33 100644 --- a/include/textbox.h +++ b/include/textbox.h @@ -23,12 +23,12 @@ typedef struct typedef enum { - TB_AUTOHEIGHT = 1 << 0, - TB_AUTOWIDTH = 1 << 1, - TB_LEFT = 1 << 16, - TB_RIGHT = 1 << 17, - TB_CENTER = 1 << 18, - TB_EDITABLE = 1 << 19, + TB_AUTOHEIGHT = 1 << 0, + TB_AUTOWIDTH = 1 << 1, + TB_LEFT = 1 << 16, + TB_RIGHT = 1 << 17, + TB_CENTER = 1 << 18, + TB_EDITABLE = 1 << 19, } TextboxFlags; diff --git a/source/keyb.c b/source/keyb.c index 6108e9ab..cc5bf6e8 100644 --- a/source/keyb.c +++ b/source/keyb.c @@ -1,6 +1,7 @@ #include "rofi.h" #include #include "x11-helper.h" +#include "xrmoptions.h" ActionBindingEntry abe[NUM_ABE]; // Use this so we can ignore numlock mask. @@ -23,10 +24,16 @@ const char *KeyBindingActionName[NUM_ABE] = // REMOVE_WORD_FORWARD "remove-word-forward", // REMOVE_CHAR_FORWARD - "remove-char-forward" + "remove-char-forward", + // REMOVE_CHAR_BACK + "remove-char-back", + // MOVE_WORD_BACK + "move-word-back", + // MOVE_WORD_FORWARD + "move-word-forward", }; -const char *KeyBindingActionDefault[NUM_ABE] = +char *KeyBindingActionDefault[NUM_ABE] = { // PASTE_PRIMARY "Control+Shift+v,Shift+Insert", @@ -44,15 +51,36 @@ const char *KeyBindingActionDefault[NUM_ABE] = "Control+Alt+d", // REMOVE_CHAR_FORWARD "Delete,Control+d", + // REMOVE_CHAR_BACK + "BackSpace,Control+h", + // MOVE_WORD_BACK + "Alt+b", + // MOVE_WORD_FORWARD + "Alt+f", }; void setup_abe ( void ) { for ( int iter = 0; iter < NUM_ABE; iter++ ) { - char *keystr = g_strdup ( KeyBindingActionDefault[iter] ); - char *sp = NULL; // set pointer to name. - abe[iter].name = KeyBindingActionName[iter]; + abe[iter].name = KeyBindingActionName[iter]; + abe[iter].keystr = g_strdup ( KeyBindingActionDefault[iter] ); + abe[iter].num_bindings = 0; + abe[iter].kb = NULL; + + config_parser_add_option ( xrm_String, + abe[iter].name, &( abe[iter].keystr ) ); + } +} + +void parse_keys_abe ( void ) +{ + for ( int iter = 0; iter < NUM_ABE; iter++ ) { + char *keystr = g_strdup ( abe[iter].keystr ); + char *sp = NULL; + + g_free ( abe[iter].kb ); + abe[iter].num_bindings = 0; // Iter over bindings. for ( char *entry = strtok_r ( keystr, ",", &sp ); entry != NULL; entry = strtok_r ( NULL, ",", &sp ) ) { @@ -66,6 +94,15 @@ void setup_abe ( void ) } } +void cleanup_abe ( void ) +{ + for ( int iter = 0; iter < NUM_ABE; iter++ ) { + g_free ( abe[iter].kb ); + abe[iter].kb = NULL; + abe[iter].num_bindings = 0; + } +} + int abe_test_action ( KeyBindingAction action, unsigned int mask, KeySym key ) { ActionBindingEntry *akb = &( abe[action] ); diff --git a/source/rofi.c b/source/rofi.c index 045a3a4a..a4a83479 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -1540,6 +1540,9 @@ static void cleanup () } } g_free ( switchers ); + + // Cleanup the custom keybinding + cleanup_abe (); } /** @@ -1648,6 +1651,7 @@ static void hup_action_handler ( int num ) if ( display ) { load_configuration ( display ); load_configuration_dynamic ( display ); + parse_keys_abe (); XCloseDisplay ( display ); } } @@ -1735,6 +1739,9 @@ int main ( int argc, char *argv[] ) return EXIT_FAILURE; } + // Setup keybinding + setup_abe (); + load_configuration ( display ); if ( !dmenu_mode ) { // setup_switchers @@ -1746,13 +1753,13 @@ int main ( int argc, char *argv[] ) } // Reload for dynamic part. load_configuration_dynamic ( display ); - - setup_abe (); // Dump. if ( find_arg ( "-dump-xresources" ) >= 0 ) { xresource_dump (); exit ( EXIT_SUCCESS ); } + // Parse the keybindings. + parse_keys_abe (); // Set up X interaction. const struct sigaction sigchld_action = { diff --git a/source/textbox.c b/source/textbox.c index 5d035e39..db3b8429 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -568,18 +568,17 @@ int textbox_keypress ( textbox *tb, XEvent *ev ) return 1; } // Alt-B - else if ( ( ev->xkey.state & Mod1Mask ) && key == XK_b ) { + else if ( abe_test_action ( MOVE_WORD_BACK, ev->xkey.state, key ) ) { textbox_cursor_dec_word ( tb ); return 1; } // Alt-F - else if ( ( ev->xkey.state & Mod1Mask ) && key == XK_f ) { + else if ( abe_test_action ( MOVE_WORD_FORWARD, ev->xkey.state, key ) ) { textbox_cursor_inc_word ( tb ); return 1; } // BackSpace, Ctrl-h - else if ( key == XK_BackSpace || - ( ( ev->xkey.state & ControlMask ) && key == XK_h ) ) { + else if ( abe_test_action ( REMOVE_CHAR_BACK, ev->xkey.state, key ) ) { textbox_cursor_bkspc ( tb ); return 1; } diff --git a/test/textbox-test.c b/test/textbox-test.c index 899c7f2e..026f6309 100644 --- a/test/textbox-test.c +++ b/test/textbox-test.c @@ -156,6 +156,9 @@ int main ( G_GNUC_UNUSED int argc, G_GNUC_UNUSED char **argv ) textbox_free(box); textbox_cleanup( ); + + cleanup_abe(); + XDestroyWindow ( display, mw); XCloseDisplay ( display ); }