diff options
-rw-r--r-- | include/sway/commands.h | 2 | ||||
-rw-r--r-- | sway/commands.c | 31 | ||||
-rw-r--r-- | sway/commands/bar/colors.c | 49 |
3 files changed, 33 insertions, 49 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h index 982125c1..f992b441 100644 --- a/include/sway/commands.h +++ b/include/sway/commands.h | |||
@@ -88,8 +88,6 @@ void free_cmd_results(struct cmd_results *results); | |||
88 | */ | 88 | */ |
89 | char *cmd_results_to_json(list_t *res_list); | 89 | char *cmd_results_to_json(list_t *res_list); |
90 | 90 | ||
91 | struct cmd_results *add_color(char *buffer, const char *color); | ||
92 | |||
93 | /** | 91 | /** |
94 | * TODO: Move this function and its dependent functions to container.c. | 92 | * TODO: Move this function and its dependent functions to container.c. |
95 | */ | 93 | */ |
diff --git a/sway/commands.c b/sway/commands.c index e7f1eafe..751dbe9c 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -567,34 +567,3 @@ char *cmd_results_to_json(list_t *res_list) { | |||
567 | json_object_put(result_array); | 567 | json_object_put(result_array); |
568 | return res; | 568 | return res; |
569 | } | 569 | } |
570 | |||
571 | /** | ||
572 | * Check and add color to buffer. | ||
573 | * | ||
574 | * return error object, or NULL if color is valid. | ||
575 | */ | ||
576 | struct cmd_results *add_color(char *buffer, const char *color) { | ||
577 | int len = strlen(color); | ||
578 | if (len != 7 && len != 9) { | ||
579 | return cmd_results_new(CMD_INVALID, | ||
580 | "Invalid color definition %s", color); | ||
581 | } | ||
582 | if (color[0] != '#') { | ||
583 | return cmd_results_new(CMD_INVALID, | ||
584 | "Invalid color definition %s", color); | ||
585 | } | ||
586 | for (int i = 1; i < len; ++i) { | ||
587 | if (!isxdigit(color[i])) { | ||
588 | return cmd_results_new(CMD_INVALID, | ||
589 | "Invalid color definition %s", color); | ||
590 | } | ||
591 | } | ||
592 | strcpy(buffer, color); | ||
593 | // add default alpha channel if color was defined without it | ||
594 | if (len == 7) { | ||
595 | buffer[7] = 'f'; | ||
596 | buffer[8] = 'f'; | ||
597 | } | ||
598 | buffer[9] = '\0'; | ||
599 | return NULL; | ||
600 | } | ||
diff --git a/sway/commands/bar/colors.c b/sway/commands/bar/colors.c index 7921db0d..ea2b912d 100644 --- a/sway/commands/bar/colors.c +++ b/sway/commands/bar/colors.c | |||
@@ -1,5 +1,7 @@ | |||
1 | #include <string.h> | 1 | #include <string.h> |
2 | #include "sway/commands.h" | 2 | #include "sway/commands.h" |
3 | #include "log.h" | ||
4 | #include "util.h" | ||
3 | 5 | ||
4 | // Must be in alphabetical order for bsearch | 6 | // Must be in alphabetical order for bsearch |
5 | static struct cmd_handler bar_colors_handlers[] = { | 7 | static struct cmd_handler bar_colors_handlers[] = { |
@@ -16,38 +18,53 @@ static struct cmd_handler bar_colors_handlers[] = { | |||
16 | { "urgent_workspace", bar_colors_cmd_urgent_workspace }, | 18 | { "urgent_workspace", bar_colors_cmd_urgent_workspace }, |
17 | }; | 19 | }; |
18 | 20 | ||
21 | static char *hex_to_rgba_hex(const char *hex) { | ||
22 | uint32_t color; | ||
23 | if (!parse_color(hex, &color)) { | ||
24 | return NULL; | ||
25 | } | ||
26 | char *rgba = malloc(10); | ||
27 | snprintf(rgba, 10, "#%08x", color); | ||
28 | return rgba; | ||
29 | } | ||
30 | |||
19 | static struct cmd_results *parse_single_color(char **color, | 31 | static struct cmd_results *parse_single_color(char **color, |
20 | const char *cmd_name, int argc, char **argv) { | 32 | const char *cmd_name, int argc, char **argv) { |
21 | struct cmd_results *error = NULL; | 33 | struct cmd_results *error = NULL; |
22 | if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { | 34 | if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) { |
23 | return error; | 35 | return error; |
24 | } | 36 | } |
25 | if (!*color && !(*color = malloc(10))) { | 37 | |
26 | return NULL; | 38 | char *rgba = hex_to_rgba_hex(argv[0]); |
27 | } | 39 | if (!*rgba) { |
28 | error = add_color(*color, argv[0]); | 40 | return cmd_results_new(CMD_INVALID, "Invalid color: %s", argv[0]); |
29 | if (error) { | ||
30 | return error; | ||
31 | } | 41 | } |
42 | |||
43 | free(*color); | ||
44 | *color = rgba; | ||
32 | return cmd_results_new(CMD_SUCCESS, NULL); | 45 | return cmd_results_new(CMD_SUCCESS, NULL); |
33 | } | 46 | } |
34 | 47 | ||
35 | static struct cmd_results *parse_three_colors(char ***colors, | 48 | static struct cmd_results *parse_three_colors(char ***colors, |
36 | const char *cmd_name, int argc, char **argv) { | 49 | const char *cmd_name, int argc, char **argv) { |
37 | struct cmd_results *error = NULL; | 50 | struct cmd_results *error = NULL; |
38 | if (argc != 3) { | 51 | if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 3))) { |
39 | return cmd_results_new(CMD_INVALID, | 52 | return error; |
40 | "Command '%s' requires exactly three color values", cmd_name); | ||
41 | } | 53 | } |
42 | for (size_t i = 0; i < 3; i++) { | 54 | |
43 | if (!*colors[i] && !(*(colors[i]) = malloc(10))) { | 55 | char *rgba[3] = {0}; |
44 | return NULL; | 56 | for (int i = 0; i < 3; i++) { |
45 | } | 57 | rgba[i] = hex_to_rgba_hex(argv[i]); |
46 | error = add_color(*(colors[i]), argv[i]); | 58 | if (!rgba[i]) { |
47 | if (error) { | 59 | return cmd_results_new(CMD_INVALID, "Invalid color: %s", argv[i]); |
48 | return error; | ||
49 | } | 60 | } |
50 | } | 61 | } |
62 | |||
63 | for (int i = 0; i < 3; i++) { | ||
64 | free(*colors[i]); | ||
65 | *colors[i] = rgba[i]; | ||
66 | } | ||
67 | |||
51 | return cmd_results_new(CMD_SUCCESS, NULL); | 68 | return cmd_results_new(CMD_SUCCESS, NULL); |
52 | } | 69 | } |
53 | 70 | ||