1 #define _POSIX_C_SOURCE 200809L
2 #include <assert.h>
3 #include <ctype.h>
4 #include <getopt.h>
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <wayland-client.h>
11 #include "background-image.h"
12 #include "cairo_util.h"
13 #include "log.h"
14 #include "pool-buffer.h"
15 #include "wlr-layer-shell-unstable-v1-client-protocol.h"
16 #include "xdg-output-unstable-v1-client-protocol.h"
17
parse_color(const char * color)18 static uint32_t parse_color(const char *color) {
19 if (color[0] == '#') {
20 ++color;
21 }
22
23 int len = strlen(color);
24 if (len != 6 && len != 8) {
25 swaybg_log(LOG_DEBUG, "Invalid color %s, defaulting to 0xFFFFFFFF",
26 color);
27 return 0xFFFFFFFF;
28 }
29 uint32_t res = (uint32_t)strtoul(color, NULL, 16);
30 if (strlen(color) == 6) {
31 res = (res << 8) | 0xFF;
32 }
33 return res;
34 }
35
36 struct swaybg_state {
37 struct wl_display *display;
38 struct wl_compositor *compositor;
39 struct wl_shm *shm;
40 struct zwlr_layer_shell_v1 *layer_shell;
41 struct zxdg_output_manager_v1 *xdg_output_manager;
42 struct wl_list configs; // struct swaybg_output_config::link
43 struct wl_list outputs; // struct swaybg_output::link
44 struct wl_list images; // struct swaybg_image::link
45 bool run_display;
46 };
47
48 struct swaybg_image {
49 struct wl_list link;
50 const char *path;
51 bool load_required;
52 };
53
54 struct swaybg_output_config {
55 char *output;
56 const char *image_path;
57 struct swaybg_image *image;
58 enum background_mode mode;
59 uint32_t color;
60 struct wl_list link;
61 };
62
63 struct swaybg_output {
64 uint32_t wl_name;
65 struct wl_output *wl_output;
66 struct zxdg_output_v1 *xdg_output;
67 char *name;
68 char *identifier;
69
70 struct swaybg_state *state;
71 struct swaybg_output_config *config;
72
73 struct wl_surface *surface;
74 struct zwlr_layer_surface_v1 *layer_surface;
75
76 uint32_t width, height;
77 int32_t scale;
78
79 uint32_t configure_serial;
80 bool dirty, needs_ack;
81 int32_t committed_width, committed_height, committed_scale;
82
83 struct wl_list link;
84 };
85
is_valid_color(const char * color)86 bool is_valid_color(const char *color) {
87 int len = strlen(color);
88 if (len != 7 || color[0] != '#') {
89 swaybg_log(LOG_ERROR, "%s is not a valid color for swaybg. "
90 "Color should be specified as #rrggbb (no alpha).", color);
91 return false;
92 }
93
94 int i;
95 for (i = 1; i < len; ++i) {
96 if (!isxdigit(color[i])) {
97 return false;
98 }
99 }
100
101 return true;
102 }
103
render_frame(struct swaybg_output * output,cairo_surface_t * surface)104 static void render_frame(struct swaybg_output *output, cairo_surface_t *surface) {
105 int buffer_width = output->width * output->scale,
106 buffer_height = output->height * output->scale;
107
108 // If the last committed buffer has the same size as this one would, do
109 // not render a new buffer, because it will be identical to the old one
110 if (output->committed_width == buffer_width &&
111 output->committed_height == buffer_height) {
112 if (output->committed_scale != output->scale) {
113 wl_surface_set_buffer_scale(output->surface, output->scale);
114 wl_surface_commit(output->surface);
115
116 output->committed_scale = output->scale;
117 }
118 return;
119 }
120 struct pool_buffer buffer;
121 if (!create_buffer(&buffer, output->state->shm,
122 buffer_width, buffer_height, WL_SHM_FORMAT_ARGB8888)) {
123 return;
124 }
125
126 cairo_t *cairo = buffer.cairo;
127 cairo_save(cairo);
128 cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR);
129 cairo_paint(cairo);
130 cairo_restore(cairo);
131 if (output->config->mode == BACKGROUND_MODE_SOLID_COLOR) {
132 cairo_set_source_u32(cairo, output->config->color);
133 cairo_paint(cairo);
134 } else {
135 if (output->config->color) {
136 cairo_set_source_u32(cairo, output->config->color);
137 cairo_paint(cairo);
138 }
139
140 if (surface) {
141 render_background_image(cairo, surface,
142 output->config->mode, buffer_width, buffer_height);
143 }
144 }
145
146 wl_surface_set_buffer_scale(output->surface, output->scale);
147 wl_surface_attach(output->surface, buffer.buffer, 0, 0);
148 wl_surface_damage_buffer(output->surface, 0, 0, INT32_MAX, INT32_MAX);
149 wl_surface_commit(output->surface);
150
151 output->committed_width = buffer_width;
152 output->committed_height = buffer_height;
153 output->committed_scale = output->scale;
154
155 // we will not reuse the buffer, so destroy it immediately
156 destroy_buffer(&buffer);
157 }
158
destroy_swaybg_image(struct swaybg_image * image)159 static void destroy_swaybg_image(struct swaybg_image *image) {
160 if (!image) {
161 return;
162 }
163 wl_list_remove(&image->link);
164 free(image);
165 }
166
destroy_swaybg_output_config(struct swaybg_output_config * config)167 static void destroy_swaybg_output_config(struct swaybg_output_config *config) {
168 if (!config) {
169 return;
170 }
171 wl_list_remove(&config->link);
172 free(config->output);
173 free(config);
174 }
175
destroy_swaybg_output(struct swaybg_output * output)176 static void destroy_swaybg_output(struct swaybg_output *output) {
177 if (!output) {
178 return;
179 }
180 wl_list_remove(&output->link);
181 if (output->layer_surface != NULL) {
182 zwlr_layer_surface_v1_destroy(output->layer_surface);
183 }
184 if (output->surface != NULL) {
185 wl_surface_destroy(output->surface);
186 }
187 zxdg_output_v1_destroy(output->xdg_output);
188 wl_output_destroy(output->wl_output);
189 free(output->name);
190 free(output->identifier);
191 free(output);
192 }
193
layer_surface_configure(void * data,struct zwlr_layer_surface_v1 * surface,uint32_t serial,uint32_t width,uint32_t height)194 static void layer_surface_configure(void *data,
195 struct zwlr_layer_surface_v1 *surface,
196 uint32_t serial, uint32_t width, uint32_t height) {
197 struct swaybg_output *output = data;
198 output->width = width;
199 output->height = height;
200 output->dirty = true;
201 output->configure_serial = serial;
202 output->needs_ack = true;
203 }
204
layer_surface_closed(void * data,struct zwlr_layer_surface_v1 * surface)205 static void layer_surface_closed(void *data,
206 struct zwlr_layer_surface_v1 *surface) {
207 struct swaybg_output *output = data;
208 swaybg_log(LOG_DEBUG, "Destroying output %s (%s)",
209 output->name, output->identifier);
210 destroy_swaybg_output(output);
211 }
212
213 static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
214 .configure = layer_surface_configure,
215 .closed = layer_surface_closed,
216 };
217
output_geometry(void * data,struct wl_output * output,int32_t x,int32_t y,int32_t width_mm,int32_t height_mm,int32_t subpixel,const char * make,const char * model,int32_t transform)218 static void output_geometry(void *data, struct wl_output *output, int32_t x,
219 int32_t y, int32_t width_mm, int32_t height_mm, int32_t subpixel,
220 const char *make, const char *model, int32_t transform) {
221 // Who cares
222 }
223
output_mode(void * data,struct wl_output * output,uint32_t flags,int32_t width,int32_t height,int32_t refresh)224 static void output_mode(void *data, struct wl_output *output, uint32_t flags,
225 int32_t width, int32_t height, int32_t refresh) {
226 // Who cares
227 }
228
output_done(void * data,struct wl_output * output)229 static void output_done(void *data, struct wl_output *output) {
230 // Who cares
231 }
232
output_scale(void * data,struct wl_output * wl_output,int32_t scale)233 static void output_scale(void *data, struct wl_output *wl_output,
234 int32_t scale) {
235 struct swaybg_output *output = data;
236 output->scale = scale;
237 if (output->state->run_display && output->width > 0 && output->height > 0) {
238 output->dirty = true;
239 }
240 }
241
242 static const struct wl_output_listener output_listener = {
243 .geometry = output_geometry,
244 .mode = output_mode,
245 .done = output_done,
246 .scale = output_scale,
247 };
248
xdg_output_handle_logical_position(void * data,struct zxdg_output_v1 * xdg_output,int32_t x,int32_t y)249 static void xdg_output_handle_logical_position(void *data,
250 struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y) {
251 // Who cares
252 }
253
xdg_output_handle_logical_size(void * data,struct zxdg_output_v1 * xdg_output,int32_t width,int32_t height)254 static void xdg_output_handle_logical_size(void *data,
255 struct zxdg_output_v1 *xdg_output, int32_t width, int32_t height) {
256 // Who cares
257 }
258
find_config(struct swaybg_output * output,const char * name)259 static void find_config(struct swaybg_output *output, const char *name) {
260 struct swaybg_output_config *config = NULL;
261 wl_list_for_each(config, &output->state->configs, link) {
262 if (strcmp(config->output, name) == 0) {
263 output->config = config;
264 return;
265 } else if (!output->config && strcmp(config->output, "*") == 0) {
266 output->config = config;
267 }
268 }
269 }
270
xdg_output_handle_name(void * data,struct zxdg_output_v1 * xdg_output,const char * name)271 static void xdg_output_handle_name(void *data,
272 struct zxdg_output_v1 *xdg_output, const char *name) {
273 struct swaybg_output *output = data;
274 output->name = strdup(name);
275
276 // If description was sent first, the config may already be populated. If
277 // there is an identifier config set, keep it.
278 if (!output->config || strcmp(output->config->output, "*") == 0) {
279 find_config(output, name);
280 }
281 }
282
xdg_output_handle_description(void * data,struct zxdg_output_v1 * xdg_output,const char * description)283 static void xdg_output_handle_description(void *data,
284 struct zxdg_output_v1 *xdg_output, const char *description) {
285 struct swaybg_output *output = data;
286
287 // wlroots currently sets the description to `make model serial (name)`
288 // If this changes in the future, this will need to be modified.
289 char *paren = strrchr(description, '(');
290 if (paren) {
291 size_t length = paren - description;
292 output->identifier = malloc(length);
293 if (!output->identifier) {
294 swaybg_log(LOG_ERROR, "Failed to allocate output identifier");
295 return;
296 }
297 strncpy(output->identifier, description, length);
298 output->identifier[length - 1] = '\0';
299
300 find_config(output, output->identifier);
301 }
302 }
303
create_layer_surface(struct swaybg_output * output)304 static void create_layer_surface(struct swaybg_output *output) {
305 output->surface = wl_compositor_create_surface(output->state->compositor);
306 assert(output->surface);
307
308 // Empty input region
309 struct wl_region *input_region =
310 wl_compositor_create_region(output->state->compositor);
311 assert(input_region);
312 wl_surface_set_input_region(output->surface, input_region);
313 wl_region_destroy(input_region);
314
315 output->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
316 output->state->layer_shell, output->surface, output->wl_output,
317 ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND, "wallpaper");
318 assert(output->layer_surface);
319
320 zwlr_layer_surface_v1_set_size(output->layer_surface, 0, 0);
321 zwlr_layer_surface_v1_set_anchor(output->layer_surface,
322 ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
323 ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
324 ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
325 ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
326 zwlr_layer_surface_v1_set_exclusive_zone(output->layer_surface, -1);
327 zwlr_layer_surface_v1_add_listener(output->layer_surface,
328 &layer_surface_listener, output);
329 wl_surface_commit(output->surface);
330 }
331
xdg_output_handle_done(void * data,struct zxdg_output_v1 * xdg_output)332 static void xdg_output_handle_done(void *data,
333 struct zxdg_output_v1 *xdg_output) {
334 struct swaybg_output *output = data;
335 if (!output->config) {
336 swaybg_log(LOG_DEBUG, "Could not find config for output %s (%s)",
337 output->name, output->identifier);
338 destroy_swaybg_output(output);
339 } else if (!output->layer_surface) {
340 swaybg_log(LOG_DEBUG, "Found config %s for output %s (%s)",
341 output->config->output, output->name, output->identifier);
342 create_layer_surface(output);
343 }
344 }
345
346 static const struct zxdg_output_v1_listener xdg_output_listener = {
347 .logical_position = xdg_output_handle_logical_position,
348 .logical_size = xdg_output_handle_logical_size,
349 .name = xdg_output_handle_name,
350 .description = xdg_output_handle_description,
351 .done = xdg_output_handle_done,
352 };
353
handle_global(void * data,struct wl_registry * registry,uint32_t name,const char * interface,uint32_t version)354 static void handle_global(void *data, struct wl_registry *registry,
355 uint32_t name, const char *interface, uint32_t version) {
356 struct swaybg_state *state = data;
357 if (strcmp(interface, wl_compositor_interface.name) == 0) {
358 state->compositor =
359 wl_registry_bind(registry, name, &wl_compositor_interface, 4);
360 } else if (strcmp(interface, wl_shm_interface.name) == 0) {
361 state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
362 } else if (strcmp(interface, wl_output_interface.name) == 0) {
363 struct swaybg_output *output = calloc(1, sizeof(struct swaybg_output));
364 output->state = state;
365 output->wl_name = name;
366 output->wl_output =
367 wl_registry_bind(registry, name, &wl_output_interface, 3);
368 wl_output_add_listener(output->wl_output, &output_listener, output);
369 wl_list_insert(&state->outputs, &output->link);
370
371 if (state->run_display) {
372 output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
373 state->xdg_output_manager, output->wl_output);
374 zxdg_output_v1_add_listener(output->xdg_output,
375 &xdg_output_listener, output);
376 }
377 } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
378 state->layer_shell =
379 wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1);
380 } else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
381 state->xdg_output_manager = wl_registry_bind(registry, name,
382 &zxdg_output_manager_v1_interface, 2);
383 }
384 }
385
handle_global_remove(void * data,struct wl_registry * registry,uint32_t name)386 static void handle_global_remove(void *data, struct wl_registry *registry,
387 uint32_t name) {
388 struct swaybg_state *state = data;
389 struct swaybg_output *output, *tmp;
390 wl_list_for_each_safe(output, tmp, &state->outputs, link) {
391 if (output->wl_name == name) {
392 swaybg_log(LOG_DEBUG, "Destroying output %s (%s)",
393 output->name, output->identifier);
394 destroy_swaybg_output(output);
395 break;
396 }
397 }
398 }
399
400 static const struct wl_registry_listener registry_listener = {
401 .global = handle_global,
402 .global_remove = handle_global_remove,
403 };
404
store_swaybg_output_config(struct swaybg_state * state,struct swaybg_output_config * config)405 static bool store_swaybg_output_config(struct swaybg_state *state,
406 struct swaybg_output_config *config) {
407 struct swaybg_output_config *oc = NULL;
408 wl_list_for_each(oc, &state->configs, link) {
409 if (strcmp(config->output, oc->output) == 0) {
410 // Merge on top
411 if (config->image_path) {
412 oc->image_path = config->image_path;
413 }
414 if (config->color) {
415 oc->color = config->color;
416 }
417 if (config->mode != BACKGROUND_MODE_INVALID) {
418 oc->mode = config->mode;
419 }
420 return false;
421 }
422 }
423 // New config, just add it
424 wl_list_insert(&state->configs, &config->link);
425 return true;
426 }
427
parse_command_line(int argc,char ** argv,struct swaybg_state * state)428 static void parse_command_line(int argc, char **argv,
429 struct swaybg_state *state) {
430 static struct option long_options[] = {
431 {"color", required_argument, NULL, 'c'},
432 {"help", no_argument, NULL, 'h'},
433 {"image", required_argument, NULL, 'i'},
434 {"mode", required_argument, NULL, 'm'},
435 {"output", required_argument, NULL, 'o'},
436 {"version", no_argument, NULL, 'v'},
437 {0, 0, 0, 0}
438 };
439
440 const char *usage =
441 "Usage: swaybg <options...>\n"
442 "\n"
443 " -c, --color Set the background color.\n"
444 " -h, --help Show help message and quit.\n"
445 " -i, --image Set the image to display.\n"
446 " -m, --mode Set the mode to use for the image.\n"
447 " -o, --output Set the output to operate on or * for all.\n"
448 " -v, --version Show the version number and quit.\n"
449 "\n"
450 "Background Modes:\n"
451 " stretch, fit, fill, center, tile, or solid_color\n";
452
453 struct swaybg_output_config *config = calloc(sizeof(struct swaybg_output_config), 1);
454 config->output = strdup("*");
455 config->mode = BACKGROUND_MODE_INVALID;
456 wl_list_init(&config->link); // init for safe removal
457
458 int c;
459 while (1) {
460 int option_index = 0;
461 c = getopt_long(argc, argv, "c:hi:m:o:v", long_options, &option_index);
462 if (c == -1) {
463 break;
464 }
465 switch (c) {
466 case 'c': // color
467 if (!is_valid_color(optarg)) {
468 swaybg_log(LOG_ERROR, "Invalid color: %s", optarg);
469 continue;
470 }
471 config->color = parse_color(optarg);
472 break;
473 case 'i': // image
474 config->image_path = optarg;
475 break;
476 case 'm': // mode
477 config->mode = parse_background_mode(optarg);
478 if (config->mode == BACKGROUND_MODE_INVALID) {
479 swaybg_log(LOG_ERROR, "Invalid mode: %s", optarg);
480 }
481 break;
482 case 'o': // output
483 if (config && !store_swaybg_output_config(state, config)) {
484 // Empty config or merged on top of an existing one
485 destroy_swaybg_output_config(config);
486 }
487 config = calloc(sizeof(struct swaybg_output_config), 1);
488 config->output = strdup(optarg);
489 config->mode = BACKGROUND_MODE_INVALID;
490 wl_list_init(&config->link); // init for safe removal
491 break;
492 case 'v': // version
493 fprintf(stdout, "swaybg version " SWAYBG_VERSION "\n");
494 exit(EXIT_SUCCESS);
495 break;
496 default:
497 fprintf(c == 'h' ? stdout : stderr, "%s", usage);
498 exit(c == 'h' ? EXIT_SUCCESS : EXIT_FAILURE);
499 }
500 }
501 if (config && !store_swaybg_output_config(state, config)) {
502 // Empty config or merged on top of an existing one
503 destroy_swaybg_output_config(config);
504 }
505
506 // Check for invalid options
507 if (optind < argc) {
508 config = NULL;
509 struct swaybg_output_config *tmp = NULL;
510 wl_list_for_each_safe(config, tmp, &state->configs, link) {
511 destroy_swaybg_output_config(config);
512 }
513 // continue into empty list
514 }
515 if (wl_list_empty(&state->configs)) {
516 fprintf(stderr, "%s", usage);
517 exit(EXIT_FAILURE);
518 }
519
520 // Set default mode and remove empties
521 config = NULL;
522 struct swaybg_output_config *tmp = NULL;
523 wl_list_for_each_safe(config, tmp, &state->configs, link) {
524 if (!config->image_path && !config->color) {
525 destroy_swaybg_output_config(config);
526 } else if (config->mode == BACKGROUND_MODE_INVALID) {
527 config->mode = config->image_path
528 ? BACKGROUND_MODE_STRETCH
529 : BACKGROUND_MODE_SOLID_COLOR;
530 }
531 }
532 }
533
main(int argc,char ** argv)534 int main(int argc, char **argv) {
535 swaybg_log_init(LOG_DEBUG);
536
537 struct swaybg_state state = {0};
538 wl_list_init(&state.configs);
539 wl_list_init(&state.outputs);
540 wl_list_init(&state.images);
541
542 parse_command_line(argc, argv, &state);
543
544 // Identify distinct image paths which will need to be loaded
545 struct swaybg_image *image;
546 struct swaybg_output_config *config;
547 wl_list_for_each(config, &state.configs, link) {
548 if (!config->image_path) {
549 continue;
550 }
551 wl_list_for_each(image, &state.images, link) {
552 if (strcmp(image->path, config->image_path) == 0) {
553 config->image = image;
554 break;
555 }
556 }
557 if (config->image) {
558 continue;
559 }
560 image = calloc(1, sizeof(struct swaybg_image));
561 image->path = config->image_path;
562 wl_list_insert(&state.images, &image->link);
563 config->image = image;
564 }
565
566 state.display = wl_display_connect(NULL);
567 if (!state.display) {
568 swaybg_log(LOG_ERROR, "Unable to connect to the compositor. "
569 "If your compositor is running, check or set the "
570 "WAYLAND_DISPLAY environment variable.");
571 return 1;
572 }
573
574 struct wl_registry *registry = wl_display_get_registry(state.display);
575 wl_registry_add_listener(registry, ®istry_listener, &state);
576 wl_display_roundtrip(state.display);
577 if (state.compositor == NULL || state.shm == NULL ||
578 state.layer_shell == NULL || state.xdg_output_manager == NULL) {
579 swaybg_log(LOG_ERROR, "Missing a required Wayland interface");
580 return 1;
581 }
582
583 struct swaybg_output *output;
584 wl_list_for_each(output, &state.outputs, link) {
585 output->xdg_output = zxdg_output_manager_v1_get_xdg_output(
586 state.xdg_output_manager, output->wl_output);
587 zxdg_output_v1_add_listener(output->xdg_output,
588 &xdg_output_listener, output);
589 }
590
591 state.run_display = true;
592 while (wl_display_dispatch(state.display) != -1 && state.run_display) {
593 // Send acks, and determine which images need to be loaded
594 wl_list_for_each(output, &state.outputs, link) {
595 if (output->needs_ack) {
596 output->needs_ack = false;
597 zwlr_layer_surface_v1_ack_configure(
598 output->layer_surface,
599 output->configure_serial);
600 }
601
602 int buffer_width = output->width * output->scale,
603 buffer_height = output->height * output->scale;
604 bool buffer_change =
605 output->committed_height != buffer_height ||
606 output->committed_width != buffer_width;
607 if (output->dirty && output->config->image && buffer_change) {
608 output->config->image->load_required = true;
609 }
610 }
611
612 // Load images, render associated frames, and unload
613 wl_list_for_each(image, &state.images, link) {
614 if (!image->load_required) {
615 continue;
616 }
617
618 cairo_surface_t *surface = load_background_image(image->path);
619 if (!surface) {
620 swaybg_log(LOG_ERROR, "Failed to load image: %s", image->path);
621 continue;
622 }
623
624 wl_list_for_each(output, &state.outputs, link) {
625 if (output->dirty && output->config->image == image) {
626 output->dirty = false;
627 render_frame(output, surface);
628 }
629 }
630
631 image->load_required = false;
632 cairo_surface_destroy(surface);
633 }
634
635 // Redraw outputs without associated image
636 wl_list_for_each(output, &state.outputs, link) {
637 if (output->dirty) {
638 output->dirty = false;
639 render_frame(output, NULL);
640 }
641 }
642 }
643
644 struct swaybg_output *tmp_output;
645 wl_list_for_each_safe(output, tmp_output, &state.outputs, link) {
646 destroy_swaybg_output(output);
647 }
648
649 struct swaybg_output_config *tmp_config = NULL;
650 wl_list_for_each_safe(config, tmp_config, &state.configs, link) {
651 destroy_swaybg_output_config(config);
652 }
653
654 struct swaybg_image *tmp_image;
655 wl_list_for_each_safe(image, tmp_image, &state.images, link) {
656 destroy_swaybg_image(image);
657 }
658
659 return 0;
660 }
661