compositor-drm: Work around page flip not setting tiling mode on BYT
[platform/upstream/weston.git] / src / cms-colord.c
1 /*
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 #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
37 struct 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
54 struct 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
63 static gint
64 colord_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
71 static void
72 colord_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
86 static int
87 edid_value_valid(const char *str)
88 {
89         if (str == NULL)
90                 return 0;
91         if (str[0] == '\0')
92                 return 0;
93         if (strcmp(str, "unknown") == 0)
94                 return 0;
95         return 1;
96 }
97
98 static gchar *
99 get_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
125 static void
126 update_device_with_profile_in_idle(struct cms_output *ocms)
127 {
128         gboolean signal_write = FALSE;
129         ssize_t rc;
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                 rc = write(cms->writefd, &tmp, 1);
143                 if (rc == 0)
144                         weston_log("colord: failed to write to pending fd");
145         }
146 }
147
148 static void
149 colord_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         }
200 out:
201         update_device_with_profile_in_idle(ocms);
202 }
203
204 static void
205 colord_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
212 static void
213 colord_notifier_output_destroy(struct wl_listener *listener, void *data)
214 {
215         struct cms_colord *cms =
216                 container_of(listener, struct cms_colord, destroy_listener);
217         struct weston_output *o = (struct weston_output *) data;
218         struct cms_output *ocms;
219         gboolean ret;
220         gchar *device_id;
221         GError *error = NULL;
222
223         colord_idle_cancel_for_output(cms, o);
224         device_id = get_output_id(cms, o);
225         weston_log("colord: output removed %s\n", device_id);
226         ocms = g_hash_table_lookup(cms->devices, device_id);
227         if (!ocms) {
228                 weston_log("colord: failed to delete device\n");
229                 goto out;
230         }
231         g_signal_handlers_disconnect_by_data(ocms->device, ocms);
232         ret = cd_client_delete_device_sync (cms->client,
233                                             ocms->device,
234                                             NULL,
235                                             &error);
236
237         if (!ret) {
238                 weston_log("colord: failed to delete device: %s\n", error->message);
239                 g_error_free(error);
240                 goto out;
241         }
242 out:
243         g_hash_table_remove (cms->devices, device_id);
244         g_free (device_id);
245 }
246
247 static void
248 colord_output_created(struct cms_colord *cms, struct weston_output *o)
249 {
250         CdDevice *device;
251         const gchar *tmp;
252         gchar *device_id;
253         GError *error = NULL;
254         GHashTable *device_props;
255         struct cms_output *ocms;
256
257         /* create device */
258         device_id = get_output_id(cms, o);
259         weston_log("colord: output added %s\n", device_id);
260         device_props = g_hash_table_new_full(g_str_hash, g_str_equal,
261                                              g_free, g_free);
262         g_hash_table_insert (device_props,
263                              g_strdup(CD_DEVICE_PROPERTY_KIND),
264                              g_strdup(cd_device_kind_to_string (CD_DEVICE_KIND_DISPLAY)));
265         g_hash_table_insert (device_props,
266                              g_strdup(CD_DEVICE_PROPERTY_FORMAT),
267                              g_strdup("ColorModel.OutputMode.OutputResolution"));
268         g_hash_table_insert (device_props,
269                              g_strdup(CD_DEVICE_PROPERTY_COLORSPACE),
270                              g_strdup(cd_colorspace_to_string(CD_COLORSPACE_RGB)));
271         if (edid_value_valid(o->make)) {
272                 tmp = g_hash_table_lookup(cms->pnp_ids, o->make);
273                 if (tmp == NULL)
274                         tmp = o->make;
275                 g_hash_table_insert (device_props,
276                                      g_strdup(CD_DEVICE_PROPERTY_VENDOR),
277                                      g_strdup(tmp));
278         }
279         if (edid_value_valid(o->model)) {
280                 g_hash_table_insert (device_props,
281                                      g_strdup(CD_DEVICE_PROPERTY_MODEL),
282                                      g_strdup(o->model));
283         }
284         if (edid_value_valid(o->serial_number)) {
285                 g_hash_table_insert (device_props,
286                                      g_strdup(CD_DEVICE_PROPERTY_SERIAL),
287                                      g_strdup(o->serial_number));
288         }
289         if (o->connection_internal) {
290                 g_hash_table_insert (device_props,
291                                      g_strdup (CD_DEVICE_PROPERTY_EMBEDDED),
292                                      NULL);
293         }
294         device = cd_client_create_device_sync(cms->client,
295                                               device_id,
296                                               CD_OBJECT_SCOPE_TEMP,
297                                               device_props,
298                                               NULL,
299                                               &error);
300         if (g_error_matches (error,
301                              CD_CLIENT_ERROR,
302                              CD_CLIENT_ERROR_ALREADY_EXISTS)) {
303                 g_clear_error(&error);
304                 device = cd_client_find_device_sync (cms->client,
305                                                      device_id,
306                                                      NULL,
307                                                      &error);
308         }
309         if (!device) {
310                 weston_log("colord: failed to create new or "
311                            "find existing device: %s\n",
312                            error->message);
313                 g_error_free(error);
314                 goto out;
315         }
316
317         /* create object and watch for the output to be destroyed */
318         ocms = g_slice_new0(struct cms_output);
319         ocms->cms = cms;
320         ocms->o = o;
321         ocms->device = g_object_ref(device);
322         ocms->destroy_listener.notify = colord_notifier_output_destroy;
323         wl_signal_add(&o->destroy_signal, &ocms->destroy_listener);
324
325         /* add to local cache */
326         g_hash_table_insert (cms->devices, g_strdup(device_id), ocms);
327         g_signal_connect (ocms->device, "changed",
328                           G_CALLBACK (colord_device_changed_cb), ocms);
329
330         /* get profiles */
331         colord_update_output_from_device (ocms);
332 out:
333         g_hash_table_unref (device_props);
334         if (device)
335                 g_object_unref (device);
336         g_free (device_id);
337 }
338
339 static void
340 colord_notifier_output_created(struct wl_listener *listener, void *data)
341 {
342         struct weston_output *o = (struct weston_output *) data;
343         struct cms_colord *cms =
344                 container_of(listener, struct cms_colord, destroy_listener);
345         weston_log("colord: output %s created\n", o->name);
346         colord_output_created(cms, o);
347 }
348
349 static gpointer
350 colord_run_loop_thread(gpointer data)
351 {
352         struct cms_colord *cms = (struct cms_colord *) data;
353         struct weston_output *o;
354
355         /* coldplug outputs */
356         wl_list_for_each(o, &cms->ec->output_list, link) {
357                 weston_log("colord: output %s coldplugged\n", o->name);
358                 colord_output_created(cms, o);
359         }
360
361         g_main_loop_run(cms->loop);
362         return NULL;
363 }
364
365 static int
366 colord_dispatch_all_pending(int fd, uint32_t mask, void *data)
367 {
368         gchar tmp;
369         GList *l;
370         ssize_t rc;
371         struct cms_colord *cms = data;
372         struct cms_output *ocms;
373
374         weston_log("colord: dispatching events\n");
375         g_mutex_lock(&cms->pending_mutex);
376         for (l = cms->pending; l != NULL; l = l->next) {
377                 ocms = l->data;
378
379                 /* optionally set backlight to calibration value */
380                 if (ocms->o->set_backlight && ocms->backlight_value != 0) {
381                         weston_log("colord: profile calibration backlight to %i/255\n",
382                                    ocms->backlight_value);
383                         ocms->o->set_backlight(ocms->o, ocms->backlight_value);
384                 }
385
386                 weston_cms_set_color_profile(ocms->o, ocms->p);
387         }
388         g_list_free (cms->pending);
389         cms->pending = NULL;
390         g_mutex_unlock(&cms->pending_mutex);
391
392         /* done */
393         rc = read(cms->readfd, &tmp, 1);
394         if (rc == 0)
395                 weston_log("colord: failed to read from pending fd");
396         return 1;
397 }
398
399 static void
400 colord_load_pnp_ids(struct cms_colord *cms)
401 {
402         gboolean ret = FALSE;
403         gchar *tmp;
404         GError *error = NULL;
405         guint i;
406         const gchar *pnp_ids_fn[] = { "/usr/share/hwdata/pnp.ids",
407                                       "/usr/share/misc/pnp.ids",
408                                       NULL };
409
410         /* find and load file */
411         for (i = 0; pnp_ids_fn[i] != NULL; i++) {
412                 if (!g_file_test(pnp_ids_fn[i], G_FILE_TEST_EXISTS))
413                         continue;
414                 ret = g_file_get_contents(pnp_ids_fn[i],
415                                           &cms->pnp_ids_data,
416                                           NULL,
417                                           &error);
418                 if (!ret) {
419                         weston_log("colord: failed to load %s: %s\n",
420                                    pnp_ids_fn[i], error->message);
421                         g_error_free(error);
422                         return;
423                 }
424                 break;
425         }
426         if (!ret) {
427                 weston_log("colord: no pnp.ids found\n");
428                 return;
429         }
430
431         /* parse fixed offsets into lines */
432         tmp = cms->pnp_ids_data;
433         for (i = 0; cms->pnp_ids_data[i] != '\0'; i++) {
434                 if (cms->pnp_ids_data[i] != '\n')
435                         continue;
436                 cms->pnp_ids_data[i] = '\0';
437                 if (tmp[0] && tmp[1] && tmp[2] && tmp[3] == '\t' && tmp[4]) {
438                         tmp[3] = '\0';
439                         g_hash_table_insert(cms->pnp_ids, tmp, tmp+4);
440                         tmp = &cms->pnp_ids_data[i+1];
441                 }
442         }
443 }
444
445 static void
446 colord_module_destroy(struct cms_colord *cms)
447 {
448         g_free(cms->pnp_ids_data);
449         g_hash_table_unref(cms->pnp_ids);
450
451         if (cms->loop) {
452                 g_main_loop_quit(cms->loop);
453                 g_main_loop_unref(cms->loop);
454         }
455         if (cms->thread)
456                 g_thread_join(cms->thread);
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         free(cms);
466 }
467
468 static void
469 colord_notifier_destroy(struct wl_listener *listener, void *data)
470 {
471         struct cms_colord *cms =
472                 container_of(listener, struct cms_colord, destroy_listener);
473         colord_module_destroy(cms);
474 }
475
476 static void
477 colord_cms_output_destroy(gpointer data)
478 {
479         struct cms_output *ocms = (struct cms_output *) data;
480         g_object_unref(ocms->device);
481         g_slice_free(struct cms_output, ocms);
482 }
483
484 WL_EXPORT int
485 module_init(struct weston_compositor *ec,
486             int *argc, char *argv[])
487 {
488         gboolean ret;
489         GError *error = NULL;
490         int fd[2];
491         struct cms_colord *cms;
492         struct wl_event_loop *loop;
493
494         weston_log("colord: initialized\n");
495
496         /* create local state object */
497         cms = zalloc(sizeof *cms);
498         if (cms == NULL)
499                 return -1;
500         cms->ec = ec;
501 #if !GLIB_CHECK_VERSION(2,36,0)
502         g_type_init();
503 #endif
504         cms->client = cd_client_new();
505         ret = cd_client_connect_sync(cms->client, NULL, &error);
506         if (!ret) {
507                 weston_log("colord: failed to contact daemon: %s\n", error->message);
508                 g_error_free(error);
509                 colord_module_destroy(cms);
510                 return -1;
511         }
512         g_mutex_init(&cms->pending_mutex);
513         cms->devices = g_hash_table_new_full(g_str_hash, g_str_equal,
514                                              g_free, colord_cms_output_destroy);
515
516         /* destroy */
517         cms->destroy_listener.notify = colord_notifier_destroy;
518         wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
519
520         /* devices added */
521         cms->output_created_listener.notify = colord_notifier_output_created;
522         wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
523
524         /* add all the PNP IDs */
525         cms->pnp_ids = g_hash_table_new_full(g_str_hash,
526                                              g_str_equal,
527                                              NULL,
528                                              NULL);
529         colord_load_pnp_ids(cms);
530
531         /* setup a thread for the GLib callbacks */
532         cms->loop = g_main_loop_new(NULL, FALSE);
533         cms->thread = g_thread_new("colord CMS main loop",
534                                    colord_run_loop_thread, cms);
535
536         /* batch device<->profile updates */
537         if (pipe2(fd, O_CLOEXEC) == -1) {
538                 colord_module_destroy(cms);
539                 return -1;
540         }
541         cms->readfd = fd[0];
542         cms->writefd = fd[1];
543         loop = wl_display_get_event_loop(ec->wl_display);
544         cms->source = wl_event_loop_add_fd (loop,
545                                             cms->readfd,
546                                             WL_EVENT_READABLE,
547                                             colord_dispatch_all_pending,
548                                             cms);
549         if (!cms->source) {
550                 colord_module_destroy(cms);
551                 return -1;
552         }
553         return 0;
554 }