weston: Port X11 backend to new output handling API

This is a complete port of the X11 backend that
uses recently added output handling API for output
configuration.

- Output can be configured at runtime by passing the
  necessary configuration parameters, which can be
  filled in manually, obtained from the configuration
  file or obtained from the command line using
  previously added functionality. It is required that
  the scale and transform values are set using the
  previously added functionality.

- Output can be created at runtime using the output
  API. The output creation only creates a pending
  output, which needs to be configured the same way as
  mentioned above.

Same as before, a single output is created at runtime
using the default configuration or a configuration
parsed from the command line. The output-count
functionality is also preserved, which means more than
one output can be created initially, and more outputs can
be added at runtime using the output API.

v2:

 - Fix wet_configure_windowed_output_from_config() usage.
 - Call x11_output_disable() explicitly from
   x11_output_destroy().

v3:

 - Remove unneeded free().
 - Disallow calling x11_output_configure more than once.
 - Remove unneeded checks for output->name == NULL as that
   has been disallowed.
 - Use weston_compositor_add_pending_output().
 - Bump weston_x11_backend_config version to 2.

Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Signed-off-by: Armin Krezović <krezovic.armin@gmail.com>
diff --git a/compositor/main.c b/compositor/main.c
index d19b3f0..fca9778 100644
--- a/compositor/main.c
+++ b/compositor/main.c
@@ -1426,48 +1426,43 @@
 	return ret;
 }
 
-static int
-weston_x11_backend_config_append_output_config(struct weston_x11_backend_config *config,
-					       struct weston_x11_backend_output_config *output_config) {
-	struct weston_x11_backend_output_config *new_outputs;
+static void
+x11_backend_output_configure(struct wl_listener *listener, void *data)
+{
+	struct weston_output *output = data;
+	struct wet_output_config defaults = {
+		.width = 1024,
+		.height = 600,
+		.scale = 1,
+		.transform = WL_OUTPUT_TRANSFORM_NORMAL
+	};
 
-	new_outputs = realloc(config->outputs, (config->num_outputs+1) *
-			      sizeof(struct weston_x11_backend_output_config));
-	if (new_outputs == NULL)
-		return -1;
-
-	config->outputs = new_outputs;
-	config->outputs[config->num_outputs].width = output_config->width;
-	config->outputs[config->num_outputs].height = output_config->height;
-	config->outputs[config->num_outputs].transform = output_config->transform;
-	config->outputs[config->num_outputs].scale = output_config->scale;
-	config->outputs[config->num_outputs].name = strdup(output_config->name);
-	config->num_outputs++;
-
-	return 0;
+	if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
+		weston_log("Cannot configure output \"%s\".\n", output->name);
 }
 
 static int
 load_x11_backend(struct weston_compositor *c,
 		 int *argc, char **argv, struct weston_config *wc)
 {
-	struct weston_x11_backend_output_config default_output;
+	char *default_output;
+	const struct weston_windowed_output_api *api;
 	struct weston_x11_backend_config config = {{ 0, }};
 	struct weston_config_section *section;
 	int ret = 0;
-	int option_width = 0;
-	int option_height = 0;
-	int option_scale = 0;
 	int option_count = 1;
 	int output_count = 0;
 	char const *section_name;
 	int i;
-	uint32_t j;
+
+	struct wet_output_config *parsed_options = wet_init_parsed_options(c);
+	if (!parsed_options)
+		return -1;
 
 	const struct weston_option options[] = {
-	       { WESTON_OPTION_INTEGER, "width", 0, &option_width },
-	       { WESTON_OPTION_INTEGER, "height", 0, &option_height },
-	       { WESTON_OPTION_INTEGER, "scale", 0, &option_scale },
+	       { WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
+	       { WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
+	       { WESTON_OPTION_INTEGER, "scale", 0, &parsed_options->scale },
 	       { WESTON_OPTION_BOOLEAN, "fullscreen", 'f', &config.fullscreen },
 	       { WESTON_OPTION_INTEGER, "output-count", 0, &option_count },
 	       { WESTON_OPTION_BOOLEAN, "no-input", 0, &config.no_input },
@@ -1476,81 +1471,6 @@
 
 	parse_options(options, ARRAY_LENGTH(options), argc, argv);
 
-	section = NULL;
-	while (weston_config_next_section(wc, &section, &section_name)) {
-		struct weston_x11_backend_output_config current_output = { 0, };
-		char *t;
-		char *mode;
-
-		if (strcmp(section_name, "output") != 0) {
-			continue;
-		}
-
-		weston_config_section_get_string(section, "name", &current_output.name, NULL);
-		if (current_output.name == NULL || current_output.name[0] != 'X') {
-			free(current_output.name);
-			continue;
-		}
-
-		weston_config_section_get_string(section, "mode", &mode, "1024x600");
-		if (sscanf(mode, "%dx%d", &current_output.width,
-			   &current_output.height) != 2) {
-			weston_log("Invalid mode \"%s\" for output %s\n",
-				   mode, current_output.name);
-			current_output.width = 1024;
-			current_output.height = 600;
-		}
-		free(mode);
-		if (current_output.width < 1)
-			current_output.width = 1024;
-		if (current_output.height < 1)
-			current_output.height = 600;
-		if (option_width)
-			current_output.width = option_width;
-		if (option_height)
-			current_output.height = option_height;
-
-		weston_config_section_get_int(section, "scale", &current_output.scale, 1);
-		if (option_scale)
-			current_output.scale = option_scale;
-
-		weston_config_section_get_string(section,
-						 "transform", &t, "normal");
-		if (weston_parse_transform(t, &current_output.transform) < 0)
-			weston_log("Invalid transform \"%s\" for output %s\n",
-				   t, current_output.name);
-		free(t);
-
-		if (weston_x11_backend_config_append_output_config(&config, &current_output) < 0) {
-			ret = -1;
-			goto out;
-		}
-
-		output_count++;
-		if (output_count >= option_count)
-			break;
-	}
-
-	default_output.name = NULL;
-	default_output.width = option_width ? option_width : 1024;
-	default_output.height = option_height ? option_height : 600;
-	default_output.scale = option_scale ? option_scale : 1;
-	default_output.transform = WL_OUTPUT_TRANSFORM_NORMAL;
-
-	for (i = output_count; i < option_count; i++) {
-		if (asprintf(&default_output.name, "screen%d", i) < 0) {
-			ret = -1;
-			goto out;
-		}
-
-		if (weston_x11_backend_config_append_output_config(&config, &default_output) < 0) {
-			ret = -1;
-			free(default_output.name);
-			goto out;
-		}
-		free(default_output.name);
-	}
-
 	config.base.struct_version = WESTON_X11_BACKEND_CONFIG_VERSION;
 	config.base.struct_size = sizeof(struct weston_x11_backend_config);
 
@@ -1558,12 +1478,59 @@
 	ret = weston_compositor_load_backend(c, WESTON_BACKEND_X11,
 					     &config.base);
 
-out:
-	for (j = 0; j < config.num_outputs; ++j)
-		free(config.outputs[j].name);
-	free(config.outputs);
+	if (ret < 0)
+		return ret;
 
-	return ret;
+	wet_set_pending_output_handler(c, x11_backend_output_configure);
+
+	api = weston_windowed_output_get_api(c);
+
+	if (!api) {
+		weston_log("Cannot use weston_windowed_output_api.\n");
+		return -1;
+	}
+
+	section = NULL;
+	while (weston_config_next_section(wc, &section, &section_name)) {
+		char *output_name;
+
+		if (output_count >= option_count)
+			break;
+
+		if (strcmp(section_name, "output") != 0) {
+			continue;
+		}
+
+		weston_config_section_get_string(section, "name", &output_name, NULL);
+		if (output_name == NULL || output_name[0] != 'X') {
+			free(output_name);
+			continue;
+		}
+
+		if (api->output_create(c, output_name) < 0) {
+			free(output_name);
+			return -1;
+		}
+		free(output_name);
+
+		output_count++;
+	}
+
+	default_output = NULL;
+
+	for (i = output_count; i < option_count; i++) {
+		if (asprintf(&default_output, "screen%d", i) < 0) {
+			return -1;
+		}
+
+		if (api->output_create(c, default_output) < 0) {
+			free(default_output);
+			return -1;
+		}
+		free(default_output);
+	}
+
+	return 0;
 }
 
 static void