aboutsummaryrefslogtreecommitdiffstats
path: root/swaynag/swaynag.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaynag/swaynag.c')
-rw-r--r--swaynag/swaynag.c154
1 files changed, 82 insertions, 72 deletions
diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c
index d9bec368..50eea148 100644
--- a/swaynag/swaynag.c
+++ b/swaynag/swaynag.c
@@ -1,4 +1,3 @@
1#define _POSIX_C_SOURCE 200809L
2#include <stdlib.h> 1#include <stdlib.h>
3#include <assert.h> 2#include <assert.h>
4#include <sys/stat.h> 3#include <sys/stat.h>
@@ -28,10 +27,15 @@ static bool terminal_execute(char *terminal, char *command) {
28 fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); 27 fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command);
29 fclose(tmp); 28 fclose(tmp);
30 chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); 29 chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR);
31 char *cmd = malloc(sizeof(char) * (strlen(terminal) + strlen(" -e ") + strlen(fname) + 1)); 30 size_t cmd_size = strlen(terminal) + strlen(" -e ") + strlen(fname) + 1;
32 sprintf(cmd, "%s -e %s", terminal, fname); 31 char *cmd = malloc(cmd_size);
33 execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); 32 if (!cmd) {
34 sway_log_errno(SWAY_ERROR, "Failed to run command, execl() returned."); 33 perror("malloc");
34 return false;
35 }
36 snprintf(cmd, cmd_size, "%s -e %s", terminal, fname);
37 execlp("sh", "sh", "-c", cmd, NULL);
38 sway_log_errno(SWAY_ERROR, "Failed to run command, execlp() returned.");
35 free(cmd); 39 free(cmd);
36 return false; 40 return false;
37} 41}
@@ -58,7 +62,7 @@ static void swaynag_button_execute(struct swaynag *swaynag,
58 } else if (pid == 0) { 62 } else if (pid == 0) {
59 // Child of the child. Will be reparented to the init process 63 // Child of the child. Will be reparented to the init process
60 char *terminal = getenv("TERMINAL"); 64 char *terminal = getenv("TERMINAL");
61 if (button->terminal && terminal && strlen(terminal)) { 65 if (button->terminal && terminal && *terminal) {
62 sway_log(SWAY_DEBUG, "Found $TERMINAL: %s", terminal); 66 sway_log(SWAY_DEBUG, "Found $TERMINAL: %s", terminal);
63 if (!terminal_execute(terminal, button->action)) { 67 if (!terminal_execute(terminal, button->action)) {
64 swaynag_destroy(swaynag); 68 swaynag_destroy(swaynag);
@@ -69,8 +73,8 @@ static void swaynag_button_execute(struct swaynag *swaynag,
69 sway_log(SWAY_DEBUG, 73 sway_log(SWAY_DEBUG,
70 "$TERMINAL not found. Running directly"); 74 "$TERMINAL not found. Running directly");
71 } 75 }
72 execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); 76 execlp("sh", "sh", "-c", button->action, NULL);
73 sway_log_errno(SWAY_DEBUG, "execl failed"); 77 sway_log_errno(SWAY_DEBUG, "execlp failed");
74 _exit(EXIT_FAILURE); 78 _exit(EXIT_FAILURE);
75 } 79 }
76 } 80 }
@@ -103,7 +107,7 @@ static void layer_surface_closed(void *data,
103 swaynag_destroy(swaynag); 107 swaynag_destroy(swaynag);
104} 108}
105 109
106static struct zwlr_layer_surface_v1_listener layer_surface_listener = { 110static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
107 .configure = layer_surface_configure, 111 .configure = layer_surface_configure,
108 .closed = layer_surface_closed, 112 .closed = layer_surface_closed,
109}; 113};
@@ -124,7 +128,7 @@ static void surface_enter(void *data, struct wl_surface *surface,
124 }; 128 };
125} 129}
126 130
127static struct wl_surface_listener surface_listener = { 131static const struct wl_surface_listener surface_listener = {
128 .enter = surface_enter, 132 .enter = surface_enter,
129 .leave = nop, 133 .leave = nop,
130}; 134};
@@ -138,7 +142,7 @@ static void update_cursor(struct swaynag_seat *seat) {
138 const char *cursor_theme = getenv("XCURSOR_THEME"); 142 const char *cursor_theme = getenv("XCURSOR_THEME");
139 unsigned cursor_size = 24; 143 unsigned cursor_size = 24;
140 const char *env_cursor_size = getenv("XCURSOR_SIZE"); 144 const char *env_cursor_size = getenv("XCURSOR_SIZE");
141 if (env_cursor_size && strlen(env_cursor_size) > 0) { 145 if (env_cursor_size && *env_cursor_size) {
142 errno = 0; 146 errno = 0;
143 char *end; 147 char *end;
144 unsigned size = strtoul(env_cursor_size, &end, 10); 148 unsigned size = strtoul(env_cursor_size, &end, 10);
@@ -148,8 +152,15 @@ static void update_cursor(struct swaynag_seat *seat) {
148 } 152 }
149 pointer->cursor_theme = wl_cursor_theme_load( 153 pointer->cursor_theme = wl_cursor_theme_load(
150 cursor_theme, cursor_size * swaynag->scale, swaynag->shm); 154 cursor_theme, cursor_size * swaynag->scale, swaynag->shm);
151 struct wl_cursor *cursor = 155 if (!pointer->cursor_theme) {
152 wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); 156 sway_log(SWAY_ERROR, "Failed to load cursor theme");
157 return;
158 }
159 struct wl_cursor *cursor = wl_cursor_theme_get_cursor(pointer->cursor_theme, "default");
160 if (!cursor) {
161 sway_log(SWAY_ERROR, "Failed to get default cursor from theme");
162 return;
163 }
153 pointer->cursor_image = cursor->images[0]; 164 pointer->cursor_image = cursor->images[0];
154 wl_surface_set_buffer_scale(pointer->cursor_surface, 165 wl_surface_set_buffer_scale(pointer->cursor_surface,
155 swaynag->scale); 166 swaynag->scale);
@@ -177,9 +188,22 @@ static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
177 uint32_t serial, struct wl_surface *surface, 188 uint32_t serial, struct wl_surface *surface,
178 wl_fixed_t surface_x, wl_fixed_t surface_y) { 189 wl_fixed_t surface_x, wl_fixed_t surface_y) {
179 struct swaynag_seat *seat = data; 190 struct swaynag_seat *seat = data;
191
180 struct swaynag_pointer *pointer = &seat->pointer; 192 struct swaynag_pointer *pointer = &seat->pointer;
181 pointer->serial = serial; 193 pointer->x = wl_fixed_to_int(surface_x);
182 update_cursor(seat); 194 pointer->y = wl_fixed_to_int(surface_y);
195
196 if (seat->swaynag->cursor_shape_manager) {
197 struct wp_cursor_shape_device_v1 *device =
198 wp_cursor_shape_manager_v1_get_pointer(
199 seat->swaynag->cursor_shape_manager, wl_pointer);
200 wp_cursor_shape_device_v1_set_shape(device, serial,
201 WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
202 wp_cursor_shape_device_v1_destroy(device);
203 } else {
204 pointer->serial = serial;
205 update_cursor(seat);
206 }
183} 207}
184 208
185static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, 209static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
@@ -198,8 +222,8 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
198 return; 222 return;
199 } 223 }
200 224
201 double x = seat->pointer.x * swaynag->scale; 225 double x = seat->pointer.x;
202 double y = seat->pointer.y * swaynag->scale; 226 double y = seat->pointer.y;
203 for (int i = 0; i < swaynag->buttons->length; i++) { 227 for (int i = 0; i < swaynag->buttons->length; i++) {
204 struct swaynag_button *nagbutton = swaynag->buttons->items[i]; 228 struct swaynag_button *nagbutton = swaynag->buttons->items[i];
205 if (x >= nagbutton->x 229 if (x >= nagbutton->x
@@ -263,7 +287,7 @@ static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
263 render_frame(swaynag); 287 render_frame(swaynag);
264} 288}
265 289
266static struct wl_pointer_listener pointer_listener = { 290static const struct wl_pointer_listener pointer_listener = {
267 .enter = wl_pointer_enter, 291 .enter = wl_pointer_enter,
268 .leave = nop, 292 .leave = nop,
269 .motion = wl_pointer_motion, 293 .motion = wl_pointer_motion,
@@ -289,7 +313,7 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
289 } 313 }
290} 314}
291 315
292const struct wl_seat_listener seat_listener = { 316static const struct wl_seat_listener seat_listener = {
293 .capabilities = seat_handle_capabilities, 317 .capabilities = seat_handle_capabilities,
294 .name = nop, 318 .name = nop,
295}; 319};
@@ -305,33 +329,25 @@ static void output_scale(void *data, struct wl_output *output,
305 } 329 }
306} 330}
307 331
308static struct wl_output_listener output_listener = { 332static void output_name(void *data, struct wl_output *output,
309 .geometry = nop, 333 const char *name) {
310 .mode = nop,
311 .done = nop,
312 .scale = output_scale,
313};
314
315static void xdg_output_handle_name(void *data,
316 struct zxdg_output_v1 *xdg_output, const char *name) {
317 struct swaynag_output *swaynag_output = data; 334 struct swaynag_output *swaynag_output = data;
318 char *outname = swaynag_output->swaynag->type->output; 335 swaynag_output->name = strdup(name);
319 sway_log(SWAY_DEBUG, "Checking against output %s for %s", name, outname); 336
320 if (!swaynag_output->swaynag->output && outname && name 337 const char *outname = swaynag_output->swaynag->type->output;
321 && strcmp(outname, name) == 0) { 338 if (!swaynag_output->swaynag->output && outname &&
339 strcmp(outname, name) == 0) {
322 sway_log(SWAY_DEBUG, "Using output %s", name); 340 sway_log(SWAY_DEBUG, "Using output %s", name);
323 swaynag_output->swaynag->output = swaynag_output; 341 swaynag_output->swaynag->output = swaynag_output;
324 } 342 }
325 swaynag_output->name = strdup(name);
326 zxdg_output_v1_destroy(xdg_output);
327 swaynag_output->swaynag->querying_outputs--;
328} 343}
329 344
330static struct zxdg_output_v1_listener xdg_output_listener = { 345static const struct wl_output_listener output_listener = {
331 .logical_position = nop, 346 .geometry = nop,
332 .logical_size = nop, 347 .mode = nop,
333 .done = nop, 348 .done = nop,
334 .name = xdg_output_handle_name, 349 .scale = output_scale,
350 .name = output_name,
335 .description = nop, 351 .description = nop,
336}; 352};
337 353
@@ -345,6 +361,7 @@ static void handle_global(void *data, struct wl_registry *registry,
345 struct swaynag_seat *seat = 361 struct swaynag_seat *seat =
346 calloc(1, sizeof(struct swaynag_seat)); 362 calloc(1, sizeof(struct swaynag_seat));
347 if (!seat) { 363 if (!seat) {
364 perror("calloc");
348 return; 365 return;
349 } 366 }
350 367
@@ -359,33 +376,28 @@ static void handle_global(void *data, struct wl_registry *registry,
359 } else if (strcmp(interface, wl_shm_interface.name) == 0) { 376 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
360 swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); 377 swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
361 } else if (strcmp(interface, wl_output_interface.name) == 0) { 378 } else if (strcmp(interface, wl_output_interface.name) == 0) {
362 if (!swaynag->output && swaynag->xdg_output_manager) { 379 if (!swaynag->output) {
363 swaynag->querying_outputs++;
364 struct swaynag_output *output = 380 struct swaynag_output *output =
365 calloc(1, sizeof(struct swaynag_output)); 381 calloc(1, sizeof(struct swaynag_output));
382 if (!output) {
383 perror("calloc");
384 return;
385 }
366 output->wl_output = wl_registry_bind(registry, name, 386 output->wl_output = wl_registry_bind(registry, name,
367 &wl_output_interface, 3); 387 &wl_output_interface, 4);
368 output->wl_name = name; 388 output->wl_name = name;
369 output->scale = 1; 389 output->scale = 1;
370 output->swaynag = swaynag; 390 output->swaynag = swaynag;
371 wl_list_insert(&swaynag->outputs, &output->link); 391 wl_list_insert(&swaynag->outputs, &output->link);
372 wl_output_add_listener(output->wl_output, 392 wl_output_add_listener(output->wl_output,
373 &output_listener, output); 393 &output_listener, output);
374
375 struct zxdg_output_v1 *xdg_output;
376 xdg_output = zxdg_output_manager_v1_get_xdg_output(
377 swaynag->xdg_output_manager, output->wl_output);
378 zxdg_output_v1_add_listener(xdg_output,
379 &xdg_output_listener, output);
380 } 394 }
381 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { 395 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
382 swaynag->layer_shell = wl_registry_bind( 396 swaynag->layer_shell = wl_registry_bind(
383 registry, name, &zwlr_layer_shell_v1_interface, 1); 397 registry, name, &zwlr_layer_shell_v1_interface, 1);
384 } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 398 } else if (strcmp(interface, wp_cursor_shape_manager_v1_interface.name) == 0) {
385 && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { 399 swaynag->cursor_shape_manager = wl_registry_bind(
386 swaynag->xdg_output_manager = wl_registry_bind(registry, name, 400 registry, name, &wp_cursor_shape_manager_v1_interface, 1);
387 &zxdg_output_manager_v1_interface,
388 ZXDG_OUTPUT_V1_NAME_SINCE_VERSION);
389 } 401 }
390} 402}
391 403
@@ -451,12 +463,11 @@ void swaynag_setup(struct swaynag *swaynag) {
451 463
452 assert(swaynag->compositor && swaynag->layer_shell && swaynag->shm); 464 assert(swaynag->compositor && swaynag->layer_shell && swaynag->shm);
453 465
454 while (swaynag->querying_outputs > 0) { 466 // Second roundtrip to get wl_output properties
455 if (wl_display_roundtrip(swaynag->display) < 0) { 467 if (wl_display_roundtrip(swaynag->display) < 0) {
456 sway_log(SWAY_ERROR, "Error during outputs init."); 468 sway_log(SWAY_ERROR, "Error during outputs init.");
457 swaynag_destroy(swaynag); 469 swaynag_destroy(swaynag);
458 exit(EXIT_FAILURE); 470 exit(EXIT_FAILURE);
459 }
460 } 471 }
461 472
462 if (!swaynag->output && swaynag->type->output) { 473 if (!swaynag->output && swaynag->type->output) {
@@ -465,7 +476,9 @@ void swaynag_setup(struct swaynag *swaynag) {
465 exit(EXIT_FAILURE); 476 exit(EXIT_FAILURE);
466 } 477 }
467 478
468 swaynag_setup_cursors(swaynag); 479 if (!swaynag->cursor_shape_manager) {
480 swaynag_setup_cursors(swaynag);
481 }
469 482
470 swaynag->surface = wl_compositor_create_surface(swaynag->compositor); 483 swaynag->surface = wl_compositor_create_surface(swaynag->compositor);
471 assert(swaynag->surface); 484 assert(swaynag->surface);
@@ -474,7 +487,8 @@ void swaynag_setup(struct swaynag *swaynag) {
474 swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface( 487 swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
475 swaynag->layer_shell, swaynag->surface, 488 swaynag->layer_shell, swaynag->surface,
476 swaynag->output ? swaynag->output->wl_output : NULL, 489 swaynag->output ? swaynag->output->wl_output : NULL,
477 ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); 490 swaynag->type->layer,
491 "swaynag");
478 assert(swaynag->layer_surface); 492 assert(swaynag->layer_surface);
479 zwlr_layer_surface_v1_add_listener(swaynag->layer_surface, 493 zwlr_layer_surface_v1_add_listener(swaynag->layer_surface,
480 &layer_surface_listener, swaynag); 494 &layer_surface_listener, swaynag);
@@ -491,10 +505,6 @@ void swaynag_run(struct swaynag *swaynag) {
491 && wl_display_dispatch(swaynag->display) != -1) { 505 && wl_display_dispatch(swaynag->display) != -1) {
492 // This is intentionally left blank 506 // This is intentionally left blank
493 } 507 }
494
495 if (swaynag->display) {
496 wl_display_disconnect(swaynag->display);
497 }
498} 508}
499 509
500void swaynag_destroy(struct swaynag *swaynag) { 510void swaynag_destroy(struct swaynag *swaynag) {
@@ -509,6 +519,7 @@ void swaynag_destroy(struct swaynag *swaynag) {
509 } 519 }
510 list_free(swaynag->buttons); 520 list_free(swaynag->buttons);
511 free(swaynag->details.message); 521 free(swaynag->details.message);
522 free(swaynag->details.details_text);
512 free(swaynag->details.button_up.text); 523 free(swaynag->details.button_up.text);
513 free(swaynag->details.button_down.text); 524 free(swaynag->details.button_down.text);
514 525
@@ -529,13 +540,8 @@ void swaynag_destroy(struct swaynag *swaynag) {
529 swaynag_seat_destroy(seat); 540 swaynag_seat_destroy(seat);
530 } 541 }
531 542
532 if (&swaynag->buffers[0]) { 543 destroy_buffer(&swaynag->buffers[0]);
533 destroy_buffer(&swaynag->buffers[0]); 544 destroy_buffer(&swaynag->buffers[1]);
534 }
535
536 if (&swaynag->buffers[1]) {
537 destroy_buffer(&swaynag->buffers[1]);
538 }
539 545
540 if (swaynag->outputs.prev || swaynag->outputs.next) { 546 if (swaynag->outputs.prev || swaynag->outputs.next) {
541 struct swaynag_output *output, *temp; 547 struct swaynag_output *output, *temp;
@@ -554,4 +560,8 @@ void swaynag_destroy(struct swaynag *swaynag) {
554 if (swaynag->shm) { 560 if (swaynag->shm) {
555 wl_shm_destroy(swaynag->shm); 561 wl_shm_destroy(swaynag->shm);
556 } 562 }
563
564 if (swaynag->display) {
565 wl_display_disconnect(swaynag->display);
566 }
557} 567}