blob: b318e4bc2cfadc3865f389e1662f097c40242985 [file] [log] [blame]
Richard Hughesbe7c4dd2013-05-11 09:48:22 +01001/*
2 * Copyright © 2013 Richard Hughes
3 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07004 * 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:
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010011 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070012 * 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.
15 *
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
23 * SOFTWARE.
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010024 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010030#include <fcntl.h>
31#include <unistd.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <colord.h>
36
37#include "compositor.h"
38#include "cms-helper.h"
39
40struct cms_colord {
41 struct weston_compositor *ec;
42 CdClient *client;
43 GHashTable *devices; /* key = device-id, value = cms_output */
44 GHashTable *pnp_ids; /* key = pnp-id, value = vendor */
45 gchar *pnp_ids_data;
46 GMainLoop *loop;
47 GThread *thread;
48 GList *pending;
49 GMutex pending_mutex;
50 struct wl_event_source *source;
51 int readfd;
52 int writefd;
53 struct wl_listener destroy_listener;
54 struct wl_listener output_created_listener;
55};
56
57struct cms_output {
58 CdDevice *device;
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;
64};
65
66static gint
67colord_idle_find_output_cb(gconstpointer a, gconstpointer b)
68{
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;
72}
73
74static void
75colord_idle_cancel_for_output(struct cms_colord *cms, struct weston_output *o)
76{
77 GList *l;
78
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);
82 if (l) {
83 struct cms_output *ocms = l->data;
84 cms->pending = g_list_remove (cms->pending, ocms);
85 }
86 g_mutex_unlock(&cms->pending_mutex);
87}
88
Derek Foreman280e7dd2014-10-03 13:13:42 -050089static bool
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010090edid_value_valid(const char *str)
91{
92 if (str == NULL)
Derek Foreman280e7dd2014-10-03 13:13:42 -050093 return false;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010094 if (str[0] == '\0')
Derek Foreman280e7dd2014-10-03 13:13:42 -050095 return false;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010096 if (strcmp(str, "unknown") == 0)
Derek Foreman280e7dd2014-10-03 13:13:42 -050097 return false;
98 return true;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010099}
100
101static gchar *
102get_output_id(struct cms_colord *cms, struct weston_output *o)
103{
104 const gchar *tmp;
105 GString *device_id;
106
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);
112 if (tmp == NULL)
113 tmp = o->make;
114 g_string_append_printf(device_id, "-%s", tmp);
115 }
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);
120
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);
124
125 return g_string_free(device_id, FALSE);
126}
127
128static void
129update_device_with_profile_in_idle(struct cms_output *ocms)
130{
131 gboolean signal_write = FALSE;
Richard Hughesd5616872013-05-15 09:17:38 +0100132 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100133 struct cms_colord *cms = ocms->cms;
134
135 colord_idle_cancel_for_output(cms, ocms->o);
136 g_mutex_lock(&cms->pending_mutex);
137 if (cms->pending == NULL)
138 signal_write = TRUE;
139 cms->pending = g_list_prepend(cms->pending, ocms);
140 g_mutex_unlock(&cms->pending_mutex);
141
142 /* signal we've got updates to do */
143 if (signal_write) {
144 gchar tmp = '\0';
Richard Hughesd5616872013-05-15 09:17:38 +0100145 rc = write(cms->writefd, &tmp, 1);
146 if (rc == 0)
147 weston_log("colord: failed to write to pending fd");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100148 }
149}
150
151static void
152colord_update_output_from_device (struct cms_output *ocms)
153{
154 CdProfile *profile;
155 const gchar *tmp;
156 gboolean ret;
157 GError *error = NULL;
158 gint percentage;
159
160 /* old profile is no longer valid */
161 weston_cms_destroy_profile(ocms->p);
162 ocms->p = NULL;
163
164 ret = cd_device_connect_sync(ocms->device, NULL, &error);
165 if (!ret) {
166 weston_log("colord: failed to connect to device %s: %s\n",
167 cd_device_get_object_path (ocms->device),
168 error->message);
169 g_error_free(error);
170 goto out;
171 }
172 profile = cd_device_get_default_profile(ocms->device);
173 if (!profile) {
174 weston_log("colord: no assigned color profile for %s\n",
175 cd_device_get_id (ocms->device));
176 goto out;
177 }
178 ret = cd_profile_connect_sync(profile, NULL, &error);
179 if (!ret) {
180 weston_log("colord: failed to connect to profile %s: %s\n",
181 cd_profile_get_object_path (profile),
182 error->message);
183 g_error_free(error);
184 goto out;
185 }
186
187 /* get the calibration brightness level (only set for some profiles) */
188 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
189 if (tmp != NULL) {
190 percentage = atoi(tmp);
191 if (percentage > 0 && percentage <= 100)
192 ocms->backlight_value = percentage * 255 / 100;
193 }
194
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),
199 error->message);
200 g_error_free(error);
201 goto out;
202 }
203out:
204 update_device_with_profile_in_idle(ocms);
205}
206
207static void
208colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
209{
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);
213}
214
215static void
216colord_notifier_output_destroy(struct wl_listener *listener, void *data)
217{
Olivier Fourdan2e710e52015-01-08 15:40:29 +0100218 struct cms_output *ocms =
219 container_of(listener, struct cms_output, destroy_listener);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100220 struct weston_output *o = (struct weston_output *) data;
Olivier Fourdan2e710e52015-01-08 15:40:29 +0100221 struct cms_colord *cms = ocms->cms;
222
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100223 gboolean ret;
224 gchar *device_id;
225 GError *error = NULL;
226
227 colord_idle_cancel_for_output(cms, o);
228 device_id = get_output_id(cms, o);
229 weston_log("colord: output removed %s\n", device_id);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100230 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
231 ret = cd_client_delete_device_sync (cms->client,
232 ocms->device,
233 NULL,
234 &error);
235
236 if (!ret) {
237 weston_log("colord: failed to delete device: %s\n", error->message);
238 g_error_free(error);
239 goto out;
240 }
241out:
242 g_hash_table_remove (cms->devices, device_id);
243 g_free (device_id);
244}
245
246static void
247colord_output_created(struct cms_colord *cms, struct weston_output *o)
248{
249 CdDevice *device;
250 const gchar *tmp;
251 gchar *device_id;
252 GError *error = NULL;
253 GHashTable *device_props;
254 struct cms_output *ocms;
255
256 /* create device */
257 device_id = get_output_id(cms, o);
258 weston_log("colord: output added %s\n", device_id);
259 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
260 g_free, g_free);
261 g_hash_table_insert (device_props,
262 g_strdup(CD_DEVICE_PROPERTY_KIND),
263 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
264 g_hash_table_insert (device_props,
265 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
266 g_strdup("ColorModel.OutputMode.OutputResolution"));
267 g_hash_table_insert (device_props,
268 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
269 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
270 if (edid_value_valid(o->make)) {
271 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
272 if (tmp == NULL)
273 tmp = o->make;
274 g_hash_table_insert (device_props,
275 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
276 g_strdup(tmp));
277 }
278 if (edid_value_valid(o->model)) {
279 g_hash_table_insert (device_props,
280 g_strdup(CD_DEVICE_PROPERTY_MODEL),
281 g_strdup(o->model));
282 }
283 if (edid_value_valid(o->serial_number)) {
284 g_hash_table_insert (device_props,
285 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
286 g_strdup(o->serial_number));
287 }
288 if (o->connection_internal) {
289 g_hash_table_insert (device_props,
290 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
291 NULL);
292 }
293 device = cd_client_create_device_sync(cms->client,
294 device_id,
295 CD_OBJECT_SCOPE_TEMP,
296 device_props,
297 NULL,
298 &error);
299 if (g_error_matches (error,
300 CD_CLIENT_ERROR,
301 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
302 g_clear_error(&error);
303 device = cd_client_find_device_sync (cms->client,
304 device_id,
305 NULL,
306 &error);
307 }
308 if (!device) {
309 weston_log("colord: failed to create new or "
310 "find existing device: %s\n",
311 error->message);
312 g_error_free(error);
313 goto out;
314 }
315
316 /* create object and watch for the output to be destroyed */
317 ocms = g_slice_new0(struct cms_output);
318 ocms->cms = cms;
319 ocms->o = o;
320 ocms->device = g_object_ref(device);
321 ocms->destroy_listener.notify = colord_notifier_output_destroy;
322 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
323
324 /* add to local cache */
325 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
326 g_signal_connect (ocms->device, "changed",
327 G_CALLBACK (colord_device_changed_cb), ocms);
328
329 /* get profiles */
330 colord_update_output_from_device (ocms);
331out:
332 g_hash_table_unref (device_props);
333 if (device)
334 g_object_unref (device);
335 g_free (device_id);
336}
337
338static void
339colord_notifier_output_created(struct wl_listener *listener, void *data)
340{
341 struct weston_output *o = (struct weston_output *) data;
342 struct cms_colord *cms =
343 container_of(listener, struct cms_colord, destroy_listener);
344 weston_log("colord: output %s created\n", o->name);
345 colord_output_created(cms, o);
346}
347
348static gpointer
349colord_run_loop_thread(gpointer data)
350{
351 struct cms_colord *cms = (struct cms_colord *) data;
352 struct weston_output *o;
353
354 /* coldplug outputs */
355 wl_list_for_each(o, &cms->ec->output_list, link) {
356 weston_log("colord: output %s coldplugged\n", o->name);
357 colord_output_created(cms, o);
358 }
359
360 g_main_loop_run(cms->loop);
361 return NULL;
362}
363
364static int
365colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
366{
367 gchar tmp;
368 GList *l;
Richard Hughesd5616872013-05-15 09:17:38 +0100369 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100370 struct cms_colord *cms = data;
371 struct cms_output *ocms;
372
373 weston_log("colord: dispatching events\n");
374 g_mutex_lock(&cms->pending_mutex);
375 for (l = cms->pending; l != NULL; l = l->next) {
376 ocms = l->data;
377
378 /* optionally set backlight to calibration value */
379 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
380 weston_log("colord: profile calibration backlight to %i/255\n",
381 ocms->backlight_value);
382 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
383 }
384
385 weston_cms_set_color_profile(ocms->o, ocms->p);
386 }
387 g_list_free (cms->pending);
388 cms->pending = NULL;
389 g_mutex_unlock(&cms->pending_mutex);
390
391 /* done */
Richard Hughesd5616872013-05-15 09:17:38 +0100392 rc = read(cms->readfd, &tmp, 1);
393 if (rc == 0)
394 weston_log("colord: failed to read from pending fd");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100395 return 1;
396}
397
398static void
399colord_load_pnp_ids(struct cms_colord *cms)
400{
401 gboolean ret = FALSE;
402 gchar *tmp;
403 GError *error = NULL;
404 guint i;
405 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
406 "/usr/share/misc/pnp.ids",
407 NULL };
408
409 /* find and load file */
410 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
411 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
412 continue;
413 ret = g_file_get_contents(pnp_ids_fn[i],
414 &cms->pnp_ids_data,
415 NULL,
416 &error);
417 if (!ret) {
418 weston_log("colord: failed to load %s: %s\n",
419 pnp_ids_fn[i], error->message);
420 g_error_free(error);
421 return;
422 }
423 break;
424 }
425 if (!ret) {
426 weston_log("colord: no pnp.ids found\n");
427 return;
428 }
429
430 /* parse fixed offsets into lines */
431 tmp = cms->pnp_ids_data;
432 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
433 if (cms->pnp_ids_data[i] != '\n')
434 continue;
435 cms->pnp_ids_data[i] = '\0';
436 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
437 tmp[3] = '\0';
438 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
439 tmp = &cms->pnp_ids_data[i+1];
440 }
441 }
442}
443
444static void
445colord_module_destroy(struct cms_colord *cms)
446{
447 g_free(cms->pnp_ids_data);
448 g_hash_table_unref(cms->pnp_ids);
449
450 if (cms->loop) {
451 g_main_loop_quit(cms->loop);
452 g_main_loop_unref(cms->loop);
453 }
454 if (cms->thread)
455 g_thread_join(cms->thread);
456 if (cms->devices)
457 g_hash_table_unref(cms->devices);
458 if (cms->client)
459 g_object_unref(cms->client);
460 if (cms->readfd)
461 close(cms->readfd);
462 if (cms->writefd)
463 close(cms->writefd);
464 free(cms);
465}
466
467static void
468colord_notifier_destroy(struct wl_listener *listener, void *data)
469{
470 struct cms_colord *cms =
471 container_of(listener, struct cms_colord, destroy_listener);
472 colord_module_destroy(cms);
473}
474
475static void
476colord_cms_output_destroy(gpointer data)
477{
478 struct cms_output *ocms = (struct cms_output *) data;
479 g_object_unref(ocms->device);
480 g_slice_free(struct cms_output, ocms);
481}
482
483WL_EXPORT int
484module_init(struct weston_compositor *ec,
Richard Hughes2379a652013-05-15 09:17:37 +0100485 int *argc, char *argv[])
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100486{
487 gboolean ret;
488 GError *error = NULL;
489 int fd[2];
490 struct cms_colord *cms;
491 struct wl_event_loop *loop;
492
493 weston_log("colord: initialized\n");
494
495 /* create local state object */
Peter Huttererf3d62272013-08-08 11:57:05 +1000496 cms = zalloc(sizeof *cms);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100497 if (cms == NULL)
498 return -1;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100499 cms->ec = ec;
500#if !GLIB_CHECK_VERSION(2,36,0)
501 g_type_init();
502#endif
503 cms->client = cd_client_new();
504 ret = cd_client_connect_sync(cms->client, NULL, &error);
505 if (!ret) {
506 weston_log("colord: failed to contact daemon: %s\n", error->message);
507 g_error_free(error);
508 colord_module_destroy(cms);
509 return -1;
510 }
511 g_mutex_init(&cms->pending_mutex);
512 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
513 g_free, colord_cms_output_destroy);
514
515 /* destroy */
516 cms->destroy_listener.notify = colord_notifier_destroy;
517 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
518
519 /* devices added */
520 cms->output_created_listener.notify = colord_notifier_output_created;
521 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
522
523 /* add all the PNP IDs */
524 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
525 g_str_equal,
526 NULL,
527 NULL);
528 colord_load_pnp_ids(cms);
529
530 /* setup a thread for the GLib callbacks */
531 cms->loop = g_main_loop_new(NULL, FALSE);
532 cms->thread = g_thread_new("colord CMS main loop",
533 colord_run_loop_thread, cms);
534
535 /* batch device<->profile updates */
536 if (pipe2(fd, O_CLOEXEC) == -1) {
537 colord_module_destroy(cms);
538 return -1;
539 }
540 cms->readfd = fd[0];
541 cms->writefd = fd[1];
542 loop = wl_display_get_event_loop(ec->wl_display);
543 cms->source = wl_event_loop_add_fd (loop,
544 cms->readfd,
545 WL_EVENT_READABLE,
546 colord_dispatch_all_pending,
547 cms);
548 if (!cms->source) {
549 colord_module_destroy(cms);
550 return -1;
551 }
552 return 0;
553}