summaryrefslogtreecommitdiff
path: root/src/sensors/argyll/cd-sensor-argyll.c
blob: ef8456dad66a513c0b93ad90c170e246f71079d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2010-2012 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include "config.h"

#include <glib-object.h>
#include <stdlib.h>

#include "cd-sensor.h"
#include "cd-spawn.h"

#define CD_SENSOR_ARGYLL_MAX_SAMPLE_TIME	60000 /* ms */

typedef enum {
	CD_SENSOR_ARGYLL_POS_UNKNOWN,
	CD_SENSOR_ARGYLL_POS_CALIBRATE,
	CD_SENSOR_ARGYLL_POS_LAST
} CdSensorArgyllPos;

typedef struct {
	gboolean			 done_startup;
	CdSpawn				*spawn;
	guint				 communication_port;
	CdSensorArgyllPos		 pos_required;
} CdSensorArgyllPrivate;

static CdSensorArgyllPrivate *
cd_sensor_argyll_get_private (CdSensor *sensor)
{
	return g_object_get_data (G_OBJECT (sensor), "priv");
}

/* async task for the sensor readings */
typedef struct {
	gboolean		 ret;
	CdColorXYZ		*sample;
	CdSensor		*sensor;
	guint			 exit_id;
	guint			 stdout_id;
	guint			 timeout_id;
} CdSensorTaskData;

static void
cd_sensor_task_data_free (CdSensorTaskData *data)
{
	CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (data->sensor);

	/* disconnect handlers */
	if (data->exit_id > 0)
		g_signal_handler_disconnect (priv->spawn, data->exit_id);
	if (data->stdout_id > 0)
		g_signal_handler_disconnect (priv->spawn, data->stdout_id);
	if (data->timeout_id > 0)
		g_source_remove (data->timeout_id);
	g_object_unref (data->sensor);
	g_free (data);
}

static gboolean
cd_sensor_get_sample_timeout_cb (GTask *task)
{
	g_task_return_new_error (task,
				 CD_SENSOR_ERROR,
				 CD_SENSOR_ERROR_INTERNAL,
				 "spotread timed out");
	g_object_unref (task);
	return G_SOURCE_REMOVE;
}

static void
cd_sensor_get_sample_exit_cb (CdSpawn *spawn,
			      CdSpawnExitType exit_type,
			      GTask *task)
{
	g_task_return_new_error (task,
				 CD_SENSOR_ERROR,
				 CD_SENSOR_ERROR_INTERNAL,
				 "spotread exited unexpectedly");
	g_object_unref (task);
}

static void
cd_sensor_get_sample_stdout_cb (CdSpawn *spawn, const gchar *line, GTask *task)
{
	CdSensorTaskData *data = g_task_get_task_data (task);
	CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (data->sensor);
	g_autoptr(GError) error = NULL;
	g_auto(GStrv) parts = NULL;

	g_debug ("line='%s'", line);

	/* ready to go, no measurement */
	if (g_str_has_prefix (line, "Place instrument on spot to be measured")) {
		if (priv->pos_required == CD_SENSOR_ARGYLL_POS_UNKNOWN)
			cd_spawn_send_stdin (spawn, "");
		return;
	}

	/* got calibration */
	if (g_strcmp0 (line, "Calibration complete") == 0) {
		priv->pos_required = CD_SENSOR_ARGYLL_POS_UNKNOWN;
		return;
	}

	/* got measurement */
	if (g_str_has_prefix (line, " Result is XYZ:")) {
		CdColorXYZ *sample;
		parts = g_strsplit_set (line, " ,", -1);
		sample = cd_color_xyz_new ();
		sample->X = atof (parts[4]);
		sample->Y = atof (parts[5]);
		sample->Z = atof (parts[6]);
		g_task_return_pointer (task, sample, (GDestroyNotify) cd_color_xyz_free);
		g_object_unref (task);
		return;
	}

	/* failed */
	if (g_str_has_prefix (line, "Instrument initialisation failed")) {
		g_task_return_new_error (task,
					 CD_SENSOR_ERROR,
					 CD_SENSOR_ERROR_INTERNAL,
					 "failed to contact hardware (replug)");
		g_object_unref (task);
		return;
	}

	/* need surface */
	if (g_strcmp0 (line, "(Sensor should be in surface position)") == 0) {
		g_task_return_new_error (task,
					 CD_SENSOR_ERROR,
					 CD_SENSOR_ERROR_REQUIRED_POSITION_SURFACE,
					 "Move to surface position");
		g_object_unref (task);
		return;
	}

	/* need calibrate */
	if (g_str_has_prefix (line, "Set instrument sensor to calibration position,")) {

		/* just try to read; argyllcms doesn't detect the
		 * sensor position before it asks the user to move the dial... */
		if (priv->pos_required == CD_SENSOR_ARGYLL_POS_UNKNOWN) {
			cd_spawn_send_stdin (spawn, "");
			priv->pos_required = CD_SENSOR_ARGYLL_POS_CALIBRATE;
			return;
		}
		g_task_return_new_error (task,
					 CD_SENSOR_ERROR,
					 CD_SENSOR_ERROR_REQUIRED_POSITION_CALIBRATE,
					 "Move to calibration position");
		g_object_unref (task);
		return;
	}
}

static const gchar *
cd_sensor_get_y_arg_for_cap (CdSensorCap cap)
{
	const gchar *arg = NULL;

	switch (cap) {
	case CD_SENSOR_CAP_LCD:
	case CD_SENSOR_CAP_LED:
		arg = "-yl";
		break;
	case CD_SENSOR_CAP_CRT:
	case CD_SENSOR_CAP_PLASMA:
		arg = "-yc";
		break;
	case CD_SENSOR_CAP_PROJECTOR:
		arg = "-yp";
		break;
	case CD_SENSOR_CAP_LCD_CCFL:
		arg = "-yf";
		break;
	case CD_SENSOR_CAP_LCD_RGB_LED:
		arg = "-yb";
		break;
	case CD_SENSOR_CAP_WIDE_GAMUT_LCD_CCFL:
		arg = "-yL";
		break;
	case CD_SENSOR_CAP_WIDE_GAMUT_LCD_RGB_LED:
		arg = "-yB";
		break;
	case CD_SENSOR_CAP_LCD_WHITE_LED:
		arg = "-ye";
		break;
	default:
		break;
	}
	return arg;
}

void
cd_sensor_get_sample_async (CdSensor *sensor,
			    CdSensorCap cap,
			    GCancellable *cancellable,
			    GAsyncReadyCallback callback,
			    gpointer user_data)
{
	CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (sensor);
	CdSensorTaskData *data;
	GTask *task = NULL;
	const gchar *envp[] = { "ARGYLL_NOT_INTERACTIVE=1", NULL };
	gboolean ret;
	g_autoptr(GError) error = NULL;
	g_autoptr(GPtrArray) argv = NULL;

	g_return_if_fail (CD_IS_SENSOR (sensor));

	task = g_task_new (sensor, cancellable, callback, user_data);

	/* set state */
	data = g_new0 (CdSensorTaskData, 1);
	data->sensor = g_object_ref (sensor);
	g_task_set_task_data (task, data, (GDestroyNotify) cd_sensor_task_data_free);

	/* set state */
	cd_sensor_set_state (sensor, CD_SENSOR_STATE_MEASURING);

	/* connect before spotread produces values */
	data->exit_id = g_signal_connect (priv->spawn,
					  "exit",
					  G_CALLBACK (cd_sensor_get_sample_exit_cb),
					  task);
	data->stdout_id = g_signal_connect (priv->spawn,
					    "stdout",
					    G_CALLBACK (cd_sensor_get_sample_stdout_cb),
					    task);

	/* if spotread is not already running then execute */
	if (!cd_spawn_is_running (priv->spawn)) {
		argv = g_ptr_array_new_with_free_func (g_free);
		g_ptr_array_add (argv, g_strdup ("spotread"));
		g_ptr_array_add (argv, g_strdup ("-d"));
		g_ptr_array_add (argv, g_strdup_printf ("-c%u", priv->communication_port));
		g_ptr_array_add (argv, g_strdup ("-N")); //no autocal
		g_ptr_array_add (argv, g_strdup (cd_sensor_get_y_arg_for_cap (cap)));
		g_ptr_array_add (argv, NULL);
		ret = cd_spawn_argv (priv->spawn,
				     (gchar **) argv->pdata,
				     (gchar **) envp,
				     &error);
		if (!ret) {
			g_task_return_new_error (task,
						 CD_SENSOR_ERROR,
						 CD_SENSOR_ERROR_INTERNAL,
						 "%s", error->message);
			return;
		}
	} else {
		cd_spawn_send_stdin (priv->spawn, "");
	}

	/* cover the case where spotread crashes */
	data->timeout_id = g_timeout_add (CD_SENSOR_ARGYLL_MAX_SAMPLE_TIME,
					  (GSourceFunc) cd_sensor_get_sample_timeout_cb,
					  task);
}

CdColorXYZ *
cd_sensor_get_sample_finish (CdSensor *sensor, GAsyncResult *res, GError **error)
{
	g_return_val_if_fail (g_task_is_valid (res, sensor), NULL);
	return g_task_propagate_pointer (G_TASK (res), error);
}

static void
cd_sensor_unref_private (CdSensorArgyllPrivate *priv)
{
	g_object_unref (priv->spawn);
	g_free (priv);
}

static const gchar *
cd_sensor_to_argyll_name (CdSensor *sensor)
{
	switch (cd_sensor_get_kind (sensor)) {
	case CD_SENSOR_KIND_DTP20:
		return "Xrite DTP20";
	case CD_SENSOR_KIND_DTP22:
		return "Xrite DTP22";
	case CD_SENSOR_KIND_DTP41:
		return "Xrite DTP41";
	case CD_SENSOR_KIND_DTP51:
		return "Xrite DTP51";
	case CD_SENSOR_KIND_DTP92:
		return "Xrite DTP92";
	case CD_SENSOR_KIND_DTP94:
		return "Xrite DTP94";
	case CD_SENSOR_KIND_SPECTRO_SCAN:
		return "GretagMacbeth SpectroScan";
	case CD_SENSOR_KIND_I1_DISPLAY1:
		return "GretagMacbeth i1 Display 1";
	case CD_SENSOR_KIND_I1_DISPLAY2:
		return "GretagMacbeth i1 Display 2";
	case CD_SENSOR_KIND_I1_DISPLAY3:
		return "Xrite i1 DisplayPro, ColorMunki Display";
	case CD_SENSOR_KIND_I1_MONITOR:
		return "GretagMacbeth i1 Monitor";
	case CD_SENSOR_KIND_I1_PRO:
		return "GretagMacbeth i1 Pro";
	case CD_SENSOR_KIND_COLOR_MUNKI_PHOTO:
		return "X-Rite ColorMunki";
	case CD_SENSOR_KIND_COLOR_MUNKI_SMILE:
		return "ColorMunki Smile";
	case CD_SENSOR_KIND_COLORIMTRE_HCFR:
		return "Colorimtre HCFR";
	case CD_SENSOR_KIND_SPYDER2:
		return "ColorVision Spyder2";
	case CD_SENSOR_KIND_SPYDER3:
		return "Datacolor Spyder3";
	case CD_SENSOR_KIND_SPYDER:
		return "Datacolor Spyder4";
	case CD_SENSOR_KIND_SPYDER5:
		return "Datacolor Spyder5";
	case CD_SENSOR_KIND_HUEY:
		return "GretagMacbeth Huey";
	case CD_SENSOR_KIND_COLORHUG:
		return "Hughski ColorHug";
	case CD_SENSOR_KIND_COLORHUG2:
		return "Hughski ColorHug2";
	case CD_SENSOR_KIND_COLORHUG_PLUS:
		return "Hughski ColorHug+";
	default:
		break;
	}
	return NULL;
}

static gboolean
cd_sensor_find_device_details (CdSensor *sensor, GError **error)
{
	CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (sensor);
	const gchar *argv[] = { "spotread", "--help", NULL };
	const gchar *argyll_name;
	const gchar *envp[] = { "ARGYLL_NOT_INTERACTIVE=1", NULL };
	const gchar *usb_path;
	gboolean ret;
	guint i;
	guint listno = 0;
	g_autofree gchar *stdout = NULL;
	g_auto(GStrv) lines = NULL;

	/* spawn the --help output to parse the comm-port */
	ret = g_spawn_sync (NULL,
			    (gchar **) argv,
			    (gchar **) envp,
			    G_SPAWN_SEARCH_PATH,
			    NULL,
			    NULL,
			    NULL,
			    &stdout,
			    NULL,
			    error);
	if (!ret)
		return FALSE;

	/* look for the usb /dev/bus/usb/002/003 path first */
	usb_path = cd_sensor_get_usb_path (sensor);
	argyll_name = cd_sensor_to_argyll_name (sensor);
	lines = g_strsplit (stdout, "\n", -1);
	for (i = 0; lines[i] != NULL; i++) {

		/* look for the communication port listing of the
		 * device type we have plugged in */
		if (g_strstr_len (lines[i], -1, " = ") != NULL) {
			listno++;
			if (usb_path != NULL &&
			    g_strstr_len (lines[i], -1, usb_path) != NULL) {
				priv->communication_port = listno;
				break;
			}
			if (argyll_name != NULL &&
			    g_strstr_len (lines[i], -1, argyll_name) != NULL) {
				priv->communication_port = listno;
				break;
			}
		}
	}

	/* did we find the right device */
	if (priv->communication_port == 0) {
		g_set_error (error,
			     CD_SENSOR_ERROR,
			     CD_SENSOR_ERROR_INTERNAL,
			     "Failed to find device %s",
			     argyll_name);
		return FALSE;
	}
	return TRUE;
}

static void
cd_sensor_unlock_exit_cb (CdSpawn *spawn,
			  CdSpawnExitType exit_type,
			  GTask *task)
{
	if (exit_type != CD_SPAWN_EXIT_TYPE_SIGQUIT) {
		g_task_return_new_error (task,
					 CD_SENSOR_ERROR,
					 CD_SENSOR_ERROR_INTERNAL,
					 "exited without sigquit");
		g_object_unref (task);
		return;
	}

	/* success */
	g_task_return_boolean (task, TRUE);
	g_object_unref (task);
}

void
cd_sensor_unlock_async (CdSensor *sensor,
			GCancellable *cancellable,
			GAsyncReadyCallback callback,
			gpointer user_data)
{
	CdSensorArgyllPrivate *priv = cd_sensor_argyll_get_private (sensor);
	CdSensorTaskData *data;
	GTask *task = NULL;

	g_return_if_fail (CD_IS_SENSOR (sensor));

	task = g_task_new (sensor, cancellable, callback, user_data);

	/* set state */
	data = g_new0 (CdSensorTaskData, 1);
	data->sensor = g_object_ref (sensor);
	g_task_set_task_data (task, data, (GDestroyNotify) cd_sensor_task_data_free);

	/* wait for exit */
	data->exit_id = g_signal_connect (priv->spawn,
					  "exit",
					  G_CALLBACK (cd_sensor_unlock_exit_cb),
					  task);
	/* kill spotread */
	if (!cd_spawn_kill (priv->spawn)) {
		g_task_return_new_error (task,
					 CD_SENSOR_ERROR,
					 CD_SENSOR_ERROR_INTERNAL,
					 "failed to kill spotread");
		g_object_unref (task);
		return;
	}
}

gboolean
cd_sensor_coldplug (CdSensor *sensor, GError **error)
{
	CdSensorArgyllPrivate *priv;
	g_object_set (sensor,
		      "native", FALSE,
		      NULL);

	/* create private data */
	priv = g_new0 (CdSensorArgyllPrivate, 1);
	priv->spawn = cd_spawn_new ();
	g_object_set_data_full (G_OBJECT (sensor), "priv", priv,
				(GDestroyNotify) cd_sensor_unref_private);

	/* try to map find the correct communication port */
	return cd_sensor_find_device_details (sensor, error);
}