aboutsummaryrefslogtreecommitdiff
path: root/sway/commands
diff options
context:
space:
mode:
authorBrian Ashworth <bosrsf04@gmail.com>2019-12-28 04:01:38 -0500
committerSimon Ser <contact@emersion.fr>2019-12-28 10:07:25 +0100
commitf898ca9a83a54133017b76039894679dcccd2e30 (patch)
tree464814f27056889b0d2c22c306c0d904be6833af /sway/commands
parent66dc33296ca97b10f60daaff8c5e4b3f95fac8cd (diff)
bar_cmd_colors: remove add_color
This is the third commit in a series of commits to refactor color handling in sway. This removes add_color from commands.c. It was only being used by bar_cmd_colors. This also changes the functions to use parse_color which is used to validate rgb(a) colors throughout the code base and is also what i3bar is using to parse the colors after they are passed over ipc. After parsing the color and ensuring it is valid, the rgba hex string is then generated using snprintf. This refactor also ensures that all the colors for the command are valid before applying any of them.
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/bar/colors.c49
1 files changed, 33 insertions, 16 deletions
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 @@
#include <string.h>
#include "sway/commands.h"
+#include "log.h"
+#include "util.h"
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_colors_handlers[] = {
@@ -16,38 +18,53 @@ static struct cmd_handler bar_colors_handlers[] = {
{ "urgent_workspace", bar_colors_cmd_urgent_workspace },
};
+static char *hex_to_rgba_hex(const char *hex) {
+ uint32_t color;
+ if (!parse_color(hex, &color)) {
+ return NULL;
+ }
+ char *rgba = malloc(10);
+ snprintf(rgba, 10, "#%08x", color);
+ return rgba;
+}
+
static struct cmd_results *parse_single_color(char **color,
const char *cmd_name, int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 1))) {
return error;
}
- if (!*color && !(*color = malloc(10))) {
- return NULL;
- }
- error = add_color(*color, argv[0]);
- if (error) {
- return error;
+
+ char *rgba = hex_to_rgba_hex(argv[0]);
+ if (!*rgba) {
+ return cmd_results_new(CMD_INVALID, "Invalid color: %s", argv[0]);
}
+
+ free(*color);
+ *color = rgba;
return cmd_results_new(CMD_SUCCESS, NULL);
}
static struct cmd_results *parse_three_colors(char ***colors,
const char *cmd_name, int argc, char **argv) {
struct cmd_results *error = NULL;
- if (argc != 3) {
- return cmd_results_new(CMD_INVALID,
- "Command '%s' requires exactly three color values", cmd_name);
+ if ((error = checkarg(argc, cmd_name, EXPECTED_EQUAL_TO, 3))) {
+ return error;
}
- for (size_t i = 0; i < 3; i++) {
- if (!*colors[i] && !(*(colors[i]) = malloc(10))) {
- return NULL;
- }
- error = add_color(*(colors[i]), argv[i]);
- if (error) {
- return error;
+
+ char *rgba[3] = {0};
+ for (int i = 0; i < 3; i++) {
+ rgba[i] = hex_to_rgba_hex(argv[i]);
+ if (!rgba[i]) {
+ return cmd_results_new(CMD_INVALID, "Invalid color: %s", argv[i]);
}
}
+
+ for (int i = 0; i < 3; i++) {
+ free(*colors[i]);
+ *colors[i] = rgba[i];
+ }
+
return cmd_results_new(CMD_SUCCESS, NULL);
}