[PATCH weston v2 9/9] wayland-backend: move all parsing functions to weston

Benoit Gschwind gschwind at gnu-log.net
Thu Apr 28 18:33:16 UTC 2016


Move all parsing functions from the wayland backend and put
them into the weston code and add versionning to configuration
structure.

Signed-off-by: Benoit Gschwind <gschwind at gnu-log.net>
---
 src/compositor-wayland.c | 232 ++++-------------------------------------------
 src/compositor-wayland.h |   5 +
 src/main.c               | 211 +++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 232 insertions(+), 216 deletions(-)

diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index 142cb3a..6083bf3 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -51,8 +51,6 @@
 #include "presentation-time-server-protocol.h"
 #include "linux-dmabuf.h"
 
-#define WINDOW_TITLE "Weston Compositor"
-
 struct wayland_backend {
 	struct weston_backend base;
 	struct weston_compositor *compositor;
@@ -1091,59 +1089,6 @@ err_name:
 	return NULL;
 }
 
-static void
-wayland_output_init_from_config(struct weston_wayland_backend_output_config *output,
-				struct weston_config_section *config_section,
-				int option_width, int option_height,
-				int option_scale)
-{
-	char *mode, *t, *str;
-	unsigned int slen;
-
-	weston_config_section_get_string(config_section, "name", &output->name,
-					 NULL);
-	if (output->name) {
-		slen = strlen(output->name);
-		slen += strlen(WINDOW_TITLE " - ");
-		str = malloc(slen + 1);
-		if (str)
-			snprintf(str, slen + 1, WINDOW_TITLE " - %s",
-				 output->name);
-		free(output->name);
-		output->name = str;
-	}
-	if (!output->name)
-		output->name = strdup(WINDOW_TITLE);
-
-	weston_config_section_get_string(config_section,
-					 "mode", &mode, "1024x600");
-	if (sscanf(mode, "%dx%d", &output->width, &output->height) != 2) {
-		weston_log("Invalid mode \"%s\" for output %s\n",
-			   mode, output->name);
-		output->width = 1024;
-		output->height = 640;
-	}
-	free(mode);
-
-	if (option_width)
-		output->width = option_width;
-	if (option_height)
-		output->height = option_height;
-
-	weston_config_section_get_int(config_section, "scale", &output->scale, 1);
-
-	if (option_scale)
-		output->scale = option_scale;
-
-	weston_config_section_get_string(config_section,
-					 "transform", &t, "normal");
-	if (weston_parse_transform(t, &output->transform) < 0)
-		weston_log("Invalid transform \"%s\" for output %s\n",
-			   t, output->name);
-	free(t);
-
-}
-
 static struct wayland_output *
 wayland_output_create_for_config(struct wayland_backend *b,
 				 struct weston_wayland_backend_output_config *oc,
@@ -2326,176 +2271,37 @@ wayland_backend_destroy(struct wayland_backend *b)
 }
 
 static void
-wayland_backend_config_release(struct weston_wayland_backend_config *new_config) {
-	int i;
-	for (i = 0; i < new_config->num_outputs; ++i) {
-		free(new_config->outputs[i].name);
-	}
-	free(new_config->cursor_theme);
-	free(new_config->display_name);
-	free(new_config->outputs);
-}
-
-/*
- * Append a new output struct at the end of new_config.outputs and return a
- * pointer to the newly allocated structure or NULL if fail. The allocated
- * structure is NOT cleared nor set to default values.
- */
-static struct weston_wayland_backend_output_config *
-wayland_backend_config_add_new_output(struct weston_wayland_backend_config *new_config) {
-	struct weston_wayland_backend_output_config *outputs;
-	outputs = realloc(new_config->outputs,
-			  (new_config->num_outputs+1)*sizeof(struct weston_wayland_backend_output_config));
-	if (!outputs)
-		return NULL;
-	new_config->num_outputs += 1;
-	new_config->outputs = outputs;
-	return &(new_config->outputs[new_config->num_outputs-1]);
-}
-
-static int
-load_wayland_backend_config(struct weston_compositor *compositor, int *argc, char *argv[],
-	     struct weston_config *config,
-	     struct weston_wayland_backend_config *out_config)
-{
-	struct weston_wayland_backend_config new_config = { 0, };
-	struct weston_config_section *section;
-	struct weston_wayland_backend_output_config *oc;
-	int count, width, height, scale;
-	const char *section_name;
-	char *name;
-
-	const struct weston_option wayland_options[] = {
-		{ WESTON_OPTION_INTEGER, "width", 0, &width },
-		{ WESTON_OPTION_INTEGER, "height", 0, &height },
-		{ WESTON_OPTION_INTEGER, "scale", 0, &scale },
-		{ WESTON_OPTION_STRING, "display", 0, &new_config.display_name },
-		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &new_config.use_pixman },
-		{ WESTON_OPTION_INTEGER, "output-count", 0, &count },
-		{ WESTON_OPTION_BOOLEAN, "fullscreen", 0, &new_config.fullscreen },
-		{ WESTON_OPTION_BOOLEAN, "sprawl", 0, &new_config.sprawl },
-	};
-
-	width = 0;
-	height = 0;
-	scale = 0;
-	new_config.display_name = NULL;
-	new_config.use_pixman = 0;
-	count = 1;
-	new_config.fullscreen = 0;
-	new_config.sprawl = 0;
-	parse_options(wayland_options,
-		      ARRAY_LENGTH(wayland_options), argc, argv);
-
-	new_config.cursor_size = 32;
-	new_config.cursor_theme = NULL;
-
-	section = weston_config_get_section(config, "shell", NULL, NULL);
-	weston_config_section_get_string(section, "cursor-theme",
-					 &new_config.cursor_theme, NULL);
-	weston_config_section_get_int(section, "cursor-size",
-				      &new_config.cursor_size, 32);
-
-	if (new_config.sprawl) {
-		/* do nothing, everything is already set */
-		*out_config = new_config;
-		return 0;
-	}
-
-	if (new_config.fullscreen) {
-
-		oc = wayland_backend_config_add_new_output(&new_config);
-
-		if (!oc)
-			goto err_outputs;
-
-		oc->width = width;
-		oc->height = height;
-		oc->name = NULL;
-		oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
-		oc->scale = 1;
-
-		*out_config = new_config;
-		return 0;
-	}
-
-	section = NULL;
-	while (weston_config_next_section(config, &section, &section_name)) {
-		if (!section_name || strcmp(section_name, "output") != 0)
-			continue;
-		weston_config_section_get_string(section, "name", &name, NULL);
-		if (name == NULL)
-			continue;
-
-		if (name[0] != 'W' || name[1] != 'L') {
-			free(name);
-			continue;
-		}
-		free(name);
-
-		oc = wayland_backend_config_add_new_output(&new_config);
-
-		if (!oc)
-			goto err_outputs;
-
-		wayland_output_init_from_config(oc, section, width,
-						height, scale);
-		--count;
-	}
-
-	if (!width)
-		width = 1024;
-	if (!height)
-		height = 640;
-	if (!scale)
-		scale = 1;
-	while (count > 0) {
-
-		oc = wayland_backend_config_add_new_output(&new_config);
-
-		if (!oc)
-			goto err_outputs;
-
-		oc->width = width;
-		oc->height = height;
-		oc->name = NULL;
-		oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
-		oc->scale = scale;
-
-		--count;
-	}
-
-	*out_config = new_config;
-	return 0;
-
-err_outputs:
-	wayland_backend_config_release(&new_config);
-	return -1;
+config_init_to_defaults(struct weston_wayland_backend_config *config)
+{
 }
 
 WL_EXPORT int
 backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
-	     struct weston_config *config,
+	     struct weston_config *wc,
 	     struct weston_backend_config *config_base)
 {
 	struct wayland_backend *b;
 	struct wayland_output *output;
 	struct wayland_parent_output *poutput;
-	struct weston_wayland_backend_config new_config;
+	struct weston_wayland_backend_config config = {{ 0, }};
 	int x, count;
 
-	if(load_wayland_backend_config(compositor, argc, argv, config,
-				    &new_config) < 0) {
-		wayland_backend_config_release(&new_config);
+	if (config_base == NULL ||
+	    config_base->struct_version != WESTON_WAYLAND_BACKEND_CONFIG_VERSION ||
+	    config_base->struct_size > sizeof(struct weston_wayland_backend_config)) {
+		weston_log("wayland backend config structure is invalid\n");
 		return -1;
 	}
 
-	b = wayland_backend_create(compositor, &new_config, argc, argv, config);
+	config_init_to_defaults(&config);
+	memcpy(&config, config_base, config_base->struct_size);
+
+	b = wayland_backend_create(compositor, &config, argc, argv, wc);
 
 	if (!b)
 		return -1;
 
-	if (new_config.sprawl || b->parent.fshell) {
+	if (config.sprawl || b->parent.fshell) {
 		b->sprawl_across_outputs = 1;
 		wl_display_roundtrip(b->parent.wl_display);
 
@@ -2505,12 +2311,12 @@ backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
 		return 0;
 	}
 
-	if (new_config.fullscreen) {
-		if (new_config.num_outputs != 1 || !new_config.outputs)
+	if (config.fullscreen) {
+		if (config.num_outputs != 1 || !config.outputs)
 			goto err_outputs;
 
 		output = wayland_output_create_for_config(b,
-							  &new_config.outputs[0],
+							  &config.outputs[0],
 							  1, 0, 0);
 		if (!output)
 			goto err_outputs;
@@ -2520,8 +2326,8 @@ backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
 	}
 
 	x = 0;
-	for (count = 0; count < new_config.num_outputs; ++count) {
-		output = wayland_output_create_for_config(b, &new_config.outputs[count],
+	for (count = 0; count < config.num_outputs; ++count) {
+		output = wayland_output_create_for_config(b, &config.outputs[count],
 							  0, x, 0);
 		if (!output)
 			goto err_outputs;
@@ -2534,11 +2340,9 @@ backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
 	weston_compositor_add_key_binding(compositor, KEY_F,
 				          MODIFIER_CTRL | MODIFIER_ALT,
 				          fullscreen_binding, b);
-	wayland_backend_config_release(&new_config);
 	return 0;
 
 err_outputs:
 	wayland_backend_destroy(b);
-	wayland_backend_config_release(&new_config);
 	return -1;
 }
diff --git a/src/compositor-wayland.h b/src/compositor-wayland.h
index ab216eb..7032f08 100644
--- a/src/compositor-wayland.h
+++ b/src/compositor-wayland.h
@@ -32,6 +32,10 @@ extern "C" {
 
 #include "compositor.h"
 
+#define WESTON_WAYLAND_BACKEND_CONFIG_VERSION 1
+
+#define WINDOW_TITLE "Weston Compositor"
+
 struct weston_wayland_backend_output_config {
 	int width;
 	int height;
@@ -41,6 +45,7 @@ struct weston_wayland_backend_output_config {
 };
 
 struct weston_wayland_backend_config {
+	struct weston_backend_config base;
 	int use_pixman;
 	int sprawl;
 	char *display_name;
diff --git a/src/main.c b/src/main.c
index f034dda..f90bd7b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -49,6 +49,7 @@
 
 #include "compositor-headless.h"
 #include "compositor-rdp.h"
+#include "compositor-wayland.h"
 
 static struct wl_list child_process_list;
 static struct weston_compositor *segv_compositor;
@@ -767,6 +768,212 @@ load_rdp_backend(struct weston_compositor *c, char const * backend,
 	return ret;
 }
 
+static void
+weston_wayland_backend_config_release(struct weston_wayland_backend_config *new_config) {
+	int i;
+	for (i = 0; i < new_config->num_outputs; ++i) {
+		free(new_config->outputs[i].name);
+	}
+	free(new_config->cursor_theme);
+	free(new_config->display_name);
+	free(new_config->outputs);
+}
+
+/*
+ * Append a new output struct at the end of new_config.outputs and return a
+ * pointer to the newly allocated structure or NULL if fail. The allocated
+ * structure is NOT cleared nor set to default values.
+ */
+static struct weston_wayland_backend_output_config *
+weston_wayland_backend_config_add_new_output(struct weston_wayland_backend_config *new_config) {
+	struct weston_wayland_backend_output_config *outputs;
+	outputs = realloc(new_config->outputs,
+			  (new_config->num_outputs+1)*sizeof(struct weston_wayland_backend_output_config));
+	if (!outputs)
+		return NULL;
+	new_config->num_outputs += 1;
+	new_config->outputs = outputs;
+	return &(new_config->outputs[new_config->num_outputs-1]);
+}
+
+static void
+weston_wayland_backend_output_init(struct weston_wayland_backend_output_config *output,
+				   struct weston_config_section *config_section,
+				   int option_width, int option_height,
+				   int option_scale)
+{
+	char *mode, *t, *str;
+	unsigned int slen;
+
+	weston_config_section_get_string(config_section, "name", &output->name,
+					 NULL);
+	if (output->name) {
+		slen = strlen(output->name);
+		slen += strlen(WINDOW_TITLE " - ");
+		str = malloc(slen + 1);
+		if (str)
+			snprintf(str, slen + 1, WINDOW_TITLE " - %s",
+				 output->name);
+		free(output->name);
+		output->name = str;
+	}
+	if (!output->name)
+		output->name = strdup(WINDOW_TITLE);
+
+	weston_config_section_get_string(config_section,
+					 "mode", &mode, "1024x600");
+	if (sscanf(mode, "%dx%d", &output->width, &output->height) != 2) {
+		weston_log("Invalid mode \"%s\" for output %s\n",
+			   mode, output->name);
+		output->width = 1024;
+		output->height = 640;
+	}
+	free(mode);
+
+	if (option_width)
+		output->width = option_width;
+	if (option_height)
+		output->height = option_height;
+
+	weston_config_section_get_int(config_section, "scale", &output->scale, 1);
+
+	if (option_scale)
+		output->scale = option_scale;
+
+	weston_config_section_get_string(config_section,
+					 "transform", &t, "normal");
+	if (weston_parse_transform(t, &output->transform) < 0)
+		weston_log("Invalid transform \"%s\" for output %s\n",
+			   t, output->name);
+	free(t);
+
+}
+
+static int
+load_wayland_backend(struct weston_compositor *c, char const *backend,
+		     int *argc, char *argv[], struct weston_config *wc)
+{
+	struct weston_wayland_backend_config config = {{ 0, }};
+	struct weston_config_section *section;
+	struct weston_wayland_backend_output_config *oc;
+	int count, width, height, scale;
+	const char *section_name;
+	char *name;
+	int ret = 0;
+
+	const struct weston_option wayland_options[] = {
+		{ WESTON_OPTION_INTEGER, "width", 0, &width },
+		{ WESTON_OPTION_INTEGER, "height", 0, &height },
+		{ WESTON_OPTION_INTEGER, "scale", 0, &scale },
+		{ WESTON_OPTION_STRING, "display", 0, &config.display_name },
+		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &config.use_pixman },
+		{ WESTON_OPTION_INTEGER, "output-count", 0, &count },
+		{ WESTON_OPTION_BOOLEAN, "fullscreen", 0, &config.fullscreen },
+		{ WESTON_OPTION_BOOLEAN, "sprawl", 0, &config.sprawl },
+	};
+
+	width = 0;
+	height = 0;
+	scale = 0;
+	config.display_name = NULL;
+	config.use_pixman = 0;
+	count = 1;
+	config.fullscreen = 0;
+	config.sprawl = 0;
+	parse_options(wayland_options,
+		      ARRAY_LENGTH(wayland_options), argc, argv);
+
+	config.base.struct_version = WESTON_WAYLAND_BACKEND_CONFIG_VERSION;
+	config.base.struct_size = sizeof(struct weston_wayland_backend_config);
+	config.cursor_size = 32;
+	config.cursor_theme = NULL;
+
+	section = weston_config_get_section(wc, "shell", NULL, NULL);
+	weston_config_section_get_string(section, "cursor-theme",
+					 &config.cursor_theme, NULL);
+	weston_config_section_get_int(section, "cursor-size",
+				      &config.cursor_size, 32);
+
+	if (config.sprawl) {
+		/* do nothing, everything is already set */
+		ret = load_backend_new(c, backend, &config.base);
+		weston_wayland_backend_config_release(&config);
+		return ret;
+	}
+
+	if (config.fullscreen) {
+
+		oc = weston_wayland_backend_config_add_new_output(&config);
+
+		if (!oc)
+			goto err_outputs;
+
+		oc->width = width;
+		oc->height = height;
+		oc->name = NULL;
+		oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+		oc->scale = 1;
+
+		ret = load_backend_new(c, backend, &config.base);
+		weston_wayland_backend_config_release(&config);
+		return ret;
+	}
+
+	section = NULL;
+	while (weston_config_next_section(wc, &section, &section_name)) {
+		if (!section_name || strcmp(section_name, "output") != 0)
+			continue;
+		weston_config_section_get_string(section, "name", &name, NULL);
+		if (name == NULL)
+			continue;
+
+		if (name[0] != 'W' || name[1] != 'L') {
+			free(name);
+			continue;
+		}
+		free(name);
+
+		oc = weston_wayland_backend_config_add_new_output(&config);
+
+		if (!oc)
+			goto err_outputs;
+
+		weston_wayland_backend_output_init(oc, section, width,
+						height, scale);
+		--count;
+	}
+
+	if (!width)
+		width = 1024;
+	if (!height)
+		height = 640;
+	if (!scale)
+		scale = 1;
+	while (count > 0) {
+
+		oc = weston_wayland_backend_config_add_new_output(&config);
+
+		if (!oc)
+			goto err_outputs;
+
+		oc->width = width;
+		oc->height = height;
+		oc->name = NULL;
+		oc->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+		oc->scale = scale;
+
+		--count;
+	}
+
+	ret = load_backend_new(c, backend, &config.base);
+	weston_wayland_backend_config_release(&config);
+	return ret;
+
+err_outputs:
+	weston_wayland_backend_config_release(&config);
+	return ret;
+}
+
 static int
 load_backend(struct weston_compositor *compositor, const char *backend,
 	     int *argc, char **argv, struct weston_config *config)
@@ -775,11 +982,11 @@ load_backend(struct weston_compositor *compositor, const char *backend,
 		return load_headless_backend(compositor, backend, argc, argv, config);
 	else if (strstr(backend, "rdp-backend.so"))
 		return load_rdp_backend(compositor, backend, argc, argv, config);
+	else if (strstr(backend, "wayland-backend.so"))
+		return load_wayland_backend(compositor, backend, argc, argv, config);
 #if 0
 	else if (strstr(backend, "drm-backend.so"))
 		return load_drm_backend(compositor, backend, argc, argv, config);
-	else if (strstr(backend, "wayland-backend.so"))
-		return load_wayland_backend(compositor, backend, argc, argv, config);
 	else if (strstr(backend, "x11-backend.so"))
 		return load_x11_backend(compositor, backend, argc, argv, config);
 	else if (strstr(backend, "fbdev-backend.so"))
-- 
2.7.3



More information about the wayland-devel mailing list