From 1ae9d084770b5ff9257b3c6a114cde231720db13 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Thu, 2 Nov 2017 14:11:53 +0200 Subject: [PATCH] libweston: create/find output by name 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 Reviewed-by: Daniel Stone Acked-by: Derek Foreman --- libweston/compositor.c | 56 ++++++++++++++++++++++++++++++++++++++++-- libweston/compositor.h | 8 ++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/libweston/compositor.c b/libweston/compositor.c index d5bbcb22..4e15c2e8 100644 --- a/libweston/compositor.c +++ b/libweston/compositor.c @@ -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; diff --git a/libweston/compositor.h b/libweston/compositor.h index 71e57755..c6083c7c 100644 --- a/libweston/compositor.h +++ b/libweston/compositor.h @@ -2075,6 +2075,14 @@ void weston_compositor_add_heads_changed_listener(struct weston_compositor *compositor, 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);