blob: 0daa2a7e9f16f14e57a8dd3c2f163a7e618c950f [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>
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030030#include <stdint.h>
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <colord.h>
35
36#include "compositor.h"
Quentin Glidic3d7ca3b2016-12-02 14:20:35 +010037#include "weston.h"
Richard Hughesbe7c4dd2013-05-11 09:48:22 +010038#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)
Chris Michaelc262b4a2015-10-01 10:51:31 -0400148 weston_log("colord: failed to write to pending fd\n");
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;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100223 gchar *device_id;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100224
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100225 device_id = get_output_id(cms, o);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100226 g_hash_table_remove (cms->devices, device_id);
227 g_free (device_id);
228}
229
230static void
231colord_output_created(struct cms_colord *cms, struct weston_output *o)
232{
233 CdDevice *device;
234 const gchar *tmp;
235 gchar *device_id;
236 GError *error = NULL;
237 GHashTable *device_props;
238 struct cms_output *ocms;
239
240 /* create device */
241 device_id = get_output_id(cms, o);
242 weston_log("colord: output added %s\n", device_id);
243 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
244 g_free, g_free);
245 g_hash_table_insert (device_props,
246 g_strdup(CD_DEVICE_PROPERTY_KIND),
247 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
248 g_hash_table_insert (device_props,
249 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
250 g_strdup("ColorModel.OutputMode.OutputResolution"));
251 g_hash_table_insert (device_props,
252 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
253 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
254 if (edid_value_valid(o->make)) {
255 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
256 if (tmp == NULL)
257 tmp = o->make;
258 g_hash_table_insert (device_props,
259 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
260 g_strdup(tmp));
261 }
262 if (edid_value_valid(o->model)) {
263 g_hash_table_insert (device_props,
264 g_strdup(CD_DEVICE_PROPERTY_MODEL),
265 g_strdup(o->model));
266 }
267 if (edid_value_valid(o->serial_number)) {
268 g_hash_table_insert (device_props,
269 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
270 g_strdup(o->serial_number));
271 }
272 if (o->connection_internal) {
273 g_hash_table_insert (device_props,
274 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
275 NULL);
276 }
277 device = cd_client_create_device_sync(cms->client,
278 device_id,
279 CD_OBJECT_SCOPE_TEMP,
280 device_props,
281 NULL,
282 &error);
283 if (g_error_matches (error,
284 CD_CLIENT_ERROR,
285 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
286 g_clear_error(&error);
287 device = cd_client_find_device_sync (cms->client,
288 device_id,
289 NULL,
290 &error);
291 }
292 if (!device) {
293 weston_log("colord: failed to create new or "
294 "find existing device: %s\n",
295 error->message);
296 g_error_free(error);
297 goto out;
298 }
299
300 /* create object and watch for the output to be destroyed */
301 ocms = g_slice_new0(struct cms_output);
302 ocms->cms = cms;
303 ocms->o = o;
304 ocms->device = g_object_ref(device);
305 ocms->destroy_listener.notify = colord_notifier_output_destroy;
306 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
307
308 /* add to local cache */
309 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
310 g_signal_connect (ocms->device, "changed",
311 G_CALLBACK (colord_device_changed_cb), ocms);
312
313 /* get profiles */
314 colord_update_output_from_device (ocms);
315out:
316 g_hash_table_unref (device_props);
317 if (device)
318 g_object_unref (device);
319 g_free (device_id);
320}
321
322static void
323colord_notifier_output_created(struct wl_listener *listener, void *data)
324{
325 struct weston_output *o = (struct weston_output *) data;
326 struct cms_colord *cms =
327 container_of(listener, struct cms_colord, destroy_listener);
328 weston_log("colord: output %s created\n", o->name);
329 colord_output_created(cms, o);
330}
331
332static gpointer
333colord_run_loop_thread(gpointer data)
334{
335 struct cms_colord *cms = (struct cms_colord *) data;
336 struct weston_output *o;
337
338 /* coldplug outputs */
339 wl_list_for_each(o, &cms->ec->output_list, link) {
340 weston_log("colord: output %s coldplugged\n", o->name);
341 colord_output_created(cms, o);
342 }
343
344 g_main_loop_run(cms->loop);
345 return NULL;
346}
347
348static int
349colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
350{
351 gchar tmp;
352 GList *l;
Richard Hughesd5616872013-05-15 09:17:38 +0100353 ssize_t rc;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100354 struct cms_colord *cms = data;
355 struct cms_output *ocms;
356
357 weston_log("colord: dispatching events\n");
358 g_mutex_lock(&cms->pending_mutex);
359 for (l = cms->pending; l != NULL; l = l->next) {
360 ocms = l->data;
361
362 /* optionally set backlight to calibration value */
363 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
364 weston_log("colord: profile calibration backlight to %i/255\n",
365 ocms->backlight_value);
366 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
367 }
368
369 weston_cms_set_color_profile(ocms->o, ocms->p);
370 }
371 g_list_free (cms->pending);
372 cms->pending = NULL;
373 g_mutex_unlock(&cms->pending_mutex);
374
375 /* done */
Richard Hughesd5616872013-05-15 09:17:38 +0100376 rc = read(cms->readfd, &tmp, 1);
377 if (rc == 0)
Chris Michaelc262b4a2015-10-01 10:51:31 -0400378 weston_log("colord: failed to read from pending fd\n");
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100379 return 1;
380}
381
382static void
383colord_load_pnp_ids(struct cms_colord *cms)
384{
385 gboolean ret = FALSE;
386 gchar *tmp;
387 GError *error = NULL;
388 guint i;
389 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
390 "/usr/share/misc/pnp.ids",
391 NULL };
392
393 /* find and load file */
394 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
395 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
396 continue;
397 ret = g_file_get_contents(pnp_ids_fn[i],
398 &cms->pnp_ids_data,
399 NULL,
400 &error);
401 if (!ret) {
402 weston_log("colord: failed to load %s: %s\n",
403 pnp_ids_fn[i], error->message);
404 g_error_free(error);
405 return;
406 }
407 break;
408 }
409 if (!ret) {
410 weston_log("colord: no pnp.ids found\n");
411 return;
412 }
413
414 /* parse fixed offsets into lines */
415 tmp = cms->pnp_ids_data;
416 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
417 if (cms->pnp_ids_data[i] != '\n')
418 continue;
419 cms->pnp_ids_data[i] = '\0';
420 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
421 tmp[3] = '\0';
422 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
423 tmp = &cms->pnp_ids_data[i+1];
424 }
425 }
426}
427
428static void
429colord_module_destroy(struct cms_colord *cms)
430{
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100431 if (cms->loop) {
432 g_main_loop_quit(cms->loop);
433 g_main_loop_unref(cms->loop);
434 }
435 if (cms->thread)
436 g_thread_join(cms->thread);
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200437
438 /* cms->devices must be destroyed before other resources, as
439 * the other resources are needed during output cleanup in
440 * cms->devices unref.
441 */
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100442 if (cms->devices)
443 g_hash_table_unref(cms->devices);
444 if (cms->client)
445 g_object_unref(cms->client);
446 if (cms->readfd)
447 close(cms->readfd);
448 if (cms->writefd)
449 close(cms->writefd);
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200450
451 g_free(cms->pnp_ids_data);
452 g_hash_table_unref(cms->pnp_ids);
453
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100454 free(cms);
455}
456
457static void
458colord_notifier_destroy(struct wl_listener *listener, void *data)
459{
460 struct cms_colord *cms =
461 container_of(listener, struct cms_colord, destroy_listener);
462 colord_module_destroy(cms);
463}
464
465static void
466colord_cms_output_destroy(gpointer data)
467{
468 struct cms_output *ocms = (struct cms_output *) data;
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200469 struct cms_colord *cms = ocms->cms;
470 struct weston_output *o = ocms->o;
471 gboolean ret;
472 gchar *device_id;
473 GError *error = NULL;
474
475 colord_idle_cancel_for_output(cms, o);
476 device_id = get_output_id(cms, o);
477 weston_log("colord: output unplugged %s\n", device_id);
478
479 wl_list_remove(&ocms->destroy_listener.link);
480 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
481
482 ret = cd_client_delete_device_sync (cms->client,
483 ocms->device,
484 NULL,
485 &error);
486
487 if (!ret) {
488 weston_log("colord: failed to delete device: %s\n",
489 error->message);
490 g_error_free(error);
491 }
492
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100493 g_object_unref(ocms->device);
494 g_slice_free(struct cms_output, ocms);
Mario Kleiner2611ebd2015-07-18 08:07:11 +0200495 g_free (device_id);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100496}
497
498WL_EXPORT int
Quentin Glidic8af2bec2016-12-02 14:21:46 +0100499wet_module_init(struct weston_compositor *ec,
500 int *argc, char *argv[])
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100501{
502 gboolean ret;
503 GError *error = NULL;
504 int fd[2];
505 struct cms_colord *cms;
506 struct wl_event_loop *loop;
507
508 weston_log("colord: initialized\n");
509
510 /* create local state object */
Peter Huttererf3d62272013-08-08 11:57:05 +1000511 cms = zalloc(sizeof *cms);
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100512 if (cms == NULL)
513 return -1;
Richard Hughesbe7c4dd2013-05-11 09:48:22 +0100514 cms->ec = ec;
515#if !GLIB_CHECK_VERSION(2,36,0)
516 g_type_init();
517#endif
518 cms->client = cd_client_new();
519 ret = cd_client_connect_sync(cms->client, NULL, &error);
520 if (!ret) {
521 weston_log("colord: failed to contact daemon: %s\n", error->message);
522 g_error_free(error);
523 colord_module_destroy(cms);
524 return -1;
525 }
526 g_mutex_init(&cms->pending_mutex);
527 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
528 g_free, colord_cms_output_destroy);
529
530 /* destroy */
531 cms->destroy_listener.notify = colord_notifier_destroy;
532 wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
533
534 /* devices added */
535 cms->output_created_listener.notify = colord_notifier_output_created;
536 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
537
538 /* add all the PNP IDs */
539 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
540 g_str_equal,
541 NULL,
542 NULL);
543 colord_load_pnp_ids(cms);
544
545 /* setup a thread for the GLib callbacks */
546 cms->loop = g_main_loop_new(NULL, FALSE);
547 cms->thread = g_thread_new("colord CMS main loop",
548 colord_run_loop_thread, cms);
549
550 /* batch device<->profile updates */
551 if (pipe2(fd, O_CLOEXEC) == -1) {
552 colord_module_destroy(cms);
553 return -1;
554 }
555 cms->readfd = fd[0];
556 cms->writefd = fd[1];
557 loop = wl_display_get_event_loop(ec->wl_display);
558 cms->source = wl_event_loop_add_fd (loop,
559 cms->readfd,
560 WL_EVENT_READABLE,
561 colord_dispatch_all_pending,
562 cms);
563 if (!cms->source) {
564 colord_module_destroy(cms);
565 return -1;
566 }
567 return 0;
568}