From a6145914c60351d8e541192c7fe35556f8e02507 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 28 Jul 2018 23:15:12 -0400 Subject: swaynag: refactor {sway_,}nagbar to swaynag --- swaynag/swaynag.c | 413 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 413 insertions(+) create mode 100644 swaynag/swaynag.c (limited to 'swaynag/swaynag.c') diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c new file mode 100644 index 00000000..367e0854 --- /dev/null +++ b/swaynag/swaynag.c @@ -0,0 +1,413 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include "log.h" +#include "list.h" +#include "swaynag/render.h" +#include "swaynag/swaynag.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 swaynag_button_execute(struct swaynag *swaynag, + struct swaynag_button *button) { + wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); + if (button->type == SWAYNAG_ACTION_DISMISS) { + swaynag->run_display = false; + } else if (button->type == SWAYNAG_ACTION_EXPAND) { + swaynag->details.visible = !swaynag->details.visible; + render_frame(swaynag); + } 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)) { + swaynag_destroy(swaynag); + 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 swaynag *swaynag = data; + swaynag->width = width; + swaynag->height = height; + zwlr_layer_surface_v1_ack_configure(surface, serial); + render_frame(swaynag); +} + +static void layer_surface_closed(void *data, + struct zwlr_layer_surface_v1 *surface) { + struct swaynag *swaynag = data; + swaynag_destroy(swaynag); +} + +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 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); +} + +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 swaynag *swaynag = data; + swaynag->pointer.x = wl_fixed_to_int(surface_x); + swaynag->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 swaynag *swaynag = data; + + if (state != WL_POINTER_BUTTON_STATE_PRESSED) { + return; + } + + double x = swaynag->pointer.x * swaynag->scale; + double y = swaynag->pointer.y * swaynag->scale; + for (int i = 0; i < swaynag->buttons->length; i++) { + struct swaynag_button *nagbutton = swaynag->buttons->items[i]; + if (x >= nagbutton->x + && y >= nagbutton->y + && x < nagbutton->x + nagbutton->width + && y < nagbutton->y + nagbutton->height) { + swaynag_button_execute(swaynag, nagbutton); + return; + } + } + + if (swaynag->details.visible && + swaynag->details.total_lines != swaynag->details.visible_lines) { + struct swaynag_button button_up = swaynag->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 + && swaynag->details.offset > 0) { + swaynag->details.offset--; + render_frame(swaynag); + return; + } + + struct swaynag_button button_down = swaynag->details.button_down; + int bot = swaynag->details.total_lines - swaynag->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 + && swaynag->details.offset < bot) { + swaynag->details.offset++; + render_frame(swaynag); + return; + } + } +} + +static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) { + struct swaynag *swaynag = data; + if (!swaynag->details.visible + || swaynag->pointer.x < swaynag->details.x + || swaynag->pointer.y < swaynag->details.y + || swaynag->pointer.x >= swaynag->details.x + swaynag->details.width + || swaynag->pointer.y >= swaynag->details.y + swaynag->details.height + || swaynag->details.total_lines == swaynag->details.visible_lines) { + return; + } + + int direction = wl_fixed_to_int(value); + int bot = swaynag->details.total_lines - swaynag->details.visible_lines; + if (direction < 0 && swaynag->details.offset > 0) { + swaynag->details.offset--; + } else if (direction > 0 && swaynag->details.offset < bot) { + swaynag->details.offset++; + } + + render_frame(swaynag); +} + +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 swaynag *swaynag = data; + if ((caps & WL_SEAT_CAPABILITY_POINTER)) { + swaynag->pointer.pointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(swaynag->pointer.pointer, &pointer_listener, + swaynag); + } +} + +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 swaynag *swaynag = data; + swaynag->scale = factor; + render_frame(swaynag); +} + +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 swaynag *swaynag; +}; + +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->swaynag->output.name; + wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); + if (outname && !state->swaynag->output.wl_output) { + wlr_log(WLR_DEBUG, "Using output %s", name); + state->swaynag->output.wl_output = state->wl_output; + state->swaynag->output.wl_name = state->wl_name; + wl_output_add_listener(state->swaynag->output.wl_output, + &output_listener, state->swaynag); + wl_display_roundtrip(state->swaynag->display); + zxdg_output_v1_destroy(state->xdg_output); + } else { + zxdg_output_v1_destroy(state->xdg_output); + wl_output_destroy(state->wl_output); + } + state->swaynag->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 swaynag *swaynag = data; + if (strcmp(interface, wl_compositor_interface.name) == 0) { + swaynag->compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 3); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + swaynag->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); + wl_seat_add_listener(swaynag->seat, &seat_listener, swaynag); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, wl_output_interface.name) == 0) { + if (!swaynag->output.wl_output && swaynag->xdg_output_manager) { + swaynag->querying_outputs++; + struct output_state *state = + calloc(1, sizeof(struct output_state)); + state->swaynag = swaynag; + 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( + swaynag->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) { + swaynag->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) { + swaynag->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 swaynag *swaynag = data; + if (swaynag->output.wl_name == name) { + swaynag->run_display = false; + } +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +void swaynag_setup(struct swaynag *swaynag) { + swaynag->display = wl_display_connect(NULL); + assert(swaynag->display); + + swaynag->scale = 1; + + struct wl_registry *registry = wl_display_get_registry(swaynag->display); + wl_registry_add_listener(registry, ®istry_listener, swaynag); + wl_display_roundtrip(swaynag->display); + assert(swaynag->compositor && swaynag->layer_shell && swaynag->shm); + + while (swaynag->querying_outputs > 0) { + wl_display_roundtrip(swaynag->display); + } + + if (!swaynag->output.wl_output && swaynag->output.name) { + wlr_log(WLR_ERROR, "Output '%s' not found", swaynag->output.name); + swaynag_destroy(swaynag); + exit(EXIT_FAILURE); + } + + struct swaynag_pointer *pointer = &swaynag->pointer; + int scale = swaynag->scale < 1 ? 1 : swaynag->scale; + 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); + + swaynag->surface = wl_compositor_create_surface(swaynag->compositor); + assert(swaynag->surface); + swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface( + swaynag->layer_shell, swaynag->surface, swaynag->output.wl_output, + ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); + assert(swaynag->layer_surface); + zwlr_layer_surface_v1_add_listener(swaynag->layer_surface, + &layer_surface_listener, swaynag); + zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface, swaynag->anchors); + + wl_registry_destroy(registry); +} + +void swaynag_run(struct swaynag *swaynag) { + swaynag->run_display = true; + render_frame(swaynag); + while (swaynag->run_display + && wl_display_dispatch(swaynag->display) != -1) { + // This is intentionally left blank + } +} + +void swaynag_destroy(struct swaynag *swaynag) { + swaynag->run_display = false; + + free(swaynag->message); + free(swaynag->font); + while (swaynag->buttons->length) { + struct swaynag_button *button = swaynag->buttons->items[0]; + list_del(swaynag->buttons, 0); + free(button->text); + free(button->action); + free(button); + } + list_free(swaynag->buttons); + free(swaynag->details.message); + free(swaynag->details.button_up.text); + free(swaynag->details.button_down.text); + + if (swaynag->type) { + swaynag_type_free(swaynag->type); + } + + if (swaynag->layer_surface) { + zwlr_layer_surface_v1_destroy(swaynag->layer_surface); + } + + if (swaynag->surface) { + wl_surface_destroy(swaynag->surface); + } + + if (swaynag->output.wl_output) { + wl_output_destroy(swaynag->output.wl_output); + } + + if (&swaynag->buffers[0]) { + destroy_buffer(&swaynag->buffers[0]); + } + + if (&swaynag->buffers[1]) { + destroy_buffer(&swaynag->buffers[1]); + } + + if (swaynag->compositor) { + wl_compositor_destroy(swaynag->compositor); + } + + if (swaynag->shm) { + wl_shm_destroy(swaynag->shm); + } + + if (swaynag->display) { + wl_display_disconnect(swaynag->display); + } +} -- cgit v1.2.3 From 894d57f19259eaaa4d7777f067ce4cf877a480a5 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 28 Jul 2018 23:25:40 -0400 Subject: swaynag: fix output selection --- swaynag/swaynag.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'swaynag/swaynag.c') diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index 367e0854..e5d2e216 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -230,7 +230,8 @@ static void xdg_output_handle_name(void *data, struct output_state *state = data; char *outname = state->swaynag->output.name; wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); - if (outname && !state->swaynag->output.wl_output) { + if (!state->swaynag->output.wl_output && outname && name + && strcmp(outname, name) == 0) { wlr_log(WLR_DEBUG, "Using output %s", name); state->swaynag->output.wl_output = state->wl_output; state->swaynag->output.wl_name = state->wl_name; -- cgit v1.2.3 From e01acb6097b583fcf2f6d0e0afe1bd878dd9b683 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sun, 29 Jul 2018 22:42:03 -0400 Subject: swaynag: allow more config options --- include/swaynag/config.h | 2 +- include/swaynag/swaynag.h | 11 ---- include/swaynag/types.h | 18 +++++- swaynag/config.c | 149 +++++++++++++++++++++++++++++++++++++++------ swaynag/main.c | 33 ++++++---- swaynag/render.c | 51 ++++++++-------- swaynag/swaynag.1.scd | 46 ++++++++++++-- swaynag/swaynag.5.scd | 52 ++++++++++++++-- swaynag/swaynag.c | 4 +- swaynag/types.c | 150 ++++++++++++++++++++++++++++------------------ 10 files changed, 379 insertions(+), 137 deletions(-) (limited to 'swaynag/swaynag.c') diff --git a/include/swaynag/config.h b/include/swaynag/config.h index 3fd5b3ce..0d8889de 100644 --- a/include/swaynag/config.h +++ b/include/swaynag/config.h @@ -4,7 +4,7 @@ #include "list.h" int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, - list_t *types, char **config, bool *debug); + list_t *types, struct swaynag_type *type, char **config, bool *debug); char *swaynag_get_config_path(void); diff --git a/include/swaynag/swaynag.h b/include/swaynag/swaynag.h index 6a56f14f..dd6fe0cd 100644 --- a/include/swaynag/swaynag.h +++ b/include/swaynag/swaynag.h @@ -7,15 +7,6 @@ #include "swaynag/types.h" #include "xdg-output-unstable-v1-client-protocol.h" -#define SWAYNAG_BAR_BORDER_THICKNESS 2 -#define SWAYNAG_MESSAGE_PADDING 8 -#define SWAYNAG_DETAILS_BORDER_THICKNESS 3 -#define SWAYNAG_BUTTON_BORDER_THICKNESS 3 -#define SWAYNAG_BUTTON_GAP 20 -#define SWAYNAG_BUTTON_GAP_CLOSE 15 -#define SWAYNAG_BUTTON_MARGIN_RIGHT 2 -#define SWAYNAG_BUTTON_PADDING 3 - #define SWAYNAG_MAX_HEIGHT 500 enum swaynag_action_type { @@ -88,9 +79,7 @@ struct swaynag { struct pool_buffer *current_buffer; struct swaynag_type *type; - uint32_t anchors; char *message; - char *font; list_t *buttons; struct swaynag_details details; }; diff --git a/include/swaynag/types.h b/include/swaynag/types.h index af83bd83..2183ce22 100644 --- a/include/swaynag/types.h +++ b/include/swaynag/types.h @@ -3,11 +3,25 @@ struct swaynag_type { char *name; + + char *font; + char *output; + uint32_t anchors; + uint32_t button_background; uint32_t background; uint32_t text; uint32_t border; uint32_t border_bottom; + + uint32_t bar_border_thickness; + uint32_t message_padding; + uint32_t details_border_thickness; + uint32_t button_border_thickness; + uint32_t button_gap; + uint32_t button_gap_close; + uint32_t button_margin_right; + uint32_t button_padding; }; void swaynag_types_add_default(list_t *types); @@ -16,10 +30,10 @@ struct swaynag_type *swaynag_type_get(list_t *types, char *name); struct swaynag_type *swaynag_type_clone(struct swaynag_type *type); +void swaynag_type_merge(struct swaynag_type *dest, struct swaynag_type *src); + void swaynag_type_free(struct swaynag_type *type); void swaynag_types_free(list_t *types); -int swaynag_parse_type(int argc, char **argv, struct swaynag_type *type); - #endif diff --git a/swaynag/config.c b/swaynag/config.c index 289fc82a..80c5ad88 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -8,6 +8,7 @@ #include "readline.h" #include "swaynag/swaynag.h" #include "swaynag/types.h" +#include "util.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" static char *read_from_stdin() { @@ -37,7 +38,23 @@ static char *read_from_stdin() { } int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, - list_t *types, char **config, bool *debug) { + list_t *types, struct swaynag_type *type, char **config, bool *debug) { + enum type_options { + TO_COLOR_BACKGROUND = 256, + TO_COLOR_BORDER, + TO_COLOR_BORDER_BOTTOM, + TO_COLOR_BUTTON, + TO_COLOR_TEXT, + TO_THICK_BAR_BORDER, + TO_PADDING_MESSAGE, + TO_THICK_DET_BORDER, + TO_THICK_BTN_BORDER, + TO_GAP_BTN, + TO_GAP_BTN_DISMISS, + TO_MARGIN_BTN_RIGHT, + TO_PADDING_BTN, + }; + static struct option opts[] = { {"button", required_argument, NULL, 'b'}, {"config", required_argument, NULL, 'c'}, @@ -52,6 +69,21 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, {"dismiss-button", required_argument, NULL, 's'}, {"type", required_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, + + {"background", required_argument, NULL, TO_COLOR_BACKGROUND}, + {"border", required_argument, NULL, TO_COLOR_BORDER}, + {"border-bottom", required_argument, NULL, TO_COLOR_BORDER_BOTTOM}, + {"button-background", required_argument, NULL, TO_COLOR_BUTTON}, + {"text", required_argument, NULL, TO_COLOR_TEXT}, + {"border-bottom-size", required_argument, NULL, TO_THICK_BAR_BORDER}, + {"message-padding", required_argument, NULL, TO_PADDING_MESSAGE}, + {"details-border-size", required_argument, NULL, TO_THICK_DET_BORDER}, + {"button-border-size", required_argument, NULL, TO_THICK_BTN_BORDER}, + {"button-gap", required_argument, NULL, TO_GAP_BTN}, + {"button-dismiss-gap", required_argument, NULL, TO_GAP_BTN_DISMISS}, + {"button-margin-right", required_argument, NULL, TO_MARGIN_BTN_RIGHT}, + {"button-padding", required_argument, NULL, TO_PADDING_BTN}, + {0, 0, 0, 0} }; @@ -71,7 +103,22 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, " -o, --output Set the output to use.\n" " -s, --dismiss-button Set the dismiss button text.\n" " -t, --type Set the message type.\n" - " -v, --version Show the version number and quit.\n"; + " -v, --version Show the version number and quit.\n" + "\n" + "The following appearance options can also be given:\n" + " --background RRGGBB[AA] Background color.\n" + " --border RRGGBB[AA] Border color.\n" + " --border-bottom RRGGBB[AA] Bottom border color.\n" + " --button-background RRGGBB[AA] Button background color.\n" + " --text RRGGBB[AA] Text color.\n" + " --border-bottom-size size Thickness of the bar border.\n" + " --message-padding padding Padding for the message.\n" + " --details-border-size size Thickness for the details border.\n" + " --button-border-size size Thickness for the button border.\n" + " --button-gap gap Size of the gap between buttons\n" + " --button-dismiss-gap gap Size of the gap for dismiss button.\n" + " --button-margin-right margin Margin from dismiss button to edge.\n" + " --button-padding padding Padding for the button text.\n"; optind = 1; while (1) { @@ -106,13 +153,13 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } break; case 'e': // Edge - if (swaynag) { + if (type) { if (strcmp(optarg, "top") == 0) { - swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else if (strcmp(optarg, "bottom") == 0) { - swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + type->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else { @@ -122,9 +169,9 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } break; case 'f': // Font - if (swaynag) { - free(swaynag->font); - swaynag->font = strdup(optarg); + if (type) { + free(type->font); + type->font = strdup(optarg); } break; case 'l': // Detailed Message @@ -148,9 +195,9 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, } break; case 'o': // Output - if (swaynag) { - free(swaynag->output.name); - swaynag->output.name = strdup(optarg); + if (type) { + free(type->output); + type->output = strdup(optarg); } break; case 's': // Dismiss Button Text @@ -173,6 +220,71 @@ int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, case 'v': // Version fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); return -1; + case TO_COLOR_BACKGROUND: // Background color + if (type) { + type->background = parse_color(optarg); + } + break; + case TO_COLOR_BORDER: // Border color + if (type) { + type->border = parse_color(optarg); + } + break; + case TO_COLOR_BORDER_BOTTOM: // Bottom border color + if (type) { + type->border_bottom = parse_color(optarg); + } + break; + case TO_COLOR_BUTTON: // Button background color + if (type) { + type->button_background = parse_color(optarg); + } + break; + case TO_COLOR_TEXT: // Text color + if (type) { + type->text = parse_color(optarg); + } + break; + case TO_THICK_BAR_BORDER: // Bottom border thickness + if (type) { + type->bar_border_thickness = strtol(optarg, NULL, 0); + } + break; + case TO_PADDING_MESSAGE: // Message padding + if (type) { + type->message_padding = strtol(optarg, NULL, 0); + } + break; + case TO_THICK_DET_BORDER: // Details border thickness + if (type) { + type->details_border_thickness = strtol(optarg, NULL, 0); + } + break; + case TO_THICK_BTN_BORDER: // Button border thickness + if (type) { + type->button_border_thickness = strtol(optarg, NULL, 0); + } + break; + case TO_GAP_BTN: // Gap between buttons + if (type) { + type->button_gap = strtol(optarg, NULL, 0); + } + break; + case TO_GAP_BTN_DISMISS: // Gap between dismiss button + if (type) { + type->button_gap_close = strtol(optarg, NULL, 0); + } + break; + case TO_MARGIN_BTN_RIGHT: // Margin on the right side of button area + if (type) { + type->button_margin_right = strtol(optarg, NULL, 0); + } + break; + case TO_PADDING_BTN: // Padding for the button text + if (type) { + type->button_padding = strtol(optarg, NULL, 0); + } + break; default: // Help or unknown flag fprintf(c == 'h' ? stdout : stderr, "%s", usage); return -1; @@ -229,7 +341,12 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { fprintf(stderr, "Failed to read config. Running without it.\n"); return 0; } - struct swaynag_type *type = NULL; + + struct swaynag_type *type; + type = calloc(1, sizeof(struct swaynag_type)); + type->name = strdup(""); + list_add(types, type); + char *line; int line_number = 0; while (!feof(config)) { @@ -271,12 +388,8 @@ int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { sprintf(flag, "--%s", line); char *argv[] = {"swaynag", flag}; int result; - if (type) { - result = swaynag_parse_type(2, argv, type); - } else { - result = swaynag_parse_options(2, argv, swaynag, types, - NULL, NULL); - } + result = swaynag_parse_options(2, argv, swaynag, types, type, + NULL, NULL); if (result != 0) { free(line); fclose(config); diff --git a/swaynag/main.c b/swaynag/main.c index 0493c1f0..20d03c31 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -5,7 +5,6 @@ #include "swaynag/config.h" #include "swaynag/swaynag.h" #include "swaynag/types.h" -#include "wlr-layer-shell-unstable-v1-client-protocol.h" static struct swaynag swaynag; @@ -26,10 +25,6 @@ int main(int argc, char **argv) { swaynag_types_add_default(types); memset(&swaynag, 0, sizeof(swaynag)); - swaynag.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - swaynag.font = strdup("pango:monospace 10"); swaynag.buttons = create_list(); struct swaynag_button *button_close = @@ -44,7 +39,7 @@ int main(int argc, char **argv) { char *config_path = NULL; bool debug = false; - int launch_status = swaynag_parse_options(argc, argv, NULL, NULL, + int launch_status = swaynag_parse_options(argc, argv, NULL, NULL, NULL, &config_path, &debug); if (launch_status != 0) { exit_code = launch_status; @@ -66,8 +61,13 @@ int main(int argc, char **argv) { } if (argc > 1) { + struct swaynag_type *type_args; + type_args = calloc(1, sizeof(struct swaynag_type)); + type_args->name = strdup(""); + list_add(types, type_args); + int result = swaynag_parse_options(argc, argv, &swaynag, types, - NULL, NULL); + type_args, NULL, NULL); if (result != 0) { exit_code = result; goto cleanup; @@ -84,7 +84,20 @@ int main(int argc, char **argv) { swaynag.type = swaynag_type_get(types, "error"); } - swaynag.type = swaynag_type_clone(swaynag.type); + // Construct a new type using the config defaults as base, then merging + // config type defaults on top, then merging arguments on top of that, and + // finally merging defaults on top. + struct swaynag_type *type = calloc(1, sizeof(struct swaynag_type)); + type->name = strdup(swaynag.type->name); + swaynag_type_merge(type, swaynag_type_get(types, "")); + swaynag_type_merge(type, swaynag.type); + swaynag_type_merge(type, swaynag_type_get(types, "")); + swaynag_type_merge(type, swaynag_type_get(types, "")); + swaynag.type = type; + if (swaynag.type->output) { + swaynag.output.name = strdup(swaynag.type->output); + } + swaynag_types_free(types); if (swaynag.details.message) { @@ -94,10 +107,10 @@ int main(int argc, char **argv) { } wlr_log(WLR_DEBUG, "Output: %s", swaynag.output.name); - wlr_log(WLR_DEBUG, "Anchors: %d", swaynag.anchors); + wlr_log(WLR_DEBUG, "Anchors: %d", swaynag.type->anchors); wlr_log(WLR_DEBUG, "Type: %s", swaynag.type->name); wlr_log(WLR_DEBUG, "Message: %s", swaynag.message); - wlr_log(WLR_DEBUG, "Font: %s", swaynag.font); + wlr_log(WLR_DEBUG, "Font: %s", swaynag.type->font); wlr_log(WLR_DEBUG, "Buttons"); for (int i = 0; i < swaynag.buttons->length; i++) { struct swaynag_button *button = swaynag.buttons->items[i]; diff --git a/swaynag/render.c b/swaynag/render.c index 67e26eaf..bc3e520e 100644 --- a/swaynag/render.c +++ b/swaynag/render.c @@ -9,13 +9,13 @@ static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { uint32_t height = swaynag->height * swaynag->scale; - height -= SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; + height -= swaynag->type->bar_border_thickness * swaynag->scale; int text_width, text_height; - get_text_size(cairo, swaynag->font, &text_width, &text_height, + get_text_size(cairo, swaynag->type->font, &text_width, &text_height, swaynag->scale, true, "%s", swaynag->message); - int padding = SWAYNAG_MESSAGE_PADDING * swaynag->scale; + int padding = swaynag->type->message_padding * swaynag->scale; uint32_t ideal_height = text_height + padding * 2; uint32_t ideal_surface_height = ideal_height / swaynag->scale; @@ -25,7 +25,7 @@ static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { cairo_set_source_u32(cairo, swaynag->type->text); cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); - pango_printf(cairo, swaynag->font, swaynag->scale, false, "%s", + pango_printf(cairo, swaynag->type->font, swaynag->scale, false, "%s", swaynag->message); return ideal_height; @@ -34,11 +34,11 @@ static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { static void render_details_scroll_button(cairo_t *cairo, struct swaynag *swaynag, struct swaynag_button *button) { int text_width, text_height; - get_text_size(cairo, swaynag->font, &text_width, &text_height, + get_text_size(cairo, swaynag->type->font, &text_width, &text_height, swaynag->scale, true, "%s", button->text); - int border = SWAYNAG_BUTTON_BORDER_THICKNESS * swaynag->scale; - int padding = SWAYNAG_BUTTON_PADDING * swaynag->scale; + int border = swaynag->type->button_border_thickness * swaynag->scale; + int padding = swaynag->type->button_padding * swaynag->scale; cairo_set_source_u32(cairo, swaynag->type->border); cairo_rectangle(cairo, button->x, button->y, @@ -53,21 +53,21 @@ static void render_details_scroll_button(cairo_t *cairo, cairo_set_source_u32(cairo, swaynag->type->text); cairo_move_to(cairo, button->x + border + padding, button->y + border + (button->height - text_height) / 2); - pango_printf(cairo, swaynag->font, swaynag->scale, true, + pango_printf(cairo, swaynag->type->font, swaynag->scale, true, "%s", button->text); } static int get_detailed_scroll_button_width(cairo_t *cairo, struct swaynag *swaynag) { int up_width, down_width, temp_height; - get_text_size(cairo, swaynag->font, &up_width, &temp_height, + get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, swaynag->scale, true, "%s", swaynag->details.button_up.text); - get_text_size(cairo, swaynag->font, &down_width, &temp_height, + get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, swaynag->scale, true, "%s", swaynag->details.button_down.text); int text_width = up_width > down_width ? up_width : down_width; - int border = SWAYNAG_BUTTON_BORDER_THICKNESS * swaynag->scale; - int padding = SWAYNAG_BUTTON_PADDING * swaynag->scale; + int border = swaynag->type->button_border_thickness * swaynag->scale; + int padding = swaynag->type->button_padding * swaynag->scale; return text_width + border * 2 + padding * 2; } @@ -76,17 +76,17 @@ static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, uint32_t y) { uint32_t width = swaynag->width * swaynag->scale; uint32_t height = swaynag->height * swaynag->scale; - height -= SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; + height -= swaynag->type->bar_border_thickness * swaynag->scale; - int border = SWAYNAG_DETAILS_BORDER_THICKNESS * swaynag->scale; - int padding = SWAYNAG_MESSAGE_PADDING * swaynag->scale; + int border = swaynag->type->details_border_thickness * swaynag->scale; + int padding = swaynag->type->message_padding * swaynag->scale; int decor = padding + border; swaynag->details.x = decor; swaynag->details.y = y + decor; swaynag->details.width = width - decor * 2; - PangoLayout *layout = get_pango_layout(cairo, swaynag->font, + PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font, swaynag->details.message, swaynag->scale, false); pango_layout_set_width(layout, (swaynag->details.width - padding * 2) * PANGO_SCALE); @@ -173,15 +173,15 @@ static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, int button_index, int *x) { uint32_t height = swaynag->height * swaynag->scale; - height -= SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; + height -= swaynag->type->bar_border_thickness * swaynag->scale; struct swaynag_button *button = swaynag->buttons->items[button_index]; int text_width, text_height; - get_text_size(cairo, swaynag->font, &text_width, &text_height, + get_text_size(cairo, swaynag->type->font, &text_width, &text_height, swaynag->scale, true, "%s", button->text); - int border = SWAYNAG_BUTTON_BORDER_THICKNESS * swaynag->scale; - int padding = SWAYNAG_BUTTON_PADDING * swaynag->scale; + int border = swaynag->type->button_border_thickness * swaynag->scale; + int padding = swaynag->type->button_padding * swaynag->scale; uint32_t ideal_height = text_height + padding * 2 + border * 2; uint32_t ideal_surface_height = ideal_height / swaynag->scale; @@ -206,7 +206,7 @@ static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, cairo_set_source_u32(cairo, swaynag->type->text); cairo_move_to(cairo, button->x + padding, button->y + padding); - pango_printf(cairo, swaynag->font, swaynag->scale, true, + pango_printf(cairo, swaynag->type->font, swaynag->scale, true, "%s", button->text); *x = button->x - border; @@ -224,13 +224,14 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { uint32_t h = render_message(cairo, swaynag); max_height = h > max_height ? h : max_height; - int x = (swaynag->width - SWAYNAG_BUTTON_MARGIN_RIGHT) * swaynag->scale; + int x = swaynag->width - swaynag->type->button_margin_right; + x *= swaynag->scale; for (int i = 0; i < swaynag->buttons->length; i++) { h = render_button(cairo, swaynag, i, &x); max_height = h > max_height ? h : max_height; - x -= SWAYNAG_BUTTON_GAP * swaynag->scale; + x -= swaynag->type->button_gap * swaynag->scale; if (i == 0) { - x -= SWAYNAG_BUTTON_GAP_CLOSE * swaynag->scale; + x -= swaynag->type->button_gap_close * swaynag->scale; } } @@ -239,7 +240,7 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { max_height = h > max_height ? h : max_height; } - int border = SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; + int border = swaynag->type->bar_border_thickness * swaynag->scale; if (max_height > swaynag->height) { max_height += border; } diff --git a/swaynag/swaynag.1.scd b/swaynag/swaynag.1.scd index 7d250a45..12787c3c 100644 --- a/swaynag/swaynag.1.scd +++ b/swaynag/swaynag.1.scd @@ -45,14 +45,12 @@ _swaynag_ [options...] Set the message text. *-o, --output* - Set the output to use. This should be the name of a _xdg\_output_. If - _xdg\_output\_manager_ is not supported, then the first detected output - will be used + Set the output to use. This should be the name of a _xdg\_output_. *-s, --dismiss-button* Sets the text for the dismiss nagbar button. The default is _X_. -*-t, --type* +*-t, --type* Set the message type. Two types are created by default _error_ and _warning_. Custom types can be defined in the config file. See _--config_ and swaynag(5) for details. Both of the default types can be @@ -61,5 +59,45 @@ _swaynag_ [options...] *-v, --version* Show the version number and quit. +# APPEARANCE OPTIONS +*--background* + Set the color of the background. + +*--border* + Set the color of the border. + +*--border-bottom* + Set the color of the bottom border. + +*--button-background* + Set the color for the background for buttons. + +*--text* + Set the text color. + +*--border-bottom-size* + Set the thickness of the bottom border. + +*--message-padding* + Set the padding for the message. + +*--details-border-size* + Set the thickness for the details border. + +*--button-border-size* + Set the thickness for the button border. + +*--button-gap* + Set the size of the gap between buttons. + +*--button-dismiss-gap* + Set the size of the gap between the dismiss button and another button. + +*--button-margin-right* + Set the margin from the right of the dismiss button to edge. + +*--button-padding* + Set the padding for the button text. + # SEE swaynag(5) diff --git a/swaynag/swaynag.5.scd b/swaynag/swaynag.5.scd index a4e05e3a..e2348d8b 100644 --- a/swaynag/swaynag.5.scd +++ b/swaynag/swaynag.5.scd @@ -20,37 +20,77 @@ following format: ``` [name-of-type] -color=RRGGBB[AA] +option=value ``` All colors may be given in the form _RRGGBB_ or _RRGGBBAA_. The following colors can be set: -*background* +*background=* The background color for _swaynag_. -*border* +*border=* The color to use for borders of buttons. -*border-bottom* +*border-bottom=* The color of the border line at the bottom of _swaynag_. -*button-background* +*button-background=* The background color for the buttons. -*text* +*text=* The color of the text. +The following sizing options can also be set: + +*border-bottom-size=* + Set the thickness of the bottom border. + +*message-padding=* + Set the padding for the message. + +*details-border-size=* + Set the thickness for the details border. + +*button-border-size=* + Set the thickness for the button border. + +*button-gap=* + Set the size of the gap between buttons. + +*button-dismiss-gap=* + Set the size of the gap between the dismiss button and another button. + +*button-margin-right=* + Set the margin from the right of the dismiss button to edge. + +*button-padding=* + Set the padding for the button text. + +Additionally, the following options can be assigned a default per-type: + +*edge=top|bottom* + Set the edge to use. + +*font=* + Set the font to use. + +*output=* + Set the output to use. This should be the name of a _xdg\_output_. + # EXAMPLE ``` font=Monospace 12 +edge=bottom [green] +edge=top background=00AA00 border=006600 border-bottom=004400 text=FFFFFF button-background=00CC00 +message-padding=10 ``` # SEE diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index e5d2e216..e79cd879 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -345,7 +345,8 @@ void swaynag_setup(struct swaynag *swaynag) { assert(swaynag->layer_surface); zwlr_layer_surface_v1_add_listener(swaynag->layer_surface, &layer_surface_listener, swaynag); - zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface, swaynag->anchors); + zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface, + swaynag->type->anchors); wl_registry_destroy(registry); } @@ -363,7 +364,6 @@ void swaynag_destroy(struct swaynag *swaynag) { swaynag->run_display = false; free(swaynag->message); - free(swaynag->font); while (swaynag->buttons->length) { struct swaynag_button *button = swaynag->buttons->items[0]; list_del(swaynag->buttons, 0); diff --git a/swaynag/types.c b/swaynag/types.c index c92d0e89..f429baf0 100644 --- a/swaynag/types.c +++ b/swaynag/types.c @@ -9,8 +9,26 @@ #include "swaynag/config.h" #include "swaynag/types.h" #include "util.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" void swaynag_types_add_default(list_t *types) { + struct swaynag_type *type_defaults; + type_defaults = calloc(1, sizeof(struct swaynag_type)); + type_defaults->name = strdup(""); + type_defaults->font = strdup("pango:Monospace 10"); + type_defaults->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + type_defaults->bar_border_thickness = 2; + type_defaults->message_padding = 8; + type_defaults->details_border_thickness = 3; + type_defaults->button_border_thickness = 3; + type_defaults->button_gap = 20; + type_defaults->button_gap_close = 15; + type_defaults->button_margin_right = 2; + type_defaults->button_padding = 3; + list_add(types, type_defaults); + struct swaynag_type *type_error; type_error = calloc(1, sizeof(struct swaynag_type)); type_error->name = strdup("error"); @@ -42,20 +60,84 @@ struct swaynag_type *swaynag_type_get(list_t *types, char *name) { return NULL; } -struct swaynag_type *swaynag_type_clone(struct swaynag_type *type) { - struct swaynag_type *clone; - clone = calloc(1, sizeof(struct swaynag_type)); - clone->name = strdup(type->name); - clone->button_background = type->button_background; - clone->background = type->background; - clone->text = type->text; - clone->border = type->border; - clone->border_bottom = type->border_bottom; - return clone; +void swaynag_type_merge(struct swaynag_type *dest, struct swaynag_type *src) { + if (!dest || !src) { + return; + } + + if (!dest->font && src->font) { + dest->font = strdup(src->font); + } + + if (!dest->output && src->output) { + dest->output = strdup(src->output); + } + + if (dest->anchors == 0 && src->anchors > 0) { + dest->anchors = src->anchors; + } + + // Colors + if (dest->button_background == 0 && src->button_background > 0) { + dest->button_background = src->button_background; + } + + if (dest->background == 0 && src->background > 0) { + dest->background = src->background; + } + + if (dest->text == 0 && src->text > 0) { + dest->text = src->text; + } + + if (dest->border == 0 && src->border > 0) { + dest->border = src->border; + } + + if (dest->border_bottom == 0 && src->border_bottom > 0) { + dest->border_bottom = src->border_bottom; + } + + // Sizing + if (dest->bar_border_thickness == 0 && src->bar_border_thickness > 0) { + dest->bar_border_thickness = src->bar_border_thickness; + } + + if (dest->message_padding == 0 && src->message_padding > 0) { + dest->message_padding = src->message_padding; + } + + if (dest->details_border_thickness == 0 + && src->details_border_thickness > 0) { + dest->details_border_thickness = src->details_border_thickness; + } + + if (dest->button_border_thickness == 0 + && src->button_border_thickness > 0) { + dest->button_border_thickness = src->button_border_thickness; + } + + if (dest->button_gap == 0 && src->button_gap > 0) { + dest->button_gap = src->button_gap; + } + + if (dest->button_gap_close == 0 && src->button_gap_close > 0) { + dest->button_gap_close = src->button_gap_close; + } + + if (dest->button_margin_right == 0 && src->button_margin_right > 0) { + dest->button_margin_right = src->button_margin_right; + } + + if (dest->button_padding == 0 && src->button_padding > 0) { + dest->button_padding = src->button_padding; + } } void swaynag_type_free(struct swaynag_type *type) { free(type->name); + free(type->font); + free(type->output); free(type); } @@ -67,51 +149,3 @@ void swaynag_types_free(list_t *types) { } list_free(types); } - -int swaynag_parse_type(int argc, char **argv, struct swaynag_type *type) { - enum color_option { - COLOR_BACKGROUND, - COLOR_BORDER, - COLOR_BORDER_BOTTOM, - COLOR_BUTTON, - COLOR_TEXT, - }; - - static struct option opts[] = { - {"background", required_argument, NULL, COLOR_BACKGROUND}, - {"border", required_argument, NULL, COLOR_BORDER}, - {"border-bottom", required_argument, NULL, COLOR_BORDER_BOTTOM}, - {"button-background", required_argument, NULL, COLOR_BUTTON}, - {"text", required_argument, NULL, COLOR_TEXT}, - {0, 0, 0, 0} - }; - - optind = 1; - while (1) { - int c = getopt_long(argc, argv, "", opts, NULL); - if (c == -1) { - break; - } - switch (c) { - case COLOR_BACKGROUND: - type->background = parse_color(optarg); - break; - case COLOR_BORDER: - type->border = parse_color(optarg); - break; - case COLOR_BORDER_BOTTOM: - type->border_bottom = parse_color(optarg); - break; - case COLOR_BUTTON: - type->button_background = parse_color(optarg); - break; - case COLOR_TEXT: - type->text = parse_color(optarg); - break; - default: - break; - } - } - return 0; -} - -- cgit v1.2.3 From 0ef3988438c251c84251ef7a2723791856505065 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 30 Jul 2018 01:02:50 -0400 Subject: swaynag: fix hidpi --- include/swaynag/swaynag.h | 8 +++- swaynag/main.c | 5 +- swaynag/render.c | 24 ++++++---- swaynag/swaynag.c | 120 ++++++++++++++++++++++++++++------------------ 4 files changed, 95 insertions(+), 62 deletions(-) (limited to 'swaynag/swaynag.c') diff --git a/include/swaynag/swaynag.h b/include/swaynag/swaynag.h index dd6fe0cd..09b95326 100644 --- a/include/swaynag/swaynag.h +++ b/include/swaynag/swaynag.h @@ -9,6 +9,8 @@ #define SWAYNAG_MAX_HEIGHT 500 +struct swaynag; + enum swaynag_action_type { SWAYNAG_ACTION_DISMISS, SWAYNAG_ACTION_EXPAND, @@ -28,6 +30,9 @@ struct swaynag_output { char *name; struct wl_output *wl_output; uint32_t wl_name; + uint32_t scale; + struct swaynag *swaynag; + struct wl_list link; }; struct swaynag_button { @@ -67,7 +72,8 @@ struct swaynag { struct wl_shm *shm; struct swaynag_pointer pointer; struct zxdg_output_manager_v1 *xdg_output_manager; - struct swaynag_output output; + struct wl_list outputs; // struct swaynag_output + struct swaynag_output *output; struct zwlr_layer_shell_v1 *layer_shell; struct zwlr_layer_surface_v1 *layer_surface; struct wl_surface *surface; diff --git a/swaynag/main.c b/swaynag/main.c index 20d03c31..854368e5 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -94,9 +94,6 @@ int main(int argc, char **argv) { swaynag_type_merge(type, swaynag_type_get(types, "")); swaynag_type_merge(type, swaynag_type_get(types, "")); swaynag.type = type; - if (swaynag.type->output) { - swaynag.output.name = strdup(swaynag.type->output); - } swaynag_types_free(types); @@ -106,7 +103,7 @@ int main(int argc, char **argv) { free(swaynag.details.button_details.text); } - wlr_log(WLR_DEBUG, "Output: %s", swaynag.output.name); + wlr_log(WLR_DEBUG, "Output: %s", swaynag.type->output); wlr_log(WLR_DEBUG, "Anchors: %d", swaynag.type->anchors); wlr_log(WLR_DEBUG, "Type: %s", swaynag.type->name); wlr_log(WLR_DEBUG, "Message: %s", swaynag.message); diff --git a/swaynag/render.c b/swaynag/render.c index bc3e520e..766409e4 100644 --- a/swaynag/render.c +++ b/swaynag/render.c @@ -25,10 +25,10 @@ static uint32_t render_message(cairo_t *cairo, struct swaynag *swaynag) { cairo_set_source_u32(cairo, swaynag->type->text); cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); - pango_printf(cairo, swaynag->type->font, swaynag->scale, false, "%s", - swaynag->message); + pango_printf(cairo, swaynag->type->font, swaynag->scale, false, + "%s", swaynag->message); - return ideal_height; + return ideal_surface_height; } static void render_details_scroll_button(cairo_t *cairo, @@ -61,9 +61,11 @@ static int get_detailed_scroll_button_width(cairo_t *cairo, struct swaynag *swaynag) { int up_width, down_width, temp_height; get_text_size(cairo, swaynag->type->font, &up_width, &temp_height, - swaynag->scale, true, "%s", swaynag->details.button_up.text); + swaynag->scale, true, + "%s", swaynag->details.button_up.text); get_text_size(cairo, swaynag->type->font, &down_width, &temp_height, - swaynag->scale, true, "%s", swaynag->details.button_down.text); + swaynag->scale, true, + "%s", swaynag->details.button_down.text); int text_width = up_width > down_width ? up_width : down_width; int border = swaynag->type->button_border_thickness * swaynag->scale; @@ -83,7 +85,7 @@ static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, int decor = padding + border; swaynag->details.x = decor; - swaynag->details.y = y + decor; + swaynag->details.y = y * swaynag->scale + decor; swaynag->details.width = width - decor * 2; PangoLayout *layout = get_pango_layout(cairo, swaynag->type->font, @@ -167,7 +169,7 @@ static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, pango_cairo_show_layout(cairo, layout); g_object_unref(layout); - return ideal_height; + return ideal_height / swaynag->scale; } static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, @@ -211,7 +213,7 @@ static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, *x = button->x - border; - return ideal_height; + return ideal_surface_height; } static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { @@ -245,8 +247,10 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { max_height += border; } cairo_set_source_u32(cairo, swaynag->type->border_bottom); - cairo_rectangle(cairo, 0, swaynag->height * swaynag->scale - border, - swaynag->width * swaynag->scale, border); + cairo_rectangle(cairo, 0, + swaynag->height * swaynag->scale - border, + swaynag->width * swaynag->scale, + border); cairo_fill(cairo); return max_height; diff --git a/swaynag/swaynag.c b/swaynag/swaynag.c index e79cd879..22511529 100644 --- a/swaynag/swaynag.c +++ b/swaynag/swaynag.c @@ -85,12 +85,34 @@ static struct zwlr_layer_surface_v1_listener layer_surface_listener = { .closed = layer_surface_closed, }; +static void surface_enter(void *data, struct wl_surface *surface, + struct wl_output *output) { + struct swaynag *swaynag = data; + struct swaynag_output *swaynag_output; + wl_list_for_each(swaynag_output, &swaynag->outputs, link) { + if (swaynag_output->wl_output == output) { + wlr_log(WLR_DEBUG, "Surface enter on output %s", + swaynag_output->name); + swaynag->output = swaynag_output; + swaynag->scale = swaynag->output->scale; + render_frame(swaynag); + break; + } + }; +} + +static struct wl_surface_listener surface_listener = { + .enter = surface_enter, + .leave = nop, +}; + 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_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, @@ -141,7 +163,8 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, } struct swaynag_button button_down = swaynag->details.button_down; - int bot = swaynag->details.total_lines - swaynag->details.visible_lines; + int bot = swaynag->details.total_lines; + bot -= swaynag->details.visible_lines; if (x >= button_down.x && y >= button_down.y && x < button_down.x + button_down.width @@ -206,9 +229,12 @@ const struct wl_seat_listener seat_listener = { static void output_scale(void *data, struct wl_output *output, int32_t factor) { - struct swaynag *swaynag = data; - swaynag->scale = factor; - render_frame(swaynag); + struct swaynag_output *swaynag_output = data; + swaynag_output->scale = factor; + if (swaynag_output->swaynag->output == swaynag_output) { + swaynag_output->swaynag->scale = swaynag_output->scale; + render_frame(swaynag_output->swaynag); + } } static struct wl_output_listener output_listener = { @@ -218,33 +244,19 @@ static struct wl_output_listener output_listener = { .scale = output_scale, }; -struct output_state { - struct wl_output *wl_output; - uint32_t wl_name; - struct zxdg_output_v1 *xdg_output; - struct swaynag *swaynag; -}; - 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->swaynag->output.name; + struct swaynag_output *swaynag_output = data; + char *outname = swaynag_output->swaynag->type->output; wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); - if (!state->swaynag->output.wl_output && outname && name + if (!swaynag_output->swaynag->output && outname && name && strcmp(outname, name) == 0) { wlr_log(WLR_DEBUG, "Using output %s", name); - state->swaynag->output.wl_output = state->wl_output; - state->swaynag->output.wl_name = state->wl_name; - wl_output_add_listener(state->swaynag->output.wl_output, - &output_listener, state->swaynag); - wl_display_roundtrip(state->swaynag->display); - zxdg_output_v1_destroy(state->xdg_output); - } else { - zxdg_output_v1_destroy(state->xdg_output); - wl_output_destroy(state->wl_output); + swaynag_output->swaynag->output = swaynag_output; } - state->swaynag->querying_outputs--; - free(state); + swaynag_output->name = strdup(name); + zxdg_output_v1_destroy(xdg_output); + swaynag_output->swaynag->querying_outputs--; } static struct zxdg_output_v1_listener xdg_output_listener = { @@ -267,18 +279,24 @@ static void handle_global(void *data, struct wl_registry *registry, } else if (strcmp(interface, wl_shm_interface.name) == 0) { swaynag->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); } else if (strcmp(interface, wl_output_interface.name) == 0) { - if (!swaynag->output.wl_output && swaynag->xdg_output_manager) { + if (!swaynag->output && swaynag->xdg_output_manager) { swaynag->querying_outputs++; - struct output_state *state = - calloc(1, sizeof(struct output_state)); - state->swaynag = swaynag; - state->wl_output = wl_registry_bind(registry, name, + struct swaynag_output *output = + calloc(1, sizeof(struct swaynag_output)); + output->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( - swaynag->xdg_output_manager, state->wl_output); - zxdg_output_v1_add_listener(state->xdg_output, - &xdg_output_listener, state); + output->wl_name = name; + output->scale = 1; + output->swaynag = swaynag; + wl_list_insert(&swaynag->outputs, &output->link); + wl_output_add_listener(output->wl_output, + &output_listener, output); + + struct zxdg_output_v1 *xdg_output; + xdg_output = zxdg_output_manager_v1_get_xdg_output( + swaynag->xdg_output_manager, output->wl_output); + zxdg_output_v1_add_listener(xdg_output, + &xdg_output_listener, output); } } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { swaynag->layer_shell = wl_registry_bind( @@ -294,7 +312,7 @@ static void handle_global(void *data, struct wl_registry *registry, static void handle_global_remove(void *data, struct wl_registry *registry, uint32_t name) { struct swaynag *swaynag = data; - if (swaynag->output.wl_name == name) { + if (swaynag->output->wl_name == name) { swaynag->run_display = false; } } @@ -309,6 +327,7 @@ void swaynag_setup(struct swaynag *swaynag) { assert(swaynag->display); swaynag->scale = 1; + wl_list_init(&swaynag->outputs); struct wl_registry *registry = wl_display_get_registry(swaynag->display); wl_registry_add_listener(registry, ®istry_listener, swaynag); @@ -319,16 +338,16 @@ void swaynag_setup(struct swaynag *swaynag) { wl_display_roundtrip(swaynag->display); } - if (!swaynag->output.wl_output && swaynag->output.name) { - wlr_log(WLR_ERROR, "Output '%s' not found", swaynag->output.name); + if (!swaynag->output && swaynag->type->output) { + wlr_log(WLR_ERROR, "Output '%s' not found", swaynag->type->output); swaynag_destroy(swaynag); exit(EXIT_FAILURE); } struct swaynag_pointer *pointer = &swaynag->pointer; - int scale = swaynag->scale < 1 ? 1 : swaynag->scale; - pointer->cursor_theme = wl_cursor_theme_load( - NULL, 24 * scale, swaynag->shm); + 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"); @@ -339,8 +358,11 @@ void swaynag_setup(struct swaynag *swaynag) { swaynag->surface = wl_compositor_create_surface(swaynag->compositor); assert(swaynag->surface); + wl_surface_add_listener(swaynag->surface, &surface_listener, swaynag); + swaynag->layer_surface = zwlr_layer_shell_v1_get_layer_surface( - swaynag->layer_shell, swaynag->surface, swaynag->output.wl_output, + swaynag->layer_shell, swaynag->surface, + swaynag->output ? swaynag->output->wl_output : NULL, ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); assert(swaynag->layer_surface); zwlr_layer_surface_v1_add_listener(swaynag->layer_surface, @@ -388,10 +410,6 @@ void swaynag_destroy(struct swaynag *swaynag) { wl_surface_destroy(swaynag->surface); } - if (swaynag->output.wl_output) { - wl_output_destroy(swaynag->output.wl_output); - } - if (&swaynag->buffers[0]) { destroy_buffer(&swaynag->buffers[0]); } @@ -400,6 +418,14 @@ 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->compositor) { wl_compositor_destroy(swaynag->compositor); } -- cgit v1.2.3 From 4f5cf330c8643a154215cbae5758b86022d6edb3 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 30 Jul 2018 13:52:02 -0400 Subject: 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 | 51 ++++++++++++++++++++++++++++------------------- swaynag/types.c | 10 +++++----- 3 files changed, 38 insertions(+), 26 deletions(-) (limited to 'swaynag/swaynag.c') 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,21 +106,33 @@ static struct wl_surface_listener surface_listener = { .leave = nop, }; -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; +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(wl_pointer, serial, pointer->cursor_surface, + 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; + pointer->serial = serial; + update_cursor(swaynag); +} + 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 swaynag *swaynag = data; @@ -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; -- cgit v1.2.3