From 852d302b703754fe90de1185e816069a32078ae1 Mon Sep 17 00:00:00 2001
From: Stefan Agner <stefan@agner.ch>
Date: Wed, 22 Aug 2018 23:56:07 +0200
Subject: [PATCH 2/3] desktop-shell: allow to center background image

Add the centered option as background-type. This draws the image
once in the center of the screen. If the image is larger, it will
be cropped like scale-crop.

Signed-off-by: Stefan Agner <stefan@agner.ch>
---
 clients/desktop-shell.c | 20 ++++++++++++++++++--
 man/weston.ini.man      |  5 ++++-
 2 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 4d0b1d0..0dc0f0b 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -736,7 +736,8 @@ panel_add_launcher(struct panel *panel, const char *icon, const char *path)
 enum {
 	BACKGROUND_SCALE,
 	BACKGROUND_SCALE_CROP,
-	BACKGROUND_TILE
+	BACKGROUND_TILE,
+	BACKGROUND_CENTERED
 };
 
 static void
@@ -800,14 +801,27 @@ background_draw(struct widget *widget, void *data)
 		case BACKGROUND_TILE:
 			cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
 			break;
+		case BACKGROUND_CENTERED:
+			s = (sx < sy) ? sx : sy;
+			if (s < 1.0)
+				s = 1.0;
+
+			/* align center */
+			tx = (im_w - s * allocation.width) * 0.5;
+			ty = (im_h - s * allocation.height) * 0.5;
+
+			cairo_matrix_init_translate(&matrix, tx, ty);
+			cairo_matrix_scale(&matrix, s, s);
+			cairo_pattern_set_matrix(pattern, &matrix);
+			break;
 		}
 
 		cairo_set_source(cr, pattern);
 		cairo_pattern_destroy (pattern);
 		cairo_surface_destroy(image);
+		cairo_mask(cr, pattern);
 	}
 
-	cairo_paint(cr);
 	cairo_destroy(cr);
 	cairo_surface_destroy(surface);
 
@@ -1143,6 +1157,8 @@ background_create(struct desktop *desktop, struct output *output)
 		background->type = BACKGROUND_SCALE_CROP;
 	} else if (strcmp(type, "tile") == 0) {
 		background->type = BACKGROUND_TILE;
+	} else if (strcmp(type, "centered") == 0) {
+		background->type = BACKGROUND_CENTERED;
 	} else {
 		background->type = -1;
 		fprintf(stderr, "invalid background-type: %s\n",
diff --git a/man/weston.ini.man b/man/weston.ini.man
index 199b465..3ee47fa 100644
--- a/man/weston.ini.man
+++ b/man/weston.ini.man
@@ -260,7 +260,10 @@ sets the path for the background image file (string).
 .TP 7
 .BI "background-type=" tile
 determines how the background image is drawn (string). Can be
-.BR scale ", " scale-crop " or " tile " (default)."
+.BR centered ", " scale ", " scale-crop " or " tile " (default)."
+Centered shows the image once centered. If the image is smaller than the
+output, the rest of the surface will be in background color. If the image
+size does fit the output it will be cropped left and right, or top and bottom.
 Scale means scaled to fit the output precisely, not preserving aspect ratio.
 Scale-crop preserves aspect ratio, scales the background image just big
 enough to cover the output, and centers it. The image ends up cropped from
-- 
2.7.4

