libweston: weston_output_init(..., +name)
authorPekka Paalanen <pekka.paalanen@collabora.co.uk>
Mon, 3 Apr 2017 10:18:13 +0000 (13:18 +0300)
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>
Wed, 4 Oct 2017 13:17:41 +0000 (16:17 +0300)
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@collabora.co.uk>
Reviewed-by: Ian Ray <ian.ray@ge.com>
Reviewed-by: David Fort <contact@hardening-consulting.com>
[Daniel: document name copying]
Acked-by Daniel Stone <daniels@collabora.com>

libweston/compositor-drm.c
libweston/compositor-fbdev.c
libweston/compositor-headless.c
libweston/compositor-rdp.c
libweston/compositor-wayland.c
libweston/compositor-x11.c
libweston/compositor.c
libweston/compositor.h

index e3fe7d5..b9014c8 100644 (file)
@@ -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) {
index 9d49f4b..fe169ca 100644 (file)
@@ -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;
index 5425ad5..ac762c1 100644 (file)
@@ -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;
index 2f9c978..f2fa0f4 100644 (file)
@@ -584,12 +584,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;
index 1fb1be6..dee972c 100644 (file)
@@ -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;
index 8cf512f..9d37142 100644 (file)
@@ -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;
index bed3cd8..165ff01 100644 (file)
@@ -4679,23 +4679,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 (the string is copied).
  *
  * 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;
 
index 769203a..804d091 100644 (file)
@@ -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,