aboutsummaryrefslogtreecommitdiff
path: root/swaybar/i3bar.c
diff options
context:
space:
mode:
authorBrian Ashworth <bosrsf04@gmail.com>2019-12-27 23:33:55 -0500
committerSimon Ser <contact@emersion.fr>2019-12-28 10:07:25 +0100
commit97f9f0b699316ba60009b395948a712ec0b671d2 (patch)
treeb5916b36f4161c1c4d670295254d0f3fd9e793df /swaybar/i3bar.c
parent088b374b1a3e7ead08e1430d3d89649b1cd5a54b (diff)
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.
Diffstat (limited to 'swaybar/i3bar.c')
-rw-r--r--swaybar/i3bar.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/swaybar/i3bar.c b/swaybar/i3bar.c
index 43e2fe2d..5c8b87a2 100644
--- a/swaybar/i3bar.c
+++ b/swaybar/i3bar.c
@@ -24,7 +24,6 @@ void i3bar_block_unref(struct i3bar_block *block) {
free(block->min_width_str);
free(block->name);
free(block->instance);
- free(block->color);
free(block);
}
}
@@ -70,8 +69,11 @@ static void i3bar_parse_json(struct status_line *status,
block->short_text = short_text ?
strdup(json_object_get_string(short_text)) : NULL;
if (color) {
- block->color = malloc(sizeof(uint32_t));
- *block->color = parse_color(json_object_get_string(color));
+ const char *hexstring = 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) {
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 ?
json_object_get_int(separator_block_width) : 9;
// Airblader features
- block->background = background ?
- parse_color(json_object_get_string(background)) : 0;
- block->border = border ?
- parse_color(json_object_get_string(border)) : 0;
+ const char *hex = background ? json_object_get_string(background) : NULL;
+ if (hex && !parse_color(hex, &block->background)) {
+ sway_log(SWAY_ERROR, "Ignoring invalid block background: %s", hex);
+ }
+ 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_bottom = border_bottom ?
json_object_get_int(border_bottom) : 1;