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 <stdlib.h>
29 #include <string.h>
30
31 #include <libweston/libweston.h>
32 #include "cms-helper.h"
33 #include "shared/helpers.h"
34 #include "weston.h"
35
36 struct cms_static {
37 struct weston_compositor *ec;
38 struct wl_listener destroy_listener;
39 struct wl_listener output_created_listener;
40 };
41
42 static void
cms_output_created(struct cms_static * cms,struct weston_output * o)43 cms_output_created(struct cms_static *cms, struct weston_output *o)
44 {
45 struct weston_color_profile *p;
46 struct weston_config_section *s;
47 char *profile;
48
49 weston_log("cms-static: output %i [%s] created\n", o->id, o->name);
50
51 if (o->name == NULL)
52 return;
53 s = weston_config_get_section(wet_get_config(cms->ec),
54 "output", "name", o->name);
55 if (s == NULL)
56 return;
57 if (weston_config_section_get_string(s, "icc_profile", &profile, NULL) < 0)
58 return;
59 p = weston_cms_load_profile(profile);
60 if (p == NULL && strlen(profile) > 0) {
61 weston_log("cms-static: failed to load %s\n", profile);
62 } else {
63 weston_log("cms-static: loading %s for %s\n",
64 (p != NULL) ? profile : "identity LUT",
65 o->name);
66 weston_cms_set_color_profile(o, p);
67 }
68 }
69
70 static void
cms_notifier_output_created(struct wl_listener * listener,void * data)71 cms_notifier_output_created(struct wl_listener *listener, void *data)
72 {
73 struct weston_output *o = (struct weston_output *) data;
74 struct cms_static *cms =
75 container_of(listener, struct cms_static, output_created_listener);
76 cms_output_created(cms, o);
77 }
78
79 static void
cms_module_destroy(struct cms_static * cms)80 cms_module_destroy(struct cms_static *cms)
81 {
82 free(cms);
83 }
84
85 static void
cms_notifier_destroy(struct wl_listener * listener,void * data)86 cms_notifier_destroy(struct wl_listener *listener, void *data)
87 {
88 struct cms_static *cms = container_of(listener, struct cms_static, destroy_listener);
89 cms_module_destroy(cms);
90 }
91
92
93 WL_EXPORT int
wet_module_init(struct weston_compositor * ec,int * argc,char * argv[])94 wet_module_init(struct weston_compositor *ec,
95 int *argc, char *argv[])
96 {
97 struct cms_static *cms;
98 struct weston_output *output;
99
100 weston_log("cms-static: initialized\n");
101
102 /* create local state object */
103 cms = zalloc(sizeof *cms);
104 if (cms == NULL)
105 return -1;
106
107 cms->ec = ec;
108
109 if (!weston_compositor_add_destroy_listener_once(ec,
110 &cms->destroy_listener,
111 cms_notifier_destroy)) {
112 free(cms);
113 return 0;
114 }
115
116 cms->output_created_listener.notify = cms_notifier_output_created;
117 wl_signal_add(&ec->output_created_signal, &cms->output_created_listener);
118
119 /* discover outputs */
120 wl_list_for_each(output, &ec->output_list, link)
121 cms_output_created(cms, output);
122
123 return 0;
124 }
125