From ef8d444dd2a715a35cd46d685016c72fbc9ba7fa Mon Sep 17 00:00:00 2001 From: Qball Cow Date: Tue, 21 Jan 2014 10:13:42 +0100 Subject: [PATCH] Add keybinding. --- doc/simpleswitcher.1 | 15 +++++++++++ include/simpleswitcher.h | 4 +-- source/run-dialog.c | 12 +++++---- source/simpleswitcher.c | 57 +++++++++++++++++++++++++++------------- source/ssh-dialog.c | 21 +++++++++------ 5 files changed, 76 insertions(+), 33 deletions(-) diff --git a/doc/simpleswitcher.1 b/doc/simpleswitcher.1 index c65a6ae1..d08bfc6b 100644 --- a/doc/simpleswitcher.1 +++ b/doc/simpleswitcher.1 @@ -23,6 +23,7 @@ simpleswitcher \- a simple EWMH window switcher .IR comdo ] .RB [ \-now ] .RB [ \-rnow ] +.RB [ \-snow ] .RB [ \-term .IR terminal ] @@ -63,12 +64,26 @@ simpleswitcher -rkey control+shift+d simpleswitcher -rkey mod1+grave (grave=backtick) .RE .TP +.B -skey +Change the key combination to display the ssh dialog (default: mod1-F3). +.P +.RS +simpleswitcher -skey F10 +.br +simpleswitcher -skey control+shift+s +.br +simpleswitcher -skey mod1+grave (grave=backtick) +.RE +.TP .B -now Run simpleswitcher in all-windows mode once then exit. Does not bind any keys. .TP .B -rnow Run simpleswitcher in run-dialog mode once then exit. Does not bind any keys. .TP +.B -snow +Run simpleswitcher in ssh mode once then exit. Does not bind any keys. +.TP .B -bg Set the background text color (X11 named color or hex #rrggbb) for the menu (default: #222222). .P diff --git a/include/simpleswitcher.h b/include/simpleswitcher.h index da22b224..391767ca 100644 --- a/include/simpleswitcher.h +++ b/include/simpleswitcher.h @@ -17,9 +17,9 @@ typedef enum { -typedef int (*menu_match_cb)(char **tokens, const char *input, int index, void *data); +typedef int ( *menu_match_cb )( char **tokens, const char *input, int index, void *data ); int menu( char **lines, char **input, char *prompt, - int selected, Time *time, int *shift, menu_match_cb mmc, void *mmc_data); + int selected, Time *time, int *shift, menu_match_cb mmc, void *mmc_data ); /** diff --git a/source/run-dialog.c b/source/run-dialog.c index 5ab45f97..2bc40f79 100644 --- a/source/run-dialog.c +++ b/source/run-dialog.c @@ -226,14 +226,16 @@ static char ** get_apps ( ) } static int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data) + __attribute__( ( unused ) )int index, + __attribute__( ( unused ) )void *data ) { int match = 1; + // Do a tokenized match. if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); - } + match = ( strcasestr( input, tokens[j] ) != NULL ); + } + return match; } @@ -250,7 +252,7 @@ SwitcherMode run_switcher_dialog ( char **input ) } int shift=0; - int n = menu( cmd_list, input, "$ ", 0, NULL, &shift,token_match, NULL); + int n = menu( cmd_list, input, "$ ", 0, NULL, &shift,token_match, NULL ); if ( n == -2 ) { retv = SSH_DIALOG; diff --git a/source/simpleswitcher.c b/source/simpleswitcher.c index 23f48a55..4ed65442 100644 --- a/source/simpleswitcher.c +++ b/source/simpleswitcher.c @@ -718,6 +718,8 @@ unsigned int windows_modmask; KeySym windows_keysym; unsigned int rundialog_modmask; KeySym rundialog_keysym; +unsigned int sshdialog_modmask; +KeySym sshdialog_keysym; // flags to set if we switch modes on the fly Window main_window = None; @@ -779,34 +781,35 @@ static int calculate_common_prefix( char **filtered, int max_lines ) } -int window_match ( char **tokens, __attribute__((unused))const char *input, int index, void *data) +int window_match ( char **tokens, __attribute__( ( unused ) )const char *input, int index, void *data ) { int match =1; - winlist *ids = (winlist *)data; - client *c = window_client(ids->array[index]); + winlist *ids = ( winlist * )data; + client *c = window_client( ids->array[index] ); + if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - int test = 0; + int test = 0; - if ( !test && c->title[0] != '\0' ) - test = ( strcasestr( c->title, tokens[j] ) != NULL ); + if ( !test && c->title[0] != '\0' ) + test = ( strcasestr( c->title, tokens[j] ) != NULL ); - if ( !test && c->class[0] != '\0' ) - test = ( strcasestr( c->class, tokens[j] ) != NULL ); + if ( !test && c->class[0] != '\0' ) + test = ( strcasestr( c->class, tokens[j] ) != NULL ); - if ( !test && c->role[0] != '\0' ) - test = ( strcasestr( c->role, tokens[j] ) != NULL ); + if ( !test && c->role[0] != '\0' ) + test = ( strcasestr( c->role, tokens[j] ) != NULL ); - if ( !test && c->name[0] != '\0' ) - test = ( strcasestr( c->name, tokens[j] ) != NULL ); + if ( !test && c->name[0] != '\0' ) + test = ( strcasestr( c->name, tokens[j] ) != NULL ); - if ( test == 0 ) match = 0; - } + if ( test == 0 ) match = 0; + } return match; -} +} int menu( char **lines, char **input, char *prompt, int selected, Time *time, int *shift, - menu_match_cb mmc, void *mmc_data) + menu_match_cb mmc, void *mmc_data ) { int line = -1, i, j, chosen = 0; workarea mon; @@ -883,7 +886,7 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in // input changed for ( i = 0, j = 0; i < num_lines && j < max_lines; i++ ) { - int match = mmc(tokens,lines[i], i, mmc_data); + int match = mmc( tokens,lines[i], i, mmc_data ); // If each token was matched, add it to list. if ( match ) { @@ -947,7 +950,7 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in // input changed for ( i = 0, j = 0; i < num_lines && j < max_lines; i++ ) { - int match = mmc(tokens,lines[i], i, mmc_data); + int match = mmc( tokens,lines[i], i, mmc_data ); // If each token was matched, add it to list. if ( match ) { @@ -977,6 +980,7 @@ int menu( char **lines, char **input, char *prompt, int selected, Time *time, in // pressing one of the global key bindings closes the switcher. this allows fast closing of the menu if an item is not selected || ( ( windows_modmask == AnyModifier || ev.xkey.state & windows_modmask ) && key == windows_keysym ) || ( ( rundialog_modmask == AnyModifier || ev.xkey.state & rundialog_modmask ) && key == rundialog_keysym ) + || ( ( sshdialog_modmask == AnyModifier || ev.xkey.state & sshdialog_modmask ) && key == sshdialog_keysym ) ) { break; } @@ -1190,6 +1194,11 @@ void handle_keypress( XEvent *ev ) key == rundialog_keysym ) { run_switcher( FORK , RUN_DIALOG ); } + + if ( ( sshdialog_modmask == AnyModifier || ev->xkey.state & sshdialog_modmask ) && + key == sshdialog_keysym ) { + run_switcher( FORK , SSH_DIALOG ); + } } // convert a Mod+key arg to mod mask and keysym @@ -1362,6 +1371,16 @@ int main( int argc, char *argv[] ) if ( i3_socket_path != NULL ) free( i3_socket_path ); +#endif + exit( EXIT_SUCCESS ); + } + + if ( find_arg( ac, av, "-snow" ) >= 0 ) { + run_switcher( NOFORK, SSH_DIALOG ); +#ifdef I3 + + if ( i3_socket_path != NULL ) free( i3_socket_path ); + #endif exit( EXIT_SUCCESS ); } @@ -1371,11 +1390,13 @@ int main( int argc, char *argv[] ) // key combination to display all windows from all desktops parse_key( find_arg_str( ac, av, "-key", "F12" ), &windows_modmask, &windows_keysym ); parse_key( find_arg_str( ac, av, "-rkey", "mod1+F2" ), &rundialog_modmask, &rundialog_keysym ); + parse_key( find_arg_str( ac, av, "-skey", "mod1+F3" ), &sshdialog_modmask, &sshdialog_keysym ); // bind key combos grab_key( windows_modmask, windows_keysym ); grab_key( rundialog_modmask, rundialog_keysym ); + grab_key( sshdialog_modmask, sshdialog_keysym ); XEvent ev; diff --git a/source/ssh-dialog.c b/source/ssh-dialog.c index bba2437d..eb6433ea 100644 --- a/source/ssh-dialog.c +++ b/source/ssh-dialog.c @@ -176,11 +176,14 @@ static char ** get_ssh ( ) if ( fd != NULL ) { while ( fgets( buffer,1024,fd ) != NULL ) { - if(strncasecmp(buffer, "Host", 4) == 0) { + if ( strncasecmp( buffer, "Host", 4 ) == 0 ) { int start = 0, stop=0; buffer[strlen( buffer )-1] = '\0'; - for(start=4; isspace(buffer[start]);start++); - for(stop=start; isalnum(buffer[stop]);stop++); + + for ( start=4; isspace( buffer[start] ); start++ ); + + for ( stop=start; isalnum( buffer[stop] ); stop++ ); + retv = realloc( retv, ( index+2 )*sizeof( char* ) ); retv[index] = strndup( &buffer[start], stop-start ); retv[index+1] = NULL; @@ -209,14 +212,16 @@ static char ** get_ssh ( ) } static int token_match ( char **tokens, const char *input, - __attribute__( ( unused ) )int index, - __attribute__( ( unused ) )void *data) + __attribute__( ( unused ) )int index, + __attribute__( ( unused ) )void *data ) { int match = 1; + // Do a tokenized match. if ( tokens ) for ( int j = 1; match && tokens[j]; j++ ) { - match = ( strcasestr( input, tokens[j] ) != NULL ); - } + match = ( strcasestr( input, tokens[j] ) != NULL ); + } + return match; } @@ -233,7 +238,7 @@ SwitcherMode ssh_switcher_dialog ( char **input ) } int shift=0; - int n = menu( cmd_list, input, "ssh ", 0, NULL, &shift,token_match, NULL); + int n = menu( cmd_list, input, "ssh ", 0, NULL, &shift,token_match, NULL ); if ( n == -2 ) { retv = WINDOW_SWITCHER;