aboutsummaryrefslogtreecommitdiffstats
path: root/common/util.c
diff options
context:
space:
mode:
authorLibravatar Brian Ashworth <bosrsf04@gmail.com>2019-12-27 23:33:55 -0500
committerLibravatar Simon Ser <contact@emersion.fr>2019-12-28 10:07:25 +0100
commit97f9f0b699316ba60009b395948a712ec0b671d2 (patch)
treeb5916b36f4161c1c4d670295254d0f3fd9e793df /common/util.c
parentlayer-shell: refocus if keyboard interactive lost (diff)
downloadsway-97f9f0b699316ba60009b395948a712ec0b671d2.tar.gz
sway-97f9f0b699316ba60009b395948a712ec0b671d2.tar.zst
sway-97f9f0b699316ba60009b395948a712ec0b671d2.zip
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.
Diffstat (limited to 'common/util.c')
-rw-r--r--common/util.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/common/util.c b/common/util.c
index c0324b2f..84ebab99 100644
--- a/common/util.c
+++ b/common/util.c
@@ -1,4 +1,5 @@
1#define _POSIX_C_SOURCE 200809L 1#define _POSIX_C_SOURCE 200809L
2#include <ctype.h>
2#include <float.h> 3#include <float.h>
3#include <fcntl.h> 4#include <fcntl.h>
4#include <math.h> 5#include <math.h>
@@ -13,21 +14,21 @@ int wrap(int i, int max) {
13 return ((i % max) + max) % max; 14 return ((i % max) + max) % max;
14} 15}
15 16
16uint32_t parse_color(const char *color) { 17bool parse_color(const char *color, uint32_t *result) {
17 if (color[0] == '#') { 18 if (color[0] == '#') {
18 ++color; 19 ++color;
19 } 20 }
20
21 int len = strlen(color); 21 int len = strlen(color);
22 if (len != 6 && len != 8) { 22 if ((len != 6 && len != 8) || !isxdigit(color[0]) || !isxdigit(color[1])) {
23 sway_log(SWAY_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); 23 return false;
24 return 0xFFFFFFFF;
25 } 24 }
26 uint32_t res = (uint32_t)strtoul(color, NULL, 16); 25 char *ptr;
27 if (strlen(color) == 6) { 26 uint32_t parsed = strtoul(color, &ptr, 16);
28 res = (res << 8) | 0xFF; 27 if (*ptr != '\0') {
28 return false;
29 } 29 }
30 return res; 30 *result = len == 6 ? ((parsed << 8) | 0xFF) : parsed;
31 return true;
31} 32}
32 33
33bool parse_boolean(const char *boolean, bool current) { 34bool parse_boolean(const char *boolean, bool current) {