Try to make things more robust, e.g. try to recover before giving up.

This commit is contained in:
Dave Davenport 2015-07-31 12:23:41 +02:00
parent 69c75971f3
commit dffc27e5aa
5 changed files with 54 additions and 43 deletions

View file

@ -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 );

View file

@ -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 ( "<big><b>The configuration failed to validate:</b></big>\n" );
if ( config.element_height < 1 ) {

View file

@ -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 );

View file

@ -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 );

View file

@ -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;