Debian Codegraph @

lib/compat/cd-compat-edid.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2013 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * SECTION:cd-edid
24 * @short_description: Compatibility routines for applications that process the EDID
25 *
26 * These functions here are designed as a ucmm-like wrapper for ArgyllCMS.
27 * Other software should use the native functionality in libcolord rather than
28 * this shim functionality.
29 */
30
31 #include "config.h"
32
33 #include <colord.h>
34 #include <string.h>
35
36 #include "cd-compat-edid.h"
37
38 /**
39 * cd_edid_install_profile:
40 * @scope: where to install the profile, e.g. %CD_EDID_SCOPE_USER
41 * @edid: the EDID data, typically just 128 bytes in size
42 * @edid_len: the size in bytes of @edid_len
43 * @profile_fn: the profile filename to install
44 *
45 * Install a profile for a given monitor.
46 *
47 * Return value: a %CdEdidError, e.g. %CD_EDID_ERROR_OK
48 *
49 * Since: 0.1.34
50 **/
51 CdEdidError
52 cd_edid_install_profile (unsigned char *edid,
53 int edid_len,
54 CdEdidScope scope,
55 char *profile_fn)
56 {
57 gboolean ret;
58 g_autoptr(GError) error = NULL;
59 g_autofree gchar *md5 = NULL;
60 g_autoptr(CdClient) client = NULL;
61 g_autoptr(CdDevice) device = NULL;
62 g_autoptr(CdProfile) profile = NULL;
63 g_autoptr(GFile) file = NULL;
64
65 g_return_val_if_fail (profile_fn != NULL, CD_EDID_ERROR_RESOURCE);
66
67 /* bad input */
68 if (edid == NULL || edid_len == 0)
69 return CD_EDID_ERROR_NO_DATA;
70
71 /* connect to daemon */
72 client = cd_client_new ();
73 ret = cd_client_connect_sync (client, NULL, &error);
74 if (!ret) {
75 g_printerr ("Failed to connect to colord: %s", error->message);
76 return CD_EDID_ERROR_ACCESS_CONFIG;
77 }
78
79 /* find device that matches the output EDID */
80 md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5,
81 (const gchar *)edid,
82 (gssize) edid_len);
83 device = cd_client_find_device_by_property_sync (client,
84 CD_DEVICE_METADATA_OUTPUT_EDID_MD5,
85 md5,
86 NULL,
87 &error);
88 if (device == NULL) {
89 g_printerr ("Failed to find device that matches %s: %s",
90 md5, error->message);
91 return CD_EDID_ERROR_MONITOR_NOT_FOUND;
92 }
93
94 /* read device properties */
95 ret = cd_device_connect_sync (device, NULL, &error);
96 if (!ret) {
97 g_printerr ("device disappeared: %s", error->message);
98 return CD_EDID_ERROR_ACCESS_CONFIG;
99 }
100
101 /* import profile */
102 file = g_file_new_for_path (profile_fn);
103 profile = cd_client_import_profile_sync (client, file, NULL, &error);
104 if (profile == NULL &&
105 g_error_matches (error,
106 CD_CLIENT_ERROR,
107 CD_CLIENT_ERROR_ALREADY_EXISTS)) {
108 g_clear_error (&error);
109 profile = cd_client_find_profile_by_property_sync (client,
110 CD_PROFILE_PROPERTY_FILENAME,
111 profile_fn,
112 NULL,
113 &error);
114 }
115 if (profile == NULL) {
116 g_printerr ("Could not import profile %s: %s",
117 profile_fn,
118 error->message);
119 return CD_EDID_ERROR_NO_PROFILE;
120 }
121
122 /* read profile properties */
123 ret = cd_profile_connect_sync (profile, NULL, &error);
124 if (!ret) {
125 g_printerr ("profile disappeared: %s", error->message);
126 return CD_EDID_ERROR_ACCESS_CONFIG;
127 }
128
129 /* add profile to device */
130 ret = cd_device_add_profile_sync (device,
131 CD_DEVICE_RELATION_HARD,
132 profile,
133 NULL,
134 &error);
135 if (!ret) {
136 if (g_error_matches (error,
137 CD_DEVICE_ERROR,
138 CD_DEVICE_ERROR_PROFILE_ALREADY_ADDED)) {
139 /* ignore this */
140 g_clear_error (&error);
141 } else {
142 g_printerr ("could not add profile %s to device %s: %s",
143 cd_profile_get_id(profile),
144 cd_device_get_id(device),
145 error->message);
146 return CD_EDID_ERROR_SET_CONFIG;
147 }
148 }
149
150 /* make default */
151 ret = cd_device_make_profile_default_sync (device,
152 profile,
153 NULL,
154 &error);
155 if (!ret) {
156 g_printerr ("could not add default profile %s to device %s: %s",
157 cd_profile_get_id(profile),
158 cd_device_get_id(device),
159 error->message);
160 return CD_EDID_ERROR_SET_CONFIG;
161 }
162
163 /* install system-wide */
164 if (scope == CD_EDID_SCOPE_SYSTEM) {
165 ret = cd_profile_install_system_wide_sync (profile, NULL, &error);
166 if (!ret) {
167 g_printerr ("could not set profile %s systemwide: %s",
168 cd_profile_get_id (profile),
169 error->message);
170 return CD_EDID_ERROR_PROFILE_COPY;
171 }
172 }
173 return CD_EDID_ERROR_OK;
174 }
175
176 /**
177 * cd_edid_remove_profile:
178 * @edid: the EDID data, typically just 128 bytes in size
179 * @edid_len: the size in bytes of @edid_len
180 * @profile_fn: the profile filename to remove
181 *
182 * Un-install a profile for a given monitor.
183 *
184 * Return value: a %CdEdidError, e.g. %CD_EDID_ERROR_OK
185 *
186 * Since: 0.1.34
187 **/
188 CdEdidError
189 cd_edid_remove_profile (unsigned char *edid,
190 int edid_len,
191 char *profile_fn)
192 {
193 gboolean ret;
194 g_autoptr(GError) error = NULL;
195 g_autofree gchar *md5 = NULL;
196 g_autoptr(CdClient) client = NULL;
197 g_autoptr(CdDevice) device = NULL;
198 g_autoptr(CdProfile) profile = NULL;
199 g_autoptr(GFile) file = NULL;
200
201 g_return_val_if_fail (profile_fn != NULL, CD_EDID_ERROR_RESOURCE);
202
203 /* bad input */
204 if (edid == NULL || edid_len == 0)
205 return CD_EDID_ERROR_NO_DATA;
206
207 /* connect to daemon */
208 client = cd_client_new ();
209 ret = cd_client_connect_sync (client, NULL, &error);
210 if (!ret) {
211 g_printerr ("Failed to connect to colord: %s", error->message);
212 return CD_EDID_ERROR_ACCESS_CONFIG;
213 }
214
215 /* find device that matches the output EDID */
216 md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5,
217 (const gchar *)edid,
218 (gssize) edid_len);
219 device = cd_client_find_device_by_property_sync (client,
220 CD_DEVICE_METADATA_OUTPUT_EDID_MD5,
221 md5,
222 NULL,
223 &error);
224 if (device == NULL) {
225 g_printerr ("Failed to find device that matches %s: %s",
226 md5, error->message);
227 return CD_EDID_ERROR_MONITOR_NOT_FOUND;
228 }
229
230 /* read device properties */
231 ret = cd_device_connect_sync (device, NULL, &error);
232 if (!ret) {
233 g_printerr ("device disappeared: %s", error->message);
234 return CD_EDID_ERROR_ACCESS_CONFIG;
235 }
236
237 /* find profile */
238 file = g_file_new_for_path (profile_fn);
239 profile = cd_client_find_profile_by_filename_sync (client,
240 profile_fn,
241 NULL,
242 &error);
243 if (profile == NULL) {
244 g_printerr ("Could not find profile %s: %s",
245 profile_fn,
246 error->message);
247 return CD_EDID_ERROR_NO_PROFILE;
248 }
249
250 /* read profile properties */
251 ret = cd_profile_connect_sync (profile, NULL, &error);
252 if (!ret) {
253 g_printerr ("profile disappeared: %s", error->message);
254 return CD_EDID_ERROR_ACCESS_CONFIG;
255 }
256
257 /* remove profile from device */
258 ret = cd_device_remove_profile_sync (device,
259 profile,
260 NULL,
261 &error);
262 if (!ret) {
263 g_printerr ("could not remove profile %s from device %s: %s",
264 cd_profile_get_id (profile),
265 cd_device_get_id (device),
266 error->message);
267 return CD_EDID_ERROR_SET_CONFIG;
268 }
269 return CD_EDID_ERROR_OK;
270 }
271
272 /**
273 * cd_edid_get_profile:
274 * @edid: the EDID data, typically just 128 bytes in size
275 * @edid_len: the size in bytes of @edid_len
276 * @profile_fn: the returned profile filename, use free() when done
277 *
278 * Get an associated monitor profile.
279 *
280 * Return value: a %CdEdidError, e.g. %CD_EDID_ERROR_OK
281 *
282 * Since: 0.1.34
283 **/
284 CdEdidError
285 cd_edid_get_profile (unsigned char *edid,
286 int edid_len,
287 char **profile_fn)
288 {
289 const gchar *filename;
290 gboolean ret;
291 g_autoptr(GError) error = NULL;
292 g_autofree gchar *md5 = NULL;
293 g_autoptr(CdClient) client = NULL;
294 g_autoptr(CdDevice) device = NULL;
295 g_autoptr(CdProfile) profile = NULL;
296
297 /* bad input */
298 if (edid == NULL || edid_len == 0)
299 return CD_EDID_ERROR_NO_DATA;
300
301 /* connect to daemon */
302 client = cd_client_new ();
303 ret = cd_client_connect_sync (client, NULL, &error);
304 if (!ret) {
305 g_printerr ("Failed to connect to colord: %s", error->message);
306 return CD_EDID_ERROR_ACCESS_CONFIG;
307 }
308
309 /* find device that matches the output EDID */
310 md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5,
311 (const gchar *)edid,
312 (gssize) edid_len);
313 device = cd_client_find_device_by_property_sync (client,
314 CD_DEVICE_METADATA_OUTPUT_EDID_MD5,
315 md5,
316 NULL,
317 &error);
318 if (device == NULL) {
319 g_printerr ("Failed to find device that matches %s: %s",
320 md5, error->message);
321 return CD_EDID_ERROR_MONITOR_NOT_FOUND;
322 }
323
324 /* read device properties */
325 ret = cd_device_connect_sync (device, NULL, &error);
326 if (!ret) {
327 g_printerr ("device disappeared: %s", error->message);
328 return CD_EDID_ERROR_ACCESS_CONFIG;
329 }
330
331 /* get the default profile for the device */
332 profile = cd_device_get_default_profile (device);
333 if (profile == NULL) {
334 g_printerr ("No profile for %s",
335 cd_device_get_id (device));
336 return CD_EDID_ERROR_NO_PROFILE;
337 }
338
339 /* read profile properties */
340 ret = cd_profile_connect_sync (profile, NULL, &error);
341 if (!ret) {
342 g_printerr ("profile disappeared: %s", error->message);
343 return CD_EDID_ERROR_ACCESS_CONFIG;
344 }
345
346 /* get filename */
347 filename = cd_profile_get_filename (profile);
348 if (filename == NULL)
349 return CD_EDID_ERROR_INVALID_PROFILE;
350
351 /* return filename of profile */
352 if (profile_fn != NULL)
353 *profile_fn = strdup (filename);
354 return CD_EDID_ERROR_OK;
355 }