From aaae59026ff3751190b93277ac6d7566e373c892 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 6 Dec 2017 12:36:06 +0100
Subject: [PATCH 01/15] Add output config

---
 include/sway/config.h    |   4 +
 include/sway/container.h |   1 +
 sway/commands.c          |   1 +
 sway/commands/output.c   | 253 +++++++++++++++++++++++++++++++++++++++
 sway/config/output.c     | 166 +++++++++++++++++++++++++
 sway/main.c              |   4 +
 sway/meson.build         |   2 +
 sway/tree/container.c    |  83 ++++++++++++-
 8 files changed, 512 insertions(+), 2 deletions(-)
 create mode 100644 sway/commands/output.c
 create mode 100644 sway/config/output.c

diff --git a/include/sway/config.h b/include/sway/config.h
index 7de85ab7c..1b49c5c95 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -6,6 +6,7 @@
 #include <libinput.h>
 #include <stdint.h>
 #include <string.h>
+#include <wayland-server.h>
 #include <wlr/types/wlr_box.h>
 #include <xkbcommon/xkbcommon.h>
 #include <time.h>
@@ -80,8 +81,11 @@ struct output_config {
 	char *name;
 	int enabled;
 	int width, height;
+	float refresh_rate;
 	int x, y;
 	int scale;
+	enum wl_output_transform transform;
+
 	char *background;
 	char *background_option;
 };
diff --git a/include/sway/container.h b/include/sway/container.h
index 08a98ed95..e3f84fc64 100644
--- a/include/sway/container.h
+++ b/include/sway/container.h
@@ -132,6 +132,7 @@ swayc_t *new_output(struct sway_output *sway_output);
 swayc_t *new_workspace(swayc_t *output, const char *name);
 swayc_t *new_view(swayc_t *sibling, struct sway_view *sway_view);
 
+swayc_t *destroy_output(swayc_t *output);
 swayc_t *destroy_view(swayc_t *view);
 
 swayc_t *swayc_parent_by_type(swayc_t *container, enum swayc_types type);
diff --git a/sway/commands.c b/sway/commands.c
index 176381299..961cb8676 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -95,6 +95,7 @@ static struct cmd_handler handlers[] = {
 	{ "exec", cmd_exec },
 	{ "exec_always", cmd_exec_always },
 	{ "exit", cmd_exit },
+	{ "output", cmd_output },
 };
 
 static int handler_compare(const void *_a, const void *_b) {
diff --git a/sway/commands/output.c b/sway/commands/output.c
new file mode 100644
index 000000000..c964bef71
--- /dev/null
+++ b/sway/commands/output.c
@@ -0,0 +1,253 @@
+#define _XOPEN_SOURCE 500
+#include <ctype.h>
+#include <libgen.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <unistd.h>
+#include <wordexp.h>
+#include "sway/commands.h"
+#include "sway/config.h"
+#include "list.h"
+#include "log.h"
+#include "stringop.h"
+
+static char *bg_options[] = {
+	"stretch",
+	"center",
+	"fill",
+	"fit",
+	"tile",
+};
+
+struct cmd_results *cmd_output(int argc, char **argv) {
+	struct cmd_results *error = NULL;
+	if ((error = checkarg(argc, "output", EXPECTED_AT_LEAST, 1))) {
+		return error;
+	}
+	const char *name = argv[0];
+
+	struct output_config *output = calloc(1, sizeof(struct output_config));
+	if (!output) {
+		return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config");
+	}
+	output->x = output->y = output->width = output->height = -1;
+	output->name = strdup(name);
+	output->enabled = -1;
+	output->scale = 1;
+
+	// TODO: atoi doesn't handle invalid numbers
+
+	int i;
+	for (i = 1; i < argc; ++i) {
+		const char *command = argv[i];
+
+		if (strcasecmp(command, "disable") == 0) {
+			output->enabled = 0;
+		} else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
+			if (++i >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument.");
+				goto fail;
+			}
+			char *res = argv[i];
+			char *x = strchr(res, 'x');
+			int width = -1, height = -1;
+			if (x != NULL) {
+				// Format is 1234x4321
+				*x = '\0';
+				width = atoi(res);
+				height = atoi(x + 1);
+				*x = 'x';
+			} else {
+				// Format is 1234 4321
+				width = atoi(res);
+				if (++i >= argc) {
+					error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height).");
+					goto fail;
+				}
+				res = argv[i];
+				height = atoi(res);
+			}
+			output->width = width;
+			output->height = height;
+		} else if (strcasecmp(command, "refresh_rate") == 0) {
+			if (++i >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing refresh_rate argument.");
+				goto fail;
+			}
+			output->refresh_rate = atof(argv[i]);
+		} else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
+			if (++i >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
+				goto fail;
+			}
+			char *res = argv[i];
+			char *c = strchr(res, ',');
+			int x = -1, y = -1;
+			if (c != NULL) {
+				// Format is 1234,4321
+				*c = '\0';
+				x = atoi(res);
+				y = atoi(c + 1);
+				*c = ',';
+			} else {
+				// Format is 1234 4321
+				x = atoi(res);
+				if (++i >= argc) {
+					error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
+					goto fail;
+				}
+				res = argv[i];
+				y = atoi(res);
+			}
+			output->x = x;
+			output->y = y;
+		} else if (strcasecmp(command, "scale") == 0) {
+			if (++i >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter.");
+				goto fail;
+			}
+			output->scale = atoi(argv[i]);
+		} else if (strcasecmp(command, "transform") == 0) {
+			if (++i >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter.");
+				goto fail;
+			}
+			char *value = argv[i];
+			if (strcmp(value, "normal") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+			} else if (strcmp(value, "90") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_90;
+			} else if (strcmp(value, "180") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_180;
+			} else if (strcmp(value, "270") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_270;
+			} else if (strcmp(value, "flipped") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED;
+			} else if (strcmp(value, "flipped-90") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_90;
+			} else if (strcmp(value, "flipped-180") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_180;
+			} else if (strcmp(value, "flipped-270") == 0) {
+				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
+			} else {
+				error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform.");
+				goto fail;
+			}
+		} else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
+			wordexp_t p;
+			if (++i >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
+				goto fail;
+			}
+			if (i + 1 >= argc) {
+				error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
+				goto fail;
+			}
+			if (strcasecmp(argv[i + 1], "solid_color") == 0) {
+				output->background = strdup(argv[argc - 2]);
+				output->background_option = strdup("solid_color");
+			} else {
+				// argv[i+j]=bg_option
+				bool valid = false;
+				char *mode;
+				size_t j;
+				for (j = 0; j < (size_t) (argc - i); ++j) {
+					mode = argv[i + j];
+					for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) {
+						if (strcasecmp(mode, bg_options[k]) == 0) {
+							valid = true;
+							break;
+						}
+					}
+					if (valid) {
+						break;
+					}
+				}
+				if (!valid) {
+					error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode.");
+					goto fail;
+				}
+
+				char *src = join_args(argv + i, j);
+				if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
+					error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
+					goto fail;
+				}
+				free(src);
+				src = p.we_wordv[0];
+				if (config->reading && *src != '/') {
+					char *conf = strdup(config->current_config);
+					if (conf) {
+						char *conf_path = dirname(conf);
+						src = malloc(strlen(conf_path) + strlen(src) + 2);
+						if (src) {
+							sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
+						} else {
+							sway_log(L_ERROR, "Unable to allocate background source");
+						}
+						free(conf);
+					} else {
+						sway_log(L_ERROR, "Unable to allocate background source");
+					}
+				}
+				if (!src || access(src, F_OK) == -1) {
+					error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
+					wordfree(&p);
+					goto fail;
+				}
+
+				output->background = strdup(src);
+				output->background_option = strdup(mode);
+				if (src != p.we_wordv[0]) {
+					free(src);
+				}
+				wordfree(&p);
+
+				i += j;
+			}
+		}
+	}
+
+	i = list_seq_find(config->output_configs, output_name_cmp, name);
+	if (i >= 0) {
+		// merge existing config
+		struct output_config *oc = config->output_configs->items[i];
+		merge_output_config(oc, output);
+		free_output_config(output);
+		output = oc;
+	} else {
+		list_add(config->output_configs, output);
+	}
+
+	sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ "
+			"%d, %d scale %d transform %d refresh_rate %f) (bg %s %s)",
+			output->name, output->enabled, output->width,
+			output->height, output->x, output->y, output->scale,
+			output->transform, output->refresh_rate,
+			output->background, output->background_option);
+
+	if (output->name) {
+		// Try to find the output container and apply configuration now. If
+		// this is during startup then there will be no container and config
+		// will be applied during normal "new output" event from wlc.
+		swayc_t *cont = NULL;
+		for (int i = 0; i < root_container.children->length; ++i) {
+			cont = root_container.children->items[i];
+			if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) {
+				apply_output_config(output, cont);
+
+				if (strcmp(output->name, "*") != 0) {
+					// stop looking if the output config isn't applicable to all outputs
+					break;
+				}
+			}
+		}
+	}
+
+	return cmd_results_new(CMD_SUCCESS, NULL, NULL);
+
+fail:
+	free_output_config(output);
+	return error;
+}
diff --git a/sway/config/output.c b/sway/config/output.c
new file mode 100644
index 000000000..02e18e59a
--- /dev/null
+++ b/sway/config/output.c
@@ -0,0 +1,166 @@
+#define _XOPEN_SOURCE 700
+#include <string.h>
+#include <assert.h>
+#include <wlr/types/wlr_output.h>
+#include "sway/config.h"
+#include "sway/output.h"
+#include "log.h"
+
+int output_name_cmp(const void *item, const void *data) {
+	const struct output_config *output = item;
+	const char *name = data;
+
+	return strcmp(output->name, name);
+}
+
+void merge_output_config(struct output_config *dst, struct output_config *src) {
+	if (src->name) {
+		if (dst->name) {
+			free(dst->name);
+		}
+		dst->name = strdup(src->name);
+	}
+	if (src->enabled != -1) {
+		dst->enabled = src->enabled;
+	}
+	if (src->width != -1) {
+		dst->width = src->width;
+	}
+	if (src->height != -1) {
+		dst->height = src->height;
+	}
+	if (src->x != -1) {
+		dst->x = src->x;
+	}
+	if (src->y != -1) {
+		dst->y = src->y;
+	}
+	if (src->scale != -1) {
+		dst->scale = src->scale;
+	}
+	if (src->background) {
+		if (dst->background) {
+			free(dst->background);
+		}
+		dst->background = strdup(src->background);
+	}
+	if (src->background_option) {
+		if (dst->background_option) {
+			free(dst->background_option);
+		}
+		dst->background_option = strdup(src->background_option);
+	}
+}
+
+static void set_mode(struct wlr_output *output, int width, int height,
+		float refresh_rate) {
+	struct wlr_output_mode *mode, *best = NULL;
+	int mhz = (int)(refresh_rate * 1000);
+	wl_list_for_each(mode, &output->modes, link) {
+		if (mode->width == width && mode->height == height) {
+			if (mode->refresh == mhz) {
+				best = mode;
+				break;
+			}
+			best = mode;
+		}
+	}
+	if (!best) {
+		sway_log(L_ERROR, "Configured mode for %s not available", output->name);
+	} else {
+		sway_log(L_DEBUG, "Assigning configured mode to %s", output->name);
+		wlr_output_set_mode(output, best);
+	}
+}
+
+void apply_output_config(struct output_config *oc, swayc_t *output) {
+	assert(output->type == C_OUTPUT);
+
+	if (oc && oc->enabled == 0) {
+		destroy_output(output);
+		return;
+	}
+
+	struct wlr_output *wlr_output = output->sway_output->wlr_output;
+	if (oc && oc->width > 0 && oc->height > 0) {
+		set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
+	}
+	if (oc && oc->scale > 0) {
+		wlr_output->scale = oc->scale;
+	}
+	if (oc && oc->transform != WL_OUTPUT_TRANSFORM_NORMAL) {
+		wlr_output_transform(wlr_output, oc->transform);
+	}
+
+	// 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);
+		output->x = oc->x;
+		output->y = oc->y;
+	} else {
+		int x = 0;
+		for (int i = 0; i < root_container.children->length; ++i) {
+			swayc_t *c = root_container.children->items[i];
+			if (c->type == C_OUTPUT) {
+				if (c->width + c->x > x) {
+					x = c->width + c->x;
+				}
+			}
+		}
+		output->x = x;
+	}
+
+	if (!oc || !oc->background) {
+		// Look for a * config for background
+		int i = list_seq_find(config->output_configs, output_name_cmp, "*");
+		if (i >= 0) {
+			oc = config->output_configs->items[i];
+		} else {
+			oc = NULL;
+		}
+	}
+
+	int output_i;
+	for (output_i = 0; output_i < root_container.children->length; ++output_i) {
+		if (root_container.children->items[output_i] == output) {
+			break;
+		}
+	}
+
+	if (oc && oc->background) {
+		// TODO: swaybg
+		/*if (output->bg_pid != 0) {
+			terminate_swaybg(output->bg_pid);
+		}
+
+		sway_log(L_DEBUG, "Setting background for output %d to %s", output_i, oc->background);
+
+		size_t bufsize = 12;
+		char output_id[bufsize];
+		snprintf(output_id, bufsize, "%d", output_i);
+		output_id[bufsize-1] = 0;
+
+		char *const cmd[] = {
+			"swaybg",
+			output_id,
+			oc->background,
+			oc->background_option,
+			NULL,
+		};
+
+		output->bg_pid = fork();
+		if (output->bg_pid == 0) {
+			execvp(cmd[0], cmd);
+		}*/
+	}
+}
+
+void free_output_config(struct output_config *oc) {
+	if (!oc) {
+		return;
+	}
+	free(oc->name);
+	free(oc->background);
+	free(oc->background_option);
+	free(oc);
+}
diff --git a/sway/main.c b/sway/main.c
index bc843591d..8952f9973 100644
--- a/sway/main.c
+++ b/sway/main.c
@@ -404,6 +404,10 @@ int main(int argc, char **argv) {
 
 	security_sanity_check();
 
+	// TODO: wait for server to be ready
+	// TODO: consume config->cmd_queue
+	config->active = true;
+
 	if (!terminate_request) {
 		server_run(&server);
 	}
diff --git a/sway/meson.build b/sway/meson.build
index 84f481378..5ae7fbb3a 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -5,7 +5,9 @@ sway_sources = files(
 	'commands/exit.c',
 	'commands/exec.c',
 	'commands/exec_always.c',
+	'commands/output.c',
 	'config.c',
+	'config/output.c',
 	'ipc-json.c',
 	'ipc-server.c',
 	'desktop/output.c',
diff --git a/sway/tree/container.c b/sway/tree/container.c
index e205fbcf4..7720718fc 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -2,7 +2,9 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <wlr/types/wlr_output_layout.h>
+#include "sway/config.h"
 #include "sway/container.h"
 #include "sway/layout.h"
 #include "sway/output.h"
@@ -23,6 +25,30 @@ void swayc_descendants_of_type(swayc_t *root, enum swayc_types type,
 	}
 }
 
+static void update_root_geometry() {
+	int width = 0;
+	int height = 0;
+	swayc_t *child;
+	int child_width;
+	int child_height;
+
+	for (int i = 0; i < root_container.children->length; ++i) {
+		child = root_container.children->items[i];
+		child_width = child->width + child->x;
+		child_height = child->height + child->y;
+		if (child_width > width) {
+			width = child_width;
+		}
+
+		if (child_height > height) {
+			height = child_height;
+		}
+	}
+
+	root_container.width = width;
+	root_container.height = height;
+}
+
 static swayc_t *new_swayc(enum swayc_types type) {
 	// next id starts at 1 because 0 is assigned to root_container in layout.c
 	static size_t next_id = 1;
@@ -44,10 +70,33 @@ static swayc_t *new_swayc(enum swayc_types type) {
 
 swayc_t *new_output(struct sway_output *sway_output) {
 	struct wlr_box size;
-	wlr_output_effective_resolution(
-			sway_output->wlr_output, &size.width, &size.height);
+	wlr_output_effective_resolution(sway_output->wlr_output, &size.width,
+		&size.height);
 	const char *name = sway_output->wlr_output->name;
 
+	struct output_config *oc = NULL, *all = NULL;
+	for (int i = 0; i < config->output_configs->length; ++i) {
+		struct output_config *cur = config->output_configs->items[i];
+		if (strcasecmp(name, cur->name) == 0) {
+			sway_log(L_DEBUG, "Matched output config for %s", name);
+			oc = cur;
+		}
+		if (strcasecmp("*", cur->name) == 0) {
+			sway_log(L_DEBUG, "Matched wildcard output config for %s", name);
+			all = cur;
+		}
+
+		if (oc && all) {
+			break;
+		}
+	}
+	if (!oc) {
+		oc = all;
+	}
+	if (oc && !oc->enabled) {
+		return NULL;
+	}
+
 	swayc_t *output = new_swayc(C_OUTPUT);
 	output->sway_output = sway_output;
 	output->name = name ? strdup(name) : NULL;
@@ -58,6 +107,8 @@ swayc_t *new_output(struct sway_output *sway_output) {
 	wlr_output_layout_add_auto(root_container.output_layout,
 		sway_output->wlr_output);
 
+	apply_output_config(oc, output);
+
 	add_child(&root_container, output);
 
 	// Create workspace
@@ -139,6 +190,34 @@ static void free_swayc(swayc_t *cont) {
 	free(cont);
 }
 
+swayc_t *destroy_output(swayc_t *output) {
+	if (!sway_assert(output, "null output passed to destroy_output")) {
+		return NULL;
+	}
+	if (output->children->length > 0) {
+		// TODO save workspaces when there are no outputs.
+		// TODO also check if there will ever be no outputs except for exiting
+		// program
+		if (root_container.children->length > 1) {
+			int p = root_container.children->items[0] == output;
+			// Move workspace from this output to another output
+			while (output->children->length) {
+				swayc_t *child = output->children->items[0];
+				remove_child(child);
+				add_child(root_container.children->items[p], child);
+			}
+			sort_workspaces(root_container.children->items[p]);
+			// TODO WLR: is this needed anymore?
+			//update_visibility(root_container.children->items[p]);
+			arrange_windows(root_container.children->items[p], -1, -1);
+		}
+	}
+	sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name);
+	free_swayc(output);
+	update_root_geometry();
+	return &root_container;
+}
+
 swayc_t *destroy_view(swayc_t *view) {
 	if (!sway_assert(view, "null view passed to destroy_view")) {
 		return NULL;

From 68ae989ceef0a144988c0a55b13aaacf514b957d Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 6 Dec 2017 12:57:13 +0100
Subject: [PATCH 02/15] Init, merge output config params, use wlr_output_layout

---
 include/sway/config.h  |  3 ++-
 sway/commands/output.c |  4 +---
 sway/config/output.c   | 45 ++++++++++++++++++++++++++++--------------
 sway/tree/container.c  |  6 ------
 4 files changed, 33 insertions(+), 25 deletions(-)

diff --git a/include/sway/config.h b/include/sway/config.h
index 1b49c5c95..231356f25 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -84,7 +84,7 @@ struct output_config {
 	float refresh_rate;
 	int x, y;
 	int scale;
-	enum wl_output_transform transform;
+	int32_t transform;
 
 	char *background;
 	char *background_option;
@@ -364,6 +364,7 @@ void apply_input_config(struct input_config *ic, struct libinput_device *dev);
 void free_input_config(struct input_config *ic);
 
 int output_name_cmp(const void *item, const void *data);
+void output_config_defaults(struct output_config *oc);
 void merge_output_config(struct output_config *dst, struct output_config *src);
 /** Sets up a WLC output handle based on a given output_config.
  */
diff --git a/sway/commands/output.c b/sway/commands/output.c
index c964bef71..bbf8efc34 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -31,10 +31,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 	if (!output) {
 		return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config");
 	}
-	output->x = output->y = output->width = output->height = -1;
+	output_config_defaults(output);
 	output->name = strdup(name);
-	output->enabled = -1;
-	output->scale = 1;
 
 	// TODO: atoi doesn't handle invalid numbers
 
diff --git a/sway/config/output.c b/sway/config/output.c
index 02e18e59a..4ed2c5311 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -2,6 +2,7 @@
 #include <string.h>
 #include <assert.h>
 #include <wlr/types/wlr_output.h>
+#include <wlr/types/wlr_output_layout.h>
 #include "sway/config.h"
 #include "sway/output.h"
 #include "log.h"
@@ -13,6 +14,15 @@ int output_name_cmp(const void *item, const void *data) {
 	return strcmp(output->name, name);
 }
 
+void output_config_defaults(struct output_config *oc) {
+	oc->enabled = -1;
+	oc->width = oc->height -1;
+	oc->refresh_rate = -1;
+	oc->x = oc->y = -1;
+	oc->scale = -1;
+	oc->transform = -1;
+}
+
 void merge_output_config(struct output_config *dst, struct output_config *src) {
 	if (src->name) {
 		if (dst->name) {
@@ -38,6 +48,12 @@ void merge_output_config(struct output_config *dst, struct output_config *src) {
 	if (src->scale != -1) {
 		dst->scale = src->scale;
 	}
+	if (src->refresh_rate != -1) {
+		dst->refresh_rate = src->refresh_rate;
+	}
+	if (src->transform != -1) {
+		dst->transform = src->transform;
+	}
 	if (src->background) {
 		if (dst->background) {
 			free(dst->background);
@@ -86,29 +102,28 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 		set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
 	}
 	if (oc && oc->scale > 0) {
+		sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale);
 		wlr_output->scale = oc->scale;
 	}
-	if (oc && oc->transform != WL_OUTPUT_TRANSFORM_NORMAL) {
+	if (oc && oc->transform >= 0) {
+		sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
 		wlr_output_transform(wlr_output, oc->transform);
 	}
 
 	// Find position for it
-	if (oc && oc->x != -1 && oc->y != -1) {
+	if (oc && (oc->x != -1 || oc->y != -1)) {
 		sway_log(L_DEBUG, "Set %s position to %d, %d", oc->name, oc->x, oc->y);
-		output->x = oc->x;
-		output->y = oc->y;
+		wlr_output_layout_add(root_container.output_layout, wlr_output, oc->x,
+			oc->y);
 	} else {
-		int x = 0;
-		for (int i = 0; i < root_container.children->length; ++i) {
-			swayc_t *c = root_container.children->items[i];
-			if (c->type == C_OUTPUT) {
-				if (c->width + c->x > x) {
-					x = c->width + c->x;
-				}
-			}
-		}
-		output->x = x;
+		wlr_output_layout_add_auto(root_container.output_layout, wlr_output);
 	}
+	struct wlr_box *output_layout_box =
+		wlr_output_layout_get_box(root_container.output_layout, wlr_output);
+	output->x = output_layout_box->x;
+	output->y = output_layout_box->y;
+	output->width = output_layout_box->width;
+	output->height = output_layout_box->height;
 
 	if (!oc || !oc->background) {
 		// Look for a * config for background
@@ -128,7 +143,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 	}
 
 	if (oc && oc->background) {
-		// TODO: swaybg
+		// TODO swaybg
 		/*if (output->bg_pid != 0) {
 			terminate_swaybg(output->bg_pid);
 		}
diff --git a/sway/tree/container.c b/sway/tree/container.c
index 7720718fc..ba305efa2 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -100,12 +100,6 @@ swayc_t *new_output(struct sway_output *sway_output) {
 	swayc_t *output = new_swayc(C_OUTPUT);
 	output->sway_output = sway_output;
 	output->name = name ? strdup(name) : NULL;
-	output->width = size.width;
-	output->height = size.width;
-
-	// TODO configure output layout position
-	wlr_output_layout_add_auto(root_container.output_layout,
-		sway_output->wlr_output);
 
 	apply_output_config(oc, output);
 

From 7c5d8c553cbf5e93538346c904f2c2e42af66c7c Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 6 Dec 2017 19:16:12 +0100
Subject: [PATCH 03/15] Simplify free calls, use wlr_output_set_scale

---
 sway/config/output.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)

diff --git a/sway/config/output.c b/sway/config/output.c
index 4ed2c5311..4a0a5cc9a 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -25,9 +25,7 @@ void output_config_defaults(struct output_config *oc) {
 
 void merge_output_config(struct output_config *dst, struct output_config *src) {
 	if (src->name) {
-		if (dst->name) {
-			free(dst->name);
-		}
+		free(dst->name);
 		dst->name = strdup(src->name);
 	}
 	if (src->enabled != -1) {
@@ -55,15 +53,11 @@ void merge_output_config(struct output_config *dst, struct output_config *src) {
 		dst->transform = src->transform;
 	}
 	if (src->background) {
-		if (dst->background) {
-			free(dst->background);
-		}
+		free(dst->background);
 		dst->background = strdup(src->background);
 	}
 	if (src->background_option) {
-		if (dst->background_option) {
-			free(dst->background_option);
-		}
+		free(dst->background_option);
 		dst->background_option = strdup(src->background_option);
 	}
 }
@@ -103,7 +97,7 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 	}
 	if (oc && oc->scale > 0) {
 		sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale);
-		wlr_output->scale = oc->scale;
+		wlr_output_set_scale(wlr_output, oc->scale);
 	}
 	if (oc && oc->transform >= 0) {
 		sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);

From 41dd29161249dd6b6c3288a2d4cf63763f40ce14 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 6 Dec 2017 19:23:43 +0100
Subject: [PATCH 04/15] Use wlr_output_layout_get_box

---
 sway/tree/container.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)

diff --git a/sway/tree/container.c b/sway/tree/container.c
index ba305efa2..ec3311a0b 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -26,27 +26,10 @@ void swayc_descendants_of_type(swayc_t *root, enum swayc_types type,
 }
 
 static void update_root_geometry() {
-	int width = 0;
-	int height = 0;
-	swayc_t *child;
-	int child_width;
-	int child_height;
-
-	for (int i = 0; i < root_container.children->length; ++i) {
-		child = root_container.children->items[i];
-		child_width = child->width + child->x;
-		child_height = child->height + child->y;
-		if (child_width > width) {
-			width = child_width;
-		}
-
-		if (child_height > height) {
-			height = child_height;
-		}
-	}
-
-	root_container.width = width;
-	root_container.height = height;
+	struct wlr_box *box =
+		wlr_output_layout_get_box(root_container.output_layout, NULL);
+	root_container.width = box->width;
+	root_container.height = box->height;
 }
 
 static swayc_t *new_swayc(enum swayc_types type) {

From 8764dc26c634379ca5b5c2c4fc26cf8be9adf027 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 6 Dec 2017 19:45:43 +0100
Subject: [PATCH 05/15] Add new_output_config, update root container size on
 output hotplug

---
 include/sway/config.h  | 4 +---
 sway/commands/output.c | 3 +--
 sway/config/output.c   | 7 ++++++-
 sway/tree/container.c  | 1 +
 4 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/include/sway/config.h b/include/sway/config.h
index 231356f25..4dd8e94c5 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -364,10 +364,8 @@ void apply_input_config(struct input_config *ic, struct libinput_device *dev);
 void free_input_config(struct input_config *ic);
 
 int output_name_cmp(const void *item, const void *data);
-void output_config_defaults(struct output_config *oc);
+struct output_config *new_output_config();
 void merge_output_config(struct output_config *dst, struct output_config *src);
-/** Sets up a WLC output handle based on a given output_config.
- */
 void apply_output_config(struct output_config *oc, swayc_t *output);
 void free_output_config(struct output_config *oc);
 
diff --git a/sway/commands/output.c b/sway/commands/output.c
index bbf8efc34..11da0ff67 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -27,11 +27,10 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 	}
 	const char *name = argv[0];
 
-	struct output_config *output = calloc(1, sizeof(struct output_config));
+	struct output_config *output = new_output_config();
 	if (!output) {
 		return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config");
 	}
-	output_config_defaults(output);
 	output->name = strdup(name);
 
 	// TODO: atoi doesn't handle invalid numbers
diff --git a/sway/config/output.c b/sway/config/output.c
index 4a0a5cc9a..26798503a 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -14,13 +14,18 @@ int output_name_cmp(const void *item, const void *data) {
 	return strcmp(output->name, name);
 }
 
-void output_config_defaults(struct output_config *oc) {
+struct output_config *new_output_config() {
+	struct output_config *oc = calloc(1, sizeof(struct output_config));
+	if (oc == NULL) {
+		return NULL;
+	}
 	oc->enabled = -1;
 	oc->width = oc->height -1;
 	oc->refresh_rate = -1;
 	oc->x = oc->y = -1;
 	oc->scale = -1;
 	oc->transform = -1;
+	return oc;
 }
 
 void merge_output_config(struct output_config *dst, struct output_config *src) {
diff --git a/sway/tree/container.c b/sway/tree/container.c
index ec3311a0b..d9bed7d87 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -93,6 +93,7 @@ swayc_t *new_output(struct sway_output *sway_output) {
 	sway_log(L_DEBUG, "Creating default workspace %s", ws_name);
 	new_workspace(output, ws_name);
 	free(ws_name);
+	update_root_geometry();
 	return output;
 }
 

From 4a14aa9ad99a6f316024e110332a0b482e231543 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Sat, 9 Dec 2017 15:48:52 +0100
Subject: [PATCH 06/15] Remove output from layout

---
 sway/commands/output.c | 2 +-
 sway/config/output.c   | 3 ++-
 sway/tree/container.c  | 4 ++++
 3 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/sway/commands/output.c b/sway/commands/output.c
index 11da0ff67..be78358a6 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -227,7 +227,7 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 	if (output->name) {
 		// Try to find the output container and apply configuration now. If
 		// this is during startup then there will be no container and config
-		// will be applied during normal "new output" event from wlc.
+		// will be applied during normal "new output" event from wlroots.
 		swayc_t *cont = NULL;
 		for (int i = 0; i < root_container.children->length; ++i) {
 			cont = root_container.children->items[i];
diff --git a/sway/config/output.c b/sway/config/output.c
index 26798503a..027a79cef 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -91,12 +91,13 @@ static void set_mode(struct wlr_output *output, int width, int height,
 void apply_output_config(struct output_config *oc, swayc_t *output) {
 	assert(output->type == C_OUTPUT);
 
+	struct wlr_output *wlr_output = output->sway_output->wlr_output;
 	if (oc && oc->enabled == 0) {
+		wlr_output_layout_remove(root_container.output_layout, wlr_output);
 		destroy_output(output);
 		return;
 	}
 
-	struct wlr_output *wlr_output = output->sway_output->wlr_output;
 	if (oc && oc->width > 0 && oc->height > 0) {
 		set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
 	}
diff --git a/sway/tree/container.c b/sway/tree/container.c
index d9bed7d87..e4c27d614 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -8,6 +8,7 @@
 #include "sway/container.h"
 #include "sway/layout.h"
 #include "sway/output.h"
+#include "sway/server.h"
 #include "sway/view.h"
 #include "sway/workspace.h"
 #include "log.h"
@@ -172,6 +173,7 @@ swayc_t *destroy_output(swayc_t *output) {
 	if (!sway_assert(output, "null output passed to destroy_output")) {
 		return NULL;
 	}
+
 	if (output->children->length > 0) {
 		// TODO save workspaces when there are no outputs.
 		// TODO also check if there will ever be no outputs except for exiting
@@ -190,9 +192,11 @@ swayc_t *destroy_output(swayc_t *output) {
 			arrange_windows(root_container.children->items[p], -1, -1);
 		}
 	}
+
 	sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name);
 	free_swayc(output);
 	update_root_geometry();
+
 	return &root_container;
 }
 

From 475a0132a99aa7c576a399fdee920aa3ecadeff9 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Mon, 11 Dec 2017 21:47:40 +0100
Subject: [PATCH 07/15] Use custom modes when output has no mode

---
 sway/config/output.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/sway/config/output.c b/sway/config/output.c
index 027a79cef..e6e680d3d 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -69,8 +69,14 @@ void merge_output_config(struct output_config *dst, struct output_config *src) {
 
 static void set_mode(struct wlr_output *output, int width, int height,
 		float refresh_rate) {
-	struct wlr_output_mode *mode, *best = NULL;
 	int mhz = (int)(refresh_rate * 1000);
+	if (wl_list_empty(&output->modes)) {
+		sway_log(L_DEBUG, "Assigning custom mode to %s", output->name);
+		wlr_output_set_custom_mode(output, width, height, mhz);
+		return;
+	}
+
+	struct wlr_output_mode *mode, *best = NULL;
 	wl_list_for_each(mode, &output->modes, link) {
 		if (mode->width == width && mode->height == height) {
 			if (mode->refresh == mhz) {
@@ -99,6 +105,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 	}
 
 	if (oc && oc->width > 0 && oc->height > 0) {
+		sway_log(L_DEBUG, "Set %s mode to %dx%d (%f GHz)", oc->name, oc->width,
+			oc->height, oc->refresh_rate);
 		set_mode(wlr_output, oc->width, oc->height, oc->refresh_rate);
 	}
 	if (oc && oc->scale > 0) {

From f3d880b0ec9eae246ef0d70dd67bed6d7488ab33 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Tue, 12 Dec 2017 19:40:17 +0100
Subject: [PATCH 08/15] Add scale and transform events to sway_output

---
 include/sway/output.h |  8 ++++++++
 sway/config/output.c  |  2 ++
 sway/desktop/output.c | 32 +++++++++++++++++++++++++++++---
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/include/sway/output.h b/include/sway/output.h
index 895cb07d4..118693986 100644
--- a/include/sway/output.h
+++ b/include/sway/output.h
@@ -12,8 +12,16 @@ struct sway_output {
 	struct sway_container *swayc;
 	struct sway_server *server;
 	struct timespec last_frame;
+
+	struct {
+		struct wl_signal scale;
+		struct wl_signal transform;
+	} events;
+
 	struct wl_listener frame;
 	struct wl_listener resolution;
+	struct wl_listener scale;
+	struct wl_listener transform;
 };
 
 #endif
diff --git a/sway/config/output.c b/sway/config/output.c
index e6e680d3d..b06c7c0e9 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -112,10 +112,12 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 	if (oc && oc->scale > 0) {
 		sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale);
 		wlr_output_set_scale(wlr_output, oc->scale);
+		wl_signal_emit(&output->sway_output->events.scale, output->sway_output);
 	}
 	if (oc && oc->transform >= 0) {
 		sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
 		wlr_output_transform(wlr_output, oc->transform);
+		wl_signal_emit(&output->sway_output->events.transform, output->sway_output);
 	}
 
 	// Find position for it
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index 7eb48bdf0..f44cda1a0 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -98,17 +98,40 @@ static void output_resolution_notify(struct wl_listener *listener, void *data) {
 	arrange_windows(soutput->swayc, -1, -1);
 }
 
+static void output_scale_notify(struct wl_listener *listener, void *data) {
+	struct sway_output *soutput = wl_container_of(
+			listener, soutput, scale);
+	arrange_windows(soutput->swayc, -1, -1);
+}
+
+static void output_transform_notify(struct wl_listener *listener, void *data) {
+	struct sway_output *soutput = wl_container_of(
+			listener, soutput, transform);
+	arrange_windows(soutput->swayc, -1, -1);
+}
+
 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);
 
 	struct sway_output *output = calloc(1, sizeof(struct sway_output));
+	if (!output) {
+		return;
+	}
 	output->wlr_output = wlr_output;
 	output->server = server;
-	output->swayc = new_output(output);
 
-	if (wl_list_length(&wlr_output->modes) > 0) {
+	wl_signal_init(&output->events.scale);
+	wl_signal_init(&output->events.transform);
+
+	output->swayc = new_output(output);
+	if (!output->swayc) {
+		free(output);
+		return;
+	}
+
+	if (!wl_list_empty(&wlr_output->modes)) {
 		struct wlr_output_mode *mode = NULL;
 		mode = wl_container_of((&wlr_output->modes)->prev, mode, link);
 		wlr_output_set_mode(wlr_output, mode);
@@ -116,9 +139,12 @@ void output_add_notify(struct wl_listener *listener, void *data) {
 
 	output->frame.notify = output_frame_notify;
 	wl_signal_add(&wlr_output->events.frame, &output->frame);
-
 	output->resolution.notify = output_resolution_notify;
 	wl_signal_add(&wlr_output->events.resolution, &output->resolution);
+	output->scale.notify = output_scale_notify;
+	wl_signal_add(&output->events.scale, &output->scale);
+	output->transform.notify = output_transform_notify;
+	wl_signal_add(&output->events.transform, &output->transform);
 
 	arrange_windows(output->swayc, -1, -1);
 }

From c7abb77f2217cc4d5642ef1650f7fc75e1c1a9a4 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Tue, 12 Dec 2017 20:02:01 +0100
Subject: [PATCH 09/15] Listen to output layout change

---
 include/sway/container.h |  4 ++--
 include/sway/layout.h    |  8 ++++++++
 sway/config/output.c     | 17 ++++++++++-------
 sway/desktop/output.c    | 12 ++++--------
 sway/desktop/xwayland.c  |  6 +++---
 sway/tree/container.c    |  9 ---------
 sway/tree/layout.c       | 17 ++++++++++++++++-
 7 files changed, 43 insertions(+), 30 deletions(-)

diff --git a/include/sway/container.h b/include/sway/container.h
index e3f84fc64..b15e04285 100644
--- a/include/sway/container.h
+++ b/include/sway/container.h
@@ -57,9 +57,9 @@ enum swayc_border_types {
 	B_NORMAL, /**< Normal border with title bar */
 };
 
+struct sway_root;
 struct sway_output;
 struct sway_view;
-struct wlr_output_layout;
 
 /**
  * Stores information about a container.
@@ -69,7 +69,7 @@ struct wlr_output_layout;
 struct sway_container {
 	union {
 		// TODO: Encapsulate state for other node types as well like C_CONTAINER
-		struct wlr_output_layout *output_layout; // C_ROOT
+		struct sway_root *sway_root;             // C_ROOT
 		struct sway_output *sway_output;         // C_OUTPUT
 		struct sway_view *sway_view;             // C_VIEW
 	};
diff --git a/include/sway/layout.h b/include/sway/layout.h
index f3b62b05e..bfd96a026 100644
--- a/include/sway/layout.h
+++ b/include/sway/layout.h
@@ -1,8 +1,16 @@
 #ifndef _SWAY_LAYOUT_H
 #define _SWAY_LAYOUT_H
 
+#include <wlr/types/wlr_output_layout.h>
+
 struct sway_container;
 
+struct sway_root {
+	struct wlr_output_layout *output_layout;
+
+	struct wl_listener output_layout_change;
+};
+
 void init_layout(void);
 void add_child(struct sway_container *parent, struct sway_container *child);
 struct sway_container *remove_child(struct sway_container *child);
diff --git a/sway/config/output.c b/sway/config/output.c
index b06c7c0e9..ed47a617c 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -99,7 +99,8 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 
 	struct wlr_output *wlr_output = output->sway_output->wlr_output;
 	if (oc && oc->enabled == 0) {
-		wlr_output_layout_remove(root_container.output_layout, wlr_output);
+		wlr_output_layout_remove(root_container.sway_root->output_layout,
+			wlr_output);
 		destroy_output(output);
 		return;
 	}
@@ -117,19 +118,21 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 	if (oc && oc->transform >= 0) {
 		sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
 		wlr_output_transform(wlr_output, oc->transform);
-		wl_signal_emit(&output->sway_output->events.transform, output->sway_output);
+		wl_signal_emit(&output->sway_output->events.transform,
+			output->sway_output);
 	}
 
 	// 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);
-		wlr_output_layout_add(root_container.output_layout, wlr_output, oc->x,
-			oc->y);
+		wlr_output_layout_add(root_container.sway_root->output_layout,
+			wlr_output, oc->x, oc->y);
 	} else {
-		wlr_output_layout_add_auto(root_container.output_layout, wlr_output);
+		wlr_output_layout_add_auto(root_container.sway_root->output_layout,
+			wlr_output);
 	}
-	struct wlr_box *output_layout_box =
-		wlr_output_layout_get_box(root_container.output_layout, wlr_output);
+	struct wlr_box *output_layout_box = wlr_output_layout_get_box(
+		root_container.sway_root->output_layout, wlr_output);
 	output->x = output_layout_box->x;
 	output->y = output_layout_box->y;
 	output->width = output_layout_box->width;
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index f44cda1a0..bcdaa7d2f 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -72,8 +72,7 @@ static void output_frame_view(swayc_t *view, void *data) {
 }
 
 static void output_frame_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(
-			listener, soutput, frame);
+	struct sway_output *soutput = wl_container_of(listener, soutput, frame);
 	struct wlr_output *wlr_output = data;
 	struct sway_server *server = soutput->server;
 
@@ -93,20 +92,17 @@ static void output_frame_notify(struct wl_listener *listener, void *data) {
 }
 
 static void output_resolution_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(
-			listener, soutput, resolution);
+	struct sway_output *soutput = wl_container_of(listener, soutput, resolution);
 	arrange_windows(soutput->swayc, -1, -1);
 }
 
 static void output_scale_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(
-			listener, soutput, scale);
+	struct sway_output *soutput = wl_container_of(listener, soutput, scale);
 	arrange_windows(soutput->swayc, -1, -1);
 }
 
 static void output_transform_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(
-			listener, soutput, transform);
+	struct sway_output *soutput = wl_container_of(listener, soutput, transform);
 	arrange_windows(soutput->swayc, -1, -1);
 }
 
diff --git a/sway/desktop/xwayland.c b/sway/desktop/xwayland.c
index 65c7e1ec3..e3799d2d1 100644
--- a/sway/desktop/xwayland.c
+++ b/sway/desktop/xwayland.c
@@ -55,7 +55,7 @@ static void set_position(struct sway_view *view, double ox, double oy) {
 	if (!sway_assert(root, "output must be within tree to set position")) {
 		return;
 	}
-	struct wlr_output_layout *layout = root->output_layout;
+	struct wlr_output_layout *layout = root->sway_root->output_layout;
 	struct wlr_output_layout_output *loutput =
 		wlr_output_layout_get(layout, output->sway_output->wlr_output);
 	if (!sway_assert(loutput, "output must be within layout to set position")) {
@@ -147,14 +147,14 @@ void handle_xwayland_surface(struct wl_listener *listener, void *data) {
 	// TODO remove from the tree when the surface goes away (unmapped)
 	sway_view->surface = xsurface->surface;
 	sway_surface->view = sway_view;
-	
+
 	// TODO:
 	// - Wire up listeners
 	// - Handle popups
 	// - Look up pid and open on appropriate workspace
 	// - Set new view to maximized so it behaves nicely
 	// - Criteria
-	
+
 	sway_surface->commit.notify = handle_commit;
 	wl_signal_add(&xsurface->surface->events.commit, &sway_surface->commit);
 	sway_surface->destroy.notify = handle_destroy;
diff --git a/sway/tree/container.c b/sway/tree/container.c
index e4c27d614..f70bccdc6 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -26,13 +26,6 @@ void swayc_descendants_of_type(swayc_t *root, enum swayc_types type,
 	}
 }
 
-static void update_root_geometry() {
-	struct wlr_box *box =
-		wlr_output_layout_get_box(root_container.output_layout, NULL);
-	root_container.width = box->width;
-	root_container.height = box->height;
-}
-
 static swayc_t *new_swayc(enum swayc_types type) {
 	// next id starts at 1 because 0 is assigned to root_container in layout.c
 	static size_t next_id = 1;
@@ -94,7 +87,6 @@ swayc_t *new_output(struct sway_output *sway_output) {
 	sway_log(L_DEBUG, "Creating default workspace %s", ws_name);
 	new_workspace(output, ws_name);
 	free(ws_name);
-	update_root_geometry();
 	return output;
 }
 
@@ -195,7 +187,6 @@ swayc_t *destroy_output(swayc_t *output) {
 
 	sway_log(L_DEBUG, "OUTPUT: Destroying output '%s'", output->name);
 	free_swayc(output);
-	update_root_geometry();
 
 	return &root_container;
 }
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index cb39a361a..fd17f8a5f 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -7,6 +7,7 @@
 #include <wlr/types/wlr_output.h>
 #include <wlr/types/wlr_output_layout.h>
 #include "sway/container.h"
+#include "sway/layout.h"
 #include "sway/output.h"
 #include "sway/view.h"
 #include "list.h"
@@ -14,13 +15,27 @@
 
 swayc_t root_container;
 
+static void output_layout_change_notify(struct wl_listener *listener, void *data) {
+	struct wlr_box *box = wlr_output_layout_get_box(
+		root_container.sway_root->output_layout, NULL);
+	root_container.width = box->width;
+	root_container.height = box->height;
+}
+
 void init_layout(void) {
 	root_container.id = 0; // normally assigned in new_swayc()
 	root_container.type = C_ROOT;
 	root_container.layout = L_NONE;
 	root_container.name = strdup("root");
 	root_container.children = create_list();
-	root_container.output_layout = wlr_output_layout_create();
+
+	root_container.sway_root = calloc(1, sizeof(*root_container.sway_root));
+	root_container.sway_root->output_layout = wlr_output_layout_create();
+
+	root_container.sway_root->output_layout_change.notify =
+		output_layout_change_notify;
+	wl_signal_add(&root_container.sway_root->output_layout->events.change,
+		&root_container.sway_root->output_layout_change);
 }
 
 void add_child(swayc_t *parent, swayc_t *child) {

From d293c429424a9f96c3fc8143af457645326e7a0e Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Tue, 12 Dec 2017 21:09:51 +0100
Subject: [PATCH 10/15] Update output container box in event handler

---
 include/sway/config.h  |  1 -
 sway/commands/output.c |  3 ++-
 sway/config/output.c   |  6 ------
 sway/desktop/output.c  | 23 +++++++++++++++++------
 4 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/include/sway/config.h b/include/sway/config.h
index 4dd8e94c5..139d7800e 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -6,7 +6,6 @@
 #include <libinput.h>
 #include <stdint.h>
 #include <string.h>
-#include <wayland-server.h>
 #include <wlr/types/wlr_box.h>
 #include <xkbcommon/xkbcommon.h>
 #include <time.h>
diff --git a/sway/commands/output.c b/sway/commands/output.c
index be78358a6..23792855c 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -29,7 +29,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 
 	struct output_config *output = new_output_config();
 	if (!output) {
-		return cmd_results_new(CMD_FAILURE, "output", "Unable to allocate output config");
+		sway_log(L_ERROR, "Failed to allocate output config");
+		return NULL;
 	}
 	output->name = strdup(name);
 
diff --git a/sway/config/output.c b/sway/config/output.c
index ed47a617c..6371763f8 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -131,12 +131,6 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 		wlr_output_layout_add_auto(root_container.sway_root->output_layout,
 			wlr_output);
 	}
-	struct wlr_box *output_layout_box = wlr_output_layout_get_box(
-		root_container.sway_root->output_layout, wlr_output);
-	output->x = output_layout_box->x;
-	output->y = output_layout_box->y;
-	output->width = output_layout_box->width;
-	output->height = output_layout_box->height;
 
 	if (!oc || !oc->background) {
 		// Look for a * config for background
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index bcdaa7d2f..3fd49846b 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -91,19 +91,30 @@ static void output_frame_notify(struct wl_listener *listener, void *data) {
 	soutput->last_frame = now;
 }
 
+static void output_update_size(struct sway_output *output) {
+	struct wlr_box *output_layout_box = wlr_output_layout_get_box(
+		root_container.sway_root->output_layout, output->wlr_output);
+	output->swayc->x = output_layout_box->x;
+	output->swayc->y = output_layout_box->y;
+	output->swayc->width = output_layout_box->width;
+	output->swayc->height = output_layout_box->height;
+
+	arrange_windows(output->swayc, -1, -1);
+}
+
 static void output_resolution_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(listener, soutput, resolution);
-	arrange_windows(soutput->swayc, -1, -1);
+	struct sway_output *output = wl_container_of(listener, output, resolution);
+	output_update_size(output);
 }
 
 static void output_scale_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(listener, soutput, scale);
-	arrange_windows(soutput->swayc, -1, -1);
+	struct sway_output *output = wl_container_of(listener, output, scale);
+	output_update_size(output);
 }
 
 static void output_transform_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *soutput = wl_container_of(listener, soutput, transform);
-	arrange_windows(soutput->swayc, -1, -1);
+	struct sway_output *output = wl_container_of(listener, output, transform);
+	output_update_size(output);
 }
 
 void output_add_notify(struct wl_listener *listener, void *data) {

From a4619e98c462690f14baf5c0c72c25553e3c6d51 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 13 Dec 2017 15:52:18 +0100
Subject: [PATCH 11/15] Update output containers on output layout change

---
 include/sway/output.h |  8 --------
 sway/config/output.c  |  5 +----
 sway/desktop/output.c | 47 +++++--------------------------------------
 sway/tree/layout.c    | 20 +++++++++++++++---
 4 files changed, 23 insertions(+), 57 deletions(-)

diff --git a/include/sway/output.h b/include/sway/output.h
index 118693986..7ca02d7b5 100644
--- a/include/sway/output.h
+++ b/include/sway/output.h
@@ -13,15 +13,7 @@ struct sway_output {
 	struct sway_server *server;
 	struct timespec last_frame;
 
-	struct {
-		struct wl_signal scale;
-		struct wl_signal transform;
-	} events;
-
 	struct wl_listener frame;
-	struct wl_listener resolution;
-	struct wl_listener scale;
-	struct wl_listener transform;
 };
 
 #endif
diff --git a/sway/config/output.c b/sway/config/output.c
index 6371763f8..8d946386f 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -113,13 +113,10 @@ void apply_output_config(struct output_config *oc, swayc_t *output) {
 	if (oc && oc->scale > 0) {
 		sway_log(L_DEBUG, "Set %s scale to %d", oc->name, oc->scale);
 		wlr_output_set_scale(wlr_output, oc->scale);
-		wl_signal_emit(&output->sway_output->events.scale, output->sway_output);
 	}
 	if (oc && oc->transform >= 0) {
 		sway_log(L_DEBUG, "Set %s transform to %d", oc->name, oc->transform);
-		wlr_output_transform(wlr_output, oc->transform);
-		wl_signal_emit(&output->sway_output->events.transform,
-			output->sway_output);
+		wlr_output_set_transform(wlr_output, oc->transform);
 	}
 
 	// Find position for it
diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index 3fd49846b..2177ad743 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -91,32 +91,6 @@ static void output_frame_notify(struct wl_listener *listener, void *data) {
 	soutput->last_frame = now;
 }
 
-static void output_update_size(struct sway_output *output) {
-	struct wlr_box *output_layout_box = wlr_output_layout_get_box(
-		root_container.sway_root->output_layout, output->wlr_output);
-	output->swayc->x = output_layout_box->x;
-	output->swayc->y = output_layout_box->y;
-	output->swayc->width = output_layout_box->width;
-	output->swayc->height = output_layout_box->height;
-
-	arrange_windows(output->swayc, -1, -1);
-}
-
-static void output_resolution_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *output = wl_container_of(listener, output, resolution);
-	output_update_size(output);
-}
-
-static void output_scale_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *output = wl_container_of(listener, output, scale);
-	output_update_size(output);
-}
-
-static void output_transform_notify(struct wl_listener *listener, void *data) {
-	struct sway_output *output = wl_container_of(listener, output, transform);
-	output_update_size(output);
-}
-
 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;
@@ -129,8 +103,11 @@ void output_add_notify(struct wl_listener *listener, void *data) {
 	output->wlr_output = wlr_output;
 	output->server = server;
 
-	wl_signal_init(&output->events.scale);
-	wl_signal_init(&output->events.transform);
+	if (!wl_list_empty(&wlr_output->modes)) {
+		struct wlr_output_mode *mode =
+			wl_container_of(wlr_output->modes.prev, mode, link);
+		wlr_output_set_mode(wlr_output, mode);
+	}
 
 	output->swayc = new_output(output);
 	if (!output->swayc) {
@@ -138,22 +115,8 @@ void output_add_notify(struct wl_listener *listener, void *data) {
 		return;
 	}
 
-	if (!wl_list_empty(&wlr_output->modes)) {
-		struct wlr_output_mode *mode = NULL;
-		mode = wl_container_of((&wlr_output->modes)->prev, mode, link);
-		wlr_output_set_mode(wlr_output, mode);
-	}
-
 	output->frame.notify = output_frame_notify;
 	wl_signal_add(&wlr_output->events.frame, &output->frame);
-	output->resolution.notify = output_resolution_notify;
-	wl_signal_add(&wlr_output->events.resolution, &output->resolution);
-	output->scale.notify = output_scale_notify;
-	wl_signal_add(&output->events.scale, &output->scale);
-	output->transform.notify = output_transform_notify;
-	wl_signal_add(&output->events.transform, &output->transform);
-
-	arrange_windows(output->swayc, -1, -1);
 }
 
 void output_remove_notify(struct wl_listener *listener, void *data) {
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index fd17f8a5f..65382231b 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -16,10 +16,24 @@
 swayc_t root_container;
 
 static void output_layout_change_notify(struct wl_listener *listener, void *data) {
-	struct wlr_box *box = wlr_output_layout_get_box(
+	struct wlr_box *layout_box = wlr_output_layout_get_box(
 		root_container.sway_root->output_layout, NULL);
-	root_container.width = box->width;
-	root_container.height = box->height;
+	root_container.width = layout_box->width;
+	root_container.height = layout_box->height;
+
+	for (int i = 0 ; i < root_container.children->length; ++i) {
+		swayc_t *output_container = root_container.children->items[i];
+		struct sway_output *output = output_container->sway_output;
+
+		struct wlr_box *output_box = wlr_output_layout_get_box(
+			root_container.sway_root->output_layout, output->wlr_output);
+		output_container->x = output_box->x;
+		output_container->y = output_box->y;
+		output_container->width = output_box->width;
+		output_container->height = output_box->height;
+	}
+
+	arrange_windows(&root_container, -1, -1);
 }
 
 void init_layout(void) {

From ec2fd6e5c0217ae58a03eca1e83d85f02c739643 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Wed, 13 Dec 2017 21:47:37 +0100
Subject: [PATCH 12/15] Handle output remove

---
 sway/desktop/output.c | 16 +++++++++++++++-
 sway/tree/layout.c    |  6 ++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/sway/desktop/output.c b/sway/desktop/output.c
index 2177ad743..ad843b316 100644
--- a/sway/desktop/output.c
+++ b/sway/desktop/output.c
@@ -123,5 +123,19 @@ void output_remove_notify(struct wl_listener *listener, void *data) {
 	struct sway_server *server = wl_container_of(listener, server, output_remove);
 	struct wlr_output *wlr_output = data;
 	sway_log(L_DEBUG, "Output %p %s removed", wlr_output, wlr_output->name);
-	// TODO
+
+	swayc_t *output_container = NULL;
+	for (int i = 0 ; i < root_container.children->length; ++i) {
+		swayc_t *child = root_container.children->items[i];
+		if (child->type == C_OUTPUT &&
+				child->sway_output->wlr_output == wlr_output) {
+			output_container = child;
+			break;
+		}
+	}
+	if (!output_container) {
+		return;
+	}
+
+	destroy_output(output_container);
 }
diff --git a/sway/tree/layout.c b/sway/tree/layout.c
index 65382231b..4bcf0e2fd 100644
--- a/sway/tree/layout.c
+++ b/sway/tree/layout.c
@@ -23,10 +23,16 @@ static void output_layout_change_notify(struct wl_listener *listener, void *data
 
 	for (int i = 0 ; i < root_container.children->length; ++i) {
 		swayc_t *output_container = root_container.children->items[i];
+		if (output_container->type != C_OUTPUT) {
+			continue;
+		}
 		struct sway_output *output = output_container->sway_output;
 
 		struct wlr_box *output_box = wlr_output_layout_get_box(
 			root_container.sway_root->output_layout, output->wlr_output);
+		if (!output_box) {
+			continue;
+		}
 		output_container->x = output_box->x;
 		output_container->y = output_box->y;
 		output_container->width = output_box->width;

From 4d389f8b6523af741761009effd4d6dd79156afe Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Thu, 14 Dec 2017 00:45:47 +0100
Subject: [PATCH 13/15] Replace refresh_rate and position by mode in output
 command

---
 sway/commands/output.c | 87 ++++++++++++++++++++++++++----------------
 sway/config/output.c   |  2 +-
 2 files changed, 56 insertions(+), 33 deletions(-)

diff --git a/sway/commands/output.c b/sway/commands/output.c
index 23792855c..daaacad7e 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -42,61 +42,84 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 
 		if (strcasecmp(command, "disable") == 0) {
 			output->enabled = 0;
-		} else if (strcasecmp(command, "resolution") == 0 || strcasecmp(command, "res") == 0) {
+		} else if (strcasecmp(command, "mode") == 0 ||
+				strcasecmp(command, "resolution") == 0 ||
+				strcasecmp(command, "res") == 0) {
 			if (++i >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Missing mode argument.");
 				goto fail;
 			}
-			char *res = argv[i];
-			char *x = strchr(res, 'x');
+
 			int width = -1, height = -1;
-			if (x != NULL) {
+			float refresh_rate = -1;
+
+			char *end;
+			width = strtol(argv[i], &end, 10);
+			if (*end) {
 				// Format is 1234x4321
-				*x = '\0';
-				width = atoi(res);
-				height = atoi(x + 1);
-				*x = 'x';
-			} else {
-				// Format is 1234 4321
-				width = atoi(res);
-				if (++i >= argc) {
-					error = cmd_results_new(CMD_INVALID, "output", "Missing resolution argument (height).");
+				if (*end != 'x') {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Invalid mode width.");
+					goto fail;
+				}
+				++end;
+				height = strtol(end, &end, 10);
+				if (*end) {
+					if (*end != '@') {
+						error = cmd_results_new(CMD_INVALID, "output",
+							"Invalid mode height.");
+						goto fail;
+					}
+					++end;
+					refresh_rate = strtof(end, &end);
+					if (strcasecmp("Hz", end) != 0) {
+						error = cmd_results_new(CMD_INVALID, "output",
+							"Invalid mode refresh rate.");
+						goto fail;
+					}
+				}
+			} else {
+				// Format is 1234 4321 (legacy)
+				++i;
+				if (i >= argc) {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Missing mode argument (height).");
+					goto fail;
+				}
+				height = strtol(argv[i], &end, 10);
+				if (*end) {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Invalid mode height.");
 					goto fail;
 				}
-				res = argv[i];
-				height = atoi(res);
 			}
 			output->width = width;
 			output->height = height;
-		} else if (strcasecmp(command, "refresh_rate") == 0) {
-			if (++i >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing refresh_rate argument.");
-				goto fail;
-			}
-			output->refresh_rate = atof(argv[i]);
+			output->refresh_rate = refresh_rate;
 		} else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
 			if (++i >= argc) {
 				error = cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
 				goto fail;
 			}
-			char *res = argv[i];
-			char *c = strchr(res, ',');
+			char *pos = argv[i];
+			char *c = strchr(pos, ',');
 			int x = -1, y = -1;
 			if (c != NULL) {
 				// Format is 1234,4321
 				*c = '\0';
-				x = atoi(res);
+				x = atoi(pos);
 				y = atoi(c + 1);
 				*c = ',';
 			} else {
 				// Format is 1234 4321
-				x = atoi(res);
+				x = atoi(pos);
 				if (++i >= argc) {
 					error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
 					goto fail;
 				}
-				res = argv[i];
-				y = atoi(res);
+				pos = argv[i];
+				y = atoi(pos);
 			}
 			output->x = x;
 			output->y = y;
@@ -218,11 +241,11 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 		list_add(config->output_configs, output);
 	}
 
-	sway_log(L_DEBUG, "Config stored for output %s (enabled:%d) (%d x %d @ "
-			"%d, %d scale %d transform %d refresh_rate %f) (bg %s %s)",
+	sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz "
+			"position %d,%d scale %d transform %d) (bg %s %s)",
 			output->name, output->enabled, output->width,
-			output->height, output->x, output->y, output->scale,
-			output->transform, output->refresh_rate,
+			output->height, output->refresh_rate, output->x, output->y,
+			output->scale, output->transform,
 			output->background, output->background_option);
 
 	if (output->name) {
diff --git a/sway/config/output.c b/sway/config/output.c
index 8d946386f..dc9ee37c3 100644
--- a/sway/config/output.c
+++ b/sway/config/output.c
@@ -20,7 +20,7 @@ struct output_config *new_output_config() {
 		return NULL;
 	}
 	oc->enabled = -1;
-	oc->width = oc->height -1;
+	oc->width = oc->height = -1;
 	oc->refresh_rate = -1;
 	oc->x = oc->y = -1;
 	oc->scale = -1;

From 9254c5a93f1a38612d5391e34e4fe3b6c9e433a7 Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Thu, 14 Dec 2017 00:50:01 +0100
Subject: [PATCH 14/15] Fail if unknown output subcommand

---
 sway/commands/output.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/sway/commands/output.c b/sway/commands/output.c
index daaacad7e..16b711f19 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -192,7 +192,7 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 
 				char *src = join_args(argv + i, j);
 				if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
-					error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s)", src);
+					error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src);
 					goto fail;
 				}
 				free(src);
@@ -213,7 +213,7 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 					}
 				}
 				if (!src || access(src, F_OK) == -1) {
-					error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s)", src);
+					error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s).", src);
 					wordfree(&p);
 					goto fail;
 				}
@@ -227,6 +227,9 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 
 				i += j;
 			}
+		} else {
+			error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command);
+			goto fail;
 		}
 	}
 

From cba592b3d2c516cab9f4e8325037dfdf6e30cb9a Mon Sep 17 00:00:00 2001
From: emersion <contact@emersion.fr>
Date: Thu, 14 Dec 2017 02:23:33 +0100
Subject: [PATCH 15/15] Use strtol instead of atoi in output command

---
 sway/commands/output.c | 118 ++++++++++++++++++++++++++---------------
 sway/tree/container.c  |   2 -
 2 files changed, 76 insertions(+), 44 deletions(-)

diff --git a/sway/commands/output.c b/sway/commands/output.c
index 16b711f19..d71e4d8d4 100644
--- a/sway/commands/output.c
+++ b/sway/commands/output.c
@@ -34,8 +34,6 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 	}
 	output->name = strdup(name);
 
-	// TODO: atoi doesn't handle invalid numbers
-
 	int i;
 	for (i = 1; i < argc; ++i) {
 		const char *command = argv[i];
@@ -80,9 +78,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 					}
 				}
 			} else {
-				// Format is 1234 4321 (legacy)
-				++i;
-				if (i >= argc) {
+				// Format is 1234 4321
+				if (++i >= argc) {
 					error = cmd_results_new(CMD_INVALID, "output",
 						"Missing mode argument (height).");
 					goto fail;
@@ -97,41 +94,66 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 			output->width = width;
 			output->height = height;
 			output->refresh_rate = refresh_rate;
-		} else if (strcasecmp(command, "position") == 0 || strcasecmp(command, "pos") == 0) {
+		} else if (strcasecmp(command, "position") == 0 ||
+				strcasecmp(command, "pos") == 0) {
 			if (++i >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing position argument.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Missing position argument.");
 				goto fail;
 			}
-			char *pos = argv[i];
-			char *c = strchr(pos, ',');
+
 			int x = -1, y = -1;
-			if (c != NULL) {
+
+			char *end;
+			x = strtol(argv[i], &end, 10);
+			if (*end) {
 				// Format is 1234,4321
-				*c = '\0';
-				x = atoi(pos);
-				y = atoi(c + 1);
-				*c = ',';
-			} else {
-				// Format is 1234 4321
-				x = atoi(pos);
-				if (++i >= argc) {
-					error = cmd_results_new(CMD_INVALID, "output", "Missing position argument (y).");
+				if (*end != ',') {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Invalid position x.");
+					goto fail;
+				}
+				++end;
+				y = strtol(end, &end, 10);
+				if (*end) {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Invalid position y.");
+					goto fail;
+				}
+			} else {
+				// Format is 1234 4321 (legacy)
+				if (++i >= argc) {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Missing position argument (y).");
+					goto fail;
+				}
+				y = strtol(argv[i], &end, 10);
+				if (*end) {
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Invalid position y.");
 					goto fail;
 				}
-				pos = argv[i];
-				y = atoi(pos);
 			}
+
 			output->x = x;
 			output->y = y;
 		} else if (strcasecmp(command, "scale") == 0) {
 			if (++i >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing scale parameter.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Missing scale parameter.");
+				goto fail;
+			}
+			char *end;
+			output->scale = strtol(argv[i], &end, 10);
+			if (*end) {
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Invalid scale.");
 				goto fail;
 			}
-			output->scale = atoi(argv[i]);
 		} else if (strcasecmp(command, "transform") == 0) {
 			if (++i >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing transform parameter.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Missing transform parameter.");
 				goto fail;
 			}
 			char *value = argv[i];
@@ -152,17 +174,21 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 			} else if (strcmp(value, "flipped-270") == 0) {
 				output->transform = WL_OUTPUT_TRANSFORM_FLIPPED_270;
 			} else {
-				error = cmd_results_new(CMD_INVALID, "output", "Invalid output transform.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Invalid output transform.");
 				goto fail;
 			}
-		} else if (strcasecmp(command, "background") == 0 || strcasecmp(command, "bg") == 0) {
+		} else if (strcasecmp(command, "background") == 0 ||
+				strcasecmp(command, "bg") == 0) {
 			wordexp_t p;
 			if (++i >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing background file or color specification.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Missing background file or color specification.");
 				goto fail;
 			}
 			if (i + 1 >= argc) {
-				error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode or `solid_color`.");
+				error = cmd_results_new(CMD_INVALID, "output",
+					"Missing background scaling mode or `solid_color`.");
 				goto fail;
 			}
 			if (strcasecmp(argv[i + 1], "solid_color") == 0) {
@@ -175,7 +201,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 				size_t j;
 				for (j = 0; j < (size_t) (argc - i); ++j) {
 					mode = argv[i + j];
-					for (size_t k = 0; k < sizeof(bg_options) / sizeof(char *); ++k) {
+					size_t n = sizeof(bg_options) / sizeof(char *);
+					for (size_t k = 0; k < n; ++k) {
 						if (strcasecmp(mode, bg_options[k]) == 0) {
 							valid = true;
 							break;
@@ -186,13 +213,15 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 					}
 				}
 				if (!valid) {
-					error = cmd_results_new(CMD_INVALID, "output", "Missing background scaling mode.");
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Missing background scaling mode.");
 					goto fail;
 				}
 
 				char *src = join_args(argv + i, j);
 				if (wordexp(src, &p, 0) != 0 || p.we_wordv[0] == NULL) {
-					error = cmd_results_new(CMD_INVALID, "output", "Invalid syntax (%s).", src);
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Invalid syntax (%s).", src);
 					goto fail;
 				}
 				free(src);
@@ -205,15 +234,18 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 						if (src) {
 							sprintf(src, "%s/%s", conf_path, p.we_wordv[0]);
 						} else {
-							sway_log(L_ERROR, "Unable to allocate background source");
+							sway_log(L_ERROR,
+								"Unable to allocate background source");
 						}
 						free(conf);
 					} else {
-						sway_log(L_ERROR, "Unable to allocate background source");
+						sway_log(L_ERROR,
+							"Unable to allocate background source");
 					}
 				}
 				if (!src || access(src, F_OK) == -1) {
-					error = cmd_results_new(CMD_INVALID, "output", "Background file unreadable (%s).", src);
+					error = cmd_results_new(CMD_INVALID, "output",
+						"Background file unreadable (%s).", src);
 					wordfree(&p);
 					goto fail;
 				}
@@ -228,7 +260,8 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 				i += j;
 			}
 		} else {
-			error = cmd_results_new(CMD_INVALID, "output", "Invalid output subcommand: %s.", command);
+			error = cmd_results_new(CMD_INVALID, "output",
+				"Invalid output subcommand: %s.", command);
 			goto fail;
 		}
 	}
@@ -245,11 +278,10 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 	}
 
 	sway_log(L_DEBUG, "Config stored for output %s (enabled: %d) (%dx%d@%fHz "
-			"position %d,%d scale %d transform %d) (bg %s %s)",
-			output->name, output->enabled, output->width,
-			output->height, output->refresh_rate, output->x, output->y,
-			output->scale, output->transform,
-			output->background, output->background_option);
+		"position %d,%d scale %d transform %d) (bg %s %s)",
+		output->name, output->enabled, output->width, output->height,
+		output->refresh_rate, output->x, output->y, output->scale,
+		output->transform, output->background, output->background_option);
 
 	if (output->name) {
 		// Try to find the output container and apply configuration now. If
@@ -258,11 +290,13 @@ struct cmd_results *cmd_output(int argc, char **argv) {
 		swayc_t *cont = NULL;
 		for (int i = 0; i < root_container.children->length; ++i) {
 			cont = root_container.children->items[i];
-			if (cont->name && ((strcmp(cont->name, output->name) == 0) || (strcmp(output->name, "*") == 0))) {
+			if (cont->name && ((strcmp(cont->name, output->name) == 0) ||
+					(strcmp(output->name, "*") == 0))) {
 				apply_output_config(output, cont);
 
 				if (strcmp(output->name, "*") != 0) {
-					// stop looking if the output config isn't applicable to all outputs
+					// Stop looking if the output config isn't applicable to all
+					// outputs
 					break;
 				}
 			}
diff --git a/sway/tree/container.c b/sway/tree/container.c
index f70bccdc6..5df10bcb8 100644
--- a/sway/tree/container.c
+++ b/sway/tree/container.c
@@ -179,8 +179,6 @@ swayc_t *destroy_output(swayc_t *output) {
 				add_child(root_container.children->items[p], child);
 			}
 			sort_workspaces(root_container.children->items[p]);
-			// TODO WLR: is this needed anymore?
-			//update_visibility(root_container.children->items[p]);
 			arrange_windows(root_container.children->items[p], -1, -1);
 		}
 	}