aboutsummaryrefslogtreecommitdiff
path: root/swaybar/bar.c
blob: cdaf6a37ce2567f7e85760d31f8f59d049bd28c3 (plain)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#define _XOPEN_SOURCE 500
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#include <poll.h>
#ifdef ENABLE_TRAY
#include <dbus/dbus.h>
#include "swaybar/tray/sni_watcher.h"
#include "swaybar/tray/tray.h"
#include "swaybar/tray/sni.h"
#endif
#include "swaybar/ipc.h"
#include "swaybar/render.h"
#include "swaybar/config.h"
#include "swaybar/status_line.h"
#include "swaybar/event_loop.h"
#include "swaybar/bar.h"
#include "ipc-client.h"
#include "list.h"
#include "log.h"

static void bar_init(struct bar *bar) {
	bar->config = init_config();
	bar->status = init_status_line();
	bar->outputs = create_list();
}

static void spawn_status_cmd_proc(struct bar *bar) {
	if (bar->config->status_command) {
		int pipefd[2];
		if (pipe(pipefd) != 0) {
			sway_log(L_ERROR, "Unable to create pipe for status_command fork");
			return;
		}
		bar->status_command_pid = fork();
		if (bar->status_command_pid == 0) {
			close(pipefd[0]);
			dup2(pipefd[1], STDOUT_FILENO);
			close(pipefd[1]);
			char *const cmd[] = {
				"sh",
				"-c",
				bar->config->status_command,
				NULL,
			};
			execvp(cmd[0], cmd);
			return;
		}

		close(pipefd[1]);
		bar->status_read_fd = pipefd[0];
		fcntl(bar->status_read_fd, F_SETFL, O_NONBLOCK);
	}
}

#ifdef ENABLE_TRAY
static void spawn_xembed_sni_proxy() {
	pid_t pid = fork();
	if (pid == 0) {
		int wstatus;
		do {
			pid = fork();
			if (pid == 0) {
				execlp("xembedsniproxy", "xembedsniproxy", NULL);
				_exit(EXIT_FAILURE);
			}
			waitpid(pid, &wstatus, 0);
		} while (!WIFEXITED(wstatus));
		_exit(EXIT_FAILURE);
	}
}
#endif

struct output *new_output(const char *name) {
	struct output *output = malloc(sizeof(struct output));
	output->name = strdup(name);
	output->window = NULL;
	output->registry = NULL;
	output->workspaces = create_list();
#ifdef ENABLE_TRAY
	output->items = create_list();
#endif
	return output;
}

static void mouse_button_notify(struct window *window, int x, int y,
		uint32_t button, uint32_t state_w) {
	sway_log(L_DEBUG, "Mouse button %d clicked at %d %d %d", button, x, y, state_w);
	if (!state_w) {
		return;
	}

	struct output *clicked_output = NULL;
	for (int i = 0; i < swaybar.outputs->length; i++) {
		struct output *output = swaybar.outputs->items[i];
		if (window == output->window) {
			clicked_output = output;
			break;
		}
	}

	if (!sway_assert(clicked_output != NULL, "Got pointer event for non-existing output")) {
		return;
	}

	double button_x = 0.5;
	for (int i = 0; i < clicked_output->workspaces->length; i++) {
		struct workspace *workspace = clicked_output->workspaces->items[i];
		int button_width, button_height;

		workspace_button_size(window, workspace->name, &button_width, &button_height);

		button_x += button_width;
		if (x <= button_x) {
			ipc_send_workspace_command(workspace->name);
			break;
		}
	}

#ifdef ENABLE_TRAY
	uint32_t tray_padding = swaybar.config->tray_padding;
	int tray_width = window->width * window->scale;

	for (int i = 0; i < clicked_output->items->length; ++i) {
		struct sni_icon_ref *item =
			 clicked_output->items->items[i];
		int icon_width = cairo_image_surface_get_width(item->icon);

		tray_width -= tray_padding;
		if (x <= tray_width && x >= tray_width - icon_width) {
			if (button == swaybar.config->activate_button) {
				sni_activate(item->ref, x, y);
			} else if (button == swaybar.config->context_button) {
				sni_context_menu(item->ref, x, y);
			} else if (button == swaybar.config->secondary_button) {
				sni_secondary(item->ref, x, y);
			}
			break;
		}
		tray_width -= icon_width;
	}
#endif
}

static void mouse_scroll_notify(struct window *window, enum scroll_direction direction) {
	sway_log(L_DEBUG, "Mouse wheel scrolled %s", direction == SCROLL_UP ? "up" : "down");

	if (!swaybar.config->wrap_scroll) {
		// Find output this window lives on
		int i;
		struct output *output = NULL;
		for (i = 0; i < swaybar.outputs->length; ++i) {
			output = swaybar.outputs->items[i];
			if (output->window == window) {
				break;
			}
		}
		if (!sway_assert(i != swaybar.outputs->length, "Unknown window in scroll event")) {
			return;
		}
		int focused = -1;
		for (i = 0; i < output->workspaces->length; ++i) {
			struct workspace *ws = output->workspaces->items[i];
			if (ws->focused) {
				focused = i;
				break;
			}
		}
		if (!sway_assert(focused != -1, "Scroll wheel event received on inactive output")) {
			return;
		}
		if ((focused == 0 && direction == SCROLL_UP) ||
				(focused == output->workspaces->length - 1 && direction == SCROLL_DOWN)) {
			// Do not wrap
			return;
		}
	}

	const char *workspace_name = direction == SCROLL_UP ? "prev_on_output" : "next_on_output";
	ipc_send_workspace_command(workspace_name);
}

void bar_setup(struct bar *bar, const char *socket_path, const char *bar_id) {
	/* initialize bar with default values */
	bar_init(bar);

	/* Initialize event loop lists */
	init_event_loop();

	/* connect to sway ipc */
	bar->ipc_socketfd = ipc_open_socket(socket_path);
	bar->ipc_event_socketfd = ipc_open_socket(socket_path);

	ipc_bar_init(bar, bar_id);

	int i;
	for (i = 0; i < bar->outputs->length; ++i) {
		struct output *bar_output = bar->outputs->items[i];

		bar_output->registry = registry_poll();

		if (!bar_output->registry->desktop_shell) {
			sway_abort("swaybar requires the compositor to support the desktop-shell extension.");
		}

		struct output_state *output = bar_output->registry->outputs->items[bar_output->idx];

		bar_output->window = window_setup(bar_output->registry,
				output->width / output->scale, 30, output->scale, false);
		if (!bar_output->window) {
			sway_abort("Failed to create window.");
		}
		desktop_shell_set_panel(bar_output->registry->desktop_shell,
				output->output, bar_output->window->surface);
		desktop_shell_set_panel_position(bar_output->registry->desktop_shell,
				bar->config->position);

		window_make_shell(bar_output->window);

		/* set font */
		bar_output->window->font = bar->config->font;

		/* set mouse event callbacks */
		bar_output->window->pointer_input.notify_button = mouse_button_notify;
		bar_output->window->pointer_input.notify_scroll = mouse_scroll_notify;

		/* set window height */
		set_window_height(bar_output->window, bar->config->height);
	}
	/* spawn status command */
	spawn_status_cmd_proc(bar);

#ifdef ENABLE_TRAY
	// We should have at least one output to serve the tray to
	if (!swaybar.config->tray_output || strcmp(swaybar.config->tray_output, "none") != 0) {
		/* Connect to the D-Bus */
		dbus_init();

		/* Start the SNI watcher */
		init_sni_watcher();

		/* Start the SNI host */
		init_tray();

		/* Start xembedsniproxy */
		spawn_xembed_sni_proxy();
	}
#endif
}

bool dirty = true;

static void respond_ipc(int fd, short mask, void *_bar) {
	struct bar *bar = (struct bar *)_bar;
	sway_log(L_DEBUG, "Got IPC event.");
	dirty = handle_ipc_event(bar);
}

static void respond_command(int fd, short mask, void *_bar) {
	struct bar *bar = (struct bar *)_bar;
	dirty = handle_status_line(bar);
}

static void respond_output(int fd, short mask, void *_output) {
	struct output *output = (struct output *)_output;
	if (wl_display_dispatch(output->registry->display) == -1) {
		sway_log(L_ERROR, "failed to dispatch wl: %d", errno);
	}
}

void bar_run(struct bar *bar) {
	add_event(bar->ipc_event_socketfd, POLLIN, respond_ipc, bar);
	add_event(bar->status_read_fd, POLLIN, respond_command, bar);

	int i;
	for (i = 0; i < bar->outputs->length; ++i) {
		struct output *output = bar->outputs->items[i];
		add_event(wl_display_get_fd(output->registry->display),
				POLLIN, respond_output, output);
	}

	while (1) {
		if (dirty) {
			int i;
			for (i = 0; i < bar->outputs->length; ++i) {
				struct output *output = bar->outputs->items[i];
				if (window_prerender(output->window) && output->window->cairo) {
					render(output, bar->config, bar->status);
					window_render(output->window);
					wl_display_flush(output->registry->display);
				}
			}
		}

		dirty = false;

		event_loop_poll();
#ifdef ENABLE_TRAY
		dispatch_dbus();
#endif
	}
}

void free_workspaces(list_t *workspaces) {
	int i;
	for (i = 0; i < workspaces->length; ++i) {
		struct workspace *ws = workspaces->items[i];
		free(ws->name);
		free(ws);
	}
	list_free(workspaces);
}

static void free_output(struct output *output) {
	window_teardown(output->window);
	if (output->registry) {
		registry_teardown(output->registry);
	}

	free(output->name);

	if (output->workspaces) {
		free_workspaces(output->workspaces);
	}

	free(output);
}

static void free_outputs(list_t *outputs) {
	int i;
	for (i = 0; i < outputs->length; ++i) {
		free_output(outputs->items[i]);
	}
	list_free(outputs);
}

static void terminate_status_command(pid_t pid) {
	if (pid) {
		// terminate status_command process
		int ret = kill(pid, SIGTERM);
		if (ret != 0) {
			sway_log(L_ERROR, "Unable to terminate status_command [pid: %d]", pid);
		} else {
			int status;
			waitpid(pid, &status, 0);
		}
	}
}

void bar_teardown(struct bar *bar) {
	if (bar->config) {
		free_config(bar->config);
	}

	if (bar->outputs) {
		free_outputs(bar->outputs);
	}

	if (bar->status) {
		free_status_line(bar->status);
	}

	/* close sockets/pipes */
	if (bar->status_read_fd) {
		close(bar->status_read_fd);
	}

	if (bar->ipc_socketfd) {
		close(bar->ipc_socketfd);
	}

	if (bar->ipc_event_socketfd) {
		close(bar->ipc_event_socketfd);
	}

	/* terminate status command process */
	terminate_status_command(bar->status_command_pid);
}