mirror of
https://github.com/gwenhael-le-moine/sway-patched-tray-menu.git
synced 2024-12-26 21:58:08 +01:00
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:
parent
97f9f0b699
commit
66dc33296c
4 changed files with 57 additions and 99 deletions
|
@ -31,6 +31,13 @@ bool parse_color(const char *color, uint32_t *result) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void color_to_rgba(float dest[static 4], uint32_t color) {
|
||||||
|
dest[0] = ((color >> 24) & 0xff) / 255.0;
|
||||||
|
dest[1] = ((color >> 16) & 0xff) / 255.0;
|
||||||
|
dest[2] = ((color >> 8) & 0xff) / 255.0;
|
||||||
|
dest[3] = (color & 0xff) / 255.0;
|
||||||
|
}
|
||||||
|
|
||||||
bool parse_boolean(const char *boolean, bool current) {
|
bool parse_boolean(const char *boolean, bool current) {
|
||||||
if (strcasecmp(boolean, "1") == 0
|
if (strcasecmp(boolean, "1") == 0
|
||||||
|| strcasecmp(boolean, "yes") == 0
|
|| strcasecmp(boolean, "yes") == 0
|
||||||
|
|
|
@ -17,6 +17,8 @@ int wrap(int i, int max);
|
||||||
*/
|
*/
|
||||||
bool parse_color(const char *color, uint32_t *result);
|
bool parse_color(const char *color, uint32_t *result);
|
||||||
|
|
||||||
|
void color_to_rgba(float dest[static 4], uint32_t color);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given a string that represents a boolean, return the boolean value. This
|
* Given a string that represents a boolean, return the boolean value. This
|
||||||
* function also takes in the current boolean value to support toggling. If
|
* function also takes in the current boolean value to support toggling. If
|
||||||
|
|
|
@ -3,56 +3,13 @@
|
||||||
#include "sway/config.h"
|
#include "sway/config.h"
|
||||||
#include "sway/output.h"
|
#include "sway/output.h"
|
||||||
#include "sway/tree/container.h"
|
#include "sway/tree/container.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
static void rebuild_textures_iterator(struct sway_container *con, void *data) {
|
static void rebuild_textures_iterator(struct sway_container *con, void *data) {
|
||||||
container_update_marks_textures(con);
|
container_update_marks_textures(con);
|
||||||
container_update_title_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,
|
static struct cmd_results *handle_command(int argc, char **argv,
|
||||||
struct border_colors *class, char *cmd_name) {
|
struct border_colors *class, char *cmd_name) {
|
||||||
struct cmd_results *error = NULL;
|
struct cmd_results *error = NULL;
|
||||||
|
@ -60,30 +17,28 @@ static struct cmd_results *handle_command(int argc, char **argv,
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parse_color_float(argv[0], class->border)) {
|
struct border_colors colors = {0};
|
||||||
return cmd_results_new(CMD_INVALID,
|
|
||||||
"Unable to parse border color '%s'", argv[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)) {
|
memcpy(class, &colors, sizeof(struct border_colors));
|
||||||
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]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config->active) {
|
if (config->active) {
|
||||||
root_for_each_container(rebuild_textures_iterator, NULL);
|
root_for_each_container(rebuild_textures_iterator, NULL);
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include "stringop.h"
|
#include "stringop.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
struct sway_config *config = NULL;
|
struct sway_config *config = NULL;
|
||||||
|
|
||||||
|
@ -192,13 +193,6 @@ static void destroy_removed_seats(struct sway_config *old_config,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void set_color(float dest[static 4], uint32_t color) {
|
|
||||||
dest[0] = ((color >> 16) & 0xff) / 255.0;
|
|
||||||
dest[1] = ((color >> 8) & 0xff) / 255.0;
|
|
||||||
dest[2] = (color & 0xff) / 255.0;
|
|
||||||
dest[3] = 1.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void config_defaults(struct sway_config *config) {
|
static void config_defaults(struct sway_config *config) {
|
||||||
if (!(config->swaynag_command = strdup("swaynag"))) goto cleanup;
|
if (!(config->swaynag_command = strdup("swaynag"))) goto cleanup;
|
||||||
config->swaynag_config_errors = (struct swaynag_instance){0};
|
config->swaynag_config_errors = (struct swaynag_instance){0};
|
||||||
|
@ -300,37 +294,37 @@ static void config_defaults(struct sway_config *config) {
|
||||||
config->hide_lone_tab = false;
|
config->hide_lone_tab = false;
|
||||||
|
|
||||||
// border colors
|
// border colors
|
||||||
set_color(config->border_colors.focused.border, 0x4C7899);
|
color_to_rgba(config->border_colors.focused.border, 0x4C7899FF);
|
||||||
set_color(config->border_colors.focused.background, 0x285577);
|
color_to_rgba(config->border_colors.focused.background, 0x285577FF);
|
||||||
set_color(config->border_colors.focused.text, 0xFFFFFFFF);
|
color_to_rgba(config->border_colors.focused.text, 0xFFFFFFFF);
|
||||||
set_color(config->border_colors.focused.indicator, 0x2E9EF4);
|
color_to_rgba(config->border_colors.focused.indicator, 0x2E9EF4FF);
|
||||||
set_color(config->border_colors.focused.child_border, 0x285577);
|
color_to_rgba(config->border_colors.focused.child_border, 0x285577FF);
|
||||||
|
|
||||||
set_color(config->border_colors.focused_inactive.border, 0x333333);
|
color_to_rgba(config->border_colors.focused_inactive.border, 0x333333FF);
|
||||||
set_color(config->border_colors.focused_inactive.background, 0x5F676A);
|
color_to_rgba(config->border_colors.focused_inactive.background, 0x5F676AFF);
|
||||||
set_color(config->border_colors.focused_inactive.text, 0xFFFFFFFF);
|
color_to_rgba(config->border_colors.focused_inactive.text, 0xFFFFFFFF);
|
||||||
set_color(config->border_colors.focused_inactive.indicator, 0x484E50);
|
color_to_rgba(config->border_colors.focused_inactive.indicator, 0x484E50FF);
|
||||||
set_color(config->border_colors.focused_inactive.child_border, 0x5F676A);
|
color_to_rgba(config->border_colors.focused_inactive.child_border, 0x5F676AFF);
|
||||||
|
|
||||||
set_color(config->border_colors.unfocused.border, 0x333333);
|
color_to_rgba(config->border_colors.unfocused.border, 0x333333FF);
|
||||||
set_color(config->border_colors.unfocused.background, 0x222222);
|
color_to_rgba(config->border_colors.unfocused.background, 0x222222FF);
|
||||||
set_color(config->border_colors.unfocused.text, 0x88888888);
|
color_to_rgba(config->border_colors.unfocused.text, 0x88888888);
|
||||||
set_color(config->border_colors.unfocused.indicator, 0x292D2E);
|
color_to_rgba(config->border_colors.unfocused.indicator, 0x292D2EFF);
|
||||||
set_color(config->border_colors.unfocused.child_border, 0x222222);
|
color_to_rgba(config->border_colors.unfocused.child_border, 0x222222FF);
|
||||||
|
|
||||||
set_color(config->border_colors.urgent.border, 0x2F343A);
|
color_to_rgba(config->border_colors.urgent.border, 0x2F343AFF);
|
||||||
set_color(config->border_colors.urgent.background, 0x900000);
|
color_to_rgba(config->border_colors.urgent.background, 0x900000FF);
|
||||||
set_color(config->border_colors.urgent.text, 0xFFFFFFFF);
|
color_to_rgba(config->border_colors.urgent.text, 0xFFFFFFFF);
|
||||||
set_color(config->border_colors.urgent.indicator, 0x900000);
|
color_to_rgba(config->border_colors.urgent.indicator, 0x900000FF);
|
||||||
set_color(config->border_colors.urgent.child_border, 0x900000);
|
color_to_rgba(config->border_colors.urgent.child_border, 0x900000FF);
|
||||||
|
|
||||||
set_color(config->border_colors.placeholder.border, 0x000000);
|
color_to_rgba(config->border_colors.placeholder.border, 0x000000FF);
|
||||||
set_color(config->border_colors.placeholder.background, 0x0C0C0C);
|
color_to_rgba(config->border_colors.placeholder.background, 0x0C0C0CFF);
|
||||||
set_color(config->border_colors.placeholder.text, 0xFFFFFFFF);
|
color_to_rgba(config->border_colors.placeholder.text, 0xFFFFFFFF);
|
||||||
set_color(config->border_colors.placeholder.indicator, 0x000000);
|
color_to_rgba(config->border_colors.placeholder.indicator, 0x000000FF);
|
||||||
set_color(config->border_colors.placeholder.child_border, 0x0C0C0C);
|
color_to_rgba(config->border_colors.placeholder.child_border, 0x0C0C0CFF);
|
||||||
|
|
||||||
set_color(config->border_colors.background, 0xFFFFFF);
|
color_to_rgba(config->border_colors.background, 0xFFFFFFFF);
|
||||||
|
|
||||||
// Security
|
// Security
|
||||||
if (!(config->command_policies = create_list())) goto cleanup;
|
if (!(config->command_policies = create_list())) goto cleanup;
|
||||||
|
|
Loading…
Reference in a new issue