diff options
Diffstat (limited to 'sway/commands/bar/hidden_state.c')
-rw-r--r-- | sway/commands/bar/hidden_state.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/sway/commands/bar/hidden_state.c b/sway/commands/bar/hidden_state.c new file mode 100644 index 00000000..6641f184 --- /dev/null +++ b/sway/commands/bar/hidden_state.c | |||
@@ -0,0 +1,73 @@ | |||
1 | #define _XOPEN_SOURCE 500 | ||
2 | #include <string.h> | ||
3 | #include <strings.h> | ||
4 | #include "sway/commands.h" | ||
5 | #include "sway/config.h" | ||
6 | #include "sway/ipc-server.h" | ||
7 | #include "log.h" | ||
8 | |||
9 | static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, | ||
10 | const char *hidden_state) { | ||
11 | char *old_state = bar->hidden_state; | ||
12 | if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) { | ||
13 | if (strcasecmp("hide", bar->hidden_state) == 0) { | ||
14 | bar->hidden_state = strdup("show"); | ||
15 | } else if (strcasecmp("show", bar->hidden_state) == 0) { | ||
16 | bar->hidden_state = strdup("hide"); | ||
17 | } | ||
18 | } else if (strcasecmp("hide", hidden_state) == 0) { | ||
19 | bar->hidden_state = strdup("hide"); | ||
20 | } else if (strcasecmp("show", hidden_state) == 0) { | ||
21 | bar->hidden_state = strdup("show"); | ||
22 | } else { | ||
23 | return cmd_results_new(CMD_INVALID, "hidden_state", | ||
24 | "Invalid value %s", hidden_state); | ||
25 | } | ||
26 | if (strcmp(old_state, bar->hidden_state) != 0) { | ||
27 | if (!config->reading) { | ||
28 | ipc_event_barconfig_update(bar); | ||
29 | } | ||
30 | wlr_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", | ||
31 | bar->hidden_state, bar->id); | ||
32 | } | ||
33 | // free old mode | ||
34 | free(old_state); | ||
35 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
36 | } | ||
37 | |||
38 | struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { | ||
39 | struct cmd_results *error = NULL; | ||
40 | if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) { | ||
41 | return error; | ||
42 | } | ||
43 | if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) { | ||
44 | return error; | ||
45 | } | ||
46 | if (config->reading && argc > 1) { | ||
47 | return cmd_results_new(CMD_INVALID, "hidden_state", | ||
48 | "Unexpected value %s in config mode", argv[1]); | ||
49 | } | ||
50 | |||
51 | const char *state = argv[0]; | ||
52 | if (config->reading) { | ||
53 | return bar_set_hidden_state(config->current_bar, state); | ||
54 | } | ||
55 | |||
56 | const char *id = NULL; | ||
57 | if (argc == 2) { | ||
58 | id = argv[1]; | ||
59 | } | ||
60 | struct bar_config *bar; | ||
61 | for (int i = 0; i < config->bars->length; ++i) { | ||
62 | bar = config->bars->items[i]; | ||
63 | if (id && strcmp(id, bar->id) == 0) { | ||
64 | return bar_set_hidden_state(bar, state); | ||
65 | } | ||
66 | |||
67 | error = bar_set_hidden_state(bar, state); | ||
68 | if (error) { | ||
69 | return error; | ||
70 | } | ||
71 | } | ||
72 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
73 | } | ||