blob: b9bc9e3b75b0809bbfd3c41f7792907ff1ee6092 [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
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070026#include "config.h"
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010027
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010028#include <fcntl.h>
29#include <unistd.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <colord.h>
34
35#include "compositor.h"
36#include "cms-helper.h"
Jon Cruz867d50e2015-06-15 15:37:10 -070037#include "shared/helpers.h"
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010038
39struct cms_colord {
40 struct weston_compositor *ec;
41 CdClient *client;
42 GHashTable *devices; /* key = device-id, value = cms_output */
43 GHashTable *pnp_ids; /* key = pnp-id, value = vendor */
44 gchar *pnp_ids_data;
45 GMainLoop *loop;
46 GThread *thread;
47 GList *pending;
48 GMutex pending_mutex;
49 struct wl_event_source *source;
50 int readfd;
51 int writefd;
52 struct wl_listener destroy_listener;
53 struct wl_listener output_created_listener;
54};
55
56struct cms_output {
57 CdDevice *device;
58 guint32 backlight_value;
59 struct cms_colord *cms;
60 struct weston_color_profile *p;
61 struct weston_output *o;
62 struct wl_listener destroy_listener;
63};
64
65static gint
66colord_idle_find_output_cb(gconstpointer a, gconstpointer b)
67{
68 struct cms_output *ocms = (struct cms_output *) a;
69 struct weston_output *o = (struct weston_output *) b;
70 return ocms->o == o ? 0 : -1;
71}
72
73static void
74colord_idle_cancel_for_output(struct cms_colord *cms, struct weston_output *o)
75{
76 GList *l;
77
78 /* cancel and remove any helpers that match the output */
79 g_mutex_lock(&cms->pending_mutex);
80 l = g_list_find_custom (cms->pending, o, colord_idle_find_output_cb);
81 if (l) {
82 struct cms_output *ocms = l->data;
83 cms->pending = g_list_remove (cms->pending, ocms);
84 }
85 g_mutex_unlock(&cms->pending_mutex);
86}
87
Derek Foreman280e7dd2014-10-03 13:13:42 -050088static bool
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010089edid_value_valid(const char *str)
90{
91 if (str == NULL)
Derek Foreman280e7dd2014-10-03 13:13:42 -050092 return false;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010093 if (str[0] == '\0')
Derek Foreman280e7dd2014-10-03 13:13:42 -050094 return false;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010095 if (strcmp(str, "unknown") == 0)
Derek Foreman280e7dd2014-10-03 13:13:42 -050096 return false;
97 return true;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010098}
99
100static gchar *
101get_output_id(struct cms_colord *cms, struct weston_output *o)
102{
103 const gchar *tmp;
104 GString *device_id;
105
106 /* see https://github.com/hughsie/colord/blob/master/doc/device-and-profile-naming-spec.txt
107 * for format and allowed values */
108 device_id = g_string_new("xrandr");
109 if (edid_value_valid(o->make)) {
110 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
111 if (tmp == NULL)
112 tmp = o->make;
113 g_string_append_printf(device_id, "-%s", tmp);
114 }
115 if (edid_value_valid(o->model))
116 g_string_append_printf(device_id, "-%s", o->model);
117 if (edid_value_valid(o->serial_number))
118 g_string_append_printf(device_id, "-%s", o->serial_number);
119
120 /* no EDID data, so use fallback */
121 if (strcmp(device_id->str, "xrandr") == 0)
122 g_string_append_printf(device_id, "-drm-%i", o->id);
123
124 return g_string_free(device_id, FALSE);
125}
126
127static void
128update_device_with_profile_in_idle(struct cms_output *ocms)
129{
130 gboolean signal_write = FALSE;
Richard Hughesd5616872013-05-15 09:17:38 +0100131 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100132 struct cms_colord *cms = ocms->cms;
133
134 colord_idle_cancel_for_output(cms, ocms->o);
135 g_mutex_lock(&cms->pending_mutex);
136 if (cms->pending == NULL)
137 signal_write = TRUE;
138 cms->pending = g_list_prepend(cms->pending, ocms);
139 g_mutex_unlock(&cms->pending_mutex);
140
141 /* signal we've got updates to do */
142 if (signal_write) {
143 gchar tmp = '\0';
Richard Hughesd5616872013-05-15 09:17:38 +0100144 rc = write(cms->writefd, &tmp, 1);
145 if (rc == 0)
Chris Michaelc262b4a2015-10-01 10:51:31 -0400146 weston_log("colord: failed to write to pending fd\n");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100147 }
148}
149
150static void
151colord_update_output_from_device (struct cms_output *ocms)
152{
153 CdProfile *profile;
154 const gchar *tmp;
155 gboolean ret;
156 GError *error = NULL;
157 gint percentage;
158
159 /* old profile is no longer valid */
160 weston_cms_destroy_profile(ocms->p);
161 ocms->p = NULL;
162
163 ret = cd_device_connect_sync(ocms->device, NULL, &error);
164 if (!ret) {
165 weston_log("colord: failed to connect to device %s: %s\n",
166 cd_device_get_object_path (ocms->device),
167 error->message);
168 g_error_free(error);
169 goto out;
170 }
171 profile = cd_device_get_default_profile(ocms->device);
172 if (!profile) {
173 weston_log("colord: no assigned color profile for %s\n",
174 cd_device_get_id (ocms->device));
175 goto out;
176 }
177 ret = cd_profile_connect_sync(profile, NULL, &error);
178 if (!ret) {
179 weston_log("colord: failed to connect to profile %s: %s\n",
180 cd_profile_get_object_path (profile),
181 error->message);
182 g_error_free(error);
183 goto out;
184 }
185
186 /* get the calibration brightness level (only set for some profiles) */
187 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
188 if (tmp != NULL) {
189 percentage = atoi(tmp);
190 if (percentage > 0 && percentage <= 100)
191 ocms->backlight_value = percentage * 255 / 100;
192 }
193
194 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
195 if (ocms->p == NULL) {
196 weston_log("colord: warning failed to load profile %s: %s\n",
197 cd_profile_get_object_path (profile),
198 error->message);
199 g_error_free(error);
200 goto out;
201 }
202out:
203 update_device_with_profile_in_idle(ocms);
204}
205
206static void
207colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
208{
209 weston_log("colord: device %s changed, update output\n",
210 cd_device_get_object_path (ocms->device));
211 colord_update_output_from_device(ocms);
212}
213
214static void
215colord_notifier_output_destroy(struct wl_listener *listener, void *data)
216{
Olivier Fourdan2e710e52015-01-08 15:40:29 +0100217 struct cms_output *ocms =
218 container_of(listener, struct cms_output, destroy_listener);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100219 struct weston_output *o = (struct weston_output *) data;
Olivier Fourdan2e710e52015-01-08 15:40:29 +0100220 struct cms_colord *cms = ocms->cms;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100221 gchar *device_id;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100222
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100223 device_id = get_output_id(cms, o);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100224 g_hash_table_remove (cms->devices, device_id);
225 g_free (device_id);
226}
227
228static void
229colord_output_created(struct cms_colord *cms, struct weston_output *o)
230{
231 CdDevice *device;
232 const gchar *tmp;
233 gchar *device_id;
234 GError *error = NULL;
235 GHashTable *device_props;
236 struct cms_output *ocms;
237
238 /* create device */
239 device_id = get_output_id(cms, o);
240 weston_log("colord: output added %s\n", device_id);
241 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
242 g_free, g_free);
243 g_hash_table_insert (device_props,
244 g_strdup(CD_DEVICE_PROPERTY_KIND),
245 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
246 g_hash_table_insert (device_props,
247 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
248 g_strdup("ColorModel.OutputMode.OutputResolution"));
249 g_hash_table_insert (device_props,
250 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
251 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
252 if (edid_value_valid(o->make)) {
253 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
254 if (tmp == NULL)
255 tmp = o->make;
256 g_hash_table_insert (device_props,
257 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
258 g_strdup(tmp));
259 }
260 if (edid_value_valid(o->model)) {
261 g_hash_table_insert (device_props,
262 g_strdup(CD_DEVICE_PROPERTY_MODEL),
263 g_strdup(o->model));
264 }
265 if (edid_value_valid(o->serial_number)) {
266 g_hash_table_insert (device_props,
267 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
268 g_strdup(o->serial_number));
269 }
270 if (o->connection_internal) {
271 g_hash_table_insert (device_props,
272 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
273 NULL);
274 }
275 device = cd_client_create_device_sync(cms->client,
276 device_id,
277 CD_OBJECT_SCOPE_TEMP,
278 device_props,
279 NULL,
280 &error);
281 if (g_error_matches (error,
282 CD_CLIENT_ERROR,
283 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
284 g_clear_error(&error);
285 device = cd_client_find_device_sync (cms->client,
286 device_id,
287 NULL,
288 &error);
289 }
290 if (!device) {
291 weston_log("colord: failed to create new or "
292 "find existing device: %s\n",
293 error->message);
294 g_error_free(error);
295 goto out;
296 }
297
298 /* create object and watch for the output to be destroyed */
299 ocms = g_slice_new0(struct cms_output);
300 ocms->cms = cms;
301 ocms->o = o;
302 ocms->device = g_object_ref(device);
303 ocms->destroy_listener.notify = colord_notifier_output_destroy;
304 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
305
306 /* add to local cache */
307 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
308 g_signal_connect (ocms->device, "changed",
309 G_CALLBACK (colord_device_changed_cb), ocms);
310
311 /* get profiles */
312 colord_update_output_from_device (ocms);
313out:
314 g_hash_table_unref (device_props);
315 if (device)
316 g_object_unref (device);
317 g_free (device_id);
318}
319
320static void
321colord_notifier_output_created(struct wl_listener *listener, void *data)
322{
323 struct weston_output *o = (struct weston_output *) data;
324 struct cms_colord *cms =
325 container_of(listener, struct cms_colord, destroy_listener);
326 weston_log("colord: output %s created\n", o->name);
327 colord_output_created(cms, o);
328}
329
330static gpointer
331colord_run_loop_thread(gpointer data)
332{
333 struct cms_colord *cms = (struct cms_colord *) data;
334 struct weston_output *o;
335
336 /* coldplug outputs */
337 wl_list_for_each(o, &cms->ec->output_list, link) {
338 weston_log("colord: output %s coldplugged\n", o->name);
339 colord_output_created(cms, o);
340 }
341
342 g_main_loop_run(cms->loop);
343 return NULL;
344}
345
346static int
347colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
348{
349 gchar tmp;
350 GList *l;
Richard Hughesd5616872013-05-15 09:17:38 +0100351 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100352 struct cms_colord *cms = data;
353 struct cms_output *ocms;
354
355 weston_log("colord: dispatching events\n");
356 g_mutex_lock(&cms->pending_mutex);
357 for (l = cms->pending; l != NULL; l = l->next) {
358 ocms = l->data;
359
360 /* optionally set backlight to calibration value */
361 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
362 weston_log("colord: profile calibration backlight to %i/255\n",
363 ocms->backlight_value);
364 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
365 }
366
367 weston_cms_set_color_profile(ocms->o, ocms->p);
368 }
369 g_list_free (cms->pending);
370 cms->pending = NULL;
371 g_mutex_unlock(&cms->pending_mutex);
372
373 /* done */
Richard Hughesd5616872013-05-15 09:17:38 +0100374 rc = read(cms->readfd, &tmp, 1);
375 if (rc == 0)
Chris Michaelc262b4a2015-10-01 10:51:31 -0400376 weston_log("colord: failed to read from pending fd\n");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100377 return 1;
378}
379
380static void
381colord_load_pnp_ids(struct cms_colord *cms)
382{
383 gboolean ret = FALSE;
384 gchar *tmp;
385 GError *error = NULL;
386 guint i;
387 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
388 "/usr/share/misc/pnp.ids",
389 NULL };
390
391 /* find and load file */
392 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
393 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
394 continue;
395 ret = g_file_get_contents(pnp_ids_fn[i],
396 &cms->pnp_ids_data,
397 NULL,
398 &error);
399 if (!ret) {
400 weston_log("colord: failed to load %s: %s\n",
401 pnp_ids_fn[i], error->message);
402 g_error_free(error);
403 return;
404 }
405 break;
406 }
407 if (!ret) {
408 weston_log("colord: no pnp.ids found\n");
409 return;
410 }
411
412 /* parse fixed offsets into lines */
413 tmp = cms->pnp_ids_data;
414 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
415 if (cms->pnp_ids_data[i] != '\n')
416 continue;
417 cms->pnp_ids_data[i] = '\0';
418 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
419 tmp[3] = '\0';
420 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
421 tmp = &cms->pnp_ids_data[i+1];
422 }
423 }
424}
425
426static void
427colord_module_destroy(struct cms_colord *cms)
428{
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100429 if (cms->loop) {
430 g_main_loop_quit(cms->loop);
431 g_main_loop_unref(cms->loop);
432 }
433 if (cms->thread)
434 g_thread_join(cms->thread);
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200435
436 /* cms->devices must be destroyed before other resources, as
437 * the other resources are needed during output cleanup in
438 * cms->devices unref.
439 */
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100440 if (cms->devices)
441 g_hash_table_unref(cms->devices);
442 if (cms->client)
443 g_object_unref(cms->client);
444 if (cms->readfd)
445 close(cms->readfd);
446 if (cms->writefd)
447 close(cms->writefd);
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200448
449 g_free(cms->pnp_ids_data);
450 g_hash_table_unref(cms->pnp_ids);
451
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100452 free(cms);
453}
454
455static void
456colord_notifier_destroy(struct wl_listener *listener, void *data)
457{
458 struct cms_colord *cms =
459 container_of(listener, struct cms_colord, destroy_listener);
460 colord_module_destroy(cms);
461}
462
463static void
464colord_cms_output_destroy(gpointer data)
465{
466 struct cms_output *ocms = (struct cms_output *) data;
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200467 struct cms_colord *cms = ocms->cms;
468 struct weston_output *o = ocms->o;
469 gboolean ret;
470 gchar *device_id;
471 GError *error = NULL;
472
473 colord_idle_cancel_for_output(cms, o);
474 device_id = get_output_id(cms, o);
475 weston_log("colord: output unplugged %s\n", device_id);
476
477 wl_list_remove(&ocms->destroy_listener.link);
478 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
479
480 ret = cd_client_delete_device_sync (cms->client,
481 ocms->device,
482 NULL,
483 &error);
484
485 if (!ret) {
486 weston_log("colord: failed to delete device: %s\n",
487 error->message);
488 g_error_free(error);
489 }
490
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100491 g_object_unref(ocms->device);
492 g_slice_free(struct cms_output, ocms);
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200493 g_free (device_id);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100494}
495
496WL_EXPORT int
497module_init(struct weston_compositor *ec,
Richard Hughes2379a652013-05-15 09:17:37 +0100498 int *argc, char *argv[])
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100499{
500 gboolean ret;
501 GError *error = NULL;
502 int fd[2];
503 struct cms_colord *cms;
504 struct wl_event_loop *loop;
505
506 weston_log("colord: initialized\n");
507
508 /* create local state object */
Peter Huttererf3d62272013-08-08 11:57:05 +1000509 cms = zalloc(sizeof *cms);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100510 if (cms == NULL)
511 return -1;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100512 cms->ec = ec;
513#if !GLIB_CHECK_VERSION(2,36,0)
514 g_type_init();
515#endif
516 cms->client = cd_client_new();
517 ret = cd_client_connect_sync(cms->client, NULL, &error);
518 if (!ret) {
519 weston_log("colord: failed to contact daemon: %s\n", error->message);
520 g_error_free(error);
521 colord_module_destroy(cms);
522 return -1;
523 }
524 g_mutex_init(&cms->pending_mutex);
525 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
526 g_free, colord_cms_output_destroy);
527
528 /* destroy */
529 cms->destroy_listener.notify = colord_notifier_destroy;
530 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
531
532 /* devices added */
533 cms->output_created_listener.notify = colord_notifier_output_created;
534 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
535
536 /* add all the PNP IDs */
537 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
538 g_str_equal,
539 NULL,
540 NULL);
541 colord_load_pnp_ids(cms);
542
543 /* setup a thread for the GLib callbacks */
544 cms->loop = g_main_loop_new(NULL, FALSE);
545 cms->thread = g_thread_new("colord CMS main loop",
546 colord_run_loop_thread, cms);
547
548 /* batch device<->profile updates */
549 if (pipe2(fd, O_CLOEXEC) == -1) {
550 colord_module_destroy(cms);
551 return -1;
552 }
553 cms->readfd = fd[0];
554 cms->writefd = fd[1];
555 loop = wl_display_get_event_loop(ec->wl_display);
556 cms->source = wl_event_loop_add_fd (loop,
557 cms->readfd,
558 WL_EVENT_READABLE,
559 colord_dispatch_all_pending,
560 cms);
561 if (!cms->source) {
562 colord_module_destroy(cms);
563 return -1;
564 }
565 return 0;
566}