From abf33468c12df258b8135089d99e7bd4f10e4f28 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sun, 22 Jul 2018 23:43:45 -0400 Subject: Arrange output in arrange_layers and commit dirty --- sway/desktop/layer_shell.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index a7d96717..71a0163c 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -7,11 +7,13 @@ #include #include #include +#include "sway/desktop/transaction.h" #include "sway/input/input-manager.h" #include "sway/input/seat.h" #include "sway/layers.h" #include "sway/output.h" #include "sway/server.h" +#include "sway/tree/arrange.h" #include "sway/tree/layout.h" #include "log.h" @@ -212,6 +214,9 @@ void arrange_layers(struct sway_output *output) { wl_list_for_each(seat, &input_manager->seats, link) { seat_set_focus_layer(seat, topmost ? topmost->layer_surface : NULL); } + + arrange_windows(output->swayc); + transaction_commit_dirty(); } static void handle_output_destroy(struct wl_listener *listener, void *data) { -- cgit v1.2.3-54-g00ecf From 88bc4b528ef3e1af3598b513dd5c1572dd09ec23 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 25 Jul 2018 21:57:19 -0400 Subject: Implements swaynagbar --- include/swaynagbar/nagbar.h | 88 +++++++++++ include/swaynagbar/render.h | 6 + meson.build | 2 + swaynagbar/main.c | 184 ++++++++++++++++++++++ swaynagbar/meson.build | 20 +++ swaynagbar/nagbar.c | 364 ++++++++++++++++++++++++++++++++++++++++++++ swaynagbar/render.c | 152 ++++++++++++++++++ swaynagbar/swaynagbar.1.scd | 41 +++++ 8 files changed, 857 insertions(+) create mode 100644 include/swaynagbar/nagbar.h create mode 100644 include/swaynagbar/render.h create mode 100644 swaynagbar/main.c create mode 100644 swaynagbar/meson.build create mode 100644 swaynagbar/nagbar.c create mode 100644 swaynagbar/render.c create mode 100644 swaynagbar/swaynagbar.1.scd diff --git a/include/swaynagbar/nagbar.h b/include/swaynagbar/nagbar.h new file mode 100644 index 00000000..07a0d51e --- /dev/null +++ b/include/swaynagbar/nagbar.h @@ -0,0 +1,88 @@ +#ifndef _SWAY_NAGBAR_NAGBAR_H +#define _SWAY_NAGBAR_NAGNAR_H +#include +#include "list.h" +#include "pool-buffer.h" +#include "xdg-output-unstable-v1-client-protocol.h" + +#define NAGBAR_BAR_BORDER_THICKNESS 2 +#define NAGBAR_MESSAGE_PADDING 8 +#define NAGBAR_BUTTON_BORDER_THICKNESS 3 +#define NAGBAR_BUTTON_GAP 20 +#define NAGBAR_BUTTON_GAP_CLOSE 15 +#define NAGBAR_BUTTON_MARGIN_RIGHT 2 +#define NAGBAR_BUTTON_PADDING 3 + +enum sway_nagbar_type { + NAGBAR_ERROR, + NAGBAR_WARNING, +}; + +struct sway_nagbar_colors { + uint32_t button_background; + uint32_t background; + uint32_t text; + uint32_t border; + uint32_t border_bottom; +}; + +struct sway_nagbar_pointer { + struct wl_pointer *pointer; + struct wl_cursor_theme *cursor_theme; + struct wl_cursor_image *cursor_image; + struct wl_surface *cursor_surface; + int x; + int y; +}; + +struct sway_nagbar_output { + char *name; + struct wl_output *wl_output; + uint32_t wl_name; +}; + +struct sway_nagbar_button { + char *text; + char *action; + int x; + int y; + int width; + int height; +}; + +struct sway_nagbar { + bool run_display; + int querying_outputs; + + struct wl_display *display; + struct wl_compositor *compositor; + struct wl_seat *seat; + struct wl_shm *shm; + struct sway_nagbar_pointer pointer; + struct zxdg_output_manager_v1 *xdg_output_manager; + struct sway_nagbar_output output; + struct zwlr_layer_shell_v1 *layer_shell; + struct zwlr_layer_surface_v1 *layer_surface; + struct wl_surface *surface; + + uint32_t width; + uint32_t height; + int32_t scale; + struct pool_buffer buffers[2]; + struct pool_buffer *current_buffer; + + enum sway_nagbar_type type; + struct sway_nagbar_colors colors; + uint32_t anchors; + char *message; + char *font; + list_t *buttons; +}; + +void nagbar_setup(struct sway_nagbar *nagbar); + +void nagbar_run(struct sway_nagbar *nagbar); + +void nagbar_destroy(struct sway_nagbar *nagbar); + +#endif diff --git a/include/swaynagbar/render.h b/include/swaynagbar/render.h new file mode 100644 index 00000000..d9429f7f --- /dev/null +++ b/include/swaynagbar/render.h @@ -0,0 +1,6 @@ +#ifndef _SWAY_NAGBAR_RENDER_H +#define _SWAY_NAGBAR_RENDER_H + +void render_frame(struct sway_nagbar *nagbar); + +#endif diff --git a/meson.build b/meson.build index 05d334d2..26d36e06 100644 --- a/meson.build +++ b/meson.build @@ -82,6 +82,7 @@ if scdoc.found() 'swaylock/swaylock.1.scd', 'swaymsg/swaymsg.1.scd', 'swayidle/swayidle.1.scd', + 'swaynagbar/swaynagbar.1.scd', ] foreach filename : man_files topic = filename.split('.')[-3].split('/')[-1] @@ -130,6 +131,7 @@ subdir('swaybg') subdir('swaybar') subdir('swaylock') subdir('swayidle') +subdir('swaynagbar') config = configuration_data() config.set('sysconfdir', join_paths(prefix, sysconfdir)) diff --git a/swaynagbar/main.c b/swaynagbar/main.c new file mode 100644 index 00000000..f5ed064e --- /dev/null +++ b/swaynagbar/main.c @@ -0,0 +1,184 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "log.h" +#include "list.h" +#include "swaynagbar/nagbar.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static struct sway_nagbar nagbar; + +void sig_handler(int signal) { + nagbar_destroy(&nagbar); + exit(EXIT_FAILURE); +} + +void sway_terminate(int code) { + nagbar_destroy(&nagbar); + exit(code); +} + +static void set_nagbar_colors() { + if (nagbar.type == NAGBAR_ERROR) { + nagbar.colors.button_background = 0x680A0AFF; + nagbar.colors.background = 0x900000FF; + nagbar.colors.text = 0xFFFFFFFF; + nagbar.colors.border = 0xD92424FF; + nagbar.colors.border_bottom = 0x470909FF; + } else if (nagbar.type == NAGBAR_WARNING) { + nagbar.colors.button_background = 0xFFC100FF; + nagbar.colors.background = 0xFFA800FF; + nagbar.colors.text = 0x000000FF; + nagbar.colors.border = 0xAB7100FF; + nagbar.colors.border_bottom = 0xAB7100FF; + } +} + +int main(int argc, char **argv) { + int exit_code = EXIT_SUCCESS; + bool debug = false; + + memset(&nagbar, 0, sizeof(nagbar)); + nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + nagbar.type = NAGBAR_ERROR; + set_nagbar_colors(); + nagbar.font = strdup("pango:monospace 8"); + nagbar.buttons = create_list(); + + struct sway_nagbar_button *button_close = + calloc(sizeof(struct sway_nagbar_button), 1); + button_close->text = strdup("X"); + button_close->action = NULL; + list_add(nagbar.buttons, button_close); + + static struct option long_options[] = { + {"button", required_argument, NULL, 'b'}, + {"debug", no_argument, NULL, 'd'}, + {"edge", required_argument, NULL, 'e'}, + {"font", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"message", required_argument, NULL, 'm'}, + {"output", required_argument, NULL, 'o'}, + {"type", required_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {0, 0, 0, 0} + }; + + const char *usage = + "Usage: swaynagbar [options...]\n" + "\n" + " -b, --button Create a button with text that " + "executes action when pressed. Multiple buttons can be defined.\n" + " -d, --debug Enable debugging.\n" + " -e, --edge top|bottom Set the edge to use.\n" + " -f, --font Set the font to use.\n" + " -h, --help Show help message and quit.\n" + " -m, --message Set the message text.\n" + " -o, --output Set the output to use.\n" + " -t, --type error|warning Set the message type.\n" + " -v, --version Show the version number and quit.\n"; + + while (1) { + int c = getopt_long(argc, argv, "b:de:f:hm:o:t:v", long_options, NULL); + if (c == -1) { + break; + } + switch (c) { + case 'b': // Button + if (optind >= argc) { + fprintf(stderr, "Missing action for button %s", optarg); + exit_code = EXIT_FAILURE; + goto cleanup; + } + struct sway_nagbar_button *button; + button = calloc(sizeof(struct sway_nagbar_button), 1); + button->text = strdup(optarg); + button->action = strdup(argv[optind]); + optind++; + list_add(nagbar.buttons, button); + break; + case 'd': // Debug + debug = true; + break; + case 'e': // Edge + if (strcmp(optarg, "top") == 0) { + nagbar.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) { + nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else { + fprintf(stderr, "Invalid edge: %s\n", optarg); + exit_code = EXIT_FAILURE; + goto cleanup; + } + break; + case 'f': // Font + free(nagbar.font); + nagbar.font = strdup(optarg); + break; + case 'm': // Message + free(nagbar.message); + nagbar.message = strdup(optarg); + break; + case 'o': // Output + free(nagbar.output.name); + nagbar.output.name = strdup(optarg); + break; + case 't': // Type + if (strcmp(optarg, "error") == 0) { + nagbar.type = NAGBAR_ERROR; + } else if (strcmp(optarg, "warning") == 0) { + nagbar.type = NAGBAR_WARNING; + } else { + fprintf(stderr, "Type must be either 'error' or 'warning'"); + exit_code = EXIT_FAILURE; + goto cleanup; + } + set_nagbar_colors(); + break; + case 'v': // Version + fprintf(stdout, "sway version " SWAY_VERSION "\n"); + exit_code = EXIT_SUCCESS; + goto cleanup; + default: // Help or unknown flag + fprintf(c == 'h' ? stdout : stderr, "%s", usage); + exit_code = c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; + goto cleanup; + } + } + + wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); + + if (!nagbar.message) { + wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); + exit_code = EXIT_FAILURE; + goto cleanup; + } + + wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); + wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); + wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); + wlr_log(WLR_DEBUG, "Message: %s", nagbar.message); + wlr_log(WLR_DEBUG, "Font: %s", nagbar.font); + wlr_log(WLR_DEBUG, "Buttons"); + for (int i = 0; i < nagbar.buttons->length; i++) { + struct sway_nagbar_button *button = nagbar.buttons->items[i]; + wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action); + } + + signal(SIGTERM, sig_handler); + + nagbar_setup(&nagbar); + nagbar_run(&nagbar); + return exit_code; + +cleanup: + nagbar_destroy(&nagbar); + return exit_code; +} + diff --git a/swaynagbar/meson.build b/swaynagbar/meson.build new file mode 100644 index 00000000..254462f2 --- /dev/null +++ b/swaynagbar/meson.build @@ -0,0 +1,20 @@ +executable( + 'swaynagbar', [ + 'main.c', + 'nagbar.c', + 'render.c', + ], + include_directories: [sway_inc], + dependencies: [ + cairo, + client_protos, + gdk_pixbuf, + pango, + pangocairo, + wayland_client, + wayland_cursor, + wlroots, + ], + link_with: [lib_sway_common, lib_sway_client], + install: true +) diff --git a/swaynagbar/nagbar.c b/swaynagbar/nagbar.c new file mode 100644 index 00000000..22e5aff4 --- /dev/null +++ b/swaynagbar/nagbar.c @@ -0,0 +1,364 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include "log.h" +#include "list.h" +#include "swaynagbar/nagbar.h" +#include "swaynagbar/render.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/swaynagbarXXXXXX"; + FILE *tmp= fdopen(mkstemp(fname), "w"); + if (!tmp) { + wlr_log(WLR_ERROR, "Failed to create temp script"); + return false; + } + wlr_log(WLR_DEBUG, "Created temp script: %s", fname); + fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); + fclose(tmp); + chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); + char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; + sprintf(cmd, "%s -e %s", terminal, fname); + execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); + return true; +} + +static void nagbar_button_execute(struct sway_nagbar *nagbar, + struct sway_nagbar_button *button) { + wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); + if (!button->action) { + nagbar->run_display = false; + } else { + if (fork() == 0) { + // Child process. Will be used to prevent zombie processes + setsid(); + if (fork() == 0) { + // Child of the child. Will be reparented to the init process + char *terminal = getenv("TERMINAL"); + if (terminal && strlen(terminal)) { + wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); + if (!terminal_execute(terminal, button->action)) { + nagbar_destroy(nagbar); + exit(EXIT_FAILURE); + } + } else { + wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); + execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); + } + } + exit(EXIT_SUCCESS); + } + } + wait(0); +} + +static void layer_surface_configure(void *data, + struct zwlr_layer_surface_v1 *surface, + uint32_t serial, uint32_t width, uint32_t height) { + struct sway_nagbar *nagbar = data; + nagbar->width = width; + nagbar->height = height; + zwlr_layer_surface_v1_ack_configure(surface, serial); + render_frame(nagbar); +} + +static void layer_surface_closed(void *data, + struct zwlr_layer_surface_v1 *surface) { + struct sway_nagbar *nagbar = data; + nagbar_destroy(nagbar); +} + +static struct zwlr_layer_surface_v1_listener layer_surface_listener = { + .configure = layer_surface_configure, + .closed = layer_surface_closed, +}; + +static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface, + wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct sway_nagbar *nagbar = data; + struct sway_nagbar_pointer *pointer = &nagbar->pointer; + wl_surface_set_buffer_scale(pointer->cursor_surface, nagbar->scale); + wl_surface_attach(pointer->cursor_surface, + wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); + wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, + pointer->cursor_image->hotspot_x / nagbar->scale, + pointer->cursor_image->hotspot_y / nagbar->scale); + wl_surface_commit(pointer->cursor_surface); +} + +static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, + uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct sway_nagbar *nagbar = data; + nagbar->pointer.x = wl_fixed_to_int(surface_x); + nagbar->pointer.y = wl_fixed_to_int(surface_y); +} + +static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { + struct sway_nagbar *nagbar = data; + + if (state != WL_POINTER_BUTTON_STATE_PRESSED) { + return; + } + + double x = nagbar->pointer.x * nagbar->scale; + double y = nagbar->pointer.y * nagbar->scale; + for (int i = 0; i < nagbar->buttons->length; i++) { + struct sway_nagbar_button *nagbutton = nagbar->buttons->items[i]; + if (x >= nagbutton->x + && y >= nagbutton->y + && x < nagbutton->x + nagbutton->width + && y < nagbutton->y + nagbutton->height) { + nagbar_button_execute(nagbar, nagbutton); + } + } +} + +static struct wl_pointer_listener pointer_listener = { + .enter = wl_pointer_enter, + .leave = nop, + .motion = wl_pointer_motion, + .button = wl_pointer_button, + .axis = nop, + .frame = nop, + .axis_source = nop, + .axis_stop = nop, + .axis_discrete = nop, +}; + +static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, + enum wl_seat_capability caps) { + struct sway_nagbar *nagbar = data; + if ((caps & WL_SEAT_CAPABILITY_POINTER)) { + nagbar->pointer.pointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(nagbar->pointer.pointer, &pointer_listener, + nagbar); + } +} + +const struct wl_seat_listener seat_listener = { + .capabilities = seat_handle_capabilities, + .name = nop, +}; + +static void output_scale(void *data, struct wl_output *output, + int32_t factor) { + struct sway_nagbar *nagbar = data; + nagbar->scale = factor; + render_frame(nagbar); +} + +static struct wl_output_listener output_listener = { + .geometry = nop, + .mode = nop, + .done = nop, + .scale = output_scale, +}; + +struct output_state { + struct wl_output *wl_output; + uint32_t wl_name; + struct zxdg_output_v1 *xdg_output; + struct sway_nagbar *nagbar; +}; + +static void xdg_output_handle_name(void *data, + struct zxdg_output_v1 *xdg_output, const char *name) { + struct output_state *state = data; + char *outname = state->nagbar->output.name; + wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); + if ((!outname && !state->nagbar->output.wl_output) + || (name && outname && strcmp(name, outname) == 0)) { + wlr_log(WLR_DEBUG, "Using output %s", name); + state->nagbar->output.wl_output = state->wl_output; + state->nagbar->output.wl_name = state->wl_name; + wl_output_add_listener(state->nagbar->output.wl_output, + &output_listener, state->nagbar); + wl_display_roundtrip(state->nagbar->display); + zxdg_output_v1_destroy(state->xdg_output); + } else { + zxdg_output_v1_destroy(state->xdg_output); + wl_output_destroy(state->wl_output); + } + state->nagbar->querying_outputs--; + free(state); +} + +static struct zxdg_output_v1_listener xdg_output_listener = { + .logical_position = nop, + .logical_size = nop, + .done = nop, + .name = xdg_output_handle_name, + .description = nop, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + struct sway_nagbar *nagbar = data; + if (strcmp(interface, wl_compositor_interface.name) == 0) { + nagbar->compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 3); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + nagbar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); + wl_seat_add_listener(nagbar->seat, &seat_listener, nagbar); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + nagbar->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, wl_output_interface.name) == 0) { + if (!nagbar->output.wl_output && nagbar->xdg_output_manager) { + nagbar->querying_outputs++; + struct output_state *state = + calloc(1, sizeof(struct output_state)); + state->nagbar = nagbar; + state->wl_output = wl_registry_bind(registry, name, + &wl_output_interface, 3); + state->wl_name = name; + state->xdg_output = zxdg_output_manager_v1_get_xdg_output( + nagbar->xdg_output_manager, state->wl_output); + zxdg_output_v1_add_listener(state->xdg_output, + &xdg_output_listener, state); + } else if (!nagbar->output.wl_output && !nagbar->xdg_output_manager) { + wlr_log(WLR_ERROR, "Warning: zxdg_output_manager_v1 not supported." + " Falling back to first detected output"); + nagbar->output.wl_output = wl_registry_bind(registry, name, + &wl_output_interface, 3); + nagbar->output.wl_name = name; + wl_output_add_listener(nagbar->output.wl_output, + &output_listener, nagbar); + } + } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { + nagbar->layer_shell = wl_registry_bind( + registry, name, &zwlr_layer_shell_v1_interface, 1); + } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 + && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { + nagbar->xdg_output_manager = wl_registry_bind(registry, name, + &zxdg_output_manager_v1_interface, + ZXDG_OUTPUT_V1_NAME_SINCE_VERSION); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + struct sway_nagbar *nagbar = data; + if (nagbar->output.wl_name == name) { + nagbar->run_display = false; + } +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +void nagbar_setup(struct sway_nagbar *nagbar) { + nagbar->display = wl_display_connect(NULL); + assert(nagbar->display); + + nagbar->scale = 1; + + struct wl_registry *registry = wl_display_get_registry(nagbar->display); + wl_registry_add_listener(registry, ®istry_listener, nagbar); + wl_display_roundtrip(nagbar->display); + assert(nagbar->compositor && nagbar->layer_shell && nagbar->shm); + + while (nagbar->querying_outputs > 0) { + wl_display_roundtrip(nagbar->display); + } + + if (!nagbar->output.wl_output) { + if (nagbar->output.name) { + wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name); + } else { + wlr_log(WLR_ERROR, "No outputs detected"); + } + nagbar_destroy(nagbar); + exit(EXIT_FAILURE); + } + + struct sway_nagbar_pointer *pointer = &nagbar->pointer; + int scale = nagbar->scale < 1 ? 1 : nagbar->scale; + pointer->cursor_theme = wl_cursor_theme_load( + NULL, 24 * scale, nagbar->shm); + assert(pointer->cursor_theme); + struct wl_cursor *cursor = + wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); + assert(cursor); + pointer->cursor_image = cursor->images[0]; + pointer->cursor_surface = wl_compositor_create_surface(nagbar->compositor); + assert(pointer->cursor_surface); + + nagbar->surface = wl_compositor_create_surface(nagbar->compositor); + assert(nagbar->surface); + nagbar->layer_surface = zwlr_layer_shell_v1_get_layer_surface( + nagbar->layer_shell, nagbar->surface, nagbar->output.wl_output, + ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "nagbar"); + assert(nagbar->layer_surface); + zwlr_layer_surface_v1_add_listener(nagbar->layer_surface, + &layer_surface_listener, nagbar); + zwlr_layer_surface_v1_set_anchor(nagbar->layer_surface, nagbar->anchors); + + wl_registry_destroy(registry); +} + +void nagbar_run(struct sway_nagbar *nagbar) { + nagbar->run_display = true; + render_frame(nagbar); + while (nagbar->run_display && wl_display_dispatch(nagbar->display) != -1) { + // This is intentionally left blank + } +} + +void nagbar_destroy(struct sway_nagbar *nagbar) { + nagbar->run_display = false; + + free(nagbar->message); + free(nagbar->font); + while (nagbar->buttons->length) { + struct sway_nagbar_button *button = nagbar->buttons->items[0]; + list_del(nagbar->buttons, 0); + free(button->text); + free(button->action); + free(button); + } + list_free(nagbar->buttons); + + if (nagbar->layer_surface) { + zwlr_layer_surface_v1_destroy(nagbar->layer_surface); + } + + if (nagbar->surface) { + wl_surface_destroy(nagbar->surface); + } + + if (nagbar->output.wl_output) { + wl_output_destroy(nagbar->output.wl_output); + } + + if (&nagbar->buffers[0]) { + destroy_buffer(&nagbar->buffers[0]); + } + + if (&nagbar->buffers[1]) { + destroy_buffer(&nagbar->buffers[1]); + } + + if (nagbar->compositor) { + wl_compositor_destroy(nagbar->compositor); + } + + if (nagbar->shm) { + wl_shm_destroy(nagbar->shm); + } + + if (nagbar->display) { + wl_display_disconnect(nagbar->display); + } +} diff --git a/swaynagbar/render.c b/swaynagbar/render.c new file mode 100644 index 00000000..c0f59298 --- /dev/null +++ b/swaynagbar/render.c @@ -0,0 +1,152 @@ +#include +#include "cairo.h" +#include "log.h" +#include "pango.h" +#include "pool-buffer.h" +#include "swaynagbar/nagbar.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { + uint32_t height = nagbar->height * nagbar->scale; + height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + + int text_width, text_height; + get_text_size(cairo, nagbar->font, &text_width, &text_height, + nagbar->scale, true, "%s", nagbar->message); + + int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; + + uint32_t ideal_height = text_height + padding * 2; + uint32_t ideal_surface_height = ideal_height / nagbar->scale; + if (nagbar->height < ideal_surface_height) { + return ideal_surface_height; + } + + cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_move_to(cairo, padding, (int)(height / 2.0 - text_height / 2.0)); + pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", + nagbar->message); + + return nagbar->height; +} + +static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, + int button_index, int *x) { + uint32_t height = nagbar->height * nagbar->scale; + height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + struct sway_nagbar_button *button = nagbar->buttons->items[button_index]; + + int text_width, text_height; + get_text_size(cairo, nagbar->font, &text_width, &text_height, + nagbar->scale, true, "%s", button->text); + + int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + + uint32_t ideal_height = text_height + padding * 2 + border * 2; + uint32_t ideal_surface_height = ideal_height / nagbar->scale; + if (nagbar->height < ideal_surface_height) { + return ideal_surface_height; + } + + button->x = *x - border - text_width - padding * 2; + button->y = (int)(height / 2.0 - text_height / 2.0) - padding; + button->width = text_width + padding * 2; + button->height = text_height + padding * 2; + + cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_rectangle(cairo, button->x - border, button->y - border, + button->width + border * 2, button->height + border * 2); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.button_background); + cairo_rectangle(cairo, button->x, button->y, + button->width, button->height); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_move_to(cairo, button->x + padding, button->y + padding); + pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); + + *x = button->x - border; + + return nagbar->height; +} + +static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { + uint32_t max_height = 0; + + cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); + cairo_set_source_u32(cairo, nagbar->colors.background); + cairo_paint(cairo); + + uint32_t h = render_message(cairo, nagbar); + max_height = h > max_height ? h : max_height; + + int x = (nagbar->width - NAGBAR_BUTTON_MARGIN_RIGHT) * nagbar->scale; + for (int i = 0; i < nagbar->buttons->length; i++) { + h = render_button(cairo, nagbar, i, &x); + max_height = h > max_height ? h : max_height; + x -= NAGBAR_BUTTON_GAP * nagbar->scale; + if (i == 0) { + x -= NAGBAR_BUTTON_GAP_CLOSE * nagbar->scale; + } + } + + int border = NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + if (max_height > nagbar->height) { + max_height += border; + } + cairo_set_source_u32(cairo, nagbar->colors.border_bottom); + cairo_rectangle(cairo, 0, nagbar->height * nagbar->scale - border, + nagbar->width * nagbar->scale, border); + cairo_fill(cairo); + + return max_height > nagbar->height ? max_height : nagbar->height; +} + +void render_frame(struct sway_nagbar *nagbar) { + if (!nagbar->run_display) { + return; + } + + cairo_surface_t *recorder = cairo_recording_surface_create( + CAIRO_CONTENT_COLOR_ALPHA, NULL); + cairo_t *cairo = cairo_create(recorder); + cairo_save(cairo); + cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); + cairo_paint(cairo); + cairo_restore(cairo); + uint32_t height = render_to_cairo(cairo, nagbar); + if (height != nagbar->height) { + zwlr_layer_surface_v1_set_size(nagbar->layer_surface, 0, height); + zwlr_layer_surface_v1_set_exclusive_zone(nagbar->layer_surface, + height); + wl_surface_commit(nagbar->surface); + wl_display_roundtrip(nagbar->display); + } else { + nagbar->current_buffer = get_next_buffer(nagbar->shm, + nagbar->buffers, + nagbar->width * nagbar->scale, + nagbar->height * nagbar->scale); + cairo_t *shm = nagbar->current_buffer->cairo; + + cairo_save(shm); + cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); + cairo_paint(shm); + cairo_restore(shm); + + cairo_set_source_surface(shm, recorder, 0.0, 0.0); + cairo_paint(shm); + + wl_surface_set_buffer_scale(nagbar->surface, nagbar->scale); + wl_surface_attach(nagbar->surface, + nagbar->current_buffer->buffer, 0, 0); + wl_surface_damage(nagbar->surface, 0, 0, + nagbar->width, nagbar->height); + wl_surface_commit(nagbar->surface); + wl_display_roundtrip(nagbar->display); + } + cairo_surface_destroy(recorder); + cairo_destroy(cairo); +} diff --git a/swaynagbar/swaynagbar.1.scd b/swaynagbar/swaynagbar.1.scd new file mode 100644 index 00000000..4235e2ea --- /dev/null +++ b/swaynagbar/swaynagbar.1.scd @@ -0,0 +1,41 @@ +swaynagbar(1) + +# NAME + +swaynagbar - Show a warning or error message with buttons + +# SYNOPSIS + +_swaynagbar_ [options...] + +# OPTIONS +*-b, --button* + Create a button with the text _text_ that executes _action_ when pressed. + Multiple buttons can be defined by providing the flag multiple times. + +*-d, --debug* + Enable debugging. + +*-e, --edge top|bottom* + Set the edge to use. + +*-f, --font * + Set the font to use. + +*-h, --help* + Show help message and quit. + +*-m, --message * + 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 + +*-t, --type error|warning* + Set the message type. + +*-v, --version + Show the version number and quit. + -- cgit v1.2.3-54-g00ecf From 72db10c2f1a1a216c50f68461a840eea3948e878 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Fri, 27 Jul 2018 01:30:35 -0400 Subject: Support a detailed message in swaynagbar --- include/swaynagbar/nagbar.h | 27 ++++++++ swaynagbar/main.c | 76 +++++++++++++++++++-- swaynagbar/nagbar.c | 60 ++++++++++++++++- swaynagbar/render.c | 160 ++++++++++++++++++++++++++++++++++++++++++-- swaynagbar/swaynagbar.1.scd | 12 ++++ 5 files changed, 321 insertions(+), 14 deletions(-) diff --git a/include/swaynagbar/nagbar.h b/include/swaynagbar/nagbar.h index 07a0d51e..8b55e4fa 100644 --- a/include/swaynagbar/nagbar.h +++ b/include/swaynagbar/nagbar.h @@ -7,17 +7,26 @@ #define NAGBAR_BAR_BORDER_THICKNESS 2 #define NAGBAR_MESSAGE_PADDING 8 +#define NAGBAR_DETAILS_BORDER_THICKNESS 3 #define NAGBAR_BUTTON_BORDER_THICKNESS 3 #define NAGBAR_BUTTON_GAP 20 #define NAGBAR_BUTTON_GAP_CLOSE 15 #define NAGBAR_BUTTON_MARGIN_RIGHT 2 #define NAGBAR_BUTTON_PADDING 3 +#define NAGBAR_MAX_HEIGHT 500 + enum sway_nagbar_type { NAGBAR_ERROR, NAGBAR_WARNING, }; +enum sway_nagbar_action_type { + NAGBAR_ACTION_DISMISS, + NAGBAR_ACTION_EXPAND, + NAGBAR_ACTION_COMMAND, +}; + struct sway_nagbar_colors { uint32_t button_background; uint32_t background; @@ -43,6 +52,7 @@ struct sway_nagbar_output { struct sway_nagbar_button { char *text; + enum sway_nagbar_action_type type; char *action; int x; int y; @@ -50,6 +60,22 @@ struct sway_nagbar_button { int height; }; +struct sway_nagbar_details { + bool visible; + char *message; + + int x; + int y; + int width; + int height; + + int offset; + int visible_lines; + int total_lines; + struct sway_nagbar_button button_up; + struct sway_nagbar_button button_down; +}; + struct sway_nagbar { bool run_display; int querying_outputs; @@ -77,6 +103,7 @@ struct sway_nagbar { char *message; char *font; list_t *buttons; + struct sway_nagbar_details details; }; void nagbar_setup(struct sway_nagbar *nagbar); diff --git a/swaynagbar/main.c b/swaynagbar/main.c index f5ed064e..389118c6 100644 --- a/swaynagbar/main.c +++ b/swaynagbar/main.c @@ -3,6 +3,7 @@ #include #include "log.h" #include "list.h" +#include "readline.h" #include "swaynagbar/nagbar.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" @@ -34,6 +35,32 @@ static void set_nagbar_colors() { } } +static char *read_from_stdin() { + char *buffer = NULL; + while (!feof(stdin)) { + char *line = read_line(stdin); + if (!line) { + continue; + } + + if (!buffer) { + buffer = strdup(line); + } else { + buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); + strcat(buffer, line); + strcat(buffer, "\n"); + } + + free(line); + } + + if (buffer && buffer[strlen(buffer) - 1] == '\n') { + buffer[strlen(buffer) - 1] = '\0'; + } + + return buffer; +} + int main(int argc, char **argv) { int exit_code = EXIT_SUCCESS; bool debug = false; @@ -50,17 +77,25 @@ int main(int argc, char **argv) { struct sway_nagbar_button *button_close = calloc(sizeof(struct sway_nagbar_button), 1); button_close->text = strdup("X"); - button_close->action = NULL; + button_close->type = NAGBAR_ACTION_DISMISS; list_add(nagbar.buttons, button_close); - static struct option long_options[] = { + struct sway_nagbar_button *button_details = + calloc(sizeof(struct sway_nagbar_button), 1); + button_details->text = strdup("Toggle Details"); + button_details->type = NAGBAR_ACTION_EXPAND; + + static struct option opts[] = { {"button", required_argument, NULL, 'b'}, {"debug", no_argument, NULL, 'd'}, {"edge", required_argument, NULL, 'e'}, {"font", required_argument, NULL, 'f'}, {"help", no_argument, NULL, 'h'}, + {"detailed-message", required_argument, NULL, 'l'}, + {"detailed-button", required_argument, NULL, 'L'}, {"message", required_argument, NULL, 'm'}, {"output", required_argument, NULL, 'o'}, + {"dismiss-button", required_argument, NULL, 's'}, {"type", required_argument, NULL, 't'}, {"version", no_argument, NULL, 'v'}, {0, 0, 0, 0} @@ -75,26 +110,30 @@ int main(int argc, char **argv) { " -e, --edge top|bottom Set the edge to use.\n" " -f, --font Set the font to use.\n" " -h, --help Show help message and quit.\n" + " -l, --detailed-message Set a detailed message.\n" + " -L, --detailed-button Set the text of the detail button.\n" " -m, --message Set the message text.\n" " -o, --output Set the output to use.\n" + " -s, --dismiss-button Set the dismiss button text.\n" " -t, --type error|warning Set the message type.\n" " -v, --version Show the version number and quit.\n"; while (1) { - int c = getopt_long(argc, argv, "b:de:f:hm:o:t:v", long_options, NULL); + int c = getopt_long(argc, argv, "b:de:f:hl:L:m:o:s:t:v", opts, NULL); if (c == -1) { break; } switch (c) { case 'b': // Button if (optind >= argc) { - fprintf(stderr, "Missing action for button %s", optarg); + fprintf(stderr, "Missing action for button %s\n", optarg); exit_code = EXIT_FAILURE; goto cleanup; } struct sway_nagbar_button *button; button = calloc(sizeof(struct sway_nagbar_button), 1); button->text = strdup(optarg); + button->type = NAGBAR_ACTION_COMMAND; button->action = strdup(argv[optind]); optind++; list_add(nagbar.buttons, button); @@ -121,6 +160,20 @@ int main(int argc, char **argv) { free(nagbar.font); nagbar.font = strdup(optarg); break; + case 'l': // Detailed Message + free(nagbar.details.message); + if (strcmp(optarg, "-") == 0) { + nagbar.details.message = read_from_stdin(); + } else { + nagbar.details.message = strdup(optarg); + } + nagbar.details.button_up.text = strdup("▲"); + nagbar.details.button_down.text = strdup("▼"); + break; + case 'L': // Detailed Button Text + free(button_details->text); + button_details->text = strdup(optarg); + break; case 'm': // Message free(nagbar.message); nagbar.message = strdup(optarg); @@ -129,13 +182,17 @@ int main(int argc, char **argv) { free(nagbar.output.name); nagbar.output.name = strdup(optarg); break; + case 's': // Dismiss Button Text + free(button_close->text); + button_close->text = strdup(optarg); + break; case 't': // Type if (strcmp(optarg, "error") == 0) { nagbar.type = NAGBAR_ERROR; } else if (strcmp(optarg, "warning") == 0) { nagbar.type = NAGBAR_WARNING; } else { - fprintf(stderr, "Type must be either 'error' or 'warning'"); + fprintf(stderr, "Type must be either 'error' or 'warning'\n"); exit_code = EXIT_FAILURE; goto cleanup; } @@ -160,6 +217,13 @@ int main(int argc, char **argv) { goto cleanup; } + if (nagbar.details.message) { + list_add(nagbar.buttons, button_details); + } else { + free(button_details->text); + free(button_details); + } + wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); @@ -178,6 +242,8 @@ int main(int argc, char **argv) { return exit_code; cleanup: + free(button_details->text); + free(button_details); nagbar_destroy(&nagbar); return exit_code; } diff --git a/swaynagbar/nagbar.c b/swaynagbar/nagbar.c index 22e5aff4..9e8bbb4b 100644 --- a/swaynagbar/nagbar.c +++ b/swaynagbar/nagbar.c @@ -34,8 +34,11 @@ static bool terminal_execute(char *terminal, char *command) { static void nagbar_button_execute(struct sway_nagbar *nagbar, struct sway_nagbar_button *button) { wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); - if (!button->action) { + if (button->type == NAGBAR_ACTION_DISMISS) { nagbar->run_display = false; + } else if (button->type == NAGBAR_ACTION_EXPAND) { + nagbar->details.visible = !nagbar->details.visible; + render_frame(nagbar); } else { if (fork() == 0) { // Child process. Will be used to prevent zombie processes @@ -119,8 +122,58 @@ static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, && x < nagbutton->x + nagbutton->width && y < nagbutton->y + nagbutton->height) { nagbar_button_execute(nagbar, nagbutton); + return; } } + + if (nagbar->details.visible && + nagbar->details.total_lines != nagbar->details.visible_lines) { + struct sway_nagbar_button button_up = nagbar->details.button_up; + if (x >= button_up.x + && y >= button_up.y + && x < button_up.x + button_up.width + && y < button_up.y + button_up.height + && nagbar->details.offset > 0) { + nagbar->details.offset--; + render_frame(nagbar); + return; + } + + struct sway_nagbar_button button_down = nagbar->details.button_down; + int bot = nagbar->details.total_lines - nagbar->details.visible_lines; + if (x >= button_down.x + && y >= button_down.y + && x < button_down.x + button_down.width + && y < button_down.y + button_down.height + && nagbar->details.offset < bot) { + nagbar->details.offset++; + render_frame(nagbar); + return; + } + } +} + +static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) { + struct sway_nagbar *nagbar = data; + if (!nagbar->details.visible + || nagbar->pointer.x < nagbar->details.x + || nagbar->pointer.y < nagbar->details.y + || nagbar->pointer.x >= nagbar->details.x + nagbar->details.width + || nagbar->pointer.y >= nagbar->details.y + nagbar->details.height + || nagbar->details.total_lines == nagbar->details.visible_lines) { + return; + } + + int direction = wl_fixed_to_int(value); + int bot = nagbar->details.total_lines - nagbar->details.visible_lines; + if (direction < 0 && nagbar->details.offset > 0) { + nagbar->details.offset--; + } else if (direction > 0 && nagbar->details.offset < bot) { + nagbar->details.offset++; + } + + render_frame(nagbar); } static struct wl_pointer_listener pointer_listener = { @@ -128,7 +181,7 @@ static struct wl_pointer_listener pointer_listener = { .leave = nop, .motion = wl_pointer_motion, .button = wl_pointer_button, - .axis = nop, + .axis = wl_pointer_axis, .frame = nop, .axis_source = nop, .axis_stop = nop, @@ -329,6 +382,9 @@ void nagbar_destroy(struct sway_nagbar *nagbar) { free(button); } list_free(nagbar->buttons); + free(nagbar->details.message); + free(nagbar->details.button_up.text); + free(nagbar->details.button_down.text); if (nagbar->layer_surface) { zwlr_layer_surface_v1_destroy(nagbar->layer_surface); diff --git a/swaynagbar/render.c b/swaynagbar/render.c index c0f59298..7bc2961e 100644 --- a/swaynagbar/render.c +++ b/swaynagbar/render.c @@ -23,11 +23,149 @@ static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { } cairo_set_source_u32(cairo, nagbar->colors.text); - cairo_move_to(cairo, padding, (int)(height / 2.0 - text_height / 2.0)); + cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", nagbar->message); - return nagbar->height; + return ideal_height; +} + +static void render_details_scroll_button(cairo_t *cairo, + struct sway_nagbar *nagbar, struct sway_nagbar_button *button) { + int text_width, text_height; + get_text_size(cairo, nagbar->font, &text_width, &text_height, + nagbar->scale, true, "%s", button->text); + + int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + + cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_rectangle(cairo, button->x, button->y, + button->width, button->height); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.button_background); + cairo_rectangle(cairo, button->x + border, button->y + border, + button->width - (border * 2), button->height - (border * 2)); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_move_to(cairo, button->x + border + padding, + button->y + border + (button->height - text_height) / 2); + pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); +} + +static int get_detailed_scroll_button_width(cairo_t *cairo, + struct sway_nagbar *nagbar) { + int up_width, down_width, temp_height; + get_text_size(cairo, nagbar->font, &up_width, &temp_height, + nagbar->scale, true, "%s", nagbar->details.button_up.text); + get_text_size(cairo, nagbar->font, &down_width, &temp_height, + nagbar->scale, true, "%s", nagbar->details.button_down.text); + + int text_width = up_width > down_width ? up_width : down_width; + int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + + return text_width + border * 2 + padding * 2; +} + +static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, + uint32_t y) { + uint32_t width = nagbar->width * nagbar->scale; + uint32_t height = nagbar->height * nagbar->scale; + height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + + int border = NAGBAR_DETAILS_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; + int decor = padding + border; + + nagbar->details.x = decor; + nagbar->details.y = y + decor; + nagbar->details.width = width - decor * 2; + + PangoLayout *layout = get_pango_layout(cairo, nagbar->font, + nagbar->details.message, nagbar->scale, true); + pango_layout_set_width(layout, + (nagbar->details.width - padding * 2) * PANGO_SCALE); + pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); + pango_layout_set_single_paragraph_mode(layout, false); + pango_cairo_update_layout(cairo, layout); + nagbar->details.total_lines = pango_layout_get_line_count(layout); + + PangoLayoutLine *line; + line = pango_layout_get_line_readonly(layout, nagbar->details.offset); + gint offset = line->start_index; + const char *text = pango_layout_get_text(layout); + pango_layout_set_text(layout, text + offset, strlen(text) - offset); + + int text_width, text_height; + pango_cairo_update_layout(cairo, layout); + pango_layout_get_pixel_size(layout, &text_width, &text_height); + + bool show_buttons = nagbar->details.offset > 0; + int button_width = get_detailed_scroll_button_width(cairo, nagbar); + if (show_buttons) { + nagbar->details.width -= button_width; + pango_layout_set_width(layout, + (nagbar->details.width - padding * 2) * PANGO_SCALE); + } + + uint32_t ideal_height; + do { + ideal_height = nagbar->details.y + text_height + decor + padding * 2; + if (ideal_height > NAGBAR_MAX_HEIGHT) { + ideal_height = NAGBAR_MAX_HEIGHT; + + if (!show_buttons) { + show_buttons = true; + nagbar->details.width -= button_width; + pango_layout_set_width(layout, + (nagbar->details.width - padding * 2) * PANGO_SCALE); + } + } + + nagbar->details.height = ideal_height - nagbar->details.y - decor; + pango_layout_set_height(layout, + (nagbar->details.height - padding * 2) * PANGO_SCALE); + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); + pango_cairo_update_layout(cairo, layout); + pango_layout_get_pixel_size(layout, &text_width, &text_height); + } while (text_height != (nagbar->details.height - padding * 2)); + + nagbar->details.visible_lines = pango_layout_get_line_count(layout); + + if (show_buttons) { + nagbar->details.button_up.x = + nagbar->details.x + nagbar->details.width; + nagbar->details.button_up.y = nagbar->details.y; + nagbar->details.button_up.width = button_width; + nagbar->details.button_up.height = nagbar->details.height / 2; + render_details_scroll_button(cairo, nagbar, + &nagbar->details.button_up); + + nagbar->details.button_down.x = + nagbar->details.x + nagbar->details.width; + nagbar->details.button_down.y = + nagbar->details.button_up.y + nagbar->details.button_up.height; + nagbar->details.button_down.width = button_width; + nagbar->details.button_down.height = nagbar->details.height / 2; + render_details_scroll_button(cairo, nagbar, + &nagbar->details.button_down); + } + + cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_rectangle(cairo, nagbar->details.x, nagbar->details.y, + nagbar->details.width, nagbar->details.height); + cairo_fill(cairo); + + cairo_move_to(cairo, nagbar->details.x + padding, + nagbar->details.y + padding); + cairo_set_source_u32(cairo, nagbar->colors.text); + pango_cairo_show_layout(cairo, layout); + g_object_unref(layout); + + return ideal_height; } static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, @@ -50,7 +188,7 @@ static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, } button->x = *x - border - text_width - padding * 2; - button->y = (int)(height / 2.0 - text_height / 2.0) - padding; + button->y = (int)(ideal_height - text_height) / 2 - padding; button->width = text_width + padding * 2; button->height = text_height + padding * 2; @@ -70,7 +208,7 @@ static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, *x = button->x - border; - return nagbar->height; + return ideal_height; } static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { @@ -93,6 +231,11 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { } } + if (nagbar->details.visible) { + h = render_detailed(cairo, nagbar, max_height); + max_height = h > max_height ? h : max_height; + } + int border = NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; if (max_height > nagbar->height) { max_height += border; @@ -102,7 +245,7 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { nagbar->width * nagbar->scale, border); cairo_fill(cairo); - return max_height > nagbar->height ? max_height : nagbar->height; + return max_height; } void render_frame(struct sway_nagbar *nagbar) { @@ -129,13 +272,16 @@ void render_frame(struct sway_nagbar *nagbar) { nagbar->buffers, nagbar->width * nagbar->scale, nagbar->height * nagbar->scale); - cairo_t *shm = nagbar->current_buffer->cairo; + if (!nagbar->current_buffer) { + wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame."); + return; + } + cairo_t *shm = nagbar->current_buffer->cairo; cairo_save(shm); cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); cairo_paint(shm); cairo_restore(shm); - cairo_set_source_surface(shm, recorder, 0.0, 0.0); cairo_paint(shm); diff --git a/swaynagbar/swaynagbar.1.scd b/swaynagbar/swaynagbar.1.scd index 4235e2ea..2a34ae68 100644 --- a/swaynagbar/swaynagbar.1.scd +++ b/swaynagbar/swaynagbar.1.scd @@ -25,6 +25,15 @@ _swaynagbar_ [options...] *-h, --help* Show help message and quit. +*-l, --detailed-message * + Set the detailed message. A button to toggle details will be added. Details + are shown in a scrollable multi-line text area. If _msg_ is _-_, then the + detailed message will be read from stdin. + +*-L, --detailed-button * + Set the text for the button that toggles details. This has no effect if + there is not a detailed message. The default is _Toggle Details_. + *-m, --message * Set the message text. @@ -33,6 +42,9 @@ _swaynagbar_ [options...] _xdg\_output\_manager_ is not supported, then the first detected output will be used +*-s, --dismiss-button * + Sets the text for the dismiss nagbar button. The default is _X_. + *-t, --type error|warning* Set the message type. -- cgit v1.2.3-54-g00ecf From a4f7bf23b21d0d838a8a19261d5fd69719003a03 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Fri, 27 Jul 2018 11:19:42 -0400 Subject: Address first round review for swaynag --- include/swaynag/nagbar.h | 115 ++++++++++++ include/swaynag/render.h | 6 + include/swaynagbar/nagbar.h | 115 ------------ include/swaynagbar/render.h | 6 - meson.build | 4 +- sway/desktop/layer_shell.c | 8 +- swaynag/main.c | 246 ++++++++++++++++++++++++++ swaynag/meson.build | 20 +++ swaynag/nagbar.c | 420 ++++++++++++++++++++++++++++++++++++++++++++ swaynag/render.c | 300 +++++++++++++++++++++++++++++++ swaynag/swaynag.1.scd | 52 ++++++ swaynagbar/main.c | 250 -------------------------- swaynagbar/meson.build | 20 --- swaynagbar/nagbar.c | 420 -------------------------------------------- swaynagbar/render.c | 298 ------------------------------- swaynagbar/swaynagbar.1.scd | 53 ------ 16 files changed, 1166 insertions(+), 1167 deletions(-) create mode 100644 include/swaynag/nagbar.h create mode 100644 include/swaynag/render.h delete mode 100644 include/swaynagbar/nagbar.h delete mode 100644 include/swaynagbar/render.h create mode 100644 swaynag/main.c create mode 100644 swaynag/meson.build create mode 100644 swaynag/nagbar.c create mode 100644 swaynag/render.c create mode 100644 swaynag/swaynag.1.scd delete mode 100644 swaynagbar/main.c delete mode 100644 swaynagbar/meson.build delete mode 100644 swaynagbar/nagbar.c delete mode 100644 swaynagbar/render.c delete mode 100644 swaynagbar/swaynagbar.1.scd diff --git a/include/swaynag/nagbar.h b/include/swaynag/nagbar.h new file mode 100644 index 00000000..8b55e4fa --- /dev/null +++ b/include/swaynag/nagbar.h @@ -0,0 +1,115 @@ +#ifndef _SWAY_NAGBAR_NAGBAR_H +#define _SWAY_NAGBAR_NAGNAR_H +#include +#include "list.h" +#include "pool-buffer.h" +#include "xdg-output-unstable-v1-client-protocol.h" + +#define NAGBAR_BAR_BORDER_THICKNESS 2 +#define NAGBAR_MESSAGE_PADDING 8 +#define NAGBAR_DETAILS_BORDER_THICKNESS 3 +#define NAGBAR_BUTTON_BORDER_THICKNESS 3 +#define NAGBAR_BUTTON_GAP 20 +#define NAGBAR_BUTTON_GAP_CLOSE 15 +#define NAGBAR_BUTTON_MARGIN_RIGHT 2 +#define NAGBAR_BUTTON_PADDING 3 + +#define NAGBAR_MAX_HEIGHT 500 + +enum sway_nagbar_type { + NAGBAR_ERROR, + NAGBAR_WARNING, +}; + +enum sway_nagbar_action_type { + NAGBAR_ACTION_DISMISS, + NAGBAR_ACTION_EXPAND, + NAGBAR_ACTION_COMMAND, +}; + +struct sway_nagbar_colors { + uint32_t button_background; + uint32_t background; + uint32_t text; + uint32_t border; + uint32_t border_bottom; +}; + +struct sway_nagbar_pointer { + struct wl_pointer *pointer; + struct wl_cursor_theme *cursor_theme; + struct wl_cursor_image *cursor_image; + struct wl_surface *cursor_surface; + int x; + int y; +}; + +struct sway_nagbar_output { + char *name; + struct wl_output *wl_output; + uint32_t wl_name; +}; + +struct sway_nagbar_button { + char *text; + enum sway_nagbar_action_type type; + char *action; + int x; + int y; + int width; + int height; +}; + +struct sway_nagbar_details { + bool visible; + char *message; + + int x; + int y; + int width; + int height; + + int offset; + int visible_lines; + int total_lines; + struct sway_nagbar_button button_up; + struct sway_nagbar_button button_down; +}; + +struct sway_nagbar { + bool run_display; + int querying_outputs; + + struct wl_display *display; + struct wl_compositor *compositor; + struct wl_seat *seat; + struct wl_shm *shm; + struct sway_nagbar_pointer pointer; + struct zxdg_output_manager_v1 *xdg_output_manager; + struct sway_nagbar_output output; + struct zwlr_layer_shell_v1 *layer_shell; + struct zwlr_layer_surface_v1 *layer_surface; + struct wl_surface *surface; + + uint32_t width; + uint32_t height; + int32_t scale; + struct pool_buffer buffers[2]; + struct pool_buffer *current_buffer; + + enum sway_nagbar_type type; + struct sway_nagbar_colors colors; + uint32_t anchors; + char *message; + char *font; + list_t *buttons; + struct sway_nagbar_details details; +}; + +void nagbar_setup(struct sway_nagbar *nagbar); + +void nagbar_run(struct sway_nagbar *nagbar); + +void nagbar_destroy(struct sway_nagbar *nagbar); + +#endif diff --git a/include/swaynag/render.h b/include/swaynag/render.h new file mode 100644 index 00000000..d9429f7f --- /dev/null +++ b/include/swaynag/render.h @@ -0,0 +1,6 @@ +#ifndef _SWAY_NAGBAR_RENDER_H +#define _SWAY_NAGBAR_RENDER_H + +void render_frame(struct sway_nagbar *nagbar); + +#endif diff --git a/include/swaynagbar/nagbar.h b/include/swaynagbar/nagbar.h deleted file mode 100644 index 8b55e4fa..00000000 --- a/include/swaynagbar/nagbar.h +++ /dev/null @@ -1,115 +0,0 @@ -#ifndef _SWAY_NAGBAR_NAGBAR_H -#define _SWAY_NAGBAR_NAGNAR_H -#include -#include "list.h" -#include "pool-buffer.h" -#include "xdg-output-unstable-v1-client-protocol.h" - -#define NAGBAR_BAR_BORDER_THICKNESS 2 -#define NAGBAR_MESSAGE_PADDING 8 -#define NAGBAR_DETAILS_BORDER_THICKNESS 3 -#define NAGBAR_BUTTON_BORDER_THICKNESS 3 -#define NAGBAR_BUTTON_GAP 20 -#define NAGBAR_BUTTON_GAP_CLOSE 15 -#define NAGBAR_BUTTON_MARGIN_RIGHT 2 -#define NAGBAR_BUTTON_PADDING 3 - -#define NAGBAR_MAX_HEIGHT 500 - -enum sway_nagbar_type { - NAGBAR_ERROR, - NAGBAR_WARNING, -}; - -enum sway_nagbar_action_type { - NAGBAR_ACTION_DISMISS, - NAGBAR_ACTION_EXPAND, - NAGBAR_ACTION_COMMAND, -}; - -struct sway_nagbar_colors { - uint32_t button_background; - uint32_t background; - uint32_t text; - uint32_t border; - uint32_t border_bottom; -}; - -struct sway_nagbar_pointer { - struct wl_pointer *pointer; - struct wl_cursor_theme *cursor_theme; - struct wl_cursor_image *cursor_image; - struct wl_surface *cursor_surface; - int x; - int y; -}; - -struct sway_nagbar_output { - char *name; - struct wl_output *wl_output; - uint32_t wl_name; -}; - -struct sway_nagbar_button { - char *text; - enum sway_nagbar_action_type type; - char *action; - int x; - int y; - int width; - int height; -}; - -struct sway_nagbar_details { - bool visible; - char *message; - - int x; - int y; - int width; - int height; - - int offset; - int visible_lines; - int total_lines; - struct sway_nagbar_button button_up; - struct sway_nagbar_button button_down; -}; - -struct sway_nagbar { - bool run_display; - int querying_outputs; - - struct wl_display *display; - struct wl_compositor *compositor; - struct wl_seat *seat; - struct wl_shm *shm; - struct sway_nagbar_pointer pointer; - struct zxdg_output_manager_v1 *xdg_output_manager; - struct sway_nagbar_output output; - struct zwlr_layer_shell_v1 *layer_shell; - struct zwlr_layer_surface_v1 *layer_surface; - struct wl_surface *surface; - - uint32_t width; - uint32_t height; - int32_t scale; - struct pool_buffer buffers[2]; - struct pool_buffer *current_buffer; - - enum sway_nagbar_type type; - struct sway_nagbar_colors colors; - uint32_t anchors; - char *message; - char *font; - list_t *buttons; - struct sway_nagbar_details details; -}; - -void nagbar_setup(struct sway_nagbar *nagbar); - -void nagbar_run(struct sway_nagbar *nagbar); - -void nagbar_destroy(struct sway_nagbar *nagbar); - -#endif diff --git a/include/swaynagbar/render.h b/include/swaynagbar/render.h deleted file mode 100644 index d9429f7f..00000000 --- a/include/swaynagbar/render.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef _SWAY_NAGBAR_RENDER_H -#define _SWAY_NAGBAR_RENDER_H - -void render_frame(struct sway_nagbar *nagbar); - -#endif diff --git a/meson.build b/meson.build index 26d36e06..c6501c39 100644 --- a/meson.build +++ b/meson.build @@ -82,7 +82,7 @@ if scdoc.found() 'swaylock/swaylock.1.scd', 'swaymsg/swaymsg.1.scd', 'swayidle/swayidle.1.scd', - 'swaynagbar/swaynagbar.1.scd', + 'swaynag/swaynag.1.scd', ] foreach filename : man_files topic = filename.split('.')[-3].split('/')[-1] @@ -131,7 +131,7 @@ subdir('swaybg') subdir('swaybar') subdir('swaylock') subdir('swayidle') -subdir('swaynagbar') +subdir('swaynag') config = configuration_data() config.set('sysconfdir', join_paths(prefix, sysconfdir)) diff --git a/sway/desktop/layer_shell.c b/sway/desktop/layer_shell.c index 71a0163c..a2935883 100644 --- a/sway/desktop/layer_shell.c +++ b/sway/desktop/layer_shell.c @@ -214,9 +214,6 @@ void arrange_layers(struct sway_output *output) { wl_list_for_each(seat, &input_manager->seats, link) { seat_set_focus_layer(seat, topmost ? topmost->layer_surface : NULL); } - - arrange_windows(output->swayc); - transaction_commit_dirty(); } static void handle_output_destroy(struct wl_listener *listener, void *data) { @@ -250,6 +247,9 @@ static void handle_surface_commit(struct wl_listener *listener, void *data) { output_damage_surface(output, layer->geo.x, layer->geo.y, layer_surface->surface, false); } + + arrange_windows(output->swayc); + transaction_commit_dirty(); } static void unmap(struct sway_layer_surface *sway_layer) { @@ -287,6 +287,8 @@ static void handle_destroy(struct wl_listener *listener, void *data) { struct sway_output *output = sway_layer->layer_surface->output->data; if (output != NULL && output->swayc != NULL) { arrange_layers(output); + arrange_windows(output->swayc); + transaction_commit_dirty(); } wl_list_remove(&sway_layer->output_destroy.link); sway_layer->layer_surface->output = NULL; diff --git a/swaynag/main.c b/swaynag/main.c new file mode 100644 index 00000000..60560c72 --- /dev/null +++ b/swaynag/main.c @@ -0,0 +1,246 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include "log.h" +#include "list.h" +#include "readline.h" +#include "swaynag/nagbar.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static struct sway_nagbar nagbar; + +void sig_handler(int signal) { + nagbar_destroy(&nagbar); + exit(EXIT_FAILURE); +} + +void sway_terminate(int code) { + nagbar_destroy(&nagbar); + exit(code); +} + +static void set_nagbar_colors() { + if (nagbar.type == NAGBAR_ERROR) { + nagbar.colors.button_background = 0x680A0AFF; + nagbar.colors.background = 0x900000FF; + nagbar.colors.text = 0xFFFFFFFF; + nagbar.colors.border = 0xD92424FF; + nagbar.colors.border_bottom = 0x470909FF; + } else if (nagbar.type == NAGBAR_WARNING) { + nagbar.colors.button_background = 0xFFC100FF; + nagbar.colors.background = 0xFFA800FF; + nagbar.colors.text = 0x000000FF; + nagbar.colors.border = 0xAB7100FF; + nagbar.colors.border_bottom = 0xAB7100FF; + } +} + +static char *read_from_stdin() { + char *buffer = NULL; + while (!feof(stdin)) { + char *line = read_line(stdin); + if (!line) { + continue; + } + + if (!buffer) { + buffer = strdup(line); + } else { + buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); + strcat(buffer, line); + strcat(buffer, "\n"); + } + + free(line); + } + + if (buffer && buffer[strlen(buffer) - 1] == '\n') { + buffer[strlen(buffer) - 1] = '\0'; + } + + return buffer; +} + +int main(int argc, char **argv) { + int exit_code = EXIT_SUCCESS; + bool debug = false; + + memset(&nagbar, 0, sizeof(nagbar)); + nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + nagbar.type = NAGBAR_ERROR; + set_nagbar_colors(); + nagbar.font = strdup("pango:monospace 10"); + nagbar.buttons = create_list(); + + struct sway_nagbar_button *button_close = + calloc(sizeof(struct sway_nagbar_button), 1); + button_close->text = strdup("X"); + button_close->type = NAGBAR_ACTION_DISMISS; + list_add(nagbar.buttons, button_close); + + struct sway_nagbar_button *button_details = + calloc(sizeof(struct sway_nagbar_button), 1); + button_details->text = strdup("Toggle Details"); + button_details->type = NAGBAR_ACTION_EXPAND; + + static struct option opts[] = { + {"button", required_argument, NULL, 'b'}, + {"debug", no_argument, NULL, 'd'}, + {"edge", required_argument, NULL, 'e'}, + {"font", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"detailed-message", no_argument, NULL, 'l'}, + {"detailed-button", required_argument, NULL, 'L'}, + {"message", required_argument, NULL, 'm'}, + {"output", required_argument, NULL, 'o'}, + {"dismiss-button", required_argument, NULL, 's'}, + {"type", required_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {0, 0, 0, 0} + }; + + const char *usage = + "Usage: swaynag [options...]\n" + "\n" + " -b, --button Create a button with text that " + "executes action when pressed. Multiple buttons can be defined.\n" + " -d, --debug Enable debugging.\n" + " -e, --edge top|bottom Set the edge to use.\n" + " -f, --font Set the font to use.\n" + " -h, --help Show help message and quit.\n" + " -l, --detailed-message Read a detailed message from stdin.\n" + " -L, --detailed-button Set the text of the detail button.\n" + " -m, --message Set the message text.\n" + " -o, --output Set the output to use.\n" + " -s, --dismiss-button Set the dismiss button text.\n" + " -t, --type error|warning Set the message type.\n" + " -v, --version Show the version number and quit.\n"; + + while (1) { + int c = getopt_long(argc, argv, "b:de:f:hlL:m:o:s:t:v", opts, NULL); + if (c == -1) { + break; + } + switch (c) { + case 'b': // Button + if (optind >= argc) { + fprintf(stderr, "Missing action for button %s\n", optarg); + exit_code = EXIT_FAILURE; + goto cleanup; + } + struct sway_nagbar_button *button; + button = calloc(sizeof(struct sway_nagbar_button), 1); + button->text = strdup(optarg); + button->type = NAGBAR_ACTION_COMMAND; + button->action = strdup(argv[optind]); + optind++; + list_add(nagbar.buttons, button); + break; + case 'd': // Debug + debug = true; + break; + case 'e': // Edge + if (strcmp(optarg, "top") == 0) { + nagbar.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) { + nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else { + fprintf(stderr, "Invalid edge: %s\n", optarg); + exit_code = EXIT_FAILURE; + goto cleanup; + } + break; + case 'f': // Font + free(nagbar.font); + nagbar.font = strdup(optarg); + break; + case 'l': // Detailed Message + free(nagbar.details.message); + nagbar.details.message = read_from_stdin(); + nagbar.details.button_up.text = strdup("▲"); + nagbar.details.button_down.text = strdup("▼"); + break; + case 'L': // Detailed Button Text + free(button_details->text); + button_details->text = strdup(optarg); + break; + case 'm': // Message + free(nagbar.message); + nagbar.message = strdup(optarg); + break; + case 'o': // Output + free(nagbar.output.name); + nagbar.output.name = strdup(optarg); + break; + case 's': // Dismiss Button Text + free(button_close->text); + button_close->text = strdup(optarg); + break; + case 't': // Type + if (strcmp(optarg, "error") == 0) { + nagbar.type = NAGBAR_ERROR; + } else if (strcmp(optarg, "warning") == 0) { + nagbar.type = NAGBAR_WARNING; + } else { + fprintf(stderr, "Type must be either 'error' or 'warning'\n"); + exit_code = EXIT_FAILURE; + goto cleanup; + } + set_nagbar_colors(); + break; + case 'v': // Version + fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); + exit_code = EXIT_SUCCESS; + goto cleanup; + default: // Help or unknown flag + fprintf(c == 'h' ? stdout : stderr, "%s", usage); + exit_code = c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; + goto cleanup; + } + } + + wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); + + if (!nagbar.message) { + wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); + exit_code = EXIT_FAILURE; + goto cleanup; + } + + if (nagbar.details.message) { + list_add(nagbar.buttons, button_details); + } else { + free(button_details->text); + free(button_details); + } + + wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); + wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); + wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); + wlr_log(WLR_DEBUG, "Message: %s", nagbar.message); + wlr_log(WLR_DEBUG, "Font: %s", nagbar.font); + wlr_log(WLR_DEBUG, "Buttons"); + for (int i = 0; i < nagbar.buttons->length; i++) { + struct sway_nagbar_button *button = nagbar.buttons->items[i]; + wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action); + } + + signal(SIGTERM, sig_handler); + + nagbar_setup(&nagbar); + nagbar_run(&nagbar); + return exit_code; + +cleanup: + free(button_details->text); + free(button_details); + nagbar_destroy(&nagbar); + return exit_code; +} + diff --git a/swaynag/meson.build b/swaynag/meson.build new file mode 100644 index 00000000..6492e4dc --- /dev/null +++ b/swaynag/meson.build @@ -0,0 +1,20 @@ +executable( + 'swaynag', [ + 'main.c', + 'nagbar.c', + 'render.c', + ], + include_directories: [sway_inc], + dependencies: [ + cairo, + client_protos, + gdk_pixbuf, + pango, + pangocairo, + wayland_client, + wayland_cursor, + wlroots, + ], + link_with: [lib_sway_common, lib_sway_client], + install: true +) diff --git a/swaynag/nagbar.c b/swaynag/nagbar.c new file mode 100644 index 00000000..6647e8c2 --- /dev/null +++ b/swaynag/nagbar.c @@ -0,0 +1,420 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include "log.h" +#include "list.h" +#include "swaynag/nagbar.h" +#include "swaynag/render.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static void nop() { + // Intentionally left blank +} + +static bool terminal_execute(char *terminal, char *command) { + char fname[] = "/tmp/swaynagXXXXXX"; + FILE *tmp= fdopen(mkstemp(fname), "w"); + if (!tmp) { + wlr_log(WLR_ERROR, "Failed to create temp script"); + return false; + } + wlr_log(WLR_DEBUG, "Created temp script: %s", fname); + fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); + fclose(tmp); + chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); + char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; + sprintf(cmd, "%s -e %s", terminal, fname); + execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); + return true; +} + +static void nagbar_button_execute(struct sway_nagbar *nagbar, + struct sway_nagbar_button *button) { + wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); + if (button->type == NAGBAR_ACTION_DISMISS) { + nagbar->run_display = false; + } else if (button->type == NAGBAR_ACTION_EXPAND) { + nagbar->details.visible = !nagbar->details.visible; + render_frame(nagbar); + } else { + if (fork() == 0) { + // Child process. Will be used to prevent zombie processes + setsid(); + if (fork() == 0) { + // Child of the child. Will be reparented to the init process + char *terminal = getenv("TERMINAL"); + if (terminal && strlen(terminal)) { + wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); + if (!terminal_execute(terminal, button->action)) { + nagbar_destroy(nagbar); + exit(EXIT_FAILURE); + } + } else { + wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); + execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); + } + } + exit(EXIT_SUCCESS); + } + } + wait(0); +} + +static void layer_surface_configure(void *data, + struct zwlr_layer_surface_v1 *surface, + uint32_t serial, uint32_t width, uint32_t height) { + struct sway_nagbar *nagbar = data; + nagbar->width = width; + nagbar->height = height; + zwlr_layer_surface_v1_ack_configure(surface, serial); + render_frame(nagbar); +} + +static void layer_surface_closed(void *data, + struct zwlr_layer_surface_v1 *surface) { + struct sway_nagbar *nagbar = data; + nagbar_destroy(nagbar); +} + +static struct zwlr_layer_surface_v1_listener layer_surface_listener = { + .configure = layer_surface_configure, + .closed = layer_surface_closed, +}; + +static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, struct wl_surface *surface, + wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct sway_nagbar *nagbar = data; + struct sway_nagbar_pointer *pointer = &nagbar->pointer; + wl_surface_set_buffer_scale(pointer->cursor_surface, nagbar->scale); + wl_surface_attach(pointer->cursor_surface, + wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); + wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, + pointer->cursor_image->hotspot_x / nagbar->scale, + pointer->cursor_image->hotspot_y / nagbar->scale); + wl_surface_commit(pointer->cursor_surface); +} + +static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, + uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { + struct sway_nagbar *nagbar = data; + nagbar->pointer.x = wl_fixed_to_int(surface_x); + nagbar->pointer.y = wl_fixed_to_int(surface_y); +} + +static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { + struct sway_nagbar *nagbar = data; + + if (state != WL_POINTER_BUTTON_STATE_PRESSED) { + return; + } + + double x = nagbar->pointer.x * nagbar->scale; + double y = nagbar->pointer.y * nagbar->scale; + for (int i = 0; i < nagbar->buttons->length; i++) { + struct sway_nagbar_button *nagbutton = nagbar->buttons->items[i]; + if (x >= nagbutton->x + && y >= nagbutton->y + && x < nagbutton->x + nagbutton->width + && y < nagbutton->y + nagbutton->height) { + nagbar_button_execute(nagbar, nagbutton); + return; + } + } + + if (nagbar->details.visible && + nagbar->details.total_lines != nagbar->details.visible_lines) { + struct sway_nagbar_button button_up = nagbar->details.button_up; + if (x >= button_up.x + && y >= button_up.y + && x < button_up.x + button_up.width + && y < button_up.y + button_up.height + && nagbar->details.offset > 0) { + nagbar->details.offset--; + render_frame(nagbar); + return; + } + + struct sway_nagbar_button button_down = nagbar->details.button_down; + int bot = nagbar->details.total_lines - nagbar->details.visible_lines; + if (x >= button_down.x + && y >= button_down.y + && x < button_down.x + button_down.width + && y < button_down.y + button_down.height + && nagbar->details.offset < bot) { + nagbar->details.offset++; + render_frame(nagbar); + return; + } + } +} + +static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) { + struct sway_nagbar *nagbar = data; + if (!nagbar->details.visible + || nagbar->pointer.x < nagbar->details.x + || nagbar->pointer.y < nagbar->details.y + || nagbar->pointer.x >= nagbar->details.x + nagbar->details.width + || nagbar->pointer.y >= nagbar->details.y + nagbar->details.height + || nagbar->details.total_lines == nagbar->details.visible_lines) { + return; + } + + int direction = wl_fixed_to_int(value); + int bot = nagbar->details.total_lines - nagbar->details.visible_lines; + if (direction < 0 && nagbar->details.offset > 0) { + nagbar->details.offset--; + } else if (direction > 0 && nagbar->details.offset < bot) { + nagbar->details.offset++; + } + + render_frame(nagbar); +} + +static struct wl_pointer_listener pointer_listener = { + .enter = wl_pointer_enter, + .leave = nop, + .motion = wl_pointer_motion, + .button = wl_pointer_button, + .axis = wl_pointer_axis, + .frame = nop, + .axis_source = nop, + .axis_stop = nop, + .axis_discrete = nop, +}; + +static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, + enum wl_seat_capability caps) { + struct sway_nagbar *nagbar = data; + if ((caps & WL_SEAT_CAPABILITY_POINTER)) { + nagbar->pointer.pointer = wl_seat_get_pointer(wl_seat); + wl_pointer_add_listener(nagbar->pointer.pointer, &pointer_listener, + nagbar); + } +} + +const struct wl_seat_listener seat_listener = { + .capabilities = seat_handle_capabilities, + .name = nop, +}; + +static void output_scale(void *data, struct wl_output *output, + int32_t factor) { + struct sway_nagbar *nagbar = data; + nagbar->scale = factor; + render_frame(nagbar); +} + +static struct wl_output_listener output_listener = { + .geometry = nop, + .mode = nop, + .done = nop, + .scale = output_scale, +}; + +struct output_state { + struct wl_output *wl_output; + uint32_t wl_name; + struct zxdg_output_v1 *xdg_output; + struct sway_nagbar *nagbar; +}; + +static void xdg_output_handle_name(void *data, + struct zxdg_output_v1 *xdg_output, const char *name) { + struct output_state *state = data; + char *outname = state->nagbar->output.name; + wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); + if ((!outname && !state->nagbar->output.wl_output) + || (name && outname && strcmp(name, outname) == 0)) { + wlr_log(WLR_DEBUG, "Using output %s", name); + state->nagbar->output.wl_output = state->wl_output; + state->nagbar->output.wl_name = state->wl_name; + wl_output_add_listener(state->nagbar->output.wl_output, + &output_listener, state->nagbar); + wl_display_roundtrip(state->nagbar->display); + zxdg_output_v1_destroy(state->xdg_output); + } else { + zxdg_output_v1_destroy(state->xdg_output); + wl_output_destroy(state->wl_output); + } + state->nagbar->querying_outputs--; + free(state); +} + +static struct zxdg_output_v1_listener xdg_output_listener = { + .logical_position = nop, + .logical_size = nop, + .done = nop, + .name = xdg_output_handle_name, + .description = nop, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + struct sway_nagbar *nagbar = data; + if (strcmp(interface, wl_compositor_interface.name) == 0) { + nagbar->compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 3); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + nagbar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); + wl_seat_add_listener(nagbar->seat, &seat_listener, nagbar); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + nagbar->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, wl_output_interface.name) == 0) { + if (!nagbar->output.wl_output && nagbar->xdg_output_manager) { + nagbar->querying_outputs++; + struct output_state *state = + calloc(1, sizeof(struct output_state)); + state->nagbar = nagbar; + state->wl_output = wl_registry_bind(registry, name, + &wl_output_interface, 3); + state->wl_name = name; + state->xdg_output = zxdg_output_manager_v1_get_xdg_output( + nagbar->xdg_output_manager, state->wl_output); + zxdg_output_v1_add_listener(state->xdg_output, + &xdg_output_listener, state); + } else if (!nagbar->output.wl_output && !nagbar->xdg_output_manager) { + wlr_log(WLR_ERROR, "Warning: zxdg_output_manager_v1 not supported." + " Falling back to first detected output"); + nagbar->output.wl_output = wl_registry_bind(registry, name, + &wl_output_interface, 3); + nagbar->output.wl_name = name; + wl_output_add_listener(nagbar->output.wl_output, + &output_listener, nagbar); + } + } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { + nagbar->layer_shell = wl_registry_bind( + registry, name, &zwlr_layer_shell_v1_interface, 1); + } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 + && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { + nagbar->xdg_output_manager = wl_registry_bind(registry, name, + &zxdg_output_manager_v1_interface, + ZXDG_OUTPUT_V1_NAME_SINCE_VERSION); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + struct sway_nagbar *nagbar = data; + if (nagbar->output.wl_name == name) { + nagbar->run_display = false; + } +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +void nagbar_setup(struct sway_nagbar *nagbar) { + nagbar->display = wl_display_connect(NULL); + assert(nagbar->display); + + nagbar->scale = 1; + + struct wl_registry *registry = wl_display_get_registry(nagbar->display); + wl_registry_add_listener(registry, ®istry_listener, nagbar); + wl_display_roundtrip(nagbar->display); + assert(nagbar->compositor && nagbar->layer_shell && nagbar->shm); + + while (nagbar->querying_outputs > 0) { + wl_display_roundtrip(nagbar->display); + } + + if (!nagbar->output.wl_output) { + if (nagbar->output.name) { + wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name); + } else { + wlr_log(WLR_ERROR, "No outputs detected"); + } + nagbar_destroy(nagbar); + exit(EXIT_FAILURE); + } + + struct sway_nagbar_pointer *pointer = &nagbar->pointer; + int scale = nagbar->scale < 1 ? 1 : nagbar->scale; + pointer->cursor_theme = wl_cursor_theme_load( + NULL, 24 * scale, nagbar->shm); + assert(pointer->cursor_theme); + struct wl_cursor *cursor = + wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); + assert(cursor); + pointer->cursor_image = cursor->images[0]; + pointer->cursor_surface = wl_compositor_create_surface(nagbar->compositor); + assert(pointer->cursor_surface); + + nagbar->surface = wl_compositor_create_surface(nagbar->compositor); + assert(nagbar->surface); + nagbar->layer_surface = zwlr_layer_shell_v1_get_layer_surface( + nagbar->layer_shell, nagbar->surface, nagbar->output.wl_output, + ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); + assert(nagbar->layer_surface); + zwlr_layer_surface_v1_add_listener(nagbar->layer_surface, + &layer_surface_listener, nagbar); + zwlr_layer_surface_v1_set_anchor(nagbar->layer_surface, nagbar->anchors); + + wl_registry_destroy(registry); +} + +void nagbar_run(struct sway_nagbar *nagbar) { + nagbar->run_display = true; + render_frame(nagbar); + while (nagbar->run_display && wl_display_dispatch(nagbar->display) != -1) { + // This is intentionally left blank + } +} + +void nagbar_destroy(struct sway_nagbar *nagbar) { + nagbar->run_display = false; + + free(nagbar->message); + free(nagbar->font); + while (nagbar->buttons->length) { + struct sway_nagbar_button *button = nagbar->buttons->items[0]; + list_del(nagbar->buttons, 0); + free(button->text); + free(button->action); + free(button); + } + list_free(nagbar->buttons); + free(nagbar->details.message); + free(nagbar->details.button_up.text); + free(nagbar->details.button_down.text); + + if (nagbar->layer_surface) { + zwlr_layer_surface_v1_destroy(nagbar->layer_surface); + } + + if (nagbar->surface) { + wl_surface_destroy(nagbar->surface); + } + + if (nagbar->output.wl_output) { + wl_output_destroy(nagbar->output.wl_output); + } + + if (&nagbar->buffers[0]) { + destroy_buffer(&nagbar->buffers[0]); + } + + if (&nagbar->buffers[1]) { + destroy_buffer(&nagbar->buffers[1]); + } + + if (nagbar->compositor) { + wl_compositor_destroy(nagbar->compositor); + } + + if (nagbar->shm) { + wl_shm_destroy(nagbar->shm); + } + + if (nagbar->display) { + wl_display_disconnect(nagbar->display); + } +} diff --git a/swaynag/render.c b/swaynag/render.c new file mode 100644 index 00000000..150ae3f2 --- /dev/null +++ b/swaynag/render.c @@ -0,0 +1,300 @@ +#include +#include "cairo.h" +#include "log.h" +#include "pango.h" +#include "pool-buffer.h" +#include "swaynag/nagbar.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { + uint32_t height = nagbar->height * nagbar->scale; + height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + + int text_width, text_height; + get_text_size(cairo, nagbar->font, &text_width, &text_height, + nagbar->scale, true, "%s", nagbar->message); + + int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; + + uint32_t ideal_height = text_height + padding * 2; + uint32_t ideal_surface_height = ideal_height / nagbar->scale; + if (nagbar->height < ideal_surface_height) { + return ideal_surface_height; + } + + cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); + pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", + nagbar->message); + + return ideal_height; +} + +static void render_details_scroll_button(cairo_t *cairo, + struct sway_nagbar *nagbar, struct sway_nagbar_button *button) { + int text_width, text_height; + get_text_size(cairo, nagbar->font, &text_width, &text_height, + nagbar->scale, true, "%s", button->text); + + int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + + cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_rectangle(cairo, button->x, button->y, + button->width, button->height); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.button_background); + cairo_rectangle(cairo, button->x + border, button->y + border, + button->width - (border * 2), button->height - (border * 2)); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_move_to(cairo, button->x + border + padding, + button->y + border + (button->height - text_height) / 2); + pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); +} + +static int get_detailed_scroll_button_width(cairo_t *cairo, + struct sway_nagbar *nagbar) { + int up_width, down_width, temp_height; + get_text_size(cairo, nagbar->font, &up_width, &temp_height, + nagbar->scale, true, "%s", nagbar->details.button_up.text); + get_text_size(cairo, nagbar->font, &down_width, &temp_height, + nagbar->scale, true, "%s", nagbar->details.button_down.text); + + int text_width = up_width > down_width ? up_width : down_width; + int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + + return text_width + border * 2 + padding * 2; +} + +static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, + uint32_t y) { + uint32_t width = nagbar->width * nagbar->scale; + uint32_t height = nagbar->height * nagbar->scale; + height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + + int border = NAGBAR_DETAILS_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; + int decor = padding + border; + + nagbar->details.x = decor; + nagbar->details.y = y + decor; + nagbar->details.width = width - decor * 2; + + PangoLayout *layout = get_pango_layout(cairo, nagbar->font, + nagbar->details.message, nagbar->scale, true); + pango_layout_set_width(layout, + (nagbar->details.width - padding * 2) * PANGO_SCALE); + pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); + pango_layout_set_single_paragraph_mode(layout, false); + pango_cairo_update_layout(cairo, layout); + nagbar->details.total_lines = pango_layout_get_line_count(layout); + + PangoLayoutLine *line; + line = pango_layout_get_line_readonly(layout, nagbar->details.offset); + gint offset = line->start_index; + const char *text = pango_layout_get_text(layout); + pango_layout_set_text(layout, text + offset, strlen(text) - offset); + + int text_width, text_height; + pango_cairo_update_layout(cairo, layout); + pango_layout_get_pixel_size(layout, &text_width, &text_height); + + bool show_buttons = nagbar->details.offset > 0; + int button_width = get_detailed_scroll_button_width(cairo, nagbar); + if (show_buttons) { + nagbar->details.width -= button_width; + pango_layout_set_width(layout, + (nagbar->details.width - padding * 2) * PANGO_SCALE); + } + + uint32_t ideal_height; + do { + ideal_height = nagbar->details.y + text_height + decor + padding * 2; + if (ideal_height > NAGBAR_MAX_HEIGHT) { + ideal_height = NAGBAR_MAX_HEIGHT; + + if (!show_buttons) { + show_buttons = true; + nagbar->details.width -= button_width; + pango_layout_set_width(layout, + (nagbar->details.width - padding * 2) * PANGO_SCALE); + } + } + + nagbar->details.height = ideal_height - nagbar->details.y - decor; + pango_layout_set_height(layout, + (nagbar->details.height - padding * 2) * PANGO_SCALE); + pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); + pango_cairo_update_layout(cairo, layout); + pango_layout_get_pixel_size(layout, &text_width, &text_height); + } while (text_height != (nagbar->details.height - padding * 2)); + + nagbar->details.visible_lines = pango_layout_get_line_count(layout); + + if (show_buttons) { + nagbar->details.button_up.x = + nagbar->details.x + nagbar->details.width; + nagbar->details.button_up.y = nagbar->details.y; + nagbar->details.button_up.width = button_width; + nagbar->details.button_up.height = nagbar->details.height / 2; + render_details_scroll_button(cairo, nagbar, + &nagbar->details.button_up); + + nagbar->details.button_down.x = + nagbar->details.x + nagbar->details.width; + nagbar->details.button_down.y = + nagbar->details.button_up.y + nagbar->details.button_up.height; + nagbar->details.button_down.width = button_width; + nagbar->details.button_down.height = nagbar->details.height / 2; + render_details_scroll_button(cairo, nagbar, + &nagbar->details.button_down); + } + + cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_rectangle(cairo, nagbar->details.x, nagbar->details.y, + nagbar->details.width, nagbar->details.height); + cairo_fill(cairo); + + cairo_move_to(cairo, nagbar->details.x + padding, + nagbar->details.y + padding); + cairo_set_source_u32(cairo, nagbar->colors.text); + pango_cairo_show_layout(cairo, layout); + g_object_unref(layout); + + return ideal_height; +} + +static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, + int button_index, int *x) { + uint32_t height = nagbar->height * nagbar->scale; + height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + struct sway_nagbar_button *button = nagbar->buttons->items[button_index]; + + int text_width, text_height; + get_text_size(cairo, nagbar->font, &text_width, &text_height, + nagbar->scale, true, "%s", button->text); + + int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; + int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + + uint32_t ideal_height = text_height + padding * 2 + border * 2; + uint32_t ideal_surface_height = ideal_height / nagbar->scale; + if (nagbar->height < ideal_surface_height) { + return ideal_surface_height; + } + + button->x = *x - border - text_width - padding * 2; + button->y = (int)(ideal_height - text_height) / 2 - padding; + button->width = text_width + padding * 2; + button->height = text_height + padding * 2; + + cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_rectangle(cairo, button->x - border, button->y - border, + button->width + border * 2, button->height + border * 2); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.button_background); + cairo_rectangle(cairo, button->x, button->y, + button->width, button->height); + cairo_fill(cairo); + + cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_move_to(cairo, button->x + padding, button->y + padding); + pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); + + *x = button->x - border; + + return ideal_height; +} + +static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { + uint32_t max_height = 0; + + cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); + cairo_set_source_u32(cairo, nagbar->colors.background); + cairo_paint(cairo); + + uint32_t h = render_message(cairo, nagbar); + max_height = h > max_height ? h : max_height; + + int x = (nagbar->width - NAGBAR_BUTTON_MARGIN_RIGHT) * nagbar->scale; + for (int i = 0; i < nagbar->buttons->length; i++) { + h = render_button(cairo, nagbar, i, &x); + max_height = h > max_height ? h : max_height; + x -= NAGBAR_BUTTON_GAP * nagbar->scale; + if (i == 0) { + x -= NAGBAR_BUTTON_GAP_CLOSE * nagbar->scale; + } + } + + if (nagbar->details.visible) { + h = render_detailed(cairo, nagbar, max_height); + max_height = h > max_height ? h : max_height; + } + + int border = NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + if (max_height > nagbar->height) { + max_height += border; + } + cairo_set_source_u32(cairo, nagbar->colors.border_bottom); + cairo_rectangle(cairo, 0, nagbar->height * nagbar->scale - border, + nagbar->width * nagbar->scale, border); + cairo_fill(cairo); + + return max_height; +} + +void render_frame(struct sway_nagbar *nagbar) { + if (!nagbar->run_display) { + return; + } + + cairo_surface_t *recorder = cairo_recording_surface_create( + CAIRO_CONTENT_COLOR_ALPHA, NULL); + cairo_t *cairo = cairo_create(recorder); + cairo_save(cairo); + cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); + cairo_paint(cairo); + cairo_restore(cairo); + uint32_t height = render_to_cairo(cairo, nagbar); + if (height != nagbar->height) { + zwlr_layer_surface_v1_set_size(nagbar->layer_surface, 0, height); + zwlr_layer_surface_v1_set_exclusive_zone(nagbar->layer_surface, + height); + wl_surface_commit(nagbar->surface); + wl_display_roundtrip(nagbar->display); + } else { + nagbar->current_buffer = get_next_buffer(nagbar->shm, + nagbar->buffers, + nagbar->width * nagbar->scale, + nagbar->height * nagbar->scale); + if (!nagbar->current_buffer) { + wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame."); + goto cleanup; + } + + cairo_t *shm = nagbar->current_buffer->cairo; + cairo_save(shm); + cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); + cairo_paint(shm); + cairo_restore(shm); + cairo_set_source_surface(shm, recorder, 0.0, 0.0); + cairo_paint(shm); + + wl_surface_set_buffer_scale(nagbar->surface, nagbar->scale); + wl_surface_attach(nagbar->surface, + nagbar->current_buffer->buffer, 0, 0); + wl_surface_damage(nagbar->surface, 0, 0, + nagbar->width, nagbar->height); + wl_surface_commit(nagbar->surface); + wl_display_roundtrip(nagbar->display); + } + +cleanup: + cairo_surface_destroy(recorder); + cairo_destroy(cairo); +} diff --git a/swaynag/swaynag.1.scd b/swaynag/swaynag.1.scd new file mode 100644 index 00000000..879aaf2e --- /dev/null +++ b/swaynag/swaynag.1.scd @@ -0,0 +1,52 @@ +swaynagbar(1) + +# NAME + +swaynag - Show a warning or error message with buttons + +# SYNOPSIS + +_swaynag_ [options...] + +# OPTIONS +*-b, --button* + Create a button with the text _text_ that executes _action_ when pressed. + Multiple buttons can be defined by providing the flag multiple times. + +*-d, --debug* + Enable debugging. + +*-e, --edge top|bottom* + Set the edge to use. + +*-f, --font * + Set the font to use. + +*-h, --help* + Show help message and quit. + +*-l, --detailed-message* + Read a detailed message from stdin. A button to toggle details will be + added. Details are shown in a scrollable multi-line text area. + +*-L, --detailed-button * + Set the text for the button that toggles details. This has no effect if + there is not a detailed message. The default is _Toggle Details_. + +*-m, --message * + 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 + +*-s, --dismiss-button * + Sets the text for the dismiss nagbar button. The default is _X_. + +*-t, --type error|warning* + Set the message type. + +*-v, --version + Show the version number and quit. + diff --git a/swaynagbar/main.c b/swaynagbar/main.c deleted file mode 100644 index 389118c6..00000000 --- a/swaynagbar/main.c +++ /dev/null @@ -1,250 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include "log.h" -#include "list.h" -#include "readline.h" -#include "swaynagbar/nagbar.h" -#include "wlr-layer-shell-unstable-v1-client-protocol.h" - -static struct sway_nagbar nagbar; - -void sig_handler(int signal) { - nagbar_destroy(&nagbar); - exit(EXIT_FAILURE); -} - -void sway_terminate(int code) { - nagbar_destroy(&nagbar); - exit(code); -} - -static void set_nagbar_colors() { - if (nagbar.type == NAGBAR_ERROR) { - nagbar.colors.button_background = 0x680A0AFF; - nagbar.colors.background = 0x900000FF; - nagbar.colors.text = 0xFFFFFFFF; - nagbar.colors.border = 0xD92424FF; - nagbar.colors.border_bottom = 0x470909FF; - } else if (nagbar.type == NAGBAR_WARNING) { - nagbar.colors.button_background = 0xFFC100FF; - nagbar.colors.background = 0xFFA800FF; - nagbar.colors.text = 0x000000FF; - nagbar.colors.border = 0xAB7100FF; - nagbar.colors.border_bottom = 0xAB7100FF; - } -} - -static char *read_from_stdin() { - char *buffer = NULL; - while (!feof(stdin)) { - char *line = read_line(stdin); - if (!line) { - continue; - } - - if (!buffer) { - buffer = strdup(line); - } else { - buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); - strcat(buffer, line); - strcat(buffer, "\n"); - } - - free(line); - } - - if (buffer && buffer[strlen(buffer) - 1] == '\n') { - buffer[strlen(buffer) - 1] = '\0'; - } - - return buffer; -} - -int main(int argc, char **argv) { - int exit_code = EXIT_SUCCESS; - bool debug = false; - - memset(&nagbar, 0, sizeof(nagbar)); - nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - nagbar.type = NAGBAR_ERROR; - set_nagbar_colors(); - nagbar.font = strdup("pango:monospace 8"); - nagbar.buttons = create_list(); - - struct sway_nagbar_button *button_close = - calloc(sizeof(struct sway_nagbar_button), 1); - button_close->text = strdup("X"); - button_close->type = NAGBAR_ACTION_DISMISS; - list_add(nagbar.buttons, button_close); - - struct sway_nagbar_button *button_details = - calloc(sizeof(struct sway_nagbar_button), 1); - button_details->text = strdup("Toggle Details"); - button_details->type = NAGBAR_ACTION_EXPAND; - - static struct option opts[] = { - {"button", required_argument, NULL, 'b'}, - {"debug", no_argument, NULL, 'd'}, - {"edge", required_argument, NULL, 'e'}, - {"font", required_argument, NULL, 'f'}, - {"help", no_argument, NULL, 'h'}, - {"detailed-message", required_argument, NULL, 'l'}, - {"detailed-button", required_argument, NULL, 'L'}, - {"message", required_argument, NULL, 'm'}, - {"output", required_argument, NULL, 'o'}, - {"dismiss-button", required_argument, NULL, 's'}, - {"type", required_argument, NULL, 't'}, - {"version", no_argument, NULL, 'v'}, - {0, 0, 0, 0} - }; - - const char *usage = - "Usage: swaynagbar [options...]\n" - "\n" - " -b, --button Create a button with text that " - "executes action when pressed. Multiple buttons can be defined.\n" - " -d, --debug Enable debugging.\n" - " -e, --edge top|bottom Set the edge to use.\n" - " -f, --font Set the font to use.\n" - " -h, --help Show help message and quit.\n" - " -l, --detailed-message Set a detailed message.\n" - " -L, --detailed-button Set the text of the detail button.\n" - " -m, --message Set the message text.\n" - " -o, --output Set the output to use.\n" - " -s, --dismiss-button Set the dismiss button text.\n" - " -t, --type error|warning Set the message type.\n" - " -v, --version Show the version number and quit.\n"; - - while (1) { - int c = getopt_long(argc, argv, "b:de:f:hl:L:m:o:s:t:v", opts, NULL); - if (c == -1) { - break; - } - switch (c) { - case 'b': // Button - if (optind >= argc) { - fprintf(stderr, "Missing action for button %s\n", optarg); - exit_code = EXIT_FAILURE; - goto cleanup; - } - struct sway_nagbar_button *button; - button = calloc(sizeof(struct sway_nagbar_button), 1); - button->text = strdup(optarg); - button->type = NAGBAR_ACTION_COMMAND; - button->action = strdup(argv[optind]); - optind++; - list_add(nagbar.buttons, button); - break; - case 'd': // Debug - debug = true; - break; - case 'e': // Edge - if (strcmp(optarg, "top") == 0) { - nagbar.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) { - nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - } else { - fprintf(stderr, "Invalid edge: %s\n", optarg); - exit_code = EXIT_FAILURE; - goto cleanup; - } - break; - case 'f': // Font - free(nagbar.font); - nagbar.font = strdup(optarg); - break; - case 'l': // Detailed Message - free(nagbar.details.message); - if (strcmp(optarg, "-") == 0) { - nagbar.details.message = read_from_stdin(); - } else { - nagbar.details.message = strdup(optarg); - } - nagbar.details.button_up.text = strdup("▲"); - nagbar.details.button_down.text = strdup("▼"); - break; - case 'L': // Detailed Button Text - free(button_details->text); - button_details->text = strdup(optarg); - break; - case 'm': // Message - free(nagbar.message); - nagbar.message = strdup(optarg); - break; - case 'o': // Output - free(nagbar.output.name); - nagbar.output.name = strdup(optarg); - break; - case 's': // Dismiss Button Text - free(button_close->text); - button_close->text = strdup(optarg); - break; - case 't': // Type - if (strcmp(optarg, "error") == 0) { - nagbar.type = NAGBAR_ERROR; - } else if (strcmp(optarg, "warning") == 0) { - nagbar.type = NAGBAR_WARNING; - } else { - fprintf(stderr, "Type must be either 'error' or 'warning'\n"); - exit_code = EXIT_FAILURE; - goto cleanup; - } - set_nagbar_colors(); - break; - case 'v': // Version - fprintf(stdout, "sway version " SWAY_VERSION "\n"); - exit_code = EXIT_SUCCESS; - goto cleanup; - default: // Help or unknown flag - fprintf(c == 'h' ? stdout : stderr, "%s", usage); - exit_code = c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; - goto cleanup; - } - } - - wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); - - if (!nagbar.message) { - wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); - exit_code = EXIT_FAILURE; - goto cleanup; - } - - if (nagbar.details.message) { - list_add(nagbar.buttons, button_details); - } else { - free(button_details->text); - free(button_details); - } - - wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); - wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); - wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); - wlr_log(WLR_DEBUG, "Message: %s", nagbar.message); - wlr_log(WLR_DEBUG, "Font: %s", nagbar.font); - wlr_log(WLR_DEBUG, "Buttons"); - for (int i = 0; i < nagbar.buttons->length; i++) { - struct sway_nagbar_button *button = nagbar.buttons->items[i]; - wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action); - } - - signal(SIGTERM, sig_handler); - - nagbar_setup(&nagbar); - nagbar_run(&nagbar); - return exit_code; - -cleanup: - free(button_details->text); - free(button_details); - nagbar_destroy(&nagbar); - return exit_code; -} - diff --git a/swaynagbar/meson.build b/swaynagbar/meson.build deleted file mode 100644 index 254462f2..00000000 --- a/swaynagbar/meson.build +++ /dev/null @@ -1,20 +0,0 @@ -executable( - 'swaynagbar', [ - 'main.c', - 'nagbar.c', - 'render.c', - ], - include_directories: [sway_inc], - dependencies: [ - cairo, - client_protos, - gdk_pixbuf, - pango, - pangocairo, - wayland_client, - wayland_cursor, - wlroots, - ], - link_with: [lib_sway_common, lib_sway_client], - install: true -) diff --git a/swaynagbar/nagbar.c b/swaynagbar/nagbar.c deleted file mode 100644 index 9e8bbb4b..00000000 --- a/swaynagbar/nagbar.c +++ /dev/null @@ -1,420 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include "log.h" -#include "list.h" -#include "swaynagbar/nagbar.h" -#include "swaynagbar/render.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/swaynagbarXXXXXX"; - FILE *tmp= fdopen(mkstemp(fname), "w"); - if (!tmp) { - wlr_log(WLR_ERROR, "Failed to create temp script"); - return false; - } - wlr_log(WLR_DEBUG, "Created temp script: %s", fname); - fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); - fclose(tmp); - chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); - char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; - sprintf(cmd, "%s -e %s", terminal, fname); - execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); - return true; -} - -static void nagbar_button_execute(struct sway_nagbar *nagbar, - struct sway_nagbar_button *button) { - wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); - if (button->type == NAGBAR_ACTION_DISMISS) { - nagbar->run_display = false; - } else if (button->type == NAGBAR_ACTION_EXPAND) { - nagbar->details.visible = !nagbar->details.visible; - render_frame(nagbar); - } else { - if (fork() == 0) { - // Child process. Will be used to prevent zombie processes - setsid(); - if (fork() == 0) { - // Child of the child. Will be reparented to the init process - char *terminal = getenv("TERMINAL"); - if (terminal && strlen(terminal)) { - wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); - if (!terminal_execute(terminal, button->action)) { - nagbar_destroy(nagbar); - exit(EXIT_FAILURE); - } - } else { - wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); - execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); - } - } - exit(EXIT_SUCCESS); - } - } - wait(0); -} - -static void layer_surface_configure(void *data, - struct zwlr_layer_surface_v1 *surface, - uint32_t serial, uint32_t width, uint32_t height) { - struct sway_nagbar *nagbar = data; - nagbar->width = width; - nagbar->height = height; - zwlr_layer_surface_v1_ack_configure(surface, serial); - render_frame(nagbar); -} - -static void layer_surface_closed(void *data, - struct zwlr_layer_surface_v1 *surface) { - struct sway_nagbar *nagbar = data; - nagbar_destroy(nagbar); -} - -static struct zwlr_layer_surface_v1_listener layer_surface_listener = { - .configure = layer_surface_configure, - .closed = layer_surface_closed, -}; - -static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, - uint32_t serial, struct wl_surface *surface, - wl_fixed_t surface_x, wl_fixed_t surface_y) { - struct sway_nagbar *nagbar = data; - struct sway_nagbar_pointer *pointer = &nagbar->pointer; - wl_surface_set_buffer_scale(pointer->cursor_surface, nagbar->scale); - wl_surface_attach(pointer->cursor_surface, - wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); - wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, - pointer->cursor_image->hotspot_x / nagbar->scale, - pointer->cursor_image->hotspot_y / nagbar->scale); - wl_surface_commit(pointer->cursor_surface); -} - -static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, - uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { - struct sway_nagbar *nagbar = data; - nagbar->pointer.x = wl_fixed_to_int(surface_x); - nagbar->pointer.y = wl_fixed_to_int(surface_y); -} - -static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, - uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { - struct sway_nagbar *nagbar = data; - - if (state != WL_POINTER_BUTTON_STATE_PRESSED) { - return; - } - - double x = nagbar->pointer.x * nagbar->scale; - double y = nagbar->pointer.y * nagbar->scale; - for (int i = 0; i < nagbar->buttons->length; i++) { - struct sway_nagbar_button *nagbutton = nagbar->buttons->items[i]; - if (x >= nagbutton->x - && y >= nagbutton->y - && x < nagbutton->x + nagbutton->width - && y < nagbutton->y + nagbutton->height) { - nagbar_button_execute(nagbar, nagbutton); - return; - } - } - - if (nagbar->details.visible && - nagbar->details.total_lines != nagbar->details.visible_lines) { - struct sway_nagbar_button button_up = nagbar->details.button_up; - if (x >= button_up.x - && y >= button_up.y - && x < button_up.x + button_up.width - && y < button_up.y + button_up.height - && nagbar->details.offset > 0) { - nagbar->details.offset--; - render_frame(nagbar); - return; - } - - struct sway_nagbar_button button_down = nagbar->details.button_down; - int bot = nagbar->details.total_lines - nagbar->details.visible_lines; - if (x >= button_down.x - && y >= button_down.y - && x < button_down.x + button_down.width - && y < button_down.y + button_down.height - && nagbar->details.offset < bot) { - nagbar->details.offset++; - render_frame(nagbar); - return; - } - } -} - -static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, - uint32_t time, uint32_t axis, wl_fixed_t value) { - struct sway_nagbar *nagbar = data; - if (!nagbar->details.visible - || nagbar->pointer.x < nagbar->details.x - || nagbar->pointer.y < nagbar->details.y - || nagbar->pointer.x >= nagbar->details.x + nagbar->details.width - || nagbar->pointer.y >= nagbar->details.y + nagbar->details.height - || nagbar->details.total_lines == nagbar->details.visible_lines) { - return; - } - - int direction = wl_fixed_to_int(value); - int bot = nagbar->details.total_lines - nagbar->details.visible_lines; - if (direction < 0 && nagbar->details.offset > 0) { - nagbar->details.offset--; - } else if (direction > 0 && nagbar->details.offset < bot) { - nagbar->details.offset++; - } - - render_frame(nagbar); -} - -static struct wl_pointer_listener pointer_listener = { - .enter = wl_pointer_enter, - .leave = nop, - .motion = wl_pointer_motion, - .button = wl_pointer_button, - .axis = wl_pointer_axis, - .frame = nop, - .axis_source = nop, - .axis_stop = nop, - .axis_discrete = nop, -}; - -static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, - enum wl_seat_capability caps) { - struct sway_nagbar *nagbar = data; - if ((caps & WL_SEAT_CAPABILITY_POINTER)) { - nagbar->pointer.pointer = wl_seat_get_pointer(wl_seat); - wl_pointer_add_listener(nagbar->pointer.pointer, &pointer_listener, - nagbar); - } -} - -const struct wl_seat_listener seat_listener = { - .capabilities = seat_handle_capabilities, - .name = nop, -}; - -static void output_scale(void *data, struct wl_output *output, - int32_t factor) { - struct sway_nagbar *nagbar = data; - nagbar->scale = factor; - render_frame(nagbar); -} - -static struct wl_output_listener output_listener = { - .geometry = nop, - .mode = nop, - .done = nop, - .scale = output_scale, -}; - -struct output_state { - struct wl_output *wl_output; - uint32_t wl_name; - struct zxdg_output_v1 *xdg_output; - struct sway_nagbar *nagbar; -}; - -static void xdg_output_handle_name(void *data, - struct zxdg_output_v1 *xdg_output, const char *name) { - struct output_state *state = data; - char *outname = state->nagbar->output.name; - wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); - if ((!outname && !state->nagbar->output.wl_output) - || (name && outname && strcmp(name, outname) == 0)) { - wlr_log(WLR_DEBUG, "Using output %s", name); - state->nagbar->output.wl_output = state->wl_output; - state->nagbar->output.wl_name = state->wl_name; - wl_output_add_listener(state->nagbar->output.wl_output, - &output_listener, state->nagbar); - wl_display_roundtrip(state->nagbar->display); - zxdg_output_v1_destroy(state->xdg_output); - } else { - zxdg_output_v1_destroy(state->xdg_output); - wl_output_destroy(state->wl_output); - } - state->nagbar->querying_outputs--; - free(state); -} - -static struct zxdg_output_v1_listener xdg_output_listener = { - .logical_position = nop, - .logical_size = nop, - .done = nop, - .name = xdg_output_handle_name, - .description = nop, -}; - -static void handle_global(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, uint32_t version) { - struct sway_nagbar *nagbar = data; - if (strcmp(interface, wl_compositor_interface.name) == 0) { - nagbar->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 3); - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - nagbar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); - wl_seat_add_listener(nagbar->seat, &seat_listener, nagbar); - } else if (strcmp(interface, wl_shm_interface.name) == 0) { - nagbar->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); - } else if (strcmp(interface, wl_output_interface.name) == 0) { - if (!nagbar->output.wl_output && nagbar->xdg_output_manager) { - nagbar->querying_outputs++; - struct output_state *state = - calloc(1, sizeof(struct output_state)); - state->nagbar = nagbar; - state->wl_output = wl_registry_bind(registry, name, - &wl_output_interface, 3); - state->wl_name = name; - state->xdg_output = zxdg_output_manager_v1_get_xdg_output( - nagbar->xdg_output_manager, state->wl_output); - zxdg_output_v1_add_listener(state->xdg_output, - &xdg_output_listener, state); - } else if (!nagbar->output.wl_output && !nagbar->xdg_output_manager) { - wlr_log(WLR_ERROR, "Warning: zxdg_output_manager_v1 not supported." - " Falling back to first detected output"); - nagbar->output.wl_output = wl_registry_bind(registry, name, - &wl_output_interface, 3); - nagbar->output.wl_name = name; - wl_output_add_listener(nagbar->output.wl_output, - &output_listener, nagbar); - } - } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - nagbar->layer_shell = wl_registry_bind( - registry, name, &zwlr_layer_shell_v1_interface, 1); - } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 - && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { - nagbar->xdg_output_manager = wl_registry_bind(registry, name, - &zxdg_output_manager_v1_interface, - ZXDG_OUTPUT_V1_NAME_SINCE_VERSION); - } -} - -static void handle_global_remove(void *data, struct wl_registry *registry, - uint32_t name) { - struct sway_nagbar *nagbar = data; - if (nagbar->output.wl_name == name) { - nagbar->run_display = false; - } -} - -static const struct wl_registry_listener registry_listener = { - .global = handle_global, - .global_remove = handle_global_remove, -}; - -void nagbar_setup(struct sway_nagbar *nagbar) { - nagbar->display = wl_display_connect(NULL); - assert(nagbar->display); - - nagbar->scale = 1; - - struct wl_registry *registry = wl_display_get_registry(nagbar->display); - wl_registry_add_listener(registry, ®istry_listener, nagbar); - wl_display_roundtrip(nagbar->display); - assert(nagbar->compositor && nagbar->layer_shell && nagbar->shm); - - while (nagbar->querying_outputs > 0) { - wl_display_roundtrip(nagbar->display); - } - - if (!nagbar->output.wl_output) { - if (nagbar->output.name) { - wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name); - } else { - wlr_log(WLR_ERROR, "No outputs detected"); - } - nagbar_destroy(nagbar); - exit(EXIT_FAILURE); - } - - struct sway_nagbar_pointer *pointer = &nagbar->pointer; - int scale = nagbar->scale < 1 ? 1 : nagbar->scale; - pointer->cursor_theme = wl_cursor_theme_load( - NULL, 24 * scale, nagbar->shm); - assert(pointer->cursor_theme); - struct wl_cursor *cursor = - wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); - assert(cursor); - pointer->cursor_image = cursor->images[0]; - pointer->cursor_surface = wl_compositor_create_surface(nagbar->compositor); - assert(pointer->cursor_surface); - - nagbar->surface = wl_compositor_create_surface(nagbar->compositor); - assert(nagbar->surface); - nagbar->layer_surface = zwlr_layer_shell_v1_get_layer_surface( - nagbar->layer_shell, nagbar->surface, nagbar->output.wl_output, - ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "nagbar"); - assert(nagbar->layer_surface); - zwlr_layer_surface_v1_add_listener(nagbar->layer_surface, - &layer_surface_listener, nagbar); - zwlr_layer_surface_v1_set_anchor(nagbar->layer_surface, nagbar->anchors); - - wl_registry_destroy(registry); -} - -void nagbar_run(struct sway_nagbar *nagbar) { - nagbar->run_display = true; - render_frame(nagbar); - while (nagbar->run_display && wl_display_dispatch(nagbar->display) != -1) { - // This is intentionally left blank - } -} - -void nagbar_destroy(struct sway_nagbar *nagbar) { - nagbar->run_display = false; - - free(nagbar->message); - free(nagbar->font); - while (nagbar->buttons->length) { - struct sway_nagbar_button *button = nagbar->buttons->items[0]; - list_del(nagbar->buttons, 0); - free(button->text); - free(button->action); - free(button); - } - list_free(nagbar->buttons); - free(nagbar->details.message); - free(nagbar->details.button_up.text); - free(nagbar->details.button_down.text); - - if (nagbar->layer_surface) { - zwlr_layer_surface_v1_destroy(nagbar->layer_surface); - } - - if (nagbar->surface) { - wl_surface_destroy(nagbar->surface); - } - - if (nagbar->output.wl_output) { - wl_output_destroy(nagbar->output.wl_output); - } - - if (&nagbar->buffers[0]) { - destroy_buffer(&nagbar->buffers[0]); - } - - if (&nagbar->buffers[1]) { - destroy_buffer(&nagbar->buffers[1]); - } - - if (nagbar->compositor) { - wl_compositor_destroy(nagbar->compositor); - } - - if (nagbar->shm) { - wl_shm_destroy(nagbar->shm); - } - - if (nagbar->display) { - wl_display_disconnect(nagbar->display); - } -} diff --git a/swaynagbar/render.c b/swaynagbar/render.c deleted file mode 100644 index 7bc2961e..00000000 --- a/swaynagbar/render.c +++ /dev/null @@ -1,298 +0,0 @@ -#include -#include "cairo.h" -#include "log.h" -#include "pango.h" -#include "pool-buffer.h" -#include "swaynagbar/nagbar.h" -#include "wlr-layer-shell-unstable-v1-client-protocol.h" - -static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { - uint32_t height = nagbar->height * nagbar->scale; - height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; - - int text_width, text_height; - get_text_size(cairo, nagbar->font, &text_width, &text_height, - nagbar->scale, true, "%s", nagbar->message); - - int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; - - uint32_t ideal_height = text_height + padding * 2; - uint32_t ideal_surface_height = ideal_height / nagbar->scale; - if (nagbar->height < ideal_surface_height) { - return ideal_surface_height; - } - - cairo_set_source_u32(cairo, nagbar->colors.text); - cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); - pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", - nagbar->message); - - return ideal_height; -} - -static void render_details_scroll_button(cairo_t *cairo, - struct sway_nagbar *nagbar, struct sway_nagbar_button *button) { - int text_width, text_height; - get_text_size(cairo, nagbar->font, &text_width, &text_height, - nagbar->scale, true, "%s", button->text); - - int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; - - cairo_set_source_u32(cairo, nagbar->colors.border); - cairo_rectangle(cairo, button->x, button->y, - button->width, button->height); - cairo_fill(cairo); - - cairo_set_source_u32(cairo, nagbar->colors.button_background); - cairo_rectangle(cairo, button->x + border, button->y + border, - button->width - (border * 2), button->height - (border * 2)); - cairo_fill(cairo); - - cairo_set_source_u32(cairo, nagbar->colors.text); - cairo_move_to(cairo, button->x + border + padding, - button->y + border + (button->height - text_height) / 2); - pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); -} - -static int get_detailed_scroll_button_width(cairo_t *cairo, - struct sway_nagbar *nagbar) { - int up_width, down_width, temp_height; - get_text_size(cairo, nagbar->font, &up_width, &temp_height, - nagbar->scale, true, "%s", nagbar->details.button_up.text); - get_text_size(cairo, nagbar->font, &down_width, &temp_height, - nagbar->scale, true, "%s", nagbar->details.button_down.text); - - int text_width = up_width > down_width ? up_width : down_width; - int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; - - return text_width + border * 2 + padding * 2; -} - -static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, - uint32_t y) { - uint32_t width = nagbar->width * nagbar->scale; - uint32_t height = nagbar->height * nagbar->scale; - height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; - - int border = NAGBAR_DETAILS_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; - int decor = padding + border; - - nagbar->details.x = decor; - nagbar->details.y = y + decor; - nagbar->details.width = width - decor * 2; - - PangoLayout *layout = get_pango_layout(cairo, nagbar->font, - nagbar->details.message, nagbar->scale, true); - pango_layout_set_width(layout, - (nagbar->details.width - padding * 2) * PANGO_SCALE); - pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); - pango_layout_set_single_paragraph_mode(layout, false); - pango_cairo_update_layout(cairo, layout); - nagbar->details.total_lines = pango_layout_get_line_count(layout); - - PangoLayoutLine *line; - line = pango_layout_get_line_readonly(layout, nagbar->details.offset); - gint offset = line->start_index; - const char *text = pango_layout_get_text(layout); - pango_layout_set_text(layout, text + offset, strlen(text) - offset); - - int text_width, text_height; - pango_cairo_update_layout(cairo, layout); - pango_layout_get_pixel_size(layout, &text_width, &text_height); - - bool show_buttons = nagbar->details.offset > 0; - int button_width = get_detailed_scroll_button_width(cairo, nagbar); - if (show_buttons) { - nagbar->details.width -= button_width; - pango_layout_set_width(layout, - (nagbar->details.width - padding * 2) * PANGO_SCALE); - } - - uint32_t ideal_height; - do { - ideal_height = nagbar->details.y + text_height + decor + padding * 2; - if (ideal_height > NAGBAR_MAX_HEIGHT) { - ideal_height = NAGBAR_MAX_HEIGHT; - - if (!show_buttons) { - show_buttons = true; - nagbar->details.width -= button_width; - pango_layout_set_width(layout, - (nagbar->details.width - padding * 2) * PANGO_SCALE); - } - } - - nagbar->details.height = ideal_height - nagbar->details.y - decor; - pango_layout_set_height(layout, - (nagbar->details.height - padding * 2) * PANGO_SCALE); - pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); - pango_cairo_update_layout(cairo, layout); - pango_layout_get_pixel_size(layout, &text_width, &text_height); - } while (text_height != (nagbar->details.height - padding * 2)); - - nagbar->details.visible_lines = pango_layout_get_line_count(layout); - - if (show_buttons) { - nagbar->details.button_up.x = - nagbar->details.x + nagbar->details.width; - nagbar->details.button_up.y = nagbar->details.y; - nagbar->details.button_up.width = button_width; - nagbar->details.button_up.height = nagbar->details.height / 2; - render_details_scroll_button(cairo, nagbar, - &nagbar->details.button_up); - - nagbar->details.button_down.x = - nagbar->details.x + nagbar->details.width; - nagbar->details.button_down.y = - nagbar->details.button_up.y + nagbar->details.button_up.height; - nagbar->details.button_down.width = button_width; - nagbar->details.button_down.height = nagbar->details.height / 2; - render_details_scroll_button(cairo, nagbar, - &nagbar->details.button_down); - } - - cairo_set_source_u32(cairo, nagbar->colors.border); - cairo_rectangle(cairo, nagbar->details.x, nagbar->details.y, - nagbar->details.width, nagbar->details.height); - cairo_fill(cairo); - - cairo_move_to(cairo, nagbar->details.x + padding, - nagbar->details.y + padding); - cairo_set_source_u32(cairo, nagbar->colors.text); - pango_cairo_show_layout(cairo, layout); - g_object_unref(layout); - - return ideal_height; -} - -static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, - int button_index, int *x) { - uint32_t height = nagbar->height * nagbar->scale; - height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; - struct sway_nagbar_button *button = nagbar->buttons->items[button_index]; - - int text_width, text_height; - get_text_size(cairo, nagbar->font, &text_width, &text_height, - nagbar->scale, true, "%s", button->text); - - int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; - - uint32_t ideal_height = text_height + padding * 2 + border * 2; - uint32_t ideal_surface_height = ideal_height / nagbar->scale; - if (nagbar->height < ideal_surface_height) { - return ideal_surface_height; - } - - button->x = *x - border - text_width - padding * 2; - button->y = (int)(ideal_height - text_height) / 2 - padding; - button->width = text_width + padding * 2; - button->height = text_height + padding * 2; - - cairo_set_source_u32(cairo, nagbar->colors.border); - cairo_rectangle(cairo, button->x - border, button->y - border, - button->width + border * 2, button->height + border * 2); - cairo_fill(cairo); - - cairo_set_source_u32(cairo, nagbar->colors.button_background); - cairo_rectangle(cairo, button->x, button->y, - button->width, button->height); - cairo_fill(cairo); - - cairo_set_source_u32(cairo, nagbar->colors.text); - cairo_move_to(cairo, button->x + padding, button->y + padding); - pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); - - *x = button->x - border; - - return ideal_height; -} - -static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { - uint32_t max_height = 0; - - cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); - cairo_set_source_u32(cairo, nagbar->colors.background); - cairo_paint(cairo); - - uint32_t h = render_message(cairo, nagbar); - max_height = h > max_height ? h : max_height; - - int x = (nagbar->width - NAGBAR_BUTTON_MARGIN_RIGHT) * nagbar->scale; - for (int i = 0; i < nagbar->buttons->length; i++) { - h = render_button(cairo, nagbar, i, &x); - max_height = h > max_height ? h : max_height; - x -= NAGBAR_BUTTON_GAP * nagbar->scale; - if (i == 0) { - x -= NAGBAR_BUTTON_GAP_CLOSE * nagbar->scale; - } - } - - if (nagbar->details.visible) { - h = render_detailed(cairo, nagbar, max_height); - max_height = h > max_height ? h : max_height; - } - - int border = NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; - if (max_height > nagbar->height) { - max_height += border; - } - cairo_set_source_u32(cairo, nagbar->colors.border_bottom); - cairo_rectangle(cairo, 0, nagbar->height * nagbar->scale - border, - nagbar->width * nagbar->scale, border); - cairo_fill(cairo); - - return max_height; -} - -void render_frame(struct sway_nagbar *nagbar) { - if (!nagbar->run_display) { - return; - } - - cairo_surface_t *recorder = cairo_recording_surface_create( - CAIRO_CONTENT_COLOR_ALPHA, NULL); - cairo_t *cairo = cairo_create(recorder); - cairo_save(cairo); - cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); - cairo_paint(cairo); - cairo_restore(cairo); - uint32_t height = render_to_cairo(cairo, nagbar); - if (height != nagbar->height) { - zwlr_layer_surface_v1_set_size(nagbar->layer_surface, 0, height); - zwlr_layer_surface_v1_set_exclusive_zone(nagbar->layer_surface, - height); - wl_surface_commit(nagbar->surface); - wl_display_roundtrip(nagbar->display); - } else { - nagbar->current_buffer = get_next_buffer(nagbar->shm, - nagbar->buffers, - nagbar->width * nagbar->scale, - nagbar->height * nagbar->scale); - if (!nagbar->current_buffer) { - wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame."); - return; - } - - cairo_t *shm = nagbar->current_buffer->cairo; - cairo_save(shm); - cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); - cairo_paint(shm); - cairo_restore(shm); - cairo_set_source_surface(shm, recorder, 0.0, 0.0); - cairo_paint(shm); - - wl_surface_set_buffer_scale(nagbar->surface, nagbar->scale); - wl_surface_attach(nagbar->surface, - nagbar->current_buffer->buffer, 0, 0); - wl_surface_damage(nagbar->surface, 0, 0, - nagbar->width, nagbar->height); - wl_surface_commit(nagbar->surface); - wl_display_roundtrip(nagbar->display); - } - cairo_surface_destroy(recorder); - cairo_destroy(cairo); -} diff --git a/swaynagbar/swaynagbar.1.scd b/swaynagbar/swaynagbar.1.scd deleted file mode 100644 index 2a34ae68..00000000 --- a/swaynagbar/swaynagbar.1.scd +++ /dev/null @@ -1,53 +0,0 @@ -swaynagbar(1) - -# NAME - -swaynagbar - Show a warning or error message with buttons - -# SYNOPSIS - -_swaynagbar_ [options...] - -# OPTIONS -*-b, --button* - Create a button with the text _text_ that executes _action_ when pressed. - Multiple buttons can be defined by providing the flag multiple times. - -*-d, --debug* - Enable debugging. - -*-e, --edge top|bottom* - Set the edge to use. - -*-f, --font * - Set the font to use. - -*-h, --help* - Show help message and quit. - -*-l, --detailed-message * - Set the detailed message. A button to toggle details will be added. Details - are shown in a scrollable multi-line text area. If _msg_ is _-_, then the - detailed message will be read from stdin. - -*-L, --detailed-button * - Set the text for the button that toggles details. This has no effect if - there is not a detailed message. The default is _Toggle Details_. - -*-m, --message * - 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 - -*-s, --dismiss-button * - Sets the text for the dismiss nagbar button. The default is _X_. - -*-t, --type error|warning* - Set the message type. - -*-v, --version - Show the version number and quit. - -- cgit v1.2.3-54-g00ecf From 8463a2896a932cd99f3dc93608b03cb4aba93293 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 28 Jul 2018 09:34:25 -0400 Subject: swaynag: implement config file support --- include/swaynag/nagbar.h | 20 +-- include/swaynag/types.h | 25 ++++ meson.build | 1 + swaynag/main.c | 357 +++++++++++++++++++++++++++++++++-------------- swaynag/meson.build | 1 + swaynag/nagbar.c | 5 + swaynag/render.c | 23 +-- swaynag/swaynag.1.scd | 33 +++-- swaynag/swaynag.5.scd | 57 ++++++++ swaynag/types.c | 116 +++++++++++++++ 10 files changed, 500 insertions(+), 138 deletions(-) create mode 100644 include/swaynag/types.h create mode 100644 swaynag/swaynag.5.scd create mode 100644 swaynag/types.c diff --git a/include/swaynag/nagbar.h b/include/swaynag/nagbar.h index 8b55e4fa..b5d9b2cb 100644 --- a/include/swaynag/nagbar.h +++ b/include/swaynag/nagbar.h @@ -1,8 +1,9 @@ #ifndef _SWAY_NAGBAR_NAGBAR_H -#define _SWAY_NAGBAR_NAGNAR_H +#define _SWAY_NAGBAR_NAGBAR_H #include #include "list.h" #include "pool-buffer.h" +#include "swaynag/types.h" #include "xdg-output-unstable-v1-client-protocol.h" #define NAGBAR_BAR_BORDER_THICKNESS 2 @@ -16,25 +17,12 @@ #define NAGBAR_MAX_HEIGHT 500 -enum sway_nagbar_type { - NAGBAR_ERROR, - NAGBAR_WARNING, -}; - enum sway_nagbar_action_type { NAGBAR_ACTION_DISMISS, NAGBAR_ACTION_EXPAND, NAGBAR_ACTION_COMMAND, }; -struct sway_nagbar_colors { - uint32_t button_background; - uint32_t background; - uint32_t text; - uint32_t border; - uint32_t border_bottom; -}; - struct sway_nagbar_pointer { struct wl_pointer *pointer; struct wl_cursor_theme *cursor_theme; @@ -72,6 +60,7 @@ struct sway_nagbar_details { int offset; int visible_lines; int total_lines; + struct sway_nagbar_button button_details; struct sway_nagbar_button button_up; struct sway_nagbar_button button_down; }; @@ -97,8 +86,7 @@ struct sway_nagbar { struct pool_buffer buffers[2]; struct pool_buffer *current_buffer; - enum sway_nagbar_type type; - struct sway_nagbar_colors colors; + struct sway_nagbar_type *type; uint32_t anchors; char *message; char *font; diff --git a/include/swaynag/types.h b/include/swaynag/types.h new file mode 100644 index 00000000..32056514 --- /dev/null +++ b/include/swaynag/types.h @@ -0,0 +1,25 @@ +#ifndef _SWAY_NAGBAR_TYPES_H +#define _SWAY_NAGBAR_TYPES_H + +struct sway_nagbar_type { + char *name; + uint32_t button_background; + uint32_t background; + uint32_t text; + uint32_t border; + uint32_t border_bottom; +}; + +void nagbar_types_add_default(list_t *types); + +struct sway_nagbar_type *nagbar_type_get(list_t *types, char *name); + +struct sway_nagbar_type *nagbar_type_clone(struct sway_nagbar_type *type); + +void nagbar_type_free(struct sway_nagbar_type *type); + +void nagbar_types_free(list_t *types); + +int nagbar_parse_type(int argc, char **argv, struct sway_nagbar_type *type); + +#endif diff --git a/meson.build b/meson.build index c6501c39..57ed70cb 100644 --- a/meson.build +++ b/meson.build @@ -83,6 +83,7 @@ if scdoc.found() 'swaymsg/swaymsg.1.scd', 'swayidle/swayidle.1.scd', 'swaynag/swaynag.1.scd', + 'swaynag/swaynag.5.scd', ] foreach filename : man_files topic = filename.split('.')[-3].split('/')[-1] diff --git a/swaynag/main.c b/swaynag/main.c index 60560c72..b199fac4 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -1,10 +1,14 @@ -#define _XOPEN_SOURCE 500 +#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200112L #include #include +#include +#include #include "log.h" #include "list.h" #include "readline.h" #include "swaynag/nagbar.h" +#include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" static struct sway_nagbar nagbar; @@ -19,22 +23,6 @@ void sway_terminate(int code) { exit(code); } -static void set_nagbar_colors() { - if (nagbar.type == NAGBAR_ERROR) { - nagbar.colors.button_background = 0x680A0AFF; - nagbar.colors.background = 0x900000FF; - nagbar.colors.text = 0xFFFFFFFF; - nagbar.colors.border = 0xD92424FF; - nagbar.colors.border_bottom = 0x470909FF; - } else if (nagbar.type == NAGBAR_WARNING) { - nagbar.colors.button_background = 0xFFC100FF; - nagbar.colors.background = 0xFFA800FF; - nagbar.colors.text = 0x000000FF; - nagbar.colors.border = 0xAB7100FF; - nagbar.colors.border_bottom = 0xAB7100FF; - } -} - static char *read_from_stdin() { char *buffer = NULL; while (!feof(stdin)) { @@ -61,32 +49,11 @@ static char *read_from_stdin() { return buffer; } -int main(int argc, char **argv) { - int exit_code = EXIT_SUCCESS; - bool debug = false; - - memset(&nagbar, 0, sizeof(nagbar)); - nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - nagbar.type = NAGBAR_ERROR; - set_nagbar_colors(); - nagbar.font = strdup("pango:monospace 10"); - nagbar.buttons = create_list(); - - struct sway_nagbar_button *button_close = - calloc(sizeof(struct sway_nagbar_button), 1); - button_close->text = strdup("X"); - button_close->type = NAGBAR_ACTION_DISMISS; - list_add(nagbar.buttons, button_close); - - struct sway_nagbar_button *button_details = - calloc(sizeof(struct sway_nagbar_button), 1); - button_details->text = strdup("Toggle Details"); - button_details->type = NAGBAR_ACTION_EXPAND; - +static int parse_options(int argc, char **argv, struct sway_nagbar *nagbar, + list_t *types, char **config, bool *debug) { static struct option opts[] = { {"button", required_argument, NULL, 'b'}, + {"config", required_argument, NULL, 'c'}, {"debug", no_argument, NULL, 'd'}, {"edge", required_argument, NULL, 'e'}, {"font", required_argument, NULL, 'f'}, @@ -106,6 +73,7 @@ int main(int argc, char **argv) { "\n" " -b, --button Create a button with text that " "executes action when pressed. Multiple buttons can be defined.\n" + " -c, --config Path to config file.\n" " -d, --debug Enable debugging.\n" " -e, --edge top|bottom Set the edge to use.\n" " -f, --font Set the font to use.\n" @@ -115,114 +83,301 @@ int main(int argc, char **argv) { " -m, --message Set the message text.\n" " -o, --output Set the output to use.\n" " -s, --dismiss-button Set the dismiss button text.\n" - " -t, --type error|warning Set the message type.\n" + " -t, --type Set the message type.\n" " -v, --version Show the version number and quit.\n"; + optind = 1; while (1) { - int c = getopt_long(argc, argv, "b:de:f:hlL:m:o:s:t:v", opts, NULL); + int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); if (c == -1) { break; } switch (c) { case 'b': // Button - if (optind >= argc) { - fprintf(stderr, "Missing action for button %s\n", optarg); - exit_code = EXIT_FAILURE; - goto cleanup; - } - struct sway_nagbar_button *button; - button = calloc(sizeof(struct sway_nagbar_button), 1); - button->text = strdup(optarg); - button->type = NAGBAR_ACTION_COMMAND; - button->action = strdup(argv[optind]); - optind++; - list_add(nagbar.buttons, button); + if (nagbar) { + if (optind >= argc) { + fprintf(stderr, "Missing action for button %s\n", optarg); + return EXIT_FAILURE; + } + struct sway_nagbar_button *button; + button = calloc(sizeof(struct sway_nagbar_button), 1); + button->text = strdup(optarg); + button->type = NAGBAR_ACTION_COMMAND; + button->action = strdup(argv[optind]); + optind++; + list_add(nagbar->buttons, button); + } + break; + case 'c': // Config + if (config) { + *config = strdup(optarg); + } break; case 'd': // Debug - debug = true; + if (debug) { + *debug = true; + } break; case 'e': // Edge - if (strcmp(optarg, "top") == 0) { - nagbar.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) { - nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - } else { - fprintf(stderr, "Invalid edge: %s\n", optarg); - exit_code = EXIT_FAILURE; - goto cleanup; + if (nagbar) { + if (strcmp(optarg, "top") == 0) { + nagbar->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) { + nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else { + fprintf(stderr, "Invalid edge: %s\n", optarg); + return EXIT_FAILURE; + } } break; case 'f': // Font - free(nagbar.font); - nagbar.font = strdup(optarg); + if (nagbar) { + free(nagbar->font); + nagbar->font = strdup(optarg); + } break; case 'l': // Detailed Message - free(nagbar.details.message); - nagbar.details.message = read_from_stdin(); - nagbar.details.button_up.text = strdup("▲"); - nagbar.details.button_down.text = strdup("▼"); + if (nagbar) { + free(nagbar->details.message); + nagbar->details.message = read_from_stdin(); + nagbar->details.button_up.text = strdup("▲"); + nagbar->details.button_down.text = strdup("▼"); + } break; case 'L': // Detailed Button Text - free(button_details->text); - button_details->text = strdup(optarg); + if (nagbar) { + free(nagbar->details.button_details.text); + nagbar->details.button_details.text = strdup(optarg); + } break; case 'm': // Message - free(nagbar.message); - nagbar.message = strdup(optarg); + if (nagbar) { + free(nagbar->message); + nagbar->message = strdup(optarg); + } break; case 'o': // Output - free(nagbar.output.name); - nagbar.output.name = strdup(optarg); + if (nagbar) { + free(nagbar->output.name); + nagbar->output.name = strdup(optarg); + } break; case 's': // Dismiss Button Text - free(button_close->text); - button_close->text = strdup(optarg); + if (nagbar) { + struct sway_nagbar_button *button_close; + button_close = nagbar->buttons->items[0]; + free(button_close->text); + button_close->text = strdup(optarg); + } break; case 't': // Type - if (strcmp(optarg, "error") == 0) { - nagbar.type = NAGBAR_ERROR; - } else if (strcmp(optarg, "warning") == 0) { - nagbar.type = NAGBAR_WARNING; - } else { - fprintf(stderr, "Type must be either 'error' or 'warning'\n"); - exit_code = EXIT_FAILURE; - goto cleanup; + if (nagbar) { + nagbar->type = nagbar_type_get(types, optarg); + if (!nagbar->type) { + fprintf(stderr, "Unknown type %s\n", optarg); + return EXIT_FAILURE; + } } - set_nagbar_colors(); break; case 'v': // Version fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); - exit_code = EXIT_SUCCESS; - goto cleanup; + return -1; default: // Help or unknown flag fprintf(c == 'h' ? stdout : stderr, "%s", usage); - exit_code = c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE; - goto cleanup; + return -1; + } + } + + return 0; +} + +static bool file_exists(const char *path) { + return path && access(path, R_OK) != -1; +} + +static char *get_config_path(void) { + static const char *config_paths[] = { + "$HOME/.swaynag/config", + "$XDG_CONFIG_HOME/swaynag/config", + SYSCONFDIR "/swaynag/config", + }; + + if (!getenv("XDG_CONFIG_HOME")) { + char *home = getenv("HOME"); + char *config_home = malloc(strlen(home) + strlen("/.config") + 1); + if (!config_home) { + wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); + } else { + strcpy(config_home, home); + strcat(config_home, "/.config"); + setenv("XDG_CONFIG_HOME", config_home, 1); + wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); + free(config_home); + } + } + + wordexp_t p; + char *path; + for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { + if (wordexp(config_paths[i], &p, 0) == 0) { + path = strdup(p.we_wordv[0]); + wordfree(&p); + if (file_exists(path)) { + return path; + } + free(path); + } + } + + return NULL; +} + +static int load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { + FILE *config = fopen(path, "r"); + if (!config) { + fprintf(stderr, "Failed to read config. Running without it.\n"); + return 0; + } + struct sway_nagbar_type *type = NULL; + char *line; + int line_number = 0; + while (!feof(config)) { + line = read_line(config); + if (!line) { + continue; + } + + line_number++; + if (line[0] == '#') { + free(line); + continue; } + if (strlen(line) == 0) { + free(line); + continue; + } + + if (line[0] == '[') { + char *close = strchr(line, ']'); + if (!close) { + free(line); + fclose(config); + fprintf(stderr, "Closing bracket not found on line %d\n", + line_number); + return 1; + } + char *name = calloc(1, close - line); + strncat(name, line + 1, close - line - 1); + type = nagbar_type_get(types, name); + if (!type) { + type = calloc(1, sizeof(struct sway_nagbar_type)); + type->name = strdup(name); + list_add(types, type); + } + free(name); + } else { + char flag[strlen(line) + 3]; + sprintf(flag, "--%s", line); + char *argv[] = {"swaynag", flag}; + int result; + if (type) { + result = nagbar_parse_type(2, argv, type); + } else { + result = parse_options(2, argv, nagbar, types, NULL, NULL); + } + if (result != 0) { + free(line); + fclose(config); + return result; + } + } + + free(line); } + fclose(config); + return 0; +} + +int main(int argc, char **argv) { + int exit_code = EXIT_SUCCESS; + + list_t *types = create_list(); + nagbar_types_add_default(types); + + memset(&nagbar, 0, sizeof(nagbar)); + nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + nagbar.font = strdup("pango:monospace 10"); + nagbar.buttons = create_list(); + + struct sway_nagbar_button *button_close = + calloc(sizeof(struct sway_nagbar_button), 1); + button_close->text = strdup("X"); + button_close->type = NAGBAR_ACTION_DISMISS; + list_add(nagbar.buttons, button_close); + + nagbar.details.button_details.text = strdup("Toggle Details"); + nagbar.details.button_details.type = NAGBAR_ACTION_EXPAND; + + char *config_path = NULL; + bool debug = false; + int launch_status = parse_options(argc, argv, NULL, NULL, + &config_path, &debug); + if (launch_status != 0) { + exit_code = launch_status; + goto cleanup; + } wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); + if (!config_path) { + config_path = get_config_path(); + } + if (config_path) { + wlr_log(WLR_DEBUG, "Loading config file: %s", config_path); + int config_status = load_config(config_path, &nagbar, types); + free(config_path); + if (config_status != 0) { + exit_code = config_status; + goto cleanup; + } + } + + if (argc > 1) { + int result = parse_options(argc, argv, &nagbar, types, NULL, NULL); + if (result != 0) { + exit_code = result; + goto cleanup; + } + } + if (!nagbar.message) { wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); exit_code = EXIT_FAILURE; goto cleanup; } + if (!nagbar.type) { + nagbar.type = nagbar_type_get(types, "error"); + } + + nagbar.type = nagbar_type_clone(nagbar.type); + nagbar_types_free(types); + if (nagbar.details.message) { - list_add(nagbar.buttons, button_details); + list_add(nagbar.buttons, &nagbar.details.button_details); } else { - free(button_details->text); - free(button_details); + free(nagbar.details.button_details.text); } wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); - wlr_log(WLR_DEBUG, "Type: %d", nagbar.type); + wlr_log(WLR_DEBUG, "Type: %s", nagbar.type->name); wlr_log(WLR_DEBUG, "Message: %s", nagbar.message); wlr_log(WLR_DEBUG, "Font: %s", nagbar.font); wlr_log(WLR_DEBUG, "Buttons"); @@ -238,8 +393,8 @@ int main(int argc, char **argv) { return exit_code; cleanup: - free(button_details->text); - free(button_details); + nagbar_types_free(types); + free(nagbar.details.button_details.text); nagbar_destroy(&nagbar); return exit_code; } diff --git a/swaynag/meson.build b/swaynag/meson.build index 6492e4dc..6a9df984 100644 --- a/swaynag/meson.build +++ b/swaynag/meson.build @@ -3,6 +3,7 @@ executable( 'main.c', 'nagbar.c', 'render.c', + 'types.c', ], include_directories: [sway_inc], dependencies: [ diff --git a/swaynag/nagbar.c b/swaynag/nagbar.c index 6647e8c2..a20a9095 100644 --- a/swaynag/nagbar.c +++ b/swaynag/nagbar.c @@ -8,6 +8,7 @@ #include "list.h" #include "swaynag/nagbar.h" #include "swaynag/render.h" +#include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" static void nop() { @@ -386,6 +387,10 @@ void nagbar_destroy(struct sway_nagbar *nagbar) { free(nagbar->details.button_up.text); free(nagbar->details.button_down.text); + if (nagbar->type) { + nagbar_type_free(nagbar->type); + } + if (nagbar->layer_surface) { zwlr_layer_surface_v1_destroy(nagbar->layer_surface); } diff --git a/swaynag/render.c b/swaynag/render.c index 150ae3f2..134c247e 100644 --- a/swaynag/render.c +++ b/swaynag/render.c @@ -4,6 +4,7 @@ #include "pango.h" #include "pool-buffer.h" #include "swaynag/nagbar.h" +#include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { @@ -22,7 +23,7 @@ static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { return ideal_surface_height; } - cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_set_source_u32(cairo, nagbar->type->text); cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", nagbar->message); @@ -39,17 +40,17 @@ static void render_details_scroll_button(cairo_t *cairo, int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; - cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_set_source_u32(cairo, nagbar->type->border); cairo_rectangle(cairo, button->x, button->y, button->width, button->height); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->colors.button_background); + cairo_set_source_u32(cairo, nagbar->type->button_background); cairo_rectangle(cairo, button->x + border, button->y + border, button->width - (border * 2), button->height - (border * 2)); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_set_source_u32(cairo, nagbar->type->text); cairo_move_to(cairo, button->x + border + padding, button->y + border + (button->height - text_height) / 2); pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); @@ -154,14 +155,14 @@ static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, &nagbar->details.button_down); } - cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_set_source_u32(cairo, nagbar->type->border); cairo_rectangle(cairo, nagbar->details.x, nagbar->details.y, nagbar->details.width, nagbar->details.height); cairo_fill(cairo); cairo_move_to(cairo, nagbar->details.x + padding, nagbar->details.y + padding); - cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_set_source_u32(cairo, nagbar->type->text); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); @@ -192,17 +193,17 @@ static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, button->width = text_width + padding * 2; button->height = text_height + padding * 2; - cairo_set_source_u32(cairo, nagbar->colors.border); + cairo_set_source_u32(cairo, nagbar->type->border); cairo_rectangle(cairo, button->x - border, button->y - border, button->width + border * 2, button->height + border * 2); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->colors.button_background); + cairo_set_source_u32(cairo, nagbar->type->button_background); cairo_rectangle(cairo, button->x, button->y, button->width, button->height); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->colors.text); + cairo_set_source_u32(cairo, nagbar->type->text); cairo_move_to(cairo, button->x + padding, button->y + padding); pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); @@ -215,7 +216,7 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { uint32_t max_height = 0; cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); - cairo_set_source_u32(cairo, nagbar->colors.background); + cairo_set_source_u32(cairo, nagbar->type->background); cairo_paint(cairo); uint32_t h = render_message(cairo, nagbar); @@ -240,7 +241,7 @@ static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { if (max_height > nagbar->height) { max_height += border; } - cairo_set_source_u32(cairo, nagbar->colors.border_bottom); + cairo_set_source_u32(cairo, nagbar->type->border_bottom); cairo_rectangle(cairo, 0, nagbar->height * nagbar->scale - border, nagbar->width * nagbar->scale, border); cairo_fill(cairo); diff --git a/swaynag/swaynag.1.scd b/swaynag/swaynag.1.scd index 879aaf2e..7d250a45 100644 --- a/swaynag/swaynag.1.scd +++ b/swaynag/swaynag.1.scd @@ -1,4 +1,4 @@ -swaynagbar(1) +swaynag(1) # NAME @@ -13,13 +13,21 @@ _swaynag_ [options...] Create a button with the text _text_ that executes _action_ when pressed. Multiple buttons can be defined by providing the flag multiple times. +*-c, --config* + The config file to use. By default, the following paths are checked: + _$HOME/.swaynag/config_, _$XDG\_CONFIG\_HOME/swaynag/config_, and + _SYSCONFDIR/swaynag/config_. All flags aside from this one and _debug_ are + valid options in the configuration file using the format + _long-option=value_. All leading dashes should be omitted and the equals + sign is required. See swaynag(5) for more information. + *-d, --debug* Enable debugging. -*-e, --edge top|bottom* +*-e, --edge* top|bottom Set the edge to use. -*-f, --font * +*-f, --font* Set the font to use. *-h, --help* @@ -29,24 +37,29 @@ _swaynag_ [options...] Read a detailed message from stdin. A button to toggle details will be added. Details are shown in a scrollable multi-line text area. -*-L, --detailed-button * +*-L, --detailed-button* Set the text for the button that toggles details. This has no effect if there is not a detailed message. The default is _Toggle Details_. -*-m, --message * +*-m, --message* Set the message text. -*-o, --output * +*-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 -*-s, --dismiss-button * +*-s, --dismiss-button* Sets the text for the dismiss nagbar button. The default is _X_. -*-t, --type error|warning* - Set the message 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 + overridden in the config file as well. -*-v, --version +*-v, --version* Show the version number and quit. +# SEE +swaynag(5) diff --git a/swaynag/swaynag.5.scd b/swaynag/swaynag.5.scd new file mode 100644 index 00000000..a4e05e3a --- /dev/null +++ b/swaynag/swaynag.5.scd @@ -0,0 +1,57 @@ +swaynag(5) + +# NAME + +swaynag - swaynag configuration file + +# SYNOPSIS + +$HOME/.swaynag/config, $XDG\_CONFIG\_HOME/swaynag/config, +SYSCONFDIR/swaynag/config + +# CONFIG FILE +At the top of the config file, _swaynag_ options can be set using the format +_long-option=value_. These will be used as default values if _swaynag_ is not +given the option. This can be useful for setting a preferred font, output, and +edge. + +Below the options, custom types may be defined. To define a type, use the +following format: + +``` +[name-of-type] +color=RRGGBB[AA] +``` + +All colors may be given in the form _RRGGBB_ or _RRGGBBAA_. The following +colors can be set: + +*background* + The background color for _swaynag_. + +*border* + The color to use for borders of buttons. + +*border-bottom* + The color of the border line at the bottom of _swaynag_. + +*button-background* + The background color for the buttons. + +*text* + The color of the text. + +# EXAMPLE +``` +font=Monospace 12 + +[green] +background=00AA00 +border=006600 +border-bottom=004400 +text=FFFFFF +button-background=00CC00 +``` + +# SEE +swaynag(1) diff --git a/swaynag/types.c b/swaynag/types.c new file mode 100644 index 00000000..8dd99d2a --- /dev/null +++ b/swaynag/types.c @@ -0,0 +1,116 @@ +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include "list.h" +#include "swaynag/types.h" +#include "util.h" + +void nagbar_types_add_default(list_t *types) { + struct sway_nagbar_type *type_error; + type_error = calloc(1, sizeof(struct sway_nagbar_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 sway_nagbar_type *type_warning; + type_warning = calloc(1, sizeof(struct sway_nagbar_type)); + type_warning->name = strdup("warning"); + type_warning->button_background = 0xFFC100FF; + type_warning->background = 0xFFA800FF; + type_warning->text = 0x000000FF; + type_warning->border = 0xAB7100FF; + type_warning->border_bottom = 0xAB7100FF; + list_add(types, type_warning); +} + +struct sway_nagbar_type *nagbar_type_get(list_t *types, char *name) { + for (int i = 0; i < types->length; i++) { + struct sway_nagbar_type *type = types->items[i]; + if (strcasecmp(type->name, name) == 0) { + return type; + } + } + return NULL; +} + +struct sway_nagbar_type *nagbar_type_clone(struct sway_nagbar_type *type) { + struct sway_nagbar_type *clone; + clone = calloc(1, sizeof(struct sway_nagbar_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 nagbar_type_free(struct sway_nagbar_type *type) { + free(type->name); + free(type); +} + +void nagbar_types_free(list_t *types) { + while (types->length) { + struct sway_nagbar_type *type = types->items[0]; + nagbar_type_free(type); + list_del(types, 0); + } + list_free(types); +} + +int nagbar_parse_type(int argc, char **argv, struct sway_nagbar_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-54-g00ecf From 37709917b115e43a4aa824df8ada11edcaf499cc Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 28 Jul 2018 09:44:01 -0400 Subject: Set output to NULL if not specified This opens nagbar on the active output. --- swaynag/nagbar.c | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/swaynag/nagbar.c b/swaynag/nagbar.c index a20a9095..e451a53a 100644 --- a/swaynag/nagbar.c +++ b/swaynag/nagbar.c @@ -230,8 +230,7 @@ static void xdg_output_handle_name(void *data, struct output_state *state = data; char *outname = state->nagbar->output.name; wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); - if ((!outname && !state->nagbar->output.wl_output) - || (name && outname && strcmp(name, outname) == 0)) { + if (outname && !state->nagbar->output.wl_output) { wlr_log(WLR_DEBUG, "Using output %s", name); state->nagbar->output.wl_output = state->wl_output; state->nagbar->output.wl_name = state->wl_name; @@ -279,14 +278,6 @@ static void handle_global(void *data, struct wl_registry *registry, nagbar->xdg_output_manager, state->wl_output); zxdg_output_v1_add_listener(state->xdg_output, &xdg_output_listener, state); - } else if (!nagbar->output.wl_output && !nagbar->xdg_output_manager) { - wlr_log(WLR_ERROR, "Warning: zxdg_output_manager_v1 not supported." - " Falling back to first detected output"); - nagbar->output.wl_output = wl_registry_bind(registry, name, - &wl_output_interface, 3); - nagbar->output.wl_name = name; - wl_output_add_listener(nagbar->output.wl_output, - &output_listener, nagbar); } } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { nagbar->layer_shell = wl_registry_bind( @@ -327,12 +318,8 @@ void nagbar_setup(struct sway_nagbar *nagbar) { wl_display_roundtrip(nagbar->display); } - if (!nagbar->output.wl_output) { - if (nagbar->output.name) { - wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name); - } else { - wlr_log(WLR_ERROR, "No outputs detected"); - } + if (!nagbar->output.wl_output && nagbar->output.name) { + wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name); nagbar_destroy(nagbar); exit(EXIT_FAILURE); } -- cgit v1.2.3-54-g00ecf From 58f3fa74aeb3e0dd9500d687f3c97a6c10612c41 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 28 Jul 2018 09:48:37 -0400 Subject: Disable pango markup for extended message --- swaynag/render.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swaynag/render.c b/swaynag/render.c index 134c247e..ef0f72a8 100644 --- a/swaynag/render.c +++ b/swaynag/render.c @@ -25,7 +25,7 @@ static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { cairo_set_source_u32(cairo, nagbar->type->text); cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); - pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", + pango_printf(cairo, nagbar->font, nagbar->scale, false, "%s", nagbar->message); return ideal_height; @@ -86,7 +86,7 @@ static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, nagbar->details.width = width - decor * 2; PangoLayout *layout = get_pango_layout(cairo, nagbar->font, - nagbar->details.message, nagbar->scale, true); + nagbar->details.message, nagbar->scale, false); pango_layout_set_width(layout, (nagbar->details.width - padding * 2) * PANGO_SCALE); pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); -- cgit v1.2.3-54-g00ecf From 6124d0f9a202ba2f39125602a35bb47d3742022b Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sat, 28 Jul 2018 22:56:12 -0400 Subject: swaynag: split config into own file and fix optind --- include/swaynag/config.h | 13 +++ include/swaynag/nagbar.h | 1 + swaynag/config.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++ swaynag/main.c | 296 ++--------------------------------------------- swaynag/meson.build | 1 + swaynag/types.c | 1 + 6 files changed, 315 insertions(+), 289 deletions(-) create mode 100644 include/swaynag/config.h create mode 100644 swaynag/config.c diff --git a/include/swaynag/config.h b/include/swaynag/config.h new file mode 100644 index 00000000..caa32710 --- /dev/null +++ b/include/swaynag/config.h @@ -0,0 +1,13 @@ +#ifndef _SWAY_NAGBAR_CONFIG_H +#define _SWAY_NAGBAR_CONFIG_H +#include "swaynag/nagbar.h" +#include "list.h" + +int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, + list_t *types, char **config, bool *debug); + +char *nagbar_get_config_path(void); + +int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types); + +#endif diff --git a/include/swaynag/nagbar.h b/include/swaynag/nagbar.h index b5d9b2cb..4ef101e9 100644 --- a/include/swaynag/nagbar.h +++ b/include/swaynag/nagbar.h @@ -1,6 +1,7 @@ #ifndef _SWAY_NAGBAR_NAGBAR_H #define _SWAY_NAGBAR_NAGBAR_H #include +#include #include "list.h" #include "pool-buffer.h" #include "swaynag/types.h" diff --git a/swaynag/config.c b/swaynag/config.c new file mode 100644 index 00000000..4e7edf2e --- /dev/null +++ b/swaynag/config.c @@ -0,0 +1,292 @@ +#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200112L +#include +#include +#include +#include "log.h" +#include "list.h" +#include "readline.h" +#include "swaynag/nagbar.h" +#include "swaynag/types.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +static char *read_from_stdin() { + char *buffer = NULL; + while (!feof(stdin)) { + char *line = read_line(stdin); + if (!line) { + continue; + } + + if (!buffer) { + buffer = strdup(line); + } else { + buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); + strcat(buffer, line); + strcat(buffer, "\n"); + } + + free(line); + } + + if (buffer && buffer[strlen(buffer) - 1] == '\n') { + buffer[strlen(buffer) - 1] = '\0'; + } + + return buffer; +} + +int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, + list_t *types, char **config, bool *debug) { + static struct option opts[] = { + {"button", required_argument, NULL, 'b'}, + {"config", required_argument, NULL, 'c'}, + {"debug", no_argument, NULL, 'd'}, + {"edge", required_argument, NULL, 'e'}, + {"font", required_argument, NULL, 'f'}, + {"help", no_argument, NULL, 'h'}, + {"detailed-message", no_argument, NULL, 'l'}, + {"detailed-button", required_argument, NULL, 'L'}, + {"message", required_argument, NULL, 'm'}, + {"output", required_argument, NULL, 'o'}, + {"dismiss-button", required_argument, NULL, 's'}, + {"type", required_argument, NULL, 't'}, + {"version", no_argument, NULL, 'v'}, + {0, 0, 0, 0} + }; + + const char *usage = + "Usage: swaynag [options...]\n" + "\n" + " -b, --button Create a button with text that " + "executes action when pressed. Multiple buttons can be defined.\n" + " -c, --config Path to config file.\n" + " -d, --debug Enable debugging.\n" + " -e, --edge top|bottom Set the edge to use.\n" + " -f, --font Set the font to use.\n" + " -h, --help Show help message and quit.\n" + " -l, --detailed-message Read a detailed message from stdin.\n" + " -L, --detailed-button Set the text of the detail button.\n" + " -m, --message Set the message text.\n" + " -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"; + + optind = 1; + while (1) { + int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); + if (c == -1) { + break; + } + switch (c) { + case 'b': // Button + if (nagbar) { + if (optind >= argc) { + fprintf(stderr, "Missing action for button %s\n", optarg); + return EXIT_FAILURE; + } + struct sway_nagbar_button *button; + button = calloc(sizeof(struct sway_nagbar_button), 1); + button->text = strdup(optarg); + button->type = NAGBAR_ACTION_COMMAND; + button->action = strdup(argv[optind]); + list_add(nagbar->buttons, button); + } + optind++; + break; + case 'c': // Config + if (config) { + *config = strdup(optarg); + } + break; + case 'd': // Debug + if (debug) { + *debug = true; + } + break; + case 'e': // Edge + if (nagbar) { + if (strcmp(optarg, "top") == 0) { + nagbar->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) { + nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT + | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + } else { + fprintf(stderr, "Invalid edge: %s\n", optarg); + return EXIT_FAILURE; + } + } + break; + case 'f': // Font + if (nagbar) { + free(nagbar->font); + nagbar->font = strdup(optarg); + } + break; + case 'l': // Detailed Message + if (nagbar) { + free(nagbar->details.message); + nagbar->details.message = read_from_stdin(); + nagbar->details.button_up.text = strdup("▲"); + nagbar->details.button_down.text = strdup("▼"); + } + break; + case 'L': // Detailed Button Text + if (nagbar) { + free(nagbar->details.button_details.text); + nagbar->details.button_details.text = strdup(optarg); + } + break; + case 'm': // Message + if (nagbar) { + free(nagbar->message); + nagbar->message = strdup(optarg); + } + break; + case 'o': // Output + if (nagbar) { + free(nagbar->output.name); + nagbar->output.name = strdup(optarg); + } + break; + case 's': // Dismiss Button Text + if (nagbar) { + struct sway_nagbar_button *button_close; + button_close = nagbar->buttons->items[0]; + free(button_close->text); + button_close->text = strdup(optarg); + } + break; + case 't': // Type + if (nagbar) { + nagbar->type = nagbar_type_get(types, optarg); + if (!nagbar->type) { + fprintf(stderr, "Unknown type %s\n", optarg); + return EXIT_FAILURE; + } + } + break; + case 'v': // Version + fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); + return -1; + default: // Help or unknown flag + fprintf(c == 'h' ? stdout : stderr, "%s", usage); + return -1; + } + } + + return 0; +} + +static bool file_exists(const char *path) { + return path && access(path, R_OK) != -1; +} + +char *nagbar_get_config_path(void) { + static const char *config_paths[] = { + "$HOME/.swaynag/config", + "$XDG_CONFIG_HOME/swaynag/config", + SYSCONFDIR "/swaynag/config", + }; + + if (!getenv("XDG_CONFIG_HOME")) { + char *home = getenv("HOME"); + char *config_home = malloc(strlen(home) + strlen("/.config") + 1); + if (!config_home) { + wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); + } else { + strcpy(config_home, home); + strcat(config_home, "/.config"); + setenv("XDG_CONFIG_HOME", config_home, 1); + wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); + free(config_home); + } + } + + wordexp_t p; + char *path; + for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { + if (wordexp(config_paths[i], &p, 0) == 0) { + path = strdup(p.we_wordv[0]); + wordfree(&p); + if (file_exists(path)) { + return path; + } + free(path); + } + } + + return NULL; +} + +int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { + FILE *config = fopen(path, "r"); + if (!config) { + fprintf(stderr, "Failed to read config. Running without it.\n"); + return 0; + } + struct sway_nagbar_type *type = NULL; + char *line; + int line_number = 0; + while (!feof(config)) { + line = read_line(config); + if (!line) { + continue; + } + + line_number++; + if (line[0] == '#') { + free(line); + continue; + } + if (strlen(line) == 0) { + free(line); + continue; + } + + if (line[0] == '[') { + char *close = strchr(line, ']'); + if (!close) { + free(line); + fclose(config); + fprintf(stderr, "Closing bracket not found on line %d\n", + line_number); + return 1; + } + char *name = calloc(1, close - line); + strncat(name, line + 1, close - line - 1); + type = nagbar_type_get(types, name); + if (!type) { + type = calloc(1, sizeof(struct sway_nagbar_type)); + type->name = strdup(name); + list_add(types, type); + } + free(name); + } else { + char flag[strlen(line) + 3]; + sprintf(flag, "--%s", line); + char *argv[] = {"swaynag", flag}; + int result; + if (type) { + result = nagbar_parse_type(2, argv, type); + } else { + result = nagbar_parse_options(2, argv, nagbar, types, + NULL, NULL); + } + if (result != 0) { + free(line); + fclose(config); + return result; + } + } + + free(line); + } + fclose(config); + return 0; +} + diff --git a/swaynag/main.c b/swaynag/main.c index b199fac4..5c97e12a 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -1,12 +1,8 @@ -#define _XOPEN_SOURCE 700 -#define _POSIX_C_SOURCE 200112L -#include +#define _XOPEN_SOURCE 500 #include -#include -#include #include "log.h" #include "list.h" -#include "readline.h" +#include "swaynag/config.h" #include "swaynag/nagbar.h" #include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" @@ -23,285 +19,6 @@ void sway_terminate(int code) { exit(code); } -static char *read_from_stdin() { - char *buffer = NULL; - while (!feof(stdin)) { - char *line = read_line(stdin); - if (!line) { - continue; - } - - if (!buffer) { - buffer = strdup(line); - } else { - buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); - strcat(buffer, line); - strcat(buffer, "\n"); - } - - free(line); - } - - if (buffer && buffer[strlen(buffer) - 1] == '\n') { - buffer[strlen(buffer) - 1] = '\0'; - } - - return buffer; -} - -static int parse_options(int argc, char **argv, struct sway_nagbar *nagbar, - list_t *types, char **config, bool *debug) { - static struct option opts[] = { - {"button", required_argument, NULL, 'b'}, - {"config", required_argument, NULL, 'c'}, - {"debug", no_argument, NULL, 'd'}, - {"edge", required_argument, NULL, 'e'}, - {"font", required_argument, NULL, 'f'}, - {"help", no_argument, NULL, 'h'}, - {"detailed-message", no_argument, NULL, 'l'}, - {"detailed-button", required_argument, NULL, 'L'}, - {"message", required_argument, NULL, 'm'}, - {"output", required_argument, NULL, 'o'}, - {"dismiss-button", required_argument, NULL, 's'}, - {"type", required_argument, NULL, 't'}, - {"version", no_argument, NULL, 'v'}, - {0, 0, 0, 0} - }; - - const char *usage = - "Usage: swaynag [options...]\n" - "\n" - " -b, --button Create a button with text that " - "executes action when pressed. Multiple buttons can be defined.\n" - " -c, --config Path to config file.\n" - " -d, --debug Enable debugging.\n" - " -e, --edge top|bottom Set the edge to use.\n" - " -f, --font Set the font to use.\n" - " -h, --help Show help message and quit.\n" - " -l, --detailed-message Read a detailed message from stdin.\n" - " -L, --detailed-button Set the text of the detail button.\n" - " -m, --message Set the message text.\n" - " -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"; - - optind = 1; - while (1) { - int c = getopt_long(argc, argv, "b:c:de:f:hlL:m:o:s:t:v", opts, NULL); - if (c == -1) { - break; - } - switch (c) { - case 'b': // Button - if (nagbar) { - if (optind >= argc) { - fprintf(stderr, "Missing action for button %s\n", optarg); - return EXIT_FAILURE; - } - struct sway_nagbar_button *button; - button = calloc(sizeof(struct sway_nagbar_button), 1); - button->text = strdup(optarg); - button->type = NAGBAR_ACTION_COMMAND; - button->action = strdup(argv[optind]); - optind++; - list_add(nagbar->buttons, button); - } - break; - case 'c': // Config - if (config) { - *config = strdup(optarg); - } - break; - case 'd': // Debug - if (debug) { - *debug = true; - } - break; - case 'e': // Edge - if (nagbar) { - if (strcmp(optarg, "top") == 0) { - nagbar->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) { - nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM - | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT - | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - } else { - fprintf(stderr, "Invalid edge: %s\n", optarg); - return EXIT_FAILURE; - } - } - break; - case 'f': // Font - if (nagbar) { - free(nagbar->font); - nagbar->font = strdup(optarg); - } - break; - case 'l': // Detailed Message - if (nagbar) { - free(nagbar->details.message); - nagbar->details.message = read_from_stdin(); - nagbar->details.button_up.text = strdup("▲"); - nagbar->details.button_down.text = strdup("▼"); - } - break; - case 'L': // Detailed Button Text - if (nagbar) { - free(nagbar->details.button_details.text); - nagbar->details.button_details.text = strdup(optarg); - } - break; - case 'm': // Message - if (nagbar) { - free(nagbar->message); - nagbar->message = strdup(optarg); - } - break; - case 'o': // Output - if (nagbar) { - free(nagbar->output.name); - nagbar->output.name = strdup(optarg); - } - break; - case 's': // Dismiss Button Text - if (nagbar) { - struct sway_nagbar_button *button_close; - button_close = nagbar->buttons->items[0]; - free(button_close->text); - button_close->text = strdup(optarg); - } - break; - case 't': // Type - if (nagbar) { - nagbar->type = nagbar_type_get(types, optarg); - if (!nagbar->type) { - fprintf(stderr, "Unknown type %s\n", optarg); - return EXIT_FAILURE; - } - } - break; - case 'v': // Version - fprintf(stdout, "swaynag version " SWAY_VERSION "\n"); - return -1; - default: // Help or unknown flag - fprintf(c == 'h' ? stdout : stderr, "%s", usage); - return -1; - } - } - - return 0; -} - -static bool file_exists(const char *path) { - return path && access(path, R_OK) != -1; -} - -static char *get_config_path(void) { - static const char *config_paths[] = { - "$HOME/.swaynag/config", - "$XDG_CONFIG_HOME/swaynag/config", - SYSCONFDIR "/swaynag/config", - }; - - if (!getenv("XDG_CONFIG_HOME")) { - char *home = getenv("HOME"); - char *config_home = malloc(strlen(home) + strlen("/.config") + 1); - if (!config_home) { - wlr_log(WLR_ERROR, "Unable to allocate $HOME/.config"); - } else { - strcpy(config_home, home); - strcat(config_home, "/.config"); - setenv("XDG_CONFIG_HOME", config_home, 1); - wlr_log(WLR_DEBUG, "Set XDG_CONFIG_HOME to %s", config_home); - free(config_home); - } - } - - wordexp_t p; - char *path; - for (size_t i = 0; i < sizeof(config_paths) / sizeof(char *); ++i) { - if (wordexp(config_paths[i], &p, 0) == 0) { - path = strdup(p.we_wordv[0]); - wordfree(&p); - if (file_exists(path)) { - return path; - } - free(path); - } - } - - return NULL; -} - -static int load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { - FILE *config = fopen(path, "r"); - if (!config) { - fprintf(stderr, "Failed to read config. Running without it.\n"); - return 0; - } - struct sway_nagbar_type *type = NULL; - char *line; - int line_number = 0; - while (!feof(config)) { - line = read_line(config); - if (!line) { - continue; - } - - line_number++; - if (line[0] == '#') { - free(line); - continue; - } - if (strlen(line) == 0) { - free(line); - continue; - } - - if (line[0] == '[') { - char *close = strchr(line, ']'); - if (!close) { - free(line); - fclose(config); - fprintf(stderr, "Closing bracket not found on line %d\n", - line_number); - return 1; - } - char *name = calloc(1, close - line); - strncat(name, line + 1, close - line - 1); - type = nagbar_type_get(types, name); - if (!type) { - type = calloc(1, sizeof(struct sway_nagbar_type)); - type->name = strdup(name); - list_add(types, type); - } - free(name); - } else { - char flag[strlen(line) + 3]; - sprintf(flag, "--%s", line); - char *argv[] = {"swaynag", flag}; - int result; - if (type) { - result = nagbar_parse_type(2, argv, type); - } else { - result = parse_options(2, argv, nagbar, types, NULL, NULL); - } - if (result != 0) { - free(line); - fclose(config); - return result; - } - } - - free(line); - } - fclose(config); - return 0; -} - int main(int argc, char **argv) { int exit_code = EXIT_SUCCESS; @@ -327,7 +44,7 @@ int main(int argc, char **argv) { char *config_path = NULL; bool debug = false; - int launch_status = parse_options(argc, argv, NULL, NULL, + int launch_status = nagbar_parse_options(argc, argv, NULL, NULL, &config_path, &debug); if (launch_status != 0) { exit_code = launch_status; @@ -336,11 +53,11 @@ int main(int argc, char **argv) { wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); if (!config_path) { - config_path = get_config_path(); + config_path = nagbar_get_config_path(); } if (config_path) { wlr_log(WLR_DEBUG, "Loading config file: %s", config_path); - int config_status = load_config(config_path, &nagbar, types); + int config_status = nagbar_load_config(config_path, &nagbar, types); free(config_path); if (config_status != 0) { exit_code = config_status; @@ -349,7 +66,8 @@ int main(int argc, char **argv) { } if (argc > 1) { - int result = parse_options(argc, argv, &nagbar, types, NULL, NULL); + int result = nagbar_parse_options(argc, argv, &nagbar, types, + NULL, NULL); if (result != 0) { exit_code = result; goto cleanup; diff --git a/swaynag/meson.build b/swaynag/meson.build index 6a9df984..b2425504 100644 --- a/swaynag/meson.build +++ b/swaynag/meson.build @@ -1,5 +1,6 @@ executable( 'swaynag', [ + 'config.c', 'main.c', 'nagbar.c', 'render.c', diff --git a/swaynag/types.c b/swaynag/types.c index 8dd99d2a..dbc841f7 100644 --- a/swaynag/types.c +++ b/swaynag/types.c @@ -6,6 +6,7 @@ #include #include #include "list.h" +#include "swaynag/config.h" #include "swaynag/types.h" #include "util.h" -- cgit v1.2.3-54-g00ecf 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 --- include/swaynag/config.h | 12 +- include/swaynag/nagbar.h | 104 ------------ include/swaynag/render.h | 7 +- include/swaynag/swaynag.h | 104 ++++++++++++ include/swaynag/types.h | 18 +- swaynag/config.c | 80 ++++----- swaynag/main.c | 78 ++++----- swaynag/meson.build | 2 +- swaynag/nagbar.c | 412 --------------------------------------------- swaynag/render.c | 256 ++++++++++++++-------------- swaynag/swaynag.c | 413 ++++++++++++++++++++++++++++++++++++++++++++++ swaynag/types.c | 30 ++-- 12 files changed, 760 insertions(+), 756 deletions(-) delete mode 100644 include/swaynag/nagbar.h create mode 100644 include/swaynag/swaynag.h delete mode 100644 swaynag/nagbar.c create mode 100644 swaynag/swaynag.c diff --git a/include/swaynag/config.h b/include/swaynag/config.h index caa32710..3fd5b3ce 100644 --- a/include/swaynag/config.h +++ b/include/swaynag/config.h @@ -1,13 +1,13 @@ -#ifndef _SWAY_NAGBAR_CONFIG_H -#define _SWAY_NAGBAR_CONFIG_H -#include "swaynag/nagbar.h" +#ifndef _SWAYNAG_CONFIG_H +#define _SWAYNAG_CONFIG_H +#include "swaynag/swaynag.h" #include "list.h" -int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, +int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, list_t *types, char **config, bool *debug); -char *nagbar_get_config_path(void); +char *swaynag_get_config_path(void); -int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types); +int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types); #endif diff --git a/include/swaynag/nagbar.h b/include/swaynag/nagbar.h deleted file mode 100644 index 4ef101e9..00000000 --- a/include/swaynag/nagbar.h +++ /dev/null @@ -1,104 +0,0 @@ -#ifndef _SWAY_NAGBAR_NAGBAR_H -#define _SWAY_NAGBAR_NAGBAR_H -#include -#include -#include "list.h" -#include "pool-buffer.h" -#include "swaynag/types.h" -#include "xdg-output-unstable-v1-client-protocol.h" - -#define NAGBAR_BAR_BORDER_THICKNESS 2 -#define NAGBAR_MESSAGE_PADDING 8 -#define NAGBAR_DETAILS_BORDER_THICKNESS 3 -#define NAGBAR_BUTTON_BORDER_THICKNESS 3 -#define NAGBAR_BUTTON_GAP 20 -#define NAGBAR_BUTTON_GAP_CLOSE 15 -#define NAGBAR_BUTTON_MARGIN_RIGHT 2 -#define NAGBAR_BUTTON_PADDING 3 - -#define NAGBAR_MAX_HEIGHT 500 - -enum sway_nagbar_action_type { - NAGBAR_ACTION_DISMISS, - NAGBAR_ACTION_EXPAND, - NAGBAR_ACTION_COMMAND, -}; - -struct sway_nagbar_pointer { - struct wl_pointer *pointer; - struct wl_cursor_theme *cursor_theme; - struct wl_cursor_image *cursor_image; - struct wl_surface *cursor_surface; - int x; - int y; -}; - -struct sway_nagbar_output { - char *name; - struct wl_output *wl_output; - uint32_t wl_name; -}; - -struct sway_nagbar_button { - char *text; - enum sway_nagbar_action_type type; - char *action; - int x; - int y; - int width; - int height; -}; - -struct sway_nagbar_details { - bool visible; - char *message; - - int x; - int y; - int width; - int height; - - int offset; - int visible_lines; - int total_lines; - struct sway_nagbar_button button_details; - struct sway_nagbar_button button_up; - struct sway_nagbar_button button_down; -}; - -struct sway_nagbar { - bool run_display; - int querying_outputs; - - struct wl_display *display; - struct wl_compositor *compositor; - struct wl_seat *seat; - struct wl_shm *shm; - struct sway_nagbar_pointer pointer; - struct zxdg_output_manager_v1 *xdg_output_manager; - struct sway_nagbar_output output; - struct zwlr_layer_shell_v1 *layer_shell; - struct zwlr_layer_surface_v1 *layer_surface; - struct wl_surface *surface; - - uint32_t width; - uint32_t height; - int32_t scale; - struct pool_buffer buffers[2]; - struct pool_buffer *current_buffer; - - struct sway_nagbar_type *type; - uint32_t anchors; - char *message; - char *font; - list_t *buttons; - struct sway_nagbar_details details; -}; - -void nagbar_setup(struct sway_nagbar *nagbar); - -void nagbar_run(struct sway_nagbar *nagbar); - -void nagbar_destroy(struct sway_nagbar *nagbar); - -#endif diff --git a/include/swaynag/render.h b/include/swaynag/render.h index d9429f7f..d09e5929 100644 --- a/include/swaynag/render.h +++ b/include/swaynag/render.h @@ -1,6 +1,7 @@ -#ifndef _SWAY_NAGBAR_RENDER_H -#define _SWAY_NAGBAR_RENDER_H +#ifndef _SWAYNAG_RENDER_H +#define _SWAYNAG_RENDER_H +#include "swaynag/swaynag.h" -void render_frame(struct sway_nagbar *nagbar); +void render_frame(struct swaynag *swaynag); #endif diff --git a/include/swaynag/swaynag.h b/include/swaynag/swaynag.h new file mode 100644 index 00000000..6a56f14f --- /dev/null +++ b/include/swaynag/swaynag.h @@ -0,0 +1,104 @@ +#ifndef _SWAYNAG_SWAYNAG_H +#define _SWAYNAG_SWAYNAG_H +#include +#include +#include "list.h" +#include "pool-buffer.h" +#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 { + SWAYNAG_ACTION_DISMISS, + SWAYNAG_ACTION_EXPAND, + SWAYNAG_ACTION_COMMAND, +}; + +struct swaynag_pointer { + struct wl_pointer *pointer; + struct wl_cursor_theme *cursor_theme; + struct wl_cursor_image *cursor_image; + struct wl_surface *cursor_surface; + int x; + int y; +}; + +struct swaynag_output { + char *name; + struct wl_output *wl_output; + uint32_t wl_name; +}; + +struct swaynag_button { + char *text; + enum swaynag_action_type type; + char *action; + int x; + int y; + int width; + int height; +}; + +struct swaynag_details { + bool visible; + char *message; + + int x; + int y; + int width; + int height; + + int offset; + int visible_lines; + int total_lines; + struct swaynag_button button_details; + struct swaynag_button button_up; + struct swaynag_button button_down; +}; + +struct swaynag { + bool run_display; + int querying_outputs; + + struct wl_display *display; + struct wl_compositor *compositor; + struct wl_seat *seat; + struct wl_shm *shm; + struct swaynag_pointer pointer; + struct zxdg_output_manager_v1 *xdg_output_manager; + struct swaynag_output output; + struct zwlr_layer_shell_v1 *layer_shell; + struct zwlr_layer_surface_v1 *layer_surface; + struct wl_surface *surface; + + uint32_t width; + uint32_t height; + int32_t scale; + struct pool_buffer buffers[2]; + struct pool_buffer *current_buffer; + + struct swaynag_type *type; + uint32_t anchors; + char *message; + char *font; + list_t *buttons; + struct swaynag_details details; +}; + +void swaynag_setup(struct swaynag *swaynag); + +void swaynag_run(struct swaynag *swaynag); + +void swaynag_destroy(struct swaynag *swaynag); + +#endif diff --git a/include/swaynag/types.h b/include/swaynag/types.h index 32056514..af83bd83 100644 --- a/include/swaynag/types.h +++ b/include/swaynag/types.h @@ -1,7 +1,7 @@ -#ifndef _SWAY_NAGBAR_TYPES_H -#define _SWAY_NAGBAR_TYPES_H +#ifndef _SWAYNAG_TYPES_H +#define _SWAYNAG_TYPES_H -struct sway_nagbar_type { +struct swaynag_type { char *name; uint32_t button_background; uint32_t background; @@ -10,16 +10,16 @@ struct sway_nagbar_type { uint32_t border_bottom; }; -void nagbar_types_add_default(list_t *types); +void swaynag_types_add_default(list_t *types); -struct sway_nagbar_type *nagbar_type_get(list_t *types, char *name); +struct swaynag_type *swaynag_type_get(list_t *types, char *name); -struct sway_nagbar_type *nagbar_type_clone(struct sway_nagbar_type *type); +struct swaynag_type *swaynag_type_clone(struct swaynag_type *type); -void nagbar_type_free(struct sway_nagbar_type *type); +void swaynag_type_free(struct swaynag_type *type); -void nagbar_types_free(list_t *types); +void swaynag_types_free(list_t *types); -int nagbar_parse_type(int argc, char **argv, struct sway_nagbar_type *type); +int swaynag_parse_type(int argc, char **argv, struct swaynag_type *type); #endif diff --git a/swaynag/config.c b/swaynag/config.c index 4e7edf2e..289fc82a 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -6,7 +6,7 @@ #include "log.h" #include "list.h" #include "readline.h" -#include "swaynag/nagbar.h" +#include "swaynag/swaynag.h" #include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" @@ -36,7 +36,7 @@ static char *read_from_stdin() { return buffer; } -int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, +int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag, list_t *types, char **config, bool *debug) { static struct option opts[] = { {"button", required_argument, NULL, 'b'}, @@ -81,17 +81,17 @@ int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, } switch (c) { case 'b': // Button - if (nagbar) { + if (swaynag) { if (optind >= argc) { fprintf(stderr, "Missing action for button %s\n", optarg); return EXIT_FAILURE; } - struct sway_nagbar_button *button; - button = calloc(sizeof(struct sway_nagbar_button), 1); + struct swaynag_button *button; + button = calloc(sizeof(struct swaynag_button), 1); button->text = strdup(optarg); - button->type = NAGBAR_ACTION_COMMAND; + button->type = SWAYNAG_ACTION_COMMAND; button->action = strdup(argv[optind]); - list_add(nagbar->buttons, button); + list_add(swaynag->buttons, button); } optind++; break; @@ -106,13 +106,13 @@ int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, } break; case 'e': // Edge - if (nagbar) { + if (swaynag) { if (strcmp(optarg, "top") == 0) { - nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + swaynag->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) { - nagbar->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM + swaynag->anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; } else { @@ -122,49 +122,49 @@ int nagbar_parse_options(int argc, char **argv, struct sway_nagbar *nagbar, } break; case 'f': // Font - if (nagbar) { - free(nagbar->font); - nagbar->font = strdup(optarg); + if (swaynag) { + free(swaynag->font); + swaynag->font = strdup(optarg); } break; case 'l': // Detailed Message - if (nagbar) { - free(nagbar->details.message); - nagbar->details.message = read_from_stdin(); - nagbar->details.button_up.text = strdup("▲"); - nagbar->details.button_down.text = strdup("▼"); + if (swaynag) { + free(swaynag->details.message); + swaynag->details.message = read_from_stdin(); + swaynag->details.button_up.text = strdup("▲"); + swaynag->details.button_down.text = strdup("▼"); } break; case 'L': // Detailed Button Text - if (nagbar) { - free(nagbar->details.button_details.text); - nagbar->details.button_details.text = strdup(optarg); + if (swaynag) { + free(swaynag->details.button_details.text); + swaynag->details.button_details.text = strdup(optarg); } break; case 'm': // Message - if (nagbar) { - free(nagbar->message); - nagbar->message = strdup(optarg); + if (swaynag) { + free(swaynag->message); + swaynag->message = strdup(optarg); } break; case 'o': // Output - if (nagbar) { - free(nagbar->output.name); - nagbar->output.name = strdup(optarg); + if (swaynag) { + free(swaynag->output.name); + swaynag->output.name = strdup(optarg); } break; case 's': // Dismiss Button Text - if (nagbar) { - struct sway_nagbar_button *button_close; - button_close = nagbar->buttons->items[0]; + if (swaynag) { + struct swaynag_button *button_close; + button_close = swaynag->buttons->items[0]; free(button_close->text); button_close->text = strdup(optarg); } break; case 't': // Type - if (nagbar) { - nagbar->type = nagbar_type_get(types, optarg); - if (!nagbar->type) { + if (swaynag) { + swaynag->type = swaynag_type_get(types, optarg); + if (!swaynag->type) { fprintf(stderr, "Unknown type %s\n", optarg); return EXIT_FAILURE; } @@ -186,7 +186,7 @@ static bool file_exists(const char *path) { return path && access(path, R_OK) != -1; } -char *nagbar_get_config_path(void) { +char *swaynag_get_config_path(void) { static const char *config_paths[] = { "$HOME/.swaynag/config", "$XDG_CONFIG_HOME/swaynag/config", @@ -223,13 +223,13 @@ char *nagbar_get_config_path(void) { return NULL; } -int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { +int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types) { FILE *config = fopen(path, "r"); if (!config) { fprintf(stderr, "Failed to read config. Running without it.\n"); return 0; } - struct sway_nagbar_type *type = NULL; + struct swaynag_type *type = NULL; char *line; int line_number = 0; while (!feof(config)) { @@ -259,9 +259,9 @@ int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { } char *name = calloc(1, close - line); strncat(name, line + 1, close - line - 1); - type = nagbar_type_get(types, name); + type = swaynag_type_get(types, name); if (!type) { - type = calloc(1, sizeof(struct sway_nagbar_type)); + type = calloc(1, sizeof(struct swaynag_type)); type->name = strdup(name); list_add(types, type); } @@ -272,9 +272,9 @@ int nagbar_load_config(char *path, struct sway_nagbar *nagbar, list_t *types) { char *argv[] = {"swaynag", flag}; int result; if (type) { - result = nagbar_parse_type(2, argv, type); + result = swaynag_parse_type(2, argv, type); } else { - result = nagbar_parse_options(2, argv, nagbar, types, + result = swaynag_parse_options(2, argv, swaynag, types, NULL, NULL); } if (result != 0) { diff --git a/swaynag/main.c b/swaynag/main.c index 5c97e12a..0493c1f0 100644 --- a/swaynag/main.c +++ b/swaynag/main.c @@ -3,19 +3,19 @@ #include "log.h" #include "list.h" #include "swaynag/config.h" -#include "swaynag/nagbar.h" +#include "swaynag/swaynag.h" #include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" -static struct sway_nagbar nagbar; +static struct swaynag swaynag; void sig_handler(int signal) { - nagbar_destroy(&nagbar); + swaynag_destroy(&swaynag); exit(EXIT_FAILURE); } void sway_terminate(int code) { - nagbar_destroy(&nagbar); + swaynag_destroy(&swaynag); exit(code); } @@ -23,28 +23,28 @@ int main(int argc, char **argv) { int exit_code = EXIT_SUCCESS; list_t *types = create_list(); - nagbar_types_add_default(types); + swaynag_types_add_default(types); - memset(&nagbar, 0, sizeof(nagbar)); - nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP + 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; - nagbar.font = strdup("pango:monospace 10"); - nagbar.buttons = create_list(); + swaynag.font = strdup("pango:monospace 10"); + swaynag.buttons = create_list(); - struct sway_nagbar_button *button_close = - calloc(sizeof(struct sway_nagbar_button), 1); + struct swaynag_button *button_close = + calloc(sizeof(struct swaynag_button), 1); button_close->text = strdup("X"); - button_close->type = NAGBAR_ACTION_DISMISS; - list_add(nagbar.buttons, button_close); + button_close->type = SWAYNAG_ACTION_DISMISS; + list_add(swaynag.buttons, button_close); - nagbar.details.button_details.text = strdup("Toggle Details"); - nagbar.details.button_details.type = NAGBAR_ACTION_EXPAND; + swaynag.details.button_details.text = strdup("Toggle Details"); + swaynag.details.button_details.type = SWAYNAG_ACTION_EXPAND; char *config_path = NULL; bool debug = false; - int launch_status = nagbar_parse_options(argc, argv, NULL, NULL, + int launch_status = swaynag_parse_options(argc, argv, NULL, NULL, &config_path, &debug); if (launch_status != 0) { exit_code = launch_status; @@ -53,11 +53,11 @@ int main(int argc, char **argv) { wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL); if (!config_path) { - config_path = nagbar_get_config_path(); + config_path = swaynag_get_config_path(); } if (config_path) { wlr_log(WLR_DEBUG, "Loading config file: %s", config_path); - int config_status = nagbar_load_config(config_path, &nagbar, types); + int config_status = swaynag_load_config(config_path, &swaynag, types); free(config_path); if (config_status != 0) { exit_code = config_status; @@ -66,7 +66,7 @@ int main(int argc, char **argv) { } if (argc > 1) { - int result = nagbar_parse_options(argc, argv, &nagbar, types, + int result = swaynag_parse_options(argc, argv, &swaynag, types, NULL, NULL); if (result != 0) { exit_code = result; @@ -74,46 +74,46 @@ int main(int argc, char **argv) { } } - if (!nagbar.message) { + if (!swaynag.message) { wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m"); exit_code = EXIT_FAILURE; goto cleanup; } - if (!nagbar.type) { - nagbar.type = nagbar_type_get(types, "error"); + if (!swaynag.type) { + swaynag.type = swaynag_type_get(types, "error"); } - nagbar.type = nagbar_type_clone(nagbar.type); - nagbar_types_free(types); + swaynag.type = swaynag_type_clone(swaynag.type); + swaynag_types_free(types); - if (nagbar.details.message) { - list_add(nagbar.buttons, &nagbar.details.button_details); + if (swaynag.details.message) { + list_add(swaynag.buttons, &swaynag.details.button_details); } else { - free(nagbar.details.button_details.text); + free(swaynag.details.button_details.text); } - wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name); - wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors); - wlr_log(WLR_DEBUG, "Type: %s", nagbar.type->name); - wlr_log(WLR_DEBUG, "Message: %s", nagbar.message); - wlr_log(WLR_DEBUG, "Font: %s", nagbar.font); + wlr_log(WLR_DEBUG, "Output: %s", swaynag.output.name); + wlr_log(WLR_DEBUG, "Anchors: %d", swaynag.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, "Buttons"); - for (int i = 0; i < nagbar.buttons->length; i++) { - struct sway_nagbar_button *button = nagbar.buttons->items[i]; + for (int i = 0; i < swaynag.buttons->length; i++) { + struct swaynag_button *button = swaynag.buttons->items[i]; wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action); } signal(SIGTERM, sig_handler); - nagbar_setup(&nagbar); - nagbar_run(&nagbar); + swaynag_setup(&swaynag); + swaynag_run(&swaynag); return exit_code; cleanup: - nagbar_types_free(types); - free(nagbar.details.button_details.text); - nagbar_destroy(&nagbar); + swaynag_types_free(types); + free(swaynag.details.button_details.text); + swaynag_destroy(&swaynag); return exit_code; } diff --git a/swaynag/meson.build b/swaynag/meson.build index b2425504..f67c10ff 100644 --- a/swaynag/meson.build +++ b/swaynag/meson.build @@ -2,8 +2,8 @@ executable( 'swaynag', [ 'config.c', 'main.c', - 'nagbar.c', 'render.c', + 'swaynag.c', 'types.c', ], include_directories: [sway_inc], diff --git a/swaynag/nagbar.c b/swaynag/nagbar.c deleted file mode 100644 index e451a53a..00000000 --- a/swaynag/nagbar.c +++ /dev/null @@ -1,412 +0,0 @@ -#define _XOPEN_SOURCE 500 -#include -#include -#include -#include -#include -#include "log.h" -#include "list.h" -#include "swaynag/nagbar.h" -#include "swaynag/render.h" -#include "swaynag/types.h" -#include "wlr-layer-shell-unstable-v1-client-protocol.h" - -static void nop() { - // Intentionally left blank -} - -static bool terminal_execute(char *terminal, char *command) { - char fname[] = "/tmp/swaynagXXXXXX"; - FILE *tmp= fdopen(mkstemp(fname), "w"); - if (!tmp) { - wlr_log(WLR_ERROR, "Failed to create temp script"); - return false; - } - wlr_log(WLR_DEBUG, "Created temp script: %s", fname); - fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command); - fclose(tmp); - chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR); - char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1]; - sprintf(cmd, "%s -e %s", terminal, fname); - execl("/bin/sh", "/bin/sh", "-c", cmd, NULL); - return true; -} - -static void nagbar_button_execute(struct sway_nagbar *nagbar, - struct sway_nagbar_button *button) { - wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action); - if (button->type == NAGBAR_ACTION_DISMISS) { - nagbar->run_display = false; - } else if (button->type == NAGBAR_ACTION_EXPAND) { - nagbar->details.visible = !nagbar->details.visible; - render_frame(nagbar); - } else { - if (fork() == 0) { - // Child process. Will be used to prevent zombie processes - setsid(); - if (fork() == 0) { - // Child of the child. Will be reparented to the init process - char *terminal = getenv("TERMINAL"); - if (terminal && strlen(terminal)) { - wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal); - if (!terminal_execute(terminal, button->action)) { - nagbar_destroy(nagbar); - exit(EXIT_FAILURE); - } - } else { - wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly"); - execl("/bin/sh", "/bin/sh", "-c", button->action, NULL); - } - } - exit(EXIT_SUCCESS); - } - } - wait(0); -} - -static void layer_surface_configure(void *data, - struct zwlr_layer_surface_v1 *surface, - uint32_t serial, uint32_t width, uint32_t height) { - struct sway_nagbar *nagbar = data; - nagbar->width = width; - nagbar->height = height; - zwlr_layer_surface_v1_ack_configure(surface, serial); - render_frame(nagbar); -} - -static void layer_surface_closed(void *data, - struct zwlr_layer_surface_v1 *surface) { - struct sway_nagbar *nagbar = data; - nagbar_destroy(nagbar); -} - -static struct zwlr_layer_surface_v1_listener layer_surface_listener = { - .configure = layer_surface_configure, - .closed = layer_surface_closed, -}; - -static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer, - uint32_t serial, struct wl_surface *surface, - wl_fixed_t surface_x, wl_fixed_t surface_y) { - struct sway_nagbar *nagbar = data; - struct sway_nagbar_pointer *pointer = &nagbar->pointer; - wl_surface_set_buffer_scale(pointer->cursor_surface, nagbar->scale); - wl_surface_attach(pointer->cursor_surface, - wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0); - wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface, - pointer->cursor_image->hotspot_x / nagbar->scale, - pointer->cursor_image->hotspot_y / nagbar->scale); - wl_surface_commit(pointer->cursor_surface); -} - -static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, - uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) { - struct sway_nagbar *nagbar = data; - nagbar->pointer.x = wl_fixed_to_int(surface_x); - nagbar->pointer.y = wl_fixed_to_int(surface_y); -} - -static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer, - uint32_t serial, uint32_t time, uint32_t button, uint32_t state) { - struct sway_nagbar *nagbar = data; - - if (state != WL_POINTER_BUTTON_STATE_PRESSED) { - return; - } - - double x = nagbar->pointer.x * nagbar->scale; - double y = nagbar->pointer.y * nagbar->scale; - for (int i = 0; i < nagbar->buttons->length; i++) { - struct sway_nagbar_button *nagbutton = nagbar->buttons->items[i]; - if (x >= nagbutton->x - && y >= nagbutton->y - && x < nagbutton->x + nagbutton->width - && y < nagbutton->y + nagbutton->height) { - nagbar_button_execute(nagbar, nagbutton); - return; - } - } - - if (nagbar->details.visible && - nagbar->details.total_lines != nagbar->details.visible_lines) { - struct sway_nagbar_button button_up = nagbar->details.button_up; - if (x >= button_up.x - && y >= button_up.y - && x < button_up.x + button_up.width - && y < button_up.y + button_up.height - && nagbar->details.offset > 0) { - nagbar->details.offset--; - render_frame(nagbar); - return; - } - - struct sway_nagbar_button button_down = nagbar->details.button_down; - int bot = nagbar->details.total_lines - nagbar->details.visible_lines; - if (x >= button_down.x - && y >= button_down.y - && x < button_down.x + button_down.width - && y < button_down.y + button_down.height - && nagbar->details.offset < bot) { - nagbar->details.offset++; - render_frame(nagbar); - return; - } - } -} - -static void wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, - uint32_t time, uint32_t axis, wl_fixed_t value) { - struct sway_nagbar *nagbar = data; - if (!nagbar->details.visible - || nagbar->pointer.x < nagbar->details.x - || nagbar->pointer.y < nagbar->details.y - || nagbar->pointer.x >= nagbar->details.x + nagbar->details.width - || nagbar->pointer.y >= nagbar->details.y + nagbar->details.height - || nagbar->details.total_lines == nagbar->details.visible_lines) { - return; - } - - int direction = wl_fixed_to_int(value); - int bot = nagbar->details.total_lines - nagbar->details.visible_lines; - if (direction < 0 && nagbar->details.offset > 0) { - nagbar->details.offset--; - } else if (direction > 0 && nagbar->details.offset < bot) { - nagbar->details.offset++; - } - - render_frame(nagbar); -} - -static struct wl_pointer_listener pointer_listener = { - .enter = wl_pointer_enter, - .leave = nop, - .motion = wl_pointer_motion, - .button = wl_pointer_button, - .axis = wl_pointer_axis, - .frame = nop, - .axis_source = nop, - .axis_stop = nop, - .axis_discrete = nop, -}; - -static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat, - enum wl_seat_capability caps) { - struct sway_nagbar *nagbar = data; - if ((caps & WL_SEAT_CAPABILITY_POINTER)) { - nagbar->pointer.pointer = wl_seat_get_pointer(wl_seat); - wl_pointer_add_listener(nagbar->pointer.pointer, &pointer_listener, - nagbar); - } -} - -const struct wl_seat_listener seat_listener = { - .capabilities = seat_handle_capabilities, - .name = nop, -}; - -static void output_scale(void *data, struct wl_output *output, - int32_t factor) { - struct sway_nagbar *nagbar = data; - nagbar->scale = factor; - render_frame(nagbar); -} - -static struct wl_output_listener output_listener = { - .geometry = nop, - .mode = nop, - .done = nop, - .scale = output_scale, -}; - -struct output_state { - struct wl_output *wl_output; - uint32_t wl_name; - struct zxdg_output_v1 *xdg_output; - struct sway_nagbar *nagbar; -}; - -static void xdg_output_handle_name(void *data, - struct zxdg_output_v1 *xdg_output, const char *name) { - struct output_state *state = data; - char *outname = state->nagbar->output.name; - wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname); - if (outname && !state->nagbar->output.wl_output) { - wlr_log(WLR_DEBUG, "Using output %s", name); - state->nagbar->output.wl_output = state->wl_output; - state->nagbar->output.wl_name = state->wl_name; - wl_output_add_listener(state->nagbar->output.wl_output, - &output_listener, state->nagbar); - wl_display_roundtrip(state->nagbar->display); - zxdg_output_v1_destroy(state->xdg_output); - } else { - zxdg_output_v1_destroy(state->xdg_output); - wl_output_destroy(state->wl_output); - } - state->nagbar->querying_outputs--; - free(state); -} - -static struct zxdg_output_v1_listener xdg_output_listener = { - .logical_position = nop, - .logical_size = nop, - .done = nop, - .name = xdg_output_handle_name, - .description = nop, -}; - -static void handle_global(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, uint32_t version) { - struct sway_nagbar *nagbar = data; - if (strcmp(interface, wl_compositor_interface.name) == 0) { - nagbar->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 3); - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - nagbar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); - wl_seat_add_listener(nagbar->seat, &seat_listener, nagbar); - } else if (strcmp(interface, wl_shm_interface.name) == 0) { - nagbar->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); - } else if (strcmp(interface, wl_output_interface.name) == 0) { - if (!nagbar->output.wl_output && nagbar->xdg_output_manager) { - nagbar->querying_outputs++; - struct output_state *state = - calloc(1, sizeof(struct output_state)); - state->nagbar = nagbar; - state->wl_output = wl_registry_bind(registry, name, - &wl_output_interface, 3); - state->wl_name = name; - state->xdg_output = zxdg_output_manager_v1_get_xdg_output( - nagbar->xdg_output_manager, state->wl_output); - zxdg_output_v1_add_listener(state->xdg_output, - &xdg_output_listener, state); - } - } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - nagbar->layer_shell = wl_registry_bind( - registry, name, &zwlr_layer_shell_v1_interface, 1); - } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0 - && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) { - nagbar->xdg_output_manager = wl_registry_bind(registry, name, - &zxdg_output_manager_v1_interface, - ZXDG_OUTPUT_V1_NAME_SINCE_VERSION); - } -} - -static void handle_global_remove(void *data, struct wl_registry *registry, - uint32_t name) { - struct sway_nagbar *nagbar = data; - if (nagbar->output.wl_name == name) { - nagbar->run_display = false; - } -} - -static const struct wl_registry_listener registry_listener = { - .global = handle_global, - .global_remove = handle_global_remove, -}; - -void nagbar_setup(struct sway_nagbar *nagbar) { - nagbar->display = wl_display_connect(NULL); - assert(nagbar->display); - - nagbar->scale = 1; - - struct wl_registry *registry = wl_display_get_registry(nagbar->display); - wl_registry_add_listener(registry, ®istry_listener, nagbar); - wl_display_roundtrip(nagbar->display); - assert(nagbar->compositor && nagbar->layer_shell && nagbar->shm); - - while (nagbar->querying_outputs > 0) { - wl_display_roundtrip(nagbar->display); - } - - if (!nagbar->output.wl_output && nagbar->output.name) { - wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name); - nagbar_destroy(nagbar); - exit(EXIT_FAILURE); - } - - struct sway_nagbar_pointer *pointer = &nagbar->pointer; - int scale = nagbar->scale < 1 ? 1 : nagbar->scale; - pointer->cursor_theme = wl_cursor_theme_load( - NULL, 24 * scale, nagbar->shm); - assert(pointer->cursor_theme); - struct wl_cursor *cursor = - wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr"); - assert(cursor); - pointer->cursor_image = cursor->images[0]; - pointer->cursor_surface = wl_compositor_create_surface(nagbar->compositor); - assert(pointer->cursor_surface); - - nagbar->surface = wl_compositor_create_surface(nagbar->compositor); - assert(nagbar->surface); - nagbar->layer_surface = zwlr_layer_shell_v1_get_layer_surface( - nagbar->layer_shell, nagbar->surface, nagbar->output.wl_output, - ZWLR_LAYER_SHELL_V1_LAYER_TOP, "swaynag"); - assert(nagbar->layer_surface); - zwlr_layer_surface_v1_add_listener(nagbar->layer_surface, - &layer_surface_listener, nagbar); - zwlr_layer_surface_v1_set_anchor(nagbar->layer_surface, nagbar->anchors); - - wl_registry_destroy(registry); -} - -void nagbar_run(struct sway_nagbar *nagbar) { - nagbar->run_display = true; - render_frame(nagbar); - while (nagbar->run_display && wl_display_dispatch(nagbar->display) != -1) { - // This is intentionally left blank - } -} - -void nagbar_destroy(struct sway_nagbar *nagbar) { - nagbar->run_display = false; - - free(nagbar->message); - free(nagbar->font); - while (nagbar->buttons->length) { - struct sway_nagbar_button *button = nagbar->buttons->items[0]; - list_del(nagbar->buttons, 0); - free(button->text); - free(button->action); - free(button); - } - list_free(nagbar->buttons); - free(nagbar->details.message); - free(nagbar->details.button_up.text); - free(nagbar->details.button_down.text); - - if (nagbar->type) { - nagbar_type_free(nagbar->type); - } - - if (nagbar->layer_surface) { - zwlr_layer_surface_v1_destroy(nagbar->layer_surface); - } - - if (nagbar->surface) { - wl_surface_destroy(nagbar->surface); - } - - if (nagbar->output.wl_output) { - wl_output_destroy(nagbar->output.wl_output); - } - - if (&nagbar->buffers[0]) { - destroy_buffer(&nagbar->buffers[0]); - } - - if (&nagbar->buffers[1]) { - destroy_buffer(&nagbar->buffers[1]); - } - - if (nagbar->compositor) { - wl_compositor_destroy(nagbar->compositor); - } - - if (nagbar->shm) { - wl_shm_destroy(nagbar->shm); - } - - if (nagbar->display) { - wl_display_disconnect(nagbar->display); - } -} diff --git a/swaynag/render.c b/swaynag/render.c index ef0f72a8..67e26eaf 100644 --- a/swaynag/render.c +++ b/swaynag/render.c @@ -3,99 +3,100 @@ #include "log.h" #include "pango.h" #include "pool-buffer.h" -#include "swaynag/nagbar.h" +#include "swaynag/swaynag.h" #include "swaynag/types.h" #include "wlr-layer-shell-unstable-v1-client-protocol.h" -static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) { - uint32_t height = nagbar->height * nagbar->scale; - height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; +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; int text_width, text_height; - get_text_size(cairo, nagbar->font, &text_width, &text_height, - nagbar->scale, true, "%s", nagbar->message); + get_text_size(cairo, swaynag->font, &text_width, &text_height, + swaynag->scale, true, "%s", swaynag->message); - int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; + int padding = SWAYNAG_MESSAGE_PADDING * swaynag->scale; uint32_t ideal_height = text_height + padding * 2; - uint32_t ideal_surface_height = ideal_height / nagbar->scale; - if (nagbar->height < ideal_surface_height) { + uint32_t ideal_surface_height = ideal_height / swaynag->scale; + if (swaynag->height < ideal_surface_height) { return ideal_surface_height; } - cairo_set_source_u32(cairo, nagbar->type->text); + cairo_set_source_u32(cairo, swaynag->type->text); cairo_move_to(cairo, padding, (int)(ideal_height - text_height) / 2); - pango_printf(cairo, nagbar->font, nagbar->scale, false, "%s", - nagbar->message); + pango_printf(cairo, swaynag->font, swaynag->scale, false, "%s", + swaynag->message); return ideal_height; } static void render_details_scroll_button(cairo_t *cairo, - struct sway_nagbar *nagbar, struct sway_nagbar_button *button) { + struct swaynag *swaynag, struct swaynag_button *button) { int text_width, text_height; - get_text_size(cairo, nagbar->font, &text_width, &text_height, - nagbar->scale, true, "%s", button->text); + get_text_size(cairo, swaynag->font, &text_width, &text_height, + swaynag->scale, true, "%s", button->text); - int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + int border = SWAYNAG_BUTTON_BORDER_THICKNESS * swaynag->scale; + int padding = SWAYNAG_BUTTON_PADDING * swaynag->scale; - cairo_set_source_u32(cairo, nagbar->type->border); + cairo_set_source_u32(cairo, swaynag->type->border); cairo_rectangle(cairo, button->x, button->y, button->width, button->height); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->type->button_background); + cairo_set_source_u32(cairo, swaynag->type->button_background); cairo_rectangle(cairo, button->x + border, button->y + border, button->width - (border * 2), button->height - (border * 2)); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->type->text); + 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, nagbar->font, nagbar->scale, true, "%s", button->text); + pango_printf(cairo, swaynag->font, swaynag->scale, true, + "%s", button->text); } static int get_detailed_scroll_button_width(cairo_t *cairo, - struct sway_nagbar *nagbar) { + struct swaynag *swaynag) { int up_width, down_width, temp_height; - get_text_size(cairo, nagbar->font, &up_width, &temp_height, - nagbar->scale, true, "%s", nagbar->details.button_up.text); - get_text_size(cairo, nagbar->font, &down_width, &temp_height, - nagbar->scale, true, "%s", nagbar->details.button_down.text); + get_text_size(cairo, swaynag->font, &up_width, &temp_height, + swaynag->scale, true, "%s", swaynag->details.button_up.text); + get_text_size(cairo, swaynag->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 = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + int border = SWAYNAG_BUTTON_BORDER_THICKNESS * swaynag->scale; + int padding = SWAYNAG_BUTTON_PADDING * swaynag->scale; return text_width + border * 2 + padding * 2; } -static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, +static uint32_t render_detailed(cairo_t *cairo, struct swaynag *swaynag, uint32_t y) { - uint32_t width = nagbar->width * nagbar->scale; - uint32_t height = nagbar->height * nagbar->scale; - height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; + uint32_t width = swaynag->width * swaynag->scale; + uint32_t height = swaynag->height * swaynag->scale; + height -= SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; - int border = NAGBAR_DETAILS_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale; + int border = SWAYNAG_DETAILS_BORDER_THICKNESS * swaynag->scale; + int padding = SWAYNAG_MESSAGE_PADDING * swaynag->scale; int decor = padding + border; - nagbar->details.x = decor; - nagbar->details.y = y + decor; - nagbar->details.width = width - decor * 2; + swaynag->details.x = decor; + swaynag->details.y = y + decor; + swaynag->details.width = width - decor * 2; - PangoLayout *layout = get_pango_layout(cairo, nagbar->font, - nagbar->details.message, nagbar->scale, false); + PangoLayout *layout = get_pango_layout(cairo, swaynag->font, + swaynag->details.message, swaynag->scale, false); pango_layout_set_width(layout, - (nagbar->details.width - padding * 2) * PANGO_SCALE); + (swaynag->details.width - padding * 2) * PANGO_SCALE); pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR); pango_layout_set_single_paragraph_mode(layout, false); pango_cairo_update_layout(cairo, layout); - nagbar->details.total_lines = pango_layout_get_line_count(layout); + swaynag->details.total_lines = pango_layout_get_line_count(layout); PangoLayoutLine *line; - line = pango_layout_get_line_readonly(layout, nagbar->details.offset); + line = pango_layout_get_line_readonly(layout, swaynag->details.offset); gint offset = line->start_index; const char *text = pango_layout_get_text(layout); pango_layout_set_text(layout, text + offset, strlen(text) - offset); @@ -104,87 +105,87 @@ static uint32_t render_detailed(cairo_t *cairo, struct sway_nagbar *nagbar, pango_cairo_update_layout(cairo, layout); pango_layout_get_pixel_size(layout, &text_width, &text_height); - bool show_buttons = nagbar->details.offset > 0; - int button_width = get_detailed_scroll_button_width(cairo, nagbar); + bool show_buttons = swaynag->details.offset > 0; + int button_width = get_detailed_scroll_button_width(cairo, swaynag); if (show_buttons) { - nagbar->details.width -= button_width; + swaynag->details.width -= button_width; pango_layout_set_width(layout, - (nagbar->details.width - padding * 2) * PANGO_SCALE); + (swaynag->details.width - padding * 2) * PANGO_SCALE); } uint32_t ideal_height; do { - ideal_height = nagbar->details.y + text_height + decor + padding * 2; - if (ideal_height > NAGBAR_MAX_HEIGHT) { - ideal_height = NAGBAR_MAX_HEIGHT; + ideal_height = swaynag->details.y + text_height + decor + padding * 2; + if (ideal_height > SWAYNAG_MAX_HEIGHT) { + ideal_height = SWAYNAG_MAX_HEIGHT; if (!show_buttons) { show_buttons = true; - nagbar->details.width -= button_width; + swaynag->details.width -= button_width; pango_layout_set_width(layout, - (nagbar->details.width - padding * 2) * PANGO_SCALE); + (swaynag->details.width - padding * 2) * PANGO_SCALE); } } - nagbar->details.height = ideal_height - nagbar->details.y - decor; + swaynag->details.height = ideal_height - swaynag->details.y - decor; pango_layout_set_height(layout, - (nagbar->details.height - padding * 2) * PANGO_SCALE); + (swaynag->details.height - padding * 2) * PANGO_SCALE); pango_layout_set_ellipsize(layout, PANGO_ELLIPSIZE_END); pango_cairo_update_layout(cairo, layout); pango_layout_get_pixel_size(layout, &text_width, &text_height); - } while (text_height != (nagbar->details.height - padding * 2)); + } while (text_height != (swaynag->details.height - padding * 2)); - nagbar->details.visible_lines = pango_layout_get_line_count(layout); + swaynag->details.visible_lines = pango_layout_get_line_count(layout); if (show_buttons) { - nagbar->details.button_up.x = - nagbar->details.x + nagbar->details.width; - nagbar->details.button_up.y = nagbar->details.y; - nagbar->details.button_up.width = button_width; - nagbar->details.button_up.height = nagbar->details.height / 2; - render_details_scroll_button(cairo, nagbar, - &nagbar->details.button_up); - - nagbar->details.button_down.x = - nagbar->details.x + nagbar->details.width; - nagbar->details.button_down.y = - nagbar->details.button_up.y + nagbar->details.button_up.height; - nagbar->details.button_down.width = button_width; - nagbar->details.button_down.height = nagbar->details.height / 2; - render_details_scroll_button(cairo, nagbar, - &nagbar->details.button_down); + swaynag->details.button_up.x = + swaynag->details.x + swaynag->details.width; + swaynag->details.button_up.y = swaynag->details.y; + swaynag->details.button_up.width = button_width; + swaynag->details.button_up.height = swaynag->details.height / 2; + render_details_scroll_button(cairo, swaynag, + &swaynag->details.button_up); + + swaynag->details.button_down.x = + swaynag->details.x + swaynag->details.width; + swaynag->details.button_down.y = + swaynag->details.button_up.y + swaynag->details.button_up.height; + swaynag->details.button_down.width = button_width; + swaynag->details.button_down.height = swaynag->details.height / 2; + render_details_scroll_button(cairo, swaynag, + &swaynag->details.button_down); } - cairo_set_source_u32(cairo, nagbar->type->border); - cairo_rectangle(cairo, nagbar->details.x, nagbar->details.y, - nagbar->details.width, nagbar->details.height); + cairo_set_source_u32(cairo, swaynag->type->border); + cairo_rectangle(cairo, swaynag->details.x, swaynag->details.y, + swaynag->details.width, swaynag->details.height); cairo_fill(cairo); - cairo_move_to(cairo, nagbar->details.x + padding, - nagbar->details.y + padding); - cairo_set_source_u32(cairo, nagbar->type->text); + cairo_move_to(cairo, swaynag->details.x + padding, + swaynag->details.y + padding); + cairo_set_source_u32(cairo, swaynag->type->text); pango_cairo_show_layout(cairo, layout); g_object_unref(layout); return ideal_height; } -static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, +static uint32_t render_button(cairo_t *cairo, struct swaynag *swaynag, int button_index, int *x) { - uint32_t height = nagbar->height * nagbar->scale; - height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; - struct sway_nagbar_button *button = nagbar->buttons->items[button_index]; + uint32_t height = swaynag->height * swaynag->scale; + height -= SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; + struct swaynag_button *button = swaynag->buttons->items[button_index]; int text_width, text_height; - get_text_size(cairo, nagbar->font, &text_width, &text_height, - nagbar->scale, true, "%s", button->text); + get_text_size(cairo, swaynag->font, &text_width, &text_height, + swaynag->scale, true, "%s", button->text); - int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale; - int padding = NAGBAR_BUTTON_PADDING * nagbar->scale; + int border = SWAYNAG_BUTTON_BORDER_THICKNESS * swaynag->scale; + int padding = SWAYNAG_BUTTON_PADDING * swaynag->scale; uint32_t ideal_height = text_height + padding * 2 + border * 2; - uint32_t ideal_surface_height = ideal_height / nagbar->scale; - if (nagbar->height < ideal_surface_height) { + uint32_t ideal_surface_height = ideal_height / swaynag->scale; + if (swaynag->height < ideal_surface_height) { return ideal_surface_height; } @@ -193,64 +194,65 @@ static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar, button->width = text_width + padding * 2; button->height = text_height + padding * 2; - cairo_set_source_u32(cairo, nagbar->type->border); + cairo_set_source_u32(cairo, swaynag->type->border); cairo_rectangle(cairo, button->x - border, button->y - border, button->width + border * 2, button->height + border * 2); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->type->button_background); + cairo_set_source_u32(cairo, swaynag->type->button_background); cairo_rectangle(cairo, button->x, button->y, button->width, button->height); cairo_fill(cairo); - cairo_set_source_u32(cairo, nagbar->type->text); + cairo_set_source_u32(cairo, swaynag->type->text); cairo_move_to(cairo, button->x + padding, button->y + padding); - pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text); + pango_printf(cairo, swaynag->font, swaynag->scale, true, + "%s", button->text); *x = button->x - border; return ideal_height; } -static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) { +static uint32_t render_to_cairo(cairo_t *cairo, struct swaynag *swaynag) { uint32_t max_height = 0; cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); - cairo_set_source_u32(cairo, nagbar->type->background); + cairo_set_source_u32(cairo, swaynag->type->background); cairo_paint(cairo); - uint32_t h = render_message(cairo, nagbar); + uint32_t h = render_message(cairo, swaynag); max_height = h > max_height ? h : max_height; - int x = (nagbar->width - NAGBAR_BUTTON_MARGIN_RIGHT) * nagbar->scale; - for (int i = 0; i < nagbar->buttons->length; i++) { - h = render_button(cairo, nagbar, i, &x); + int x = (swaynag->width - SWAYNAG_BUTTON_MARGIN_RIGHT) * 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 -= NAGBAR_BUTTON_GAP * nagbar->scale; + x -= SWAYNAG_BUTTON_GAP * swaynag->scale; if (i == 0) { - x -= NAGBAR_BUTTON_GAP_CLOSE * nagbar->scale; + x -= SWAYNAG_BUTTON_GAP_CLOSE * swaynag->scale; } } - if (nagbar->details.visible) { - h = render_detailed(cairo, nagbar, max_height); + if (swaynag->details.visible) { + h = render_detailed(cairo, swaynag, max_height); max_height = h > max_height ? h : max_height; } - int border = NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale; - if (max_height > nagbar->height) { + int border = SWAYNAG_BAR_BORDER_THICKNESS * swaynag->scale; + if (max_height > swaynag->height) { max_height += border; } - cairo_set_source_u32(cairo, nagbar->type->border_bottom); - cairo_rectangle(cairo, 0, nagbar->height * nagbar->scale - border, - nagbar->width * nagbar->scale, 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_fill(cairo); return max_height; } -void render_frame(struct sway_nagbar *nagbar) { - if (!nagbar->run_display) { +void render_frame(struct swaynag *swaynag) { + if (!swaynag->run_display) { return; } @@ -261,24 +263,24 @@ void render_frame(struct sway_nagbar *nagbar) { cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); cairo_paint(cairo); cairo_restore(cairo); - uint32_t height = render_to_cairo(cairo, nagbar); - if (height != nagbar->height) { - zwlr_layer_surface_v1_set_size(nagbar->layer_surface, 0, height); - zwlr_layer_surface_v1_set_exclusive_zone(nagbar->layer_surface, + uint32_t height = render_to_cairo(cairo, swaynag); + if (height != swaynag->height) { + zwlr_layer_surface_v1_set_size(swaynag->layer_surface, 0, height); + zwlr_layer_surface_v1_set_exclusive_zone(swaynag->layer_surface, height); - wl_surface_commit(nagbar->surface); - wl_display_roundtrip(nagbar->display); + wl_surface_commit(swaynag->surface); + wl_display_roundtrip(swaynag->display); } else { - nagbar->current_buffer = get_next_buffer(nagbar->shm, - nagbar->buffers, - nagbar->width * nagbar->scale, - nagbar->height * nagbar->scale); - if (!nagbar->current_buffer) { + swaynag->current_buffer = get_next_buffer(swaynag->shm, + swaynag->buffers, + swaynag->width * swaynag->scale, + swaynag->height * swaynag->scale); + if (!swaynag->current_buffer) { wlr_log(WLR_DEBUG, "Failed to get buffer. Skipping frame."); goto cleanup; } - cairo_t *shm = nagbar->current_buffer->cairo; + cairo_t *shm = swaynag->current_buffer->cairo; cairo_save(shm); cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); cairo_paint(shm); @@ -286,13 +288,13 @@ void render_frame(struct sway_nagbar *nagbar) { cairo_set_source_surface(shm, recorder, 0.0, 0.0); cairo_paint(shm); - wl_surface_set_buffer_scale(nagbar->surface, nagbar->scale); - wl_surface_attach(nagbar->surface, - nagbar->current_buffer->buffer, 0, 0); - wl_surface_damage(nagbar->surface, 0, 0, - nagbar->width, nagbar->height); - wl_surface_commit(nagbar->surface); - wl_display_roundtrip(nagbar->display); + wl_surface_set_buffer_scale(swaynag->surface, swaynag->scale); + wl_surface_attach(swaynag->surface, + swaynag->current_buffer->buffer, 0, 0); + wl_surface_damage(swaynag->surface, 0, 0, + swaynag->width, swaynag->height); + wl_surface_commit(swaynag->surface); + wl_display_roundtrip(swaynag->display); } cleanup: 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); + } +} diff --git a/swaynag/types.c b/swaynag/types.c index dbc841f7..c92d0e89 100644 --- a/swaynag/types.c +++ b/swaynag/types.c @@ -10,9 +10,9 @@ #include "swaynag/types.h" #include "util.h" -void nagbar_types_add_default(list_t *types) { - struct sway_nagbar_type *type_error; - type_error = calloc(1, sizeof(struct sway_nagbar_type)); +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; @@ -21,8 +21,8 @@ void nagbar_types_add_default(list_t *types) { type_error->border_bottom = 0x470909FF; list_add(types, type_error); - struct sway_nagbar_type *type_warning; - type_warning = calloc(1, sizeof(struct sway_nagbar_type)); + struct swaynag_type *type_warning; + type_warning = calloc(1, sizeof(struct swaynag_type)); type_warning->name = strdup("warning"); type_warning->button_background = 0xFFC100FF; type_warning->background = 0xFFA800FF; @@ -32,9 +32,9 @@ void nagbar_types_add_default(list_t *types) { list_add(types, type_warning); } -struct sway_nagbar_type *nagbar_type_get(list_t *types, char *name) { +struct swaynag_type *swaynag_type_get(list_t *types, char *name) { for (int i = 0; i < types->length; i++) { - struct sway_nagbar_type *type = types->items[i]; + struct swaynag_type *type = types->items[i]; if (strcasecmp(type->name, name) == 0) { return type; } @@ -42,9 +42,9 @@ struct sway_nagbar_type *nagbar_type_get(list_t *types, char *name) { return NULL; } -struct sway_nagbar_type *nagbar_type_clone(struct sway_nagbar_type *type) { - struct sway_nagbar_type *clone; - clone = calloc(1, sizeof(struct sway_nagbar_type)); +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; @@ -54,21 +54,21 @@ struct sway_nagbar_type *nagbar_type_clone(struct sway_nagbar_type *type) { return clone; } -void nagbar_type_free(struct sway_nagbar_type *type) { +void swaynag_type_free(struct swaynag_type *type) { free(type->name); free(type); } -void nagbar_types_free(list_t *types) { +void swaynag_types_free(list_t *types) { while (types->length) { - struct sway_nagbar_type *type = types->items[0]; - nagbar_type_free(type); + struct swaynag_type *type = types->items[0]; + swaynag_type_free(type); list_del(types, 0); } list_free(types); } -int nagbar_parse_type(int argc, char **argv, struct sway_nagbar_type *type) { +int swaynag_parse_type(int argc, char **argv, struct swaynag_type *type) { enum color_option { COLOR_BACKGROUND, COLOR_BORDER, -- cgit v1.2.3-54-g00ecf 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(-) 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-54-g00ecf From ca40298af4205abd36b7d1766ffe5cb1e72ed711 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Sun, 29 Jul 2018 00:28:01 -0400 Subject: swaynag: add math to meson.build --- swaynag/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/swaynag/meson.build b/swaynag/meson.build index f67c10ff..2ba3ed95 100644 --- a/swaynag/meson.build +++ b/swaynag/meson.build @@ -11,6 +11,7 @@ executable( cairo, client_protos, gdk_pixbuf, + math, pango, pangocairo, wayland_client, -- cgit v1.2.3-54-g00ecf 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(-) 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-54-g00ecf 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(-) 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-54-g00ecf 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(-) 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-54-g00ecf From 1e7fbe493eadc32531bc2aae69d75e5ffbf214c5 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 30 Jul 2018 14:14:16 -0400 Subject: swaynag: swaybar like default colors --- swaynag/types.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/swaynag/types.c b/swaynag/types.c index 66b802cd..1e0a138b 100644 --- a/swaynag/types.c +++ b/swaynag/types.c @@ -19,11 +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->button_background = 0x333333FF; + type_defaults->background = 0x323232FF; type_defaults->text = 0xFFFFFFFF; - type_defaults->border = 0xD92424FF; - type_defaults->border_bottom = 0x470909FF; + type_defaults->border = 0x222222FF; + type_defaults->border_bottom = 0x444444FF; type_defaults->bar_border_thickness = 2; type_defaults->message_padding = 8; type_defaults->details_border_thickness = 3; @@ -36,6 +36,11 @@ void swaynag_types_add_default(list_t *types) { struct swaynag_type *type_error; type_error = calloc(1, sizeof(struct swaynag_type)); + type_error->button_background = 0x680A0AFF; + type_error->background = 0x900000FF; + type_error->text = 0xFFFFFFFF; + type_error->border = 0xD92424FF; + type_error->border_bottom = 0x470909FF; type_error->name = strdup("error"); list_add(types, type_error); -- cgit v1.2.3-54-g00ecf From 41d858b4397f1c89a13900f078854e28ea0ac45a Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Mon, 30 Jul 2018 15:17:51 -0400 Subject: swaynag: add blank lines after headings in scdocs --- swaynag/swaynag.1.scd | 3 +++ swaynag/swaynag.5.scd | 3 +++ 2 files changed, 6 insertions(+) diff --git a/swaynag/swaynag.1.scd b/swaynag/swaynag.1.scd index 12787c3c..1c395aee 100644 --- a/swaynag/swaynag.1.scd +++ b/swaynag/swaynag.1.scd @@ -9,6 +9,7 @@ swaynag - Show a warning or error message with buttons _swaynag_ [options...] # OPTIONS + *-b, --button* Create a button with the text _text_ that executes _action_ when pressed. Multiple buttons can be defined by providing the flag multiple times. @@ -60,6 +61,7 @@ _swaynag_ [options...] Show the version number and quit. # APPEARANCE OPTIONS + *--background* Set the color of the background. @@ -100,4 +102,5 @@ _swaynag_ [options...] Set the padding for the button text. # SEE + swaynag(5) diff --git a/swaynag/swaynag.5.scd b/swaynag/swaynag.5.scd index e2348d8b..d3daadf7 100644 --- a/swaynag/swaynag.5.scd +++ b/swaynag/swaynag.5.scd @@ -10,6 +10,7 @@ $HOME/.swaynag/config, $XDG\_CONFIG\_HOME/swaynag/config, SYSCONFDIR/swaynag/config # CONFIG FILE + At the top of the config file, _swaynag_ options can be set using the format _long-option=value_. These will be used as default values if _swaynag_ is not given the option. This can be useful for setting a preferred font, output, and @@ -79,6 +80,7 @@ Additionally, the following options can be assigned a default per-type: Set the output to use. This should be the name of a _xdg\_output_. # EXAMPLE + ``` font=Monospace 12 edge=bottom @@ -94,4 +96,5 @@ message-padding=10 ``` # SEE + swaynag(1) -- cgit v1.2.3-54-g00ecf From 26c5ef18ba295e016074c9d87affe5da44e71cb1 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 1 Aug 2018 22:55:20 -0400 Subject: swaynag: don't drop \n for first line --- swaynag/config.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/swaynag/config.c b/swaynag/config.c index 80c5ad88..d6c5739d 100644 --- a/swaynag/config.c +++ b/swaynag/config.c @@ -19,18 +19,14 @@ static char *read_from_stdin() { continue; } - if (!buffer) { - buffer = strdup(line); - } else { - buffer = realloc(buffer, strlen(buffer) + strlen(line) + 2); - strcat(buffer, line); - strcat(buffer, "\n"); - } + size_t curlen = buffer ? strlen(buffer) : 0; + buffer = realloc(buffer, curlen + strlen(line) + 2); + snprintf(buffer + curlen, strlen(line) + 2, "%s\n", line); free(line); } - if (buffer && buffer[strlen(buffer) - 1] == '\n') { + while (buffer && buffer[strlen(buffer) - 1] == '\n') { buffer[strlen(buffer) - 1] = '\0'; } -- cgit v1.2.3-54-g00ecf