parse_color: return success + drop fallback color

This is the first in a series of commits to refactor the color handling
in sway. This changes parse_color to return whether it was success and
no longer uses 0xFFFFFFFF as the fallback color. This also verifies that
the string actually contains a valid hexadecimal number along with
the length checks.

In the process of altering the calls to parse_color, I also took the
opportunity to heavily refactor swaybar's ipc_parse_colors function.
This allowed for several lines of duplicated code to be removed.
This commit is contained in:
Brian Ashworth 2019-12-27 23:33:55 -05:00 committed by Simon Ser
parent 088b374b1a
commit 97f9f0b699
7 changed files with 76 additions and 142 deletions

View file

@ -1,4 +1,5 @@
#define _POSIX_C_SOURCE 200809L #define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <float.h> #include <float.h>
#include <fcntl.h> #include <fcntl.h>
#include <math.h> #include <math.h>
@ -13,21 +14,21 @@ int wrap(int i, int max) {
return ((i % max) + max) % max; return ((i % max) + max) % max;
} }
uint32_t parse_color(const char *color) { bool parse_color(const char *color, uint32_t *result) {
if (color[0] == '#') { if (color[0] == '#') {
++color; ++color;
} }
int len = strlen(color); int len = strlen(color);
if (len != 6 && len != 8) { if ((len != 6 && len != 8) || !isxdigit(color[0]) || !isxdigit(color[1])) {
sway_log(SWAY_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); return false;
return 0xFFFFFFFF;
} }
uint32_t res = (uint32_t)strtoul(color, NULL, 16); char *ptr;
if (strlen(color) == 6) { uint32_t parsed = strtoul(color, &ptr, 16);
res = (res << 8) | 0xFF; if (*ptr != '\0') {
return false;
} }
return res; *result = len == 6 ? ((parsed << 8) | 0xFF) : parsed;
return true;
} }
bool parse_boolean(const char *boolean, bool current) { bool parse_boolean(const char *boolean, bool current) {

View file

@ -9,7 +9,8 @@ struct i3bar_block {
int ref_count; int ref_count;
char *full_text, *short_text, *align, *min_width_str; char *full_text, *short_text, *align, *min_width_str;
bool urgent; bool urgent;
uint32_t *color; uint32_t color;
bool color_set;
int min_width; int min_width;
char *name, *instance; char *name, *instance;
bool separator; bool separator;

View file

@ -11,10 +11,11 @@
int wrap(int i, int max); int wrap(int i, int max);
/** /**
* Given a string that represents an RGB(A) color, return a uint32_t * Given a string that represents an RGB(A) color, result will be set to a
* version of the color. * uint32_t version of the color, as long as it is valid. If it is invalid,
* then false will be returned and result will be untouched.
*/ */
uint32_t parse_color(const char *color); bool parse_color(const char *color, uint32_t *result);
/** /**
* Given a string that represents a boolean, return the boolean value. This * Given a string that represents a boolean, return the boolean value. This

View file

@ -24,7 +24,6 @@ void i3bar_block_unref(struct i3bar_block *block) {
free(block->min_width_str); free(block->min_width_str);
free(block->name); free(block->name);
free(block->instance); free(block->instance);
free(block->color);
free(block); free(block);
} }
} }
@ -70,8 +69,11 @@ static void i3bar_parse_json(struct status_line *status,
block->short_text = short_text ? block->short_text = short_text ?
strdup(json_object_get_string(short_text)) : NULL; strdup(json_object_get_string(short_text)) : NULL;
if (color) { if (color) {
block->color = malloc(sizeof(uint32_t)); const char *hexstring = json_object_get_string(color);
*block->color = parse_color(json_object_get_string(color)); block->color_set = parse_color(hexstring, &block->color);
if (!block->color_set) {
sway_log(SWAY_ERROR, "Invalid block color: %s", hexstring);
}
} }
if (min_width) { if (min_width) {
json_type type = json_object_get_type(min_width); json_type type = json_object_get_type(min_width);
@ -98,10 +100,14 @@ static void i3bar_parse_json(struct status_line *status,
block->separator_block_width = separator_block_width ? block->separator_block_width = separator_block_width ?
json_object_get_int(separator_block_width) : 9; json_object_get_int(separator_block_width) : 9;
// Airblader features // Airblader features
block->background = background ? const char *hex = background ? json_object_get_string(background) : NULL;
parse_color(json_object_get_string(background)) : 0; if (hex && !parse_color(hex, &block->background)) {
block->border = border ? sway_log(SWAY_ERROR, "Ignoring invalid block background: %s", hex);
parse_color(json_object_get_string(border)) : 0; }
hex = border ? json_object_get_string(border) : NULL;
if (hex && !parse_color(hex, &block->border)) {
sway_log(SWAY_ERROR, "Ignoring invalid block border: %s", hex);
}
block->border_top = border_top ? json_object_get_int(border_top) : 1; block->border_top = border_top ? json_object_get_int(border_top) : 1;
block->border_bottom = border_bottom ? block->border_bottom = border_bottom ?
json_object_get_int(border_bottom) : 1; json_object_get_int(border_bottom) : 1;

View file

@ -55,117 +55,42 @@ char *parse_font(const char *font) {
static void ipc_parse_colors( static void ipc_parse_colors(
struct swaybar_config *config, json_object *colors) { struct swaybar_config *config, json_object *colors) {
json_object *background, *statusline, *separator; struct {
json_object *focused_background, *focused_statusline, *focused_separator; const char *name;
json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text; uint32_t *color;
json_object *inactive_workspace_border, *inactive_workspace_bg, *inactive_workspace_text; } properties[] = {
json_object *active_workspace_border, *active_workspace_bg, *active_workspace_text; { "background", &config->colors.background },
json_object *urgent_workspace_border, *urgent_workspace_bg, *urgent_workspace_text; { "statusline", &config->colors.statusline },
json_object *binding_mode_border, *binding_mode_bg, *binding_mode_text; { "separator", &config->colors.separator },
json_object_object_get_ex(colors, "background", &background); { "focused_background", &config->colors.focused_background },
json_object_object_get_ex(colors, "statusline", &statusline); { "focused_statusline", &config->colors.focused_statusline },
json_object_object_get_ex(colors, "separator", &separator); { "focused_separator", &config->colors.focused_separator },
json_object_object_get_ex(colors, "focused_background", &focused_background); { "focused_workspace_border", &config->colors.focused_workspace.border },
json_object_object_get_ex(colors, "focused_statusline", &focused_statusline); { "focused_workspace_bg", &config->colors.focused_workspace.background },
json_object_object_get_ex(colors, "focused_separator", &focused_separator); { "focused_workspace_text", &config->colors.focused_workspace.text },
json_object_object_get_ex(colors, "focused_workspace_border", &focused_workspace_border); { "active_workspace_border", &config->colors.active_workspace.border },
json_object_object_get_ex(colors, "focused_workspace_bg", &focused_workspace_bg); { "active_workspace_bg", &config->colors.active_workspace.background },
json_object_object_get_ex(colors, "focused_workspace_text", &focused_workspace_text); { "active_workspace_text", &config->colors.active_workspace.text },
json_object_object_get_ex(colors, "active_workspace_border", &active_workspace_border); { "inactive_workspace_border", &config->colors.inactive_workspace.border },
json_object_object_get_ex(colors, "active_workspace_bg", &active_workspace_bg); { "inactive_workspace_bg", &config->colors.inactive_workspace.background },
json_object_object_get_ex(colors, "active_workspace_text", &active_workspace_text); { "inactive_workspace_text", &config->colors.inactive_workspace.text },
json_object_object_get_ex(colors, "inactive_workspace_border", &inactive_workspace_border); { "urgent_workspace_border", &config->colors.urgent_workspace.border },
json_object_object_get_ex(colors, "inactive_workspace_bg", &inactive_workspace_bg); { "urgent_workspace_bg", &config->colors.urgent_workspace.background },
json_object_object_get_ex(colors, "inactive_workspace_text", &inactive_workspace_text); { "urgent_workspace_text", &config->colors.urgent_workspace.text },
json_object_object_get_ex(colors, "urgent_workspace_border", &urgent_workspace_border); { "binding_mode_border", &config->colors.binding_mode.border },
json_object_object_get_ex(colors, "urgent_workspace_bg", &urgent_workspace_bg); { "binding_mode_bg", &config->colors.binding_mode.background },
json_object_object_get_ex(colors, "urgent_workspace_text", &urgent_workspace_text); { "binding_mode_text", &config->colors.binding_mode.text },
json_object_object_get_ex(colors, "binding_mode_border", &binding_mode_border); };
json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg);
json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text); for (size_t i = 0; i < sizeof(properties) / sizeof(properties[i]); i++) {
if (background) { json_object *object;
config->colors.background = parse_color( if (json_object_object_get_ex(colors, properties[i].name, &object)) {
json_object_get_string(background)); const char *hexstring = json_object_get_string(object);
} if (!parse_color(hexstring, properties[i].color)) {
if (statusline) { sway_log(SWAY_ERROR, "Ignoring invalid %s: %s",
config->colors.statusline = parse_color( properties[i].name, hexstring);
json_object_get_string(statusline)); }
} }
if (separator) {
config->colors.separator = parse_color(
json_object_get_string(separator));
}
if (focused_background) {
config->colors.focused_background = parse_color(
json_object_get_string(focused_background));
}
if (focused_statusline) {
config->colors.focused_statusline = parse_color(
json_object_get_string(focused_statusline));
}
if (focused_separator) {
config->colors.focused_separator = parse_color(
json_object_get_string(focused_separator));
}
if (focused_workspace_border) {
config->colors.focused_workspace.border = parse_color(
json_object_get_string(focused_workspace_border));
}
if (focused_workspace_bg) {
config->colors.focused_workspace.background = parse_color(
json_object_get_string(focused_workspace_bg));
}
if (focused_workspace_text) {
config->colors.focused_workspace.text = parse_color(
json_object_get_string(focused_workspace_text));
}
if (active_workspace_border) {
config->colors.active_workspace.border = parse_color(
json_object_get_string(active_workspace_border));
}
if (active_workspace_bg) {
config->colors.active_workspace.background = parse_color(
json_object_get_string(active_workspace_bg));
}
if (active_workspace_text) {
config->colors.active_workspace.text = parse_color(
json_object_get_string(active_workspace_text));
}
if (inactive_workspace_border) {
config->colors.inactive_workspace.border = parse_color(
json_object_get_string(inactive_workspace_border));
}
if (inactive_workspace_bg) {
config->colors.inactive_workspace.background = parse_color(
json_object_get_string(inactive_workspace_bg));
}
if (inactive_workspace_text) {
config->colors.inactive_workspace.text = parse_color(
json_object_get_string(inactive_workspace_text));
}
if (urgent_workspace_border) {
config->colors.urgent_workspace.border = parse_color(
json_object_get_string(urgent_workspace_border));
}
if (urgent_workspace_bg) {
config->colors.urgent_workspace.background = parse_color(
json_object_get_string(urgent_workspace_bg));
}
if (urgent_workspace_text) {
config->colors.urgent_workspace.text = parse_color(
json_object_get_string(urgent_workspace_text));
}
if (binding_mode_border) {
config->colors.binding_mode.border = parse_color(
json_object_get_string(binding_mode_border));
}
if (binding_mode_bg) {
config->colors.binding_mode.background = parse_color(
json_object_get_string(binding_mode_bg));
}
if (binding_mode_text) {
config->colors.binding_mode.text = parse_color(
json_object_get_string(binding_mode_text));
} }
} }

View file

@ -265,7 +265,7 @@ static uint32_t render_status_block(cairo_t *cairo,
} }
double text_y = height / 2.0 - text_height / 2.0; double text_y = height / 2.0 - text_height / 2.0;
cairo_move_to(cairo, offset, (int)floor(text_y)); cairo_move_to(cairo, offset, (int)floor(text_y));
uint32_t color = block->color ? *block->color : config->colors.statusline; uint32_t color = block->color_set ? block->color : config->colors.statusline;
color = block->urgent ? config->colors.urgent_workspace.text : color; color = block->urgent ? config->colors.urgent_workspace.text : color;
cairo_set_source_u32(cairo, color); cairo_set_source_u32(cairo, color);
pango_printf(cairo, config->font, output->scale, pango_printf(cairo, config->font, output->scale,

View file

@ -221,28 +221,28 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); fprintf(stdout, "swaynag version " SWAY_VERSION "\n");
return -1; return -1;
case TO_COLOR_BACKGROUND: // Background color case TO_COLOR_BACKGROUND: // Background color
if (type) { if (type && !parse_color(optarg, &type->background)) {
type->background = parse_color(optarg); fprintf(stderr, "Invalid background color: %s", optarg);
} }
break; break;
case TO_COLOR_BORDER: // Border color case TO_COLOR_BORDER: // Border color
if (type) { if (type && !parse_color(optarg, &type->border)) {
type->border = parse_color(optarg); fprintf(stderr, "Invalid border color: %s", optarg);
} }
break; break;
case TO_COLOR_BORDER_BOTTOM: // Bottom border color case TO_COLOR_BORDER_BOTTOM: // Bottom border color
if (type) { if (type && !parse_color(optarg, &type->border_bottom)) {
type->border_bottom = parse_color(optarg); fprintf(stderr, "Invalid border bottom color: %s", optarg);
} }
break; break;
case TO_COLOR_BUTTON: // Button background color case TO_COLOR_BUTTON: // Button background color
if (type) { if (type && !parse_color(optarg, &type->button_background)) {
type->button_background = parse_color(optarg); fprintf(stderr, "Invalid button background color: %s", optarg);
} }
break; break;
case TO_COLOR_TEXT: // Text color case TO_COLOR_TEXT: // Text color
if (type) { if (type && !parse_color(optarg, &type->text)) {
type->text = parse_color(optarg); fprintf(stderr, "Invalid text color: %s", optarg);
} }
break; break;
case TO_THICK_BAR_BORDER: // Bottom border thickness case TO_THICK_BAR_BORDER: // Bottom border thickness