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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#define _POSIX_C_SOURCE 200809L
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <wordexp.h>
#include "sway/config.h"
#include "sway/input/keyboard.h"
#include "sway/output.h"
#include "config.h"
#include "list.h"
#include "log.h"
#include "stringop.h"
#include "util.h"
void free_bar_binding(struct bar_binding *binding) {
if (!binding) {
return;
}
free(binding->command);
free(binding);
}
void free_bar_config(struct bar_config *bar) {
if (!bar) {
return;
}
free(bar->id);
free(bar->mode);
free(bar->position);
free(bar->hidden_state);
free(bar->status_command);
free(bar->swaybar_command);
free(bar->font);
free(bar->separator_symbol);
if (bar->bindings) {
for (int i = 0; i < bar->bindings->length; i++) {
free_bar_binding(bar->bindings->items[i]);
}
}
list_free(bar->bindings);
list_free_items_and_destroy(bar->outputs);
if (bar->client != NULL) {
wl_client_destroy(bar->client);
}
free(bar->colors.background);
free(bar->colors.statusline);
free(bar->colors.separator);
free(bar->colors.focused_background);
free(bar->colors.focused_statusline);
free(bar->colors.focused_separator);
free(bar->colors.focused_workspace_border);
free(bar->colors.focused_workspace_bg);
free(bar->colors.focused_workspace_text);
free(bar->colors.active_workspace_border);
free(bar->colors.active_workspace_bg);
free(bar->colors.active_workspace_text);
free(bar->colors.inactive_workspace_border);
free(bar->colors.inactive_workspace_bg);
free(bar->colors.inactive_workspace_text);
free(bar->colors.urgent_workspace_border);
free(bar->colors.urgent_workspace_bg);
free(bar->colors.urgent_workspace_text);
free(bar->colors.binding_mode_border);
free(bar->colors.binding_mode_bg);
free(bar->colors.binding_mode_text);
#if HAVE_TRAY
list_free_items_and_destroy(bar->tray_outputs);
free(bar->icon_theme);
struct tray_binding *tray_bind = NULL, *tmp_tray_bind = NULL;
wl_list_for_each_safe(tray_bind, tmp_tray_bind, &bar->tray_bindings, link) {
wl_list_remove(&tray_bind->link);
free(tray_bind);
}
#endif
free(bar);
}
struct bar_config *default_bar_config(void) {
struct bar_config *bar = NULL;
bar = calloc(1, sizeof(struct bar_config));
if (!bar) {
return NULL;
}
bar->outputs = NULL;
bar->position = strdup("bottom");
bar->pango_markup = false;
bar->swaybar_command = NULL;
bar->font = NULL;
bar->height = 0;
bar->workspace_buttons = true;
bar->wrap_scroll = false;
bar->separator_symbol = NULL;
bar->strip_workspace_numbers = false;
bar->strip_workspace_name = false;
bar->binding_mode_indicator = true;
bar->verbose = false;
bar->modifier = get_modifier_mask_by_name("Mod4");
bar->status_padding = 1;
bar->status_edge_padding = 3;
bar->workspace_min_width = 0;
if (!(bar->mode = strdup("dock"))) {
goto cleanup;
}
if (!(bar->hidden_state = strdup("hide"))) {
goto cleanup;
}
if (!(bar->bindings = create_list())) {
goto cleanup;
}
// set default colors
if (!(bar->colors.background = strndup("#000000ff", 9))) {
goto cleanup;
}
if (!(bar->colors.statusline = strndup("#ffffffff", 9))) {
goto cleanup;
}
if (!(bar->colors.separator = strndup("#666666ff", 9))) {
goto cleanup;
}
if (!(bar->colors.focused_workspace_border = strndup("#4c7899ff", 9))) {
goto cleanup;
}
if (!(bar->colors.focused_workspace_bg = strndup("#285577ff", 9))) {
goto cleanup;
}
if (!(bar->colors.focused_workspace_text = strndup("#ffffffff", 9))) {
goto cleanup;
}
if (!(bar->colors.active_workspace_border = strndup("#333333ff", 9))) {
goto cleanup;
}
if (!(bar->colors.active_workspace_bg = strndup("#5f676aff", 9))) {
goto cleanup;
}
if (!(bar->colors.active_workspace_text = strndup("#ffffffff", 9))) {
goto cleanup;
}
if (!(bar->colors.inactive_workspace_border = strndup("#333333ff", 9))) {
goto cleanup;
}
if (!(bar->colors.inactive_workspace_bg = strndup("#222222ff", 9))) {
goto cleanup;
}
if (!(bar->colors.inactive_workspace_text = strndup("#888888ff", 9))) {
goto cleanup;
}
if (!(bar->colors.urgent_workspace_border = strndup("#2f343aff", 9))) {
goto cleanup;
}
if (!(bar->colors.urgent_workspace_bg = strndup("#900000ff", 9))) {
goto cleanup;
}
if (!(bar->colors.urgent_workspace_text = strndup("#ffffffff", 9))) {
goto cleanup;
}
// if the following colors stay undefined, they fall back to background,
// statusline, separator and urgent_workspace_*.
bar->colors.focused_background = NULL;
bar->colors.focused_statusline = NULL;
bar->colors.focused_separator = NULL;
bar->colors.binding_mode_border = NULL;
bar->colors.binding_mode_bg = NULL;
bar->colors.binding_mode_text = NULL;
#if HAVE_TRAY
bar->tray_padding = 2;
wl_list_init(&bar->tray_bindings);
#endif
return bar;
cleanup:
free_bar_config(bar);
return NULL;
}
static void handle_swaybar_client_destroy(struct wl_listener *listener,
void *data) {
struct bar_config *bar = wl_container_of(listener, bar, client_destroy);
wl_list_remove(&bar->client_destroy.link);
wl_list_init(&bar->client_destroy.link);
bar->client = NULL;
}
static void invoke_swaybar(struct bar_config *bar) {
int sockets[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
sway_log_errno(SWAY_ERROR, "socketpair failed");
return;
}
if (!sway_set_cloexec(sockets[0], true) || !sway_set_cloexec(sockets[1], true)) {
return;
}
bar->client = wl_client_create(server.wl_display, sockets[0]);
if (bar->client == NULL) {
sway_log_errno(SWAY_ERROR, "wl_client_create failed");
return;
}
bar->client_destroy.notify = handle_swaybar_client_destroy;
wl_client_add_destroy_listener(bar->client, &bar->client_destroy);
pid_t pid = fork();
if (pid < 0) {
sway_log(SWAY_ERROR, "Failed to create fork for swaybar");
return;
} else if (pid == 0) {
// Remove the SIGUSR1 handler that wlroots adds for xwayland
sigset_t set;
sigemptyset(&set);
sigprocmask(SIG_SETMASK, &set, NULL);
pid = fork();
if (pid < 0) {
sway_log_errno(SWAY_ERROR, "fork failed");
_exit(EXIT_FAILURE);
} else if (pid == 0) {
if (!sway_set_cloexec(sockets[1], false)) {
_exit(EXIT_FAILURE);
}
char wayland_socket_str[16];
snprintf(wayland_socket_str, sizeof(wayland_socket_str),
"%d", sockets[1]);
setenv("WAYLAND_SOCKET", wayland_socket_str, true);
// run custom swaybar
char *const cmd[] = {
bar->swaybar_command ? bar->swaybar_command : "swaybar",
"-b", bar->id, NULL};
execvp(cmd[0], cmd);
_exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
if (close(sockets[1]) != 0) {
sway_log_errno(SWAY_ERROR, "close failed");
return;
}
if (waitpid(pid, NULL, 0) < 0) {
sway_log_errno(SWAY_ERROR, "waitpid failed");
return;
}
sway_log(SWAY_DEBUG, "Spawned swaybar %s", bar->id);
return;
}
void load_swaybar(struct bar_config *bar) {
if (bar->client != NULL) {
wl_client_destroy(bar->client);
}
sway_log(SWAY_DEBUG, "Invoking swaybar for bar id '%s'", bar->id);
invoke_swaybar(bar);
}
void load_swaybars(void) {
for (int i = 0; i < config->bars->length; ++i) {
struct bar_config *bar = config->bars->items[i];
load_swaybar(bar);
}
}
|