From 4eb3fd1da33e91ad76efe1508702de50daf0df34 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Fri, 19 Feb 2016 19:29:06 +0100 Subject: Test for single mainloop --- source/dialogs/dmenu.c | 255 ++++++++++++++++++++++++--------------------- source/rofi.c | 277 ++++++++++++++++++++++++++----------------------- source/textbox.c | 4 +- source/view.c | 61 ++++++++--- source/x11-helper.c | 2 +- 5 files changed, 334 insertions(+), 265 deletions(-) (limited to 'source') diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index d76b0816..1c584e53 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -68,6 +68,7 @@ typedef struct _DmenuModePrivateData // List with entries. char **cmd_list; unsigned int cmd_list_length; + unsigned int only_selected; } DmenuModePrivateData; static char **get_dmenu ( FILE *fd, unsigned int *length ) @@ -358,23 +359,151 @@ Mode dmenu_mode = .free = NULL }; +static void dmenu_finalize ( RofiViewState *state ) +{ + int retv = FALSE; + DmenuModePrivateData *pd = (DmenuModePrivateData *) (Mode *) ( rofi_view_get_mode ( state ) )->private_data; + unsigned int cmd_list_length = pd->cmd_list_length; + char **cmd_list = pd->cmd_list; + + char *input = g_strdup ( rofi_view_get_user_input ( state ) ); + pd->selected_line = rofi_view_get_selected_line ( state );; + MenuReturn mretv = rofi_view_get_return_value ( state ); + unsigned int next_pos = rofi_view_get_next_position ( state ); + + int restart = 0; + // Special behavior. + // TODO clean this up! + if ( pd->only_selected ) { + /** + * Select item mode. + */ + restart = 1; + // Skip if no valid item is selected. + if ( ( mretv & MENU_CANCEL ) == MENU_CANCEL ) { + // In no custom mode we allow canceling. + restart = ( find_arg ( "-only-match" ) >= 0 ); + } + else if ( pd->selected_line != UINT32_MAX ) { + if ( ( mretv & ( MENU_OK | MENU_QUICK_SWITCH ) ) && cmd_list[pd->selected_line] != NULL ) { + dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input ); + retv = TRUE; + if ( ( mretv & MENU_QUICK_SWITCH ) ) { + retv = 10 + ( mretv & MENU_LOWER_MASK ); + } + rofi_view_free ( state ); + g_free ( input ); + mode_destroy ( &dmenu_mode ); + if ( retv == FALSE ) { + rofi_set_return_code ( EXIT_FAILURE ); + } + else if ( retv >= 10 ) { + rofi_set_return_code ( retv ); + } + else{ + rofi_set_return_code ( EXIT_SUCCESS ); + } + rofi_view_free ( state ); + mode_destroy ( &dmenu_mode ); + rofi_view_set_active ( NULL ); +// g_main_loop_quit(NULL); + return; + } + pd->selected_line = next_pos - 1; + } + // Restart + rofi_view_restart ( state ); + rofi_view_set_selected_line ( state, pd->selected_line ); + return; + } + // We normally do not want to restart the loop. + restart = FALSE; + // Normal mode + if ( ( mretv & MENU_OK ) && pd->selected_line != UINT32_MAX && cmd_list[pd->selected_line] != NULL ) { + dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input ); + if ( ( mretv & MENU_SHIFT ) ) { + restart = TRUE; + int seen = FALSE; + if ( pd->selected_list != NULL ) { + if ( pd->selected_list[pd->num_selected_list - 1].stop == ( pd->selected_line - 1 ) ) { + pd->selected_list[pd->num_selected_list - 1].stop = pd->selected_line; + seen = TRUE; + } + } + if ( !seen ) { + pd->selected_list = g_realloc ( pd->selected_list, + ( pd->num_selected_list + 1 ) * sizeof ( struct range_pair ) ); + pd->selected_list[pd->num_selected_list].start = pd->selected_line; + pd->selected_list[pd->num_selected_list].stop = pd->selected_line; + ( pd->num_selected_list )++; + } + + // Move to next line. + pd->selected_line = MIN ( next_pos, cmd_list_length - 1 ); + } + retv = TRUE; + } + // Custom input + else if ( ( mretv & ( MENU_CUSTOM_INPUT ) ) ) { + dmenu_output_formatted_line ( pd->format, input, -1, input ); + if ( ( mretv & MENU_SHIFT ) ) { + restart = TRUE; + // Move to next line. + pd->selected_line = MIN ( next_pos, cmd_list_length - 1 ); + } + + retv = TRUE; + } + // Quick switch with entry selected. + else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line < UINT32_MAX ) { + dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input ); + + restart = FALSE; + retv = 10 + ( mretv & MENU_LOWER_MASK ); + } + // Quick switch without entry selected. + else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line == UINT32_MAX ) { + dmenu_output_formatted_line ( pd->format, input, -1, input ); + + restart = FALSE; + retv = 10 + ( mretv & MENU_LOWER_MASK ); + } + g_free ( input ); + if ( restart ) { + rofi_view_restart ( state ); + rofi_view_set_selected_line ( state, pd->selected_line ); + } + else { + if ( retv == FALSE ) { + rofi_set_return_code ( EXIT_FAILURE ); + } + else if ( retv >= 10 ) { + rofi_set_return_code ( retv ); + } + else{ + rofi_set_return_code ( EXIT_SUCCESS ); + } + rofi_view_free ( state ); + mode_destroy ( &dmenu_mode ); + rofi_view_set_active ( NULL ); + } +} + int dmenu_switcher_dialog ( void ) { mode_init ( &dmenu_mode ); MenuFlags menu_flags = MENU_NORMAL; DmenuModePrivateData *pd = (DmenuModePrivateData *) dmenu_mode.private_data; char *input = NULL; - int retv = FALSE; - int restart = FALSE; unsigned int cmd_list_length = pd->cmd_list_length; char **cmd_list = pd->cmd_list; - int only_selected = FALSE; + pd->only_selected = FALSE; if ( find_arg ( "-markup-rows" ) >= 0 ) { pd->do_markup = TRUE; } if ( find_arg ( "-only-match" ) >= 0 || find_arg ( "-no-custom" ) >= 0 ) { - only_selected = TRUE; + pd->only_selected = TRUE; if ( cmd_list_length == 0 ) { return TRUE; } @@ -416,122 +545,12 @@ int dmenu_switcher_dialog ( void ) g_strfreev ( tokens ); return TRUE; } - - RofiViewState *state = rofi_view_create ( &dmenu_mode, input, pd->prompt, pd->message, menu_flags ); + // TODO remove + RofiViewState *state = rofi_view_create ( &dmenu_mode, input, pd->prompt, pd->message, menu_flags, dmenu_finalize ); rofi_view_set_selected_line ( state, pd->selected_line ); - while ( XPending ( display ) ) { - XEvent ev; - XNextEvent ( display, &ev ); - rofi_view_itterrate ( state, &ev ); - } - do { - retv = FALSE; - - rofi_view_set_active ( state ); - // Enter main loop. - while ( !rofi_view_get_completed ( state ) ) { - g_main_context_iteration ( NULL, TRUE ); - } - rofi_view_set_active ( NULL ); - g_free ( input ); - input = g_strdup ( rofi_view_get_user_input ( state ) ); - pd->selected_line = rofi_view_get_selected_line ( state );; - MenuReturn mretv = rofi_view_get_return_value ( state ); - unsigned int next_pos = rofi_view_get_next_position ( state ); - - // Special behavior. - // TODO clean this up! - if ( only_selected ) { - /** - * Select item mode. - */ - restart = 1; - // Skip if no valid item is selected. - if ( ( mretv & MENU_CANCEL ) == MENU_CANCEL ) { - // In no custom mode we allow canceling. - restart = ( find_arg ( "-only-match" ) >= 0 ); - } - else if ( pd->selected_line != UINT32_MAX ) { - if ( ( mretv & ( MENU_OK | MENU_QUICK_SWITCH ) ) && cmd_list[pd->selected_line] != NULL ) { - dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input ); - retv = TRUE; - if ( ( mretv & MENU_QUICK_SWITCH ) ) { - retv = 10 + ( mretv & MENU_LOWER_MASK ); - } - rofi_view_free ( state ); - g_free ( input ); - mode_destroy ( &dmenu_mode ); - return retv; - } - pd->selected_line = next_pos - 1; - } - // Restart - rofi_view_restart ( state ); - rofi_view_set_selected_line ( state, pd->selected_line ); - continue; - } - // We normally do not want to restart the loop. - restart = FALSE; - // Normal mode - if ( ( mretv & MENU_OK ) && pd->selected_line != UINT32_MAX && cmd_list[pd->selected_line] != NULL ) { - dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input ); - if ( ( mretv & MENU_SHIFT ) ) { - restart = TRUE; - int seen = FALSE; - if ( pd->selected_list != NULL ) { - if ( pd->selected_list[pd->num_selected_list - 1].stop == ( pd->selected_line - 1 ) ) { - pd->selected_list[pd->num_selected_list - 1].stop = pd->selected_line; - seen = TRUE; - } - } - if ( !seen ) { - pd->selected_list = g_realloc ( pd->selected_list, - ( pd->num_selected_list + 1 ) * sizeof ( struct range_pair ) ); - pd->selected_list[pd->num_selected_list].start = pd->selected_line; - pd->selected_list[pd->num_selected_list].stop = pd->selected_line; - ( pd->num_selected_list )++; - } - - // Move to next line. - pd->selected_line = MIN ( next_pos, cmd_list_length - 1 ); - } - retv = TRUE; - } - // Custom input - else if ( ( mretv & ( MENU_CUSTOM_INPUT ) ) ) { - dmenu_output_formatted_line ( pd->format, input, -1, input ); - if ( ( mretv & MENU_SHIFT ) ) { - restart = TRUE; - // Move to next line. - pd->selected_line = MIN ( next_pos, cmd_list_length - 1 ); - } - - retv = TRUE; - } - // Quick switch with entry selected. - else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line < UINT32_MAX ) { - dmenu_output_formatted_line ( pd->format, cmd_list[pd->selected_line], pd->selected_line, input ); + rofi_view_set_active ( state ); - restart = FALSE; - retv = 10 + ( mretv & MENU_LOWER_MASK ); - } - // Quick switch without entry selected. - else if ( ( mretv & MENU_QUICK_SWITCH ) && pd->selected_line == UINT32_MAX ) { - dmenu_output_formatted_line ( pd->format, input, -1, input ); - - restart = FALSE; - retv = 10 + ( mretv & MENU_LOWER_MASK ); - } - if ( restart ) { - rofi_view_restart ( state ); - rofi_view_set_selected_line ( state, pd->selected_line ); - } - } while ( restart ); - - rofi_view_free ( state ); - g_free ( input ); - mode_destroy ( &dmenu_mode ); - return retv; + return FALSE; } void print_dmenu_options ( void ) diff --git a/source/rofi.c b/source/rofi.c index 95e33c6c..7f1ce608 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -80,9 +80,19 @@ GMainLoop *main_loop = NULL; GSource *main_loop_source = NULL; gboolean quiet = FALSE; -static void process_result ( RofiViewState *state ); +static int dmenu_mode = FALSE; + +int return_code = EXIT_SUCCESS; + +void process_result ( RofiViewState *state ); +void process_result_error ( RofiViewState *state ); gboolean main_loop_x11_event_handler ( G_GNUC_UNUSED gpointer data ); +void rofi_set_return_code ( int code ) +{ + return_code = code; +} + unsigned int rofi_get_num_enabled_modi ( void ) { return num_modi; @@ -155,31 +165,28 @@ static void teardown ( int pfd ) remove_pid_file ( pfd ); } +static int pfd = -1; /** * Start dmenu mode. */ static int run_dmenu () { int ret_state = EXIT_FAILURE; - int pfd = setup (); + pfd = setup (); if ( pfd < 0 ) { return ret_state; } // Dmenu modi has a return state. ret_state = dmenu_switcher_dialog (); - teardown ( pfd ); return ret_state; } -static int pfd = -1; - static void __run_switcher_internal ( ModeMode mode, char *input ) { char *prompt = g_strdup_printf ( "%s:", mode_get_name ( modi[mode] ) ); curr_switcher = mode; - RofiViewState * state = rofi_view_create ( modi[mode], input, prompt, NULL, MENU_NORMAL ); - state->finalize = process_result; + RofiViewState * state = rofi_view_create ( modi[mode], input, prompt, NULL, MENU_NORMAL, process_result ); rofi_view_set_active ( state ); g_free ( prompt ); } @@ -201,57 +208,61 @@ static void run_switcher ( ModeMode mode ) __run_switcher_internal ( mode, input ); g_free ( input ); } -static void process_result ( RofiViewState *state ) +void process_result ( RofiViewState *state ) { - Mode *sw = state->sw; - unsigned int selected_line = rofi_view_get_selected_line ( state );; - MenuReturn mretv = rofi_view_get_return_value ( state ); - char *input = g_strdup ( rofi_view_get_user_input ( state ) ); - rofi_view_set_active ( NULL ); - rofi_view_free ( state ); - ModeMode retv = mode_result ( sw, mretv, &input, selected_line ); - - ModeMode mode = curr_switcher; - // Find next enabled - if ( retv == NEXT_DIALOG ) { - mode = ( mode + 1 ) % num_modi; - } - else if ( retv == PREVIOUS_DIALOG ) { - if ( mode == 0 ) { - mode = num_modi - 1; + Mode *sw = state->sw; + if ( sw != NULL ) { + unsigned int selected_line = rofi_view_get_selected_line ( state );; + MenuReturn mretv = rofi_view_get_return_value ( state ); + char *input = g_strdup ( rofi_view_get_user_input ( state ) ); + rofi_view_set_active ( NULL ); + rofi_view_free ( state ); + ModeMode retv = mode_result ( sw, mretv, &input, selected_line ); + + ModeMode mode = curr_switcher; + // Find next enabled + if ( retv == NEXT_DIALOG ) { + mode = ( mode + 1 ) % num_modi; + } + else if ( retv == PREVIOUS_DIALOG ) { + if ( mode == 0 ) { + mode = num_modi - 1; + } + else { + mode = ( mode - 1 ) % num_modi; + } + } + else if ( retv == RELOAD_DIALOG ) { + // do nothing. + } + else if ( retv < MODE_EXIT ) { + mode = ( retv ) % num_modi; } else { - mode = ( mode - 1 ) % num_modi; + mode = retv; } - } - else if ( retv == RELOAD_DIALOG ) { - // do nothing. - } - else if ( retv < MODE_EXIT ) { - mode = ( retv ) % num_modi; - } - else { - mode = retv; - } - if ( mode != MODE_EXIT ) { - /** - * Load in the new mode. - */ - __run_switcher_internal ( mode, input ); + if ( mode != MODE_EXIT ) { + /** + * Load in the new mode. + */ + __run_switcher_internal ( mode, input ); + g_free ( input ); + main_loop_x11_event_handler ( NULL ); + return; + } + // Cleanup g_free ( input ); - main_loop_x11_event_handler ( NULL ); - return; - } - // Cleanup - g_free ( input ); - for ( unsigned int i = 0; i < num_modi; i++ ) { - mode_destroy ( modi[i] ); + for ( unsigned int i = 0; i < num_modi; i++ ) { + mode_destroy ( modi[i] ); + } } - // cleanup +} +void process_result_error ( RofiViewState *state ) +{ + rofi_view_set_active ( NULL ); + rofi_view_free ( state ); teardown ( pfd ); - if ( !daemon_mode ) { - g_main_loop_quit ( main_loop ); - } + g_main_loop_quit ( main_loop ); } int show_error_message ( const char *msg, int markup ) @@ -261,9 +272,9 @@ int show_error_message ( const char *msg, int markup ) return EXIT_FAILURE; } rofi_view_error_dialog ( msg, markup ); - teardown ( pfd ); + //teardown ( pfd ); // TODO this looks incorrect. - g_main_loop_quit ( main_loop ); + // g_main_loop_quit ( main_loop ); return EXIT_SUCCESS; } @@ -552,6 +563,13 @@ gboolean main_loop_x11_event_handler ( G_GNUC_UNUSED gpointer data ) if ( rofi_view_get_completed ( state ) ) { // This menu is done. rofi_view_finalize ( state ); + // cleanup + if ( rofi_view_get_active () == NULL ) { + teardown ( pfd ); + if ( !daemon_mode ) { + g_main_loop_quit ( main_loop ); + } + } } return G_SOURCE_CONTINUE; } @@ -635,6 +653,82 @@ static gboolean delayed_start ( G_GNUC_UNUSED gpointer data ) return FALSE; } +static gboolean startup ( G_GNUC_UNUSED gpointer data ) +{ + // flags to run immediately and exit + char *sname = NULL; + char *msg = NULL; + // Dmenu mode. + if ( dmenu_mode == TRUE ) { + // force off sidebar mode: + config.sidebar_mode = FALSE; + int retv = run_dmenu (); + if ( retv ) { + rofi_set_return_code ( EXIT_SUCCESS ); + // Directly exit. + g_main_loop_quit(main_loop); + } + } + else if ( find_arg_str ( "-e", &( msg ) ) ) { + int markup = FALSE; + if ( find_arg ( "-markup" ) >= 0 ) { + markup = TRUE; + } + show_error_message ( msg, markup ); + } + else if ( find_arg_str ( "-show", &sname ) == TRUE ) { + int index = switcher_get ( sname ); + if ( index < 0 ) { + // Add it to the list + index = add_mode ( sname ); + // Complain + if ( index >= 0 ) { + fprintf ( stdout, "Mode %s not enabled. Please add it to the list of enabled modi: %s\n", + sname, config.modi ); + fprintf ( stdout, "Adding mode: %s\n", sname ); + } + // Run it anyway if found. + } + if ( index >= 0 ) { + run_switcher ( index ); + g_idle_add ( delayed_start, GINT_TO_POINTER ( index ) ); + } + else { + fprintf ( stderr, "The %s switcher has not been enabled\n", sname ); + return G_SOURCE_REMOVE; + } + } + else{ + // Daemon mode, Listen to key presses.. + if ( !grab_global_keybindings () ) { + fprintf ( stderr, "Rofi was launched in daemon mode, but no key-binding was specified.\n" ); + fprintf ( stderr, "Please check the manpage on how to specify a key-binding.\n" ); + fprintf ( stderr, "The following modi are enabled and keys can be specified:\n" ); + for ( unsigned int i = 0; i < num_modi; i++ ) { + const char *name = mode_get_name ( modi[i] ); + fprintf ( stderr, "\t* "color_bold "%s"color_reset ": -key-%s \n", name, name ); + } + // Cleanup + return G_SOURCE_REMOVE; + } + if ( !quiet ) { + fprintf ( stdout, "Rofi is launched in daemon mode.\n" ); + print_global_keybindings (); + } + + // done starting deamon. + + if ( sncontext != NULL ) { + sn_launchee_context_complete ( sncontext ); + } + daemon_mode = TRUE; + XSelectInput ( display, DefaultRootWindow ( display ), KeyPressMask ); + XFlush ( display ); + } + + return G_SOURCE_REMOVE; +} + int main ( int argc, char *argv[] ) { TIMINGS_START (); @@ -651,7 +745,6 @@ int main ( int argc, char *argv[] ) // Detect if we are in dmenu mode. // This has two possible causes. // 1 the user specifies it on the command-line. - int dmenu_mode = FALSE; if ( find_arg ( "-dmenu" ) >= 0 ) { dmenu_mode = TRUE; } @@ -773,31 +866,8 @@ int main ( int argc, char *argv[] ) // Parse the keybindings. parse_keys_abe (); TICK_N ( "Parse ABE" ); - char *msg = NULL; - if ( find_arg_str ( "-e", &( msg ) ) ) { - int markup = FALSE; - if ( find_arg ( "-markup" ) >= 0 ) { - markup = TRUE; - } - return show_error_message ( msg, markup ); - } rofi_view_workers_initialize (); - // Dmenu mode. - if ( dmenu_mode == TRUE ) { - // force off sidebar mode: - config.sidebar_mode = FALSE; - int retv = run_dmenu (); - - // User canceled the operation. - if ( retv == FALSE ) { - return EXIT_FAILURE; - } - else if ( retv >= 10 ) { - return retv; - } - return EXIT_SUCCESS; - } // Setup signal handling sources. // SIGHup signal. @@ -806,60 +876,11 @@ int main ( int argc, char *argv[] ) g_unix_signal_add ( SIGINT, main_loop_signal_handler_int, NULL ); // SIGUSR1 g_unix_signal_add ( SIGUSR1, main_loop_signal_handler_usr1, NULL ); - // flags to run immediately and exit - char *sname = NULL; - if ( find_arg_str ( "-show", &sname ) == TRUE ) { - int index = switcher_get ( sname ); - if ( index < 0 ) { - // Add it to the list - index = add_mode ( sname ); - // Complain - if ( index >= 0 ) { - fprintf ( stdout, "Mode %s not enabled. Please add it to the list of enabled modi: %s\n", - sname, config.modi ); - fprintf ( stdout, "Adding mode: %s\n", sname ); - } - // Run it anyway if found. - } - if ( index >= 0 ) { - run_switcher ( index ); - g_idle_add ( delayed_start, GINT_TO_POINTER ( index ) ); - } - else { - fprintf ( stderr, "The %s switcher has not been enabled\n", sname ); - return EXIT_FAILURE; - } - } - else{ - // Daemon mode, Listen to key presses.. - if ( !grab_global_keybindings () ) { - fprintf ( stderr, "Rofi was launched in daemon mode, but no key-binding was specified.\n" ); - fprintf ( stderr, "Please check the manpage on how to specify a key-binding.\n" ); - fprintf ( stderr, "The following modi are enabled and keys can be specified:\n" ); - for ( unsigned int i = 0; i < num_modi; i++ ) { - const char *name = mode_get_name ( modi[i] ); - fprintf ( stderr, "\t* "color_bold "%s"color_reset ": -key-%s \n", name, name ); - } - // Cleanup - return EXIT_FAILURE; - } - if ( !quiet ) { - fprintf ( stdout, "Rofi is launched in daemon mode.\n" ); - print_global_keybindings (); - } - // done starting deamon. - - if ( sncontext != NULL ) { - sn_launchee_context_complete ( sncontext ); - } - daemon_mode = TRUE; - XSelectInput ( display, DefaultRootWindow ( display ), KeyPressMask ); - XFlush ( display ); - } + g_idle_add ( startup, NULL ); // Start mainloop. g_main_loop_run ( main_loop ); - return EXIT_SUCCESS; + return return_code; } diff --git a/source/textbox.c b/source/textbox.c index efa36023..4a456a56 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -78,7 +78,7 @@ static gboolean textbox_blink ( gpointer data ) textbox* textbox_create ( TextboxFlags flags, short x, short y, short w, short h, TextBoxFontType tbft, const char *text ) { - textbox *tb = g_malloc0 ( sizeof ( textbox ) ); + textbox *tb = g_slice_new0 ( textbox ); tb->flags = flags; @@ -258,7 +258,7 @@ void textbox_free ( textbox *tb ) tb->main_surface = NULL; } - g_free ( tb ); + g_slice_free ( textbox, tb ); } static void texbox_update ( textbox *tb ) diff --git a/source/view.c b/source/view.c index 4abcafc5..9c51fbfe 100644 --- a/source/view.c +++ b/source/view.c @@ -400,6 +400,7 @@ static Window __create_window ( Display *display, MenuFlags menu_flags ) surface = cairo_xlib_surface_create ( display, box, vinfo.visual, 200, 100 ); // Create a drawable. draw = cairo_create ( surface ); + g_assert ( draw != NULL ); cairo_set_operator ( draw, CAIRO_OPERATOR_SOURCE ); // Set up pango context. @@ -690,14 +691,14 @@ static unsigned int rofi_scroll_per_page ( RofiViewState * state ) static unsigned int rofi_scroll_continious ( RofiViewState * state ) { - unsigned int middle = (state->menu_lines - ((state->menu_lines&1) == 0))/ 2; + unsigned int middle = ( state->menu_lines - ( ( state->menu_lines & 1 ) == 0 ) ) / 2; unsigned int offset = 0; if ( state->selected > middle ) { - if ( state->selected < ( state->filtered_lines - (state->menu_lines -middle) ) ) { + if ( state->selected < ( state->filtered_lines - ( state->menu_lines - middle ) ) ) { offset = state->selected - middle; } // Don't go below zero. - else if ( state->filtered_lines > state->menu_lines){ + else if ( state->filtered_lines > state->menu_lines ) { offset = state->filtered_lines - state->menu_lines; } } @@ -738,13 +739,13 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d ) element_width = ( element_width - ( columns - 1 ) * config.line_margin ) / columns; } - int element_height = state->line_height * config.element_height; - int y_offset = state->top_offset; - int x_offset = state->border; + int element_height = state->line_height * config.element_height; + int y_offset = state->top_offset; + int x_offset = state->border; if ( state->rchanged ) { // Move, resize visible boxes and show them. - for ( i = 0; i < max_elements && (i+offset) < state->filtered_lines; i++ ) { + for ( i = 0; i < max_elements && ( i + offset ) < state->filtered_lines; i++ ) { unsigned int ex = ( ( i ) / state->max_rows ) * ( element_width + config.line_margin ); unsigned int ey = ( ( i ) % state->max_rows ) * ( element_height + config.line_margin ); // Move it around. @@ -764,7 +765,7 @@ static void rofi_view_draw ( RofiViewState *state, cairo_t *d ) } else{ // Only do basic redrawing + highlight of row. - for ( i = 0; i < max_elements && (i+offset) < state->filtered_lines; i++ ) { + for ( i = 0; i < max_elements && ( i + offset ) < state->filtered_lines; i++ ) { TextBoxFontType type = ( ( ( i % state->max_rows ) & 1 ) == 0 ) ? NORMAL : ALT; int fstate = 0; mode_get_display_value ( state->sw, state->line_map[i + offset], &fstate, FALSE ); @@ -1416,7 +1417,8 @@ RofiViewState *rofi_view_create ( Mode *sw, const char *input, char *prompt, const char *message, - MenuFlags menu_flags ) + MenuFlags menu_flags, + void ( *finalize )( RofiViewState *state ) ) { TICK (); RofiViewState *state = __rofi_view_state_create (); @@ -1434,6 +1436,7 @@ RofiViewState *rofi_view_create ( Mode *sw, state->cur_page = -1; state->border = config.padding + config.menu_bw; state->x11_event_loop = rofi_view_mainloop_iter; + state->finalize = finalize; // Request the lines to show. state->num_lines = mode_get_num_entries ( sw ); @@ -1625,6 +1628,7 @@ RofiViewState *rofi_view_create ( Mode *sw, } static void __error_dialog_event_loop ( RofiViewState *state, XEvent *ev ) { + printf ( "Event\n" ); // Handle event. if ( ev->type == Expose ) { while ( XCheckTypedEvent ( display, Expose, ev ) ) { @@ -1632,6 +1636,21 @@ static void __error_dialog_event_loop ( RofiViewState *state, XEvent *ev ) } state->update = TRUE; } + else if ( ev->type == ConfigureNotify ) { + XConfigureEvent xce = ev->xconfigure; + if ( xce.window == main_window ) { + if ( state->x != (int ) xce.x || state->y != (int) xce.y ) { + state->x = xce.x; + state->y = xce.y; + state->update = TRUE; + } + if ( state->w != (unsigned int) xce.width || state->h != (unsigned int ) xce.height ) { + state->w = xce.width; + state->h = xce.height; + cairo_xlib_surface_set_size ( surface, state->w, state->h ); + } + } + } // Key press event. else if ( ev->type == KeyPress ) { while ( XCheckTypedEvent ( display, KeyPress, ev ) ) { @@ -1641,6 +1660,7 @@ static void __error_dialog_event_loop ( RofiViewState *state, XEvent *ev ) } rofi_view_update ( state ); } +void process_result_error ( RofiViewState *state ); void rofi_view_error_dialog ( const char *msg, int markup ) { RofiViewState *state = __rofi_view_state_create (); @@ -1648,7 +1668,8 @@ void rofi_view_error_dialog ( const char *msg, int markup ) state->update = TRUE; state->border = config.padding + config.menu_bw; state->x11_event_loop = __error_dialog_event_loop; - state->finalize = NULL; + // TODO fix + state->finalize = process_result_error; // Try to grab the keyboard as early as possible. // We grab this using the rootwindow (as dmenu does it). @@ -1668,6 +1689,7 @@ void rofi_view_error_dialog ( const char *msg, int markup ) XWindowAttributes attr; if ( main_window == None || XGetWindowAttributes ( display, main_window, &attr ) == 0 ) { main_window = __create_window ( display, MENU_NORMAL ); + printf ( "new window\n" ); } rofi_view_calculate_window_and_element_width ( state ); @@ -1689,15 +1711,18 @@ void rofi_view_error_dialog ( const char *msg, int markup ) XMapRaised ( display, main_window ); if ( sncontext != NULL ) { - sn_launchee_context_complete ( sncontext ); +// sn_launchee_context_complete ( sncontext ); } + printf ( "start\n" ); rofi_view_set_active ( state ); + //rofi_view_queue_redraw(); main_loop_x11_event_handler ( NULL ); - while ( !rofi_view_get_completed ( state ) ) { - g_main_context_iteration ( NULL, TRUE ); - } - rofi_view_set_active ( NULL ); - rofi_view_free ( state ); + //while ( !rofi_view_get_completed ( state ) ) { + // printf("main loop: %d\n", g_main_context_is_owner(g_main_context_default())); + // g_main_context_iteration ( NULL, TRUE ); + //} + //rofi_view_set_active ( NULL ); + //rofi_view_free ( state ); } void rofi_view_cleanup () @@ -1756,4 +1781,8 @@ void rofi_view_workers_finalize ( void ) tpool = NULL; } } +Mode * rofi_view_get_mode ( RofiViewState *state ) +{ + return state->sw; +} diff --git a/source/x11-helper.c b/source/x11-helper.c index 89c89a7b..7137c55d 100644 --- a/source/x11-helper.c +++ b/source/x11-helper.c @@ -521,7 +521,7 @@ void x11_parse_key ( char *combo, unsigned int *mod, KeySym *key ) if ( str->len > 0 ) { show_error_message ( str->str, TRUE ); g_string_free ( str, TRUE ); - exit ( EXIT_FAILURE ); + return; } g_string_free ( str, TRUE ); *key = sym; -- cgit v1.2.3