From 7eafcc75f6f8abd2346e0d72b063bc10ce24378f Mon Sep 17 00:00:00 2001
From: Drew DeVault <sir@cmpwn.com>
Date: Sat, 11 Nov 2017 11:58:43 -0500
Subject: Initialize outputs from backend and add to tree

---
 include/sway/container.h | 20 +++++++++++++++-----
 include/sway/server.h    | 10 +++++++++-
 sway/CMakeLists.txt      | 12 ++++++++----
 sway/config.c            | 14 ++++++++++----
 sway/desktop/output.c    | 21 +++++++++++++++++++++
 sway/ipc-json.c          | 17 +++++++++++------
 sway/main.c              |  3 ++-
 sway/server.c            | 30 +++++++++++++++++-------------
 sway/tree/container.c    | 20 ++++++++++----------
 9 files changed, 103 insertions(+), 44 deletions(-)
 create mode 100644 sway/desktop/output.c

diff --git a/include/sway/container.h b/include/sway/container.h
index 37192ce3..f6aae7d1 100644
--- a/include/sway/container.h
+++ b/include/sway/container.h
@@ -2,6 +2,7 @@
 #define _SWAY_CONTAINER_H
 #include <sys/types.h>
 #include <wlc/wlc.h>
+#include <wlr/types/wlr_output.h>
 #include <stdint.h>
 
 #include "list.h"
@@ -27,6 +28,14 @@ enum swayc_types {
 	C_TYPES,
 };
 
+enum swayc_view_types {
+	V_WL_SHELL,
+	V_XDG_SHELL_V6,
+	V_XWAYLAND,
+	// Keep last
+	V_TYPES,
+};
+
 /**
  * Different ways to arrange a container.
  */
@@ -63,12 +72,13 @@ enum swayc_border_types {
  * The tree is made of these. Views are containers that cannot have children.
  */
 struct sway_container {
-	/**
-	 * If this container maps to a WLC object, this is set to that object's
-	 * handle. Otherwise, NULL.
-	 */
+	// TODO WLR: reconcile these
 	wlc_handle handle;
 
+	union {
+		struct wlr_output *output;
+	} _handle;
+
 	/**
 	 * A unique ID to identify this container. Primarily used in the
 	 * get_tree JSON output.
@@ -179,7 +189,7 @@ enum visibility_mask {
 /**
  * Allocates a new output container.
  */
-swayc_t *new_output(wlc_handle handle);
+swayc_t *new_output(struct wlr_output *wlr_output);
 /**
  * Allocates a new workspace container.
  */
diff --git a/include/sway/server.h b/include/sway/server.h
index 22069f9c..f3e86bcb 100644
--- a/include/sway/server.h
+++ b/include/sway/server.h
@@ -12,6 +12,7 @@
 struct sway_server {
 	struct wl_display *wl_display;
 	struct wl_event_loop *wl_event_loop;
+	const char *socket;
 
 	struct wlr_backend *backend;
 	struct wlr_renderer *renderer;
@@ -19,11 +20,18 @@ struct sway_server {
 	struct wlr_data_device_manager *data_device_manager;
 
 	struct sway_input *input;
+
+	struct wl_listener output_add;
+	struct wl_listener output_remove;
+	struct wl_listener output_frame;
 };
 
+struct sway_server server;
+
 bool server_init(struct sway_server *server);
 void server_fini(struct sway_server *server);
+void server_run(struct sway_server *server);
 
-struct sway_server server;
+void output_add_notify(struct wl_listener *listener, void *data);
 
 #endif
diff --git a/sway/CMakeLists.txt b/sway/CMakeLists.txt
index 1df24222..617a71f0 100644
--- a/sway/CMakeLists.txt
+++ b/sway/CMakeLists.txt
@@ -18,14 +18,20 @@ file(GLOB cmds
 )
 
 add_executable(sway
-	commands.c
-	${cmds}
+    desktop/output.c
+
 	tree/container.c
 	tree/criteria.c
 	tree/focus.c
 	tree/output.c
 	tree/workspace.c
 	tree/layout.c
+
+	input/input.c
+
+	commands.c
+	${cmds}
+
 	base64.c
 	config.c
 	debug_log.c
@@ -36,8 +42,6 @@ add_executable(sway
 	border.c
 	security.c
     server.c
-
-	input/input.c
 )
 
 add_definitions(
diff --git a/sway/config.c b/sway/config.c
index a33b8edc..78ab8f3b 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -15,6 +15,7 @@
 #include <float.h>
 #include <dirent.h>
 #include <strings.h>
+#include <wlr/types/wlr_output.h>
 #include "wayland-desktop-shell-server-protocol.h"
 #include "sway/commands.h"
 #include "sway/config.h"
@@ -930,6 +931,7 @@ void merge_output_config(struct output_config *dst, struct output_config *src) {
 }
 
 static void invoke_swaybar(struct bar_config *bar) {
+	return; // TODO WLR
 	// Pipe to communicate errors
 	int filedes[2];
 	if (pipe(filedes) == -1) {
@@ -1128,13 +1130,15 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 		output->height = oc->height;
 
 		sway_log(L_DEBUG, "Set %s size to %ix%i (%d)", oc->name, oc->width, oc->height, oc->scale);
-		struct wlc_size new_size = { .w = oc->width, .h = oc->height };
-		wlc_output_set_resolution(output->handle, &new_size, (uint32_t)oc->scale);
+		// TODO WLR: modes
+		//struct wlc_size new_size = { .w = oc->width, .h = oc->height };
+		//wlc_output_set_resolution(output->handle, &new_size, (uint32_t)oc->scale);
 	} else if (oc) {
-		const struct wlc_size *new_size = wlc_output_get_resolution(output->handle);
-		wlc_output_set_resolution(output->handle, new_size, (uint32_t)oc->scale);
+		//const struct wlc_size *new_size = wlc_output_get_resolution(output->handle);
+		//wlc_output_set_resolution(output->handle, new_size, (uint32_t)oc->scale);
 	}
 
+	// TODO WLR: wlr_output_layout
 	// Find position for it
 	if (oc && oc->x != -1 && oc->y != -1) {
 		sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y);
@@ -1170,6 +1174,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 		}
 	}
 
+	/* TODO WLR
 	if (oc && oc->background) {
 		if (output->bg_pid != 0) {
 			terminate_swaybg(output->bg_pid);
@@ -1195,6 +1200,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 			execvp(cmd[0], cmd);
 		}
 	}
+	*/
 }
 
 char *do_var_replacement(char *str) {
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
new file mode 100644
index 00000000..51363e76
--- /dev/null
+++ b/sway/desktop/output.c
@@ -0,0 +1,21 @@
+#include <wayland-server.h>
+#include <wlr/types/wlr_output.h>
+#include "sway/server.h"
+#include "sway/container.h"
+#include "sway/workspace.h"
+#include "log.h"
+
+void output_add_notify(struct wl_listener *listener, void *data) {
+	struct sway_server *server = wl_container_of(listener, server, output_add);
+	struct wlr_output *wlr_output = data;
+	sway_log(L_DEBUG, "New output %p: %s", wlr_output, wlr_output->name);
+	swayc_t *op = new_output(wlr_output);
+	if (!sway_assert(op, "Failed to allocate output")) {
+		return;
+	}
+	// Switch to workspace if we need to
+	if (swayc_active_workspace() == NULL) {
+		swayc_t *ws = op->children->items[0];
+		workspace_switch(ws);
+	}
+}
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index 6ab63c75..064509c9 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -3,6 +3,8 @@
 #include <string.h>
 #include <stdint.h>
 #include <libinput.h>
+#include <wlr/types/wlr_box.h>
+#include <wlr/types/wlr_output.h>
 #include "sway/container.h"
 #include "sway/input.h"
 #include "sway/ipc-json.h"
@@ -14,16 +16,19 @@ static json_object *ipc_json_create_rect(swayc_t *c) {
 	json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x));
 	json_object_object_add(rect, "y", json_object_new_int((int32_t)c->y));
 
-	struct wlc_size size;
+	struct wlr_box box;
 	if (c->type == C_OUTPUT) {
-		size = *wlc_output_get_resolution(c->handle);
+		wlr_output_effective_resolution(c->_handle.output,
+				&box.width, &box.height);
 	} else {
-		size.w = c->width;
-		size.h = c->height;
+		box.width = c->width;
+		box.width = c->height;
 	}
 
-	json_object_object_add(rect, "width", json_object_new_int((int32_t)size.w));
-	json_object_object_add(rect, "height", json_object_new_int((int32_t)size.h));
+	json_object_object_add(rect, "width",
+			json_object_new_int((int32_t)box.width));
+	json_object_object_add(rect, "height",
+			json_object_new_int((int32_t)box.height));
 
 	return rect;
 }
diff --git a/sway/main.c b/sway/main.c
index efca96d5..7d6f2873 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -441,6 +441,7 @@ int main(int argc, char **argv) {
 	if (!server_init(&server)) {
 		return 1;
 	}
+
 	init_layout();
 	ipc_init();
 
@@ -460,7 +461,7 @@ int main(int argc, char **argv) {
 	security_sanity_check();
 
 	if (!terminate_request) {
-		wl_display_run(server.wl_display);
+		server_run(&server);
 	}
 
 	server_fini(&server);
diff --git a/sway/server.c b/sway/server.c
index 4a74cfb5..b7ce4612 100644
--- a/sway/server.c
+++ b/sway/server.c
@@ -27,23 +27,14 @@ bool server_init(struct sway_server *server) {
 	server->data_device_manager =
 		wlr_data_device_manager_create(server->wl_display);
 
-	const char *socket = wl_display_add_socket_auto(server->wl_display);
-	if (!socket) {
-		sway_log_errno(L_ERROR, "Unable to open wayland socket");
-		wlr_backend_destroy(server->backend);
-		return false;
-	}
+	server->output_add.notify = output_add_notify;
+	wl_signal_add(&server->backend->events.output_add, &server->output_add);
 
-	sway_log(L_INFO, "Running compositor on wayland display '%s'", socket);
-	setenv("_WAYLAND_DISPLAY", socket, true);
-
-	if (!wlr_backend_start(server->backend)) {
-		sway_log(L_ERROR, "Failed to start backend");
+	server->socket = wl_display_add_socket_auto(server->wl_display);
+	if (!sway_assert(server->socket,  "Unable to open wayland socket")) {
 		wlr_backend_destroy(server->backend);
 		return false;
 	}
-
-	setenv("WAYLAND_DISPLAY", socket, true);
 	return true;
 }
 
@@ -51,3 +42,16 @@ void server_fini(struct sway_server *server) {
 	// TODO WLR: tear down more stuff
 	wlr_backend_destroy(server->backend);
 }
+
+void server_run(struct sway_server *server) {
+	sway_log(L_INFO, "Running compositor on wayland display '%s'",
+			server->socket);
+	setenv("_WAYLAND_DISPLAY", server->socket, true);
+	if (!sway_assert(wlr_backend_start(server->backend),
+				"Failed to start backend")) {
+		wlr_backend_destroy(server->backend);
+		return;
+	}
+	setenv("WAYLAND_DISPLAY", server->socket, true);
+	wl_display_run(server->wl_display);
+}
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 829fde69..61c9c5e3 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -4,6 +4,8 @@
 #include <stdbool.h>
 #include <strings.h>
 #include <string.h>
+#include <wlr/types/wlr_box.h>
+#include <wlr/types/wlr_output.h>
 #include "sway/config.h"
 #include "sway/container.h"
 #include "sway/workspace.h"
@@ -118,10 +120,10 @@ static void update_root_geometry() {
 
 // New containers
 
-swayc_t *new_output(wlc_handle handle) {
-	struct wlc_size size;
-	output_get_scaled_size(handle, &size);
-	const char *name = wlc_output_get_name(handle);
+swayc_t *new_output(struct wlr_output *wlr_output) {
+	struct wlr_box size;
+	wlr_output_effective_resolution(wlr_output, &size.width, &size.height);
+	const char *name = wlr_output->name;
 	// Find current outputs to see if this already exists
 	{
 		int i, len = root_container.children->length;
@@ -129,14 +131,12 @@ swayc_t *new_output(wlc_handle handle) {
 			swayc_t *op = root_container.children->items[i];
 			const char *op_name = op->name;
 			if (op_name && name && strcmp(op_name, name) == 0) {
-				sway_log(L_DEBUG, "restoring output %" PRIuPTR ":%s", handle, op_name);
+				sway_log(L_DEBUG, "restoring output %p: %s", wlr_output, op_name);
 				return op;
 			}
 		}
 	}
 
-	sway_log(L_DEBUG, "New output %" PRIuPTR ":%s", handle, name);
-
 	struct output_config *oc = NULL, *all = NULL;
 	int i;
 	for (i = 0; i < config->output_configs->length; ++i) {
@@ -164,10 +164,10 @@ swayc_t *new_output(wlc_handle handle) {
 	}
 
 	swayc_t *output = new_swayc(C_OUTPUT);
-	output->handle = handle;
+	output->_handle.output = wlr_output;
 	output->name = name ? strdup(name) : NULL;
-	output->width = size.w;
-	output->height = size.h;
+	output->width = size.width;
+	output->height = size.width;
 	output->unmanaged = create_list();
 	output->bg_pid = 0;
 
-- 
cgit v1.2.3