From 29931e73036b07c6a6d72a10c943659041e829f8 Mon Sep 17 00:00:00 2001
From: Richard Hughes <richard@hughsie.com>
Date: Thu, 4 Apr 2013 16:27:24 +0100
Subject: [PATCH 2/2] Add a colord implementation of a CMS plugin for weston

This allows users to change the assigned display profile in GNOME (using
gnome-control-center) or KDE (using colord-kde) and also allows the profiling
tools to correctly inhibit the calibration state whilst measuring the native
screen response.
---
 configure.ac    |  10 ++
 src/Makefile.am |   9 ++
 src/colord.c    | 280 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 299 insertions(+)
 create mode 100644 src/colord.c

diff --git a/configure.ac b/configure.ac
index 16ed8c9..77372d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -226,6 +226,16 @@ AC_ARG_ENABLE(tablet-shell,
 AM_CONDITIONAL(ENABLE_TABLET_SHELL,
 	       test "x$enable_tablet_shell" = "xyes")
 
+# CMS modules
+AC_ARG_ENABLE(colord,
+              AS_HELP_STRING([--disable-colord],
+                             [do not build colord CMS support]),,
+	      enable_colord=yes)
+AM_CONDITIONAL(ENABLE_COLORD,
+	       test "x$enable_colord" = "xyes")
+if test x$enable_colord = xyes; then
+  PKG_CHECK_MODULES(COLORD, colord >= 0.1.8)
+fi
 
 AC_ARG_ENABLE(wcap-tools, [  --disable-wcap-tools],, enable_wcap_tools=yes)
 AM_CONDITIONAL(BUILD_WCAP_TOOLS, test x$enable_wcap_tools = xyes)
diff --git a/src/Makefile.am b/src/Makefile.am
index 02d6183..e252378 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -86,6 +86,7 @@ moduledir = $(libdir)/weston
 module_LTLIBRARIES =				\
 	$(desktop_shell)			\
 	$(tablet_shell)				\
+	$(cms_colord)				\
 	$(x11_backend)				\
 	$(drm_backend)				\
 	$(wayland_backend)
@@ -184,6 +185,14 @@ tablet_shell_la_SOURCES =			\
 	tablet-shell-server-protocol.h
 endif
 
+if ENABLE_COLORD
+cms_colord = colord.la
+colord_la_LDFLAGS = -module -avoid-version
+colord_la_LIBADD = $(COMPOSITOR_LIBS) $(COLORD_LIBS)
+colord_la_CFLAGS = $(GCC_CFLAGS) $(COMPOSITOR_CFLAGS) $(COLORD_CFLAGS)
+colord_la_SOURCES = colord.c
+endif
+
 BUILT_SOURCES =					\
 	screenshooter-server-protocol.h		\
 	screenshooter-protocol.c		\
diff --git a/src/colord.c b/src/colord.c
new file mode 100644
index 0000000..c1079d2
--- /dev/null
+++ b/src/colord.c
@@ -0,0 +1,280 @@
+/*
+ * Copyright © 2013 Richard Hughes
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of the copyright holders not be used in
+ * advertising or publicity pertaining to distribution of the software
+ * without specific, written prior permission.  The copyright holders make
+ * no representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <colord.h>
+
+#include "compositor.h"
+#include "cms.h"
+
+struct colord_color_manager {
+	struct weston_color_manager	 base;
+	CdClient			*client;
+	GHashTable			*devices;
+	GMainLoop			*loop;
+	GThread				*thread;
+};
+
+static gchar *
+get_output_id (struct weston_output *o)
+{
+	gchar *device_id;
+
+	//FIXME: use the EDID / DRM name if available?
+	device_id = g_strdup_printf("weston-drm-%i", o->id);
+	return device_id;
+}
+
+static struct weston_color_profile *
+colord_create_color_profile(CdProfile *profile, GError **error)
+{
+	gboolean ret;
+	struct weston_color_profile *p = NULL;
+
+	ret = cd_profile_connect_sync(profile, NULL, error);
+	if (ret)
+		weston_cms_load_profile(cd_profile_get_filename(profile), &p);
+	return p;
+}
+
+static void
+colord_update_output_from_device (struct weston_output *o, CdDevice *device)
+{
+	CdProfile *profile;
+	gboolean ret;
+	GError *error = NULL;
+	struct weston_color_profile *p = NULL;
+
+	ret = cd_device_connect_sync(device, NULL, &error);
+	if (!ret) {
+		weston_log("colord: failed to connect to device %s: %s\n",
+			   cd_device_get_object_path (device),
+			   error->message);
+		g_error_free(error);
+		goto out;
+	}
+	profile = cd_device_get_default_profile(device);
+	if (!profile) {
+		weston_log("colord: no assigned color profile for %s\n",
+			   cd_device_get_id (device));
+		goto out;
+	}
+	p = colord_create_color_profile(profile, &error);
+	if (p == NULL) {
+		weston_log("colord: warning failed to convert profile %s: %s\n",
+			   cd_profile_get_object_path (profile),
+			   error->message);
+		g_error_free(error);
+		goto out;
+	}
+out:
+	weston_cms_set_color_profile(o, p);
+}
+
+static void
+colord_device_changed_cb (CdDevice *device, struct weston_output *o)
+{
+	colord_update_output_from_device(o, device);
+}
+
+static inline struct colord_color_manager *
+to_colord_cm(struct weston_color_manager *base)
+{
+	return container_of(base, struct colord_color_manager, base);
+}
+
+static int
+colord_output_added (struct weston_output *o,
+		     enum weston_color_manager_flags flags)
+{
+	CdDevice *device;
+	gchar *device_id;
+	GError *error = NULL;
+	GHashTable *device_props;
+	gint rc = 0;
+	struct colord_color_manager *colord_cm = to_colord_cm(o->compositor->cms);
+
+	/* create device */
+	device_id = get_output_id(o);
+	weston_log("colord: output added %s\n", device_id);
+	device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
+					     g_free, g_free);
+	g_hash_table_insert (device_props,
+			     g_strdup(CD_DEVICE_PROPERTY_KIND),
+			     g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
+	g_hash_table_insert (device_props,
+			     g_strdup(CD_DEVICE_PROPERTY_FORMAT),
+			     g_strdup("ColorModel.OutputMode.OutputResolution"));
+	g_hash_table_insert (device_props,
+			     g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
+			     g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
+	if ((flags & WESTON_COLOR_MANAGER_FLAG_OUTPUT_INTERNAL) > 0) {
+		g_hash_table_insert (device_props,
+				     g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
+				     NULL);
+	}
+
+	/* FIXME: add EDID details here too */
+	device = cd_client_create_device_sync(colord_cm->client,
+					      device_id,
+					      CD_OBJECT_SCOPE_TEMP,
+					      device_props,
+					      NULL,
+					      &error);
+	if (!device) {
+		rc = -1;
+		weston_log("colord: failed to create device: %s", error->message);
+		g_error_free(error);
+		goto out;
+	}
+
+	/* add to local cache */
+	g_hash_table_insert (colord_cm->devices, g_strdup(device_id), g_object_ref(device));
+	g_signal_connect (device, "changed", G_CALLBACK (colord_device_changed_cb), o);
+
+	/* get profiles */
+	colord_update_output_from_device (o, device);
+out:
+	g_hash_table_unref (device_props);
+	if (device)
+		g_object_unref (device);
+	g_free (device_id);
+	return rc;
+}
+
+static int
+colord_output_removed (struct weston_output *o,
+		       enum weston_color_manager_flags flags)
+{
+	CdDevice *device;
+	gboolean ret;
+	gchar *device_id;
+	GError *error = NULL;
+	gint rc = 0;
+	struct colord_color_manager *colord_cm = to_colord_cm(o->compositor->cms);
+
+	device_id = get_output_id(o);
+	weston_log("colord: output removed %s\n", device_id);
+	device = g_hash_table_lookup(colord_cm->devices, device_id);
+	if (!device) {
+		rc = -1;
+		weston_log("colord: failed to find device\n");
+		goto out;
+	}
+	g_signal_handlers_disconnect_by_data(device, o);
+	ret = cd_client_delete_device_sync (colord_cm->client,
+					    device,
+					    NULL,
+					    &error);
+
+	if (!ret) {
+		rc = -1;
+		weston_log("colord: failed to delete device: %s", error->message);
+		g_error_free(error);
+		goto out;
+	}
+out:
+	g_hash_table_remove (colord_cm->devices, device_id);
+	g_free (device_id);
+	return rc;
+}
+
+static void
+colord_module_destroy(struct weston_compositor *compositor)
+{
+	struct colord_color_manager *colord_cm;
+	if (!compositor->cms)
+		return;
+	colord_cm = to_colord_cm(compositor->cms);
+
+	/* close down the thread */
+	g_main_loop_quit(colord_cm->loop);
+	g_thread_join(colord_cm->thread);
+
+	g_hash_table_unref(colord_cm->devices);
+	g_object_unref(colord_cm->client);
+	g_main_loop_unref(colord_cm->loop);
+	free (colord_cm);
+
+	/* mark as unused */
+	compositor->cms = NULL;
+}
+
+static gpointer
+colord_run_loop_thread(gpointer data)
+{
+	struct colord_color_manager *colord_cm = (struct colord_color_manager *) data;
+	g_main_loop_run(colord_cm->loop);
+	return NULL;
+}
+
+WL_EXPORT int
+module_init(struct weston_compositor *compositor)
+{
+	gboolean ret;
+	GError *error = NULL;
+	struct colord_color_manager *colord_cm;
+	struct weston_output *output;
+
+	weston_log("colord: initialized\n");
+
+	/* create local state object */
+	colord_cm = malloc(sizeof *colord_cm);
+	if (colord_cm == NULL)
+		return -1;
+	memset(colord_cm, 0, sizeof *colord_cm);
+#if !GLIB_CHECK_VERSION(2,36,0)
+	g_type_init();
+#endif
+	colord_cm->client = cd_client_new();
+	ret = cd_client_connect_sync(colord_cm->client, NULL, &error);
+	if (!ret) {
+		weston_log("colord: failed to contact daemon: %s", error->message);
+		g_error_free(error);
+		free(colord_cm);
+		return -1;
+	}
+	colord_cm->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
+						   g_free, (GDestroyNotify) g_object_unref);
+	colord_cm->base.destroy = colord_module_destroy;
+	colord_cm->base.output_added = colord_output_added;
+	colord_cm->base.output_removed = colord_output_removed;
+	compositor->cms = &colord_cm->base;
+
+	/* coldplug outputs */
+	wl_list_for_each(output, &compositor->output_list, link)
+		colord_output_added(output, 0);
+
+	/* setup a thread for the GLib callbacks */
+	colord_cm->loop = g_main_loop_new(NULL, TRUE);
+	colord_cm->thread = g_thread_new("colord CMS main loop",
+					 colord_run_loop_thread,
+					 colord_cm);
+
+	return 0;
+}
-- 
1.8.2

