aboutsummaryrefslogtreecommitdiffstats
path: root/swaynag/nagbar.c
blob: e451a53a676028e8f965bf54362bb77e32580fb1 (plain) (blame)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include "log.h"
#include "list.h"
#include "swaynag/nagbar.h"
#include "swaynag/render.h"
#include "swaynag/types.h"
#include "wlr-layer-shell-unstable-v1-client-protocol.h"

static void nop() {
	// Intentionally left blank
}

static bool terminal_execute(char *terminal, char *command) {
	char fname[] = "/tmp/swaynagXXXXXX";
	FILE *tmp= fdopen(mkstemp(fname), "w");
	if (!tmp) {
		wlr_log(WLR_ERROR, "Failed to create temp script");
		return false;
	}
	wlr_log(WLR_DEBUG, "Created temp script: %s", fname);
	fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command);
	fclose(tmp);
	chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR);
	char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1];
	sprintf(cmd, "%s -e %s", terminal, fname);
	execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
	return true;
}

static void nagbar_button_execute(struct sway_nagbar *nagbar,
		struct sway_nagbar_button *button) {
	wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action);
	if (button->type == NAGBAR_ACTION_DISMISS) {
		nagbar->run_display = false;
	} else if (button->type == NAGBAR_ACTION_EXPAND) {
		nagbar->details.visible = !nagbar->details.visible;
		render_frame(nagbar);
	} else {
		if (fork() == 0) {
			// Child process. Will be used to prevent zombie processes
			setsid();
			if (fork() == 0) {
				// Child of the child. Will be reparented to the init process
				char *terminal = getenv("TERMINAL");
				if (terminal && strlen(terminal)) {
					wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal);
					if (!terminal_execute(terminal, button->action)) {
						nagbar_destroy(nagbar);
						exit(EXIT_FAILURE);
					}
				} else {
					wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly");
					execl("/bin/sh", "/bin/sh", "-c", button->action, NULL);
				}
			}
			exit(EXIT_SUCCESS);
		}
	}
	wait(0);
}

static void layer_surface_configure(void *data,
		struct zwlr_layer_surface_v1 *surface,
		uint32_t serial, uint32_t width, uint32_t height) {
	struct sway_nagbar *nagbar = data;
	nagbar->width = width;
	nagbar->height = height;
	zwlr_layer_surface_v1_ack_configure(surface, serial);
	render_frame(nagbar);
}

static void layer_surface_closed(void *data,
		struct zwlr_layer_surface_v1 *surface) {
	struct sway_nagbar *nagbar = data;
	nagbar_destroy(nagbar);
}

static struct zwlr_layer_surface_v1_listener layer_surface_listener = {
	.configure = layer_surface_configure,
	.closed = layer_surface_closed,
};

static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
		uint32_t serial, struct wl_surface *surface,
		wl_fixed_t surface_x, wl_fixed_t surface_y) {
	struct sway_nagbar *nagbar = data;
	struct sway_nagbar_pointer *pointer = &nagbar->pointer;
	wl_surface_set_buffer_scale(pointer->cursor_surface, nagbar->scale);
	wl_surface_attach(pointer->cursor_surface,
			wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
	wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
			pointer->cursor_image->hotspot_x / nagbar->scale,
			pointer->cursor_image->hotspot_y / nagbar->scale);
	wl_surface_commit(pointer->cursor_surface);
}

static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
		uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
	struct sway_nagbar *nagbar = data;
	nagbar->pointer.x = wl_fixed_to_int(surface_x);
	nagbar->pointer.y = wl_fixed_to_int(surface_y);
}

static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
		uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
	struct sway_nagbar *nagbar = data;

	if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
		return;
	}

	double x = nagbar->pointer.x * nagbar->scale;
	double y = nagbar->pointer.y * nagbar->scale;
	for (int i = 0; i < nagbar->buttons->length; i++) {
		struct sway_nagbar_button *nagbutton = nagbar->buttons->items[i];
		if (x >= nagbutton->x
				&& y >= nagbutton->y
				&& x < nagbutton->x + nagbutton->width
				&& y < nagbutton->y + nagbutton->height) {
			nagbar_button_execute(nagbar, nagbutton);
			return;
		}
	}

	if (nagbar->details.visible &&
			nagbar->details.total_lines != nagbar->details.visible_lines) {
		struct sway_nagbar_button button_up = nagbar->details.button_up;
		if (x >= button_up.x
				&& y >= button_up.y
				&& x < button_up.x + button_up.width
				&& y < button_up.y + button_up.height
				&& nagbar->details.offset > 0) {
			nagbar->details.offset--;
			render_frame(nagbar);
			return;
		}

		struct sway_nagbar_button button_down = nagbar->details.button_down;
		int bot = nagbar->details.total_lines - nagbar->details.visible_lines;
		if (x >= button_down.x
				&& y >= button_down.y
				&& x < button_down.x + button_down.width
				&& y < button_down.y + button_down.height
				&& nagbar->details.offset < bot) {
			nagbar->details.offset++;
			render_frame(nagbar);
			return;
		}
	}
}

static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
		uint32_t time, uint32_t axis, wl_fixed_t value) {
	struct sway_nagbar *nagbar = data;
	if (!nagbar->details.visible
			|| nagbar->pointer.x < nagbar->details.x
			|| nagbar->pointer.y < nagbar->details.y
			|| nagbar->pointer.x >= nagbar->details.x + nagbar->details.width
			|| nagbar->pointer.y >= nagbar->details.y + nagbar->details.height
			|| nagbar->details.total_lines == nagbar->details.visible_lines) {
		return;
	}

	int direction = wl_fixed_to_int(value);
	int bot = nagbar->details.total_lines - nagbar->details.visible_lines;
	if (direction < 0 && nagbar->details.offset > 0) {
		nagbar->details.offset--;
	} else if (direction > 0 && nagbar->details.offset < bot) {
		nagbar->details.offset++;
	}

	render_frame(nagbar);
}

static struct wl_pointer_listener pointer_listener = {
	.enter = wl_pointer_enter,
	.leave = nop,
	.motion = wl_pointer_motion,
	.button = wl_pointer_button,
	.axis = wl_pointer_axis,
	.frame = nop,
	.axis_source = nop,
	.axis_stop = nop,
	.axis_discrete = nop,
};

static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
		enum wl_seat_capability caps) {
	struct sway_nagbar *nagbar = data;
	if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
		nagbar->pointer.pointer = wl_seat_get_pointer(wl_seat);
		wl_pointer_add_listener(nagbar->pointer.pointer, &pointer_listener,
				nagbar);
	}
}

const struct wl_seat_listener seat_listener = {
	.capabilities = seat_handle_capabilities,
	.name = nop,
};

static void output_scale(void *data, struct wl_output *output,
		int32_t factor) {
	struct sway_nagbar *nagbar = data;
	nagbar->scale = factor;
	render_frame(nagbar);
}

static struct wl_output_listener output_listener = {
	.geometry = nop,
	.mode = nop,
	.done = nop,
	.scale = output_scale,
};

struct output_state {
	struct wl_output *wl_output;
	uint32_t wl_name;
	struct zxdg_output_v1 *xdg_output;
	struct sway_nagbar *nagbar;
};

static void xdg_output_handle_name(void *data,
		struct zxdg_output_v1 *xdg_output, const char *name) {
	struct output_state *state = data;
	char *outname = state->nagbar->output.name;
	wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname);
	if (outname && !state->nagbar->output.wl_output) {
		wlr_log(WLR_DEBUG, "Using output %s", name);
		state->nagbar->output.wl_output = state->wl_output;
		state->nagbar->output.wl_name = state->wl_name;
		wl_output_add_listener(state->nagbar->output.wl_output,
				&output_listener, state->nagbar);
		wl_display_roundtrip(state->nagbar->display);
		zxdg_output_v1_destroy(state->xdg_output);
	} else {
		zxdg_output_v1_destroy(state->xdg_output);
		wl_output_destroy(state->wl_output);
	}
	state->nagbar->querying_outputs--;
	free(state);
}

static struct zxdg_output_v1_listener xdg_output_listener = {
	.logical_position = nop,
	.logical_size = nop,
	.done = nop,
	.name = xdg_output_handle_name,
	.description = nop,
};

static void handle_global(void *data, struct wl_registry *registry,
		uint32_t name, const char *interface, uint32_t version) {
	struct sway_nagbar *nagbar = data;
	if (strcmp(interface, wl_compositor_interface.name) == 0) {
		nagbar->compositor = wl_registry_bind(registry, name,
				&wl_compositor_interface, 3);
	} else if (strcmp(interface, wl_seat_interface.name) == 0) {
		nagbar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
		wl_seat_add_listener(nagbar->seat, &seat_listener, nagbar);
	} else if (strcmp(interface, wl_shm_interface.name) == 0) {
		nagbar->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
	} else if (strcmp(interface, wl_output_interface.name) == 0) {
		if (!nagbar->output.wl_output && nagbar->xdg_output_manager) {
			nagbar->querying_outputs++;
			struct output_state *state =
				calloc(1, sizeof(struct output_state));
			state->nagbar = nagbar;
			state->wl_output = wl_registry_bind(registry, name,
					&wl_output_interface, 3);
			state->wl_name = name;
			state->xdg_output = zxdg_output_manager_v1_get_xdg_output(
					nagbar->xdg_output_manager, state->wl_output);
			zxdg_output_v1_add_listener(state->xdg_output,
					&xdg_output_listener, state);
		}
	} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
		nagbar->layer_shell = wl_registry_bind(
				registry, name, &zwlr_layer_shell_v1_interface, 1);
	} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0
			&& version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) {
		nagbar->xdg_output_manager = wl_registry_bind(registry, name,
				&zxdg_output_manager_v1_interface,
				ZXDG_OUTPUT_V1_NAME_SINCE_VERSION);
	}
}

static void handle_global_remove(void *data, struct wl_registry *registry,
		uint32_t name) {
	struct sway_nagbar *nagbar = data;
	if (nagbar->output.wl_name == name) {
		nagbar->run_display = false;
	}
}

static const struct wl_registry_listener registry_listener = {
	.global = handle_global,
	.global_remove = handle_global_remove,
};

void nagbar_setup(struct sway_nagbar *nagbar) {
	nagbar->display = wl_display_connect(NULL);
	assert(nagbar->display);

	nagbar->scale = 1;

	struct wl_registry *registry = wl_display_get_registry(nagbar->display);
	wl_registry_add_listener(registry, &registry_listener, nagbar);
	wl_display_roundtrip(nagbar->display);
	assert(nagbar->compositor && nagbar->layer_shell && nagbar->shm);

	while (nagbar->querying_outputs > 0) {
		wl_display_roundtrip(nagbar->display);
	}

	if (!nagbar->output.wl_output && nagbar->output.name) {
		wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name);
		nagbar_destroy(nagbar);
		exit(EXIT_FAILURE);
	}

	struct sway_nagbar_pointer *pointer = &nagbar->pointer;
	int scale = nagbar->scale < 1 ? 1 : nagbar->scale;
	pointer->cursor_theme = wl_cursor_theme_load(
			NULL, 24 * scale, nagbar->shm);
	assert(pointer->cursor_theme);
	struct wl_cursor *cursor =
		wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr");
	assert(cursor);
	pointer->cursor_image = cursor->images[0];
	pointer->cursor_surface = wl_compositor_create_surface(nagbar->compositor);
	assert(pointer->cursor_surface);

	nagbar->surface = wl_compositor_create_surface(nagbar->compositor);
	assert(nagbar->surface);
	nagbar->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
			nagbar->layer_shell, nagbar->surface, nagbar->output.wl_output,
			ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag");
	assert(nagbar->layer_surface);
	zwlr_layer_surface_v1_add_listener(nagbar->layer_surface,
			&layer_surface_listener, nagbar);
	zwlr_layer_surface_v1_set_anchor(nagbar->layer_surface, nagbar->anchors);

	wl_registry_destroy(registry);
}

void nagbar_run(struct sway_nagbar *nagbar) {
	nagbar->run_display = true;
	render_frame(nagbar);
	while (nagbar->run_display && wl_display_dispatch(nagbar->display) != -1) {
		// This is intentionally left blank
	}
}

void nagbar_destroy(struct sway_nagbar *nagbar) {
	nagbar->run_display = false;

	free(nagbar->message);
	free(nagbar->font);
	while (nagbar->buttons->length) {
		struct sway_nagbar_button *button = nagbar->buttons->items[0];
		list_del(nagbar->buttons, 0);
		free(button->text);
		free(button->action);
		free(button);
	}
	list_free(nagbar->buttons);
	free(nagbar->details.message);
	free(nagbar->details.button_up.text);
	free(nagbar->details.button_down.text);

	if (nagbar->type) {
		nagbar_type_free(nagbar->type);
	}

	if (nagbar->layer_surface) {
		zwlr_layer_surface_v1_destroy(nagbar->layer_surface);
	}

	if (nagbar->surface) {
		wl_surface_destroy(nagbar->surface);
	}

	if (nagbar->output.wl_output) {
		wl_output_destroy(nagbar->output.wl_output);
	}

	if (&nagbar->buffers[0]) {
		destroy_buffer(&nagbar->buffers[0]);
	}

	if (&nagbar->buffers[1]) {
		destroy_buffer(&nagbar->buffers[1]);
	}

	if (nagbar->compositor) {
		wl_compositor_destroy(nagbar->compositor);
	}

	if (nagbar->shm) {
		wl_shm_destroy(nagbar->shm);
	}

	if (nagbar->display) {
		wl_display_disconnect(nagbar->display);
	}
}