weston: Port headless backend to new output handling API

This is a complete port of the headless backend that
uses the 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.

After everything has been set, output needs to be
enabled manually using weston_output_enable().

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

New feature:

This patch also adds, as a bonus of using shared
functionality, support for setting options for outputs
created by this backend in the weston config file in
addition to setting them from the command line.

v2:

 - Fix wet_configure_windowed_output_from_config() usage.
 - Call headless_output_disable() explicitly from
   headless_output_destroy().

v3:

 - Add scale support to output width and height.
 - Use scaled values in calls to various functions which
   require width and height.
 - Disallow calling headless_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_headless_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 2a1f0e1..12f5e76 100644
--- a/compositor/main.c
+++ b/compositor/main.c
@@ -1208,31 +1208,50 @@
 	return ret;
 }
 
+static void
+headless_backend_output_configure(struct wl_listener *listener, void *data)
+{
+	struct weston_output *output = data;
+	struct wet_output_config defaults = {
+		.width = 1024,
+		.height = 640,
+		.scale = 1,
+		.transform = WL_OUTPUT_TRANSFORM_NORMAL
+	};
+
+	if (wet_configure_windowed_output_from_config(output, &defaults) < 0)
+		weston_log("Cannot configure output \"%s\".\n", output->name);
+}
+
 static int
 load_headless_backend(struct weston_compositor *c,
 		      int *argc, char **argv, struct weston_config *wc)
 {
+	const struct weston_windowed_output_api *api;
 	struct weston_headless_backend_config config = {{ 0, }};
+	int no_outputs = 0;
 	int ret = 0;
 	char *transform = NULL;
 
-	config.width = 1024;
-	config.height = 640;
+	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, &config.width },
-		{ WESTON_OPTION_INTEGER, "height", 0, &config.height },
+		{ WESTON_OPTION_INTEGER, "width", 0, &parsed_options->width },
+		{ WESTON_OPTION_INTEGER, "height", 0, &parsed_options->height },
 		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
 		{ WESTON_OPTION_STRING, "transform", 0, &transform },
-		{ WESTON_OPTION_BOOLEAN, "no-outputs", 0, &config.no_outputs },
+		{ WESTON_OPTION_BOOLEAN, "no-outputs", 0, &no_outputs },
 	};
 
 	parse_options(options, ARRAY_LENGTH(options), argc, argv);
 
-	config.transform = WL_OUTPUT_TRANSFORM_NORMAL;
 	if (transform) {
-		if (weston_parse_transform(transform, &config.transform) < 0)
+		if (weston_parse_transform(transform, &parsed_options->transform) < 0) {
 			weston_log("Invalid transform \"%s\"\n", transform);
+			parsed_options->transform = UINT32_MAX;
+		}
 		free(transform);
 	}
 
@@ -1243,7 +1262,24 @@
 	ret = weston_compositor_load_backend(c, WESTON_BACKEND_HEADLESS,
 					     &config.base);
 
-	return ret;
+	if (ret < 0)
+		return ret;
+
+	wet_set_pending_output_handler(c, headless_backend_output_configure);
+
+	if (!no_outputs) {
+		api = weston_windowed_output_get_api(c);
+
+		if (!api) {
+			weston_log("Cannot use weston_windowed_output_api.\n");
+			return -1;
+		}
+
+		if (api->output_create(c, "headless") < 0)
+			return -1;
+	}
+
+	return 0;
 }
 
 static void