parse_color: return success + drop fallback color

This is the first in a series of commits to refactor the color handling
in sway. This changes parse_color to return whether it was success and
no longer uses 0xFFFFFFFF as the fallback color. This also verifies that
the string actually contains a valid hexadecimal number along with
the length checks.

In the process of altering the calls to parse_color, I also took the
opportunity to heavily refactor swaybar's ipc_parse_colors function.
This allowed for several lines of duplicated code to be removed.
This commit is contained in:
Brian Ashworth 2019-12-27 23:33:55 -05:00 committed by Simon Ser
parent 088b374b1a
commit 97f9f0b699
7 changed files with 76 additions and 142 deletions

View file

@ -9,7 +9,8 @@ struct i3bar_block {
int ref_count;
char *full_text, *short_text, *align, *min_width_str;
bool urgent;
uint32_t *color;
uint32_t color;
bool color_set;
int min_width;
char *name, *instance;
bool separator;

View file

@ -11,10 +11,11 @@
int wrap(int i, int max);
/**
* Given a string that represents an RGB(A) color, return a uint32_t
* version of the color.
* Given a string that represents an RGB(A) color, result will be set to a
* uint32_t version of the color, as long as it is valid. If it is invalid,
* then false will be returned and result will be untouched.
*/
uint32_t parse_color(const char *color);
bool parse_color(const char *color, uint32_t *result);
/**
* Given a string that represents a boolean, return the boolean value. This