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