libweston: allow attaching heads to enabled outputs

Move the responsibility of ensuring the head will work in the enabled
output to the backends.

A compositor cannot enable an output without heads, and removing the
last head from an output automatically disables the output, so attaching
a new head to an enabled output is only possible for clone mode.

Backends headless, rdp, and x11 forbid clone mode by not having an
attach_head hook implemented; fbdev and wayland explicitly deny clone
mode. Only the DRM backend is affected by this change and even that not
yet because MAX_CLONED_CONNECTORS is 1 in the DRM backend.

Also ensure a global is created for the head when attached to an enabled
output, and log it.

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>
diff --git a/libweston/compositor.c b/libweston/compositor.c
index c4410e6..14fa126 100644
--- a/libweston/compositor.c
+++ b/libweston/compositor.c
@@ -76,6 +76,9 @@
 static void
 weston_compositor_build_view_list(struct weston_compositor *compositor);
 
+static char *
+weston_output_create_heads_string(struct weston_output *output);
+
 /** Send wl_output events for mode and scale changes
  *
  * \param head Send on all resources bound to this head.
@@ -4387,6 +4390,14 @@
 		wl_output_send_done(resource);
 }
 
+static void
+weston_head_add_global(struct weston_head *head)
+{
+	head->global = wl_global_create(head->compositor->wl_display,
+					&wl_output_interface, 3,
+					head, bind_output);
+}
+
 /** Remove the global wl_output protocol object
  *
  * \param head The head whose global to remove.
@@ -4621,7 +4632,7 @@
 	return container_of(node, struct weston_head, output_link);
 }
 
-/** Attach a head to an inactive output
+/** Attach a head to an output
  *
  * \param output The output to attach to.
  * \param head The head that is not yet attached.
@@ -4636,7 +4647,7 @@
  *
  * On failure, the head remains unattached. Success of this function does not
  * guarantee the output configuration is actually valid. The final checks are
- * made on weston_output_enable().
+ * made on weston_output_enable() unless the output was already enabled.
  *
  * \memberof weston_output
  */
@@ -4644,8 +4655,7 @@
 weston_output_attach_head(struct weston_output *output,
 			  struct weston_head *head)
 {
-	if (output->enabled)
-		return -1;
+	char *head_names;
 
 	if (!wl_list_empty(&head->output_link))
 		return -1;
@@ -4661,6 +4671,15 @@
 	head->output = output;
 	wl_list_insert(output->head_list.prev, &head->output_link);
 
+	if (output->enabled) {
+		weston_head_add_global(head);
+
+		head_names = weston_output_create_heads_string(output);
+		weston_log("Output '%s' updated to have head(s) %s\n",
+			   output->name, head_names);
+		free(head_names);
+	}
+
 	return 0;
 }
 
@@ -5219,11 +5238,8 @@
 	wl_list_insert(compositor->output_list.prev, &output->link);
 	output->enabled = true;
 
-	wl_list_for_each(head, &output->head_list, output_link) {
-		head->global = wl_global_create(compositor->wl_display,
-						&wl_output_interface, 3,
-						head, bind_output);
-	}
+	wl_list_for_each(head, &output->head_list, output_link)
+		weston_head_add_global(head);
 
 	wl_signal_emit(&compositor->output_created_signal, output);