[PATCH weston v0] refactor configuration API of wayland-backend

Benoit Gschwind gschwind at gnu-log.net
Sun Feb 21 11:52:05 UTC 2016


Hello,

Here is my proposal to update the configuration API of wayland-backend. I did
not tried to mimic the old configuration behaviors but instead I guessed it. If
someone can help me and describe the desired behaviors, I will be happy to
update the patch accordingly. Currently the behaviors is the following:

* if --fullscreen is set, only one output is created in fullscreen mode
  using --height and --width if available.
* else look for outputs in the configuration file, using --heigth and --width
  if invalid mode is provided or fall-back to default values.
* once outputs are depleted in config file and --output-count is provided,
  create new outputs to have at less output-count outputs. those output are
  built using --width and --height if available 

Best regards.

---
 Makefile.am              |   1 +
 src/compositor-wayland.c | 202 ++++++++++++-----------------------------------
 src/compositor-wayland.h |  68 ++++++++++++++++
 src/main.c               | 180 ++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 297 insertions(+), 154 deletions(-)
 create mode 100644 src/compositor-wayland.h

diff --git a/Makefile.am b/Makefile.am
index 623621d..8b7a66f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -72,6 +72,7 @@ weston_SOURCES =					\
 	src/log.c					\
 	src/compositor.c				\
 	src/compositor.h				\
+	src/compositor-wayland.h		\
 	src/input.c					\
 	src/data-device.c				\
 	src/screenshooter.c				\
diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c
index d1c020d..d6867ba 100644
--- a/src/compositor-wayland.c
+++ b/src/compositor-wayland.c
@@ -40,6 +40,7 @@
 #include <wayland-cursor.h>
 
 #include "compositor.h"
+#include "compositor-wayland.h"
 #include "gl-renderer.h"
 #include "pixman-renderer.h"
 #include "shared/helpers.h"
@@ -79,6 +80,8 @@ struct wayland_backend {
 	struct wl_cursor *cursor;
 
 	struct wl_list input_list;
+
+	struct weston_wayland_backend_config * config;
 };
 
 struct wayland_output {
@@ -192,6 +195,27 @@ struct wayland_input {
 struct gl_renderer_interface *gl_renderer;
 
 static void
+weston_wayland_backend_config_outputs_clear(
+		struct weston_wayland_backend_config * ths) {
+	struct weston_wayland_backend_output_config * pos;
+	struct weston_wayland_backend_output_config * tmp;
+	wl_list_for_each_safe(pos, tmp, &ths->outputs, link) {
+		wl_list_remove(&pos->link);
+		free(pos->name);
+		free(pos);
+	}
+}
+
+static void
+weston_wayland_backend_config_destroy(
+		struct weston_wayland_backend_config * ths) {
+	weston_wayland_backend_config_outputs_clear(ths);
+	free(ths->display_name);
+	free(ths->cursor_theme);
+	free(ths);
+}
+
+static void
 wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
 {
 	cairo_surface_destroy(buffer->c_surface);
@@ -1091,65 +1115,6 @@ err_name:
 }
 
 static struct wayland_output *
-wayland_output_create_for_config(struct wayland_backend *b,
-				 struct weston_config_section *config_section,
-				 int option_width, int option_height,
-				 int option_scale, int32_t x, int32_t y)
-{
-	struct wayland_output *output;
-	char *mode, *t, *name, *str;
-	int width, height, scale;
-	uint32_t transform;
-	unsigned int slen;
-
-	weston_config_section_get_string(config_section, "name", &name, NULL);
-	if (name) {
-		slen = strlen(name);
-		slen += strlen(WINDOW_TITLE " - ");
-		str = malloc(slen + 1);
-		if (str)
-			snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
-		free(name);
-		name = str;
-	}
-	if (!name)
-		name = strdup(WINDOW_TITLE);
-
-	weston_config_section_get_string(config_section,
-					 "mode", &mode, "1024x600");
-	if (sscanf(mode, "%dx%d", &width, &height) != 2) {
-		weston_log("Invalid mode \"%s\" for output %s\n",
-			   mode, name);
-		width = 1024;
-		height = 640;
-	}
-	free(mode);
-
-	if (option_width)
-		width = option_width;
-	if (option_height)
-		height = option_height;
-
-	weston_config_section_get_int(config_section, "scale", &scale, 1);
-
-	if (option_scale)
-		scale = option_scale;
-
-	weston_config_section_get_string(config_section,
-					 "transform", &t, "normal");
-	if (weston_parse_transform(t, &transform) < 0)
-		weston_log("Invalid transform \"%s\" for output %s\n",
-			   t, name);
-	free(t);
-
-	output = wayland_output_create(b, x, y, width, height, name, 0,
-				       transform, scale);
-	free(name);
-
-	return output;
-}
-
-static struct wayland_output *
 wayland_output_create_for_parent_output(struct wayland_backend *b,
 					struct wayland_parent_output *poutput)
 {
@@ -2155,25 +2120,18 @@ static const char *left_ptrs[] = {
 };
 
 static void
-create_cursor(struct wayland_backend *b, struct weston_config *config)
+create_cursor(struct wayland_backend *b,
+		struct weston_wayland_backend_config *config)
 {
-	struct weston_config_section *s;
-	int size;
-	char *theme = NULL;
 	unsigned int i;
 
-	s = weston_config_get_section(config, "shell", NULL, NULL);
-	weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
-	weston_config_section_get_int(s, "cursor-size", &size, 32);
-
-	b->cursor_theme = wl_cursor_theme_load(theme, size, b->parent.shm);
+	b->cursor_theme = wl_cursor_theme_load(config->cursor_theme,
+			config->cursor_size, b->parent.shm);
 	if (!b->cursor_theme) {
 		fprintf(stderr, "could not load cursor theme\n");
 		return;
 	}
 
-	free(theme);
-
 	b->cursor = NULL;
 	for (i = 0; !b->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
 		b->cursor = wl_cursor_theme_get_cursor(b->cursor_theme,
@@ -2207,9 +2165,8 @@ fullscreen_binding(struct weston_keyboard *keyboard, uint32_t time,
 }
 
 static struct wayland_backend *
-wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
-		       const char *display_name, int *argc, char *argv[],
-		       struct weston_config *config)
+wayland_backend_create(struct weston_compositor *compositor,
+		       struct weston_wayland_backend_config *config)
 {
 	struct wayland_backend *b;
 	struct wl_event_loop *loop;
@@ -2219,11 +2176,12 @@ wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
 	if (b == NULL)
 		return NULL;
 
+	b->config = config;
 	b->compositor = compositor;
 	if (weston_compositor_set_presentation_clock_software(compositor) < 0)
 		goto err_compositor;
 
-	b->parent.wl_display = wl_display_connect(display_name);
+	b->parent.wl_display = wl_display_connect(config->display_name);
 	if (b->parent.wl_display == NULL) {
 		weston_log("failed to create display: %m\n");
 		goto err_compositor;
@@ -2237,7 +2195,7 @@ wayland_backend_create(struct weston_compositor *compositor, int use_pixman,
 
 	create_cursor(b, config);
 
-	b->use_pixman = use_pixman;
+	b->use_pixman = config->use_pixman;
 
 	if (!b->use_pixman) {
 		gl_renderer = weston_load_module("gl-renderer.so",
@@ -2308,50 +2266,26 @@ wayland_backend_destroy(struct wayland_backend *b)
 	wl_cursor_theme_destroy(b->cursor_theme);
 
 	weston_compositor_shutdown(b->compositor);
+
+	weston_wayland_backend_config_destroy(b->config);
 	free(b);
 }
 
 WL_EXPORT int
-backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
-	     struct weston_config *config,
-	     struct weston_backend_config *config_base)
+wayland_backend_init(struct weston_compositor *compositor,
+	     struct weston_wayland_backend_config *config)
 {
 	struct wayland_backend *b;
 	struct wayland_output *output;
 	struct wayland_parent_output *poutput;
-	struct weston_config_section *section;
-	int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
-	const char *section_name, *display_name;
-	char *name;
+	struct weston_wayland_backend_output_config * pos;
+	int x;
 
-	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, &display_name },
-		{ WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
-		{ WESTON_OPTION_INTEGER, "output-count", 0, &count },
-		{ WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
-		{ WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
-	};
-
-	width = 0;
-	height = 0;
-	scale = 0;
-	display_name = NULL;
-	use_pixman = 0;
-	count = 1;
-	fullscreen = 0;
-	sprawl = 0;
-	parse_options(wayland_options,
-		      ARRAY_LENGTH(wayland_options), argc, argv);
-
-	b = wayland_backend_create(compositor, use_pixman, display_name,
-				   argc, argv, config);
+	b = wayland_backend_create(compositor, config);
 	if (!b)
 		return -1;
 
-	if (sprawl || b->parent.fshell) {
+	if (config->sprawl || b->parent.fshell) {
 		b->sprawl_across_outputs = 1;
 		wl_display_roundtrip(b->parent.wl_display);
 
@@ -2361,58 +2295,24 @@ backend_init(struct weston_compositor *compositor, int *argc, char *argv[],
 		return 0;
 	}
 
-	if (fullscreen) {
-		output = wayland_output_create(b, 0, 0, width, height,
-					       NULL, 1, 0, 1);
-		if (!output)
-			goto err_outputs;
-
-		wayland_output_set_fullscreen(output, 0, 0, NULL);
-		return 0;
-	}
-
-	section = NULL;
 	x = 0;
-	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)
+	wl_list_for_each(pos, &config->outputs, link) {
+		/* should not append */
+		if (pos->name == NULL)
 			continue;
 
-		if (name[0] != 'W' || name[1] != 'L') {
-			free(name);
-			continue;
-		}
-		free(name);
+		output = wayland_output_create(b, x, 0, pos->width, pos->height,
+				pos->name, pos->fullscreen, pos->transform, pos->scale);
 
-		output = wayland_output_create_for_config(b, section, width,
-							  height, scale, x, 0);
 		if (!output)
 			goto err_outputs;
-		if (wayland_output_set_windowed(output))
-			goto err_outputs;
-
-		x += output->base.width;
-		--count;
-	}
 
-	if (!width)
-		width = 1024;
-	if (!height)
-		height = 640;
-	if (!scale)
-		scale = 1;
-	while (count > 0) {
-		output = wayland_output_create(b, x, 0, width, height,
-					       NULL, 0, 0, scale);
-		if (!output)
-			goto err_outputs;
-		if (wayland_output_set_windowed(output))
-			goto err_outputs;
+		if (!pos->fullscreen) {
+			if (wayland_output_set_windowed(output))
+				goto err_outputs;
+		}
 
-		x += width;
-		--count;
+		x += pos->width;
 	}
 
 	weston_compositor_add_key_binding(compositor, KEY_F,
diff --git a/src/compositor-wayland.h b/src/compositor-wayland.h
new file mode 100644
index 0000000..594b1fa
--- /dev/null
+++ b/src/compositor-wayland.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright © 2016 Benoit Gschwind
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#ifndef SRC_COMPOSITOR_WAYLAND_H_
+#define SRC_COMPOSITOR_WAYLAND_H_
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+#include "compositor.h"
+
+struct weston_wayland_backend_output_config {
+	struct wl_list link; /* link defined output from config file */
+	char *name;
+	int fullscreen;
+	int width, height;
+	uint32_t transform;
+	int32_t scale;
+};
+
+struct weston_wayland_backend_config {
+	int use_pixman;
+	int sprawl;
+	char *display_name;
+
+	char *cursor_theme;
+	int cursor_size;
+
+	/* list wished outputs */
+	struct wl_list outputs;
+};
+
+typedef int (*wayland_backend_init_func)(
+		struct weston_compositor *c,
+	    struct weston_wayland_backend_config *config);
+
+WL_EXPORT int
+wayland_backend_init(struct weston_compositor *compositor,
+	     struct weston_wayland_backend_config *config);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* SRC_COMPOSITOR_WAYLAND_H_ */
diff --git a/src/main.c b/src/main.c
index 1850fa6..654541f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -42,6 +42,7 @@
 #endif
 
 #include "compositor.h"
+#include "compositor-wayland.h"
 #include "../shared/os-compatibility.h"
 #include "../shared/helpers.h"
 #include "git-version.h"
@@ -653,15 +654,188 @@ load_backend_old(struct weston_compositor *compositor, const char *backend,
 	return backend_init(compositor, argc, argv, wc, NULL);
 }
 
+/** Create a new default drm backend configuration */
+static struct weston_wayland_backend_config *
+weston_wayland_backend_config_create(void) {
+	struct weston_wayland_backend_config * ret =
+			(__typeof__(ret))zalloc(sizeof *ret);
+
+	ret->cursor_size = 32;
+	wl_list_init(&ret->outputs);
+	return ret;
+
+}
+
+static void
+weston_wayland_backend_config_add_output(
+		struct weston_wayland_backend_config * ths,
+		int fullscreen,
+		int width, int height,
+		char const *name, uint32_t transform, int32_t scale) {
+
+	struct weston_wayland_backend_output_config * output =
+			(__typeof__(output))malloc(sizeof *output);
+
+	wl_list_init(&output->link);
+	output->fullscreen = fullscreen;
+	output->width = width;
+	output->height = height;
+	output->name = strdup(name);
+	output->transform = transform;
+	output->scale = scale;
+
+	wl_list_insert(ths->outputs.prev, &output->link);
+
+}
+
+static int
+weston_wayland_backend_load(
+		struct weston_compositor *compositor,
+		struct weston_wayland_backend_config *config) {
+
+	wayland_backend_init_func backend_init;
+
+	backend_init = weston_load_module("wayland-backend.so", "wayland_backend_init");
+	if (!backend_init)
+		return -1;
+
+	return backend_init(compositor, config);
+}
+
+static int
+load_wayland_backend(struct weston_compositor *c, char const * backend,
+		 int *argc, char **argv, struct weston_config *wc)
+{
+	struct weston_config_section *section;
+	int count, option_width, option_height, option_scale, fullscreen;
+	const char *section_name;
+	char *name;
+	int ret = 0;
+
+	struct weston_wayland_backend_config *config =
+			weston_wayland_backend_config_create();
+	if (!config)
+		return -1;
+
+	const struct weston_option wayland_options[] = {
+		{ WESTON_OPTION_INTEGER, "width", 0, &option_width },
+		{ WESTON_OPTION_INTEGER, "height", 0, &option_height },
+		{ WESTON_OPTION_INTEGER, "scale", 0, &option_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, &fullscreen },
+		{ WESTON_OPTION_BOOLEAN, "sprawl", 0, &config->sprawl },
+	};
+
+	option_width = 0;
+	option_height = 0;
+	option_scale = 0;
+	count = 1;
+	fullscreen = 0;
+	parse_options(wayland_options,
+		      ARRAY_LENGTH(wayland_options), argc, argv);
+
+	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 (!fullscreen) {
+		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;
+			}
+
+			/* setup the width, the height, the scale and the transform of
+			 * this output. */
+			int width, height, scale;
+			uint32_t transform = WL_OUTPUT_TRANSFORM_NORMAL;
+
+			width = option_width ? option_width : 1024;
+			height = option_height ? option_height : 640;
+
+			char *mode;
+			weston_config_section_get_string(section,
+							 "mode", &mode, NULL);
+			if(!mode) {
+				weston_log("Undefined mode for output %s\n", name);
+			} else {
+				int xwidth, xheight;
+				if (sscanf(mode, "%dx%d", &xwidth, &xheight) != 2) {
+					weston_log("Invalid mode \"%s\" for output %s\n",
+						   mode, name);
+				} else {
+					width = xwidth;
+					height = xheight;
+				}
+				free(mode);
+			}
+
+			scale = option_scale ? option_scale : 1;
+			weston_config_section_get_int(section, "scale", &scale, scale);
+
+			char *t;
+			weston_config_section_get_string(section,
+							 "transform", &t, "normal");
+			if (weston_parse_transform(t, &transform) < 0)
+				weston_log("Invalid transform \"%s\" for output %s\n",
+					   t, name);
+			free(t);
+
+			weston_wayland_backend_config_add_output(config, 0, width, height,
+					name, transform, scale);
+
+			free(name);
+
+			--count;
+		}
+
+	} else {
+		count = 1;
+	}
+
+	/* fill remaining outputs */
+	if (!option_width)
+		option_width = 1024;
+	if (!option_height)
+		option_height = 640;
+	if (!option_scale)
+		option_scale = 1;
+	while (count > 0) {
+		char name[32];
+		snprintf(name, 32, "screen%d", count);
+		weston_wayland_backend_config_add_output(config, fullscreen, option_width,
+				option_height, name, WL_OUTPUT_TRANSFORM_NORMAL,
+				option_scale);
+		--count;
+	}
+
+	/* load the actual wayland backend and configure it */
+	if (weston_wayland_backend_load(c, config) < 0) {
+		ret = -1;
+	}
+
+	return ret;
+
+}
+
 static int
 load_backend(struct weston_compositor *compositor, const char *backend,
 	     int *argc, char **argv, struct weston_config *config)
 {
+	if (strstr(backend, "wayland-backend.so"))
+		return load_wayland_backend(compositor, backend, argc, argv, config);
 #if 0
-	if (strstr(backend, "drm-backend.so"))
+	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.4.10



More information about the wayland-devel mailing list