From 8c1d56812bea35b4543898ef3df82b2d35355be1 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Tue, 15 Dec 2015 13:12:08 +0100 Subject: [PATCH 1/2] Implement bar option: colors {} --- include/config.h | 29 ++++--- sway/commands.c | 214 ++++++++++++++++++++++++++++++++++++++++++++++- sway/config.c | 33 ++++++++ 3 files changed, 264 insertions(+), 12 deletions(-) diff --git a/include/config.h b/include/config.h index 5bb7107a..2423c9ed 100644 --- a/include/config.h +++ b/include/config.h @@ -99,17 +99,24 @@ struct bar_config { bool binding_mode_indicator; bool verbose; struct { - char *background; - char *foreground; - char *focused_workspace_border; - char *focused_workspace_bg; - char *focused_workspace_text; - char *active_workspace_border; - char *active_workspace_bg; - char *active_workspace_text; - char *inactive_workspace_border; - char *inactive_workspace_bg; - char *inactive_workspace_text; + char background[8]; + char statusline[8]; + char separator[8]; + char focused_workspace_border[8]; + char focused_workspace_bg[8]; + char focused_workspace_text[8]; + char active_workspace_border[8]; + char active_workspace_bg[8]; + char active_workspace_text[8]; + char inactive_workspace_border[8]; + char inactive_workspace_bg[8]; + char inactive_workspace_text[8]; + char urgent_workspace_border[8]; + char urgent_workspace_bg[8]; + char urgent_workspace_text[8]; + char binding_mode_border[8]; + char binding_mode_bg[8]; + char binding_mode_text[8]; } colors; }; diff --git a/sway/commands.c b/sway/commands.c index 559d1e06..d2b53783 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -66,6 +66,7 @@ static sway_cmd cmd_workspace; static sway_cmd cmd_ws_auto_back_and_forth; static sway_cmd bar_cmd_bindsym; +static sway_cmd bar_cmd_colors; static sway_cmd bar_cmd_mode; static sway_cmd bar_cmd_modifier; static sway_cmd bar_cmd_hidden_state; @@ -77,6 +78,16 @@ static sway_cmd bar_cmd_tray_output; static sway_cmd bar_cmd_tray_padding; static sway_cmd bar_cmd_workspace_buttons; +static sway_cmd bar_colors_cmd_active_workspace; +static sway_cmd bar_colors_cmd_background; +static sway_cmd bar_colors_cmd_background; +static sway_cmd bar_colors_cmd_binding_mode; +static sway_cmd bar_colors_cmd_focused_workspace; +static sway_cmd bar_colors_cmd_inactive_workspace; +static sway_cmd bar_colors_cmd_separator; +static sway_cmd bar_colors_cmd_statusline; +static sway_cmd bar_colors_cmd_urgent_workspace; + swayc_t *sp_view; int sp_index = 0; @@ -1567,6 +1578,20 @@ static struct cmd_results *bar_cmd_bindsym(int argc, char **argv) { return cmd_results_new(CMD_SUCCESS, NULL, NULL); } +static struct cmd_results *bar_cmd_colors(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if (strcmp("{", argv[0]) != 0) { + return cmd_results_new(CMD_INVALID, "colors", + "Expected '{' at the start of colors config definition."); + } + + return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL); +} + static struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { struct cmd_results *error = NULL; if ((error = checkarg(argc, "hidden_state", EXPECTED_EQUAL_TO, 1))) { @@ -1804,7 +1829,7 @@ static struct cmd_results *bar_cmd_workspace_buttons(int argc, char **argv) { static struct cmd_handler bar_handlers[] = { { "binding_mode_indicator", NULL }, { "bindsym", bar_cmd_bindsym }, - { "colors", NULL }, + { "colors", bar_cmd_colors }, { "font", NULL }, { "hidden_state", bar_cmd_hidden_state }, { "id", bar_cmd_id }, @@ -1820,6 +1845,189 @@ static struct cmd_handler bar_handlers[] = { { "workspace_buttons", bar_cmd_workspace_buttons }, }; +/** + * Check and add color to buffer. + * + * return error object, or NULL if color is valid. + */ +static struct cmd_results *add_color(const char *name, char *buffer, const char *color) { + if (strlen(color) != 7) { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + + if (color[0] != '#') { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + + int i; + for (i = 1; i < 7; ++i) { + if (!isxdigit(color[i])) { + return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); + } + } + + // copy color to buffer + strncpy(buffer, color, 7); + sway_log(L_DEBUG, "Setting %s color %s for bar: %s", name, color, config->current_bar->id); + + return NULL; +} + +static struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "active_workspace", EXPECTED_EQUAL_TO, 3))) { + return error; + } + + if ((error = add_color("active_workspace_border", config->current_bar->colors.active_workspace_border, argv[0]))) { + return error; + } + + if ((error = add_color("active_workspace_bg", config->current_bar->colors.active_workspace_bg, argv[1]))) { + return error; + } + + if ((error = add_color("active_workspace_text", config->current_bar->colors.active_workspace_text, argv[2]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_background(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "background", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if ((error = add_color("background", config->current_bar->colors.background, argv[0]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_binding_mode(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "binding_mode", EXPECTED_EQUAL_TO, 3))) { + return error; + } + + if ((error = add_color("binding_mode_border", config->current_bar->colors.binding_mode_border, argv[0]))) { + return error; + } + + if ((error = add_color("binding_mode_bg", config->current_bar->colors.binding_mode_bg, argv[1]))) { + return error; + } + + if ((error = add_color("binding_mode_text", config->current_bar->colors.binding_mode_text, argv[2]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_focused_workspace(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "focused_workspace", EXPECTED_EQUAL_TO, 3))) { + return error; + } + + if ((error = add_color("focused_workspace_border", config->current_bar->colors.focused_workspace_border, argv[0]))) { + return error; + } + + if ((error = add_color("focused_workspace_bg", config->current_bar->colors.focused_workspace_bg, argv[1]))) { + return error; + } + + if ((error = add_color("focused_workspace_text", config->current_bar->colors.focused_workspace_text, argv[2]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_inactive_workspace(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "inactive_workspace", EXPECTED_EQUAL_TO, 3))) { + return error; + } + + if ((error = add_color("inactive_workspace_border", config->current_bar->colors.inactive_workspace_border, argv[0]))) { + return error; + } + + if ((error = add_color("inactive_workspace_bg", config->current_bar->colors.inactive_workspace_bg, argv[1]))) { + return error; + } + + if ((error = add_color("inactive_workspace_text", config->current_bar->colors.inactive_workspace_text, argv[2]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_separator(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "separator", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if ((error = add_color("separator", config->current_bar->colors.separator, argv[0]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_statusline(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "statusline", EXPECTED_EQUAL_TO, 1))) { + return error; + } + + if ((error = add_color("statusline", config->current_bar->colors.statusline, argv[0]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_results *bar_colors_cmd_urgent_workspace(int argc, char **argv) { + struct cmd_results *error = NULL; + if ((error = checkarg(argc, "urgent_workspace", EXPECTED_EQUAL_TO, 3))) { + return error; + } + + if ((error = add_color("urgent_workspace_border", config->current_bar->colors.urgent_workspace_border, argv[0]))) { + return error; + } + + if ((error = add_color("urgent_workspace_bg", config->current_bar->colors.urgent_workspace_bg, argv[1]))) { + return error; + } + + if ((error = add_color("urgent_workspace_text", config->current_bar->colors.urgent_workspace_text, argv[2]))) { + return error; + } + + return cmd_results_new(CMD_SUCCESS, NULL, NULL); +} + +static struct cmd_handler bar_colors_handlers[] = { + { "active_workspace", bar_colors_cmd_active_workspace }, + { "background", bar_colors_cmd_background }, + { "binding_mode", bar_colors_cmd_binding_mode }, + { "focused_workspace", bar_colors_cmd_focused_workspace }, + { "inactive_workspace", bar_colors_cmd_inactive_workspace }, + { "separator", bar_colors_cmd_separator }, + { "statusline", bar_colors_cmd_statusline }, + { "urgent_workspace", bar_colors_cmd_urgent_workspace }, +}; + static int handler_compare(const void *_a, const void *_b) { const struct cmd_handler *a = _a; const struct cmd_handler *b = _b; @@ -1833,6 +2041,10 @@ static struct cmd_handler *find_handler(char *line, enum cmd_status block) { res = bsearch(&d, bar_handlers, sizeof(bar_handlers) / sizeof(struct cmd_handler), sizeof(struct cmd_handler), handler_compare); + } else if (block == CMD_BLOCK_BAR_COLORS){ + res = bsearch(&d, bar_colors_handlers, + sizeof(bar_colors_handlers) / sizeof(struct cmd_handler), + sizeof(struct cmd_handler), handler_compare); } else { res = bsearch(&d, handlers, sizeof(handlers) / sizeof(struct cmd_handler), diff --git a/sway/config.c b/sway/config.c index 1fff3a08..a0e95677 100644 --- a/sway/config.c +++ b/sway/config.c @@ -263,6 +263,14 @@ bool read_config(FILE *file, bool is_active) { } break; + case CMD_BLOCK_BAR_COLORS: + if (block == CMD_BLOCK_BAR) { + block = CMD_BLOCK_BAR_COLORS; + } else { + sway_log(L_ERROR, "Invalid block '%s'", line); + } + break; + case CMD_BLOCK_END: switch(block) { case CMD_BLOCK_MODE: @@ -277,6 +285,11 @@ bool read_config(FILE *file, bool is_active) { block = CMD_BLOCK_END; break; + case CMD_BLOCK_BAR_COLORS: + sway_log(L_DEBUG, "End of bar colors block"); + block = CMD_BLOCK_BAR; + break; + case CMD_BLOCK_END: sway_log(L_ERROR, "Unmatched }"); break; @@ -554,6 +567,26 @@ struct bar_config *default_bar_config(void) { bar->strip_workspace_numbers = false; bar->binding_mode_indicator = true; bar->tray_padding = 2; + // set default colors + strcpy(bar->colors.background, "#000000"); + strcpy(bar->colors.statusline, "#ffffff"); + strcpy(bar->colors.separator, "#666666"); + strcpy(bar->colors.focused_workspace_border, "#4c7899"); + strcpy(bar->colors.focused_workspace_bg, "#285577"); + strcpy(bar->colors.focused_workspace_text, "#ffffff"); + strcpy(bar->colors.active_workspace_border, "333333"); + strcpy(bar->colors.active_workspace_bg, "#5f676a"); + strcpy(bar->colors.active_workspace_text, "#ffffff"); + strcpy(bar->colors.inactive_workspace_border, "#333333"); + strcpy(bar->colors.inactive_workspace_bg,"#222222"); + strcpy(bar->colors.inactive_workspace_text, "#888888"); + strcpy(bar->colors.urgent_workspace_border, "#2f343a"); + strcpy(bar->colors.urgent_workspace_bg,"#900000"); + strcpy(bar->colors.urgent_workspace_text, "#ffffff"); + strcpy(bar->colors.binding_mode_border, "#2f343a"); + strcpy(bar->colors.binding_mode_bg,"#900000"); + strcpy(bar->colors.binding_mode_text, "#ffffff"); + list_add(config->bars, bar); return bar; From de1f625c27d090332918939ef584caf0bf938e45 Mon Sep 17 00:00:00 2001 From: Mikkel Oscar Lyderik Date: Tue, 15 Dec 2015 13:45:04 +0100 Subject: [PATCH 2/2] Add rgba support for bar colors --- include/config.h | 36 ++++++++++++++++++------------------ sway/commands.c | 14 ++++++++++---- sway/config.c | 36 ++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/include/config.h b/include/config.h index 2423c9ed..80340720 100644 --- a/include/config.h +++ b/include/config.h @@ -99,24 +99,24 @@ struct bar_config { bool binding_mode_indicator; bool verbose; struct { - char background[8]; - char statusline[8]; - char separator[8]; - char focused_workspace_border[8]; - char focused_workspace_bg[8]; - char focused_workspace_text[8]; - char active_workspace_border[8]; - char active_workspace_bg[8]; - char active_workspace_text[8]; - char inactive_workspace_border[8]; - char inactive_workspace_bg[8]; - char inactive_workspace_text[8]; - char urgent_workspace_border[8]; - char urgent_workspace_bg[8]; - char urgent_workspace_text[8]; - char binding_mode_border[8]; - char binding_mode_bg[8]; - char binding_mode_text[8]; + char background[10]; + char statusline[10]; + char separator[10]; + char focused_workspace_border[10]; + char focused_workspace_bg[10]; + char focused_workspace_text[10]; + char active_workspace_border[10]; + char active_workspace_bg[10]; + char active_workspace_text[10]; + char inactive_workspace_border[10]; + char inactive_workspace_bg[10]; + char inactive_workspace_text[10]; + char urgent_workspace_border[10]; + char urgent_workspace_bg[10]; + char urgent_workspace_text[10]; + char binding_mode_border[10]; + char binding_mode_bg[10]; + char binding_mode_text[10]; } colors; }; diff --git a/sway/commands.c b/sway/commands.c index d2b53783..733fb293 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -1851,7 +1851,8 @@ static struct cmd_handler bar_handlers[] = { * return error object, or NULL if color is valid. */ static struct cmd_results *add_color(const char *name, char *buffer, const char *color) { - if (strlen(color) != 7) { + int len = strlen(color); + if (len != 7 && len != 9 ) { return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); } @@ -1860,15 +1861,20 @@ static struct cmd_results *add_color(const char *name, char *buffer, const char } int i; - for (i = 1; i < 7; ++i) { + for (i = 1; i < len; ++i) { if (!isxdigit(color[i])) { return cmd_results_new(CMD_INVALID, name, "Invalid color definition %s", color); } } // copy color to buffer - strncpy(buffer, color, 7); - sway_log(L_DEBUG, "Setting %s color %s for bar: %s", name, color, config->current_bar->id); + strncpy(buffer, color, len); + // add default alpha channel if color was defined without it + if (len == 7) { + buffer[7] = 'f'; + buffer[8] = 'f'; + } + sway_log(L_DEBUG, "Setting %s color %s for bar: %s", name, buffer, config->current_bar->id); return NULL; } diff --git a/sway/config.c b/sway/config.c index a0e95677..e0c9151d 100644 --- a/sway/config.c +++ b/sway/config.c @@ -568,24 +568,24 @@ struct bar_config *default_bar_config(void) { bar->binding_mode_indicator = true; bar->tray_padding = 2; // set default colors - strcpy(bar->colors.background, "#000000"); - strcpy(bar->colors.statusline, "#ffffff"); - strcpy(bar->colors.separator, "#666666"); - strcpy(bar->colors.focused_workspace_border, "#4c7899"); - strcpy(bar->colors.focused_workspace_bg, "#285577"); - strcpy(bar->colors.focused_workspace_text, "#ffffff"); - strcpy(bar->colors.active_workspace_border, "333333"); - strcpy(bar->colors.active_workspace_bg, "#5f676a"); - strcpy(bar->colors.active_workspace_text, "#ffffff"); - strcpy(bar->colors.inactive_workspace_border, "#333333"); - strcpy(bar->colors.inactive_workspace_bg,"#222222"); - strcpy(bar->colors.inactive_workspace_text, "#888888"); - strcpy(bar->colors.urgent_workspace_border, "#2f343a"); - strcpy(bar->colors.urgent_workspace_bg,"#900000"); - strcpy(bar->colors.urgent_workspace_text, "#ffffff"); - strcpy(bar->colors.binding_mode_border, "#2f343a"); - strcpy(bar->colors.binding_mode_bg,"#900000"); - strcpy(bar->colors.binding_mode_text, "#ffffff"); + strcpy(bar->colors.background, "#000000ff"); + strcpy(bar->colors.statusline, "#ffffffff"); + strcpy(bar->colors.separator, "#666666ff"); + strcpy(bar->colors.focused_workspace_border, "#4c7899ff"); + strcpy(bar->colors.focused_workspace_bg, "#285577ff"); + strcpy(bar->colors.focused_workspace_text, "#ffffffff"); + strcpy(bar->colors.active_workspace_border, "333333ff"); + strcpy(bar->colors.active_workspace_bg, "#5f676aff"); + strcpy(bar->colors.active_workspace_text, "#ffffffff"); + strcpy(bar->colors.inactive_workspace_border, "#333333ff"); + strcpy(bar->colors.inactive_workspace_bg,"#222222ff"); + strcpy(bar->colors.inactive_workspace_text, "#888888ff"); + strcpy(bar->colors.urgent_workspace_border, "#2f343aff"); + strcpy(bar->colors.urgent_workspace_bg,"#900000ff"); + strcpy(bar->colors.urgent_workspace_text, "#ffffffff"); + strcpy(bar->colors.binding_mode_border, "#2f343aff"); + strcpy(bar->colors.binding_mode_bg,"#900000ff"); + strcpy(bar->colors.binding_mode_text, "#ffffffff"); list_add(config->bars, bar);