diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2019-12-27 23:56:11 -0500 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2019-12-28 10:07:25 +0100 |
commit | 66dc33296ca97b10f60daaff8c5e4b3f95fac8cd (patch) | |
tree | 95cb83ff86cf43d096df4f10a3cfb402d34315f7 /sway/commands | |
parent | parse_color: return success + drop fallback color (diff) | |
download | sway-66dc33296ca97b10f60daaff8c5e4b3f95fac8cd.tar.gz sway-66dc33296ca97b10f60daaff8c5e4b3f95fac8cd.tar.zst sway-66dc33296ca97b10f60daaff8c5e4b3f95fac8cd.zip |
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')
-rw-r--r-- | sway/commands/client.c | 87 |
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 @@ | |||
3 | #include "sway/config.h" | 3 | #include "sway/config.h" |
4 | #include "sway/output.h" | 4 | #include "sway/output.h" |
5 | #include "sway/tree/container.h" | 5 | #include "sway/tree/container.h" |
6 | #include "util.h" | ||
6 | 7 | ||
7 | static void rebuild_textures_iterator(struct sway_container *con, void *data) { | 8 | static void rebuild_textures_iterator(struct sway_container *con, void *data) { |
8 | container_update_marks_textures(con); | 9 | container_update_marks_textures(con); |
9 | container_update_title_textures(con); | 10 | container_update_title_textures(con); |
10 | } | 11 | } |
11 | 12 | ||
12 | /** | ||
13 | * Parse the hex string into an integer. | ||
14 | */ | ||
15 | static bool parse_color_int(char *hexstring, uint32_t *dest) { | ||
16 | if (hexstring[0] != '#') { | ||
17 | return false; | ||
18 | } | ||
19 | |||
20 | if (strlen(hexstring) != 7 && strlen(hexstring) != 9) { | ||
21 | return false; | ||
22 | } | ||
23 | |||
24 | ++hexstring; | ||
25 | char *end; | ||
26 | uint32_t decimal = strtol(hexstring, &end, 16); | ||
27 | |||
28 | if (*end != '\0') { | ||
29 | return false; | ||
30 | } | ||
31 | |||
32 | if (strlen(hexstring) == 6) { | ||
33 | // Add alpha | ||
34 | decimal = (decimal << 8) | 0xff; | ||
35 | } | ||
36 | |||
37 | *dest = decimal; | ||
38 | return true; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Parse the hex string into a float value. | ||
43 | */ | ||
44 | static bool parse_color_float(char *hexstring, float dest[static 4]) { | ||
45 | uint32_t decimal; | ||
46 | if (!parse_color_int(hexstring, &decimal)) { | ||
47 | return false; | ||
48 | } | ||
49 | dest[0] = ((decimal >> 24) & 0xff) / 255.0; | ||
50 | dest[1] = ((decimal >> 16) & 0xff) / 255.0; | ||
51 | dest[2] = ((decimal >> 8) & 0xff) / 255.0; | ||
52 | dest[3] = (decimal & 0xff) / 255.0; | ||
53 | return true; | ||
54 | } | ||
55 | |||
56 | static struct cmd_results *handle_command(int argc, char **argv, | 13 | static struct cmd_results *handle_command(int argc, char **argv, |
57 | struct border_colors *class, char *cmd_name) { | 14 | struct border_colors *class, char *cmd_name) { |
58 | struct cmd_results *error = NULL; | 15 | struct cmd_results *error = NULL; |
@@ -60,30 +17,28 @@ static struct cmd_results *handle_command(int argc, char **argv, | |||
60 | return error; | 17 | return error; |
61 | } | 18 | } |
62 | 19 | ||
63 | if (!parse_color_float(argv[0], class->border)) { | 20 | struct border_colors colors = {0}; |
64 | return cmd_results_new(CMD_INVALID, | 21 | |
65 | "Unable to parse border color '%s'", argv[0]); | 22 | struct { |
66 | } | 23 | const char *name; |
67 | 24 | float *rgba[4]; | |
68 | if (!parse_color_float(argv[1], class->background)) { | 25 | } properties[] = { |
69 | return cmd_results_new(CMD_INVALID, | 26 | { "border", colors.border }, |
70 | "Unable to parse background color '%s'", argv[1]); | 27 | { "background", colors.background }, |
71 | } | 28 | { "text", colors.text }, |
72 | 29 | { "indicator", colors.indicator }, | |
73 | if (!parse_color_float(argv[2], class->text)) { | 30 | { "child_border", colors.child_border } |
74 | return cmd_results_new(CMD_INVALID, | 31 | }; |
75 | "Unable to parse text color '%s'", argv[2]); | 32 | for (size_t i = 0; i < sizeof(properties) / sizeof(properties[0]); i++) { |
76 | } | 33 | uint32_t color; |
77 | 34 | if (!parse_color(argv[i], &color)) { | |
78 | if (!parse_color_float(argv[3], class->indicator)) { | 35 | return cmd_results_new(CMD_INVALID, |
79 | return cmd_results_new(CMD_INVALID, | 36 | "Invalid %s color %s", properties[i].name, argv[i]); |
80 | "Unable to parse indicator color '%s'", argv[3]); | 37 | } |
38 | color_to_rgba(*properties[i].rgba, color); | ||
81 | } | 39 | } |
82 | 40 | ||
83 | if (!parse_color_float(argv[4], class->child_border)) { | 41 | memcpy(class, &colors, sizeof(struct border_colors)); |
84 | return cmd_results_new(CMD_INVALID, | ||
85 | "Unable to parse child border color '%s'", argv[4]); | ||
86 | } | ||
87 | 42 | ||
88 | if (config->active) { | 43 | if (config->active) { |
89 | root_for_each_container(rebuild_textures_iterator, NULL); | 44 | root_for_each_container(rebuild_textures_iterator, NULL); |