2 * Copyright © 2013 Richard Hughes
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include "compositor.h"
38 #include "cms-helper.h"
39 #include "shared/helpers.h"
42 struct weston_compositor *ec;
44 GHashTable *devices; /* key = device-id, value = cms_output */
45 GHashTable *pnp_ids; /* key = pnp-id, value = vendor */
51 struct wl_event_source *source;
54 struct wl_listener destroy_listener;
55 struct wl_listener output_created_listener;
60 guint32 backlight_value;
61 struct cms_colord *cms;
62 struct weston_color_profile *p;
63 struct weston_output *o;
64 struct wl_listener destroy_listener;
68 colord_idle_find_output_cb(gconstpointer a, gconstpointer b)
70 struct cms_output *ocms = (struct cms_output *) a;
71 struct weston_output *o = (struct weston_output *) b;
72 return ocms->o == o ? 0 : -1;
76 colord_idle_cancel_for_output(struct cms_colord *cms, struct weston_output *o)
80 /* cancel and remove any helpers that match the output */
81 g_mutex_lock(&cms->pending_mutex);
82 l = g_list_find_custom (cms->pending, o, colord_idle_find_output_cb);
84 struct cms_output *ocms = l->data;
85 cms->pending = g_list_remove (cms->pending, ocms);
87 g_mutex_unlock(&cms->pending_mutex);
91 edid_value_valid(const char *str)
97 if (strcmp(str, "unknown") == 0)
103 get_output_id(struct cms_colord *cms, struct weston_output *o)
105 struct weston_head *head;
109 /* XXX: What to do with multiple heads?
110 * This is potentially unstable, if head configuration is changed
111 * while the output is enabled. */
112 head = weston_output_get_first_head(o);
114 if (wl_list_length(&o->head_list) > 1) {
115 weston_log("colord: WARNING: multiple heads are not supported (output %s).\n",
119 /* see https://github.com/hughsie/colord/blob/master/doc/device-and-profile-naming-spec.txt
120 * for format and allowed values */
121 device_id = g_string_new("xrandr");
122 if (edid_value_valid(head->make)) {
123 tmp = g_hash_table_lookup(cms->pnp_ids, head->make);
126 g_string_append_printf(device_id, "-%s", tmp);
128 if (edid_value_valid(head->model))
129 g_string_append_printf(device_id, "-%s", head->model);
130 if (edid_value_valid(head->serial_number))
131 g_string_append_printf(device_id, "-%s", head->serial_number);
133 /* no EDID data, so use fallback */
134 if (strcmp(device_id->str, "xrandr") == 0)
135 g_string_append_printf(device_id, "-drm-%i", o->id);
137 return g_string_free(device_id, FALSE);
141 update_device_with_profile_in_idle(struct cms_output *ocms)
143 gboolean signal_write = FALSE;
145 struct cms_colord *cms = ocms->cms;
147 colord_idle_cancel_for_output(cms, ocms->o);
148 g_mutex_lock(&cms->pending_mutex);
149 if (cms->pending == NULL)
151 cms->pending = g_list_prepend(cms->pending, ocms);
152 g_mutex_unlock(&cms->pending_mutex);
154 /* signal we've got updates to do */
157 rc = write(cms->writefd, &tmp, 1);
159 weston_log("colord: failed to write to pending fd\n");
164 colord_update_output_from_device (struct cms_output *ocms)
169 GError *error = NULL;
172 /* old profile is no longer valid */
173 weston_cms_destroy_profile(ocms->p);
176 ret = cd_device_connect_sync(ocms->device, NULL, &error);
178 weston_log("colord: failed to connect to device %s: %s\n",
179 cd_device_get_object_path (ocms->device),
184 profile = cd_device_get_default_profile(ocms->device);
186 weston_log("colord: no assigned color profile for %s\n",
187 cd_device_get_id (ocms->device));
190 ret = cd_profile_connect_sync(profile, NULL, &error);
192 weston_log("colord: failed to connect to profile %s: %s\n",
193 cd_profile_get_object_path (profile),
199 /* get the calibration brightness level (only set for some profiles) */
200 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
202 percentage = atoi(tmp);
203 if (percentage > 0 && percentage <= 100)
204 ocms->backlight_value = percentage * 255 / 100;
207 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
208 if (ocms->p == NULL) {
209 weston_log("colord: warning failed to load profile %s: %s\n",
210 cd_profile_get_object_path (profile),
216 update_device_with_profile_in_idle(ocms);
220 colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
222 weston_log("colord: device %s changed, update output\n",
223 cd_device_get_object_path (ocms->device));
224 colord_update_output_from_device(ocms);
228 colord_notifier_output_destroy(struct wl_listener *listener, void *data)
230 struct cms_output *ocms =
231 container_of(listener, struct cms_output, destroy_listener);
232 struct weston_output *o = (struct weston_output *) data;
233 struct cms_colord *cms = ocms->cms;
236 device_id = get_output_id(cms, o);
237 g_hash_table_remove (cms->devices, device_id);
242 colord_output_created(struct cms_colord *cms, struct weston_output *o)
244 struct weston_head *head;
248 GError *error = NULL;
249 GHashTable *device_props;
250 struct cms_output *ocms;
252 /* XXX: What to do with multiple heads? */
253 head = weston_output_get_first_head(o);
256 device_id = get_output_id(cms, o);
257 weston_log("colord: output added %s\n", device_id);
258 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
260 g_hash_table_insert (device_props,
261 g_strdup(CD_DEVICE_PROPERTY_KIND),
262 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
263 g_hash_table_insert (device_props,
264 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
265 g_strdup("ColorModel.OutputMode.OutputResolution"));
266 g_hash_table_insert (device_props,
267 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
268 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
269 if (edid_value_valid(head->make)) {
270 tmp = g_hash_table_lookup(cms->pnp_ids, head->make);
273 g_hash_table_insert (device_props,
274 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
277 if (edid_value_valid(head->model)) {
278 g_hash_table_insert (device_props,
279 g_strdup(CD_DEVICE_PROPERTY_MODEL),
280 g_strdup(head->model));
282 if (edid_value_valid(head->serial_number)) {
283 g_hash_table_insert (device_props,
284 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
285 g_strdup(head->serial_number));
287 if (head->connection_internal) {
288 g_hash_table_insert (device_props,
289 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
292 device = cd_client_create_device_sync(cms->client,
294 CD_OBJECT_SCOPE_TEMP,
298 if (g_error_matches (error,
300 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
301 g_clear_error(&error);
302 device = cd_client_find_device_sync (cms->client,
308 weston_log("colord: failed to create new or "
309 "find existing device: %s\n",
315 /* create object and watch for the output to be destroyed */
316 ocms = g_slice_new0(struct cms_output);
319 ocms->device = g_object_ref(device);
320 ocms->destroy_listener.notify = colord_notifier_output_destroy;
321 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
323 /* add to local cache */
324 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
325 g_signal_connect (ocms->device, "changed",
326 G_CALLBACK (colord_device_changed_cb), ocms);
329 colord_update_output_from_device (ocms);
331 g_hash_table_unref (device_props);
333 g_object_unref (device);
338 colord_notifier_output_created(struct wl_listener *listener, void *data)
340 struct weston_output *o = (struct weston_output *) data;
341 struct cms_colord *cms =
342 container_of(listener, struct cms_colord, destroy_listener);
343 weston_log("colord: output %s created\n", o->name);
344 colord_output_created(cms, o);
348 colord_run_loop_thread(gpointer data)
350 struct cms_colord *cms = (struct cms_colord *) data;
351 struct weston_output *o;
353 /* coldplug outputs */
354 wl_list_for_each(o, &cms->ec->output_list, link) {
355 weston_log("colord: output %s coldplugged\n", o->name);
356 colord_output_created(cms, o);
359 g_main_loop_run(cms->loop);
364 colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
369 struct cms_colord *cms = data;
370 struct cms_output *ocms;
372 weston_log("colord: dispatching events\n");
373 g_mutex_lock(&cms->pending_mutex);
374 for (l = cms->pending; l != NULL; l = l->next) {
377 /* optionally set backlight to calibration value */
378 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
379 weston_log("colord: profile calibration backlight to %i/255\n",
380 ocms->backlight_value);
381 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
384 weston_cms_set_color_profile(ocms->o, ocms->p);
386 g_list_free (cms->pending);
388 g_mutex_unlock(&cms->pending_mutex);
391 rc = read(cms->readfd, &tmp, 1);
393 weston_log("colord: failed to read from pending fd\n");
398 colord_load_pnp_ids(struct cms_colord *cms)
400 gboolean ret = FALSE;
402 GError *error = NULL;
404 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
405 "/usr/share/misc/pnp.ids",
408 /* find and load file */
409 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
410 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
412 ret = g_file_get_contents(pnp_ids_fn[i],
417 weston_log("colord: failed to load %s: %s\n",
418 pnp_ids_fn[i], error->message);
425 weston_log("colord: no pnp.ids found\n");
429 /* parse fixed offsets into lines */
430 tmp = cms->pnp_ids_data;
431 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
432 if (cms->pnp_ids_data[i] != '\n')
434 cms->pnp_ids_data[i] = '\0';
435 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
437 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
438 tmp = &cms->pnp_ids_data[i+1];
444 colord_module_destroy(struct cms_colord *cms)
447 g_main_loop_quit(cms->loop);
448 g_main_loop_unref(cms->loop);
451 g_thread_join(cms->thread);
453 /* cms->devices must be destroyed before other resources, as
454 * the other resources are needed during output cleanup in
455 * cms->devices unref.
458 g_hash_table_unref(cms->devices);
460 g_object_unref(cms->client);
466 g_free(cms->pnp_ids_data);
467 g_hash_table_unref(cms->pnp_ids);
473 colord_notifier_destroy(struct wl_listener *listener, void *data)
475 struct cms_colord *cms =
476 container_of(listener, struct cms_colord, destroy_listener);
477 colord_module_destroy(cms);
481 colord_cms_output_destroy(gpointer data)
483 struct cms_output *ocms = (struct cms_output *) data;
484 struct cms_colord *cms = ocms->cms;
485 struct weston_output *o = ocms->o;
488 GError *error = NULL;
490 colord_idle_cancel_for_output(cms, o);
491 device_id = get_output_id(cms, o);
492 weston_log("colord: output unplugged %s\n", device_id);
494 wl_list_remove(&ocms->destroy_listener.link);
495 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
497 ret = cd_client_delete_device_sync (cms->client,
503 weston_log("colord: failed to delete device: %s\n",
508 g_object_unref(ocms->device);
509 g_slice_free(struct cms_output, ocms);
514 wet_module_init(struct weston_compositor *ec,
515 int *argc, char *argv[])
518 GError *error = NULL;
520 struct cms_colord *cms;
521 struct wl_event_loop *loop;
523 weston_log("colord: initialized\n");
525 /* create local state object */
526 cms = zalloc(sizeof *cms);
530 #if !GLIB_CHECK_VERSION(2,36,0)
533 cms->client = cd_client_new();
534 ret = cd_client_connect_sync(cms->client, NULL, &error);
536 weston_log("colord: failed to contact daemon: %s\n", error->message);
538 colord_module_destroy(cms);
541 g_mutex_init(&cms->pending_mutex);
542 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
543 g_free, colord_cms_output_destroy);
546 cms->destroy_listener.notify = colord_notifier_destroy;
547 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
550 cms->output_created_listener.notify = colord_notifier_output_created;
551 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
553 /* add all the PNP IDs */
554 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
558 colord_load_pnp_ids(cms);
560 /* setup a thread for the GLib callbacks */
561 cms->loop = g_main_loop_new(NULL, FALSE);
562 cms->thread = g_thread_new("colord CMS main loop",
563 colord_run_loop_thread, cms);
565 /* batch device<->profile updates */
566 if (pipe2(fd, O_CLOEXEC) == -1) {
567 colord_module_destroy(cms);
571 cms->writefd = fd[1];
572 loop = wl_display_get_event_loop(ec->wl_display);
573 cms->source = wl_event_loop_add_fd (loop,
576 colord_dispatch_all_pending,
579 colord_module_destroy(cms);