libweston: create/find output by name
authorPekka Paalanen <pekka.paalanen@collabora.co.uk>
Thu, 2 Nov 2017 12:11:53 +0000 (14:11 +0200)
committerPekka Paalanen <pekka.paalanen@collabora.co.uk>
Tue, 17 Apr 2018 12:57:41 +0000 (15:57 +0300)
To let users pick an arbitrary name for an output, to be used as e.g. a
configuration key, add API to create an output with a given name. For
the same configuration purpose, add a search function as well.

For the search function to be predictable, forbid creating multiple
outputs with the same name. Previously, creating multiple outputs with
the same name would have needed detatching to create outputs from the
same head, now that is forbidden.

Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Reviewed-by: Daniel Stone <daniels@collabora.com>
Acked-by: Derek Foreman <derekf@osg.samsung.com>
libweston/compositor.c
libweston/compositor.h

index d5bbcb2..4e15c2e 100644 (file)
@@ -5720,6 +5720,59 @@ weston_output_release(struct weston_output *output)
        free(output->name);
 }
 
+/** Find an output by its given name
+ *
+ * \param compositor The compositor to search in.
+ * \param name The output name to search for.
+ * \return An existing output with the given name, or NULL if not found.
+ *
+ * \memberof weston_compositor
+ */
+WL_EXPORT struct weston_output *
+weston_compositor_find_output_by_name(struct weston_compositor *compositor,
+                                     const char *name)
+{
+       struct weston_output *output;
+
+       wl_list_for_each(output, &compositor->output_list, link)
+               if (strcmp(output->name, name) == 0)
+                       return output;
+
+       wl_list_for_each(output, &compositor->pending_output_list, link)
+               if (strcmp(output->name, name) == 0)
+                       return output;
+
+       return NULL;
+}
+
+/** Create a named output
+ *
+ * \param compositor The compositor.
+ * \param name The name for the output.
+ * \return A new \c weston_output, or NULL on failure.
+ *
+ * This creates a new weston_output that starts with no heads attached.
+ *
+ * An output must be configured and it must have at least one head before
+ * it can be enabled.
+ *
+ * \memberof weston_compositor
+ */
+WL_EXPORT struct weston_output *
+weston_compositor_create_output(struct weston_compositor *compositor,
+                               const char *name)
+{
+       assert(compositor->backend->create_output);
+
+       if (weston_compositor_find_output_by_name(compositor, name)) {
+               weston_log("Warning: attempted to create an output with a "
+                          "duplicate name '%s'.\n", name);
+               return NULL;
+       }
+
+       return compositor->backend->create_output(compositor, name);
+}
+
 /** Create an output for an unused head
  *
  * \param compositor The compositor.
@@ -5740,8 +5793,7 @@ weston_compositor_create_output_with_head(struct weston_compositor *compositor,
 {
        struct weston_output *output;
 
-       assert(compositor->backend->create_output);
-       output = compositor->backend->create_output(compositor, head->name);
+       output = weston_compositor_create_output(compositor, head->name);
        if (!output)
                return NULL;
 
index 71e5775..c6083c7 100644 (file)
@@ -2076,6 +2076,14 @@ weston_compositor_add_heads_changed_listener(struct weston_compositor *composito
                                             struct wl_listener *listener);
 
 struct weston_output *
+weston_compositor_find_output_by_name(struct weston_compositor *compositor,
+                                     const char *name);
+
+struct weston_output *
+weston_compositor_create_output(struct weston_compositor *compositor,
+                               const char *name);
+
+struct weston_output *
 weston_compositor_create_output_with_head(struct weston_compositor *compositor,
                                          struct weston_head *head);