summaryrefslogtreecommitdiffstats
path: root/sway/commands/bar/hidden_state.c
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands/bar/hidden_state.c')
-rw-r--r--sway/commands/bar/hidden_state.c78
1 files changed, 78 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..245d0858
--- /dev/null
+++ b/sway/commands/bar/hidden_state.c
@@ -0,0 +1,78 @@
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
9static 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
27 if (strcmp(old_state, bar->hidden_state) != 0) {
28 if (!config->reading) {
29 ipc_event_barconfig_update(bar);
30 }
31 wlr_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s",
32 bar->hidden_state, bar->id);
33 }
34
35 // free old mode
36 free(old_state);
37 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
38}
39
40struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) {
41 struct cmd_results *error = NULL;
42 if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) {
43 return error;
44 }
45 if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) {
46 return error;
47 }
48
49 if (config->reading && argc > 1) {
50 return cmd_results_new(CMD_INVALID, "hidden_state",
51 "Unexpected value %s in config mode", argv[1]);
52 }
53
54 const char *state = argv[0];
55
56 if (config->reading) {
57 return bar_set_hidden_state(config->current_bar, state);
58 }
59
60 const char *id = NULL;
61 if (argc == 2) {
62 id = argv[1];
63 }
64
65 struct bar_config *bar;
66 for (int i = 0; i < config->bars->length; ++i) {
67 bar = config->bars->items[i];
68 if (id && strcmp(id, bar->id) == 0) {
69 return bar_set_hidden_state(bar, state);
70 }
71
72 error = bar_set_hidden_state(bar, state);
73 if (error) {
74 return error;
75 }
76 }
77 return cmd_results_new(CMD_SUCCESS, NULL, NULL);
78}