cmd_client_*: refactor duplicated code

This is the second in a series of commits to refactor the color handling
in sway. This removes the duplicated color parsing code in
sway/commands/client.c. Additionally, this combines the parsing of
colors to float arrays with that in sway/config.c and introduces a
color_to_rgba function in commom/util.c.

As an added bonus, this also makes it so non of the colors in a border
color class will be changed unless all of the colors specified are
valid. This ensures that an invalid command does not get partially
applied.
This commit is contained in:
Brian Ashworth 2019-12-27 23:56:11 -05:00 committed by Simon Ser
parent 97f9f0b699
commit 66dc33296c
4 changed files with 57 additions and 99 deletions

View file

@ -3,56 +3,13 @@
#include "sway/config.h"
#include "sway/output.h"
#include "sway/tree/container.h"
#include "util.h"
static void rebuild_textures_iterator(struct sway_container *con, void *data) {
container_update_marks_textures(con);
container_update_title_textures(con);
}
/**
* Parse the hex string into an integer.
*/
static bool parse_color_int(char *hexstring, uint32_t *dest) {
if (hexstring[0] != '#') {
return false;
}
if (strlen(hexstring) != 7 && strlen(hexstring) != 9) {
return false;
}
++hexstring;
char *end;
uint32_t decimal = strtol(hexstring, &end, 16);
if (*end != '\0') {
return false;
}
if (strlen(hexstring) == 6) {
// Add alpha
decimal = (decimal << 8) | 0xff;
}
*dest = decimal;
return true;
}
/**
* Parse the hex string into a float value.
*/
static bool parse_color_float(char *hexstring, float dest[static 4]) {
uint32_t decimal;
if (!parse_color_int(hexstring, &decimal)) {
return false;
}
dest[0] = ((decimal >> 24) & 0xff) / 255.0;
dest[1] = ((decimal >> 16) & 0xff) / 255.0;
dest[2] = ((decimal >> 8) & 0xff) / 255.0;
dest[3] = (decimal & 0xff) / 255.0;
return true;
}
static struct cmd_results *handle_command(int argc, char **argv,
struct border_colors *class, char *cmd_name) {
struct cmd_results *error = NULL;
@ -60,30 +17,28 @@ static struct cmd_results *handle_command(int argc, char **argv,
return error;
}
if (!parse_color_float(argv[0], class->border)) {
return cmd_results_new(CMD_INVALID,
"Unable to parse border color '%s'", argv[0]);
struct border_colors colors = {0};
struct {
const char *name;
float *rgba[4];
} properties[] = {
{ "border", colors.border },
{ "background", colors.background },
{ "text", colors.text },
{ "indicator", colors.indicator },
{ "child_border", colors.child_border }
};
for (size_t i = 0; i < sizeof(properties) / sizeof(properties[0]); i++) {
uint32_t color;
if (!parse_color(argv[i], &color)) {
return cmd_results_new(CMD_INVALID,
"Invalid %s color %s", properties[i].name, argv[i]);
}
color_to_rgba(*properties[i].rgba, color);
}
if (!parse_color_float(argv[1], class->background)) {
return cmd_results_new(CMD_INVALID,
"Unable to parse background color '%s'", argv[1]);
}
if (!parse_color_float(argv[2], class->text)) {
return cmd_results_new(CMD_INVALID,
"Unable to parse text color '%s'", argv[2]);
}
if (!parse_color_float(argv[3], class->indicator)) {
return cmd_results_new(CMD_INVALID,
"Unable to parse indicator color '%s'", argv[3]);
}
if (!parse_color_float(argv[4], class->child_border)) {
return cmd_results_new(CMD_INVALID,
"Unable to parse child border color '%s'", argv[4]);
}
memcpy(class, &colors, sizeof(struct border_colors));
if (config->active) {
root_for_each_container(rebuild_textures_iterator, NULL);