From 4f5cf330c8643a154215cbae5758b86022d6edb3 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 30 Jul 2018 13:52:02 -0400 Subject: [PATCH] swaynag: address some more of sircmpwn's comments Fixes segfauls for any case where swaynag->outputs was not inititalized including -h/--help, -v/--version, and invalid arguments. Sets sane defaults for colors not given. Any color not given will fallback to the default color values for type error. Adds support for a hidpi cursor --- include/swaynag/swaynag.h | 3 ++- swaynag/swaynag.c | 57 +++++++++++++++++++++++---------------- swaynag/types.c | 10 +++---- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/include/swaynag/swaynag.h b/include/swaynag/swaynag.h index 09b95326..1bf8b640 100644 --- a/include/swaynag/swaynag.h +++ b/include/swaynag/swaynag.h @@ -19,6 +19,7 @@ enum swaynag_action_type { struct swaynag_pointer { struct wl_pointer *pointer; + uint32_t serial; struct wl_cursor_theme *cursor_theme; struct wl_cursor_image *cursor_image; struct wl_surface *cursor_surface; @@ -72,7 +73,7 @@ struct swaynag { struct wl_shm *shm; struct swaynag_pointer pointer; struct zxdg_output_manager_v1 *xdg_output_manager; - struct wl_list outputs; // struct swaynag_output + struct wl_list outputs; // swaynag_output::link struct swaynag_output *output; struct zwlr_layer_shell_v1 *layer_shell; struct zwlr_layer_surface_v1 *layer_surface; diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index 22511529..3966277d 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -106,19 +106,31 @@ static struct wl_surface_listener surface_listener = { .leave = nop, }; +static void update_cursor(struct swaynag *swaynag) { + struct swaynag_pointer *pointer = &swaynag->pointer; + pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * swaynag->scale, + swaynag->shm); + struct wl_cursor *cursor = + wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); + pointer->cursor_image = cursor->images[0]; + wl_surface_set_buffer_scale(pointer->cursor_surface, + swaynag->scale); + wl_surface_attach(pointer->cursor_surface, + wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); + wl_pointer_set_cursor(pointer->pointer, pointer->serial, + pointer->cursor_surface, + pointer->cursor_image->hotspot_x / swaynag->scale, + pointer->cursor_image->hotspot_y / swaynag->scale); + wl_surface_commit(pointer->cursor_surface); +} + 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 swaynag *swaynag = data; struct swaynag_pointer *pointer = &swaynag->pointer; - wl_surface_set_buffer_scale(pointer->cursor_surface, - swaynag->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 / swaynag->scale, - pointer->cursor_image->hotspot_y / swaynag->scale); - wl_surface_commit(pointer->cursor_surface); + pointer->serial = serial; + update_cursor(swaynag); } static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, @@ -233,6 +245,7 @@ static void output_scale(void *data, struct wl_output *output, swaynag_output->scale = factor; if (swaynag_output->swaynag->output == swaynag_output) { swaynag_output->swaynag->scale = swaynag_output->scale; + update_cursor(swaynag_output->swaynag); render_frame(swaynag_output->swaynag); } } @@ -345,14 +358,6 @@ void swaynag_setup(struct swaynag *swaynag) { } struct swaynag_pointer *pointer = &swaynag->pointer; - int scale = swaynag->output ? swaynag->scale : 1; - pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * scale, - swaynag->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(swaynag->compositor); assert(pointer->cursor_surface); @@ -410,6 +415,10 @@ void swaynag_destroy(struct swaynag *swaynag) { wl_surface_destroy(swaynag->surface); } + if (swaynag->pointer.cursor_theme) { + wl_cursor_theme_destroy(swaynag->pointer.cursor_theme); + } + if (&swaynag->buffers[0]) { destroy_buffer(&swaynag->buffers[0]); } @@ -418,13 +427,15 @@ void swaynag_destroy(struct swaynag *swaynag) { destroy_buffer(&swaynag->buffers[1]); } - struct swaynag_output *output, *temp; - wl_list_for_each_safe(output, temp, &swaynag->outputs, link) { - wl_output_destroy(output->wl_output); - free(output->name); - wl_list_remove(&output->link); - free(output); - }; + if (swaynag->outputs.prev || swaynag->outputs.next) { + struct swaynag_output *output, *temp; + wl_list_for_each_safe(output, temp, &swaynag->outputs, link) { + wl_output_destroy(output->wl_output); + free(output->name); + wl_list_remove(&output->link); + free(output); + }; + } if (swaynag->compositor) { wl_compositor_destroy(swaynag->compositor); diff --git a/swaynag/types.c b/swaynag/types.c index f429baf0..66b802cd 100644 --- a/swaynag/types.c +++ b/swaynag/types.c @@ -19,6 +19,11 @@ void swaynag_types_add_default(list_t *types) { type_defaults->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + type_defaults->button_background = 0x680A0AFF; + type_defaults->background = 0x900000FF; + type_defaults->text = 0xFFFFFFFF; + type_defaults->border = 0xD92424FF; + type_defaults->border_bottom = 0x470909FF; type_defaults->bar_border_thickness = 2; type_defaults->message_padding = 8; type_defaults->details_border_thickness = 3; @@ -32,11 +37,6 @@ void swaynag_types_add_default(list_t *types) { struct swaynag_type *type_error; type_error = calloc(1, sizeof(struct swaynag_type)); type_error->name = strdup("error"); - type_error->button_background = 0x680A0AFF; - type_error->background = 0x900000FF; - type_error->text = 0xFFFFFFFF; - type_error->border = 0xD92424FF; - type_error->border_bottom = 0x470909FF; list_add(types, type_error); struct swaynag_type *type_warning;