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