Implements swaynagbar

This commit is contained in:
Brian Ashworth 2018-07-25 21:57:19 -04:00
parent abf33468c1
commit 88bc4b528e
8 changed files with 857 additions and 0 deletions

View file

@ -0,0 +1,88 @@
#ifndef _SWAY_NAGBAR_NAGBAR_H
#define _SWAY_NAGBAR_NAGNAR_H
#include <stdint.h>
#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

View file

@ -0,0 +1,6 @@
#ifndef _SWAY_NAGBAR_RENDER_H
#define _SWAY_NAGBAR_RENDER_H
void render_frame(struct sway_nagbar *nagbar);
#endif

View file

@ -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))

184
swaynagbar/main.c Normal file
View file

@ -0,0 +1,184 @@
#define _XOPEN_SOURCE 500
#include <getopt.h>
#include <signal.h>
#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 <text> <action> 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 <font> Set the font to use.\n"
" -h, --help Show help message and quit.\n"
" -m, --message <msg> Set the message text.\n"
" -o, --output <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;
}

20
swaynagbar/meson.build Normal file
View file

@ -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
)

364
swaynagbar/nagbar.c Normal file
View file

@ -0,0 +1,364 @@
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#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, &registry_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);
}
}

152
swaynagbar/render.c Normal file
View file

@ -0,0 +1,152 @@
#include <stdint.h>
#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);
}

View file

@ -0,0 +1,41 @@
swaynagbar(1)
# NAME
swaynagbar - Show a warning or error message with buttons
# SYNOPSIS
_swaynagbar_ [options...]
# OPTIONS
*-b, --button* <text> <action>
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 <font>*
Set the font to use.
*-h, --help*
Show help message and quit.
*-m, --message <msg>*
Set the message text.
*-o, --output <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.