From 78c2e293282d20a2627f68a5b2921b2066ade2a3 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 16:53:02 +0200 Subject: [PATCH 1/7] swaybg: make argv const --- swaybg/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swaybg/main.c b/swaybg/main.c index 5a12eec69..e65108005 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -23,7 +23,7 @@ void sway_terminate(void) { exit(1); } -int main(int argc, char **argv) { +int main(int argc, const char **argv) { init_log(L_INFO); surfaces = create_list(); registry = registry_poll(); @@ -49,7 +49,7 @@ int main(int argc, char **argv) { desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); list_add(surfaces, window); - char *scaling_mode = argv[3]; + const char *scaling_mode = argv[3]; cairo_surface_t *image = cairo_image_surface_create_from_png(argv[2]); double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); From 504ac84cbcd52d3052b980f576a1bceed3ebc69c Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 16:53:18 +0200 Subject: [PATCH 2/7] swaybg: check for exact number of arguments --- swaybg/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swaybg/main.c b/swaybg/main.c index e65108005..cb9cc42bc 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -28,7 +28,7 @@ int main(int argc, const char **argv) { surfaces = create_list(); registry = registry_poll(); - if (argc < 4) { + if (argc != 4) { sway_abort("Do not run this program manually. See man 5 sway and look for output options."); } From 904983c3753f3b1f275dedc1c7841542f77c9f89 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 16:57:35 +0200 Subject: [PATCH 3/7] swaybg: implement scaling mode "stretch" --- swaybg/main.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/swaybg/main.c b/swaybg/main.c index cb9cc42bc..5510f2ef8 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -12,6 +12,10 @@ list_t *surfaces; struct registry *registry; +enum scaling_mode_t { + SCALING_MODE_STRETCH, +}; + void sway_terminate(void) { int i; for (i = 0; i < surfaces->length; ++i) { @@ -49,16 +53,33 @@ int main(int argc, const char **argv) { desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); list_add(surfaces, window); - const char *scaling_mode = argv[3]; cairo_surface_t *image = cairo_image_surface_create_from_png(argv[2]); double width = cairo_image_surface_get_width(image); double height = cairo_image_surface_get_height(image); + const char *scaling_mode_str = argv[3]; + enum scaling_mode_t scaling_mode; + if (strcmp(scaling_mode_str, "stretch") == 0) { + scaling_mode = SCALING_MODE_STRETCH; + } else { + sway_abort("Unsupported scaling mode: %s", scaling_mode_str); + } + for (i = 0; i < surfaces->length; ++i) { struct window *window = surfaces->items[i]; if (window_prerender(window) && window->cairo) { - cairo_scale(window->cairo, window->width / width, window->height / height); - cairo_set_source_surface(window->cairo, image, 0, 0); + + switch (scaling_mode) { + case SCALING_MODE_STRETCH: + cairo_scale(window->cairo, + (double) window->width / width, + (double) window->height / height); + cairo_set_source_surface(window->cairo, image, 0, 0); + break; + default: + sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); + } + cairo_paint(window->cairo); window_render(window); From 81a87ec7a721b2f14c129f86b8403cce8c434435 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 20:52:34 +0200 Subject: [PATCH 4/7] swaybg: implement scaling mode "fill" --- swaybg/main.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/swaybg/main.c b/swaybg/main.c index 5510f2ef8..a85c56bdc 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -14,6 +14,7 @@ struct registry *registry; enum scaling_mode_t { SCALING_MODE_STRETCH, + SCALING_MODE_FILL, }; void sway_terminate(void) { @@ -61,6 +62,8 @@ int main(int argc, const char **argv) { enum scaling_mode_t scaling_mode; if (strcmp(scaling_mode_str, "stretch") == 0) { scaling_mode = SCALING_MODE_STRETCH; + } else if (strcmp(scaling_mode_str, "fill") == 0) { + scaling_mode = SCALING_MODE_FILL; } else { sway_abort("Unsupported scaling mode: %s", scaling_mode_str); } @@ -76,6 +79,26 @@ int main(int argc, const char **argv) { (double) window->height / height); cairo_set_source_surface(window->cairo, image, 0, 0); break; + case SCALING_MODE_FILL: + { + double window_ratio = (double) window->width / window->height; + double bg_ratio = width / height; + + if (window_ratio > bg_ratio) { + double scale = (double) window->width / width; + cairo_scale(window->cairo, scale, scale); + cairo_set_source_surface(window->cairo, image, + 0, + (double) window->height/2 / scale - height/2); + } else { + double scale = (double) window->height / height; + cairo_scale(window->cairo, scale, scale); + cairo_set_source_surface(window->cairo, image, + (double) window->width/2 / scale - width/2, + 0); + } + } + break; default: sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); } From 621062f18d3511a2b0fae0a18f168e07f1ff1b0d Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 20:53:13 +0200 Subject: [PATCH 5/7] swaybg: implement scaling mode "center" --- swaybg/main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/swaybg/main.c b/swaybg/main.c index a85c56bdc..481fdf9a5 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -15,6 +15,7 @@ struct registry *registry; enum scaling_mode_t { SCALING_MODE_STRETCH, SCALING_MODE_FILL, + SCALING_MODE_CENTER, }; void sway_terminate(void) { @@ -64,6 +65,8 @@ int main(int argc, const char **argv) { scaling_mode = SCALING_MODE_STRETCH; } else if (strcmp(scaling_mode_str, "fill") == 0) { scaling_mode = SCALING_MODE_FILL; + } else if (strcmp(scaling_mode_str, "center") == 0) { + scaling_mode = SCALING_MODE_CENTER; } else { sway_abort("Unsupported scaling mode: %s", scaling_mode_str); } @@ -99,6 +102,11 @@ int main(int argc, const char **argv) { } } break; + case SCALING_MODE_CENTER: + cairo_set_source_surface(window->cairo, image, + (double) window->width/2 - width/2, + (double) window->height/2 - height/2); + break; default: sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); } From 8630bc37524434096165acff5493eff399387df2 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 21:07:34 +0200 Subject: [PATCH 6/7] swaybg: implement scaling mode "tile" --- swaybg/main.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/swaybg/main.c b/swaybg/main.c index 481fdf9a5..7cfe13201 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -16,6 +16,7 @@ enum scaling_mode_t { SCALING_MODE_STRETCH, SCALING_MODE_FILL, SCALING_MODE_CENTER, + SCALING_MODE_TILE, }; void sway_terminate(void) { @@ -67,6 +68,8 @@ int main(int argc, const char **argv) { scaling_mode = SCALING_MODE_FILL; } else if (strcmp(scaling_mode_str, "center") == 0) { scaling_mode = SCALING_MODE_CENTER; + } else if (strcmp(scaling_mode_str, "tile") == 0) { + scaling_mode = SCALING_MODE_TILE; } else { sway_abort("Unsupported scaling mode: %s", scaling_mode_str); } @@ -107,6 +110,13 @@ int main(int argc, const char **argv) { (double) window->width/2 - width/2, (double) window->height/2 - height/2); break; + case SCALING_MODE_TILE: + { + cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); + cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); + cairo_set_source(window->cairo, pattern); + } + break; default: sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); } From be3fae148bc2a48a1d828b72221c0fd69bea79f7 Mon Sep 17 00:00:00 2001 From: Christoph Gysin Date: Wed, 25 Nov 2015 22:26:21 +0200 Subject: [PATCH 7/7] swaybg: implement scaling mode "fit" --- sway.5.txt | 2 +- sway/commands.c | 1 + swaybg/main.c | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/sway.5.txt b/sway.5.txt index 700de8b2e..8a9494650 100644 --- a/sway.5.txt +++ b/sway.5.txt @@ -127,7 +127,7 @@ Commands **output** :: Sets the wallpaper for the given output to the specified file, using the given - scaling mode (one of "stretch", "fill", "center", "tile"). + scaling mode (one of "stretch", "fill", "fit", "center", "tile"). **output** disable:: Disables the specified output. diff --git a/sway/commands.c b/sway/commands.c index 6a4af43c2..c63aa3204 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -85,6 +85,7 @@ static char *bg_options[] = { "stretch", "center", "fill", + "fit", "tile" }; diff --git a/swaybg/main.c b/swaybg/main.c index 7cfe13201..e4c699c10 100644 --- a/swaybg/main.c +++ b/swaybg/main.c @@ -15,6 +15,7 @@ struct registry *registry; enum scaling_mode_t { SCALING_MODE_STRETCH, SCALING_MODE_FILL, + SCALING_MODE_FIT, SCALING_MODE_CENTER, SCALING_MODE_TILE, }; @@ -66,6 +67,8 @@ int main(int argc, const char **argv) { scaling_mode = SCALING_MODE_STRETCH; } else if (strcmp(scaling_mode_str, "fill") == 0) { scaling_mode = SCALING_MODE_FILL; + } else if (strcmp(scaling_mode_str, "fit") == 0) { + scaling_mode = SCALING_MODE_FIT; } else if (strcmp(scaling_mode_str, "center") == 0) { scaling_mode = SCALING_MODE_CENTER; } else if (strcmp(scaling_mode_str, "tile") == 0) { @@ -105,6 +108,26 @@ int main(int argc, const char **argv) { } } break; + case SCALING_MODE_FIT: + { + double window_ratio = (double) window->width / window->height; + double bg_ratio = width / height; + + if (window_ratio > bg_ratio) { + double scale = (double) window->height / height; + cairo_scale(window->cairo, scale, scale); + cairo_set_source_surface(window->cairo, image, + (double) window->width/2 / scale - width/2, + 0); + } else { + double scale = (double) window->width / width; + cairo_scale(window->cairo, scale, scale); + cairo_set_source_surface(window->cairo, image, + 0, + (double) window->height/2 / scale - height/2); + } + } + break; case SCALING_MODE_CENTER: cairo_set_source_surface(window->cairo, image, (double) window->width/2 - width/2,