blob: 33f23b27db78cd6442a62e733e8d34b9535962a8 [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;
130 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';
142 write(cms->writefd, &tmp, 1);
143 }
144}
145
146static void
147colord_update_output_from_device (struct cms_output *ocms)
148{
149 CdProfile *profile;
150 const gchar *tmp;
151 gboolean ret;
152 GError *error = NULL;
153 gint percentage;
154
155 /* old profile is no longer valid */
156 weston_cms_destroy_profile(ocms->p);
157 ocms->p = NULL;
158
159 ret = cd_device_connect_sync(ocms->device, NULL, &error);
160 if (!ret) {
161 weston_log("colord: failed to connect to device %s: %s\n",
162 cd_device_get_object_path (ocms->device),
163 error->message);
164 g_error_free(error);
165 goto out;
166 }
167 profile = cd_device_get_default_profile(ocms->device);
168 if (!profile) {
169 weston_log("colord: no assigned color profile for %s\n",
170 cd_device_get_id (ocms->device));
171 goto out;
172 }
173 ret = cd_profile_connect_sync(profile, NULL, &error);
174 if (!ret) {
175 weston_log("colord: failed to connect to profile %s: %s\n",
176 cd_profile_get_object_path (profile),
177 error->message);
178 g_error_free(error);
179 goto out;
180 }
181
182 /* get the calibration brightness level (only set for some profiles) */
183 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
184 if (tmp != NULL) {
185 percentage = atoi(tmp);
186 if (percentage > 0 && percentage <= 100)
187 ocms->backlight_value = percentage * 255 / 100;
188 }
189
190 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
191 if (ocms->p == NULL) {
192 weston_log("colord: warning failed to load profile %s: %s\n",
193 cd_profile_get_object_path (profile),
194 error->message);
195 g_error_free(error);
196 goto out;
197 }
198out:
199 update_device_with_profile_in_idle(ocms);
200}
201
202static void
203colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
204{
205 weston_log("colord: device %s changed, update output\n",
206 cd_device_get_object_path (ocms->device));
207 colord_update_output_from_device(ocms);
208}
209
210static void
211colord_notifier_output_destroy(struct wl_listener *listener, void *data)
212{
213 struct cms_colord *cms =
214 container_of(listener, struct cms_colord, destroy_listener);
215 struct weston_output *o = (struct weston_output *) data;
216 struct cms_output *ocms;
217 gboolean ret;
218 gchar *device_id;
219 GError *error = NULL;
220
221 colord_idle_cancel_for_output(cms, o);
222 device_id = get_output_id(cms, o);
223 weston_log("colord: output removed %s\n", device_id);
224 ocms = g_hash_table_lookup(cms->devices, device_id);
225 if (!ocms) {
226 weston_log("colord: failed to delete device\n");
227 goto out;
228 }
229 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
230 ret = cd_client_delete_device_sync (cms->client,
231 ocms->device,
232 NULL,
233 &error);
234
235 if (!ret) {
236 weston_log("colord: failed to delete device: %s\n", error->message);
237 g_error_free(error);
238 goto out;
239 }
240out:
241 g_hash_table_remove (cms->devices, device_id);
242 g_free (device_id);
243}
244
245static void
246colord_output_created(struct cms_colord *cms, struct weston_output *o)
247{
248 CdDevice *device;
249 const gchar *tmp;
250 gchar *device_id;
251 GError *error = NULL;
252 GHashTable *device_props;
253 struct cms_output *ocms;
254
255 /* create device */
256 device_id = get_output_id(cms, o);
257 weston_log("colord: output added %s\n", device_id);
258 device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
259 g_free, g_free);
260 g_hash_table_insert (device_props,
261 g_strdup(CD_DEVICE_PROPERTY_KIND),
262 g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
263 g_hash_table_insert (device_props,
264 g_strdup(CD_DEVICE_PROPERTY_FORMAT),
265 g_strdup("ColorModel.OutputMode.OutputResolution"));
266 g_hash_table_insert (device_props,
267 g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
268 g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
269 if (edid_value_valid(o->make)) {
270 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
271 if (tmp == NULL)
272 tmp = o->make;
273 g_hash_table_insert (device_props,
274 g_strdup(CD_DEVICE_PROPERTY_VENDOR),
275 g_strdup(tmp));
276 }
277 if (edid_value_valid(o->model)) {
278 g_hash_table_insert (device_props,
279 g_strdup(CD_DEVICE_PROPERTY_MODEL),
280 g_strdup(o->model));
281 }
282 if (edid_value_valid(o->serial_number)) {
283 g_hash_table_insert (device_props,
284 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
285 g_strdup(o->serial_number));
286 }
287 if (o->connection_internal) {
288 g_hash_table_insert (device_props,
289 g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
290 NULL);
291 }
292 device = cd_client_create_device_sync(cms->client,
293 device_id,
294 CD_OBJECT_SCOPE_TEMP,
295 device_props,
296 NULL,
297 &error);
298 if (g_error_matches (error,
299 CD_CLIENT_ERROR,
300 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
301 g_clear_error(&error);
302 device = cd_client_find_device_sync (cms->client,
303 device_id,
304 NULL,
305 &error);
306 }
307 if (!device) {
308 weston_log("colord: failed to create new or "
309 "find existing device: %s\n",
310 error->message);
311 g_error_free(error);
312 goto out;
313 }
314
315 /* create object and watch for the output to be destroyed */
316 ocms = g_slice_new0(struct cms_output);
317 ocms->cms = cms;
318 ocms->o = o;
319 ocms->device = g_object_ref(device);
320 ocms->destroy_listener.notify = colord_notifier_output_destroy;
321 wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
322
323 /* add to local cache */
324 g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
325 g_signal_connect (ocms->device, "changed",
326 G_CALLBACK (colord_device_changed_cb), ocms);
327
328 /* get profiles */
329 colord_update_output_from_device (ocms);
330out:
331 g_hash_table_unref (device_props);
332 if (device)
333 g_object_unref (device);
334 g_free (device_id);
335}
336
337static void
338colord_notifier_output_created(struct wl_listener *listener, void *data)
339{
340 struct weston_output *o = (struct weston_output *) data;
341 struct cms_colord *cms =
342 container_of(listener, struct cms_colord, destroy_listener);
343 weston_log("colord: output %s created\n", o->name);
344 colord_output_created(cms, o);
345}
346
347static gpointer
348colord_run_loop_thread(gpointer data)
349{
350 struct cms_colord *cms = (struct cms_colord *) data;
351 struct weston_output *o;
352
353 /* coldplug outputs */
354 wl_list_for_each(o, &cms->ec->output_list, link) {
355 weston_log("colord: output %s coldplugged\n", o->name);
356 colord_output_created(cms, o);
357 }
358
359 g_main_loop_run(cms->loop);
360 return NULL;
361}
362
363static int
364colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
365{
366 gchar tmp;
367 GList *l;
368 struct cms_colord *cms = data;
369 struct cms_output *ocms;
370
371 weston_log("colord: dispatching events\n");
372 g_mutex_lock(&cms->pending_mutex);
373 for (l = cms->pending; l != NULL; l = l->next) {
374 ocms = l->data;
375
376 /* optionally set backlight to calibration value */
377 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
378 weston_log("colord: profile calibration backlight to %i/255\n",
379 ocms->backlight_value);
380 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
381 }
382
383 weston_cms_set_color_profile(ocms->o, ocms->p);
384 }
385 g_list_free (cms->pending);
386 cms->pending = NULL;
387 g_mutex_unlock(&cms->pending_mutex);
388
389 /* done */
390 read(cms->readfd, &tmp, 1);
391 return 1;
392}
393
394static void
395colord_load_pnp_ids(struct cms_colord *cms)
396{
397 gboolean ret = FALSE;
398 gchar *tmp;
399 GError *error = NULL;
400 guint i;
401 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
402 "/usr/share/misc/pnp.ids",
403 NULL };
404
405 /* find and load file */
406 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
407 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
408 continue;
409 ret = g_file_get_contents(pnp_ids_fn[i],
410 &cms->pnp_ids_data,
411 NULL,
412 &error);
413 if (!ret) {
414 weston_log("colord: failed to load %s: %s\n",
415 pnp_ids_fn[i], error->message);
416 g_error_free(error);
417 return;
418 }
419 break;
420 }
421 if (!ret) {
422 weston_log("colord: no pnp.ids found\n");
423 return;
424 }
425
426 /* parse fixed offsets into lines */
427 tmp = cms->pnp_ids_data;
428 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
429 if (cms->pnp_ids_data[i] != '\n')
430 continue;
431 cms->pnp_ids_data[i] = '\0';
432 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
433 tmp[3] = '\0';
434 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
435 tmp = &cms->pnp_ids_data[i+1];
436 }
437 }
438}
439
440static void
441colord_module_destroy(struct cms_colord *cms)
442{
443 g_free(cms->pnp_ids_data);
444 g_hash_table_unref(cms->pnp_ids);
445
446 if (cms->loop) {
447 g_main_loop_quit(cms->loop);
448 g_main_loop_unref(cms->loop);
449 }
450 if (cms->thread)
451 g_thread_join(cms->thread);
452 if (cms->devices)
453 g_hash_table_unref(cms->devices);
454 if (cms->client)
455 g_object_unref(cms->client);
456 if (cms->readfd)
457 close(cms->readfd);
458 if (cms->writefd)
459 close(cms->writefd);
460 free(cms);
461}
462
463static void
464colord_notifier_destroy(struct wl_listener *listener, void *data)
465{
466 struct cms_colord *cms =
467 container_of(listener, struct cms_colord, destroy_listener);
468 colord_module_destroy(cms);
469}
470
471static void
472colord_cms_output_destroy(gpointer data)
473{
474 struct cms_output *ocms = (struct cms_output *) data;
475 g_object_unref(ocms->device);
476 g_slice_free(struct cms_output, ocms);
477}
478
479WL_EXPORT int
480module_init(struct weston_compositor *ec,
481 int *argc, char *argv[], const char *config_file)
482{
483 gboolean ret;
484 GError *error = NULL;
485 int fd[2];
486 struct cms_colord *cms;
487 struct wl_event_loop *loop;
488
489 weston_log("colord: initialized\n");
490
491 /* create local state object */
492 cms = malloc(sizeof *cms);
493 if (cms == NULL)
494 return -1;
495 memset(cms, 0, sizeof *cms);
496 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}