Debian Codegraph @

src/sensors/argyll/cd-sensor-argyll.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2012 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program 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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22
23 #include "config.h"
24
25 #include <glib-object.h>
26 #include <stdlib.h>
27
28 #include "cd-sensor.h"
29 #include "cd-spawn.h"
30
31 #define CD_SENSOR_ARGYLL_MAX_SAMPLE_TIME 60000 /* ms */
32
33 typedef enum {
34 CD_SENSOR_ARGYLL_POS_UNKNOWN,
35 CD_SENSOR_ARGYLL_POS_CALIBRATE,
36 CD_SENSOR_ARGYLL_POS_LAST
37 } CdSensorArgyllPos;
38
39 typedef struct {
40 gboolean done_startup;
41 CdSpawn *spawn;
42 guint communication_port;
43 CdSensorArgyllPos pos_required;
44 } CdSensorArgyllPrivate;
45
46 static CdSensorArgyllPrivate *
47 cd_sensor_argyll_get_private (CdSensor *sensor)
48 {
49 return g_object_get_data (G_OBJECT (sensor), "priv");
50 }
51
52 /* async task for the sensor readings */
53 typedef struct {
54 gboolean ret;
55 CdColorXYZ *sample;
56 CdSensor *sensor;
57 guint exit_id;
58 guint stdout_id;
59 guint timeout_id;
60 } CdSensorTaskData;
61
62 static void
63 cd_sensor_task_data_free (CdSensorTaskData *data)
64 {
65 CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (data->sensor);
66
67 /* disconnect handlers */
68 if (data->exit_id > 0)
69 g_signal_handler_disconnect (priv->spawn, data->exit_id);
70 if (data->stdout_id > 0)
71 g_signal_handler_disconnect (priv->spawn, data->stdout_id);
72 if (data->timeout_id > 0)
73 g_source_remove (data->timeout_id);
74 g_object_unref (data->sensor);
75 g_free (data);
76 }
77
78 static gboolean
79 cd_sensor_get_sample_timeout_cb (GTask *task)
80 {
81 g_task_return_new_error (task,
82 CD_SENSOR_ERROR,
83 CD_SENSOR_ERROR_INTERNAL,
84 "spotread timed out");
85 g_object_unref (task);
86 return G_SOURCE_REMOVE;
87 }
88
89 static void
90 cd_sensor_get_sample_exit_cb (CdSpawn *spawn,
91 CdSpawnExitType exit_type,
92 GTask *task)
93 {
94 g_task_return_new_error (task,
95 CD_SENSOR_ERROR,
96 CD_SENSOR_ERROR_INTERNAL,
97 "spotread exited unexpectedly");
98 g_object_unref (task);
99 }
100
101 static void
102 cd_sensor_get_sample_stdout_cb (CdSpawn *spawn, const gchar *line, GTask *task)
103 {
104 CdSensorTaskData *data = g_task_get_task_data (task);
105 CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (data->sensor);
106 g_autoptr(GError) error = NULL;
107 g_auto(GStrv) parts = NULL;
108
109 g_debug ("line='%s'", line);
110
111 /* ready to go, no measurement */
112 if (g_str_has_prefix (line, "Place instrument on spot to be measured")) {
113 if (priv->pos_required == CD_SENSOR_ARGYLL_POS_UNKNOWN)
114 cd_spawn_send_stdin (spawn, "");
115 return;
116 }
117
118 /* got calibration */
119 if (g_strcmp0 (line, "Calibration complete") == 0) {
120 priv->pos_required = CD_SENSOR_ARGYLL_POS_UNKNOWN;
121 return;
122 }
123
124 /* got measurement */
125 if (g_str_has_prefix (line, " Result is XYZ:")) {
126 CdColorXYZ *sample;
127 parts = g_strsplit_set (line, " ,", -1);
128 sample = cd_color_xyz_new ();
129 sample->X = g_ascii_strtod (parts[4], NULL);
130 sample->Y = g_ascii_strtod (parts[5], NULL);
131 sample->Z = g_ascii_strtod (parts[6], NULL);
132 g_task_return_pointer (task, sample, (GDestroyNotify) cd_color_xyz_free);
133 g_object_unref (task);
134 return;
135 }
136
137 /* failed */
138 if (g_str_has_prefix (line, "Instrument initialisation failed")) {
139 g_task_return_new_error (task,
140 CD_SENSOR_ERROR,
141 CD_SENSOR_ERROR_INTERNAL,
142 "failed to contact hardware (replug)");
143 g_object_unref (task);
144 return;
145 }
146
147 /* need surface */
148 if (g_strcmp0 (line, "(Sensor should be in surface position)") == 0) {
149 g_task_return_new_error (task,
150 CD_SENSOR_ERROR,
151 CD_SENSOR_ERROR_REQUIRED_POSITION_SURFACE,
152 "Move to surface position");
153 g_object_unref (task);
154 return;
155 }
156
157 /* need calibrate */
158 if (g_str_has_prefix (line, "Set instrument sensor to calibration position,")) {
159
160 /* just try to read; argyllcms doesn't detect the
161 * sensor position before it asks the user to move the dial... */
162 if (priv->pos_required == CD_SENSOR_ARGYLL_POS_UNKNOWN) {
163 cd_spawn_send_stdin (spawn, "");
164 priv->pos_required = CD_SENSOR_ARGYLL_POS_CALIBRATE;
165 return;
166 }
167 g_task_return_new_error (task,
168 CD_SENSOR_ERROR,
169 CD_SENSOR_ERROR_REQUIRED_POSITION_CALIBRATE,
170 "Move to calibration position");
171 g_object_unref (task);
172 return;
173 }
174 }
175
176 static const gchar *
177 cd_sensor_get_y_arg_for_cap (CdSensorCap cap)
178 {
179 const gchar *arg = NULL;
180
181 switch (cap) {
182 case CD_SENSOR_CAP_LCD:
183 case CD_SENSOR_CAP_LED:
184 arg = "-yl";
185 break;
186 case CD_SENSOR_CAP_CRT:
187 case CD_SENSOR_CAP_PLASMA:
188 arg = "-yc";
189 break;
190 case CD_SENSOR_CAP_PROJECTOR:
191 arg = "-yp";
192 break;
193 case CD_SENSOR_CAP_LCD_CCFL:
194 arg = "-yf";
195 break;
196 case CD_SENSOR_CAP_LCD_RGB_LED:
197 arg = "-yb";
198 break;
199 case CD_SENSOR_CAP_WIDE_GAMUT_LCD_CCFL:
200 arg = "-yL";
201 break;
202 case CD_SENSOR_CAP_WIDE_GAMUT_LCD_RGB_LED:
203 arg = "-yB";
204 break;
205 case CD_SENSOR_CAP_LCD_WHITE_LED:
206 arg = "-ye";
207 break;
208 default:
209 break;
210 }
211 return arg;
212 }
213
214 void
215 cd_sensor_get_sample_async (CdSensor *sensor,
216 CdSensorCap cap,
217 GCancellable *cancellable,
218 GAsyncReadyCallback callback,
219 gpointer user_data)
220 {
221 CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (sensor);
222 CdSensorTaskData *data;
223 GTask *task = NULL;
224 const gchar *envp[] = { "ARGYLL_NOT_INTERACTIVE=1", NULL };
225 gboolean ret;
226 g_autoptr(GError) error = NULL;
227 g_autoptr(GPtrArray) argv = NULL;
228
229 g_return_if_fail (CD_IS_SENSOR (sensor));
230
231 task = g_task_new (sensor, cancellable, callback, user_data);
232
233 /* set state */
234 data = g_new0 (CdSensorTaskData, 1);
235 data->sensor = g_object_ref (sensor);
236 g_task_set_task_data (task, data, (GDestroyNotify) cd_sensor_task_data_free);
237
238 /* set state */
239 cd_sensor_set_state (sensor, CD_SENSOR_STATE_MEASURING);
240
241 /* connect before spotread produces values */
242 data->exit_id = g_signal_connect (priv->spawn,
243 "exit",
244 G_CALLBACK (cd_sensor_get_sample_exit_cb),
245 task);
246 data->stdout_id = g_signal_connect (priv->spawn,
247 "stdout",
248 G_CALLBACK (cd_sensor_get_sample_stdout_cb),
249 task);
250
251 /* if spotread is not already running then execute */
252 if (!cd_spawn_is_running (priv->spawn)) {
253 argv = g_ptr_array_new_with_free_func (g_free);
254 g_ptr_array_add (argv, g_strdup ("spotread"));
255 g_ptr_array_add (argv, g_strdup ("-d"));
256 g_ptr_array_add (argv, g_strdup_printf ("-c%u", priv->communication_port));
257 g_ptr_array_add (argv, g_strdup ("-N")); //no autocal
258 g_ptr_array_add (argv, g_strdup (cd_sensor_get_y_arg_for_cap (cap)));
259 g_ptr_array_add (argv, NULL);
260 ret = cd_spawn_argv (priv->spawn,
261 (gchar **) argv->pdata,
262 (gchar **) envp,
263 &error);
264 if (!ret) {
265 g_task_return_new_error (task,
266 CD_SENSOR_ERROR,
267 CD_SENSOR_ERROR_INTERNAL,
268 "%s", error->message);
269 return;
270 }
271 } else {
272 cd_spawn_send_stdin (priv->spawn, "");
273 }
274
275 /* cover the case where spotread crashes */
276 data->timeout_id = g_timeout_add (CD_SENSOR_ARGYLL_MAX_SAMPLE_TIME,
277 (GSourceFunc) cd_sensor_get_sample_timeout_cb,
278 task);
279 }
280
281 CdColorXYZ *
282 cd_sensor_get_sample_finish (CdSensor *sensor, GAsyncResult *res, GError **error)
283 {
284 g_return_val_if_fail (g_task_is_valid (res, sensor), NULL);
285 return g_task_propagate_pointer (G_TASK (res), error);
286 }
287
288 static void
289 cd_sensor_unref_private (CdSensorArgyllPrivate *priv)
290 {
291 g_object_unref (priv->spawn);
292 g_free (priv);
293 }
294
295 static const gchar *
296 cd_sensor_to_argyll_name (CdSensor *sensor)
297 {
298 switch (cd_sensor_get_kind (sensor)) {
299 case CD_SENSOR_KIND_DTP20:
300 return "Xrite DTP20";
301 case CD_SENSOR_KIND_DTP22:
302 return "Xrite DTP22";
303 case CD_SENSOR_KIND_DTP41:
304 return "Xrite DTP41";
305 case CD_SENSOR_KIND_DTP51:
306 return "Xrite DTP51";
307 case CD_SENSOR_KIND_DTP92:
308 return "Xrite DTP92";
309 case CD_SENSOR_KIND_DTP94:
310 return "Xrite DTP94";
311 case CD_SENSOR_KIND_SPECTRO_SCAN:
312 return "GretagMacbeth SpectroScan";
313 case CD_SENSOR_KIND_I1_DISPLAY1:
314 return "GretagMacbeth i1 Display 1";
315 case CD_SENSOR_KIND_I1_DISPLAY2:
316 return "GretagMacbeth i1 Display 2";
317 case CD_SENSOR_KIND_I1_DISPLAY3:
318 return "Xrite i1 DisplayPro, ColorMunki Display";
319 case CD_SENSOR_KIND_I1_MONITOR:
320 return "GretagMacbeth i1 Monitor";
321 case CD_SENSOR_KIND_I1_PRO:
322 return "GretagMacbeth i1 Pro";
323 case CD_SENSOR_KIND_COLOR_MUNKI_PHOTO:
324 return "X-Rite ColorMunki";
325 case CD_SENSOR_KIND_COLOR_MUNKI_SMILE:
326 return "ColorMunki Smile";
327 case CD_SENSOR_KIND_COLORIMTRE_HCFR:
328 return "Colorimtre HCFR";
329 case CD_SENSOR_KIND_SPYDER2:
330 return "ColorVision Spyder2";
331 case CD_SENSOR_KIND_SPYDER3:
332 return "Datacolor Spyder3";
333 case CD_SENSOR_KIND_SPYDER:
334 return "Datacolor Spyder4";
335 case CD_SENSOR_KIND_SPYDER5:
336 return "Datacolor Spyder5";
337 case CD_SENSOR_KIND_SPYDERX:
338 return "Datacolor SpyderX";
339 case CD_SENSOR_KIND_HUEY:
340 return "GretagMacbeth Huey";
341 case CD_SENSOR_KIND_COLORHUG:
342 return "Hughski ColorHug";
343 case CD_SENSOR_KIND_COLORHUG2:
344 return "Hughski ColorHug2";
345 case CD_SENSOR_KIND_COLORHUG_PLUS:
346 return "Hughski ColorHug+";
347 default:
348 break;
349 }
350 return NULL;
351 }
352
353 static gboolean
354 cd_sensor_find_device_details (CdSensor *sensor, GError **error)
355 {
356 CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (sensor);
357 const gchar *argv[] = { "spotread", "--help", NULL };
358 const gchar *argyll_name;
359 const gchar *envp[] = { "ARGYLL_NOT_INTERACTIVE=1", NULL };
360 const gchar *usb_path;
361 gboolean ret;
362 guint i;
363 guint listno = 0;
364 g_autofree gchar *stdout = NULL;
365 g_auto(GStrv) lines = NULL;
366
367 /* spawn the --help output to parse the comm-port */
368 ret = g_spawn_sync (NULL,
369 (gchar **) argv,
370 (gchar **) envp,
371 G_SPAWN_SEARCH_PATH,
372 NULL,
373 NULL,
374 NULL,
375 &stdout,
376 NULL,
377 error);
378 if (!ret)
379 return FALSE;
380
381 /* look for the usb /dev/bus/usb/002/003 path first */
382 usb_path = cd_sensor_get_usb_path (sensor);
383 argyll_name = cd_sensor_to_argyll_name (sensor);
384 lines = g_strsplit (stdout, "\n", -1);
385 for (i = 0; lines[i] != NULL; i++) {
386
387 /* look for the communication port listing of the
388 * device type we have plugged in */
389 if (g_strstr_len (lines[i], -1, " = ") != NULL) {
390 listno++;
391 if (usb_path != NULL &&
392 g_strstr_len (lines[i], -1, usb_path) != NULL) {
393 priv->communication_port = listno;
394 break;
395 }
396 if (argyll_name != NULL &&
397 g_strstr_len (lines[i], -1, argyll_name) != NULL) {
398 priv->communication_port = listno;
399 break;
400 }
401 }
402 }
403
404 /* did we find the right device */
405 if (priv->communication_port == 0) {
406 g_set_error (error,
407 CD_SENSOR_ERROR,
408 CD_SENSOR_ERROR_INTERNAL,
409 "Failed to find device %s",
410 argyll_name);
411 return FALSE;
412 }
413 return TRUE;
414 }
415
416 static void
417 cd_sensor_unlock_exit_cb (CdSpawn *spawn,
418 CdSpawnExitType exit_type,
419 GTask *task)
420 {
421 if (exit_type != CD_SPAWN_EXIT_TYPE_SIGQUIT) {
422 g_task_return_new_error (task,
423 CD_SENSOR_ERROR,
424 CD_SENSOR_ERROR_INTERNAL,
425 "exited without sigquit");
426 g_object_unref (task);
427 return;
428 }
429
430 /* success */
431 g_task_return_boolean (task, TRUE);
432 g_object_unref (task);
433 }
434
435 void
436 cd_sensor_unlock_async (CdSensor *sensor,
437 GCancellable *cancellable,
438 GAsyncReadyCallback callback,
439 gpointer user_data)
440 {
441 CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (sensor);
442 CdSensorTaskData *data;
443 GTask *task = NULL;
444
445 g_return_if_fail (CD_IS_SENSOR (sensor));
446
447 task = g_task_new (sensor, cancellable, callback, user_data);
448
449 /* set state */
450 data = g_new0 (CdSensorTaskData, 1);
451 data->sensor = g_object_ref (sensor);
452 g_task_set_task_data (task, data, (GDestroyNotify) cd_sensor_task_data_free);
453
454 /* wait for exit */
455 data->exit_id = g_signal_connect (priv->spawn,
456 "exit",
457 G_CALLBACK (cd_sensor_unlock_exit_cb),
458 task);
459 /* kill spotread */
460 if (!cd_spawn_kill (priv->spawn)) {
461 g_task_return_new_error (task,
462 CD_SENSOR_ERROR,
463 CD_SENSOR_ERROR_INTERNAL,
464 "failed to kill spotread");
465 g_object_unref (task);
466 return;
467 }
468 }
469
470 gboolean
471 cd_sensor_coldplug (CdSensor *sensor, GError **error)
472 {
473 CdSensorArgyllPrivate *priv;
474 g_object_set (sensor,
475 "native", FALSE,
476 NULL);
477
478 /* create private data */
479 priv = g_new0 (CdSensorArgyllPrivate, 1);
480 priv->spawn = cd_spawn_new ();
481 g_object_set_data_full (G_OBJECT (sensor), "priv", priv,
482 (GDestroyNotify) cd_sensor_unref_private);
483
484 /* try to map find the correct communication port */
485 return cd_sensor_find_device_details (sensor, error);
486 }