summaryrefslogtreecommitdiffstats
path: root/swaynag/swaynag.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaynag/swaynag.c')
-rw-r--r--swaynag/swaynag.c451
1 files changed, 451 insertions, 0 deletions
diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c
new file mode 100644
index 00000000..3966277d
--- /dev/null
+++ b/swaynag/swaynag.c
@@ -0,0 +1,451 @@
1#define _XOPEN_SOURCE 500
2#include <assert.h>
3#include <sys/stat.h>
4#include <sys/wait.h>
5#include <wayland-client.h>
6#include <wayland-cursor.h>
7#include "log.h"
8#include "list.h"
9#include "swaynag/render.h"
10#include "swaynag/swaynag.h"
11#include "swaynag/types.h"
12#include "wlr-layer-shell-unstable-v1-client-protocol.h"
13
14static void nop() {
15 // Intentionally left blank
16}
17
18static bool terminal_execute(char *terminal, char *command) {
19 char fname[] = "/tmp/swaynagXXXXXX";
20 FILE *tmp= fdopen(mkstemp(fname), "w");
21 if (!tmp) {
22 wlr_log(WLR_ERROR, "Failed to create temp script");
23 return false;
24 }
25 wlr_log(WLR_DEBUG, "Created temp script: %s", fname);
26 fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command);
27 fclose(tmp);
28 chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR);
29 char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1];
30 sprintf(cmd, "%s -e %s", terminal, fname);
31 execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
32 return true;
33}
34
35static void swaynag_button_execute(struct swaynag *swaynag,
36 struct swaynag_button *button) {
37 wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action);
38 if (button->type == SWAYNAG_ACTION_DISMISS) {
39 swaynag->run_display = false;
40 } else if (button->type == SWAYNAG_ACTION_EXPAND) {
41 swaynag->details.visible = !swaynag->details.visible;
42 render_frame(swaynag);
43 } else {
44 if (fork() == 0) {
45 // Child process. Will be used to prevent zombie processes
46 setsid();
47 if (fork() == 0) {
48 // Child of the child. Will be reparented to the init process
49 char *terminal = getenv("TERMINAL");
50 if (terminal && strlen(terminal)) {
51 wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal);
52 if (!terminal_execute(terminal, button->action)) {
53 swaynag_destroy(swaynag);
54 exit(EXIT_FAILURE);
55 }
56 } else {
57 wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly");
58 execl("/bin/sh", "/bin/sh", "-c", button->action, NULL);
59 }
60 }
61 exit(EXIT_SUCCESS);
62 }
63 }
64 wait(0);
65}
66
67static void layer_surface_configure(void *data,
68 struct zwlr_layer_surface_v1 *surface,
69 uint32_t serial, uint32_t width, uint32_t height) {
70 struct swaynag *swaynag = data;
71 swaynag->width = width;
72 swaynag->height = height;
73 zwlr_layer_surface_v1_ack_configure(surface, serial);
74 render_frame(swaynag);
75}
76
77static void layer_surface_closed(void *data,
78 struct zwlr_layer_surface_v1 *surface) {
79 struct swaynag *swaynag = data;
80 swaynag_destroy(swaynag);
81}
82
83static struct zwlr_layer_surface_v1_listener layer_surface_listener = {
84 .configure = layer_surface_configure,
85 .closed = layer_surface_closed,
86};
87
88static void surface_enter(void *data, struct wl_surface *surface,
89 struct wl_output *output) {
90 struct swaynag *swaynag = data;
91 struct swaynag_output *swaynag_output;
92 wl_list_for_each(swaynag_output, &swaynag->outputs, link) {
93 if (swaynag_output->wl_output == output) {
94 wlr_log(WLR_DEBUG, "Surface enter on output %s",
95 swaynag_output->name);
96 swaynag->output = swaynag_output;
97 swaynag->scale = swaynag->output->scale;
98 render_frame(swaynag);
99 break;
100 }
101 };
102}
103
104static struct wl_surface_listener surface_listener = {
105 .enter = surface_enter,
106 .leave = nop,
107};
108
109static void update_cursor(struct swaynag *swaynag) {
110 struct swaynag_pointer *pointer = &swaynag->pointer;
111 pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * swaynag->scale,
112 swaynag->shm);
113 struct wl_cursor *cursor =
114 wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr");
115 pointer->cursor_image = cursor->images[0];
116 wl_surface_set_buffer_scale(pointer->cursor_surface,
117 swaynag->scale);
118 wl_surface_attach(pointer->cursor_surface,
119 wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
120 wl_pointer_set_cursor(pointer->pointer, pointer->serial,
121 pointer->cursor_surface,
122 pointer->cursor_image->hotspot_x / swaynag->scale,
123 pointer->cursor_image->hotspot_y / swaynag->scale);
124 wl_surface_commit(pointer->cursor_surface);
125}
126
127static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
128 uint32_t serial, struct wl_surface *surface,
129 wl_fixed_t surface_x, wl_fixed_t surface_y) {
130 struct swaynag *swaynag = data;
131 struct swaynag_pointer *pointer = &swaynag->pointer;
132 pointer->serial = serial;
133 update_cursor(swaynag);
134}
135
136static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
137 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
138 struct swaynag *swaynag = data;
139 swaynag->pointer.x = wl_fixed_to_int(surface_x);
140 swaynag->pointer.y = wl_fixed_to_int(surface_y);
141}
142
143static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
144 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
145 struct swaynag *swaynag = data;
146
147 if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
148 return;
149 }
150
151 double x = swaynag->pointer.x * swaynag->scale;
152 double y = swaynag->pointer.y * swaynag->scale;
153 for (int i = 0; i < swaynag->buttons->length; i++) {
154 struct swaynag_button *nagbutton = swaynag->buttons->items[i];
155 if (x >= nagbutton->x
156 && y >= nagbutton->y
157 && x < nagbutton->x + nagbutton->width
158 && y < nagbutton->y + nagbutton->height) {
159 swaynag_button_execute(swaynag, nagbutton);
160 return;
161 }
162 }
163
164 if (swaynag->details.visible &&
165 swaynag->details.total_lines != swaynag->details.visible_lines) {
166 struct swaynag_button button_up = swaynag->details.button_up;
167 if (x >= button_up.x
168 && y >= button_up.y
169 && x < button_up.x + button_up.width
170 && y < button_up.y + button_up.height
171 && swaynag->details.offset > 0) {
172 swaynag->details.offset--;
173 render_frame(swaynag);
174 return;
175 }
176
177 struct swaynag_button button_down = swaynag->details.button_down;
178 int bot = swaynag->details.total_lines;
179 bot -= swaynag->details.visible_lines;
180 if (x >= button_down.x
181 && y >= button_down.y
182 && x < button_down.x + button_down.width
183 && y < button_down.y + button_down.height
184 && swaynag->details.offset < bot) {
185 swaynag->details.offset++;
186 render_frame(swaynag);
187 return;
188 }
189 }
190}
191
192static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
193 uint32_t time, uint32_t axis, wl_fixed_t value) {
194 struct swaynag *swaynag = data;
195 if (!swaynag->details.visible
196 || swaynag->pointer.x < swaynag->details.x
197 || swaynag->pointer.y < swaynag->details.y
198 || swaynag->pointer.x >= swaynag->details.x + swaynag->details.width
199 || swaynag->pointer.y >= swaynag->details.y + swaynag->details.height
200 || swaynag->details.total_lines == swaynag->details.visible_lines) {
201 return;
202 }
203
204 int direction = wl_fixed_to_int(value);
205 int bot = swaynag->details.total_lines - swaynag->details.visible_lines;
206 if (direction < 0 && swaynag->details.offset > 0) {
207 swaynag->details.offset--;
208 } else if (direction > 0 && swaynag->details.offset < bot) {
209 swaynag->details.offset++;
210 }
211
212 render_frame(swaynag);
213}
214
215static struct wl_pointer_listener pointer_listener = {
216 .enter = wl_pointer_enter,
217 .leave = nop,
218 .motion = wl_pointer_motion,
219 .button = wl_pointer_button,
220 .axis = wl_pointer_axis,
221 .frame = nop,
222 .axis_source = nop,
223 .axis_stop = nop,
224 .axis_discrete = nop,
225};
226
227static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
228 enum wl_seat_capability caps) {
229 struct swaynag *swaynag = data;
230 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
231 swaynag->pointer.pointer = wl_seat_get_pointer(wl_seat);
232 wl_pointer_add_listener(swaynag->pointer.pointer, &pointer_listener,
233 swaynag);
234 }
235}
236
237const struct wl_seat_listener seat_listener = {
238 .capabilities = seat_handle_capabilities,
239 .name = nop,
240};
241
242static void output_scale(void *data, struct wl_output *output,
243 int32_t factor) {
244 struct swaynag_output *swaynag_output = data;
245 swaynag_output->scale = factor;
246 if (swaynag_output->swaynag->output == swaynag_output) {
247 swaynag_output->swaynag->scale = swaynag_output->scale;
248 update_cursor(swaynag_output->swaynag);
249 render_frame(swaynag_output->swaynag);
250 }
251}
252
253static struct wl_output_listener output_listener = {
254 .geometry = nop,
255 .mode = nop,
256 .done = nop,
257 .scale = output_scale,
258};
259
260static void xdg_output_handle_name(void *data,
261 struct zxdg_output_v1 *xdg_output, const char *name) {
262 struct swaynag_output *swaynag_output = data;
263 char *outname = swaynag_output->swaynag->type->output;
264 wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname);
265 if (!swaynag_output->swaynag->output && outname && name
266 && strcmp(outname, name) == 0) {
267 wlr_log(WLR_DEBUG, "Using output %s", name);
268 swaynag_output->swaynag->output = swaynag_output;
269 }
270 swaynag_output->name = strdup(name);
271 zxdg_output_v1_destroy(xdg_output);
272 swaynag_output->swaynag->querying_outputs--;
273}
274
275static struct zxdg_output_v1_listener xdg_output_listener = {
276 .logical_position = nop,
277 .logical_size = nop,
278 .done = nop,
279 .name = xdg_output_handle_name,
280 .description = nop,
281};
282
283static void handle_global(void *data, struct wl_registry *registry,
284 uint32_t name, const char *interface, uint32_t version) {
285 struct swaynag *swaynag = data;
286 if (strcmp(interface, wl_compositor_interface.name) == 0) {
287 swaynag->compositor = wl_registry_bind(registry, name,
288 &wl_compositor_interface, 3);
289 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
290 swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
291 wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag);
292 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
293 swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
294 } else if (strcmp(interface, wl_output_interface.name) == 0) {
295 if (!swaynag->output && swaynag->xdg_output_manager) {
296 swaynag->querying_outputs++;
297 struct swaynag_output *output =
298 calloc(1, sizeof(struct swaynag_output));
299 output->wl_output = wl_registry_bind(registry, name,
300 &wl_output_interface, 3);
301 output->wl_name = name;
302 output->scale = 1;
303 output->swaynag = swaynag;
304 wl_list_insert(&swaynag->outputs, &output->link);
305 wl_output_add_listener(output->wl_output,
306 &output_listener, output);
307
308 struct zxdg_output_v1 *xdg_output;
309 xdg_output = zxdg_output_manager_v1_get_xdg_output(
310 swaynag->xdg_output_manager, output->wl_output);
311 zxdg_output_v1_add_listener(xdg_output,
312 &xdg_output_listener, output);
313 }
314 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
315 swaynag->layer_shell = wl_registry_bind(
316 registry, name, &zwlr_layer_shell_v1_interface, 1);
317 } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0
318 && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) {
319 swaynag->xdg_output_manager = wl_registry_bind(registry, name,
320 &zxdg_output_manager_v1_interface,
321 ZXDG_OUTPUT_V1_NAME_SINCE_VERSION);
322 }
323}
324
325static void handle_global_remove(void *data, struct wl_registry *registry,
326 uint32_t name) {
327 struct swaynag *swaynag = data;
328 if (swaynag->output->wl_name == name) {
329 swaynag->run_display = false;
330 }
331}
332
333static const struct wl_registry_listener registry_listener = {
334 .global = handle_global,
335 .global_remove = handle_global_remove,
336};
337
338void swaynag_setup(struct swaynag *swaynag) {
339 swaynag->display = wl_display_connect(NULL);
340 assert(swaynag->display);
341
342 swaynag->scale = 1;
343 wl_list_init(&swaynag->outputs);
344
345 struct wl_registry *registry = wl_display_get_registry(swaynag->display);
346 wl_registry_add_listener(registry, &registry_listener, swaynag);
347 wl_display_roundtrip(swaynag->display);
348 assert(swaynag->compositor && swaynag->layer_shell && swaynag->shm);
349
350 while (swaynag->querying_outputs > 0) {
351 wl_display_roundtrip(swaynag->display);
352 }
353
354 if (!swaynag->output && swaynag->type->output) {
355 wlr_log(WLR_ERROR, "Output '%s' not found", swaynag->type->output);
356 swaynag_destroy(swaynag);
357 exit(EXIT_FAILURE);
358 }
359
360 struct swaynag_pointer *pointer = &swaynag->pointer;
361 pointer->cursor_surface = wl_compositor_create_surface(swaynag->compositor);
362 assert(pointer->cursor_surface);
363
364 swaynag->surface = wl_compositor_create_surface(swaynag->compositor);
365 assert(swaynag->surface);
366 wl_surface_add_listener(swaynag->surface, &surface_listener, swaynag);
367
368 swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
369 swaynag->layer_shell, swaynag->surface,
370 swaynag->output ? swaynag->output->wl_output : NULL,
371 ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag");
372 assert(swaynag->layer_surface);
373 zwlr_layer_surface_v1_add_listener(swaynag->layer_surface,
374 &layer_surface_listener, swaynag);
375 zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface,
376 swaynag->type->anchors);
377
378 wl_registry_destroy(registry);
379}
380
381void swaynag_run(struct swaynag *swaynag) {
382 swaynag->run_display = true;
383 render_frame(swaynag);
384 while (swaynag->run_display
385 && wl_display_dispatch(swaynag->display) != -1) {
386 // This is intentionally left blank
387 }
388}
389
390void swaynag_destroy(struct swaynag *swaynag) {
391 swaynag->run_display = false;
392
393 free(swaynag->message);
394 while (swaynag->buttons->length) {
395 struct swaynag_button *button = swaynag->buttons->items[0];
396 list_del(swaynag->buttons, 0);
397 free(button->text);
398 free(button->action);
399 free(button);
400 }
401 list_free(swaynag->buttons);
402 free(swaynag->details.message);
403 free(swaynag->details.button_up.text);
404 free(swaynag->details.button_down.text);
405
406 if (swaynag->type) {
407 swaynag_type_free(swaynag->type);
408 }
409
410 if (swaynag->layer_surface) {
411 zwlr_layer_surface_v1_destroy(swaynag->layer_surface);
412 }
413
414 if (swaynag->surface) {
415 wl_surface_destroy(swaynag->surface);
416 }
417
418 if (swaynag->pointer.cursor_theme) {
419 wl_cursor_theme_destroy(swaynag->pointer.cursor_theme);
420 }
421
422 if (&swaynag->buffers[0]) {
423 destroy_buffer(&swaynag->buffers[0]);
424 }
425
426 if (&swaynag->buffers[1]) {
427 destroy_buffer(&swaynag->buffers[1]);
428 }
429
430 if (swaynag->outputs.prev || swaynag->outputs.next) {
431 struct swaynag_output *output, *temp;
432 wl_list_for_each_safe(output, temp, &swaynag->outputs, link) {
433 wl_output_destroy(output->wl_output);
434 free(output->name);
435 wl_list_remove(&output->link);
436 free(output);
437 };
438 }
439
440 if (swaynag->compositor) {
441 wl_compositor_destroy(swaynag->compositor);
442 }
443
444 if (swaynag->shm) {
445 wl_shm_destroy(swaynag->shm);
446 }
447
448 if (swaynag->display) {
449 wl_display_disconnect(swaynag->display);
450 }
451}