cms-static: Use the right wl_listener for the output hotplug listener
[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 =
77                 container_of(listener, struct cms_static, output_created_listener);
78         cms_output_created(cms, o);
79 }
80
81 static void
82 cms_module_destroy(struct cms_static *cms)
83 {
84         struct cms_configured_output *configured_output, *next_co;
85
86         wl_list_for_each_safe(configured_output, next_co,
87                               &cms->configured_output_list, link) {
88                 free(configured_output->name);
89                 free(configured_output->icc_profile);
90                 free(configured_output);
91         }
92         free(cms);
93 }
94
95 static void
96 cms_notifier_destroy(struct wl_listener *listener, void *data)
97 {
98         struct cms_static *cms = container_of(listener, struct cms_static, destroy_listener);
99         cms_module_destroy(cms);
100 }
101
102 static char *output_icc_profile;
103 static char *output_name;
104
105 static void
106 output_section_done(void *data)
107 {
108         struct cms_configured_output *configured_output;
109         struct cms_static *cms = (struct cms_static *) data;
110
111         if (output_name == NULL || output_icc_profile == NULL) {
112                 free(output_name);
113                 free(output_icc_profile);
114                 output_name = NULL;
115                 output_icc_profile = NULL;
116                 return;
117         }
118
119         weston_log("cms-static: output %s profile configured as %s\n",
120                    output_name, output_icc_profile);
121
122         /* create an object used to store name<->profile data to avoid parsing
123          * the config file every time a new output is added */
124         configured_output = malloc(sizeof *configured_output);
125         memset(configured_output, 0, sizeof *configured_output);
126         configured_output->name = output_name;
127         configured_output->icc_profile = output_icc_profile;
128         wl_list_insert(&cms->configured_output_list, &configured_output->link);
129         output_name = NULL;
130         output_icc_profile = NULL;
131 }
132
133 WL_EXPORT int
134 module_init(struct weston_compositor *ec,
135             int *argc, char *argv[])
136 {
137         struct cms_static *cms;
138         struct weston_output *output;
139
140         weston_log("cms-static: initialized\n");
141
142         /* create local state object */
143         cms = malloc(sizeof *cms);
144         if (cms == NULL)
145                 return -1;
146         memset(cms, 0, sizeof *cms);
147
148         wl_list_init(&cms->configured_output_list);
149
150         /* parse config file */
151         const struct config_key drm_config_keys[] = {
152                 { "name", CONFIG_KEY_STRING, &output_name },
153                 { "icc_profile", CONFIG_KEY_STRING, &output_icc_profile },
154         };
155
156         const struct config_section config_section[] = {
157                 { "output", drm_config_keys,
158                 ARRAY_LENGTH(drm_config_keys), output_section_done },
159         };
160
161         parse_config_file(ec->config_fd, config_section,
162                           ARRAY_LENGTH(config_section), cms);
163
164         cms->destroy_listener.notify = cms_notifier_destroy;
165         wl_signal_add(&ec->destroy_signal, &cms->destroy_listener);
166
167         cms->output_created_listener.notify = cms_notifier_output_created;
168         wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
169
170         /* discover outputs */
171         wl_list_for_each(output, &ec->output_list, link)
172                 cms_output_created(cms, output);
173
174         return 0;
175 }