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