Merge pull request #2366 from RedSoxFan/nagbar

Implement swaynag
This commit is contained in:
Drew DeVault 2018-08-02 09:28:13 -04:00 committed by GitHub
commit ea14ef4095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 1843 additions and 0 deletions

13
include/swaynag/config.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _SWAYNAG_CONFIG_H
#define _SWAYNAG_CONFIG_H
#include "swaynag/swaynag.h"
#include "list.h"
int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
list_t *types, struct swaynag_type *type, char **config, bool *debug);
char *swaynag_get_config_path(void);
int swaynag_load_config(char *path, struct swaynag *swaynag, list_t *types);
#endif

7
include/swaynag/render.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _SWAYNAG_RENDER_H
#define _SWAYNAG_RENDER_H
#include "swaynag/swaynag.h"
void render_frame(struct swaynag *swaynag);
#endif

100
include/swaynag/swaynag.h Normal file
View File

@ -0,0 +1,100 @@
#ifndef _SWAYNAG_SWAYNAG_H
#define _SWAYNAG_SWAYNAG_H
#include <stdint.h>
#include <strings.h>
#include "list.h"
#include "pool-buffer.h"
#include "swaynag/types.h"
#include "xdg-output-unstable-v1-client-protocol.h"
#define SWAYNAG_MAX_HEIGHT 500
struct swaynag;
enum swaynag_action_type {
SWAYNAG_ACTION_DISMISS,
SWAYNAG_ACTION_EXPAND,
SWAYNAG_ACTION_COMMAND,
};
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;
int x;
int y;
};
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 {
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 wl_list outputs; // swaynag_output::link
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;
char *message;
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

39
include/swaynag/types.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef _SWAYNAG_TYPES_H
#define _SWAYNAG_TYPES_H
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);
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);
#endif

View File

@ -82,6 +82,8 @@ if scdoc.found()
'swaylock/swaylock.1.scd',
'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]
@ -130,6 +132,7 @@ subdir('swaybg')
subdir('swaybar')
subdir('swaylock')
subdir('swayidle')
subdir('swaynag')
config = configuration_data()
config.set('sysconfdir', join_paths(prefix, sysconfdir))

View File

@ -7,11 +7,13 @@
#include <wlr/types/wlr_output_damage.h>
#include <wlr/types/wlr_output.h>
#include <wlr/util/log.h>
#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"
@ -245,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) {
@ -282,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;

401
swaynag/config.c Normal file
View File

@ -0,0 +1,401 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200112L
#include <getopt.h>
#include <stdlib.h>
#include <wordexp.h>
#include "log.h"
#include "list.h"
#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() {
char *buffer = NULL;
while (!feof(stdin)) {
char *line = read_line(stdin);
if (!line) {
continue;
}
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);
}
while (buffer && buffer[strlen(buffer) - 1] == '\n') {
buffer[strlen(buffer) - 1] = '\0';
}
return buffer;
}
int swaynag_parse_options(int argc, char **argv, struct swaynag *swaynag,
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'},
{"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'},
{"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}
};
const char *usage =
"Usage: swaynag [options...]\n"
"\n"
" -b, --button <text> <action> Create a button with text that "
"executes action when pressed. Multiple buttons can be defined.\n"
" -c, --config <path> Path to config file.\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"
" -l, --detailed-message Read a detailed message from stdin.\n"
" -L, --detailed-button <text> Set the text of the detail button.\n"
" -m, --message <msg> Set the message text.\n"
" -o, --output <output> Set the output to use.\n"
" -s, --dismiss-button <text> Set the dismiss button text.\n"
" -t, --type <type> Set the message type.\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) {
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 (swaynag) {
if (optind >= argc) {
fprintf(stderr, "Missing action for button %s\n", optarg);
return EXIT_FAILURE;
}
struct swaynag_button *button;
button = calloc(sizeof(struct swaynag_button), 1);
button->text = strdup(optarg);
button->type = SWAYNAG_ACTION_COMMAND;
button->action = strdup(argv[optind]);
list_add(swaynag->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 (type) {
if (strcmp(optarg, "top") == 0) {
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) {
type->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 (type) {
free(type->font);
type->font = strdup(optarg);
}
break;
case 'l': // Detailed Message
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 (swaynag) {
free(swaynag->details.button_details.text);
swaynag->details.button_details.text = strdup(optarg);
}
break;
case 'm': // Message
if (swaynag) {
free(swaynag->message);
swaynag->message = strdup(optarg);
}
break;
case 'o': // Output
if (type) {
free(type->output);
type->output = strdup(optarg);
}
break;
case 's': // Dismiss Button Text
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 (swaynag) {
swaynag->type = swaynag_type_get(types, optarg);
if (!swaynag->type) {
fprintf(stderr, "Unknown type %s\n", optarg);
return EXIT_FAILURE;
}
}
break;
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;
}
}
return 0;
}
static bool file_exists(const char *path) {
return path && access(path, R_OK) != -1;
}
char *swaynag_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 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 swaynag_type *type;
type = calloc(1, sizeof(struct swaynag_type));
type->name = strdup("<config>");
list_add(types, type);
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 = swaynag_type_get(types, name);
if (!type) {
type = calloc(1, sizeof(struct swaynag_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;
result = swaynag_parse_options(2, argv, swaynag, types, type,
NULL, NULL);
if (result != 0) {
free(line);
fclose(config);
return result;
}
}
free(line);
}
fclose(config);
return 0;
}

129
swaynag/main.c Normal file
View File

@ -0,0 +1,129 @@
#define _XOPEN_SOURCE 500
#include <signal.h>
#include "log.h"
#include "list.h"
#include "swaynag/config.h"
#include "swaynag/swaynag.h"
#include "swaynag/types.h"
static struct swaynag swaynag;
void sig_handler(int signal) {
swaynag_destroy(&swaynag);
exit(EXIT_FAILURE);
}
void sway_terminate(int code) {
swaynag_destroy(&swaynag);
exit(code);
}
int main(int argc, char **argv) {
int exit_code = EXIT_SUCCESS;
list_t *types = create_list();
swaynag_types_add_default(types);
memset(&swaynag, 0, sizeof(swaynag));
swaynag.buttons = create_list();
struct swaynag_button *button_close =
calloc(sizeof(struct swaynag_button), 1);
button_close->text = strdup("X");
button_close->type = SWAYNAG_ACTION_DISMISS;
list_add(swaynag.buttons, button_close);
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 = swaynag_parse_options(argc, argv, NULL, 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 = swaynag_get_config_path();
}
if (config_path) {
wlr_log(WLR_DEBUG, "Loading config file: %s", config_path);
int config_status = swaynag_load_config(config_path, &swaynag, types);
free(config_path);
if (config_status != 0) {
exit_code = config_status;
goto cleanup;
}
}
if (argc > 1) {
struct swaynag_type *type_args;
type_args = calloc(1, sizeof(struct swaynag_type));
type_args->name = strdup("<args>");
list_add(types, type_args);
int result = swaynag_parse_options(argc, argv, &swaynag, types,
type_args, NULL, NULL);
if (result != 0) {
exit_code = result;
goto cleanup;
}
}
if (!swaynag.message) {
wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m");
exit_code = EXIT_FAILURE;
goto cleanup;
}
if (!swaynag.type) {
swaynag.type = swaynag_type_get(types, "error");
}
// 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, "<args>"));
swaynag_type_merge(type, swaynag.type);
swaynag_type_merge(type, swaynag_type_get(types, "<config>"));
swaynag_type_merge(type, swaynag_type_get(types, "<defaults>"));
swaynag.type = type;
swaynag_types_free(types);
if (swaynag.details.message) {
list_add(swaynag.buttons, &swaynag.details.button_details);
} else {
free(swaynag.details.button_details.text);
}
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);
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];
wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action);
}
signal(SIGTERM, sig_handler);
swaynag_setup(&swaynag);
swaynag_run(&swaynag);
return exit_code;
cleanup:
swaynag_types_free(types);
free(swaynag.details.button_details.text);
swaynag_destroy(&swaynag);
return exit_code;
}

23
swaynag/meson.build Normal file
View File

@ -0,0 +1,23 @@
executable(
'swaynag', [
'config.c',
'main.c',
'render.c',
'swaynag.c',
'types.c',
],
include_directories: [sway_inc],
dependencies: [
cairo,
client_protos,
gdk_pixbuf,
math,
pango,
pangocairo,
wayland_client,
wayland_cursor,
wlroots,
],
link_with: [lib_sway_common, lib_sway_client],
install: true
)

308
swaynag/render.c Normal file
View File

@ -0,0 +1,308 @@
#include <stdint.h>
#include "cairo.h"
#include "log.h"
#include "pango.h"
#include "pool-buffer.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 swaynag *swaynag) {
uint32_t height = swaynag->height * swaynag->scale;
height -= swaynag->type->bar_border_thickness * swaynag->scale;
int text_width, text_height;
get_text_size(cairo, swaynag->type->font, &text_width, &text_height,
swaynag->scale, true, "%s", swaynag->message);
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;
if (swaynag->height < ideal_surface_height) {
return ideal_surface_height;
}
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);
return ideal_surface_height;
}
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->type->font, &text_width, &text_height,
swaynag->scale, true, "%s", button->text);
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,
button->width, button->height);
cairo_fill(cairo);
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, swaynag->type->text);
cairo_move_to(cairo, button->x + border + padding,
button->y + border + (button->height - text_height) / 2);
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->type->font, &up_width, &temp_height,
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);
int text_width = up_width > down_width ? up_width : down_width;
int border = swaynag->type->button_border_thickness * swaynag->scale;
int padding = swaynag->type->button_padding * swaynag->scale;
return text_width + border * 2 + padding * 2;
}
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->type->bar_border_thickness * 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 * swaynag->scale + decor;
swaynag->details.width = width - decor * 2;
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);
pango_layout_set_wrap(layout, PANGO_WRAP_WORD_CHAR);
pango_layout_set_single_paragraph_mode(layout, false);
pango_cairo_update_layout(cairo, layout);
swaynag->details.total_lines = pango_layout_get_line_count(layout);
PangoLayoutLine *line;
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);
int text_width, text_height;
pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, &text_width, &text_height);
bool show_buttons = swaynag->details.offset > 0;
int button_width = get_detailed_scroll_button_width(cairo, swaynag);
if (show_buttons) {
swaynag->details.width -= button_width;
pango_layout_set_width(layout,
(swaynag->details.width - padding * 2) * PANGO_SCALE);
}
uint32_t ideal_height;
do {
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;
swaynag->details.width -= button_width;
pango_layout_set_width(layout,
(swaynag->details.width - padding * 2) * PANGO_SCALE);
}
}
swaynag->details.height = ideal_height - swaynag->details.y - decor;
pango_layout_set_height(layout,
(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 != (swaynag->details.height - padding * 2));
swaynag->details.visible_lines = pango_layout_get_line_count(layout);
if (show_buttons) {
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, 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, 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 / swaynag->scale;
}
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->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->type->font, &text_width, &text_height,
swaynag->scale, true, "%s", button->text);
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;
if (swaynag->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, 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, swaynag->type->button_background);
cairo_rectangle(cairo, button->x, button->y,
button->width, button->height);
cairo_fill(cairo);
cairo_set_source_u32(cairo, swaynag->type->text);
cairo_move_to(cairo, button->x + padding, button->y + padding);
pango_printf(cairo, swaynag->type->font, swaynag->scale, true,
"%s", button->text);
*x = button->x - border;
return ideal_surface_height;
}
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, swaynag->type->background);
cairo_paint(cairo);
uint32_t h = render_message(cairo, swaynag);
max_height = h > max_height ? h : max_height;
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->type->button_gap * swaynag->scale;
if (i == 0) {
x -= swaynag->type->button_gap_close * swaynag->scale;
}
}
if (swaynag->details.visible) {
h = render_detailed(cairo, swaynag, max_height);
max_height = h > max_height ? h : max_height;
}
int border = swaynag->type->bar_border_thickness * swaynag->scale;
if (max_height > swaynag->height) {
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_fill(cairo);
return max_height;
}
void render_frame(struct swaynag *swaynag) {
if (!swaynag->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, 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(swaynag->surface);
wl_display_roundtrip(swaynag->display);
} else {
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 = swaynag->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(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:
cairo_surface_destroy(recorder);
cairo_destroy(cairo);
}

106
swaynag/swaynag.1.scd Normal file
View File

@ -0,0 +1,106 @@
swaynag(1)
# NAME
swaynag - Show a warning or error message with buttons
# SYNOPSIS
_swaynag_ [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.
*-c, --config* <path>
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
Set the edge to use.
*-f, --font* <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* <text>
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* <msg>
Set the message text.
*-o, --output* <output>
Set the output to use. This should be the name of a _xdg\_output_.
*-s, --dismiss-button* <text>
Sets the text for the dismiss nagbar button. The default is _X_.
*-t, --type* <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*
Show the version number and quit.
# APPEARANCE OPTIONS
*--background* <RRGGBB[AA]>
Set the color of the background.
*--border* <RRGGBB[AA]>
Set the color of the border.
*--border-bottom* <RRGGBB[AA]>
Set the color of the bottom border.
*--button-background* <RRGGBB[AA]>
Set the color for the background for buttons.
*--text* <RRGGBB[AA]>
Set the text color.
*--border-bottom-size* <size>
Set the thickness of the bottom border.
*--message-padding* <padding>
Set the padding for the message.
*--details-border-size* <size>
Set the thickness for the details border.
*--button-border-size* <size>
Set the thickness for the button border.
*--button-gap* <gap>
Set the size of the gap between buttons.
*--button-dismiss-gap* <gap>
Set the size of the gap between the dismiss button and another button.
*--button-margin-right* <margin>
Set the margin from the right of the dismiss button to edge.
*--button-padding* <padding>
Set the padding for the button text.
# SEE
swaynag(5)

100
swaynag/swaynag.5.scd Normal file
View File

@ -0,0 +1,100 @@
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]
option=value
```
All colors may be given in the form _RRGGBB_ or _RRGGBBAA_. The following
colors can be set:
*background=<color>*
The background color for _swaynag_.
*border=<color>*
The color to use for borders of buttons.
*border-bottom=<color>*
The color of the border line at the bottom of _swaynag_.
*button-background=<color>*
The background color for the buttons.
*text=<color>*
The color of the text.
The following sizing options can also be set:
*border-bottom-size=<size>*
Set the thickness of the bottom border.
*message-padding=<padding>*
Set the padding for the message.
*details-border-size=<size>*
Set the thickness for the details border.
*button-border-size=<size>*
Set the thickness for the button border.
*button-gap=<gap>*
Set the size of the gap between buttons.
*button-dismiss-gap=<gap>*
Set the size of the gap between the dismiss button and another button.
*button-margin-right=<margin>*
Set the margin from the right of the dismiss button to edge.
*button-padding=<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=<font>*
Set the font to use.
*output=<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
swaynag(1)

451
swaynag/swaynag.c Normal file
View File

@ -0,0 +1,451 @@
#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 "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 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 update_cursor(struct swaynag *swaynag) {
struct swaynag_pointer *pointer = &swaynag->pointer;
pointer->cursor_theme = wl_cursor_theme_load(NULL, 24 * swaynag->scale,
swaynag->shm);
struct wl_cursor *cursor =
wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr");
pointer->cursor_image = cursor->images[0];
wl_surface_set_buffer_scale(pointer->cursor_surface,
swaynag->scale);
wl_surface_attach(pointer->cursor_surface,
wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
wl_pointer_set_cursor(pointer->pointer, pointer->serial,
pointer->cursor_surface,
pointer->cursor_image->hotspot_x / swaynag->scale,
pointer->cursor_image->hotspot_y / swaynag->scale);
wl_surface_commit(pointer->cursor_surface);
}
static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t surface_x, wl_fixed_t surface_y) {
struct swaynag *swaynag = data;
struct swaynag_pointer *pointer = &swaynag->pointer;
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;
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;
bot -= 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_output *swaynag_output = data;
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);
}
}
static struct wl_output_listener output_listener = {
.geometry = nop,
.mode = nop,
.done = nop,
.scale = output_scale,
};
static void xdg_output_handle_name(void *data,
struct zxdg_output_v1 *xdg_output, const char *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 (!swaynag_output->swaynag->output && outname && name
&& strcmp(outname, name) == 0) {
wlr_log(WLR_DEBUG, "Using output %s", name);
swaynag_output->swaynag->output = swaynag_output;
}
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 = {
.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 && swaynag->xdg_output_manager) {
swaynag->querying_outputs++;
struct swaynag_output *output =
calloc(1, sizeof(struct swaynag_output));
output->wl_output = wl_registry_bind(registry, name,
&wl_output_interface, 3);
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(
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;
wl_list_init(&swaynag->outputs);
struct wl_registry *registry = wl_display_get_registry(swaynag->display);
wl_registry_add_listener(registry, &registry_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 && 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;
pointer->cursor_surface = wl_compositor_create_surface(swaynag->compositor);
assert(pointer->cursor_surface);
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 ? 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,
&layer_surface_listener, swaynag);
zwlr_layer_surface_v1_set_anchor(swaynag->layer_surface,
swaynag->type->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);
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->pointer.cursor_theme) {
wl_cursor_theme_destroy(swaynag->pointer.cursor_theme);
}
if (&swaynag->buffers[0]) {
destroy_buffer(&swaynag->buffers[0]);
}
if (&swaynag->buffers[1]) {
destroy_buffer(&swaynag->buffers[1]);
}
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);
}
if (swaynag->shm) {
wl_shm_destroy(swaynag->shm);
}
if (swaynag->display) {
wl_display_disconnect(swaynag->display);
}
}

156
swaynag/types.c Normal file
View File

@ -0,0 +1,156 @@
#define _XOPEN_SOURCE 500
#include <getopt.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include "list.h"
#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("<defaults>");
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->button_background = 0x333333FF;
type_defaults->background = 0x323232FF;
type_defaults->text = 0xFFFFFFFF;
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;
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->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);
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;
type_warning->text = 0x000000FF;
type_warning->border = 0xAB7100FF;
type_warning->border_bottom = 0xAB7100FF;
list_add(types, type_warning);
}
struct swaynag_type *swaynag_type_get(list_t *types, char *name) {
for (int i = 0; i < types->length; i++) {
struct swaynag_type *type = types->items[i];
if (strcasecmp(type->name, name) == 0) {
return type;
}
}
return NULL;
}
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);
}
void swaynag_types_free(list_t *types) {
while (types->length) {
struct swaynag_type *type = types->items[0];
swaynag_type_free(type);
list_del(types, 0);
}
list_free(types);
}