From dffc27e5aa13dd5e5ada9cee24e4a7151d82f8b6 Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Fri, 31 Jul 2015 12:23:41 +0200 Subject: [PATCH] Try to make things more robust, e.g. try to recover before giving up. --- include/x11-helper.h | 2 +- source/helper.c | 11 ++------- source/rofi.c | 5 ++-- source/textbox.c | 54 +++++++++++++++++++++++++------------------- source/x11-helper.c | 25 +++++++++++++------- 5 files changed, 54 insertions(+), 43 deletions(-) diff --git a/include/x11-helper.h b/include/x11-helper.h index 4531dfa1..56ebc8b1 100644 --- a/include/x11-helper.h +++ b/include/x11-helper.h @@ -142,7 +142,7 @@ void create_visual_and_colormap ( Display *display ); * * Allocate a pixel value for an X named color */ -unsigned int color_get ( Display *display, const char *const name ); +unsigned int color_get ( Display *display, const char *const name, const char * const defn ); unsigned int color_background ( Display *display ); unsigned int color_border ( Display *display ); diff --git a/source/helper.c b/source/helper.c index 6d4e4766..061a510b 100644 --- a/source/helper.c +++ b/source/helper.c @@ -307,7 +307,8 @@ char helper_parse_char ( const char *arg ) } if ( retv < 0 ) { fprintf ( stderr, "Failed to parse character string: \"%s\"\n", arg ); - exit ( 1 ); + // for now default to newline. + retv = '\n'; } return retv; } @@ -458,14 +459,6 @@ void remove_pid_file ( int fd ) */ void config_sanity_check ( ) { - if ( find_arg ( "-rnow" ) >= 0 || find_arg ( "-snow" ) >= 0 || - find_arg ( "-now" ) >= 0 || find_arg ( "-key" ) >= 0 || - find_arg ( "-skey" ) >= 0 || find_arg ( "-rkey" ) >= 0 ) { - fprintf ( stderr, "The -snow, -now, -rnow, -key, -rkey, -skey are deprecated " - "and have been removed.\n" - "Please see the manpage: %s -help for the correct syntax.", stored_argv[0] ); - exit ( EXIT_FAILURE ); - } int found_error = FALSE; GString *msg = g_string_new ( "The configuration failed to validate:\n" ); if ( config.element_height < 1 ) { diff --git a/source/rofi.c b/source/rofi.c index 7e86a45d..733fc90a 100644 --- a/source/rofi.c +++ b/source/rofi.c @@ -953,7 +953,8 @@ MenuReturn menu ( char **lines, unsigned int num_lines, char **input, char *prom if ( !has_keyboard ) { fprintf ( stderr, "Failed to grab keyboard, even after %d uS.", 500 * 1000 ); - exit ( EXIT_FAILURE ); + // Break off. + return MENU_CANCEL; } // main window isn't explicitly destroyed in case we switch modes. Reusing it prevents flicker XWindowAttributes attr; @@ -1396,7 +1397,7 @@ void error_dialog ( const char *msg, int markup ) if ( !has_keyboard ) { fprintf ( stderr, "Failed to grab keyboard, even after %d uS.", 500 * 1000 ); - exit ( EXIT_FAILURE ); + return; } // Get active monitor size. monitor_active ( display, &mon ); diff --git a/source/textbox.c b/source/textbox.c index f6f7d0d9..46e0c438 100644 --- a/source/textbox.c +++ b/source/textbox.c @@ -655,7 +655,7 @@ int textbox_keypress ( textbox *tb, XEvent *ev ) * Font setup. */ static void parse_color ( Visual *visual, Colormap colormap, - const char *bg, XftColor *color ) + const char *bg, XftColor *color, const char *def ) { if ( bg == NULL ) { return; @@ -669,13 +669,21 @@ static void parse_color ( Visual *visual, Colormap colormap, col.blue = ( ( val & 0x000000FF ) ) * 255; if ( !XftColorAllocValue ( display, visual, colormap, &col, color ) ) { fprintf ( stderr, "Failed to parse color: '%s'\n", bg ); - exit ( EXIT_FAILURE ); + // Go for default. + if ( !XftColorAllocName ( display, visual, colormap, def, color ) ) { + fprintf ( stderr, "Cannot allocate default color, giving up.\n" ); + exit ( EXIT_FAILURE ); + } } } else { if ( !XftColorAllocName ( display, visual, colormap, bg, color ) ) { fprintf ( stderr, "Failed to parse color: '%s'\n", bg ); - exit ( EXIT_FAILURE ); + // Go for default. + if ( !XftColorAllocName ( display, visual, colormap, def, color ) ) { + fprintf ( stderr, "Cannot allocate default color, giving up.\n" ); + exit ( EXIT_FAILURE ); + } } } } @@ -692,19 +700,19 @@ static void textbox_parse_string ( XVisualInfo *visual, Colormap colormap, const switch ( index ) { case 0: - parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->bg ) ); + parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->bg ), "black" ); break; case 1: - parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->fg ) ); + parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->fg ), "white" ); break; case 2: - parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->bgalt ) ); + parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->bgalt ), "black" ); break; case 3: - parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->hlbg ) ); + parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->hlbg ), "black" ); break; case 4: - parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->hlfg ) ); + parse_color ( visual->visual, colormap, g_strstrip ( token ), &( color->hlfg ), "white" ); break; } index++; @@ -725,23 +733,23 @@ void textbox_setup ( XVisualInfo *visual, Colormap colormap ) config.color_active, &( colors[ACTIVE] ) ); } else { - parse_color ( visual_info->visual, target_colormap, config.menu_bg, &( colors[NORMAL].bg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_fg, &( colors[NORMAL].fg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_bg_alt, &( colors[NORMAL].bgalt ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_hlfg, &( colors[NORMAL].hlfg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_hlbg, &( colors[NORMAL].hlbg ) ); + parse_color ( visual_info->visual, target_colormap, config.menu_bg, &( colors[NORMAL].bg ), "black" ); + parse_color ( visual_info->visual, target_colormap, config.menu_fg, &( colors[NORMAL].fg ), "white" ); + parse_color ( visual_info->visual, target_colormap, config.menu_bg_alt, &( colors[NORMAL].bgalt ), "black" ); + parse_color ( visual_info->visual, target_colormap, config.menu_hlfg, &( colors[NORMAL].hlfg ), "white" ); + parse_color ( visual_info->visual, target_colormap, config.menu_hlbg, &( colors[NORMAL].hlbg ), "black" ); - parse_color ( visual_info->visual, target_colormap, config.menu_bg_urgent, &( colors[URGENT].bg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_fg_urgent, &( colors[URGENT].fg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_bg_alt, &( colors[URGENT].bgalt ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_hlfg_urgent, &( colors[URGENT].hlfg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_hlbg_urgent, &( colors[URGENT].hlbg ) ); + parse_color ( visual_info->visual, target_colormap, config.menu_bg_urgent, &( colors[URGENT].bg ), "black" ); + parse_color ( visual_info->visual, target_colormap, config.menu_fg_urgent, &( colors[URGENT].fg ), "white" ); + parse_color ( visual_info->visual, target_colormap, config.menu_bg_alt, &( colors[URGENT].bgalt ), "black" ); + parse_color ( visual_info->visual, target_colormap, config.menu_hlfg_urgent, &( colors[URGENT].hlfg ), "white" ); + parse_color ( visual_info->visual, target_colormap, config.menu_hlbg_urgent, &( colors[URGENT].hlbg ), "black" ); - parse_color ( visual_info->visual, target_colormap, config.menu_bg_active, &( colors[ACTIVE].bg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_fg_active, &( colors[ACTIVE].fg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_bg_alt, &( colors[ACTIVE].bgalt ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_hlfg_active, &( colors[ACTIVE].hlfg ) ); - parse_color ( visual_info->visual, target_colormap, config.menu_hlbg_active, &( colors[ACTIVE].hlbg ) ); + parse_color ( visual_info->visual, target_colormap, config.menu_bg_active, &( colors[ACTIVE].bg ), "black" ); + parse_color ( visual_info->visual, target_colormap, config.menu_fg_active, &( colors[ACTIVE].fg ), "white" ); + parse_color ( visual_info->visual, target_colormap, config.menu_bg_alt, &( colors[ACTIVE].bgalt ), "black" ); + parse_color ( visual_info->visual, target_colormap, config.menu_hlfg_active, &( colors[ACTIVE].hlfg ), "white" ); + parse_color ( visual_info->visual, target_colormap, config.menu_hlbg_active, &( colors[ACTIVE].hlbg ), "black" ); } PangoFontMap *font_map = pango_xft_get_font_map ( display, DefaultScreen ( display ) ); p_context = pango_font_map_create_context ( font_map ); diff --git a/source/x11-helper.c b/source/x11-helper.c index 2c574e3e..395b176a 100644 --- a/source/x11-helper.c +++ b/source/x11-helper.c @@ -447,9 +447,10 @@ void create_visual_and_colormap ( Display *display ) map = DefaultColormap ( display, screen ); } } -unsigned int color_get ( Display *display, const char *const name ) +unsigned int color_get ( Display *display, const char *const name, const char * const defn ) { XColor color = { 0, 0, 0, 0, 0, 0 }; + XColor def; // Special format. if ( strncmp ( name, "argb:", 5 ) == 0 ) { color.pixel = strtoul ( &name[5], NULL, 16 ); @@ -461,17 +462,25 @@ unsigned int color_get ( Display *display, const char *const name ) Status st = XAllocColor ( display, map, &color ); if ( st == None ) { fprintf ( stderr, "Failed to parse color: '%s'\n", name ); - exit ( EXIT_FAILURE ); + st = XAllocNamedColor ( display, map, defn, &color, &def ); + if ( st == None ) { + fprintf ( stderr, "Failed to allocate fallback color\n" ); + exit ( EXIT_FAILURE ); + } } return color.pixel; } return color.pixel; } else { - Status st = XAllocNamedColor ( display, map, name, &color, &color ); + Status st = XAllocNamedColor ( display, map, name, &color, &def ); if ( st == None ) { fprintf ( stderr, "Failed to parse color: '%s'\n", name ); - exit ( EXIT_FAILURE ); + st = XAllocNamedColor ( display, map, defn, &color, &def ); + if ( st == None ) { + fprintf ( stderr, "Failed to allocate fallback color\n" ); + exit ( EXIT_FAILURE ); + } } return color.pixel; } @@ -480,14 +489,14 @@ unsigned int color_get ( Display *display, const char *const name ) unsigned int color_background ( Display *display ) { if ( !config.color_enabled ) { - return color_get ( display, config.menu_bg ); + return color_get ( display, config.menu_bg, "black" ); } else { unsigned int retv = 0; gchar **vals = g_strsplit ( config.color_window, ",", 2 ); if ( vals != NULL && vals[0] != NULL ) { - retv = color_get ( display, g_strstrip ( vals[0] ) ); + retv = color_get ( display, g_strstrip ( vals[0] ), "black" ); } g_strfreev ( vals ); return retv; @@ -497,14 +506,14 @@ unsigned int color_background ( Display *display ) unsigned int color_border ( Display *display ) { if ( !config.color_enabled ) { - return color_get ( display, config.menu_bc ); + return color_get ( display, config.menu_bc, "white" ); } else { unsigned int retv = 0; gchar **vals = g_strsplit ( config.color_window, ",", 2 ); if ( vals != NULL && vals[0] != NULL && vals[1] != NULL ) { - retv = color_get ( display, vals[1] ); + retv = color_get ( display, vals[1], "white" ); } g_strfreev ( vals ); return retv;