mirror of
https://github.com/swaywm/sway.git
synced 2026-03-15 05:34:12 -04:00
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:
parent
088b374b1a
commit
97f9f0b699
7 changed files with 76 additions and 142 deletions
|
|
@ -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;
|
||||
|
|
|
|||
147
swaybar/ipc.c
147
swaybar/ipc.c
|
|
@ -55,117 +55,42 @@ char *parse_font(const char *font) {
|
|||
|
||||
static void ipc_parse_colors(
|
||||
struct swaybar_config *config, json_object *colors) {
|
||||
json_object *background, *statusline, *separator;
|
||||
json_object *focused_background, *focused_statusline, *focused_separator;
|
||||
json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text;
|
||||
json_object *inactive_workspace_border, *inactive_workspace_bg, *inactive_workspace_text;
|
||||
json_object *active_workspace_border, *active_workspace_bg, *active_workspace_text;
|
||||
json_object *urgent_workspace_border, *urgent_workspace_bg, *urgent_workspace_text;
|
||||
json_object *binding_mode_border, *binding_mode_bg, *binding_mode_text;
|
||||
json_object_object_get_ex(colors, "background", &background);
|
||||
json_object_object_get_ex(colors, "statusline", &statusline);
|
||||
json_object_object_get_ex(colors, "separator", &separator);
|
||||
json_object_object_get_ex(colors, "focused_background", &focused_background);
|
||||
json_object_object_get_ex(colors, "focused_statusline", &focused_statusline);
|
||||
json_object_object_get_ex(colors, "focused_separator", &focused_separator);
|
||||
json_object_object_get_ex(colors, "focused_workspace_border", &focused_workspace_border);
|
||||
json_object_object_get_ex(colors, "focused_workspace_bg", &focused_workspace_bg);
|
||||
json_object_object_get_ex(colors, "focused_workspace_text", &focused_workspace_text);
|
||||
json_object_object_get_ex(colors, "active_workspace_border", &active_workspace_border);
|
||||
json_object_object_get_ex(colors, "active_workspace_bg", &active_workspace_bg);
|
||||
json_object_object_get_ex(colors, "active_workspace_text", &active_workspace_text);
|
||||
json_object_object_get_ex(colors, "inactive_workspace_border", &inactive_workspace_border);
|
||||
json_object_object_get_ex(colors, "inactive_workspace_bg", &inactive_workspace_bg);
|
||||
json_object_object_get_ex(colors, "inactive_workspace_text", &inactive_workspace_text);
|
||||
json_object_object_get_ex(colors, "urgent_workspace_border", &urgent_workspace_border);
|
||||
json_object_object_get_ex(colors, "urgent_workspace_bg", &urgent_workspace_bg);
|
||||
json_object_object_get_ex(colors, "urgent_workspace_text", &urgent_workspace_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);
|
||||
if (background) {
|
||||
config->colors.background = parse_color(
|
||||
json_object_get_string(background));
|
||||
}
|
||||
if (statusline) {
|
||||
config->colors.statusline = parse_color(
|
||||
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));
|
||||
struct {
|
||||
const char *name;
|
||||
uint32_t *color;
|
||||
} properties[] = {
|
||||
{ "background", &config->colors.background },
|
||||
{ "statusline", &config->colors.statusline },
|
||||
{ "separator", &config->colors.separator },
|
||||
{ "focused_background", &config->colors.focused_background },
|
||||
{ "focused_statusline", &config->colors.focused_statusline },
|
||||
{ "focused_separator", &config->colors.focused_separator },
|
||||
{ "focused_workspace_border", &config->colors.focused_workspace.border },
|
||||
{ "focused_workspace_bg", &config->colors.focused_workspace.background },
|
||||
{ "focused_workspace_text", &config->colors.focused_workspace.text },
|
||||
{ "active_workspace_border", &config->colors.active_workspace.border },
|
||||
{ "active_workspace_bg", &config->colors.active_workspace.background },
|
||||
{ "active_workspace_text", &config->colors.active_workspace.text },
|
||||
{ "inactive_workspace_border", &config->colors.inactive_workspace.border },
|
||||
{ "inactive_workspace_bg", &config->colors.inactive_workspace.background },
|
||||
{ "inactive_workspace_text", &config->colors.inactive_workspace.text },
|
||||
{ "urgent_workspace_border", &config->colors.urgent_workspace.border },
|
||||
{ "urgent_workspace_bg", &config->colors.urgent_workspace.background },
|
||||
{ "urgent_workspace_text", &config->colors.urgent_workspace.text },
|
||||
{ "binding_mode_border", &config->colors.binding_mode.border },
|
||||
{ "binding_mode_bg", &config->colors.binding_mode.background },
|
||||
{ "binding_mode_text", &config->colors.binding_mode.text },
|
||||
};
|
||||
|
||||
for (size_t i = 0; i < sizeof(properties) / sizeof(properties[i]); i++) {
|
||||
json_object *object;
|
||||
if (json_object_object_get_ex(colors, properties[i].name, &object)) {
|
||||
const char *hexstring = json_object_get_string(object);
|
||||
if (!parse_color(hexstring, properties[i].color)) {
|
||||
sway_log(SWAY_ERROR, "Ignoring invalid %s: %s",
|
||||
properties[i].name, hexstring);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -265,7 +265,7 @@ static uint32_t render_status_block(cairo_t *cairo,
|
|||
}
|
||||
double text_y = height / 2.0 - text_height / 2.0;
|
||||
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;
|
||||
cairo_set_source_u32(cairo, color);
|
||||
pango_printf(cairo, config->font, output->scale,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue