1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#define _POSIX_C_SOURCE 200809
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include "sway/commands.h"
#include "sway/config.h"
#include "log.h"
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_handlers[] = {
{ "bindcode", bar_cmd_bindcode },
{ "binding_mode_indicator", bar_cmd_binding_mode_indicator },
{ "bindsym", bar_cmd_bindsym },
{ "colors", bar_cmd_colors },
{ "font", bar_cmd_font },
{ "gaps", bar_cmd_gaps },
{ "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 },
{ "status_edge_padding", bar_cmd_status_edge_padding },
{ "status_padding", bar_cmd_status_padding },
{ "strip_workspace_name", bar_cmd_strip_workspace_name },
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
{ "tray_bindcode", bar_cmd_tray_bindcode },
{ "tray_bindsym", bar_cmd_tray_bindsym },
{ "tray_output", bar_cmd_tray_output },
{ "tray_padding", bar_cmd_tray_padding },
{ "unbindcode", bar_cmd_unbindcode },
{ "unbindsym", bar_cmd_unbindsym },
{ "workspace_buttons", bar_cmd_workspace_buttons },
{ "wrap_scroll", bar_cmd_wrap_scroll },
};
// Must be in alphabetical order for bsearch
static struct cmd_handler bar_config_handlers[] = {
{ "id", bar_cmd_id },
{ "swaybar_command", bar_cmd_swaybar_command },
};
// 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));
}
struct cmd_results *cmd_bar(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 2))) {
return error;
}
bool spawn = false;
struct bar_config *bar = NULL;
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) {
sway_log(SWAY_DEBUG, "Selecting bar: %s", argv[0]);
bar = item;
break;
}
}
if (!bar) {
spawn = !config->reading;
sway_log(SWAY_DEBUG, "Creating bar: %s", argv[0]);
bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE,
"Unable to allocate bar state");
}
bar->id = strdup(argv[0]);
}
config->current_bar = bar;
++argv; --argc;
} else if (!config->reading && strcmp(argv[0], "mode") != 0 &&
strcmp(argv[0], "hidden_state") != 0) {
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]);
}
}
if (!config->current_bar) {
if (config->reading) {
// Create new bar with default values
struct bar_config *bar = default_bar_config();
if (!bar) {
return cmd_results_new(CMD_FAILURE,
"Unable to allocate bar state");
}
// set bar id
int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
bar->id = malloc(len * sizeof(char));
if (bar->id) {
snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
} else {
return cmd_results_new(CMD_FAILURE, "Unable to allocate bar ID");
}
// Set current bar
config->current_bar = bar;
sway_log(SWAY_DEBUG, "Creating bar %s", bar->id);
}
}
if (find_handler(argv[0], bar_config_handlers,
sizeof(bar_config_handlers))) {
if (config->reading) {
return config_subcommand(argv, argc, bar_config_handlers,
sizeof(bar_config_handlers));
} else if (spawn) {
for (int i = config->bars->length - 1; i >= 0; i--) {
struct bar_config *bar = config->bars->items[i];
if (bar == config->current_bar) {
list_del(config->bars, i);
free_bar_config(bar);
break;
}
}
}
return cmd_results_new(CMD_INVALID,
"Can only be used in the config file.");
}
struct cmd_results *res =
config_subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
if (!config->reading) {
if (spawn) {
load_swaybar(config->current_bar);
}
config->current_bar = NULL;
}
return res;
}
|