diff options
author | Brian Ashworth <bosrsf04@gmail.com> | 2019-12-27 23:33:55 -0500 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2019-12-28 10:07:25 +0100 |
commit | 97f9f0b699316ba60009b395948a712ec0b671d2 (patch) | |
tree | b5916b36f4161c1c4d670295254d0f3fd9e793df /swaybar/ipc.c | |
parent | layer-shell: refocus if keyboard interactive lost (diff) | |
download | sway-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 'swaybar/ipc.c')
-rw-r--r-- | swaybar/ipc.c | 147 |
1 files changed, 36 insertions, 111 deletions
diff --git a/swaybar/ipc.c b/swaybar/ipc.c index afaffb04..cca510c6 100644 --- a/swaybar/ipc.c +++ b/swaybar/ipc.c | |||
@@ -55,117 +55,42 @@ char *parse_font(const char *font) { | |||
55 | 55 | ||
56 | static void ipc_parse_colors( | 56 | static void ipc_parse_colors( |
57 | struct swaybar_config *config, json_object *colors) { | 57 | struct swaybar_config *config, json_object *colors) { |
58 | json_object *background, *statusline, *separator; | 58 | struct { |
59 | json_object *focused_background, *focused_statusline, *focused_separator; | 59 | const char *name; |
60 | json_object *focused_workspace_border, *focused_workspace_bg, *focused_workspace_text; | 60 | uint32_t *color; |
61 | json_object *inactive_workspace_border, *inactive_workspace_bg, *inactive_workspace_text; | 61 | } properties[] = { |
62 | json_object *active_workspace_border, *active_workspace_bg, *active_workspace_text; | 62 | { "background", &config->colors.background }, |
63 | json_object *urgent_workspace_border, *urgent_workspace_bg, *urgent_workspace_text; | 63 | { "statusline", &config->colors.statusline }, |
64 | json_object *binding_mode_border, *binding_mode_bg, *binding_mode_text; | 64 | { "separator", &config->colors.separator }, |
65 | json_object_object_get_ex(colors, "background", &background); | 65 | { "focused_background", &config->colors.focused_background }, |
66 | json_object_object_get_ex(colors, "statusline", &statusline); | 66 | { "focused_statusline", &config->colors.focused_statusline }, |
67 | json_object_object_get_ex(colors, "separator", &separator); | 67 | { "focused_separator", &config->colors.focused_separator }, |
68 | json_object_object_get_ex(colors, "focused_background", &focused_background); | 68 | { "focused_workspace_border", &config->colors.focused_workspace.border }, |
69 | json_object_object_get_ex(colors, "focused_statusline", &focused_statusline); | 69 | { "focused_workspace_bg", &config->colors.focused_workspace.background }, |
70 | json_object_object_get_ex(colors, "focused_separator", &focused_separator); | 70 | { "focused_workspace_text", &config->colors.focused_workspace.text }, |
71 | json_object_object_get_ex(colors, "focused_workspace_border", &focused_workspace_border); | 71 | { "active_workspace_border", &config->colors.active_workspace.border }, |
72 | json_object_object_get_ex(colors, "focused_workspace_bg", &focused_workspace_bg); | 72 | { "active_workspace_bg", &config->colors.active_workspace.background }, |
73 | json_object_object_get_ex(colors, "focused_workspace_text", &focused_workspace_text); | 73 | { "active_workspace_text", &config->colors.active_workspace.text }, |
74 | json_object_object_get_ex(colors, "active_workspace_border", &active_workspace_border); | 74 | { "inactive_workspace_border", &config->colors.inactive_workspace.border }, |
75 | json_object_object_get_ex(colors, "active_workspace_bg", &active_workspace_bg); | 75 | { "inactive_workspace_bg", &config->colors.inactive_workspace.background }, |
76 | json_object_object_get_ex(colors, "active_workspace_text", &active_workspace_text); | 76 | { "inactive_workspace_text", &config->colors.inactive_workspace.text }, |
77 | json_object_object_get_ex(colors, "inactive_workspace_border", &inactive_workspace_border); | 77 | { "urgent_workspace_border", &config->colors.urgent_workspace.border }, |
78 | json_object_object_get_ex(colors, "inactive_workspace_bg", &inactive_workspace_bg); | 78 | { "urgent_workspace_bg", &config->colors.urgent_workspace.background }, |
79 | json_object_object_get_ex(colors, "inactive_workspace_text", &inactive_workspace_text); | 79 | { "urgent_workspace_text", &config->colors.urgent_workspace.text }, |
80 | json_object_object_get_ex(colors, "urgent_workspace_border", &urgent_workspace_border); | 80 | { "binding_mode_border", &config->colors.binding_mode.border }, |
81 | json_object_object_get_ex(colors, "urgent_workspace_bg", &urgent_workspace_bg); | 81 | { "binding_mode_bg", &config->colors.binding_mode.background }, |
82 | json_object_object_get_ex(colors, "urgent_workspace_text", &urgent_workspace_text); | 82 | { "binding_mode_text", &config->colors.binding_mode.text }, |
83 | json_object_object_get_ex(colors, "binding_mode_border", &binding_mode_border); | 83 | }; |
84 | json_object_object_get_ex(colors, "binding_mode_bg", &binding_mode_bg); | 84 | |
85 | json_object_object_get_ex(colors, "binding_mode_text", &binding_mode_text); | 85 | for (size_t i = 0; i < sizeof(properties) / sizeof(properties[i]); i++) { |
86 | if (background) { | 86 | json_object *object; |
87 | config->colors.background = parse_color( | 87 | if (json_object_object_get_ex(colors, properties[i].name, &object)) { |
88 | json_object_get_string(background)); | 88 | const char *hexstring = json_object_get_string(object); |
89 | } | 89 | if (!parse_color(hexstring, properties[i].color)) { |
90 | if (statusline) { | 90 | sway_log(SWAY_ERROR, "Ignoring invalid %s: %s", |
91 | config->colors.statusline = parse_color( | 91 | properties[i].name, hexstring); |
92 | json_object_get_string(statusline)); | 92 | } |
93 | } | 93 | } |
94 | if (separator) { | ||
95 | config->colors.separator = parse_color( | ||
96 | json_object_get_string(separator)); | ||
97 | } | ||
98 | if (focused_background) { | ||
99 | config->colors.focused_background = parse_color( | ||
100 | json_object_get_string(focused_background)); | ||
101 | } | ||
102 | if (focused_statusline) { | ||
103 | config->colors.focused_statusline = parse_color( | ||
104 | json_object_get_string(focused_statusline)); | ||
105 | } | ||
106 | if (focused_separator) { | ||
107 | config->colors.focused_separator = parse_color( | ||
108 | json_object_get_string(focused_separator)); | ||
109 | } | ||
110 | if (focused_workspace_border) { | ||
111 | config->colors.focused_workspace.border = parse_color( | ||
112 | json_object_get_string(focused_workspace_border)); | ||
113 | } | ||
114 | if (focused_workspace_bg) { | ||
115 | config->colors.focused_workspace.background = parse_color( | ||
116 | json_object_get_string(focused_workspace_bg)); | ||
117 | } | ||
118 | if (focused_workspace_text) { | ||
119 | config->colors.focused_workspace.text = parse_color( | ||
120 | json_object_get_string(focused_workspace_text)); | ||
121 | } | ||
122 | if (active_workspace_border) { | ||
123 | config->colors.active_workspace.border = parse_color( | ||
124 | json_object_get_string(active_workspace_border)); | ||
125 | } | ||
126 | if (active_workspace_bg) { | ||
127 | config->colors.active_workspace.background = parse_color( | ||
128 | json_object_get_string(active_workspace_bg)); | ||
129 | } | ||
130 | if (active_workspace_text) { | ||
131 | config->colors.active_workspace.text = parse_color( | ||
132 | json_object_get_string(active_workspace_text)); | ||
133 | } | ||
134 | if (inactive_workspace_border) { | ||
135 | config->colors.inactive_workspace.border = parse_color( | ||
136 | json_object_get_string(inactive_workspace_border)); | ||
137 | } | ||
138 | if (inactive_workspace_bg) { | ||
139 | config->colors.inactive_workspace.background = parse_color( | ||
140 | json_object_get_string(inactive_workspace_bg)); | ||
141 | } | ||
142 | if (inactive_workspace_text) { | ||
143 | config->colors.inactive_workspace.text = parse_color( | ||
144 | json_object_get_string(inactive_workspace_text)); | ||
145 | } | ||
146 | if (urgent_workspace_border) { | ||
147 | config->colors.urgent_workspace.border = parse_color( | ||
148 | json_object_get_string(urgent_workspace_border)); | ||
149 | } | ||
150 | if (urgent_workspace_bg) { | ||
151 | config->colors.urgent_workspace.background = parse_color( | ||
152 | json_object_get_string(urgent_workspace_bg)); | ||
153 | } | ||
154 | if (urgent_workspace_text) { | ||
155 | config->colors.urgent_workspace.text = parse_color( | ||
156 | json_object_get_string(urgent_workspace_text)); | ||
157 | } | ||
158 | if (binding_mode_border) { | ||
159 | config->colors.binding_mode.border = parse_color( | ||
160 | json_object_get_string(binding_mode_border)); | ||
161 | } | ||
162 | if (binding_mode_bg) { | ||
163 | config->colors.binding_mode.background = parse_color( | ||
164 | json_object_get_string(binding_mode_bg)); | ||
165 | } | ||
166 | if (binding_mode_text) { | ||
167 | config->colors.binding_mode.text = parse_color( | ||
168 | json_object_get_string(binding_mode_text)); | ||
169 | } | 94 | } |
170 | } | 95 | } |
171 | 96 | ||