94fea99d90b2d57c705f30bb2031d4a08f6ebd4b
[platform/upstream/weston.git] / src / cms-static.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 #define _GNU_SOURCE
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "compositor.h"
32 #include "cms-helper.h"
33
34 struct cms_static {
35         struct weston_compositor        *ec;
36         struct wl_listener               destroy_listener;
37         struct wl_listener               output_created_listener;
38         struct wl_list                   configured_output_list;
39 };
40
41 struct cms_configured_output {
42         char                            *icc_profile;
43         char                            *name;
44         struct wl_list                   link;
45 };
46
47 static void
48 cms_output_created(struct cms_static *cms, struct weston_output *o)
49 {
50         struct cms_configured_output *configured_output;
51         struct weston_color_profile *p;
52
53         weston_log("cms-static: output %i [%s] created\n", o->id, o->name);
54
55         /* find profile from configured list */
56         wl_list_for_each(configured_output, &cms->configured_output_list, link) {
57                 if (strcmp (o->name, configured_output->name) == 0) {
58                         p = weston_cms_load_profile(configured_output->icc_profile);
59                         if (p == NULL) {
60                                 weston_log("cms-static: failed to load %s\n",
61                                            configured_output->icc_profile);
62                         } else {
63                                        weston_log("cms-static: loading %s for %s\n",
64                                                   configured_output->icc_profile, o->name);
65                                        weston_cms_set_color_profile(o, p);
66                         }
67                         break;
68                 }
69         }
70 }
71
72 static void
73 cms_notifier_output_created(struct wl_listener *listener, void *data)
74 {
75         struct weston_output *o = (struct weston_output *) data;
76         struct cms_static *cms = container_of(listener, struct cms_static, destroy_listener);
77         cms_output_created(cms, o);
78 }
79
80 static void
81 cms_module_destroy(struct cms_static *cms)
82 {
83         struct cms_configured_output *configured_output, *next_co;
84
85         wl_list_for_each_safe(configured_output, next_co,
86                               &cms->configured_output_list, link) {
87                 free(configured_output->name);
88                 free(configured_output->icc_profile);
89                 free(configured_output);
90         }
91         free(cms);
92 }
93
94 static void
95 cms_notifier_destroy(struct wl_listener *listener, void *data)
96 {
97         struct cms_static *cms = container_of(listener, struct cms_static, destroy_listener);
98         cms_module_destroy(cms);
99 }
100
101 static char *output_icc_profile;
102 static char *output_name;
103
104 static void
105 output_section_done(void *data)
106 {
107         struct cms_configured_output *configured_output;
108         struct cms_static *cms = (struct cms_static *) data;
109
110         if (output_name == NULL || output_icc_profile == NULL) {
111                 free(output_name);
112                 free(output_icc_profile);
113                 output_name = NULL;
114                 output_icc_profile = NULL;
115                 return;
116         }
117
118         weston_log("cms-static: output %s profile configured as %s\n",
119                    output_name, output_icc_profile);
120
121         /* create an object used to store name<->profile data to avoid parsing
122          * the config file every time a new output is added */
123         configured_output = malloc(sizeof *configured_output);
124         memset(configured_output, 0, sizeof *configured_output);
125         configured_output->name = output_name;
126         configured_output->icc_profile = output_icc_profile;
127         wl_list_insert(&cms->configured_output_list, &configured_output->link);
128         output_name = NULL;
129         output_icc_profile = NULL;
130 }
131
132 WL_EXPORT int
133 module_init(struct weston_compositor *ec,
134             int *argc, char *argv[])
135 {
136         struct cms_static *cms;
137         struct weston_output *output;
138
139         weston_log("cms-static: initialized\n");
140
141         /* create local state object */
142         cms = malloc(sizeof *cms);
143         if (cms == NULL)
144                 return -1;
145         memset(cms, 0, sizeof *cms);
146
147         wl_list_init(&cms->configured_output_list);
148
149         /* parse config file */
150         const struct config_key drm_config_keys[] = {
151                 { "name", CONFIG_KEY_STRING, &output_name },
152                 { "icc_profile", CONFIG_KEY_STRING, &output_icc_profile },
153         };
154
155         const struct config_section config_section[] = {
156                 { "output", drm_config_keys,
157                 ARRAY_LENGTH(drm_config_keys), output_section_done },
158         };
159
160         parse_config_file(ec->config_fd, config_section,
161                           ARRAY_LENGTH(config_section), cms);
162
163         cms->destroy_listener.notify = cms_notifier_destroy;
164         wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
165
166         cms->output_created_listener.notify = cms_notifier_output_created;
167         wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
168
169         /* discover outputs */
170         wl_list_for_each(output, &ec->output_list, link)
171                 cms_output_created(cms, output);
172
173         return 0;
174 }