2018-11-25 22:12:48 +11:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2018-03-30 08:20:03 +11:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/ipc-server.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static struct cmd_results *bar_set_hidden_state(struct bar_config *bar,
|
|
|
|
const char *hidden_state) {
|
|
|
|
char *old_state = bar->hidden_state;
|
|
|
|
if (strcasecmp("toggle", hidden_state) == 0 && !config->reading) {
|
|
|
|
if (strcasecmp("hide", bar->hidden_state) == 0) {
|
|
|
|
bar->hidden_state = strdup("show");
|
|
|
|
} else if (strcasecmp("show", bar->hidden_state) == 0) {
|
|
|
|
bar->hidden_state = strdup("hide");
|
|
|
|
}
|
|
|
|
} else if (strcasecmp("hide", hidden_state) == 0) {
|
|
|
|
bar->hidden_state = strdup("hide");
|
|
|
|
} else if (strcasecmp("show", hidden_state) == 0) {
|
|
|
|
bar->hidden_state = strdup("show");
|
|
|
|
} else {
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_INVALID, "Invalid value %s", hidden_state);
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
|
|
|
if (strcmp(old_state, bar->hidden_state) != 0) {
|
2019-09-03 11:41:11 +10:00
|
|
|
if (!config->current_bar) {
|
2018-03-30 08:20:03 +11:00
|
|
|
ipc_event_barconfig_update(bar);
|
|
|
|
}
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_DEBUG, "Setting hidden_state: '%s' for bar: %s",
|
2018-03-30 08:20:03 +11:00
|
|
|
bar->hidden_state, bar->id);
|
|
|
|
}
|
|
|
|
// free old mode
|
|
|
|
free(old_state);
|
2018-10-01 08:06:40 +10:00
|
|
|
return NULL;
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
struct cmd_results *bar_cmd_hidden_state(int argc, char **argv) {
|
|
|
|
struct cmd_results *error = NULL;
|
|
|
|
if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_LEAST, 1))) {
|
|
|
|
return error;
|
|
|
|
}
|
2018-10-23 20:40:03 +11:00
|
|
|
if ((error = checkarg(argc, "hidden_state", EXPECTED_AT_MOST, 2))) {
|
2018-03-30 08:20:03 +11:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
if (config->reading && argc > 1) {
|
2019-01-11 10:27:21 +11:00
|
|
|
return cmd_results_new(CMD_INVALID,
|
2018-03-30 08:20:03 +11:00
|
|
|
"Unexpected value %s in config mode", argv[1]);
|
|
|
|
}
|
|
|
|
|
2019-09-03 11:41:11 +10:00
|
|
|
if (config->current_bar && argc == 2 &&
|
|
|
|
strcmp(config->current_bar->id, argv[1]) != 0) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "Conflicting bar ids: %s and %s",
|
|
|
|
config->current_bar->id, argv[1]);
|
|
|
|
}
|
|
|
|
|
2018-03-30 08:20:03 +11:00
|
|
|
const char *state = argv[0];
|
|
|
|
if (config->reading) {
|
2018-10-01 08:06:40 +10:00
|
|
|
error = bar_set_hidden_state(config->current_bar, state);
|
|
|
|
} else {
|
|
|
|
const char *id = argc == 2 ? argv[1] : NULL;
|
|
|
|
for (int i = 0; i < config->bars->length; ++i) {
|
|
|
|
struct bar_config *bar = config->bars->items[i];
|
|
|
|
if (id) {
|
|
|
|
if (strcmp(id, bar->id) == 0) {
|
|
|
|
error = bar_set_hidden_state(bar, state);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if ((error = bar_set_hidden_state(bar, state))) {
|
|
|
|
break;
|
|
|
|
}
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 10:27:21 +11:00
|
|
|
return error ? error : cmd_results_new(CMD_SUCCESS, NULL);
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|