aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/swaynagbar/nagbar.h88
-rw-r--r--include/swaynagbar/render.h6
-rw-r--r--meson.build2
-rw-r--r--swaynagbar/main.c184
-rw-r--r--swaynagbar/meson.build20
-rw-r--r--swaynagbar/nagbar.c364
-rw-r--r--swaynagbar/render.c152
-rw-r--r--swaynagbar/swaynagbar.1.scd41
8 files changed, 857 insertions, 0 deletions
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 @@
1#ifndef _SWAY_NAGBAR_NAGBAR_H
2#define _SWAY_NAGBAR_NAGNAR_H
3#include <stdint.h>
4#include "list.h"
5#include "pool-buffer.h"
6#include "xdg-output-unstable-v1-client-protocol.h"
7
8#define NAGBAR_BAR_BORDER_THICKNESS 2
9#define NAGBAR_MESSAGE_PADDING 8
10#define NAGBAR_BUTTON_BORDER_THICKNESS 3
11#define NAGBAR_BUTTON_GAP 20
12#define NAGBAR_BUTTON_GAP_CLOSE 15
13#define NAGBAR_BUTTON_MARGIN_RIGHT 2
14#define NAGBAR_BUTTON_PADDING 3
15
16enum sway_nagbar_type {
17 NAGBAR_ERROR,
18 NAGBAR_WARNING,
19};
20
21struct sway_nagbar_colors {
22 uint32_t button_background;
23 uint32_t background;
24 uint32_t text;
25 uint32_t border;
26 uint32_t border_bottom;
27};
28
29struct sway_nagbar_pointer {
30 struct wl_pointer *pointer;
31 struct wl_cursor_theme *cursor_theme;
32 struct wl_cursor_image *cursor_image;
33 struct wl_surface *cursor_surface;
34 int x;
35 int y;
36};
37
38struct sway_nagbar_output {
39 char *name;
40 struct wl_output *wl_output;
41 uint32_t wl_name;
42};
43
44struct sway_nagbar_button {
45 char *text;
46 char *action;
47 int x;
48 int y;
49 int width;
50 int height;
51};
52
53struct sway_nagbar {
54 bool run_display;
55 int querying_outputs;
56
57 struct wl_display *display;
58 struct wl_compositor *compositor;
59 struct wl_seat *seat;
60 struct wl_shm *shm;
61 struct sway_nagbar_pointer pointer;
62 struct zxdg_output_manager_v1 *xdg_output_manager;
63 struct sway_nagbar_output output;
64 struct zwlr_layer_shell_v1 *layer_shell;
65 struct zwlr_layer_surface_v1 *layer_surface;
66 struct wl_surface *surface;
67
68 uint32_t width;
69 uint32_t height;
70 int32_t scale;
71 struct pool_buffer buffers[2];
72 struct pool_buffer *current_buffer;
73
74 enum sway_nagbar_type type;
75 struct sway_nagbar_colors colors;
76 uint32_t anchors;
77 char *message;
78 char *font;
79 list_t *buttons;
80};
81
82void nagbar_setup(struct sway_nagbar *nagbar);
83
84void nagbar_run(struct sway_nagbar *nagbar);
85
86void nagbar_destroy(struct sway_nagbar *nagbar);
87
88#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 @@
1#ifndef _SWAY_NAGBAR_RENDER_H
2#define _SWAY_NAGBAR_RENDER_H
3
4void render_frame(struct sway_nagbar *nagbar);
5
6#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()
82 'swaylock/swaylock.1.scd', 82 'swaylock/swaylock.1.scd',
83 'swaymsg/swaymsg.1.scd', 83 'swaymsg/swaymsg.1.scd',
84 'swayidle/swayidle.1.scd', 84 'swayidle/swayidle.1.scd',
85 'swaynagbar/swaynagbar.1.scd',
85 ] 86 ]
86 foreach filename : man_files 87 foreach filename : man_files
87 topic = filename.split('.')[-3].split('/')[-1] 88 topic = filename.split('.')[-3].split('/')[-1]
@@ -130,6 +131,7 @@ subdir('swaybg')
130subdir('swaybar') 131subdir('swaybar')
131subdir('swaylock') 132subdir('swaylock')
132subdir('swayidle') 133subdir('swayidle')
134subdir('swaynagbar')
133 135
134config = configuration_data() 136config = configuration_data()
135config.set('sysconfdir', join_paths(prefix, sysconfdir)) 137config.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 @@
1#define _XOPEN_SOURCE 500
2#include <getopt.h>
3#include <signal.h>
4#include "log.h"
5#include "list.h"
6#include "swaynagbar/nagbar.h"
7#include "wlr-layer-shell-unstable-v1-client-protocol.h"
8
9static struct sway_nagbar nagbar;
10
11void sig_handler(int signal) {
12 nagbar_destroy(&nagbar);
13 exit(EXIT_FAILURE);
14}
15
16void sway_terminate(int code) {
17 nagbar_destroy(&nagbar);
18 exit(code);
19}
20
21static void set_nagbar_colors() {
22 if (nagbar.type == NAGBAR_ERROR) {
23 nagbar.colors.button_background = 0x680A0AFF;
24 nagbar.colors.background = 0x900000FF;
25 nagbar.colors.text = 0xFFFFFFFF;
26 nagbar.colors.border = 0xD92424FF;
27 nagbar.colors.border_bottom = 0x470909FF;
28 } else if (nagbar.type == NAGBAR_WARNING) {
29 nagbar.colors.button_background = 0xFFC100FF;
30 nagbar.colors.background = 0xFFA800FF;
31 nagbar.colors.text = 0x000000FF;
32 nagbar.colors.border = 0xAB7100FF;
33 nagbar.colors.border_bottom = 0xAB7100FF;
34 }
35}
36
37int main(int argc, char **argv) {
38 int exit_code = EXIT_SUCCESS;
39 bool debug = false;
40
41 memset(&nagbar, 0, sizeof(nagbar));
42 nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
43 | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
44 | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
45 nagbar.type = NAGBAR_ERROR;
46 set_nagbar_colors();
47 nagbar.font = strdup("pango:monospace 8");
48 nagbar.buttons = create_list();
49
50 struct sway_nagbar_button *button_close =
51 calloc(sizeof(struct sway_nagbar_button), 1);
52 button_close->text = strdup("X");
53 button_close->action = NULL;
54 list_add(nagbar.buttons, button_close);
55
56 static struct option long_options[] = {
57 {"button", required_argument, NULL, 'b'},
58 {"debug", no_argument, NULL, 'd'},
59 {"edge", required_argument, NULL, 'e'},
60 {"font", required_argument, NULL, 'f'},
61 {"help", no_argument, NULL, 'h'},
62 {"message", required_argument, NULL, 'm'},
63 {"output", required_argument, NULL, 'o'},
64 {"type", required_argument, NULL, 't'},
65 {"version", no_argument, NULL, 'v'},
66 {0, 0, 0, 0}
67 };
68
69 const char *usage =
70 "Usage: swaynagbar [options...]\n"
71 "\n"
72 " -b, --button <text> <action> Create a button with text that "
73 "executes action when pressed. Multiple buttons can be defined.\n"
74 " -d, --debug Enable debugging.\n"
75 " -e, --edge top|bottom Set the edge to use.\n"
76 " -f, --font <font> Set the font to use.\n"
77 " -h, --help Show help message and quit.\n"
78 " -m, --message <msg> Set the message text.\n"
79 " -o, --output <output> Set the output to use.\n"
80 " -t, --type error|warning Set the message type.\n"
81 " -v, --version Show the version number and quit.\n";
82
83 while (1) {
84 int c = getopt_long(argc, argv, "b:de:f:hm:o:t:v", long_options, NULL);
85 if (c == -1) {
86 break;
87 }
88 switch (c) {
89 case 'b': // Button
90 if (optind >= argc) {
91 fprintf(stderr, "Missing action for button %s", optarg);
92 exit_code = EXIT_FAILURE;
93 goto cleanup;
94 }
95 struct sway_nagbar_button *button;
96 button = calloc(sizeof(struct sway_nagbar_button), 1);
97 button->text = strdup(optarg);
98 button->action = strdup(argv[optind]);
99 optind++;
100 list_add(nagbar.buttons, button);
101 break;
102 case 'd': // Debug
103 debug = true;
104 break;
105 case 'e': // Edge
106 if (strcmp(optarg, "top") == 0) {
107 nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
108 | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
109 | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
110 } else if (strcmp(optarg, "bottom") == 0) {
111 nagbar.anchors = ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
112 | ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
113 | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT;
114 } else {
115 fprintf(stderr, "Invalid edge: %s\n", optarg);
116 exit_code = EXIT_FAILURE;
117 goto cleanup;
118 }
119 break;
120 case 'f': // Font
121 free(nagbar.font);
122 nagbar.font = strdup(optarg);
123 break;
124 case 'm': // Message
125 free(nagbar.message);
126 nagbar.message = strdup(optarg);
127 break;
128 case 'o': // Output
129 free(nagbar.output.name);
130 nagbar.output.name = strdup(optarg);
131 break;
132 case 't': // Type
133 if (strcmp(optarg, "error") == 0) {
134 nagbar.type = NAGBAR_ERROR;
135 } else if (strcmp(optarg, "warning") == 0) {
136 nagbar.type = NAGBAR_WARNING;
137 } else {
138 fprintf(stderr, "Type must be either 'error' or 'warning'");
139 exit_code = EXIT_FAILURE;
140 goto cleanup;
141 }
142 set_nagbar_colors();
143 break;
144 case 'v': // Version
145 fprintf(stdout, "sway version " SWAY_VERSION "\n");
146 exit_code = EXIT_SUCCESS;
147 goto cleanup;
148 default: // Help or unknown flag
149 fprintf(c == 'h' ? stdout : stderr, "%s", usage);
150 exit_code = c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE;
151 goto cleanup;
152 }
153 }
154
155 wlr_log_init(debug ? WLR_DEBUG : WLR_ERROR, NULL);
156
157 if (!nagbar.message) {
158 wlr_log(WLR_ERROR, "No message passed. Please provide --message/-m");
159 exit_code = EXIT_FAILURE;
160 goto cleanup;
161 }
162
163 wlr_log(WLR_DEBUG, "Output: %s", nagbar.output.name);
164 wlr_log(WLR_DEBUG, "Anchors: %d", nagbar.anchors);
165 wlr_log(WLR_DEBUG, "Type: %d", nagbar.type);
166 wlr_log(WLR_DEBUG, "Message: %s", nagbar.message);
167 wlr_log(WLR_DEBUG, "Font: %s", nagbar.font);
168 wlr_log(WLR_DEBUG, "Buttons");
169 for (int i = 0; i < nagbar.buttons->length; i++) {
170 struct sway_nagbar_button *button = nagbar.buttons->items[i];
171 wlr_log(WLR_DEBUG, "\t[%s] `%s`", button->text, button->action);
172 }
173
174 signal(SIGTERM, sig_handler);
175
176 nagbar_setup(&nagbar);
177 nagbar_run(&nagbar);
178 return exit_code;
179
180cleanup:
181 nagbar_destroy(&nagbar);
182 return exit_code;
183}
184
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 @@
1executable(
2 'swaynagbar', [
3 'main.c',
4 'nagbar.c',
5 'render.c',
6 ],
7 include_directories: [sway_inc],
8 dependencies: [
9 cairo,
10 client_protos,
11 gdk_pixbuf,
12 pango,
13 pangocairo,
14 wayland_client,
15 wayland_cursor,
16 wlroots,
17 ],
18 link_with: [lib_sway_common, lib_sway_client],
19 install: true
20)
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 @@
1#define _XOPEN_SOURCE 500
2#include <assert.h>
3#include <sys/stat.h>
4#include <sys/wait.h>
5#include <wayland-client.h>
6#include <wayland-cursor.h>
7#include "log.h"
8#include "list.h"
9#include "swaynagbar/nagbar.h"
10#include "swaynagbar/render.h"
11#include "wlr-layer-shell-unstable-v1-client-protocol.h"
12
13static void nop() {
14 // Intentionally left blank
15}
16
17static bool terminal_execute(char *terminal, char *command) {
18 char fname[] = "/tmp/swaynagbarXXXXXX";
19 FILE *tmp= fdopen(mkstemp(fname), "w");
20 if (!tmp) {
21 wlr_log(WLR_ERROR, "Failed to create temp script");
22 return false;
23 }
24 wlr_log(WLR_DEBUG, "Created temp script: %s", fname);
25 fprintf(tmp, "#!/bin/sh\nrm %s\n%s", fname, command);
26 fclose(tmp);
27 chmod(fname, S_IRUSR | S_IWUSR | S_IXUSR);
28 char cmd[strlen(terminal) + strlen(" -e ") + strlen(fname) + 1];
29 sprintf(cmd, "%s -e %s", terminal, fname);
30 execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
31 return true;
32}
33
34static void nagbar_button_execute(struct sway_nagbar *nagbar,
35 struct sway_nagbar_button *button) {
36 wlr_log(WLR_DEBUG, "Executing [%s]: %s", button->text, button->action);
37 if (!button->action) {
38 nagbar->run_display = false;
39 } else {
40 if (fork() == 0) {
41 // Child process. Will be used to prevent zombie processes
42 setsid();
43 if (fork() == 0) {
44 // Child of the child. Will be reparented to the init process
45 char *terminal = getenv("TERMINAL");
46 if (terminal && strlen(terminal)) {
47 wlr_log(WLR_DEBUG, "Found $TERMINAL: %s", terminal);
48 if (!terminal_execute(terminal, button->action)) {
49 nagbar_destroy(nagbar);
50 exit(EXIT_FAILURE);
51 }
52 } else {
53 wlr_log(WLR_DEBUG, "$TERMINAL not found. Running directly");
54 execl("/bin/sh", "/bin/sh", "-c", button->action, NULL);
55 }
56 }
57 exit(EXIT_SUCCESS);
58 }
59 }
60 wait(0);
61}
62
63static void layer_surface_configure(void *data,
64 struct zwlr_layer_surface_v1 *surface,
65 uint32_t serial, uint32_t width, uint32_t height) {
66 struct sway_nagbar *nagbar = data;
67 nagbar->width = width;
68 nagbar->height = height;
69 zwlr_layer_surface_v1_ack_configure(surface, serial);
70 render_frame(nagbar);
71}
72
73static void layer_surface_closed(void *data,
74 struct zwlr_layer_surface_v1 *surface) {
75 struct sway_nagbar *nagbar = data;
76 nagbar_destroy(nagbar);
77}
78
79static struct zwlr_layer_surface_v1_listener layer_surface_listener = {
80 .configure = layer_surface_configure,
81 .closed = layer_surface_closed,
82};
83
84static void wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
85 uint32_t serial, struct wl_surface *surface,
86 wl_fixed_t surface_x, wl_fixed_t surface_y) {
87 struct sway_nagbar *nagbar = data;
88 struct sway_nagbar_pointer *pointer = &nagbar->pointer;
89 wl_surface_set_buffer_scale(pointer->cursor_surface, nagbar->scale);
90 wl_surface_attach(pointer->cursor_surface,
91 wl_cursor_image_get_buffer(pointer->cursor_image), 0, 0);
92 wl_pointer_set_cursor(wl_pointer, serial, pointer->cursor_surface,
93 pointer->cursor_image->hotspot_x / nagbar->scale,
94 pointer->cursor_image->hotspot_y / nagbar->scale);
95 wl_surface_commit(pointer->cursor_surface);
96}
97
98static void wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
99 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
100 struct sway_nagbar *nagbar = data;
101 nagbar->pointer.x = wl_fixed_to_int(surface_x);
102 nagbar->pointer.y = wl_fixed_to_int(surface_y);
103}
104
105static void wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
106 uint32_t serial, uint32_t time, uint32_t button, uint32_t state) {
107 struct sway_nagbar *nagbar = data;
108
109 if (state != WL_POINTER_BUTTON_STATE_PRESSED) {
110 return;
111 }
112
113 double x = nagbar->pointer.x * nagbar->scale;
114 double y = nagbar->pointer.y * nagbar->scale;
115 for (int i = 0; i < nagbar->buttons->length; i++) {
116 struct sway_nagbar_button *nagbutton = nagbar->buttons->items[i];
117 if (x >= nagbutton->x
118 && y >= nagbutton->y
119 && x < nagbutton->x + nagbutton->width
120 && y < nagbutton->y + nagbutton->height) {
121 nagbar_button_execute(nagbar, nagbutton);
122 }
123 }
124}
125
126static struct wl_pointer_listener pointer_listener = {
127 .enter = wl_pointer_enter,
128 .leave = nop,
129 .motion = wl_pointer_motion,
130 .button = wl_pointer_button,
131 .axis = nop,
132 .frame = nop,
133 .axis_source = nop,
134 .axis_stop = nop,
135 .axis_discrete = nop,
136};
137
138static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
139 enum wl_seat_capability caps) {
140 struct sway_nagbar *nagbar = data;
141 if ((caps & WL_SEAT_CAPABILITY_POINTER)) {
142 nagbar->pointer.pointer = wl_seat_get_pointer(wl_seat);
143 wl_pointer_add_listener(nagbar->pointer.pointer, &pointer_listener,
144 nagbar);
145 }
146}
147
148const struct wl_seat_listener seat_listener = {
149 .capabilities = seat_handle_capabilities,
150 .name = nop,
151};
152
153static void output_scale(void *data, struct wl_output *output,
154 int32_t factor) {
155 struct sway_nagbar *nagbar = data;
156 nagbar->scale = factor;
157 render_frame(nagbar);
158}
159
160static struct wl_output_listener output_listener = {
161 .geometry = nop,
162 .mode = nop,
163 .done = nop,
164 .scale = output_scale,
165};
166
167struct output_state {
168 struct wl_output *wl_output;
169 uint32_t wl_name;
170 struct zxdg_output_v1 *xdg_output;
171 struct sway_nagbar *nagbar;
172};
173
174static void xdg_output_handle_name(void *data,
175 struct zxdg_output_v1 *xdg_output, const char *name) {
176 struct output_state *state = data;
177 char *outname = state->nagbar->output.name;
178 wlr_log(WLR_DEBUG, "Checking against output %s for %s", name, outname);
179 if ((!outname && !state->nagbar->output.wl_output)
180 || (name && outname && strcmp(name, outname) == 0)) {
181 wlr_log(WLR_DEBUG, "Using output %s", name);
182 state->nagbar->output.wl_output = state->wl_output;
183 state->nagbar->output.wl_name = state->wl_name;
184 wl_output_add_listener(state->nagbar->output.wl_output,
185 &output_listener, state->nagbar);
186 wl_display_roundtrip(state->nagbar->display);
187 zxdg_output_v1_destroy(state->xdg_output);
188 } else {
189 zxdg_output_v1_destroy(state->xdg_output);
190 wl_output_destroy(state->wl_output);
191 }
192 state->nagbar->querying_outputs--;
193 free(state);
194}
195
196static struct zxdg_output_v1_listener xdg_output_listener = {
197 .logical_position = nop,
198 .logical_size = nop,
199 .done = nop,
200 .name = xdg_output_handle_name,
201 .description = nop,
202};
203
204static void handle_global(void *data, struct wl_registry *registry,
205 uint32_t name, const char *interface, uint32_t version) {
206 struct sway_nagbar *nagbar = data;
207 if (strcmp(interface, wl_compositor_interface.name) == 0) {
208 nagbar->compositor = wl_registry_bind(registry, name,
209 &wl_compositor_interface, 3);
210 } else if (strcmp(interface, wl_seat_interface.name) == 0) {
211 nagbar->seat = wl_registry_bind(registry, name, &wl_seat_interface, 1);
212 wl_seat_add_listener(nagbar->seat, &seat_listener, nagbar);
213 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
214 nagbar->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
215 } else if (strcmp(interface, wl_output_interface.name) == 0) {
216 if (!nagbar->output.wl_output && nagbar->xdg_output_manager) {
217 nagbar->querying_outputs++;
218 struct output_state *state =
219 calloc(1, sizeof(struct output_state));
220 state->nagbar = nagbar;
221 state->wl_output = wl_registry_bind(registry, name,
222 &wl_output_interface, 3);
223 state->wl_name = name;
224 state->xdg_output = zxdg_output_manager_v1_get_xdg_output(
225 nagbar->xdg_output_manager, state->wl_output);
226 zxdg_output_v1_add_listener(state->xdg_output,
227 &xdg_output_listener, state);
228 } else if (!nagbar->output.wl_output && !nagbar->xdg_output_manager) {
229 wlr_log(WLR_ERROR, "Warning: zxdg_output_manager_v1 not supported."
230 " Falling back to first detected output");
231 nagbar->output.wl_output = wl_registry_bind(registry, name,
232 &wl_output_interface, 3);
233 nagbar->output.wl_name = name;
234 wl_output_add_listener(nagbar->output.wl_output,
235 &output_listener, nagbar);
236 }
237 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
238 nagbar->layer_shell = wl_registry_bind(
239 registry, name, &zwlr_layer_shell_v1_interface, 1);
240 } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0
241 && version >= ZXDG_OUTPUT_V1_NAME_SINCE_VERSION) {
242 nagbar->xdg_output_manager = wl_registry_bind(registry, name,
243 &zxdg_output_manager_v1_interface,
244 ZXDG_OUTPUT_V1_NAME_SINCE_VERSION);
245 }
246}
247
248static void handle_global_remove(void *data, struct wl_registry *registry,
249 uint32_t name) {
250 struct sway_nagbar *nagbar = data;
251 if (nagbar->output.wl_name == name) {
252 nagbar->run_display = false;
253 }
254}
255
256static const struct wl_registry_listener registry_listener = {
257 .global = handle_global,
258 .global_remove = handle_global_remove,
259};
260
261void nagbar_setup(struct sway_nagbar *nagbar) {
262 nagbar->display = wl_display_connect(NULL);
263 assert(nagbar->display);
264
265 nagbar->scale = 1;
266
267 struct wl_registry *registry = wl_display_get_registry(nagbar->display);
268 wl_registry_add_listener(registry, &registry_listener, nagbar);
269 wl_display_roundtrip(nagbar->display);
270 assert(nagbar->compositor && nagbar->layer_shell && nagbar->shm);
271
272 while (nagbar->querying_outputs > 0) {
273 wl_display_roundtrip(nagbar->display);
274 }
275
276 if (!nagbar->output.wl_output) {
277 if (nagbar->output.name) {
278 wlr_log(WLR_ERROR, "Output '%s' not found", nagbar->output.name);
279 } else {
280 wlr_log(WLR_ERROR, "No outputs detected");
281 }
282 nagbar_destroy(nagbar);
283 exit(EXIT_FAILURE);
284 }
285
286 struct sway_nagbar_pointer *pointer = &nagbar->pointer;
287 int scale = nagbar->scale < 1 ? 1 : nagbar->scale;
288 pointer->cursor_theme = wl_cursor_theme_load(
289 NULL, 24 * scale, nagbar->shm);
290 assert(pointer->cursor_theme);
291 struct wl_cursor *cursor =
292 wl_cursor_theme_get_cursor(pointer->cursor_theme, "left_ptr");
293 assert(cursor);
294 pointer->cursor_image = cursor->images[0];
295 pointer->cursor_surface = wl_compositor_create_surface(nagbar->compositor);
296 assert(pointer->cursor_surface);
297
298 nagbar->surface = wl_compositor_create_surface(nagbar->compositor);
299 assert(nagbar->surface);
300 nagbar->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
301 nagbar->layer_shell, nagbar->surface, nagbar->output.wl_output,
302 ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, "nagbar");
303 assert(nagbar->layer_surface);
304 zwlr_layer_surface_v1_add_listener(nagbar->layer_surface,
305 &layer_surface_listener, nagbar);
306 zwlr_layer_surface_v1_set_anchor(nagbar->layer_surface, nagbar->anchors);
307
308 wl_registry_destroy(registry);
309}
310
311void nagbar_run(struct sway_nagbar *nagbar) {
312 nagbar->run_display = true;
313 render_frame(nagbar);
314 while (nagbar->run_display && wl_display_dispatch(nagbar->display) != -1) {
315 // This is intentionally left blank
316 }
317}
318
319void nagbar_destroy(struct sway_nagbar *nagbar) {
320 nagbar->run_display = false;
321
322 free(nagbar->message);
323 free(nagbar->font);
324 while (nagbar->buttons->length) {
325 struct sway_nagbar_button *button = nagbar->buttons->items[0];
326 list_del(nagbar->buttons, 0);
327 free(button->text);
328 free(button->action);
329 free(button);
330 }
331 list_free(nagbar->buttons);
332
333 if (nagbar->layer_surface) {
334 zwlr_layer_surface_v1_destroy(nagbar->layer_surface);
335 }
336
337 if (nagbar->surface) {
338 wl_surface_destroy(nagbar->surface);
339 }
340
341 if (nagbar->output.wl_output) {
342 wl_output_destroy(nagbar->output.wl_output);
343 }
344
345 if (&nagbar->buffers[0]) {
346 destroy_buffer(&nagbar->buffers[0]);
347 }
348
349 if (&nagbar->buffers[1]) {
350 destroy_buffer(&nagbar->buffers[1]);
351 }
352
353 if (nagbar->compositor) {
354 wl_compositor_destroy(nagbar->compositor);
355 }
356
357 if (nagbar->shm) {
358 wl_shm_destroy(nagbar->shm);
359 }
360
361 if (nagbar->display) {
362 wl_display_disconnect(nagbar->display);
363 }
364}
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 @@
1#include <stdint.h>
2#include "cairo.h"
3#include "log.h"
4#include "pango.h"
5#include "pool-buffer.h"
6#include "swaynagbar/nagbar.h"
7#include "wlr-layer-shell-unstable-v1-client-protocol.h"
8
9static uint32_t render_message(cairo_t *cairo, struct sway_nagbar *nagbar) {
10 uint32_t height = nagbar->height * nagbar->scale;
11 height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale;
12
13 int text_width, text_height;
14 get_text_size(cairo, nagbar->font, &text_width, &text_height,
15 nagbar->scale, true, "%s", nagbar->message);
16
17 int padding = NAGBAR_MESSAGE_PADDING * nagbar->scale;
18
19 uint32_t ideal_height = text_height + padding * 2;
20 uint32_t ideal_surface_height = ideal_height / nagbar->scale;
21 if (nagbar->height < ideal_surface_height) {
22 return ideal_surface_height;
23 }
24
25 cairo_set_source_u32(cairo, nagbar->colors.text);
26 cairo_move_to(cairo, padding, (int)(height / 2.0 - text_height / 2.0));
27 pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s",
28 nagbar->message);
29
30 return nagbar->height;
31}
32
33static uint32_t render_button(cairo_t *cairo, struct sway_nagbar *nagbar,
34 int button_index, int *x) {
35 uint32_t height = nagbar->height * nagbar->scale;
36 height -= NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale;
37 struct sway_nagbar_button *button = nagbar->buttons->items[button_index];
38
39 int text_width, text_height;
40 get_text_size(cairo, nagbar->font, &text_width, &text_height,
41 nagbar->scale, true, "%s", button->text);
42
43 int border = NAGBAR_BUTTON_BORDER_THICKNESS * nagbar->scale;
44 int padding = NAGBAR_BUTTON_PADDING * nagbar->scale;
45
46 uint32_t ideal_height = text_height + padding * 2 + border * 2;
47 uint32_t ideal_surface_height = ideal_height / nagbar->scale;
48 if (nagbar->height < ideal_surface_height) {
49 return ideal_surface_height;
50 }
51
52 button->x = *x - border - text_width - padding * 2;
53 button->y = (int)(height / 2.0 - text_height / 2.0) - padding;
54 button->width = text_width + padding * 2;
55 button->height = text_height + padding * 2;
56
57 cairo_set_source_u32(cairo, nagbar->colors.border);
58 cairo_rectangle(cairo, button->x - border, button->y - border,
59 button->width + border * 2, button->height + border * 2);
60 cairo_fill(cairo);
61
62 cairo_set_source_u32(cairo, nagbar->colors.button_background);
63 cairo_rectangle(cairo, button->x, button->y,
64 button->width, button->height);
65 cairo_fill(cairo);
66
67 cairo_set_source_u32(cairo, nagbar->colors.text);
68 cairo_move_to(cairo, button->x + padding, button->y + padding);
69 pango_printf(cairo, nagbar->font, nagbar->scale, true, "%s", button->text);
70
71 *x = button->x - border;
72
73 return nagbar->height;
74}
75
76static uint32_t render_to_cairo(cairo_t *cairo, struct sway_nagbar *nagbar) {
77 uint32_t max_height = 0;
78
79 cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE);
80 cairo_set_source_u32(cairo, nagbar->colors.background);
81 cairo_paint(cairo);
82
83 uint32_t h = render_message(cairo, nagbar);
84 max_height = h > max_height ? h : max_height;
85
86 int x = (nagbar->width - NAGBAR_BUTTON_MARGIN_RIGHT) * nagbar->scale;
87 for (int i = 0; i < nagbar->buttons->length; i++) {
88 h = render_button(cairo, nagbar, i, &x);
89 max_height = h > max_height ? h : max_height;
90 x -= NAGBAR_BUTTON_GAP * nagbar->scale;
91 if (i == 0) {
92 x -= NAGBAR_BUTTON_GAP_CLOSE * nagbar->scale;
93 }
94 }
95
96 int border = NAGBAR_BAR_BORDER_THICKNESS * nagbar->scale;
97 if (max_height > nagbar->height) {
98 max_height += border;
99 }
100 cairo_set_source_u32(cairo, nagbar->colors.border_bottom);
101 cairo_rectangle(cairo, 0, nagbar->height * nagbar->scale - border,
102 nagbar->width * nagbar->scale, border);
103 cairo_fill(cairo);
104
105 return max_height > nagbar->height ? max_height : nagbar->height;
106}
107
108void render_frame(struct sway_nagbar *nagbar) {
109 if (!nagbar->run_display) {
110 return;
111 }
112
113 cairo_surface_t *recorder = cairo_recording_surface_create(
114 CAIRO_CONTENT_COLOR_ALPHA, NULL);
115 cairo_t *cairo = cairo_create(recorder);
116 cairo_save(cairo);
117 cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
118 cairo_paint(cairo);
119 cairo_restore(cairo);
120 uint32_t height = render_to_cairo(cairo, nagbar);
121 if (height != nagbar->height) {
122 zwlr_layer_surface_v1_set_size(nagbar->layer_surface, 0, height);
123 zwlr_layer_surface_v1_set_exclusive_zone(nagbar->layer_surface,
124 height);
125 wl_surface_commit(nagbar->surface);
126 wl_display_roundtrip(nagbar->display);
127 } else {
128 nagbar->current_buffer = get_next_buffer(nagbar->shm,
129 nagbar->buffers,
130 nagbar->width * nagbar->scale,
131 nagbar->height * nagbar->scale);
132 cairo_t *shm = nagbar->current_buffer->cairo;
133
134 cairo_save(shm);
135 cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR);
136 cairo_paint(shm);
137 cairo_restore(shm);
138
139 cairo_set_source_surface(shm, recorder, 0.0, 0.0);
140 cairo_paint(shm);
141
142 wl_surface_set_buffer_scale(nagbar->surface, nagbar->scale);
143 wl_surface_attach(nagbar->surface,
144 nagbar->current_buffer->buffer, 0, 0);
145 wl_surface_damage(nagbar->surface, 0, 0,
146 nagbar->width, nagbar->height);
147 wl_surface_commit(nagbar->surface);
148 wl_display_roundtrip(nagbar->display);
149 }
150 cairo_surface_destroy(recorder);
151 cairo_destroy(cairo);
152}
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 @@
1swaynagbar(1)
2
3# NAME
4
5swaynagbar - Show a warning or error message with buttons
6
7# SYNOPSIS
8
9_swaynagbar_ [options...]
10
11# OPTIONS
12*-b, --button* <text> <action>
13 Create a button with the text _text_ that executes _action_ when pressed.
14 Multiple buttons can be defined by providing the flag multiple times.
15
16*-d, --debug*
17 Enable debugging.
18
19*-e, --edge top|bottom*
20 Set the edge to use.
21
22*-f, --font <font>*
23 Set the font to use.
24
25*-h, --help*
26 Show help message and quit.
27
28*-m, --message <msg>*
29 Set the message text.
30
31*-o, --output <output>*
32 Set the output to use. This should be the name of a _xdg\_output_. If
33 _xdg\_output\_manager_ is not supported, then the first detected output
34 will be used
35
36*-t, --type error|warning*
37 Set the message type.
38
39*-v, --version
40 Show the version number and quit.
41