blob: 2adc886bc9f397688d161e5cdb6ce622eb0fab26 [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"
Jon Cruz867d50e2015-06-15 15:37:10 -070039#include "shared/helpers.h"
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010040
41struct cms_colord {
42 struct weston_compositor *ec;
43 CdClient *client;
44 GHashTable *devices; /* key = device-id, value = cms_output */
45 GHashTable *pnp_ids; /* key = pnp-id, value = vendor */
46 gchar *pnp_ids_data;
47 GMainLoop *loop;
48 GThread *thread;
49 GList *pending;
50 GMutex pending_mutex;
51 struct wl_event_source *source;
52 int readfd;
53 int writefd;
54 struct wl_listener destroy_listener;
55 struct wl_listener output_created_listener;
56};
57
58struct cms_output {
59 CdDevice *device;
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;
65};
66
67static gint
68colord_idle_find_output_cb(gconstpointer a, gconstpointer b)
69{
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;
73}
74
75static void
76colord_idle_cancel_for_output(struct cms_colord *cms, struct weston_output *o)
77{
78 GList *l;
79
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);
83 if (l) {
84 struct cms_output *ocms = l->data;
85 cms->pending = g_list_remove (cms->pending, ocms);
86 }
87 g_mutex_unlock(&cms->pending_mutex);
88}
89
Derek Foreman280e7dd2014-10-03 13:13:42 -050090static bool
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010091edid_value_valid(const char *str)
92{
93 if (str == NULL)
Derek Foreman280e7dd2014-10-03 13:13:42 -050094 return false;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010095 if (str[0] == '\0')
Derek Foreman280e7dd2014-10-03 13:13:42 -050096 return false;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010097 if (strcmp(str, "unknown") == 0)
Derek Foreman280e7dd2014-10-03 13:13:42 -050098 return false;
99 return true;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100100}
101
102static gchar *
103get_output_id(struct cms_colord *cms, struct weston_output *o)
104{
105 const gchar *tmp;
106 GString *device_id;
107
108 /* see https://github.com/hughsie/colord/blob/master/doc/device-and-profile-naming-spec.txt
109 * for format and allowed values */
110 device_id = g_string_new("xrandr");
111 if (edid_value_valid(o->make)) {
112 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
113 if (tmp == NULL)
114 tmp = o->make;
115 g_string_append_printf(device_id, "-%s", tmp);
116 }
117 if (edid_value_valid(o->model))
118 g_string_append_printf(device_id, "-%s", o->model);
119 if (edid_value_valid(o->serial_number))
120 g_string_append_printf(device_id, "-%s", o->serial_number);
121
122 /* no EDID data, so use fallback */
123 if (strcmp(device_id->str, "xrandr") == 0)
124 g_string_append_printf(device_id, "-drm-%i", o->id);
125
126 return g_string_free(device_id, FALSE);
127}
128
129static void
130update_device_with_profile_in_idle(struct cms_output *ocms)
131{
132 gboolean signal_write = FALSE;
Richard Hughesd5616872013-05-15 09:17:38 +0100133 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100134 struct cms_colord *cms = ocms->cms;
135
136 colord_idle_cancel_for_output(cms, ocms->o);
137 g_mutex_lock(&cms->pending_mutex);
138 if (cms->pending == NULL)
139 signal_write = TRUE;
140 cms->pending = g_list_prepend(cms->pending, ocms);
141 g_mutex_unlock(&cms->pending_mutex);
142
143 /* signal we've got updates to do */
144 if (signal_write) {
145 gchar tmp = '\0';
Richard Hughesd5616872013-05-15 09:17:38 +0100146 rc = write(cms->writefd, &tmp, 1);
147 if (rc == 0)
148 weston_log("colord: failed to write to pending fd");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100149 }
150}
151
152static void
153colord_update_output_from_device (struct cms_output *ocms)
154{
155 CdProfile *profile;
156 const gchar *tmp;
157 gboolean ret;
158 GError *error = NULL;
159 gint percentage;
160
161 /* old profile is no longer valid */
162 weston_cms_destroy_profile(ocms->p);
163 ocms->p = NULL;
164
165 ret = cd_device_connect_sync(ocms->device, NULL, &error);
166 if (!ret) {
167 weston_log("colord: failed to connect to device %s: %s\n",
168 cd_device_get_object_path (ocms->device),
169 error->message);
170 g_error_free(error);
171 goto out;
172 }
173 profile = cd_device_get_default_profile(ocms->device);
174 if (!profile) {
175 weston_log("colord: no assigned color profile for %s\n",
176 cd_device_get_id (ocms->device));
177 goto out;
178 }
179 ret = cd_profile_connect_sync(profile, NULL, &error);
180 if (!ret) {
181 weston_log("colord: failed to connect to profile %s: %s\n",
182 cd_profile_get_object_path (profile),
183 error->message);
184 g_error_free(error);
185 goto out;
186 }
187
188 /* get the calibration brightness level (only set for some profiles) */
189 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
190 if (tmp != NULL) {
191 percentage = atoi(tmp);
192 if (percentage > 0 && percentage <= 100)
193 ocms->backlight_value = percentage * 255 / 100;
194 }
195
196 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
197 if (ocms->p == NULL) {
198 weston_log("colord: warning failed to load profile %s: %s\n",
199 cd_profile_get_object_path (profile),
200 error->message);
201 g_error_free(error);
202 goto out;
203 }
204out:
205 update_device_with_profile_in_idle(ocms);
206}
207
208static void
209colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
210{
211 weston_log("colord: device %s changed, update output\n",
212 cd_device_get_object_path (ocms->device));
213 colord_update_output_from_device(ocms);
214}
215
216static void
217colord_notifier_output_destroy(struct wl_listener *listener, void *data)
218{
Olivier Fourdan2e710e52015-01-08 15:40:29 +0100219 struct cms_output *ocms =
220 container_of(listener, struct cms_output, destroy_listener);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100221 struct weston_output *o = (struct weston_output *) data;
Olivier Fourdan2e710e52015-01-08 15:40:29 +0100222 struct cms_colord *cms = ocms->cms;
223
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100224 gboolean ret;
225 gchar *device_id;
226 GError *error = NULL;
227
228 colord_idle_cancel_for_output(cms, o);
229 device_id = get_output_id(cms, o);
230 weston_log("colord: output removed %s\n", device_id);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100231 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
232 ret = cd_client_delete_device_sync (cms->client,
233 ocms->device,
234 NULL,
235 &error);
236
237 if (!ret) {
238 weston_log("colord: failed to delete device: %s\n", error->message);
239 g_error_free(error);
240 goto out;
241 }
242out:
243 g_hash_table_remove (cms->devices, device_id);
244 g_free (device_id);
245}
246
247static void
248colord_output_created(struct cms_colord *cms, struct weston_output *o)
249{
250 CdDevice *device;
251 const gchar *tmp;
252 gchar *device_id;
253 GError *error = NULL;
254 GHashTable *device_props;
255 struct cms_output *ocms;
256
257 /* create device */
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,
261 g_free, g_free);
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);
273 if (tmp == NULL)
274 tmp = o->make;
275 g_hash_table_insert (device_props,
276 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
277 g_strdup(tmp));
278 }
279 if (edid_value_valid(o->model)) {
280 g_hash_table_insert (device_props,
281 g_strdup(CD_DEVICE_PROPERTY_MODEL),
282 g_strdup(o->model));
283 }
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));
288 }
289 if (o->connection_internal) {
290 g_hash_table_insert (device_props,
291 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
292 NULL);
293 }
294 device = cd_client_create_device_sync(cms->client,
295 device_id,
296 CD_OBJECT_SCOPE_TEMP,
297 device_props,
298 NULL,
299 &error);
300 if (g_error_matches (error,
301 CD_CLIENT_ERROR,
302 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
303 g_clear_error(&error);
304 device = cd_client_find_device_sync (cms->client,
305 device_id,
306 NULL,
307 &error);
308 }
309 if (!device) {
310 weston_log("colord: failed to create new or "
311 "find existing device: %s\n",
312 error->message);
313 g_error_free(error);
314 goto out;
315 }
316
317 /* create object and watch for the output to be destroyed */
318 ocms = g_slice_new0(struct cms_output);
319 ocms->cms = cms;
320 ocms->o = o;
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);
324
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);
329
330 /* get profiles */
331 colord_update_output_from_device (ocms);
332out:
333 g_hash_table_unref (device_props);
334 if (device)
335 g_object_unref (device);
336 g_free (device_id);
337}
338
339static void
340colord_notifier_output_created(struct wl_listener *listener, void *data)
341{
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);
347}
348
349static gpointer
350colord_run_loop_thread(gpointer data)
351{
352 struct cms_colord *cms = (struct cms_colord *) data;
353 struct weston_output *o;
354
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);
359 }
360
361 g_main_loop_run(cms->loop);
362 return NULL;
363}
364
365static int
366colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
367{
368 gchar tmp;
369 GList *l;
Richard Hughesd5616872013-05-15 09:17:38 +0100370 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100371 struct cms_colord *cms = data;
372 struct cms_output *ocms;
373
374 weston_log("colord: dispatching events\n");
375 g_mutex_lock(&cms->pending_mutex);
376 for (l = cms->pending; l != NULL; l = l->next) {
377 ocms = l->data;
378
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);
384 }
385
386 weston_cms_set_color_profile(ocms->o, ocms->p);
387 }
388 g_list_free (cms->pending);
389 cms->pending = NULL;
390 g_mutex_unlock(&cms->pending_mutex);
391
392 /* done */
Richard Hughesd5616872013-05-15 09:17:38 +0100393 rc = read(cms->readfd, &tmp, 1);
394 if (rc == 0)
395 weston_log("colord: failed to read from pending fd");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100396 return 1;
397}
398
399static void
400colord_load_pnp_ids(struct cms_colord *cms)
401{
402 gboolean ret = FALSE;
403 gchar *tmp;
404 GError *error = NULL;
405 guint i;
406 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
407 "/usr/share/misc/pnp.ids",
408 NULL };
409
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))
413 continue;
414 ret = g_file_get_contents(pnp_ids_fn[i],
415 &cms->pnp_ids_data,
416 NULL,
417 &error);
418 if (!ret) {
419 weston_log("colord: failed to load %s: %s\n",
420 pnp_ids_fn[i], error->message);
421 g_error_free(error);
422 return;
423 }
424 break;
425 }
426 if (!ret) {
427 weston_log("colord: no pnp.ids found\n");
428 return;
429 }
430
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')
435 continue;
436 cms->pnp_ids_data[i] = '\0';
437 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
438 tmp[3] = '\0';
439 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
440 tmp = &cms->pnp_ids_data[i+1];
441 }
442 }
443}
444
445static void
446colord_module_destroy(struct cms_colord *cms)
447{
448 g_free(cms->pnp_ids_data);
449 g_hash_table_unref(cms->pnp_ids);
450
451 if (cms->loop) {
452 g_main_loop_quit(cms->loop);
453 g_main_loop_unref(cms->loop);
454 }
455 if (cms->thread)
456 g_thread_join(cms->thread);
457 if (cms->devices)
458 g_hash_table_unref(cms->devices);
459 if (cms->client)
460 g_object_unref(cms->client);
461 if (cms->readfd)
462 close(cms->readfd);
463 if (cms->writefd)
464 close(cms->writefd);
465 free(cms);
466}
467
468static void
469colord_notifier_destroy(struct wl_listener *listener, void *data)
470{
471 struct cms_colord *cms =
472 container_of(listener, struct cms_colord, destroy_listener);
473 colord_module_destroy(cms);
474}
475
476static void
477colord_cms_output_destroy(gpointer data)
478{
479 struct cms_output *ocms = (struct cms_output *) data;
480 g_object_unref(ocms->device);
481 g_slice_free(struct cms_output, ocms);
482}
483
484WL_EXPORT int
485module_init(struct weston_compositor *ec,
Richard Hughes2379a652013-05-15 09:17:37 +0100486 int *argc, char *argv[])
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100487{
488 gboolean ret;
489 GError *error = NULL;
490 int fd[2];
491 struct cms_colord *cms;
492 struct wl_event_loop *loop;
493
494 weston_log("colord: initialized\n");
495
496 /* create local state object */
Peter Huttererf3d62272013-08-08 11:57:05 +1000497 cms = zalloc(sizeof *cms);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100498 if (cms == NULL)
499 return -1;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100500 cms->ec = ec;
501#if !GLIB_CHECK_VERSION(2,36,0)
502 g_type_init();
503#endif
504 cms->client = cd_client_new();
505 ret = cd_client_connect_sync(cms->client, NULL, &error);
506 if (!ret) {
507 weston_log("colord: failed to contact daemon: %s\n", error->message);
508 g_error_free(error);
509 colord_module_destroy(cms);
510 return -1;
511 }
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);
515
516 /* destroy */
517 cms->destroy_listener.notify = colord_notifier_destroy;
518 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
519
520 /* devices added */
521 cms->output_created_listener.notify = colord_notifier_output_created;
522 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
523
524 /* add all the PNP IDs */
525 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
526 g_str_equal,
527 NULL,
528 NULL);
529 colord_load_pnp_ids(cms);
530
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);
535
536 /* batch device<->profile updates */
537 if (pipe2(fd, O_CLOEXEC) == -1) {
538 colord_module_destroy(cms);
539 return -1;
540 }
541 cms->readfd = fd[0];
542 cms->writefd = fd[1];
543 loop = wl_display_get_event_loop(ec->wl_display);
544 cms->source = wl_event_loop_add_fd (loop,
545 cms->readfd,
546 WL_EVENT_READABLE,
547 colord_dispatch_all_pending,
548 cms);
549 if (!cms->source) {
550 colord_module_destroy(cms);
551 return -1;
552 }
553 return 0;
554}