aboutsummaryrefslogtreecommitdiffstats
path: root/swaybar/bar.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaybar/bar.c')
-rw-r--r--swaybar/bar.c464
1 files changed, 136 insertions, 328 deletions
diff --git a/swaybar/bar.c b/swaybar/bar.c
index f12923a8..0fc41517 100644
--- a/swaybar/bar.c
+++ b/swaybar/bar.c
@@ -1,390 +1,198 @@
1#define _XOPEN_SOURCE 500 1#define _XOPEN_SOURCE 500
2#include <assert.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <poll.h>
6#include <signal.h>
2#include <stdlib.h> 7#include <stdlib.h>
3#include <unistd.h>
4#include <string.h> 8#include <string.h>
5#include <fcntl.h>
6#include <errno.h>
7#include <sys/wait.h> 9#include <sys/wait.h>
8#include <signal.h> 10#include <unistd.h>
9#include <poll.h> 11#include <wayland-client.h>
10#ifdef __FreeBSD__ 12#include <wlr/util/log.h>
11#include <dev/evdev/input-event-codes.h>
12#else
13#include <linux/input-event-codes.h>
14#endif
15#ifdef ENABLE_TRAY
16#include <dbus/dbus.h>
17#include "swaybar/tray/sni_watcher.h"
18#include "swaybar/tray/tray.h"
19#include "swaybar/tray/sni.h"
20#endif
21#include "swaybar/ipc.h"
22#include "swaybar/render.h" 13#include "swaybar/render.h"
23#include "swaybar/config.h" 14#include "swaybar/config.h"
24#include "swaybar/status_line.h"
25#include "swaybar/event_loop.h" 15#include "swaybar/event_loop.h"
16#include "swaybar/status_line.h"
26#include "swaybar/bar.h" 17#include "swaybar/bar.h"
18#include "swaybar/ipc.h"
27#include "ipc-client.h" 19#include "ipc-client.h"
28#include "list.h" 20#include "list.h"
29#include "log.h" 21#include "pango.h"
22#include "pool-buffer.h"
23#include "wlr-layer-shell-unstable-v1-client-protocol.h"
30 24
31static void bar_init(struct bar *bar) { 25static void bar_init(struct swaybar *bar) {
32 bar->config = init_config(); 26 bar->config = init_config();
33 bar->status = init_status_line(); 27 wl_list_init(&bar->outputs);
34 bar->outputs = create_list();
35} 28}
36 29
37static void spawn_status_cmd_proc(struct bar *bar) { 30struct swaybar_output *new_output(const char *name) {
38 if (bar->config->status_command) { 31 struct swaybar_output *output = malloc(sizeof(struct swaybar_output));
39 int pipe_read_fd[2];
40 int pipe_write_fd[2];
41
42 if (pipe(pipe_read_fd) != 0) {
43 sway_log(L_ERROR, "Unable to create pipes for status_command fork");
44 return;
45 }
46 if (pipe(pipe_write_fd) != 0) {
47 sway_log(L_ERROR, "Unable to create pipe for status_command fork (write)");
48 close(pipe_read_fd[0]);
49 close(pipe_read_fd[1]);
50 return;
51 }
52
53 bar->status_command_pid = fork();
54 if (bar->status_command_pid == 0) {
55 close(pipe_read_fd[0]);
56 dup2(pipe_read_fd[1], STDOUT_FILENO);
57 close(pipe_read_fd[1]);
58
59 dup2(pipe_write_fd[0], STDIN_FILENO);
60 close(pipe_write_fd[0]);
61 close(pipe_write_fd[1]);
62
63 char *const cmd[] = {
64 "sh",
65 "-c",
66 bar->config->status_command,
67 NULL,
68 };
69 execvp(cmd[0], cmd);
70 return;
71 }
72
73 close(pipe_read_fd[1]);
74 bar->status_read_fd = pipe_read_fd[0];
75 fcntl(bar->status_read_fd, F_SETFL, O_NONBLOCK);
76
77 close(pipe_write_fd[0]);
78 bar->status_write_fd = pipe_write_fd[1];
79 fcntl(bar->status_write_fd, F_SETFL, O_NONBLOCK);
80 }
81}
82
83struct output *new_output(const char *name) {
84 struct output *output = malloc(sizeof(struct output));
85 output->name = strdup(name); 32 output->name = strdup(name);
86 output->window = NULL;
87 output->registry = NULL;
88 output->workspaces = create_list();
89#ifdef ENABLE_TRAY
90 output->items = create_list();
91#endif
92 return output; 33 return output;
93} 34}
94 35
95static void mouse_button_notify(struct window *window, int x, int y, 36static void layer_surface_configure(void *data,
96 uint32_t button, uint32_t state_w) { 37 struct zwlr_layer_surface_v1 *surface,
97 sway_log(L_DEBUG, "Mouse button %d clicked at %d %d %d", button, x, y, state_w); 38 uint32_t serial, uint32_t width, uint32_t height) {
98 if (!state_w) { 39 struct swaybar_output *output = data;
99 return; 40 output->width = width;
100 } 41 output->height = height;
101 42 zwlr_layer_surface_v1_ack_configure(surface, serial);
102 struct output *clicked_output = NULL; 43 render_frame(output->bar, output);
103 for (int i = 0; i < swaybar.outputs->length; i++) { 44}
104 struct output *output = swaybar.outputs->items[i];
105 if (window == output->window) {
106 clicked_output = output;
107 break;
108 }
109 }
110
111 if (!sway_assert(clicked_output != NULL, "Got pointer event for non-existing output")) {
112 return;
113 }
114
115 double button_x = 0.5;
116 for (int i = 0; i < clicked_output->workspaces->length; i++) {
117 struct workspace *workspace = clicked_output->workspaces->items[i];
118 int button_width, button_height;
119
120 workspace_button_size(window, workspace->name, &button_width, &button_height);
121 45
122 button_x += button_width; 46static void layer_surface_closed(void *_output,
123 if (x <= button_x) { 47 struct zwlr_layer_surface_v1 *surface) {
124 ipc_send_workspace_command(workspace->name); 48 // TODO: Deal with hotplugging
125 break; 49 struct swaybar_output *output = _output;
126 } 50 zwlr_layer_surface_v1_destroy(output->layer_surface);
127 } 51 wl_surface_destroy(output->surface);
52}
128 53
129 switch (button) { 54struct zwlr_layer_surface_v1_listener layer_surface_listener = {
130 case BTN_LEFT: 55 .configure = layer_surface_configure,
131 status_line_mouse_event(&swaybar, x, y, 1); 56 .closed = layer_surface_closed,
132 break; 57};
133 case BTN_MIDDLE: 58
134 status_line_mouse_event(&swaybar, x, y, 2); 59static void handle_global(void *data, struct wl_registry *registry,
135 break; 60 uint32_t name, const char *interface, uint32_t version) {
136 case BTN_RIGHT: 61 struct swaybar *bar = data;
137 status_line_mouse_event(&swaybar, x, y, 3); 62 if (strcmp(interface, wl_compositor_interface.name) == 0) {
138 break; 63 bar->compositor = wl_registry_bind(registry, name,
64 &wl_compositor_interface, 1);
65 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
66 bar->shm = wl_registry_bind(registry, name,
67 &wl_shm_interface, 1);
68 } else if (strcmp(interface, wl_output_interface.name) == 0) {
69 static size_t index = 0;
70 struct swaybar_output *output =
71 calloc(1, sizeof(struct swaybar_output));
72 output->bar = bar;
73 output->output = wl_registry_bind(registry, name,
74 &wl_output_interface, 1);
75 output->index = index++;
76 wl_list_init(&output->workspaces);
77 wl_list_insert(&bar->outputs, &output->link);
78 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
79 bar->layer_shell = wl_registry_bind(
80 registry, name, &zwlr_layer_shell_v1_interface, 1);
139 } 81 }
140
141#ifdef ENABLE_TRAY
142 tray_mouse_event(clicked_output, x, y, button, state_w);
143#endif
144
145} 82}
146 83
147static void mouse_scroll_notify(struct window *window, enum scroll_direction direction) { 84static void handle_global_remove(void *data, struct wl_registry *registry,
148 sway_log(L_DEBUG, "Mouse wheel scrolled %s", direction == SCROLL_UP ? "up" : "down"); 85 uint32_t name) {
86 // who cares
87}
149 88
150 // If there are status blocks and click_events are enabled 89static const struct wl_registry_listener registry_listener = {
151 // check if the position is within the status area and if so 90 .global = handle_global,
152 // tell the status line to output the event and skip workspace 91 .global_remove = handle_global_remove,
153 // switching below. 92};
154 int num_blocks = swaybar.status->block_line->length;
155 if (swaybar.status->click_events && num_blocks > 0) {
156 struct status_block *first_block = swaybar.status->block_line->items[0];
157 int x = window->pointer_input.last_x;
158 int y = window->pointer_input.last_y;
159 if (x > first_block->x) {
160 if (direction == SCROLL_UP) {
161 status_line_mouse_event(&swaybar, x, y, 4);
162 } else {
163 status_line_mouse_event(&swaybar, x, y, 5);
164 }
165 return;
166 }
167 }
168 93
169 if (!swaybar.config->wrap_scroll) { 94static void render_all_frames(struct swaybar *bar) {
170 // Find output this window lives on 95 struct swaybar_output *output;
171 int i; 96 wl_list_for_each(output, &bar->outputs, link) {
172 struct output *output = NULL; 97 render_frame(bar, output);
173 for (i = 0; i < swaybar.outputs->length; ++i) {
174 output = swaybar.outputs->items[i];
175 if (output->window == window) {
176 break;
177 }
178 }
179 if (!sway_assert(i != swaybar.outputs->length, "Unknown window in scroll event")) {
180 return;
181 }
182 int focused = -1;
183 for (i = 0; i < output->workspaces->length; ++i) {
184 struct workspace *ws = output->workspaces->items[i];
185 if (ws->focused) {
186 focused = i;
187 break;
188 }
189 }
190 if (!sway_assert(focused != -1, "Scroll wheel event received on inactive output")) {
191 return;
192 }
193 if ((focused == 0 && direction == SCROLL_UP) ||
194 (focused == output->workspaces->length - 1 && direction == SCROLL_DOWN)) {
195 // Do not wrap
196 return;
197 }
198 } 98 }
199
200 const char *workspace_name = direction == SCROLL_UP ? "prev_on_output" : "next_on_output";
201 ipc_send_workspace_command(workspace_name);
202} 99}
203 100
204void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) { 101void bar_setup(struct swaybar *bar,
205 /* initialize bar with default values */ 102 const char *socket_path, const char *bar_id) {
206 bar_init(bar); 103 bar_init(bar);
207
208 /* Initialize event loop lists */
209 init_event_loop(); 104 init_event_loop();
210 105
211 /* connect to sway ipc */
212 bar->ipc_socketfd = ipc_open_socket(socket_path); 106 bar->ipc_socketfd = ipc_open_socket(socket_path);
213 bar->ipc_event_socketfd = ipc_open_socket(socket_path); 107 bar->ipc_event_socketfd = ipc_open_socket(socket_path);
214 108 ipc_initialize(bar, bar_id);
215 ipc_bar_init(bar, bar_id); 109 if (bar->config->status_command) {
216 110 bar->status = status_line_init(bar->config->status_command);
217 int i;
218 for (i = 0; i < bar->outputs->length; ++i) {
219 struct output *bar_output = bar->outputs->items[i];
220
221 bar_output->registry = registry_poll();
222
223 if (!bar_output->registry->desktop_shell) {
224 sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
225 }
226
227 struct output_state *output = bar_output->registry->outputs->items[bar_output->idx];
228
229 bar_output->window = window_setup(bar_output->registry,
230 output->width / output->scale, 30, output->scale, false);
231 if (!bar_output->window) {
232 sway_abort("Failed to create window.");
233 }
234 desktop_shell_set_panel(bar_output->registry->desktop_shell,
235 output->output, bar_output->window->surface);
236 desktop_shell_set_panel_position(bar_output->registry->desktop_shell,
237 bar->config->position);
238
239 window_make_shell(bar_output->window);
240
241 /* set font */
242 bar_output->window->font = bar->config->font;
243
244 /* set mouse event callbacks */
245 bar_output->window->pointer_input.notify_button = mouse_button_notify;
246 bar_output->window->pointer_input.notify_scroll = mouse_scroll_notify;
247
248 /* set window height */
249 set_window_height(bar_output->window, bar->config->height);
250 }
251 /* spawn status command */
252 spawn_status_cmd_proc(bar);
253
254#ifdef ENABLE_TRAY
255 init_tray(bar);
256#endif
257}
258
259bool dirty = true;
260
261static void respond_ipc(int fd, short mask, void *_bar) {
262 struct bar *bar = (struct bar *)_bar;
263 sway_log(L_DEBUG, "Got IPC event.");
264 dirty = handle_ipc_event(bar);
265}
266
267static void respond_command(int fd, short mask, void *_bar) {
268 struct bar *bar = (struct bar *)_bar;
269 dirty = handle_status_line(bar);
270}
271
272static void respond_output(int fd, short mask, void *_output) {
273 struct output *output = (struct output *)_output;
274 if (wl_display_dispatch(output->registry->display) == -1) {
275 sway_log(L_ERROR, "failed to dispatch wl: %d", errno);
276 } 111 }
277}
278 112
279void bar_run(struct bar *bar) { 113 assert(bar->display = wl_display_connect(NULL));
280 add_event(bar->ipc_event_socketfd, POLLIN, respond_ipc, bar);
281 add_event(bar->status_read_fd, POLLIN, respond_command, bar);
282 114
283 int i; 115 struct wl_registry *registry = wl_display_get_registry(bar->display);
284 for (i = 0; i < bar->outputs->length; ++i) { 116 wl_registry_add_listener(registry, &registry_listener, bar);
285 struct output *output = bar->outputs->items[i]; 117 wl_display_roundtrip(bar->display);
286 add_event(wl_display_get_fd(output->registry->display), 118 assert(bar->compositor && bar->layer_shell && bar->shm);
287 POLLIN, respond_output, output);
288 }
289 119
290 while (1) { 120 // TODO: we might not necessarily be meant to do all of the outputs
291 if (dirty) { 121 struct swaybar_output *output;
292 int i; 122 wl_list_for_each(output, &bar->outputs, link) {
293 for (i = 0; i < bar->outputs->length; ++i) { 123 struct config_output *coutput;
294 struct output *output = bar->outputs->items[i]; 124 wl_list_for_each(coutput, &bar->config->outputs, link) {
295 if (window_prerender(output->window) && output->window->cairo) { 125 if (coutput->index != output->index) {
296 render(output, bar->config, bar->status); 126 continue;
297 window_render(output->window);
298 wl_display_flush(output->registry->display);
299 }
300 } 127 }
128 output->name = strdup(coutput->name);
129 assert(output->surface = wl_compositor_create_surface(
130 bar->compositor));
131 output->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
132 bar->layer_shell, output->surface, output->output,
133 ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "panel");
134 assert(output->layer_surface);
135 zwlr_layer_surface_v1_add_listener(output->layer_surface,
136 &layer_surface_listener, output);
137 zwlr_layer_surface_v1_set_anchor(output->layer_surface,
138 bar->config->position);
139 break;
301 } 140 }
302
303 dirty = false;
304
305 event_loop_poll();
306#ifdef ENABLE_TRAY
307 dispatch_dbus();
308#endif
309 } 141 }
142 ipc_get_workspaces(bar);
143 render_all_frames(bar);
310} 144}
311 145
312void free_workspaces(list_t *workspaces) { 146static void display_in(int fd, short mask, void *_bar) {
313 int i; 147 struct swaybar *bar = (struct swaybar *)_bar;
314 for (i = 0; i < workspaces->length; ++i) { 148 if (wl_display_dispatch(bar->display) == -1) {
315 struct workspace *ws = workspaces->items[i]; 149 bar_teardown(bar);
316 free(ws->name); 150 exit(0);
317 free(ws);
318 } 151 }
319 list_free(workspaces);
320} 152}
321 153
322static void free_output(struct output *output) { 154static void ipc_in(int fd, short mask, void *_bar) {
323 window_teardown(output->window); 155 struct swaybar *bar = (struct swaybar *)_bar;
324 if (output->registry) { 156 if (handle_ipc_event(bar)) {
325 registry_teardown(output->registry); 157 render_all_frames(bar);
326 } 158 }
159}
327 160
328 free(output->name); 161static void status_in(int fd, short mask, void *_bar) {
329 162 struct swaybar *bar = (struct swaybar *)_bar;
330 if (output->workspaces) { 163 if (handle_status_readable(bar->status)) {
331 free_workspaces(output->workspaces); 164 render_all_frames(bar);
332 } 165 }
333
334 free(output);
335} 166}
336 167
337static void free_outputs(list_t *outputs) { 168void bar_run(struct swaybar *bar) {
338 int i; 169 add_event(wl_display_get_fd(bar->display), POLLIN, display_in, bar);
339 for (i = 0; i < outputs->length; ++i) { 170 add_event(bar->ipc_event_socketfd, POLLIN, ipc_in, bar);
340 free_output(outputs->items[i]); 171 if (bar->status) {
172 add_event(bar->status->read_fd, POLLIN, status_in, bar);
173 }
174 while (1) {
175 event_loop_poll();
341 } 176 }
342 list_free(outputs);
343} 177}
344 178
345static void terminate_status_command(pid_t pid) { 179static void free_outputs(struct wl_list *list) {
346 if (pid) { 180 struct swaybar_output *output, *tmp;
347 // terminate status_command process 181 wl_list_for_each_safe(output, tmp, list, link) {
348 int ret = kill(pid, SIGTERM); 182 wl_list_remove(&output->link);
349 if (ret != 0) { 183 free(output->name);
350 sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid); 184 free(output);
351 } else {
352 int status;
353 waitpid(pid, &status, 0);
354 }
355 } 185 }
356} 186}
357 187
358void bar_teardown(struct bar *bar) { 188void bar_teardown(struct swaybar *bar) {
189 free_outputs(&bar->outputs);
359 if (bar->config) { 190 if (bar->config) {
360 free_config(bar->config); 191 free_config(bar->config);
361 } 192 }
362 193 close(bar->ipc_event_socketfd);
363 if (bar->outputs) { 194 close(bar->ipc_socketfd);
364 free_outputs(bar->outputs);
365 }
366
367 if (bar->status) { 195 if (bar->status) {
368 free_status_line(bar->status); 196 status_line_free(bar->status);
369 }
370
371 /* close sockets/pipes */
372 if (bar->status_read_fd) {
373 close(bar->status_read_fd);
374 }
375
376 if (bar->status_write_fd) {
377 close(bar->status_write_fd);
378 }
379
380 if (bar->ipc_socketfd) {
381 close(bar->ipc_socketfd);
382 } 197 }
383
384 if (bar->ipc_event_socketfd) {
385 close(bar->ipc_event_socketfd);
386 }
387
388 /* terminate status command process */
389 terminate_status_command(bar->status_command_pid);
390} 198}