2018-09-03 17:08:49 +10:00
|
|
|
#define _POSIX_C_SOURCE 200809
|
2019-01-22 21:43:48 +11:00
|
|
|
#include <stdio.h>
|
2018-03-30 08:20:03 +11:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/config.h"
|
2019-09-03 11:41:11 +10:00
|
|
|
#include "sway/ipc-server.h"
|
2019-01-21 05:51:12 +11:00
|
|
|
#include "log.h"
|
2018-03-30 08:20:03 +11:00
|
|
|
|
2018-05-31 03:20:02 +10:00
|
|
|
// Must be in alphabetical order for bsearch
|
2021-02-03 11:54:35 +11:00
|
|
|
static const struct cmd_handler bar_handlers[] = {
|
2019-01-11 04:43:10 +11:00
|
|
|
{ "bindcode", bar_cmd_bindcode },
|
2018-05-31 03:20:02 +10:00
|
|
|
{ "binding_mode_indicator", bar_cmd_binding_mode_indicator },
|
|
|
|
{ "bindsym", bar_cmd_bindsym },
|
|
|
|
{ "colors", bar_cmd_colors },
|
|
|
|
{ "font", bar_cmd_font },
|
2018-11-29 03:23:48 +11:00
|
|
|
{ "gaps", bar_cmd_gaps },
|
2018-05-31 03:20:02 +10:00
|
|
|
{ "height", bar_cmd_height },
|
|
|
|
{ "hidden_state", bar_cmd_hidden_state },
|
|
|
|
{ "icon_theme", bar_cmd_icon_theme },
|
|
|
|
{ "mode", bar_cmd_mode },
|
|
|
|
{ "modifier", bar_cmd_modifier },
|
|
|
|
{ "output", bar_cmd_output },
|
|
|
|
{ "pango_markup", bar_cmd_pango_markup },
|
|
|
|
{ "position", bar_cmd_position },
|
|
|
|
{ "separator_symbol", bar_cmd_separator_symbol },
|
|
|
|
{ "status_command", bar_cmd_status_command },
|
2019-01-11 16:12:24 +11:00
|
|
|
{ "status_edge_padding", bar_cmd_status_edge_padding },
|
2019-01-11 15:43:45 +11:00
|
|
|
{ "status_padding", bar_cmd_status_padding },
|
2018-11-18 03:11:28 +11:00
|
|
|
{ "strip_workspace_name", bar_cmd_strip_workspace_name },
|
2018-05-31 03:20:02 +10:00
|
|
|
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
|
2019-01-16 13:25:28 +11:00
|
|
|
{ "tray_bindcode", bar_cmd_tray_bindcode },
|
2018-12-10 02:10:41 +11:00
|
|
|
{ "tray_bindsym", bar_cmd_tray_bindsym },
|
2018-05-31 03:20:02 +10:00
|
|
|
{ "tray_output", bar_cmd_tray_output },
|
|
|
|
{ "tray_padding", bar_cmd_tray_padding },
|
2019-04-13 02:30:36 +10:00
|
|
|
{ "unbindcode", bar_cmd_unbindcode },
|
|
|
|
{ "unbindsym", bar_cmd_unbindsym },
|
2018-05-31 03:20:02 +10:00
|
|
|
{ "workspace_buttons", bar_cmd_workspace_buttons },
|
2020-10-03 23:45:26 +10:00
|
|
|
{ "workspace_min_width", bar_cmd_workspace_min_width },
|
2018-05-31 03:20:02 +10:00
|
|
|
{ "wrap_scroll", bar_cmd_wrap_scroll },
|
|
|
|
};
|
|
|
|
|
|
|
|
// Must be in alphabetical order for bsearch
|
2021-02-03 11:54:35 +11:00
|
|
|
static const struct cmd_handler bar_config_handlers[] = {
|
2018-10-10 04:41:12 +11:00
|
|
|
{ "id", bar_cmd_id },
|
|
|
|
{ "swaybar_command", bar_cmd_swaybar_command },
|
2018-05-31 03:20:02 +10:00
|
|
|
};
|
|
|
|
|
2018-10-11 01:49:34 +11:00
|
|
|
// Determines whether the subcommand is valid in any bar handler struct
|
|
|
|
static bool is_subcommand(char *name) {
|
|
|
|
return find_handler(name, bar_handlers, sizeof(bar_handlers)) ||
|
|
|
|
find_handler(name, bar_config_handlers, sizeof(bar_config_handlers));
|
|
|
|
}
|
|
|
|
|
2018-03-30 08:20:03 +11:00
|
|
|
struct cmd_results *cmd_bar(int argc, char **argv) {
|
|
|
|
struct cmd_results *error = NULL;
|
2018-10-13 12:53:40 +11:00
|
|
|
if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 2))) {
|
2018-03-30 08:20:03 +11:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2019-09-03 11:41:11 +10:00
|
|
|
char *id = NULL;
|
2018-10-13 12:53:40 +11:00
|
|
|
if (strcmp(argv[0], "id") != 0 && is_subcommand(argv[1])) {
|
|
|
|
for (int i = 0; i < config->bars->length; ++i) {
|
|
|
|
struct bar_config *item = config->bars->items[i];
|
|
|
|
if (strcmp(item->id, argv[0]) == 0) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_DEBUG, "Selecting bar: %s", argv[0]);
|
2019-09-03 11:41:11 +10:00
|
|
|
config->current_bar = item;
|
2018-10-13 12:53:40 +11:00
|
|
|
break;
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
2018-10-13 12:53:40 +11:00
|
|
|
}
|
2019-09-03 11:41:11 +10:00
|
|
|
if (!config->current_bar) {
|
|
|
|
id = strdup(argv[0]);
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
2018-10-13 12:53:40 +11:00
|
|
|
++argv; --argc;
|
2019-09-03 11:41:11 +10:00
|
|
|
} else if (config->reading && !config->current_bar) {
|
|
|
|
int len = snprintf(NULL, 0, "bar-%d", config->bars->length) + 1;
|
|
|
|
id = malloc(len * sizeof(char));
|
|
|
|
if (!id) {
|
|
|
|
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar id");
|
|
|
|
}
|
|
|
|
snprintf(id, len, "bar-%d", config->bars->length);
|
2019-05-23 02:42:27 +10:00
|
|
|
} else if (!config->reading && strcmp(argv[0], "mode") != 0 &&
|
|
|
|
strcmp(argv[0], "hidden_state") != 0) {
|
2019-05-17 10:13:41 +10:00
|
|
|
if (is_subcommand(argv[0])) {
|
|
|
|
return cmd_results_new(CMD_INVALID, "No bar defined.");
|
|
|
|
} else {
|
|
|
|
return cmd_results_new(CMD_INVALID,
|
|
|
|
"Unknown/invalid command '%s'", argv[1]);
|
|
|
|
}
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
2019-09-03 11:41:11 +10:00
|
|
|
if (id) {
|
|
|
|
sway_log(SWAY_DEBUG, "Creating bar: %s", id);
|
|
|
|
config->current_bar = default_bar_config();
|
|
|
|
if (!config->current_bar) {
|
|
|
|
free(id);
|
|
|
|
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar config");
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
2019-09-03 11:41:11 +10:00
|
|
|
config->current_bar->id = id;
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|
|
|
|
|
2019-09-03 11:41:11 +10:00
|
|
|
struct cmd_results *res = NULL;
|
2018-10-10 04:41:12 +11:00
|
|
|
if (find_handler(argv[0], bar_config_handlers,
|
|
|
|
sizeof(bar_config_handlers))) {
|
|
|
|
if (config->reading) {
|
2019-09-03 11:41:11 +10:00
|
|
|
res = config_subcommand(argv, argc, bar_config_handlers,
|
2018-10-10 04:41:12 +11:00
|
|
|
sizeof(bar_config_handlers));
|
2019-09-03 11:41:11 +10:00
|
|
|
} else {
|
|
|
|
res = cmd_results_new(CMD_INVALID,
|
|
|
|
"Can only be used in the config file");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res = config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res && res->status != CMD_SUCCESS) {
|
|
|
|
if (id) {
|
|
|
|
free_bar_config(config->current_bar);
|
2021-04-19 07:45:01 +10:00
|
|
|
config->current_bar = NULL;
|
2019-09-03 11:41:11 +10:00
|
|
|
id = NULL;
|
2018-10-10 04:41:12 +11:00
|
|
|
}
|
2019-09-03 11:41:11 +10:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
list_add(config->bars, config->current_bar);
|
2018-10-10 04:41:12 +11:00
|
|
|
}
|
|
|
|
|
2019-09-03 11:41:11 +10:00
|
|
|
if (!config->reading && config->current_bar) {
|
|
|
|
ipc_event_barconfig_update(config->current_bar);
|
|
|
|
if (id) {
|
2018-10-10 04:41:12 +11:00
|
|
|
load_swaybar(config->current_bar);
|
|
|
|
}
|
|
|
|
config->current_bar = NULL;
|
|
|
|
}
|
2019-09-03 11:41:11 +10:00
|
|
|
|
2018-10-10 04:41:12 +11:00
|
|
|
return res;
|
2018-03-30 08:20:03 +11:00
|
|
|
}
|