sway-patched-tray-menu-github/sway/commands/bar.c

58 lines
1.5 KiB
C
Raw Normal View History

2018-03-29 23:20:03 +02:00
#include <string.h>
#include <strings.h>
#include <wlr/util/log.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "util.h"
struct cmd_results *cmd_bar(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (config->reading && strcmp("{", argv[0]) != 0) {
return cmd_results_new(CMD_INVALID, "bar",
"Expected '{' at start of bar config definition.");
}
if (!config->reading) {
if (argc > 1) {
if (strcasecmp("mode", argv[0]) == 0) {
return bar_cmd_mode(argc-1, argv + 1);
}
if (strcasecmp("hidden_state", argv[0]) == 0) {
return bar_cmd_hidden_state(argc-1, argv + 1);
}
}
return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file.");
}
// Create new bar with default values
struct bar_config *bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state");
}
// set bar id
2018-03-30 03:54:08 +02:00
for (int i = 0; i < config->bars->length; ++i) {
2018-03-29 23:20:03 +02:00
if (bar == config->bars->items[i]) {
const int len = 5 + numlen(i); // "bar-" + i + \0
bar->id = malloc(len * sizeof(char));
if (bar->id) {
snprintf(bar->id, len, "bar-%d", i);
} else {
2018-03-30 03:54:08 +02:00
return cmd_results_new(CMD_FAILURE,
"bar", "Unable to allocate bar ID");
2018-03-29 23:20:03 +02:00
}
break;
}
}
// Set current bar
config->current_bar = bar;
wlr_log(L_DEBUG, "Configuring bar %s", bar->id);
return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL);
}