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"
37 #include "cms-helper.h"
38 #include "shared/helpers.h"
41 struct weston_compositor *ec;
43 GHashTable *devices; /* key = device-id, value = cms_output */
44 GHashTable *pnp_ids; /* key = pnp-id, value = vendor */
50 struct wl_event_source *source;
53 struct wl_listener destroy_listener;
54 struct wl_listener output_created_listener;
59 guint32 backlight_value;
60 struct cms_colord *cms;
61 struct weston_color_profile *p;
62 struct weston_output *o;
63 struct wl_listener destroy_listener;
67 colord_idle_find_output_cb(gconstpointer a, gconstpointer b)
69 struct cms_output *ocms = (struct cms_output *) a;
70 struct weston_output *o = (struct weston_output *) b;
71 return ocms->o == o ? 0 : -1;
75 colord_idle_cancel_for_output(struct cms_colord *cms, struct weston_output *o)
79 /* cancel and remove any helpers that match the output */
80 g_mutex_lock(&cms->pending_mutex);
81 l = g_list_find_custom (cms->pending, o, colord_idle_find_output_cb);
83 struct cms_output *ocms = l->data;
84 cms->pending = g_list_remove (cms->pending, ocms);
86 g_mutex_unlock(&cms->pending_mutex);
90 edid_value_valid(const char *str)
96 if (strcmp(str, "unknown") == 0)
102 get_output_id(struct cms_colord *cms, struct weston_output *o)
107 /* see https://github.com/hughsie/colord/blob/master/doc/device-and-profile-naming-spec.txt
108 * for format and allowed values */
109 device_id = g_string_new("xrandr");
110 if (edid_value_valid(o->make)) {
111 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
114 g_string_append_printf(device_id, "-%s", tmp);
116 if (edid_value_valid(o->model))
117 g_string_append_printf(device_id, "-%s", o->model);
118 if (edid_value_valid(o->serial_number))
119 g_string_append_printf(device_id, "-%s", o->serial_number);
121 /* no EDID data, so use fallback */
122 if (strcmp(device_id->str, "xrandr") == 0)
123 g_string_append_printf(device_id, "-drm-%i", o->id);
125 return g_string_free(device_id, FALSE);
129 update_device_with_profile_in_idle(struct cms_output *ocms)
131 gboolean signal_write = FALSE;
133 struct cms_colord *cms = ocms->cms;
135 colord_idle_cancel_for_output(cms, ocms->o);
136 g_mutex_lock(&cms->pending_mutex);
137 if (cms->pending == NULL)
139 cms->pending = g_list_prepend(cms->pending, ocms);
140 g_mutex_unlock(&cms->pending_mutex);
142 /* signal we've got updates to do */
145 rc = write(cms->writefd, &tmp, 1);
147 weston_log("colord: failed to write to pending fd\n");
152 colord_update_output_from_device (struct cms_output *ocms)
157 GError *error = NULL;
160 /* old profile is no longer valid */
161 weston_cms_destroy_profile(ocms->p);
164 ret = cd_device_connect_sync(ocms->device, NULL, &error);
166 weston_log("colord: failed to connect to device %s: %s\n",
167 cd_device_get_object_path (ocms->device),
172 profile = cd_device_get_default_profile(ocms->device);
174 weston_log("colord: no assigned color profile for %s\n",
175 cd_device_get_id (ocms->device));
178 ret = cd_profile_connect_sync(profile, NULL, &error);
180 weston_log("colord: failed to connect to profile %s: %s\n",
181 cd_profile_get_object_path (profile),
187 /* get the calibration brightness level (only set for some profiles) */
188 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
190 percentage = atoi(tmp);
191 if (percentage > 0 && percentage <= 100)
192 ocms->backlight_value = percentage * 255 / 100;
195 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
196 if (ocms->p == NULL) {
197 weston_log("colord: warning failed to load profile %s: %s\n",
198 cd_profile_get_object_path (profile),
204 update_device_with_profile_in_idle(ocms);
208 colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
210 weston_log("colord: device %s changed, update output\n",
211 cd_device_get_object_path (ocms->device));
212 colord_update_output_from_device(ocms);
216 colord_notifier_output_destroy(struct wl_listener *listener, void *data)
218 struct cms_output *ocms =
219 container_of(listener, struct cms_output, destroy_listener);
220 struct weston_output *o = (struct weston_output *) data;
221 struct cms_colord *cms = ocms->cms;
224 device_id = get_output_id(cms, o);
225 g_hash_table_remove (cms->devices, device_id);
230 colord_output_created(struct cms_colord *cms, struct weston_output *o)
235 GError *error = NULL;
236 GHashTable *device_props;
237 struct cms_output *ocms;
240 device_id = get_output_id(cms, o);
241 weston_log("colord: output added %s\n", device_id);
242 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
244 g_hash_table_insert (device_props,
245 g_strdup(CD_DEVICE_PROPERTY_KIND),
246 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
247 g_hash_table_insert (device_props,
248 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
249 g_strdup("ColorModel.OutputMode.OutputResolution"));
250 g_hash_table_insert (device_props,
251 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
252 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
253 if (edid_value_valid(o->make)) {
254 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
257 g_hash_table_insert (device_props,
258 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
261 if (edid_value_valid(o->model)) {
262 g_hash_table_insert (device_props,
263 g_strdup(CD_DEVICE_PROPERTY_MODEL),
266 if (edid_value_valid(o->serial_number)) {
267 g_hash_table_insert (device_props,
268 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
269 g_strdup(o->serial_number));
271 if (o->connection_internal) {
272 g_hash_table_insert (device_props,
273 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
276 device = cd_client_create_device_sync(cms->client,
278 CD_OBJECT_SCOPE_TEMP,
282 if (g_error_matches (error,
284 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
285 g_clear_error(&error);
286 device = cd_client_find_device_sync (cms->client,
292 weston_log("colord: failed to create new or "
293 "find existing device: %s\n",
299 /* create object and watch for the output to be destroyed */
300 ocms = g_slice_new0(struct cms_output);
303 ocms->device = g_object_ref(device);
304 ocms->destroy_listener.notify = colord_notifier_output_destroy;
305 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
307 /* add to local cache */
308 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
309 g_signal_connect (ocms->device, "changed",
310 G_CALLBACK (colord_device_changed_cb), ocms);
313 colord_update_output_from_device (ocms);
315 g_hash_table_unref (device_props);
317 g_object_unref (device);
322 colord_notifier_output_created(struct wl_listener *listener, void *data)
324 struct weston_output *o = (struct weston_output *) data;
325 struct cms_colord *cms =
326 container_of(listener, struct cms_colord, destroy_listener);
327 weston_log("colord: output %s created\n", o->name);
328 colord_output_created(cms, o);
332 colord_run_loop_thread(gpointer data)
334 struct cms_colord *cms = (struct cms_colord *) data;
335 struct weston_output *o;
337 /* coldplug outputs */
338 wl_list_for_each(o, &cms->ec->output_list, link) {
339 weston_log("colord: output %s coldplugged\n", o->name);
340 colord_output_created(cms, o);
343 g_main_loop_run(cms->loop);
348 colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
353 struct cms_colord *cms = data;
354 struct cms_output *ocms;
356 weston_log("colord: dispatching events\n");
357 g_mutex_lock(&cms->pending_mutex);
358 for (l = cms->pending; l != NULL; l = l->next) {
361 /* optionally set backlight to calibration value */
362 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
363 weston_log("colord: profile calibration backlight to %i/255\n",
364 ocms->backlight_value);
365 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
368 weston_cms_set_color_profile(ocms->o, ocms->p);
370 g_list_free (cms->pending);
372 g_mutex_unlock(&cms->pending_mutex);
375 rc = read(cms->readfd, &tmp, 1);
377 weston_log("colord: failed to read from pending fd\n");
382 colord_load_pnp_ids(struct cms_colord *cms)
384 gboolean ret = FALSE;
386 GError *error = NULL;
388 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
389 "/usr/share/misc/pnp.ids",
392 /* find and load file */
393 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
394 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
396 ret = g_file_get_contents(pnp_ids_fn[i],
401 weston_log("colord: failed to load %s: %s\n",
402 pnp_ids_fn[i], error->message);
409 weston_log("colord: no pnp.ids found\n");
413 /* parse fixed offsets into lines */
414 tmp = cms->pnp_ids_data;
415 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
416 if (cms->pnp_ids_data[i] != '\n')
418 cms->pnp_ids_data[i] = '\0';
419 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
421 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
422 tmp = &cms->pnp_ids_data[i+1];
428 colord_module_destroy(struct cms_colord *cms)
431 g_main_loop_quit(cms->loop);
432 g_main_loop_unref(cms->loop);
435 g_thread_join(cms->thread);
437 /* cms->devices must be destroyed before other resources, as
438 * the other resources are needed during output cleanup in
439 * cms->devices unref.
442 g_hash_table_unref(cms->devices);
444 g_object_unref(cms->client);
450 g_free(cms->pnp_ids_data);
451 g_hash_table_unref(cms->pnp_ids);
457 colord_notifier_destroy(struct wl_listener *listener, void *data)
459 struct cms_colord *cms =
460 container_of(listener, struct cms_colord, destroy_listener);
461 colord_module_destroy(cms);
465 colord_cms_output_destroy(gpointer data)
467 struct cms_output *ocms = (struct cms_output *) data;
468 struct cms_colord *cms = ocms->cms;
469 struct weston_output *o = ocms->o;
472 GError *error = NULL;
474 colord_idle_cancel_for_output(cms, o);
475 device_id = get_output_id(cms, o);
476 weston_log("colord: output unplugged %s\n", device_id);
478 wl_list_remove(&ocms->destroy_listener.link);
479 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
481 ret = cd_client_delete_device_sync (cms->client,
487 weston_log("colord: failed to delete device: %s\n",
492 g_object_unref(ocms->device);
493 g_slice_free(struct cms_output, ocms);
498 module_init(struct weston_compositor *ec,
499 int *argc, char *argv[])
502 GError *error = NULL;
504 struct cms_colord *cms;
505 struct wl_event_loop *loop;
507 weston_log("colord: initialized\n");
509 /* create local state object */
510 cms = zalloc(sizeof *cms);
514 #if !GLIB_CHECK_VERSION(2,36,0)
517 cms->client = cd_client_new();
518 ret = cd_client_connect_sync(cms->client, NULL, &error);
520 weston_log("colord: failed to contact daemon: %s\n", error->message);
522 colord_module_destroy(cms);
525 g_mutex_init(&cms->pending_mutex);
526 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
527 g_free, colord_cms_output_destroy);
530 cms->destroy_listener.notify = colord_notifier_destroy;
531 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
534 cms->output_created_listener.notify = colord_notifier_output_created;
535 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
537 /* add all the PNP IDs */
538 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
542 colord_load_pnp_ids(cms);
544 /* setup a thread for the GLib callbacks */
545 cms->loop = g_main_loop_new(NULL, FALSE);
546 cms->thread = g_thread_new("colord CMS main loop",
547 colord_run_loop_thread, cms);
549 /* batch device<->profile updates */
550 if (pipe2(fd, O_CLOEXEC) == -1) {
551 colord_module_destroy(cms);
555 cms->writefd = fd[1];
556 loop = wl_display_get_event_loop(ec->wl_display);
557 cms->source = wl_event_loop_add_fd (loop,
560 colord_dispatch_all_pending,
563 colord_module_destroy(cms);