diff options
author | Mikkel Oscar Lyderik <mikkeloscar@gmail.com> | 2016-01-23 02:47:44 +0100 |
---|---|---|
committer | Mikkel Oscar Lyderik <mikkeloscar@gmail.com> | 2016-01-24 14:22:17 +0100 |
commit | bb986cb33637147663c115c8b8f8bdff170f23a8 (patch) | |
tree | 34ca3a44732114013e7357d7cd894941ea1b95be | |
parent | numlen(0) == 1 (diff) | |
download | sway-bb986cb33637147663c115c8b8f8bdff170f23a8.tar.gz sway-bb986cb33637147663c115c8b8f8bdff170f23a8.tar.zst sway-bb986cb33637147663c115c8b8f8bdff170f23a8.zip |
swaybar: Separate config
-rw-r--r-- | swaybar/CMakeLists.txt | 1 | ||||
-rw-r--r-- | swaybar/config.c | 96 | ||||
-rw-r--r-- | swaybar/config.h | 73 | ||||
-rw-r--r-- | swaybar/main.c | 258 |
4 files changed, 257 insertions, 171 deletions
diff --git a/swaybar/CMakeLists.txt b/swaybar/CMakeLists.txt index a03ffcf4..c09cf020 100644 --- a/swaybar/CMakeLists.txt +++ b/swaybar/CMakeLists.txt | |||
@@ -8,6 +8,7 @@ include_directories( | |||
8 | 8 | ||
9 | add_executable(swaybar | 9 | add_executable(swaybar |
10 | main.c | 10 | main.c |
11 | config.c | ||
11 | ) | 12 | ) |
12 | 13 | ||
13 | target_link_libraries(swaybar | 14 | target_link_libraries(swaybar |
diff --git a/swaybar/config.c b/swaybar/config.c new file mode 100644 index 00000000..388daa55 --- /dev/null +++ b/swaybar/config.c | |||
@@ -0,0 +1,96 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <string.h> | ||
3 | |||
4 | #include "wayland-desktop-shell-client-protocol.h" | ||
5 | #include "log.h" | ||
6 | #include "config.h" | ||
7 | |||
8 | uint32_t parse_color(const char *color) { | ||
9 | if (color[0] != '#') { | ||
10 | sway_log(L_DEBUG, "Invalid color %s, defaulting to color 0xFFFFFFFF", color); | ||
11 | return 0xFFFFFFFF; | ||
12 | } | ||
13 | char *end; | ||
14 | uint32_t res = (uint32_t)strtol(color + 1, &end, 16); | ||
15 | if (strlen(color) == 7) { | ||
16 | res = (res << 8) | 0xFF; | ||
17 | } | ||
18 | return res; | ||
19 | } | ||
20 | |||
21 | uint32_t parse_position(const char *position) { | ||
22 | if (strcmp("top", position) == 0) { | ||
23 | return DESKTOP_SHELL_PANEL_POSITION_TOP; | ||
24 | } else if (strcmp("bottom", position) == 0) { | ||
25 | return DESKTOP_SHELL_PANEL_POSITION_BOTTOM; | ||
26 | } else if (strcmp("left", position) == 0) { | ||
27 | return DESKTOP_SHELL_PANEL_POSITION_LEFT; | ||
28 | } else if (strcmp("right", position) == 0) { | ||
29 | return DESKTOP_SHELL_PANEL_POSITION_RIGHT; | ||
30 | } else { | ||
31 | return DESKTOP_SHELL_PANEL_POSITION_BOTTOM; | ||
32 | } | ||
33 | } | ||
34 | |||
35 | char *parse_font(const char *font) { | ||
36 | char *new_font = NULL; | ||
37 | if (strncmp("pango:", font, 6) == 0) { | ||
38 | new_font = strdup(font + 6); | ||
39 | } | ||
40 | |||
41 | return new_font; | ||
42 | } | ||
43 | |||
44 | struct swaybar_config *init_config() { | ||
45 | struct swaybar_config *config = calloc(1, sizeof(struct swaybar_config)); | ||
46 | config->status_command = NULL; | ||
47 | config->position = DESKTOP_SHELL_PANEL_POSITION_BOTTOM; | ||
48 | config->font = NULL; | ||
49 | config->mode = NULL; | ||
50 | config->sep_symbol = NULL; | ||
51 | config->strip_workspace_numbers = false; | ||
52 | config->binding_mode_indicator = true; | ||
53 | config->workspace_buttons = true; | ||
54 | |||
55 | /* layout */ | ||
56 | config->margin = 3; | ||
57 | config->ws_horizontal_padding = 5; | ||
58 | config->ws_vertical_padding = 1.5; | ||
59 | config->ws_spacing = 1; | ||
60 | config->text_height = 30; | ||
61 | |||
62 | /* colors */ | ||
63 | config->colors.background = 0x000000FF; | ||
64 | config->colors.statusline = 0xFFFFFFFF; | ||
65 | config->colors.separator = 0x666666FF; | ||
66 | |||
67 | config->colors.focused_workspace.border = 0x4C7899FF; | ||
68 | config->colors.focused_workspace.background = 0x285577FF; | ||
69 | config->colors.focused_workspace.text = 0xFFFFFFFF; | ||
70 | |||
71 | config->colors.active_workspace.border = 0x333333FF; | ||
72 | config->colors.active_workspace.background = 0x5F676AFF; | ||
73 | config->colors.active_workspace.text = 0xFFFFFFFF; | ||
74 | |||
75 | config->colors.inactive_workspace.border = 0x333333FF; | ||
76 | config->colors.inactive_workspace.background = 0x222222FF; | ||
77 | config->colors.inactive_workspace.text = 0x888888FF; | ||
78 | |||
79 | config->colors.urgent_workspace.border = 0x2F343AFF; | ||
80 | config->colors.urgent_workspace.background = 0x900000FF; | ||
81 | config->colors.urgent_workspace.text = 0xFFFFFFFF; | ||
82 | |||
83 | config->colors.binding_mode.border = 0x2F343AFF; | ||
84 | config->colors.binding_mode.background = 0x900000FF; | ||
85 | config->colors.binding_mode.text = 0xFFFFFFFF; | ||
86 | |||
87 | return config; | ||
88 | } | ||
89 | |||
90 | void free_config(struct swaybar_config *config) { | ||
91 | free(config->status_command); | ||
92 | free(config->font); | ||
93 | free(config->mode); | ||
94 | free(config->sep_symbol); | ||
95 | free(config); | ||
96 | } | ||
diff --git a/swaybar/config.h b/swaybar/config.h new file mode 100644 index 00000000..5cda97e6 --- /dev/null +++ b/swaybar/config.h | |||
@@ -0,0 +1,73 @@ | |||
1 | #ifndef _SWAYBAR_CONFIG_H | ||
2 | #define _SWAYBAR_CONFIG_H | ||
3 | |||
4 | #include <stdint.h> | ||
5 | #include <stdbool.h> | ||
6 | |||
7 | /** | ||
8 | * Colors for a box with background, border and text colors. | ||
9 | */ | ||
10 | struct box_colors { | ||
11 | uint32_t border; | ||
12 | uint32_t background; | ||
13 | uint32_t text; | ||
14 | }; | ||
15 | |||
16 | /** | ||
17 | * Swaybar config. | ||
18 | */ | ||
19 | struct swaybar_config { | ||
20 | char *status_command; | ||
21 | uint32_t position; | ||
22 | char *font; | ||
23 | char *sep_symbol; | ||
24 | char *mode; | ||
25 | bool strip_workspace_numbers; | ||
26 | bool binding_mode_indicator; | ||
27 | bool workspace_buttons; | ||
28 | |||
29 | int margin; | ||
30 | int ws_horizontal_padding; | ||
31 | double ws_vertical_padding; | ||
32 | int ws_spacing; | ||
33 | int text_height; | ||
34 | |||
35 | struct { | ||
36 | uint32_t background; | ||
37 | uint32_t statusline; | ||
38 | uint32_t separator; | ||
39 | |||
40 | struct box_colors focused_workspace; | ||
41 | struct box_colors active_workspace; | ||
42 | struct box_colors inactive_workspace; | ||
43 | struct box_colors urgent_workspace; | ||
44 | struct box_colors binding_mode; | ||
45 | } colors; | ||
46 | }; | ||
47 | |||
48 | /** | ||
49 | * Parse colors defined as hex string to uint32_t. | ||
50 | */ | ||
51 | uint32_t parse_color(const char *color); | ||
52 | |||
53 | /** | ||
54 | * Parse position top|bottom|left|right. | ||
55 | */ | ||
56 | uint32_t parse_position(const char *position); | ||
57 | |||
58 | /** | ||
59 | * Parse font. | ||
60 | */ | ||
61 | char *parse_font(const char *font); | ||
62 | |||
63 | /** | ||
64 | * Initialize default sway config. | ||
65 | */ | ||
66 | struct swaybar_config *init_config(); | ||
67 | |||
68 | /** | ||
69 | * Free config struct. | ||
70 | */ | ||
71 | void free_config(struct swaybar_config *config); | ||
72 | |||
73 | #endif /* _SWAYBAR_CONFIG_H */ | ||
diff --git a/swaybar/main.c b/swaybar/main.c index a3a3b56a..41170bf7 100644 --- a/swaybar/main.c +++ b/swaybar/main.c | |||
@@ -19,24 +19,7 @@ | |||
19 | #include "client/pango.h" | 19 | #include "client/pango.h" |
20 | #include "stringop.h" | 20 | #include "stringop.h" |
21 | #include "log.h" | 21 | #include "log.h" |
22 | 22 | #include "config.h" | |
23 | struct box_colors { | ||
24 | uint32_t border; | ||
25 | uint32_t background; | ||
26 | uint32_t text; | ||
27 | }; | ||
28 | |||
29 | struct colors { | ||
30 | uint32_t background; | ||
31 | uint32_t statusline; | ||
32 | uint32_t separator; | ||
33 | |||
34 | struct box_colors focused_workspace; | ||
35 | struct box_colors active_workspace; | ||
36 | struct box_colors inactive_workspace; | ||
37 | struct box_colors urgent_workspace; | ||
38 | struct box_colors binding_mode; | ||
39 | }; | ||
40 | 23 | ||
41 | struct workspace { | 24 | struct workspace { |
42 | int num; | 25 | int num; |
@@ -71,50 +54,14 @@ pid_t pid; | |||
71 | int status_read_fd; | 54 | int status_read_fd; |
72 | char line[1024]; | 55 | char line[1024]; |
73 | char line_rest[1024]; | 56 | char line_rest[1024]; |
74 | char *output, *status_command; | 57 | char *output; |
75 | struct registry *registry; | 58 | struct registry *registry; |
76 | struct window *window; | 59 | struct window *window; |
77 | bool dirty = true; | 60 | bool dirty = true; |
78 | char *separator_symbol = NULL; | 61 | struct swaybar_config *config; |
79 | char *mode = NULL; | ||
80 | bool binding_mode_indicator = true; | ||
81 | bool strip_workspace_numbers = false; | ||
82 | bool workspace_buttons = true; | ||
83 | typedef enum {UNDEF, TEXT, I3BAR} command_protocol; | 62 | typedef enum {UNDEF, TEXT, I3BAR} command_protocol; |
84 | command_protocol protocol = UNDEF; | 63 | command_protocol protocol = UNDEF; |
85 | 64 | ||
86 | struct colors colors = { | ||
87 | .background = 0x000000FF, | ||
88 | .statusline = 0xFFFFFFFF, | ||
89 | .separator = 0x666666FF, | ||
90 | |||
91 | .focused_workspace = { | ||
92 | .border = 0x4C7899FF, | ||
93 | .background = 0x285577FF, | ||
94 | .text = 0xFFFFFFFF | ||
95 | }, | ||
96 | .active_workspace = { | ||
97 | .border = 0x333333FF, | ||
98 | .background = 0x5F676AFF, | ||
99 | .text = 0xFFFFFFFF | ||
100 | }, | ||
101 | .inactive_workspace = { | ||
102 | .border = 0x333333FF, | ||
103 | .background = 0x222222FF, | ||
104 | .text = 0x888888FF | ||
105 | }, | ||
106 | .urgent_workspace = { | ||
107 | .border = 0x2F343AFF, | ||
108 | .background = 0x900000FF, | ||
109 | .text = 0xFFFFFFFF | ||
110 | }, | ||
111 | .binding_mode = { | ||
112 | .border = 0x2F343AFF, | ||
113 | .background = 0x900000FF, | ||
114 | .text = 0xFFFFFFFF | ||
115 | }, | ||
116 | }; | ||
117 | |||
118 | #define I3JSON_MAXDEPTH 4 | 65 | #define I3JSON_MAXDEPTH 4 |
119 | #define I3JSON_UNKNOWN 0 | 66 | #define I3JSON_UNKNOWN 0 |
120 | #define I3JSON_ARRAY 1 | 67 | #define I3JSON_ARRAY 1 |
@@ -237,46 +184,6 @@ void ipc_update_workspaces() { | |||
237 | free(res); | 184 | free(res); |
238 | } | 185 | } |
239 | 186 | ||
240 | uint32_t parse_color(const char *color) { | ||
241 | if (color[0] != '#') { | ||
242 | sway_abort("Invalid color %s", color); | ||
243 | } | ||
244 | char *end; | ||
245 | uint32_t res = (uint32_t)strtol(color + 1, &end, 16); | ||
246 | if (strlen(color) == 7) { | ||
247 | res = (res << 8) | 0xFF; | ||
248 | } | ||
249 | return res; | ||
250 | } | ||
251 | |||
252 | uint32_t parse_position(const char *position) { | ||
253 | if (strcmp("top", position) == 0) { | ||
254 | return DESKTOP_SHELL_PANEL_POSITION_TOP; | ||
255 | } else if (strcmp("bottom", position) == 0) { | ||
256 | return DESKTOP_SHELL_PANEL_POSITION_BOTTOM; | ||
257 | } else if (strcmp("left", position) == 0) { | ||
258 | return DESKTOP_SHELL_PANEL_POSITION_LEFT; | ||
259 | } else if (strcmp("right", position) == 0) { | ||
260 | return DESKTOP_SHELL_PANEL_POSITION_RIGHT; | ||
261 | } else { | ||
262 | return DESKTOP_SHELL_PANEL_POSITION_BOTTOM; | ||
263 | } | ||
264 | } | ||
265 | |||
266 | char *parse_font(const char *font) { | ||
267 | char *new_font = NULL; | ||
268 | if (strncmp("pango:", font, 6) == 0) { | ||
269 | new_font = strdup(font + 6); | ||
270 | } | ||
271 | |||
272 | return new_font; | ||
273 | } | ||
274 | |||
275 | static int margin = 3; | ||
276 | static const int ws_hor_padding = 5; | ||
277 | static double ws_ver_padding = 1.5; | ||
278 | static const int ws_spacing = 1; | ||
279 | |||
280 | void bar_ipc_init(int outputi, const char *bar_id) { | 187 | void bar_ipc_init(int outputi, const char *bar_id) { |
281 | uint32_t len = 0; | 188 | uint32_t len = 0; |
282 | char *res = ipc_single_command(ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); | 189 | char *res = ipc_single_command(ipc_socketfd, IPC_GET_OUTPUTS, NULL, &len); |
@@ -312,11 +219,13 @@ void bar_ipc_init(int outputi, const char *bar_id) { | |||
312 | // TODO: More of these options | 219 | // TODO: More of these options |
313 | // TODO: Refactor swaybar into several files, create a bar config struct (shared with compositor?) | 220 | // TODO: Refactor swaybar into several files, create a bar config struct (shared with compositor?) |
314 | if (_status_command) { | 221 | if (_status_command) { |
315 | status_command = strdup(json_object_get_string(_status_command)); | 222 | free(config->status_command); |
223 | config->status_command = strdup(json_object_get_string(_status_command)); | ||
316 | } | 224 | } |
317 | 225 | ||
318 | if (position) { | 226 | if (position) { |
319 | desktop_shell_set_panel_position(registry->desktop_shell, parse_position(json_object_get_string(position))); | 227 | config->position = parse_position(json_object_get_string(position)); |
228 | desktop_shell_set_panel_position(registry->desktop_shell, config->position); | ||
320 | } | 229 | } |
321 | 230 | ||
322 | if (font) { | 231 | if (font) { |
@@ -324,19 +233,20 @@ void bar_ipc_init(int outputi, const char *bar_id) { | |||
324 | } | 233 | } |
325 | 234 | ||
326 | if (sep_symbol) { | 235 | if (sep_symbol) { |
327 | separator_symbol = strdup(json_object_get_string(sep_symbol)); | 236 | free(config->sep_symbol); |
237 | config->sep_symbol = strdup(json_object_get_string(sep_symbol)); | ||
328 | } | 238 | } |
329 | 239 | ||
330 | if (_strip_workspace_numbers) { | 240 | if (_strip_workspace_numbers) { |
331 | strip_workspace_numbers = json_object_get_boolean(_strip_workspace_numbers); | 241 | config->strip_workspace_numbers = json_object_get_boolean(_strip_workspace_numbers); |
332 | } | 242 | } |
333 | 243 | ||
334 | if (_binding_mode_indicator) { | 244 | if (_binding_mode_indicator) { |
335 | binding_mode_indicator = json_object_get_boolean(_binding_mode_indicator); | 245 | config->binding_mode_indicator = json_object_get_boolean(_binding_mode_indicator); |
336 | } | 246 | } |
337 | 247 | ||
338 | if (_workspace_buttons) { | 248 | if (_workspace_buttons) { |
339 | workspace_buttons = json_object_get_boolean(_workspace_buttons); | 249 | config->workspace_buttons = json_object_get_boolean(_workspace_buttons); |
340 | } | 250 | } |
341 | 251 | ||
342 | if (bar_height) { | 252 | if (bar_height) { |
@@ -344,10 +254,10 @@ void bar_ipc_init(int outputi, const char *bar_id) { | |||
344 | get_text_size(window, &width, &height, "Test string for measuring purposes"); | 254 | get_text_size(window, &width, &height, "Test string for measuring purposes"); |
345 | int bar_height_value = json_object_get_int(bar_height); | 255 | int bar_height_value = json_object_get_int(bar_height); |
346 | if (bar_height_value > 0) { | 256 | if (bar_height_value > 0) { |
347 | margin = (bar_height_value - height) / 2; | 257 | config->margin = (bar_height_value - height) / 2; |
348 | ws_ver_padding = margin - 1.5; | 258 | config->ws_vertical_padding = config->margin - 1.5; |
349 | } | 259 | } |
350 | window->height = height + margin * 2; | 260 | window->height = height + config->margin * 2; |
351 | } | 261 | } |
352 | 262 | ||
353 | if (_colors) { | 263 | if (_colors) { |
@@ -375,44 +285,50 @@ void bar_ipc_init(int outputi, const char *bar_id) { | |||
375 | json_object_object_get_ex(_colors, "binding_mode_border", &binding_mode_border); | 285 | json_object_object_get_ex(_colors, "binding_mode_border", &binding_mode_border); |
376 | json_object_object_get_ex(_colors, "binding_mode_bg", &binding_mode_bg); | 286 | json_object_object_get_ex(_colors, "binding_mode_bg", &binding_mode_bg); |
377 | json_object_object_get_ex(_colors, "binding_mode_text", &binding_mode_text); | 287 | json_object_object_get_ex(_colors, "binding_mode_text", &binding_mode_text); |
378 | if (background) colors.background = parse_color(json_object_get_string(background)); | 288 | if (background) { |
379 | if (statusline) colors.statusline = parse_color(json_object_get_string(statusline)); | 289 | config->colors.background = parse_color(json_object_get_string(background)); |
380 | if (separator) colors.separator = parse_color(json_object_get_string(separator)); | 290 | } |
291 | if (statusline) { | ||
292 | config->colors.statusline = parse_color(json_object_get_string(statusline)); | ||
293 | } | ||
294 | if (separator) { | ||
295 | config->colors.separator = parse_color(json_object_get_string(separator)); | ||
296 | } | ||
381 | if (focused_workspace_border) { | 297 | if (focused_workspace_border) { |
382 | colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); | 298 | config->colors.focused_workspace.border = parse_color(json_object_get_string(focused_workspace_border)); |
383 | } | 299 | } |
384 | if (focused_workspace_bg) { | 300 | if (focused_workspace_bg) { |
385 | colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); | 301 | config->colors.focused_workspace.background = parse_color(json_object_get_string(focused_workspace_bg)); |
386 | } | 302 | } |
387 | if (focused_workspace_text) { | 303 | if (focused_workspace_text) { |
388 | colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); | 304 | config->colors.focused_workspace.text = parse_color(json_object_get_string(focused_workspace_text)); |
389 | } | 305 | } |
390 | if (active_workspace_border) { | 306 | if (active_workspace_border) { |
391 | colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); | 307 | config->colors.active_workspace.border = parse_color(json_object_get_string(active_workspace_border)); |
392 | } | 308 | } |
393 | if (active_workspace_bg) { | 309 | if (active_workspace_bg) { |
394 | colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); | 310 | config->colors.active_workspace.background = parse_color(json_object_get_string(active_workspace_bg)); |
395 | } | 311 | } |
396 | if (active_workspace_text) { | 312 | if (active_workspace_text) { |
397 | colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); | 313 | config->colors.active_workspace.text = parse_color(json_object_get_string(active_workspace_text)); |
398 | } | 314 | } |
399 | if (inactive_workspace_border) { | 315 | if (inactive_workspace_border) { |
400 | colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); | 316 | config->colors.inactive_workspace.border = parse_color(json_object_get_string(inactive_workspace_border)); |
401 | } | 317 | } |
402 | if (inactive_workspace_bg) { | 318 | if (inactive_workspace_bg) { |
403 | colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); | 319 | config->colors.inactive_workspace.background = parse_color(json_object_get_string(inactive_workspace_bg)); |
404 | } | 320 | } |
405 | if (inactive_workspace_text) { | 321 | if (inactive_workspace_text) { |
406 | colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); | 322 | config->colors.inactive_workspace.text = parse_color(json_object_get_string(inactive_workspace_text)); |
407 | } | 323 | } |
408 | if (binding_mode_border) { | 324 | if (binding_mode_border) { |
409 | colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); | 325 | config->colors.binding_mode.border = parse_color(json_object_get_string(binding_mode_border)); |
410 | } | 326 | } |
411 | if (binding_mode_bg) { | 327 | if (binding_mode_bg) { |
412 | colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); | 328 | config->colors.binding_mode.background = parse_color(json_object_get_string(binding_mode_bg)); |
413 | } | 329 | } |
414 | if (binding_mode_text) { | 330 | if (binding_mode_text) { |
415 | colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); | 331 | config->colors.binding_mode.text = parse_color(json_object_get_string(binding_mode_text)); |
416 | } | 332 | } |
417 | } | 333 | } |
418 | 334 | ||
@@ -473,27 +389,27 @@ void render_block(struct status_block *block, double *x, bool edge) { | |||
473 | *x -= width; | 389 | *x -= width; |
474 | 390 | ||
475 | if (block->border != 0 && block->border_left > 0) { | 391 | if (block->border != 0 && block->border_left > 0) { |
476 | *x -= (block->border_left + margin); | 392 | *x -= (block->border_left + config->margin); |
477 | block_width += block->border_left + margin; | 393 | block_width += block->border_left + config->margin; |
478 | } | 394 | } |
479 | 395 | ||
480 | if (block->border != 0 && block->border_right > 0) { | 396 | if (block->border != 0 && block->border_right > 0) { |
481 | *x -= (block->border_right + margin); | 397 | *x -= (block->border_right + config->margin); |
482 | block_width += block->border_right + margin; | 398 | block_width += block->border_right + config->margin; |
483 | } | 399 | } |
484 | 400 | ||
485 | // Add separator | 401 | // Add separator |
486 | if (!edge) { | 402 | if (!edge) { |
487 | if (separator_symbol) { | 403 | if (config->sep_symbol) { |
488 | get_text_size(window, &sep_width, &height, "%s", separator_symbol); | 404 | get_text_size(window, &sep_width, &height, "%s", config->sep_symbol); |
489 | if (sep_width > block->separator_block_width) { | 405 | if (sep_width > block->separator_block_width) { |
490 | block->separator_block_width = sep_width + margin * 2; | 406 | block->separator_block_width = sep_width + config->margin * 2; |
491 | } | 407 | } |
492 | } | 408 | } |
493 | 409 | ||
494 | *x -= block->separator_block_width; | 410 | *x -= block->separator_block_width; |
495 | } else { | 411 | } else { |
496 | *x -= margin; | 412 | *x -= config->margin; |
497 | } | 413 | } |
498 | 414 | ||
499 | double pos = *x; | 415 | double pos = *x; |
@@ -531,7 +447,7 @@ void render_block(struct status_block *block, double *x, bool edge) { | |||
531 | block->border_left, | 447 | block->border_left, |
532 | window->height - 2); | 448 | window->height - 2); |
533 | 449 | ||
534 | pos += block->border_left + margin; | 450 | pos += block->border_left + config->margin; |
535 | } | 451 | } |
536 | 452 | ||
537 | // render text | 453 | // render text |
@@ -545,7 +461,7 @@ void render_block(struct status_block *block, double *x, bool edge) { | |||
545 | offset = pos + (width - textwidth) / 2; | 461 | offset = pos + (width - textwidth) / 2; |
546 | } | 462 | } |
547 | 463 | ||
548 | cairo_move_to(window->cairo, offset, margin); | 464 | cairo_move_to(window->cairo, offset, config->margin); |
549 | cairo_set_source_u32(window->cairo, block->color); | 465 | cairo_set_source_u32(window->cairo, block->color); |
550 | pango_printf(window, "%s", block->full_text); | 466 | pango_printf(window, "%s", block->full_text); |
551 | 467 | ||
@@ -553,7 +469,7 @@ void render_block(struct status_block *block, double *x, bool edge) { | |||
553 | 469 | ||
554 | // render right border | 470 | // render right border |
555 | if (block->border != 0 && block->border_right > 0) { | 471 | if (block->border != 0 && block->border_right > 0) { |
556 | pos += margin; | 472 | pos += config->margin; |
557 | 473 | ||
558 | render_sharp_line(window->cairo, block->border, | 474 | render_sharp_line(window->cairo, block->border, |
559 | pos - 0.5, | 475 | pos - 0.5, |
@@ -566,17 +482,17 @@ void render_block(struct status_block *block, double *x, bool edge) { | |||
566 | 482 | ||
567 | // render separator | 483 | // render separator |
568 | if (!edge && block->separator) { | 484 | if (!edge && block->separator) { |
569 | cairo_set_source_u32(window->cairo, colors.separator); | 485 | cairo_set_source_u32(window->cairo, config->colors.separator); |
570 | if (separator_symbol) { | 486 | if (config->sep_symbol) { |
571 | offset = pos + (block->separator_block_width - sep_width) / 2; | 487 | offset = pos + (block->separator_block_width - sep_width) / 2; |
572 | cairo_move_to(window->cairo, offset, margin); | 488 | cairo_move_to(window->cairo, offset, config->margin); |
573 | pango_printf(window, "%s", separator_symbol); | 489 | pango_printf(window, "%s", config->sep_symbol); |
574 | } else { | 490 | } else { |
575 | cairo_set_line_width(window->cairo, 1); | 491 | cairo_set_line_width(window->cairo, 1); |
576 | cairo_move_to(window->cairo, pos + block->separator_block_width/2, | 492 | cairo_move_to(window->cairo, pos + block->separator_block_width/2, |
577 | margin); | 493 | config->margin); |
578 | cairo_line_to(window->cairo, pos + block->separator_block_width/2, | 494 | cairo_line_to(window->cairo, pos + block->separator_block_width/2, |
579 | window->height - margin); | 495 | window->height - config->margin); |
580 | cairo_stroke(window->cairo); | 496 | cairo_stroke(window->cairo); |
581 | } | 497 | } |
582 | } | 498 | } |
@@ -609,63 +525,63 @@ char *handle_workspace_number(bool strip_num, const char *ws_name) { | |||
609 | 525 | ||
610 | void render_workspace_button(struct workspace *ws, double *x) { | 526 | void render_workspace_button(struct workspace *ws, double *x) { |
611 | // strip workspace numbers if required | 527 | // strip workspace numbers if required |
612 | char *name = handle_workspace_number(strip_workspace_numbers, ws->name); | 528 | char *name = handle_workspace_number(config->strip_workspace_numbers, ws->name); |
613 | 529 | ||
614 | int width, height; | 530 | int width, height; |
615 | get_text_size(window, &width, &height, "%s", name); | 531 | get_text_size(window, &width, &height, "%s", name); |
616 | struct box_colors box_colors; | 532 | struct box_colors box_colors; |
617 | if (ws->urgent) { | 533 | if (ws->urgent) { |
618 | box_colors = colors.urgent_workspace; | 534 | box_colors = config->colors.urgent_workspace; |
619 | } else if (ws->focused) { | 535 | } else if (ws->focused) { |
620 | box_colors = colors.focused_workspace; | 536 | box_colors = config->colors.focused_workspace; |
621 | } else if (ws->visible) { | 537 | } else if (ws->visible) { |
622 | box_colors = colors.active_workspace; | 538 | box_colors = config->colors.active_workspace; |
623 | } else { | 539 | } else { |
624 | box_colors = colors.inactive_workspace; | 540 | box_colors = config->colors.inactive_workspace; |
625 | } | 541 | } |
626 | 542 | ||
627 | // background | 543 | // background |
628 | cairo_set_source_u32(window->cairo, box_colors.background); | 544 | cairo_set_source_u32(window->cairo, box_colors.background); |
629 | cairo_rectangle(window->cairo, *x, 1.5, width + ws_hor_padding * 2 - 1, | 545 | cairo_rectangle(window->cairo, *x, 1.5, width + config->ws_horizontal_padding * 2 - 1, |
630 | height + ws_ver_padding * 2); | 546 | height + config->ws_vertical_padding * 2); |
631 | cairo_fill(window->cairo); | 547 | cairo_fill(window->cairo); |
632 | 548 | ||
633 | // border | 549 | // border |
634 | cairo_set_source_u32(window->cairo, box_colors.border); | 550 | cairo_set_source_u32(window->cairo, box_colors.border); |
635 | cairo_rectangle(window->cairo, *x, 1.5, width + ws_hor_padding * 2 - 1, | 551 | cairo_rectangle(window->cairo, *x, 1.5, width + config->ws_horizontal_padding * 2 - 1, |
636 | height + ws_ver_padding * 2); | 552 | height + config->ws_vertical_padding * 2); |
637 | cairo_stroke(window->cairo); | 553 | cairo_stroke(window->cairo); |
638 | 554 | ||
639 | // text | 555 | // text |
640 | cairo_set_source_u32(window->cairo, box_colors.text); | 556 | cairo_set_source_u32(window->cairo, box_colors.text); |
641 | cairo_move_to(window->cairo, (int)*x + ws_hor_padding, margin); | 557 | cairo_move_to(window->cairo, (int)*x + config->ws_horizontal_padding, config->margin); |
642 | pango_printf(window, "%s", name); | 558 | pango_printf(window, "%s", name); |
643 | 559 | ||
644 | *x += width + ws_hor_padding * 2 + ws_spacing; | 560 | *x += width + config->ws_horizontal_padding * 2 + config->ws_spacing; |
645 | 561 | ||
646 | free(name); | 562 | free(name); |
647 | } | 563 | } |
648 | 564 | ||
649 | void render_binding_mode_indicator(double pos) { | 565 | void render_binding_mode_indicator(double pos) { |
650 | int width, height; | 566 | int width, height; |
651 | get_text_size(window, &width, &height, "%s", mode); | 567 | get_text_size(window, &width, &height, "%s", config->mode); |
652 | 568 | ||
653 | // background | 569 | // background |
654 | cairo_set_source_u32(window->cairo, colors.binding_mode.background); | 570 | cairo_set_source_u32(window->cairo, config->colors.binding_mode.background); |
655 | cairo_rectangle(window->cairo, pos, 1.5, width + ws_hor_padding * 2 - 1, | 571 | cairo_rectangle(window->cairo, pos, 1.5, width + config->ws_horizontal_padding * 2 - 1, |
656 | height + ws_ver_padding * 2); | 572 | height + config->ws_vertical_padding * 2); |
657 | cairo_fill(window->cairo); | 573 | cairo_fill(window->cairo); |
658 | 574 | ||
659 | // border | 575 | // border |
660 | cairo_set_source_u32(window->cairo, colors.binding_mode.border); | 576 | cairo_set_source_u32(window->cairo, config->colors.binding_mode.border); |
661 | cairo_rectangle(window->cairo, pos, 1.5, width + ws_hor_padding * 2 - 1, | 577 | cairo_rectangle(window->cairo, pos, 1.5, width + config->ws_horizontal_padding * 2 - 1, |
662 | height + ws_ver_padding * 2); | 578 | height + config->ws_vertical_padding * 2); |
663 | cairo_stroke(window->cairo); | 579 | cairo_stroke(window->cairo); |
664 | 580 | ||
665 | // text | 581 | // text |
666 | cairo_set_source_u32(window->cairo, colors.binding_mode.text); | 582 | cairo_set_source_u32(window->cairo, config->colors.binding_mode.text); |
667 | cairo_move_to(window->cairo, (int)pos + ws_hor_padding, margin); | 583 | cairo_move_to(window->cairo, (int)pos + config->ws_horizontal_padding, config->margin); |
668 | pango_printf(window, "%s", mode); | 584 | pango_printf(window, "%s", config->mode); |
669 | } | 585 | } |
670 | 586 | ||
671 | void render() { | 587 | void render() { |
@@ -678,16 +594,16 @@ void render() { | |||
678 | cairo_restore(window->cairo); | 594 | cairo_restore(window->cairo); |
679 | 595 | ||
680 | // Background | 596 | // Background |
681 | cairo_set_source_u32(window->cairo, colors.background); | 597 | cairo_set_source_u32(window->cairo, config->colors.background); |
682 | cairo_paint(window->cairo); | 598 | cairo_paint(window->cairo); |
683 | 599 | ||
684 | // Command output | 600 | // Command output |
685 | cairo_set_source_u32(window->cairo, colors.statusline); | 601 | cairo_set_source_u32(window->cairo, config->colors.statusline); |
686 | int width, height; | 602 | int width, height; |
687 | 603 | ||
688 | if (protocol == TEXT) { | 604 | if (protocol == TEXT) { |
689 | get_text_size(window, &width, &height, "%s", line); | 605 | get_text_size(window, &width, &height, "%s", line); |
690 | cairo_move_to(window->cairo, window->width - margin - width, margin); | 606 | cairo_move_to(window->cairo, window->width - config->margin - width, config->margin); |
691 | pango_printf(window, "%s", line); | 607 | pango_printf(window, "%s", line); |
692 | } else if (protocol == I3BAR && status_line) { | 608 | } else if (protocol == I3BAR && status_line) { |
693 | double pos = window->width - 0.5; | 609 | double pos = window->width - 0.5; |
@@ -705,7 +621,7 @@ void render() { | |||
705 | double x = 0.5; | 621 | double x = 0.5; |
706 | 622 | ||
707 | // Workspaces | 623 | // Workspaces |
708 | if (workspace_buttons) { | 624 | if (config->workspace_buttons) { |
709 | for (i = 0; i < workspaces->length; ++i) { | 625 | for (i = 0; i < workspaces->length; ++i) { |
710 | struct workspace *ws = workspaces->items[i]; | 626 | struct workspace *ws = workspaces->items[i]; |
711 | render_workspace_button(ws, &x); | 627 | render_workspace_button(ws, &x); |
@@ -713,7 +629,7 @@ void render() { | |||
713 | } | 629 | } |
714 | 630 | ||
715 | // binding mode indicator | 631 | // binding mode indicator |
716 | if (mode && binding_mode_indicator) { | 632 | if (config->mode && config->binding_mode_indicator) { |
717 | render_binding_mode_indicator(x); | 633 | render_binding_mode_indicator(x); |
718 | } | 634 | } |
719 | } | 635 | } |
@@ -802,7 +718,7 @@ void parse_json(const char *text) { | |||
802 | if (color) { | 718 | if (color) { |
803 | new->color = parse_color(json_object_get_string(color)); | 719 | new->color = parse_color(json_object_get_string(color)); |
804 | } else { | 720 | } else { |
805 | new->color = colors.statusline; | 721 | new->color = config->colors.statusline; |
806 | } | 722 | } |
807 | 723 | ||
808 | if (min_width) { | 724 | if (min_width) { |
@@ -1092,11 +1008,11 @@ bool handle_ipc_event() { | |||
1092 | if (json_object_object_get_ex(result, "change", &json_change)) { | 1008 | if (json_object_object_get_ex(result, "change", &json_change)) { |
1093 | const char *change = json_object_get_string(json_change); | 1009 | const char *change = json_object_get_string(json_change); |
1094 | 1010 | ||
1095 | free(mode); | 1011 | free(config->mode); |
1096 | if (strcmp(change, "default") == 0) { | 1012 | if (strcmp(change, "default") == 0) { |
1097 | mode = NULL; | 1013 | config->mode = NULL; |
1098 | } else { | 1014 | } else { |
1099 | mode = strdup(change); | 1015 | config->mode = strdup(change); |
1100 | } | 1016 | } |
1101 | } else { | 1017 | } else { |
1102 | sway_log(L_ERROR, "failed to parse response"); | 1018 | sway_log(L_ERROR, "failed to parse response"); |
@@ -1142,7 +1058,7 @@ void poll_for_update() { | |||
1142 | dirty = handle_ipc_event(); | 1058 | dirty = handle_ipc_event(); |
1143 | } | 1059 | } |
1144 | 1060 | ||
1145 | if (status_command && FD_ISSET(status_read_fd, &readfds)) { | 1061 | if (config->status_command && FD_ISSET(status_read_fd, &readfds)) { |
1146 | sway_log(L_DEBUG, "Got update from status command."); | 1062 | sway_log(L_DEBUG, "Got update from status command."); |
1147 | switch (protocol) { | 1063 | switch (protocol) { |
1148 | case I3BAR: | 1064 | case I3BAR: |
@@ -1185,10 +1101,10 @@ void poll_for_update() { | |||
1185 | } | 1101 | } |
1186 | 1102 | ||
1187 | int main(int argc, char **argv) { | 1103 | int main(int argc, char **argv) { |
1188 | |||
1189 | char *socket_path = NULL; | 1104 | char *socket_path = NULL; |
1190 | char *bar_id = NULL; | 1105 | char *bar_id = NULL; |
1191 | bool debug = false; | 1106 | bool debug = false; |
1107 | config = init_config(); | ||
1192 | 1108 | ||
1193 | static struct option long_options[] = { | 1109 | static struct option long_options[] = { |
1194 | {"help", no_argument, NULL, 'h'}, | 1110 | {"help", no_argument, NULL, 'h'}, |
@@ -1284,7 +1200,7 @@ int main(int argc, char **argv) { | |||
1284 | 1200 | ||
1285 | bar_ipc_init(desired_output, bar_id); | 1201 | bar_ipc_init(desired_output, bar_id); |
1286 | 1202 | ||
1287 | if (status_command) { | 1203 | if (config->status_command) { |
1288 | int pipefd[2]; | 1204 | int pipefd[2]; |
1289 | pipe(pipefd); | 1205 | pipe(pipefd); |
1290 | pid = fork(); | 1206 | pid = fork(); |
@@ -1295,7 +1211,7 @@ int main(int argc, char **argv) { | |||
1295 | char *const cmd[] = { | 1211 | char *const cmd[] = { |
1296 | "sh", | 1212 | "sh", |
1297 | "-c", | 1213 | "-c", |
1298 | status_command, | 1214 | config->status_command, |
1299 | NULL, | 1215 | NULL, |
1300 | }; | 1216 | }; |
1301 | execvp(cmd[0], cmd); | 1217 | execvp(cmd[0], cmd); |