1 /*
2 * Copyright © 2013 Richard Hughes
3 *
4 * 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:
11 *
12 * 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.
24 */
25
26 #include "config.h"
27
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <colord.h>
35
36 #include <libweston/libweston.h>
37 #include "weston.h"
38 #include "cms-helper.h"
39 #include "shared/helpers.h"
40
41 struct 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
58 struct 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
67 static gint
colord_idle_find_output_cb(gconstpointer a,gconstpointer b)68 colord_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
75 static void
colord_idle_cancel_for_output(struct cms_colord * cms,struct weston_output * o)76 colord_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
90 static bool
edid_value_valid(const char * str)91 edid_value_valid(const char *str)
92 {
93 if (str == NULL)
94 return false;
95 if (str[0] == '\0')
96 return false;
97 if (strcmp(str, "unknown") == 0)
98 return false;
99 return true;
100 }
101
102 static gchar *
get_output_id(struct cms_colord * cms,struct weston_output * o)103 get_output_id(struct cms_colord *cms, struct weston_output *o)
104 {
105 struct weston_head *head;
106 const gchar *tmp;
107 GString *device_id;
108
109 /* XXX: What to do with multiple heads?
110 * This is potentially unstable, if head configuration is changed
111 * while the output is enabled. */
112 head = weston_output_get_first_head(o);
113
114 if (wl_list_length(&o->head_list) > 1) {
115 weston_log("colord: WARNING: multiple heads are not supported (output %s).\n",
116 o->name);
117 }
118
119 /* see https://github.com/hughsie/colord/blob/master/doc/device-and-profile-naming-spec.txt
120 * for format and allowed values */
121 device_id = g_string_new("xrandr");
122 if (edid_value_valid(head->make)) {
123 tmp = g_hash_table_lookup(cms->pnp_ids, head->make);
124 if (tmp == NULL)
125 tmp = head->make;
126 g_string_append_printf(device_id, "-%s", tmp);
127 }
128 if (edid_value_valid(head->model))
129 g_string_append_printf(device_id, "-%s", head->model);
130 if (edid_value_valid(head->serial_number))
131 g_string_append_printf(device_id, "-%s", head->serial_number);
132
133 /* no EDID data, so use fallback */
134 if (strcmp(device_id->str, "xrandr") == 0)
135 g_string_append_printf(device_id, "-drm-%i", o->id);
136
137 return g_string_free(device_id, FALSE);
138 }
139
140 static void
update_device_with_profile_in_idle(struct cms_output * ocms)141 update_device_with_profile_in_idle(struct cms_output *ocms)
142 {
143 gboolean signal_write = FALSE;
144 ssize_t rc;
145 struct cms_colord *cms = ocms->cms;
146
147 colord_idle_cancel_for_output(cms, ocms->o);
148 g_mutex_lock(&cms->pending_mutex);
149 if (cms->pending == NULL)
150 signal_write = TRUE;
151 cms->pending = g_list_prepend(cms->pending, ocms);
152 g_mutex_unlock(&cms->pending_mutex);
153
154 /* signal we've got updates to do */
155 if (signal_write) {
156 gchar tmp = '\0';
157 rc = write(cms->writefd, &tmp, 1);
158 if (rc == 0)
159 weston_log("colord: failed to write to pending fd\n");
160 }
161 }
162
163 static void
colord_update_output_from_device(struct cms_output * ocms)164 colord_update_output_from_device (struct cms_output *ocms)
165 {
166 CdProfile *profile;
167 const gchar *tmp;
168 gboolean ret;
169 GError *error = NULL;
170 gint percentage;
171
172 /* old profile is no longer valid */
173 weston_cms_destroy_profile(ocms->p);
174 ocms->p = NULL;
175
176 ret = cd_device_connect_sync(ocms->device, NULL, &error);
177 if (!ret) {
178 weston_log("colord: failed to connect to device %s: %s\n",
179 cd_device_get_object_path (ocms->device),
180 error->message);
181 g_error_free(error);
182 goto out;
183 }
184 profile = cd_device_get_default_profile(ocms->device);
185 if (!profile) {
186 weston_log("colord: no assigned color profile for %s\n",
187 cd_device_get_id (ocms->device));
188 goto out;
189 }
190 ret = cd_profile_connect_sync(profile, NULL, &error);
191 if (!ret) {
192 weston_log("colord: failed to connect to profile %s: %s\n",
193 cd_profile_get_object_path (profile),
194 error->message);
195 g_error_free(error);
196 goto out;
197 }
198
199 /* get the calibration brightness level (only set for some profiles) */
200 tmp = cd_profile_get_metadata_item(profile, CD_PROFILE_METADATA_SCREEN_BRIGHTNESS);
201 if (tmp != NULL) {
202 percentage = atoi(tmp);
203 if (percentage > 0 && percentage <= 100)
204 ocms->backlight_value = percentage * 255 / 100;
205 }
206
207 ocms->p = weston_cms_load_profile(cd_profile_get_filename(profile));
208 if (ocms->p == NULL) {
209 weston_log("colord: warning failed to load profile %s: %s\n",
210 cd_profile_get_object_path (profile),
211 error->message);
212 g_error_free(error);
213 goto out;
214 }
215 out:
216 update_device_with_profile_in_idle(ocms);
217 }
218
219 static void
colord_device_changed_cb(CdDevice * device,struct cms_output * ocms)220 colord_device_changed_cb(CdDevice *device, struct cms_output *ocms)
221 {
222 weston_log("colord: device %s changed, update output\n",
223 cd_device_get_object_path (ocms->device));
224 colord_update_output_from_device(ocms);
225 }
226
227 static void
colord_notifier_output_destroy(struct wl_listener * listener,void * data)228 colord_notifier_output_destroy(struct wl_listener *listener, void *data)
229 {
230 struct cms_output *ocms =
231 container_of(listener, struct cms_output, destroy_listener);
232 struct weston_output *o = (struct weston_output *) data;
233 struct cms_colord *cms = ocms->cms;
234 gchar *device_id;
235
236 device_id = get_output_id(cms, o);
237 g_hash_table_remove (cms->devices, device_id);
238 g_free (device_id);
239 }
240
241 static void
colord_output_created(struct cms_colord * cms,struct weston_output * o)242 colord_output_created(struct cms_colord *cms, struct weston_output *o)
243 {
244 struct weston_head *head;
245 CdDevice *device;
246 const gchar *tmp;
247 gchar *device_id;
248 GError *error = NULL;
249 GHashTable *device_props;
250 struct cms_output *ocms;
251
252 /* XXX: What to do with multiple heads? */
253 head = weston_output_get_first_head(o);
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(head->make)) {
270 tmp = g_hash_table_lookup(cms->pnp_ids, head->make);
271 if (tmp == NULL)
272 tmp = head->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(head->model)) {
278 g_hash_table_insert (device_props,
279 g_strdup(CD_DEVICE_PROPERTY_MODEL),
280 g_strdup(head->model));
281 }
282 if (edid_value_valid(head->serial_number)) {
283 g_hash_table_insert (device_props,
284 g_strdup(CD_DEVICE_PROPERTY_SERIAL),
285 g_strdup(head->serial_number));
286 }
287 if (head->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);
330 out:
331 g_hash_table_unref (device_props);
332 if (device)
333 g_object_unref (device);
334 g_free (device_id);
335 }
336
337 static void
colord_notifier_output_created(struct wl_listener * listener,void * data)338 colord_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
347 static gpointer
colord_run_loop_thread(gpointer data)348 colord_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
363 static int
colord_dispatch_all_pending(int fd,uint32_t mask,void * data)364 colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
365 {
366 gchar tmp;
367 GList *l;
368 ssize_t rc;
369 struct cms_colord *cms = data;
370 struct cms_output *ocms;
371
372 weston_log("colord: dispatching events\n");
373 g_mutex_lock(&cms->pending_mutex);
374 for (l = cms->pending; l != NULL; l = l->next) {
375 ocms = l->data;
376
377 /* optionally set backlight to calibration value */
378 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
379 weston_log("colord: profile calibration backlight to %i/255\n",
380 ocms->backlight_value);
381 ocms->o->set_backlight(ocms->o, ocms->backlight_value);
382 }
383
384 weston_cms_set_color_profile(ocms->o, ocms->p);
385 }
386 g_list_free (cms->pending);
387 cms->pending = NULL;
388 g_mutex_unlock(&cms->pending_mutex);
389
390 /* done */
391 rc = read(cms->readfd, &tmp, 1);
392 if (rc == 0)
393 weston_log("colord: failed to read from pending fd\n");
394 return 1;
395 }
396
397 static void
colord_load_pnp_ids(struct cms_colord * cms)398 colord_load_pnp_ids(struct cms_colord *cms)
399 {
400 gboolean ret = FALSE;
401 gchar *tmp;
402 GError *error = NULL;
403 guint i;
404 const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
405 "/usr/share/misc/pnp.ids",
406 NULL };
407
408 /* find and load file */
409 for (i = 0; pnp_ids_fn[i] != NULL; i++) {
410 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
411 continue;
412 ret = g_file_get_contents(pnp_ids_fn[i],
413 &cms->pnp_ids_data,
414 NULL,
415 &error);
416 if (!ret) {
417 weston_log("colord: failed to load %s: %s\n",
418 pnp_ids_fn[i], error->message);
419 g_error_free(error);
420 return;
421 }
422 break;
423 }
424 if (!ret) {
425 weston_log("colord: no pnp.ids found\n");
426 return;
427 }
428
429 /* parse fixed offsets into lines */
430 tmp = cms->pnp_ids_data;
431 for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
432 if (cms->pnp_ids_data[i] != '\n')
433 continue;
434 cms->pnp_ids_data[i] = '\0';
435 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
436 tmp[3] = '\0';
437 g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
438 tmp = &cms->pnp_ids_data[i+1];
439 }
440 }
441 }
442
443 static void
colord_module_destroy(struct cms_colord * cms)444 colord_module_destroy(struct cms_colord *cms)
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
453 /* cms->devices must be destroyed before other resources, as
454 * the other resources are needed during output cleanup in
455 * cms->devices unref.
456 */
457 if (cms->devices)
458 g_hash_table_unref(cms->devices);
459 if (cms->client)
460 g_object_unref(cms->client);
461 if (cms->readfd)
462 close(cms->readfd);
463 if (cms->writefd)
464 close(cms->writefd);
465
466 g_free(cms->pnp_ids_data);
467 g_hash_table_unref(cms->pnp_ids);
468
469 wl_list_remove(&cms->destroy_listener.link);
470 free(cms);
471 }
472
473 static void
colord_notifier_destroy(struct wl_listener * listener,void * data)474 colord_notifier_destroy(struct wl_listener *listener, void *data)
475 {
476 struct cms_colord *cms =
477 container_of(listener, struct cms_colord, destroy_listener);
478 colord_module_destroy(cms);
479 }
480
481 static void
colord_cms_output_destroy(gpointer data)482 colord_cms_output_destroy(gpointer data)
483 {
484 struct cms_output *ocms = (struct cms_output *) data;
485 struct cms_colord *cms = ocms->cms;
486 struct weston_output *o = ocms->o;
487 gboolean ret;
488 gchar *device_id;
489 GError *error = NULL;
490
491 colord_idle_cancel_for_output(cms, o);
492 device_id = get_output_id(cms, o);
493 weston_log("colord: output unplugged %s\n", device_id);
494
495 wl_list_remove(&ocms->destroy_listener.link);
496 g_signal_handlers_disconnect_by_data(ocms->device, ocms);
497
498 ret = cd_client_delete_device_sync (cms->client,
499 ocms->device,
500 NULL,
501 &error);
502
503 if (!ret) {
504 weston_log("colord: failed to delete device: %s\n",
505 error->message);
506 g_error_free(error);
507 }
508
509 g_object_unref(ocms->device);
510 g_slice_free(struct cms_output, ocms);
511 g_free (device_id);
512 }
513
514 WL_EXPORT int
wet_module_init(struct weston_compositor * ec,int * argc,char * argv[])515 wet_module_init(struct weston_compositor *ec,
516 int *argc, char *argv[])
517 {
518 gboolean ret;
519 GError *error = NULL;
520 int fd[2];
521 struct cms_colord *cms;
522 struct wl_event_loop *loop;
523
524 weston_log("colord: initialized\n");
525
526 /* create local state object */
527 cms = zalloc(sizeof *cms);
528 if (cms == NULL)
529 return -1;
530 cms->ec = ec;
531
532 if (!weston_compositor_add_destroy_listener_once(ec,
533 &cms->destroy_listener,
534 colord_notifier_destroy)) {
535 free(cms);
536 return 0;
537 }
538
539 #if !GLIB_CHECK_VERSION(2,36,0)
540 g_type_init();
541 #endif
542 cms->client = cd_client_new();
543 ret = cd_client_connect_sync(cms->client, NULL, &error);
544 if (!ret) {
545 weston_log("colord: failed to contact daemon: %s\n", error->message);
546 g_error_free(error);
547 colord_module_destroy(cms);
548 return -1;
549 }
550 g_mutex_init(&cms->pending_mutex);
551 cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
552 g_free, colord_cms_output_destroy);
553
554 /* devices added */
555 cms->output_created_listener.notify = colord_notifier_output_created;
556 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
557
558 /* add all the PNP IDs */
559 cms->pnp_ids = g_hash_table_new_full(g_str_hash,
560 g_str_equal,
561 NULL,
562 NULL);
563 colord_load_pnp_ids(cms);
564
565 /* setup a thread for the GLib callbacks */
566 cms->loop = g_main_loop_new(NULL, FALSE);
567 cms->thread = g_thread_new("colord CMS main loop",
568 colord_run_loop_thread, cms);
569
570 /* batch device<->profile updates */
571 if (pipe2(fd, O_CLOEXEC) == -1) {
572 colord_module_destroy(cms);
573 return -1;
574 }
575 cms->readfd = fd[0];
576 cms->writefd = fd[1];
577 loop = wl_display_get_event_loop(ec->wl_display);
578 cms->source = wl_event_loop_add_fd (loop,
579 cms->readfd,
580 WL_EVENT_READABLE,
581 colord_dispatch_all_pending,
582 cms);
583 if (!cms->source) {
584 colord_module_destroy(cms);
585 return -1;
586 }
587 return 0;
588 }
589