aboutsummaryrefslogtreecommitdiff
path: root/sway/commands/client.c
diff options
context:
space:
mode:
authorBrian Ashworth <bosrsf04@gmail.com>2019-12-27 23:56:11 -0500
committerSimon Ser <contact@emersion.fr>2019-12-28 10:07:25 +0100
commit66dc33296ca97b10f60daaff8c5e4b3f95fac8cd (patch)
tree95cb83ff86cf43d096df4f10a3cfb402d34315f7 /sway/commands/client.c
parent97f9f0b699316ba60009b395948a712ec0b671d2 (diff)
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.
Diffstat (limited to 'sway/commands/client.c')
-rw-r--r--sway/commands/client.c87
1 files changed, 21 insertions, 66 deletions
diff --git a/sway/commands/client.c b/sway/commands/client.c
index f5c7d90f..e4282341 100644
--- a/sway/commands/client.c
+++ b/sway/commands/client.c
@@ -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]);
- }
-
- 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]);
+ 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[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);