Implement bar option: colors {}

This commit is contained in:
Mikkel Oscar Lyderik 2015-12-15 13:12:08 +01:00
parent cfba09a4ac
commit 8c1d56812b
3 changed files with 264 additions and 12 deletions

View file

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

View file

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

View file

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