From 65b8a5c3ce023584cdc5dd84394ea1101c9f23e9 Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Wed, 18 Nov 2015 22:14:57 -0500
Subject: [PATCH] Add background handling

This does not work as expected. I think the problem is on the wlc side.
Please review, @Cloudef. To reproduce the issues:

1. Run sway
2. Open terminal in sway
3. Run swaybg

swaybg will create a surface and ask to have it set as the background,
but wlc_handle_from_wl_surface_resource will return 0. If the swaybg
surface is a shell surface, then it works - but wlc complains about the
pointer type and segfaults as soon as the pre-render hook tries to draw
the background.
---
 include/extensions.h | 13 +++++++++++++
 sway/extensions.c    | 19 ++++++++++++++++---
 sway/handlers.c      | 19 ++++++++++++++++++-
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/include/extensions.h b/include/extensions.h
index e122c59a..3e2d1cde 100644
--- a/include/extensions.h
+++ b/include/extensions.h
@@ -1,6 +1,19 @@
 #ifndef _SWAY_EXTENSIONS_H
 #define _SWAY_EXTENSIONS_H
 
+#include "list.h"
+
+struct background_config {
+        wlc_handle output;
+        wlc_handle surface;
+};
+
+struct desktop_shell_state {
+        list_t *backgrounds;
+};
+
+extern struct desktop_shell_state desktop_shell;
+
 void register_extensions(void);
 
 #endif
diff --git a/sway/extensions.c b/sway/extensions.c
index a37ceaa8..5ef8a0ff 100644
--- a/sway/extensions.c
+++ b/sway/extensions.c
@@ -2,14 +2,26 @@
 #include <wlc/wlc-wayland.h>
 #include "wayland-desktop-shell-server-protocol.h"
 #include "log.h"
+#include "extensions.h"
+
+struct desktop_shell_state desktop_shell;
 
 static void set_background(struct wl_client *client, struct wl_resource *resource,
-		struct wl_resource *output, struct wl_resource *surface) {
-	sway_log(L_DEBUG, "Surface requesting background for output");
+		struct wl_resource *_output, struct wl_resource *_surface) {
+	wlc_handle output = wlc_handle_from_wl_output_resource(_output);
+	wlc_handle surface = wlc_handle_from_wl_surface_resource(_surface);
+	sway_log(L_DEBUG, "Setting surface %d as background for output %d", (int)surface, (int)output);
+	if (!output || !surface) {
+		return;
+	}
+	struct background_config *config = malloc(sizeof(struct background_config));
+	config->output = output;
+	config->surface = surface;
+	list_add(desktop_shell.backgrounds, config);
 }
 
 static struct desktop_shell_interface desktop_shell_implementation = {
-	.set_background = set_background,
+	.set_background = set_background
 };
 
 static void desktop_shell_bind(struct wl_client *client, void *data,
@@ -29,4 +41,5 @@ static void desktop_shell_bind(struct wl_client *client, void *data,
 
 void register_extensions(void) {
 	wl_global_create(wlc_get_wl_display(), &desktop_shell_interface, 1, NULL, desktop_shell_bind);
+	desktop_shell.backgrounds = create_list();
 }
diff --git a/sway/handlers.c b/sway/handlers.c
index 3f3c1bdd..9e98cffe 100644
--- a/sway/handlers.c
+++ b/sway/handlers.c
@@ -3,6 +3,7 @@
 #include <stdbool.h>
 #include <math.h>
 #include <wlc/wlc.h>
+#include <wlc/wlc-wayland.h>
 #include <ctype.h>
 
 #include "handlers.h"
@@ -17,6 +18,7 @@
 #include "focus.h"
 #include "input_state.h"
 #include "resize.h"
+#include "extensions.h"
 
 // Event should be sent to client
 #define EVENT_PASSTHROUGH false
@@ -63,6 +65,18 @@ static void handle_output_destroyed(wlc_handle output) {
 	}
 }
 
+static void handle_output_pre_render(wlc_handle output) {
+	int i;
+	for (i = 0; i < desktop_shell.backgrounds->length; ++i) {
+		struct background_config *config = desktop_shell.backgrounds->items[i];
+		if (config->output == output) {
+			sway_log(L_DEBUG, "Rendering background surface %d", (int)config->surface);
+			wlc_surface_render(config->surface, &(struct wlc_geometry){ wlc_origin_zero, *wlc_output_get_resolution(output) });
+			break;
+		}
+	}
+}
+
 static void handle_output_resolution_change(wlc_handle output, const struct wlc_size *from, const struct wlc_size *to) {
 	sway_log(L_DEBUG, "Output %u resolution changed to %d x %d", (unsigned int)output, to->w, to->h);
 	swayc_t *c = swayc_by_handle(output);
@@ -463,7 +477,10 @@ struct wlc_interface interface = {
 		.created = handle_output_created,
 		.destroyed = handle_output_destroyed,
 		.resolution = handle_output_resolution_change,
-		.focus = handle_output_focused
+		.focus = handle_output_focused,
+		.render = {
+			.pre = handle_output_pre_render
+		}
 	},
 	.view = {
 		.created = handle_view_created,