2 * Copyright © 2013 Richard Hughes
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 #include "compositor.h"
35 #include "cms-helper.h"
38 struct weston_compositor *ec;
40 GHashTable *devices; /* key = device-id, value = cms_output */
41 GHashTable *pnp_ids; /* key = pnp-id, value = vendor */
47 struct wl_event_source *source;
50 struct wl_listener destroy_listener;
51 struct wl_listener output_created_listener;
56 guint32 backlight_value;
57 struct cms_colord *cms;
58 struct weston_color_profile *p;
59 struct weston_output *o;
60 struct wl_listener destroy_listener;
64 colord_idle_find_output_cb(gconstpointer a, gconstpointer b)
66 struct cms_output *ocms = (struct cms_output *) a;
67 struct weston_output *o = (struct weston_output *) b;
68 return ocms->o == o ? 0 : -1;
72 colord_idle_cancel_for_output(struct cms_colord *cms, struct weston_output *o)
76 /* cancel and remove any helpers that match the output */
77 g_mutex_lock(&cms->pending_mutex);
78 l = g_list_find_custom (cms->pending, o, colord_idle_find_output_cb);
80 struct cms_output *ocms = l->data;
81 cms->pending = g_list_remove (cms->pending, ocms);
83 g_mutex_unlock(&cms->pending_mutex);
87 edid_value_valid(const char *str)
93 if (strcmp(str, "unknown") == 0)
99 get_output_id(struct cms_colord *cms, struct weston_output *o)
104 /* see https://github.com/hughsie/colord/blob/master/doc/device-and-profile-naming-spec.txt
105 * for format and allowed values */
106 device_id = g_string_new("xrandr");
107 if (edid_value_valid(o->make)) {
108 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
111 g_string_append_printf(device_id, "-%s", tmp);
113 if (edid_value_valid(o->model))
114 g_string_append_printf(device_id, "-%s", o->model);
115 if (edid_value_valid(o->serial_number))
116 g_string_append_printf(device_id, "-%s", o->serial_number);
118 /* no EDID data, so use fallback */
119 if (strcmp(device_id->str, "xrandr") == 0)
120 g_string_append_printf(device_id, "-drm-%i", o->id);
122 return g_string_free(device_id, FALSE);
126 update_device_with_profile_in_idle(struct cms_output *ocms)
128 gboolean signal_write = FALSE;
130 struct cms_colord *cms = ocms->cms;
132 colord_idle_cancel_for_output(cms, ocms->o);
133 g_mutex_lock(&cms->pending_mutex);
134 if (cms->pending == NULL)
136 cms->pending = g_list_prepend(cms->pending, ocms);
137 g_mutex_unlock(&cms->pending_mutex);
139 /* signal we've got updates to do */
142 rc = write(cms->writefd, &tmp, 1);
144 weston_log("colord: failed to write to pending fd");
149 colord_update_output_from_device (struct cms_output *ocms)
154 GError *error = NULL;
157 /* old profile is no longer valid */
158 weston_cms_destroy_profile(ocms->p);
161 ret = cd_device_connect_sync(ocms->device, NULL, &error);
163 weston_log("colord: failed to connect to device %s: %s\n",
164 cd_device_get_object_path (ocms->device),
169 profile = cd_device_get_default_profile(ocms->device);
171 weston_log("colord: no assigned color profile for %s\n",
172 cd_device_get_id (ocms->device));
175 ret = cd_profile_connect_sync(profile, NULL, &error);
177 weston_log("colord: failed to connect to profile %s: %s\n",
178 cd_profile_get_object_path (profile),
184 /* get the calibration brightness level (only set for some profiles) */
185 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
187 percentage = atoi(tmp);
188 if (percentage > 0 && percentage <= 100)
189 ocms->backlight_value = percentage * 255 / 100;
192 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
193 if (ocms->p == NULL) {
194 weston_log("colord: warning failed to load profile %s: %s\n",
195 cd_profile_get_object_path (profile),
201 update_device_with_profile_in_idle(ocms);
205 colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
207 weston_log("colord: device %s changed, update output\n",
208 cd_device_get_object_path (ocms->device));
209 colord_update_output_from_device(ocms);
213 colord_notifier_output_destroy(struct wl_listener *listener, void *data)
215 struct cms_colord *cms =
216 container_of(listener, struct cms_colord, destroy_listener);
217 struct weston_output *o = (struct weston_output *) data;
218 struct cms_output *ocms;
221 GError *error = NULL;
223 colord_idle_cancel_for_output(cms, o);
224 device_id = get_output_id(cms, o);
225 weston_log("colord: output removed %s\n", device_id);
226 ocms = g_hash_table_lookup(cms->devices, device_id);
228 weston_log("colord: failed to delete device\n");
231 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
232 ret = cd_client_delete_device_sync (cms->client,
238 weston_log("colord: failed to delete device: %s\n", error->message);
243 g_hash_table_remove (cms->devices, device_id);
248 colord_output_created(struct cms_colord *cms, struct weston_output *o)
253 GError *error = NULL;
254 GHashTable *device_props;
255 struct cms_output *ocms;
258 device_id = get_output_id(cms, o);
259 weston_log("colord: output added %s\n", device_id);
260 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
262 g_hash_table_insert (device_props,
263 g_strdup(CD_DEVICE_PROPERTY_KIND),
264 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
265 g_hash_table_insert (device_props,
266 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
267 g_strdup("ColorModel.OutputMode.OutputResolution"));
268 g_hash_table_insert (device_props,
269 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
270 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
271 if (edid_value_valid(o->make)) {
272 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
275 g_hash_table_insert (device_props,
276 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
279 if (edid_value_valid(o->model)) {
280 g_hash_table_insert (device_props,
281 g_strdup(CD_DEVICE_PROPERTY_MODEL),
284 if (edid_value_valid(o->serial_number)) {
285 g_hash_table_insert (device_props,
286 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
287 g_strdup(o->serial_number));
289 if (o->connection_internal) {
290 g_hash_table_insert (device_props,
291 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
294 device = cd_client_create_device_sync(cms->client,
296 CD_OBJECT_SCOPE_TEMP,
300 if (g_error_matches (error,
302 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
303 g_clear_error(&error);
304 device = cd_client_find_device_sync (cms->client,
310 weston_log("colord: failed to create new or "
311 "find existing device: %s\n",
317 /* create object and watch for the output to be destroyed */
318 ocms = g_slice_new0(struct cms_output);
321 ocms->device = g_object_ref(device);
322 ocms->destroy_listener.notify = colord_notifier_output_destroy;
323 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
325 /* add to local cache */
326 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
327 g_signal_connect (ocms->device, "changed",
328 G_CALLBACK (colord_device_changed_cb), ocms);
331 colord_update_output_from_device (ocms);
333 g_hash_table_unref (device_props);
335 g_object_unref (device);
340 colord_notifier_output_created(struct wl_listener *listener, void *data)
342 struct weston_output *o = (struct weston_output *) data;
343 struct cms_colord *cms =
344 container_of(listener, struct cms_colord, destroy_listener);
345 weston_log("colord: output %s created\n", o->name);
346 colord_output_created(cms, o);
350 colord_run_loop_thread(gpointer data)
352 struct cms_colord *cms = (struct cms_colord *) data;
353 struct weston_output *o;
355 /* coldplug outputs */
356 wl_list_for_each(o, &cms->ec->output_list, link) {
357 weston_log("colord: output %s coldplugged\n", o->name);
358 colord_output_created(cms, o);
361 g_main_loop_run(cms->loop);
366 colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
371 struct cms_colord *cms = data;
372 struct cms_output *ocms;
374 weston_log("colord: dispatching events\n");
375 g_mutex_lock(&cms->pending_mutex);
376 for (l = cms->pending; l != NULL; l = l->next) {
379 /* optionally set backlight to calibration value */
380 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
381 weston_log("colord: profile calibration backlight to %i/255\n",
382 ocms->backlight_value);
383 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
386 weston_cms_set_color_profile(ocms->o, ocms->p);
388 g_list_free (cms->pending);
390 g_mutex_unlock(&cms->pending_mutex);
393 rc = read(cms->readfd, &tmp, 1);
395 weston_log("colord: failed to read from pending fd");
400 colord_load_pnp_ids(struct cms_colord *cms)
402 gboolean ret = FALSE;
404 GError *error = NULL;
406 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
407 "/usr/share/misc/pnp.ids",
410 /* find and load file */
411 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
412 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
414 ret = g_file_get_contents(pnp_ids_fn[i],
419 weston_log("colord: failed to load %s: %s\n",
420 pnp_ids_fn[i], error->message);
427 weston_log("colord: no pnp.ids found\n");
431 /* parse fixed offsets into lines */
432 tmp = cms->pnp_ids_data;
433 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
434 if (cms->pnp_ids_data[i] != '\n')
436 cms->pnp_ids_data[i] = '\0';
437 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
439 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
440 tmp = &cms->pnp_ids_data[i+1];
446 colord_module_destroy(struct cms_colord *cms)
448 g_free(cms->pnp_ids_data);
449 g_hash_table_unref(cms->pnp_ids);
452 g_main_loop_quit(cms->loop);
453 g_main_loop_unref(cms->loop);
456 g_thread_join(cms->thread);
458 g_hash_table_unref(cms->devices);
460 g_object_unref(cms->client);
469 colord_notifier_destroy(struct wl_listener *listener, void *data)
471 struct cms_colord *cms =
472 container_of(listener, struct cms_colord, destroy_listener);
473 colord_module_destroy(cms);
477 colord_cms_output_destroy(gpointer data)
479 struct cms_output *ocms = (struct cms_output *) data;
480 g_object_unref(ocms->device);
481 g_slice_free(struct cms_output, ocms);
485 module_init(struct weston_compositor *ec,
486 int *argc, char *argv[])
489 GError *error = NULL;
491 struct cms_colord *cms;
492 struct wl_event_loop *loop;
494 weston_log("colord: initialized\n");
496 /* create local state object */
497 cms = zalloc(sizeof *cms);
501 #if !GLIB_CHECK_VERSION(2,36,0)
504 cms->client = cd_client_new();
505 ret = cd_client_connect_sync(cms->client, NULL, &error);
507 weston_log("colord: failed to contact daemon: %s\n", error->message);
509 colord_module_destroy(cms);
512 g_mutex_init(&cms->pending_mutex);
513 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
514 g_free, colord_cms_output_destroy);
517 cms->destroy_listener.notify = colord_notifier_destroy;
518 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
521 cms->output_created_listener.notify = colord_notifier_output_created;
522 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
524 /* add all the PNP IDs */
525 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
529 colord_load_pnp_ids(cms);
531 /* setup a thread for the GLib callbacks */
532 cms->loop = g_main_loop_new(NULL, FALSE);
533 cms->thread = g_thread_new("colord CMS main loop",
534 colord_run_loop_thread, cms);
536 /* batch device<->profile updates */
537 if (pipe2(fd, O_CLOEXEC) == -1) {
538 colord_module_destroy(cms);
542 cms->writefd = fd[1];
543 loop = wl_display_get_event_loop(ec->wl_display);
544 cms->source = wl_event_loop_add_fd (loop,
547 colord_dispatch_all_pending,
550 colord_module_destroy(cms);