Fix #76: DMENU returns 1 on cancel.

* Pull out dmenu, separate it from normal modi.
    * Give dmenu a return value (1 on cancel)
This commit is contained in:
Dave Davenport 2014-08-05 09:07:41 +02:00
parent da69111a20
commit b196649f10
4 changed files with 36 additions and 17 deletions

View file

@ -2,6 +2,9 @@
#define __DMENU_DIALOG_H__
extern char *dmenu_prompt;
SwitcherMode dmenu_switcher_dialog ( char **input, void *data );
/**
* Returns TRUE when success, FALSE when canceled.
*/
int dmenu_switcher_dialog ( char **input );
#endif

View file

@ -25,8 +25,6 @@ extern const char *cache_dir;
*/
typedef enum
{
/** Dmenu mode */
DMENU_DIALOG = 999,
/** Exit. */
MODE_EXIT = 1000,
/** Skip to the next cycle-able dialog. */

View file

@ -65,10 +65,10 @@ static char **get_dmenu ( unsigned int *length )
return retv;
}
SwitcherMode dmenu_switcher_dialog ( char **input, void *data )
int dmenu_switcher_dialog ( char **input )
{
int selected_line = 0;
SwitcherMode retv = MODE_EXIT;
int retv = FALSE;
unsigned int length = 0;
char **list = get_dmenu ( &length );
int restart = FALSE;
@ -80,10 +80,7 @@ SwitcherMode dmenu_switcher_dialog ( char **input, void *data )
// We normally do not want to restart the loop.
restart = FALSE;
if ( mretv == MENU_NEXT ) {
retv = RELOAD_DIALOG;
}
else if ( mretv == MENU_OK && list[selected_line] != NULL ) {
if ( mretv == MENU_OK && list[selected_line] != NULL ) {
fputs ( list[selected_line], stdout );
fputc ( '\n', stdout );
fflush ( stdout );
@ -92,6 +89,7 @@ SwitcherMode dmenu_switcher_dialog ( char **input, void *data )
// Move to next line.
selected_line = MIN ( selected_line + 1, length - 1 );
}
retv = TRUE;
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
fputs ( *input, stdout );
@ -102,6 +100,7 @@ SwitcherMode dmenu_switcher_dialog ( char **input, void *data )
// Move to next line.
selected_line = MIN ( selected_line + 1, length - 1 );
}
retv = TRUE;
}
} while ( restart );
for ( unsigned int i = 0; i < length; i++ ) {

View file

@ -1711,6 +1711,25 @@ SwitcherMode run_switcher_window ( char **input, void *data )
return retv;
}
static int run_dmenu ()
{
int ret_state = TRUE;
textbox_setup (
config.menu_bg, config.menu_fg,
config.menu_hlbg,
config.menu_hlfg );
char *input = NULL;
// Dmenu modi has a return state.
ret_state = dmenu_switcher_dialog ( &input );
free ( input );
// Cleanup font setup.
textbox_cleanup ();
return ret_state;
}
static void run_switcher ( int do_fork, SwitcherMode mode )
{
// we fork because it's technically possible to have multiple window
@ -1720,7 +1739,7 @@ static void run_switcher ( int do_fork, SwitcherMode mode )
// strangeness...
if ( do_fork == TRUE ) {
if ( fork () ) {
return;
return ;
}
display = XOpenDisplay ( 0 );
@ -1733,12 +1752,8 @@ static void run_switcher ( int do_fork, SwitcherMode mode )
config.menu_hlbg,
config.menu_hlfg );
char *input = NULL;
// Dmenu is a special mode. You can cycle away from it.
if ( mode == DMENU_DIALOG ) {
dmenu_switcher_dialog ( &input, NULL );
}
// Otherwise check if requested mode is enabled.
else if ( switchers[mode].cb != NULL ) {
if ( switchers[mode].cb != NULL ) {
do {
SwitcherMode retv = MODE_EXIT;
@ -1751,7 +1766,7 @@ static void run_switcher ( int do_fork, SwitcherMode mode )
else if ( retv == RELOAD_DIALOG ) {
// do nothing.
}
else if ( retv < DMENU_DIALOG ) {
else if ( retv < MODE_EXIT ) {
mode = ( retv ) % num_switchers;
}
else {
@ -2216,7 +2231,11 @@ int main ( int argc, char *argv[] )
}
else if ( find_arg ( argc, argv, "-dmenu" ) >= 0 ) {
find_arg_str ( argc, argv, "-p", &dmenu_prompt );
run_switcher ( FALSE, DMENU_DIALOG );
int retv = run_dmenu();
// User cancelled the operation.
if(retv == FALSE) {
return EXIT_FAILURE;
}
}
else{
// Daemon mode, Listen to key presses..