[PATCH weston 05/21] libweston: weston_output_init(..., +name)

Pekka Paalanen ppaalanen at gmail.com
Fri Sep 22 14:31:13 UTC 2017


From: Pekka Paalanen <pekka.paalanen at collabora.co.uk>

Add 'name' argument to weston_output_init(). This is much more obvious
than the assert inside weston_output_init() to ensure the caller has set
a field in weston_output first.

Now weston_output_init() will strdup() the name itself, which means we
can drop a whole bunch of strdup()s in the backends. This matches
weston_output_destroy() which was already calling free() on the name.

All backends are slightly reordered to call weston_output_init() before
accessing any fields of weston_output, except the Wayland backend which
would make it a little awkward to do it in this patch. Mind, that
weston_output_init() still does not reset the struct to zero - it is
presumed the caller has done it, since weston_output is embedded in the
backend output structs.

Signed-off-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
---
 libweston/compositor-drm.c      |  8 +++++---
 libweston/compositor-fbdev.c    |  5 ++---
 libweston/compositor-headless.c |  4 ++--
 libweston/compositor-rdp.c      |  4 ++--
 libweston/compositor-wayland.c  |  7 +++----
 libweston/compositor-x11.c      |  4 ++--
 libweston/compositor.c          | 12 +++++++-----
 libweston/compositor.h          |  3 ++-
 8 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index 885b5f0f..36e2015e 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -3327,6 +3327,7 @@ create_output_for_connector(struct drm_backend *b,
 	struct drm_output *output;
 	drmModeObjectPropertiesPtr props;
 	struct drm_mode *drm_mode;
+	char *name;
 	int i;
 
 	static const struct drm_property_info connector_props[] = {
@@ -3354,10 +3355,13 @@ create_output_for_connector(struct drm_backend *b,
 
 	output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
 
+	name = make_connector_name(connector);
+	weston_output_init(&output->base, b->compositor, name);
+	free(name);
+
 	output->base.enable = drm_output_enable;
 	output->base.destroy = drm_output_destroy;
 	output->base.disable = drm_output_disable;
-	output->base.name = make_connector_name(connector);
 
 	output->destroy_pending = 0;
 	output->disable_pending = 0;
@@ -3373,8 +3377,6 @@ create_output_for_connector(struct drm_backend *b,
 	find_and_parse_output_edid(b, output, props);
 	drmModeFreeObjectProperties(props);
 
-	weston_output_init(&output->base, b->compositor);
-
 	for (i = 0; i < output->connector->count_modes; i++) {
 		drm_mode = drm_output_add_mode(output, &output->connector->modes[i]);
 		if (!drm_mode) {
diff --git a/libweston/compositor-fbdev.c b/libweston/compositor-fbdev.c
index b3ce0588..33fa61d9 100644
--- a/libweston/compositor-fbdev.c
+++ b/libweston/compositor-fbdev.c
@@ -510,13 +510,12 @@ fbdev_output_create(struct fbdev_backend *backend,
 		goto out_free;
 	}
 
-	output->base.name = strdup("fbdev");
+	weston_output_init(&output->base, backend->compositor, "fbdev");
+
 	output->base.destroy = fbdev_output_destroy;
 	output->base.disable = fbdev_output_disable_handler;
 	output->base.enable = fbdev_output_enable;
 
-	weston_output_init(&output->base, backend->compositor);
-
 	/* only one static mode in list */
 	output->mode.flags =
 		WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
diff --git a/libweston/compositor-headless.c b/libweston/compositor-headless.c
index 5425ad5b..ac762c18 100644
--- a/libweston/compositor-headless.c
+++ b/libweston/compositor-headless.c
@@ -234,12 +234,12 @@ headless_output_create(struct weston_compositor *compositor,
 	if (output == NULL)
 		return -1;
 
-	output->base.name = strdup(name);
+	weston_output_init(&output->base, compositor, name);
+
 	output->base.destroy = headless_output_destroy;
 	output->base.disable = headless_output_disable;
 	output->base.enable = headless_output_enable;
 
-	weston_output_init(&output->base, compositor);
 	weston_compositor_add_pending_output(&output->base, compositor);
 
 	return 0;
diff --git a/libweston/compositor-rdp.c b/libweston/compositor-rdp.c
index 6886038b..ca560b3c 100644
--- a/libweston/compositor-rdp.c
+++ b/libweston/compositor-rdp.c
@@ -580,12 +580,12 @@ rdp_backend_create_output(struct weston_compositor *compositor)
 	if (output == NULL)
 		return -1;
 
-	output->base.name =  strdup("rdp");
+	weston_output_init(&output->base, compositor, "rdp");
+
 	output->base.destroy = rdp_output_destroy;
 	output->base.disable = rdp_output_disable;
 	output->base.enable = rdp_output_enable;
 
-	weston_output_init(&output->base, compositor);
 	weston_compositor_add_pending_output(&output->base, compositor);
 
 	return 0;
diff --git a/libweston/compositor-wayland.c b/libweston/compositor-wayland.c
index 1fb1be6c..dee972c8 100644
--- a/libweston/compositor-wayland.c
+++ b/libweston/compositor-wayland.c
@@ -1265,7 +1265,6 @@ wayland_output_create_common(const char *name)
 	output->base.destroy = wayland_output_destroy;
 	output->base.disable = wayland_output_disable;
 	output->base.enable = wayland_output_enable;
-	output->base.name = strdup(name);
 
 	return output;
 }
@@ -1278,7 +1277,7 @@ wayland_output_create(struct weston_compositor *compositor, const char *name)
 	if (!output)
 		return -1;
 
-	weston_output_init(&output->base, compositor);
+	weston_output_init(&output->base, compositor, name);
 	weston_compositor_add_pending_output(&output->base, compositor);
 
 	return 0;
@@ -1354,7 +1353,7 @@ wayland_output_create_for_parent_output(struct wayland_backend *b,
 		goto out;
 	}
 
-	weston_output_init(&output->base, b->compositor);
+	weston_output_init(&output->base, b->compositor, "wlparent");
 
 	output->base.scale = 1;
 	output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
@@ -1393,7 +1392,7 @@ wayland_output_create_fullscreen(struct wayland_backend *b)
 	if (!output)
 		return -1;
 
-	weston_output_init(&output->base, b->compositor);
+	weston_output_init(&output->base, b->compositor, "wayland-fullscreen");
 
 	output->base.scale = 1;
 	output->base.transform = WL_OUTPUT_TRANSFORM_NORMAL;
diff --git a/libweston/compositor-x11.c b/libweston/compositor-x11.c
index 8cf512fe..9d371428 100644
--- a/libweston/compositor-x11.c
+++ b/libweston/compositor-x11.c
@@ -1028,12 +1028,12 @@ x11_output_create(struct weston_compositor *compositor,
 		return -1;
 	}
 
-	output->base.name = strdup(name);
+	weston_output_init(&output->base, compositor, name);
+
 	output->base.destroy = x11_output_destroy;
 	output->base.disable = x11_output_disable;
 	output->base.enable = x11_output_enable;
 
-	weston_output_init(&output->base, compositor);
 	weston_compositor_add_pending_output(&output->base, compositor);
 
 	return 0;
diff --git a/libweston/compositor.c b/libweston/compositor.c
index ae839b87..35bf3eaa 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -4680,23 +4680,25 @@ weston_output_set_transform(struct weston_output *output,
  *
  * \param output     The weston_output object to initialize
  * \param compositor The compositor instance.
+ * \param name       Name for the output.
  *
  * Sets initial values for fields that are expected to be
  * configured either by compositors or backends.
  *
+ * The name is used in logs, and can be used by compositors as a configuration
+ * identifier.
+ *
  * \memberof weston_output
  * \internal
  */
 WL_EXPORT void
 weston_output_init(struct weston_output *output,
-		   struct weston_compositor *compositor)
+		   struct weston_compositor *compositor,
+		   const char *name)
 {
 	output->compositor = compositor;
 	output->destroying = 0;
-
-	/* Backends must set output->name */
-	assert(output->name);
-
+	output->name = strdup(name);
 	wl_list_init(&output->link);
 	output->enabled = false;
 
diff --git a/libweston/compositor.h b/libweston/compositor.h
index 769203a1..804d0912 100644
--- a/libweston/compositor.h
+++ b/libweston/compositor.h
@@ -1926,7 +1926,8 @@ weston_output_set_transform(struct weston_output *output,
 
 void
 weston_output_init(struct weston_output *output,
-		   struct weston_compositor *compositor);
+		   struct weston_compositor *compositor,
+		   const char *name);
 
 void
 weston_compositor_add_pending_output(struct weston_output *output,
-- 
2.13.5



More information about the wayland-devel mailing list