diff options
author | Zandr Martin <zandrmartin@gmail.com> | 2016-09-01 21:39:08 -0500 |
---|---|---|
committer | Zandr Martin <zandrmartin@gmail.com> | 2016-09-01 21:39:08 -0500 |
commit | b374c35758777f98e5ddbe4b0dc43bd7c80f36d7 (patch) | |
tree | 04bb4cfc3da7d2e0de7fbc38db42f65c66d2df4c /sway/commands/bar/hidden_state.c | |
parent | Merge pull request #874 from yohanesu75/ipc-client-fix (diff) | |
download | sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.gz sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.tar.zst sway-b374c35758777f98e5ddbe4b0dc43bd7c80f36d7.zip |
refactor commands.c
Diffstat (limited to 'sway/commands/bar/hidden_state.c')
-rw-r--r-- | sway/commands/bar/hidden_state.c | 77 |
1 files changed, 77 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..22035c98 --- /dev/null +++ b/sway/commands/bar/hidden_state.c | |||
@@ -0,0 +1,77 @@ | |||
1 | #include <string.h> | ||
2 | #include "commands.h" | ||
3 | #include "config.h" | ||
4 | #include "ipc-server.h" | ||
5 | #include "log.h" | ||
6 | |||
7 | static struct cmd_results *bar_set_hidden_state(struct bar_config *bar, const char *hidden_state) { | ||
8 | char *old_state = bar->hidden_state; | ||
9 | if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) { | ||
10 | if (strcasecmp("hide", bar->hidden_state) == 0) { | ||
11 | bar->hidden_state = strdup("show"); | ||
12 | } else if (strcasecmp("show", bar->hidden_state) == 0) { | ||
13 | bar->hidden_state = strdup("hide"); | ||
14 | } | ||
15 | } else if (strcasecmp("hide", hidden_state) == 0) { | ||
16 | bar->hidden_state = strdup("hide"); | ||
17 | } else if (strcasecmp("show", hidden_state) == 0) { | ||
18 | bar->hidden_state = strdup("show"); | ||
19 | } else { | ||
20 | return cmd_results_new(CMD_INVALID, "hidden_state", "Invalid value %s", hidden_state); | ||
21 | } | ||
22 | |||
23 | if (strcmp(old_state, bar->hidden_state) != 0) { | ||
24 | if (!config->reading) { | ||
25 | ipc_event_barconfig_update(bar); | ||
26 | } | ||
27 | sway_log(L_DEBUG, "Setting hidden_state: '%s' for bar: %s", bar->hidden_state, bar->id); | ||
28 | } | ||
29 | |||
30 | // free old mode | ||
31 | free(old_state); | ||
32 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
33 | } | ||
34 | |||
35 | struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) { | ||
36 | struct cmd_results *error = NULL; | ||
37 | if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) { | ||
38 | return error; | ||
39 | } | ||
40 | if ((error = checkarg(argc, "hidden_state", EXPECTED_LESS_THAN, 3))) { | ||
41 | return error; | ||
42 | } | ||
43 | |||
44 | if (config->reading && argc > 1) { | ||
45 | return cmd_results_new(CMD_INVALID, "hidden_state", "Unexpected value %s in config mode", argv[1]); | ||
46 | } | ||
47 | |||
48 | const char *state = argv[0]; | ||
49 | |||
50 | if (config->reading) { | ||
51 | return bar_set_hidden_state(config->current_bar, state); | ||
52 | } | ||
53 | |||
54 | const char *id = NULL; | ||
55 | if (argc == 2) { | ||
56 | id = argv[1]; | ||
57 | } | ||
58 | |||
59 | int i; | ||
60 | struct bar_config *bar; | ||
61 | for (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 | |||
73 | // active bar modifiers might have changed. | ||
74 | update_active_bar_modifiers(); | ||
75 | |||
76 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
77 | } | ||