Debian Codegraph @

src/cd-sensor.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2011 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 #include "config.h"
23
24 #include <glib-object.h>
25 #include <gio/gio.h>
26 #include <sys/time.h>
27 #include <gmodule.h>
28 #include <colord-private.h>
29
30 #include "cd-common.h"
31 #include "cd-sensor.h"
32
33 static void cd_sensor_finalize (GObject *object);
34
35 #define GET_PRIVATE(o) (cd_sensor_get_instance_private (o))
36
37 typedef struct {
38 void (*get_sample_async) (CdSensor *sensor,
39 CdSensorCap cap,
40 GCancellable *cancellable,
41 GAsyncReadyCallback callback,
42 gpointer user_data);
43 CdColorXYZ *(*get_sample_finish) (CdSensor *sensor,
44 GAsyncResult *res,
45 GError **error);
46 void (*get_spectrum_async) (CdSensor *sensor,
47 CdSensorCap cap,
48 GCancellable *cancellable,
49 GAsyncReadyCallback callback,
50 gpointer user_data);
51 CdSpectrum *(*get_spectrum_finish) (CdSensor *sensor,
52 GAsyncResult *res,
53 GError **error);
54 gboolean (*coldplug) (CdSensor *sensor,
55 GError **error);
56 gboolean (*dump_device) (CdSensor *sensor,
57 GString *data,
58 GError **error);
59 void (*lock_async) (CdSensor *sensor,
60 GCancellable *cancellable,
61 GAsyncReadyCallback callback,
62 gpointer user_data);
63 gboolean (*lock_finish) (CdSensor *sensor,
64 GAsyncResult *res,
65 GError **error);
66 void (*unlock_async) (CdSensor *sensor,
67 GCancellable *cancellable,
68 GAsyncReadyCallback callback,
69 gpointer user_data);
70 gboolean (*unlock_finish) (CdSensor *sensor,
71 GAsyncResult *res,
72 GError **error);
73 void (*set_options_async) (CdSensor *sensor,
74 GHashTable *options,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
77 gpointer user_data);
78 gboolean (*set_options_finish) (CdSensor *sensor,
79 GAsyncResult *res,
80 GError **error);
81 } CdSensorIface;
82
83 typedef struct
84 {
85 gchar *id;
86 CdSensorKind kind;
87 CdSensorState state;
88 CdSensorCap mode;
89 gchar *serial;
90 gchar *model;
91 gchar *vendor;
92 GUdevDevice *device;
93 gboolean native;
94 gboolean embedded;
95 gboolean locked;
96 guint64 caps;
97 gchar *object_path;
98 gchar *usb_path;
99 guint watcher_id;
100 GDBusConnection *connection;
101 guint registration_id;
102 guint set_state_id;
103 CdSensorIface *desc;
104 GHashTable *options;
105 GHashTable *metadata;
106 GUsbContext *usb_ctx;
107 } CdSensorPrivate;
108
109 enum {
110 PROP_0,
111 PROP_OBJECT_PATH,
112 PROP_ID,
113 PROP_NATIVE,
114 PROP_LOCKED,
115 PROP_STATE,
116 PROP_MODE,
117 PROP_VENDOR,
118 PROP_MODEL,
119 PROP_SERIAL,
120 PROP_KIND,
121 PROP_CAPS,
122 PROP_LAST
123 };
124
125 G_DEFINE_TYPE_WITH_PRIVATE (CdSensor, cd_sensor, G_TYPE_OBJECT)
126
127 GQuark
128 cd_sensor_error_quark (void)
129 {
130 guint i;
131 static GQuark quark = 0;
132 if (!quark) {
133 quark = g_quark_from_static_string ("CdSensor");
134 for (i = 0; i < CD_SENSOR_ERROR_LAST; i++) {
135 g_dbus_error_register_error (quark,
136 i,
137 cd_sensor_error_to_string (i));
138 }
139 }
140 return quark;
141 }
142
143 const gchar *
144 cd_sensor_get_object_path (CdSensor *sensor)
145 {
146 CdSensorPrivate *priv = GET_PRIVATE (sensor);
147 g_return_val_if_fail (CD_IS_SENSOR (sensor), NULL);
148 return priv->object_path;
149 }
150
151 const gchar *
152 cd_sensor_get_id (CdSensor *sensor)
153 {
154 CdSensorPrivate *priv = GET_PRIVATE (sensor);
155 g_return_val_if_fail (CD_IS_SENSOR (sensor), NULL);
156 return priv->id;
157 }
158
159 static void
160 cd_sensor_set_id (CdSensor *sensor, const gchar *id)
161 {
162 CdSensorPrivate *priv = GET_PRIVATE (sensor);
163 g_autofree gchar *id_tmp = NULL;
164
165 g_return_if_fail (CD_IS_SENSOR (sensor));
166 g_free (priv->id);
167
168 /* make sure object path is sane */
169 id_tmp = cd_main_ensure_dbus_path (id);
170 priv->object_path = g_build_filename (COLORD_DBUS_PATH,
171 "sensors",
172 id_tmp,
173 NULL);
174 priv->id = g_strdup (id);
175 }
176
177 static void
178 cd_sensor_dbus_emit_property_changed (CdSensor *sensor,
179 const gchar *property_name,
180 GVariant *property_value)
181 {
182 CdSensorPrivate *priv = GET_PRIVATE (sensor);
183 GVariantBuilder builder;
184 GVariantBuilder invalidated_builder;
185
186 /* not yet connected */
187 if (priv->connection == NULL)
188 return;
189
190 /* build the dict */
191 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
192 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
193 g_variant_builder_add (&builder,
194 "{sv}",
195 property_name,
196 property_value);
197 g_debug ("CdSensor: emit PropertiesChanged(%s)", property_name);
198 g_dbus_connection_emit_signal (priv->connection,
199 NULL,
200 priv->object_path,
201 "org.freedesktop.DBus.Properties",
202 "PropertiesChanged",
203 g_variant_new ("(sa{sv}as)",
204 COLORD_DBUS_INTERFACE_SENSOR,
205 &builder,
206 &invalidated_builder),
207 NULL);
208 g_variant_builder_clear (&builder);
209 g_variant_builder_clear (&invalidated_builder);
210 }
211
212 void
213 cd_sensor_button_pressed (CdSensor *sensor)
214 {
215 CdSensorPrivate *priv = GET_PRIVATE (sensor);
216
217 /* not yet connected */
218 if (priv->connection == NULL)
219 return;
220
221 /* emit signal */
222 g_debug ("CdSensor: emit ButtonPressed on %s",
223 priv->object_path);
224 g_dbus_connection_emit_signal (priv->connection,
225 NULL,
226 priv->object_path,
227 COLORD_DBUS_INTERFACE_SENSOR,
228 "ButtonPressed",
229 NULL,
230 NULL);
231 }
232
233 /**
234 * cd_sensor_set_serial:
235 * @sensor: a valid #CdSensor instance
236 * @serial_number: the serial number
237 *
238 * Sets the sensor serial number which can be used to uniquely identify
239 * the device.
240 **/
241 void
242 cd_sensor_set_serial (CdSensor *sensor, const gchar *serial)
243 {
244 CdSensorPrivate *priv = GET_PRIVATE (sensor);
245 g_free (priv->serial);
246 priv->serial = g_strdup (serial);
247 cd_sensor_dbus_emit_property_changed (sensor,
248 "Serial",
249 g_variant_new_string (serial));
250 }
251
252 /**
253 * cd_sensor_set_kind:
254 * @sensor: a valid #CdSensor instance
255 * @kind: the sensor kind, e.g %CD_SENSOR_KIND_HUEY
256 *
257 * Sets the device kind.
258 **/
259 void
260 cd_sensor_set_kind (CdSensor *sensor, CdSensorKind kind)
261 {
262 CdSensorPrivate *priv = GET_PRIVATE (sensor);
263 priv->kind = kind;
264 cd_sensor_dbus_emit_property_changed (sensor,
265 "Kind",
266 g_variant_new_uint32 (kind));
267 }
268
269 CdSensorKind
270 cd_sensor_get_kind (CdSensor *sensor)
271 {
272 CdSensorPrivate *priv = GET_PRIVATE (sensor);
273 return priv->kind;
274 }
275
276 gboolean
277 cd_sensor_get_is_embedded (CdSensor *sensor)
278 {
279 CdSensorPrivate *priv = GET_PRIVATE (sensor);
280 return priv->embedded;
281 }
282
283 /**
284 * cd_sensor_load:
285 * @sensor: a valid #CdSensor instance
286 * @kind: the sensor kind, e.g %CD_SENSOR_KIND_HUEY
287 *
288 * Sets the device kind.
289 **/
290 gboolean
291 cd_sensor_load (CdSensor *sensor, GError **error)
292 {
293 CdSensorPrivate *priv = GET_PRIVATE (sensor);
294 CdSensorIface *desc;
295 GModule *handle;
296 const gchar *module_name;
297 g_autofree gchar *backend_name = NULL;
298 g_autofree gchar *path_fallback = NULL;
299 g_autofree gchar *path = NULL;
300
301 /* no module */
302 if (priv->kind == CD_SENSOR_KIND_UNKNOWN)
303 return TRUE;
304
305 /* some modules are shared */
306 switch (priv->kind) {
307 case CD_SENSOR_KIND_COLORHUG:
308 case CD_SENSOR_KIND_COLORHUG2:
309 module_name = "colorhug";
310 break;
311 default:
312 module_name = cd_sensor_kind_to_string (priv->kind);
313 break;
314 }
315
316 /* can we load a module? */
317 backend_name = g_strdup_printf ("libcolord_sensor_%s." G_MODULE_SUFFIX,
318 module_name);
319 path = g_build_filename (LIBDIR, "colord-sensors", backend_name, NULL);
320 g_debug ("Trying to load sensor driver: %s", path);
321 handle = g_module_open (path, G_MODULE_BIND_LOCAL);
322 if (handle == NULL) {
323 g_debug ("opening module %s failed : %s",
324 backend_name, g_module_error ());
325 g_debug ("Trying to fall back to : libcolord_sensor_argyll");
326 path_fallback = g_build_filename (LIBDIR,
327 "colord-sensors",
328 "libcolord_sensor_argyll.so",
329 NULL);
330 handle = g_module_open (path_fallback, G_MODULE_BIND_LOCAL);
331 }
332 if (handle == NULL) {
333 g_set_error (error, 1, 0,
334 "opening module %s (and fallback) failed : %s",
335 backend_name, g_module_error ());
336 return FALSE;
337 }
338
339 /* dlload module if it exists */
340 desc = priv->desc = g_new0 (CdSensorIface, 1);
341
342 /* connect up exported methods */
343 g_module_symbol (handle, "cd_sensor_get_sample_async", (gpointer *)&desc->get_sample_async);
344 g_module_symbol (handle, "cd_sensor_get_sample_finish", (gpointer *)&desc->get_sample_finish);
345 g_module_symbol (handle, "cd_sensor_get_spectrum_async", (gpointer *)&desc->get_spectrum_async);
346 g_module_symbol (handle, "cd_sensor_get_spectrum_finish", (gpointer *)&desc->get_spectrum_finish);
347 g_module_symbol (handle, "cd_sensor_set_options_async", (gpointer *)&desc->set_options_async);
348 g_module_symbol (handle, "cd_sensor_set_options_finish", (gpointer *)&desc->set_options_finish);
349 g_module_symbol (handle, "cd_sensor_coldplug", (gpointer *)&desc->coldplug);
350 g_module_symbol (handle, "cd_sensor_dump_device", (gpointer *)&desc->dump_device);
351 g_module_symbol (handle, "cd_sensor_lock_async", (gpointer *)&desc->lock_async);
352 g_module_symbol (handle, "cd_sensor_lock_finish", (gpointer *)&desc->lock_finish);
353 g_module_symbol (handle, "cd_sensor_unlock_async", (gpointer *)&desc->unlock_async);
354 g_module_symbol (handle, "cd_sensor_unlock_finish", (gpointer *)&desc->unlock_finish);
355
356 /* coldplug with data */
357 if (desc->coldplug != NULL)
358 return desc->coldplug (sensor, error);
359 return TRUE;
360 }
361
362 static void
363 cd_sensor_set_locked (CdSensor *sensor, gboolean locked)
364 {
365 CdSensorPrivate *priv = GET_PRIVATE (sensor);
366 priv->locked = locked;
367 cd_sensor_dbus_emit_property_changed (sensor,
368 "Locked",
369 g_variant_new_boolean (locked));
370 }
371
372 /**
373 * _cd_sensor_lock_async:
374 *
375 * Lock the sensor. You don't ever need to call this, and this method
376 * should only be used from the internal self check program.
377 **/
378 void
379 _cd_sensor_lock_async (CdSensor *sensor,
380 GCancellable *cancellable,
381 GAsyncReadyCallback callback,
382 gpointer user_data)
383 {
384 CdSensorPrivate *priv = GET_PRIVATE (sensor);
385
386 g_return_if_fail (CD_IS_SENSOR (sensor));
387 g_return_if_fail (priv->desc != NULL);
388
389 /* proxy up */
390 if (priv->desc->lock_async) {
391 priv->desc->lock_async (sensor,
392 cancellable,
393 callback,
394 user_data);
395 }
396 }
397
398 /**
399 * _cd_sensor_lock_finish:
400 *
401 * Finish locking the sensor. You don't ever need to call this, and this
402 * method should only be used from the internal self check program.
403 **/
404 gboolean
405 _cd_sensor_lock_finish (CdSensor *sensor,
406 GAsyncResult *res,
407 GError **error)
408 {
409 CdSensorPrivate *priv = GET_PRIVATE (sensor);
410 gboolean ret = TRUE;
411
412 g_return_val_if_fail (CD_IS_SENSOR (sensor), FALSE);
413 g_return_val_if_fail (priv->desc != NULL, FALSE);
414
415 /* proxy up */
416 if (priv->desc->lock_finish) {
417 ret = priv->desc->lock_finish (sensor,
418 res,
419 error);
420 }
421 cd_sensor_set_locked (sensor, TRUE);
422 return ret;
423 }
424
425 /**
426 * cd_sensor_set_state:
427 * @sensor: a valid #CdSensor instance
428 * @state: the sensor state, e.g %CD_SENSOR_STATE_IDLE
429 *
430 * Sets the device state.
431 **/
432 void
433 cd_sensor_set_state (CdSensor *sensor, CdSensorState state)
434 {
435 CdSensorPrivate *priv = GET_PRIVATE (sensor);
436
437 /* invalidate */
438 if (priv->set_state_id > 0) {
439 g_source_remove (priv->set_state_id);
440 priv->set_state_id = 0;
441 }
442
443 priv->state = state;
444 cd_sensor_dbus_emit_property_changed (sensor,
445 "State",
446 g_variant_new_string (cd_sensor_state_to_string (state)));
447 }
448
449 typedef struct {
450 CdSensor *sensor;
451 CdSensorState state;
452 } CdSensorIdleHelper;
453
454 static gboolean
455 cd_sensor_set_state_in_idle_cb (gpointer user_data)
456 {
457 CdSensorIdleHelper *helper = (CdSensorIdleHelper *) user_data;
458 CdSensorPrivate *priv = GET_PRIVATE (helper->sensor);
459
460 /* this is us */
461 priv->set_state_id = 0;
462
463 /* set state now */
464 cd_sensor_set_state (helper->sensor, helper->state);
465 g_object_unref (helper->sensor);
466 g_free (helper);
467 return G_SOURCE_REMOVE;
468 }
469
470 /**
471 * cd_sensor_set_state_in_idle:
472 * @sensor: a valid #CdSensor instance
473 * @state: the sensor state, e.g %CD_SENSOR_STATE_IDLE
474 *
475 * Sets the device state.
476 **/
477 void
478 cd_sensor_set_state_in_idle (CdSensor *sensor, CdSensorState state)
479 {
480 CdSensorPrivate *priv = GET_PRIVATE (sensor);
481 CdSensorIdleHelper *helper = g_new0 (CdSensorIdleHelper, 1);
482 helper->sensor = g_object_ref (sensor);
483 helper->state = state;
484 if (priv->set_state_id > 0)
485 g_source_remove (priv->set_state_id);
486 priv->set_state_id =
487 g_idle_add (cd_sensor_set_state_in_idle_cb, helper);
488 }
489
490 void
491 cd_sensor_set_mode (CdSensor *sensor, CdSensorCap mode)
492 {
493 CdSensorPrivate *priv = GET_PRIVATE (sensor);
494 priv->mode = mode;
495 cd_sensor_dbus_emit_property_changed (sensor,
496 "Mode",
497 g_variant_new_string (cd_sensor_cap_to_string (mode)));
498 }
499
500 CdSensorCap
501 cd_sensor_get_mode (CdSensor *sensor)
502 {
503 CdSensorPrivate *priv = GET_PRIVATE (sensor);
504 return priv->mode;
505 }
506
507 /**
508 * cd_sensor_dump:
509 * @sensor: a valid #CdSensor instance
510 * @data: A valid #GString for the returned data
511 * @error: a #GError or %NULL
512 *
513 * Dumps the unstructured device data to a string.
514 *
515 * Return value: %TRUE for success.
516 **/
517 gboolean
518 cd_sensor_dump (CdSensor *sensor, GString *data, GError **error)
519 {
520 CdSensorPrivate *priv = GET_PRIVATE (sensor);
521
522 /* write common sensor details */
523 g_string_append (data, "// AUTOMATICALLY GENERATED -- DO NOT EDIT\n");
524 g_string_append_printf (data, "generic-dump-version:%i\n", 1);
525 g_string_append_printf (data, "kind:%s\n", cd_sensor_kind_to_string (priv->kind));
526 g_string_append_printf (data, "vendor:%s\n", priv->vendor);
527 g_string_append_printf (data, "model:%s\n", priv->model);
528 g_string_append_printf (data, "serial-number:%s\n", priv->serial);
529
530 /* no type */
531 if (priv->desc == NULL) {
532 g_set_error_literal (error,
533 CD_SENSOR_ERROR,
534 CD_SENSOR_ERROR_INTERNAL,
535 "need to load sensor! [cd_sensor_load]");
536 return FALSE;
537 }
538
539 /* dump sensor */
540 if (priv->desc->dump_device == NULL) {
541 g_set_error_literal (error,
542 CD_SENSOR_ERROR,
543 CD_SENSOR_ERROR_INTERNAL,
544 "no klass support");
545 return FALSE;
546 }
547
548 /* proxy */
549 return priv->desc->dump_device (sensor, data, error);
550 }
551
552 static void
553 cd_sensor_get_sample_cb (GObject *source_object,
554 GAsyncResult *res,
555 gpointer user_data)
556 {
557 GVariant *result = NULL;
558 g_autoptr(CdColorXYZ) sample = NULL;
559 CdSensor *sensor = CD_SENSOR (source_object);
560 CdSensorPrivate *priv = GET_PRIVATE (sensor);
561 GDBusMethodInvocation *invocation = (GDBusMethodInvocation *) user_data;
562 g_autoptr(GError) error = NULL;
563
564 /* set here to avoid every sensor doing this */
565 cd_sensor_set_state (sensor, CD_SENSOR_STATE_IDLE);
566
567 /* get the result */
568 sample = priv->desc->get_sample_finish (sensor, res, &error);
569 if (sample == NULL) {
570 g_dbus_method_invocation_return_gerror (invocation, error);
571 return;
572 }
573
574 /* return value */
575 g_debug ("returning value %f, %f, %f", sample->X, sample->Y, sample->Z);
576 result = g_variant_new ("(ddd)", sample->X, sample->Y, sample->Z);
577 g_dbus_method_invocation_return_value (invocation, result);
578 }
579
580 static void
581 cd_sensor_get_spectrum_cb (GObject *source_object,
582 GAsyncResult *res,
583 gpointer user_data)
584 {
585 CdSensor *sensor = CD_SENSOR (source_object);
586 CdSensorPrivate *priv = GET_PRIVATE (sensor);
587 GDBusMethodInvocation *invocation = (GDBusMethodInvocation *) user_data;
588 GVariant *result = NULL;
589 GVariantBuilder data;
590 guint i;
591 g_autoptr(CdSpectrum) sp = NULL;
592 g_autoptr(GError) error = NULL;
593
594 /* set here to avoid every sensor doing this */
595 cd_sensor_set_state (sensor, CD_SENSOR_STATE_IDLE);
596
597 /* get the result */
598 sp = priv->desc->get_spectrum_finish (sensor, res, &error);
599 if (sp == NULL) {
600 g_dbus_method_invocation_return_gerror (invocation, error);
601 return;
602 }
603
604 /* build data array */
605 g_variant_builder_init (&data, G_VARIANT_TYPE ("ad"));
606 for (i = 0; i < cd_spectrum_get_size (sp); i++) {
607 g_variant_builder_add (&data, "d",
608 cd_spectrum_get_value (sp, i));
609 }
610
611 /* return value */
612 g_debug ("returning value %f, %f, [%u]",
613 cd_spectrum_get_start (sp),
614 cd_spectrum_get_end (sp),
615 cd_spectrum_get_size (sp));
616 result = g_variant_new ("(ddad)",
617 cd_spectrum_get_start (sp),
618 cd_spectrum_get_end (sp),
619 &data);
620 g_dbus_method_invocation_return_value (invocation, result);
621 }
622
623 static void
624 cd_sensor_set_options_cb (GObject *source_object,
625 GAsyncResult *res,
626 gpointer user_data)
627 {
628 gboolean ret;
629 CdSensor *sensor = CD_SENSOR (source_object);
630 CdSensorPrivate *priv = GET_PRIVATE (sensor);
631 GDBusMethodInvocation *invocation = (GDBusMethodInvocation *) user_data;
632 g_autoptr(GError) error = NULL;
633
634 /* get the result */
635 ret = priv->desc->set_options_finish (sensor, res, &error);
636 if (!ret) {
637 g_dbus_method_invocation_return_gerror (invocation, error);
638 return;
639 }
640 g_dbus_method_invocation_return_value (invocation, NULL);
641 }
642
643 static void
644 cd_sensor_lock_cb (GObject *source_object,
645 GAsyncResult *res,
646 gpointer user_data)
647 {
648 CdSensor *sensor = CD_SENSOR (source_object);
649 CdSensorPrivate *priv = GET_PRIVATE (sensor);
650 GDBusMethodInvocation *invocation = (GDBusMethodInvocation *) user_data;
651 gboolean ret;
652 g_autoptr(GError) error = NULL;
653
654 /* set here to avoid every sensor doing this */
655 cd_sensor_set_state (sensor, CD_SENSOR_STATE_IDLE);
656
657 /* get the result */
658 ret = priv->desc->lock_finish (sensor, res, &error);
659 if (!ret) {
660 g_dbus_method_invocation_return_error (invocation,
661 CD_SENSOR_ERROR,
662 CD_SENSOR_ERROR_NO_SUPPORT,
663 "failed to lock: %s",
664 error->message);
665 return;
666 }
667 cd_sensor_set_locked (sensor, TRUE);
668
669 /* debug */
670 if (g_getenv ("COLORD_DEVICE_DEBUG") != NULL) {
671 g_autoptr(GString) str = g_string_new (NULL);
672 if (!cd_sensor_dump (sensor, str, &error)) {
673 g_dbus_method_invocation_return_error (invocation,
674 CD_SENSOR_ERROR,
675 CD_SENSOR_ERROR_NO_SUPPORT,
676 "failed to dump: %s",
677 error->message);
678 return;
679 }
680 }
681
682 g_dbus_method_invocation_return_value (invocation, NULL);
683 }
684
685 static void
686 cd_sensor_unlock_cb (GObject *source_object,
687 GAsyncResult *res,
688 gpointer user_data)
689 {
690 CdSensor *sensor = CD_SENSOR (source_object);
691 CdSensorPrivate *priv = GET_PRIVATE (sensor);
692 GDBusMethodInvocation *invocation = (GDBusMethodInvocation *) user_data;
693 gboolean ret;
694 g_autoptr(GError) error = NULL;
695
696 /* set here to avoid every sensor doing this */
697 cd_sensor_set_state (sensor, CD_SENSOR_STATE_IDLE);
698
699 /* get the result */
700 if (priv->desc != NULL &&
701 priv->desc->unlock_finish != NULL) {
702 ret = priv->desc->unlock_finish (sensor, res, &error);
703 if (!ret) {
704 g_dbus_method_invocation_return_error (invocation,
705 CD_SENSOR_ERROR,
706 CD_SENSOR_ERROR_NO_SUPPORT,
707 "failed to unlock: %s",
708 error->message);
709 return;
710 }
711 }
712 cd_sensor_set_locked (sensor, FALSE);
713 g_dbus_method_invocation_return_value (invocation, NULL);
714 }
715
716 static void
717 cd_sensor_unlock_quietly_cb (GObject *source_object,
718 GAsyncResult *res,
719 gpointer user_data)
720 {
721 CdSensor *sensor = CD_SENSOR (source_object);
722 CdSensorPrivate *priv = GET_PRIVATE (sensor);
723 gboolean ret;
724 g_autoptr(GError) error = NULL;
725
726 /* set here to avoid every sensor doing this */
727 cd_sensor_set_state (sensor, CD_SENSOR_STATE_IDLE);
728
729 /* get the result */
730 if (priv->desc != NULL &&
731 priv->desc->unlock_finish != NULL) {
732 ret = priv->desc->unlock_finish (sensor, res, &error);
733 if (!ret) {
734 g_warning ("failed to unlock: %s",
735 error->message);
736 return;
737 }
738 }
739 cd_sensor_set_locked (sensor, FALSE);
740 }
741
742 static void
743 cd_sensor_name_vanished_cb (GDBusConnection *connection,
744 const gchar *name,
745 gpointer user_data)
746 {
747 CdSensor *sensor = CD_SENSOR (user_data);
748 CdSensorPrivate *priv = GET_PRIVATE (sensor);
749
750 /* dummy */
751 g_debug ("locked sender has vanished without doing Unlock()!");
752 if (priv->desc == NULL ||
753 priv->desc->unlock_async == NULL) {
754 cd_sensor_set_locked (sensor, FALSE);
755 goto out;
756 }
757
758 /* no longer valid */
759 priv->desc->unlock_async (sensor,
760 NULL,
761 cd_sensor_unlock_quietly_cb,
762 NULL);
763 out:
764 priv->watcher_id = 0;
765 }
766
767 static void
768 cd_sensor_dbus_method_call (GDBusConnection *connection, const gchar *sender,
769 const gchar *object_path, const gchar *interface_name,
770 const gchar *method_name, GVariant *parameters,
771 GDBusMethodInvocation *invocation, gpointer user_data)
772 {
773 CdSensorCap cap;
774 CdSensor *sensor = CD_SENSOR (user_data);
775 CdSensorPrivate *priv = GET_PRIVATE (sensor);
776 GVariantIter iter;
777 GVariant *value;
778 const gchar *cap_tmp = NULL;
779 gboolean ret;
780 gchar *key;
781 g_autoptr(GError) error = NULL;
782
783 /* return '' */
784 if (g_strcmp0 (method_name, "Lock") == 0) {
785
786 g_debug ("CdSensor %s:Lock()", sender);
787
788 /* check locked */
789 if (priv->locked) {
790 g_dbus_method_invocation_return_error (invocation,
791 CD_SENSOR_ERROR,
792 CD_SENSOR_ERROR_ALREADY_LOCKED,
793 "sensor is already locked");
794 return;
795 }
796
797 /* require auth */
798 ret = cd_main_sender_authenticated (connection,
799 sender,
800 "org.freedesktop.color-manager.sensor-lock",
801 &error);
802 if (!ret) {
803 g_dbus_method_invocation_return_error (invocation,
804 CD_SENSOR_ERROR,
805 CD_SENSOR_ERROR_FAILED_TO_AUTHENTICATE,
806 "%s", error->message);
807 return;
808 }
809
810 /* watch this bus name */
811 priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
812 sender,
813 G_BUS_NAME_WATCHER_FLAGS_NONE,
814 NULL,
815 cd_sensor_name_vanished_cb,
816 sensor,
817 NULL);
818
819 /* no support */
820 if (priv->desc == NULL ||
821 priv->desc->lock_async == NULL) {
822 cd_sensor_set_locked (sensor, TRUE);
823 g_dbus_method_invocation_return_value (invocation, NULL);
824 return;
825 }
826
827 /* proxy */
828 priv->desc->lock_async (sensor,
829 NULL,
830 cd_sensor_lock_cb,
831 invocation);
832 return;
833 }
834
835 if (g_strcmp0 (method_name, "Unlock") == 0) {
836
837 g_debug ("CdSensor %s:Unlock()", sender);
838
839 /* check locked */
840 if (!priv->locked) {
841 g_dbus_method_invocation_return_error (invocation,
842 CD_SENSOR_ERROR,
843 CD_SENSOR_ERROR_NOT_LOCKED,
844 "sensor is not yet locked");
845 return;
846 }
847
848 /* require auth */
849 ret = cd_main_sender_authenticated (connection,
850 sender,
851 "org.freedesktop.color-manager.sensor-lock",
852 &error);
853 if (!ret) {
854 g_dbus_method_invocation_return_error (invocation,
855 CD_SENSOR_ERROR,
856 CD_SENSOR_ERROR_FAILED_TO_AUTHENTICATE,
857 "%s", error->message);
858 return;
859 }
860
861 /* un-watch this bus name */
862 if (priv->watcher_id != 0) {
863 g_bus_unwatch_name (priv->watcher_id);
864 priv->watcher_id = 0;
865 }
866
867 /* no support */
868 if (priv->desc == NULL ||
869 priv->desc->unlock_async == NULL) {
870 cd_sensor_set_locked (sensor, FALSE);
871 g_dbus_method_invocation_return_value (invocation, NULL);
872 return;
873 }
874
875 /* proxy */
876 priv->desc->unlock_async (sensor,
877 NULL,
878 cd_sensor_unlock_cb,
879 invocation);
880 return;
881 }
882
883 /* return 'ddd,d' */
884 if (g_strcmp0 (method_name, "GetSample") == 0) {
885
886 g_debug ("CdSensor %s:GetSample()", sender);
887
888 /* check locked */
889 if (!priv->locked) {
890 g_dbus_method_invocation_return_error (invocation,
891 CD_SENSOR_ERROR,
892 CD_SENSOR_ERROR_NOT_LOCKED,
893 "sensor is not yet locked");
894 return;
895 }
896
897 /* check idle */
898 if (priv->state != CD_SENSOR_STATE_IDLE) {
899 g_dbus_method_invocation_return_error (invocation,
900 CD_SENSOR_ERROR,
901 CD_SENSOR_ERROR_IN_USE,
902 "sensor not idle: %s",
903 cd_sensor_state_to_string (priv->state));
904 return;
905 }
906
907 /* no support */
908 if (priv->desc == NULL ||
909 priv->desc->get_sample_async == NULL) {
910 g_dbus_method_invocation_return_error (invocation,
911 CD_SENSOR_ERROR,
912 CD_SENSOR_ERROR_NO_SUPPORT,
913 "no sensor->get_sample");
914 return;
915 }
916
917 /* get the type */
918 g_variant_get (parameters, "(&s)", &cap_tmp);
919 cap = cd_sensor_cap_from_string (cap_tmp);
920 if (cap == CD_SENSOR_CAP_UNKNOWN) {
921 g_dbus_method_invocation_return_error (invocation,
922 CD_SENSOR_ERROR,
923 CD_SENSOR_ERROR_INTERNAL,
924 "cap '%s' unknown",
925 cap_tmp);
926 return;
927 }
928
929 /* check type */
930 if (cap == CD_SENSOR_CAP_SPECTRAL) {
931 g_dbus_method_invocation_return_error (invocation,
932 CD_SENSOR_ERROR,
933 CD_SENSOR_ERROR_INTERNAL,
934 "cannot return spectral");
935 return;
936 }
937
938 /* proxy */
939 priv->desc->get_sample_async (sensor,
940 cap,
941 NULL,
942 cd_sensor_get_sample_cb,
943 invocation);
944 return;
945 }
946
947 /* return 'ddad' */
948 if (g_strcmp0 (method_name, "GetSpectrum") == 0) {
949
950 g_debug ("CdSensor %s:GetSpectrum()", sender);
951
952 /* check locked */
953 if (!priv->locked) {
954 g_dbus_method_invocation_return_error (invocation,
955 CD_SENSOR_ERROR,
956 CD_SENSOR_ERROR_NOT_LOCKED,
957 "sensor is not yet locked");
958 return;
959 }
960
961 /* check idle */
962 if (priv->state != CD_SENSOR_STATE_IDLE) {
963 g_dbus_method_invocation_return_error (invocation,
964 CD_SENSOR_ERROR,
965 CD_SENSOR_ERROR_IN_USE,
966 "sensor not idle: %s",
967 cd_sensor_state_to_string (priv->state));
968 return;
969 }
970
971 /* no support */
972 if (priv->desc == NULL ||
973 priv->desc->get_spectrum_async == NULL) {
974 g_dbus_method_invocation_return_error (invocation,
975 CD_SENSOR_ERROR,
976 CD_SENSOR_ERROR_NO_SUPPORT,
977 "no sensor->get_sample");
978 return;
979 }
980
981 /* get the type */
982 g_variant_get (parameters, "(&s)", &cap_tmp);
983 cap = cd_sensor_cap_from_string (cap_tmp);
984 if (cap == CD_SENSOR_CAP_UNKNOWN) {
985 g_dbus_method_invocation_return_error (invocation,
986 CD_SENSOR_ERROR,
987 CD_SENSOR_ERROR_INTERNAL,
988 "cap '%s' unknown",
989 cap_tmp);
990 return;
991 }
992
993 /* check type */
994 if (cap != CD_SENSOR_CAP_SPECTRAL &&
995 cap != CD_SENSOR_CAP_CALIBRATION_DARK &&
996 cap != CD_SENSOR_CAP_CALIBRATION_IRRADIANCE) {
997 g_dbus_method_invocation_return_error (invocation,
998 CD_SENSOR_ERROR,
999 CD_SENSOR_ERROR_INTERNAL,
1000 "invalid cap, only spectral "
1001 "or calibration type supported");
1002 return;
1003 }
1004
1005 /* proxy */
1006 priv->desc->get_spectrum_async (sensor,
1007 cap,
1008 NULL,
1009 cd_sensor_get_spectrum_cb,
1010 invocation);
1011 return;
1012 }
1013
1014 /* return '' */
1015 if (g_strcmp0 (method_name, "SetOptions") == 0) {
1016
1017 g_autoptr(GHashTable) options = NULL;
1018 g_autoptr(GVariant) result = NULL;
1019
1020 g_debug ("CdSensor %s:SetOptions()", sender);
1021
1022 /* check locked */
1023 if (!priv->locked) {
1024 g_dbus_method_invocation_return_error (invocation,
1025 CD_SENSOR_ERROR,
1026 CD_SENSOR_ERROR_NOT_LOCKED,
1027 "sensor is not yet locked");
1028 return;
1029 }
1030
1031 /* check idle */
1032 if (priv->state != CD_SENSOR_STATE_IDLE) {
1033 g_dbus_method_invocation_return_error (invocation,
1034 CD_SENSOR_ERROR,
1035 CD_SENSOR_ERROR_IN_USE,
1036 "sensor not idle: %s",
1037 cd_sensor_state_to_string (priv->state));
1038 return;
1039 }
1040
1041 /* no support */
1042 if (priv->desc == NULL ||
1043 priv->desc->set_options_async == NULL) {
1044 g_dbus_method_invocation_return_error (invocation,
1045 CD_SENSOR_ERROR,
1046 CD_SENSOR_ERROR_NO_SUPPORT,
1047 "no sensor options support");
1048 return;
1049 }
1050
1051 /* unwrap the parameters into a hash table */
1052 options = g_hash_table_new_full (g_str_hash, g_str_equal,
1053 g_free, (GDestroyNotify) g_variant_unref);
1054 result = g_variant_get_child_value (parameters, 0);
1055 g_variant_iter_init (&iter, result);
1056 while (g_variant_iter_next (&iter, "{sv}", &key, &value))
1057 g_hash_table_insert (options, key, value);
1058
1059 /* proxy */
1060 priv->desc->set_options_async (sensor,
1061 options,
1062 NULL,
1063 cd_sensor_set_options_cb,
1064 invocation);
1065 return;
1066 }
1067
1068 /* we suck */
1069 g_critical ("failed to process sensor method %s", method_name);
1070 }
1071
1072 static GVariant *
1073 cd_sensor_get_options_as_variant (CdSensor *sensor)
1074 {
1075 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1076 GList *l;
1077 GVariantBuilder builder;
1078 g_autoptr(GList) list = NULL;
1079
1080 /* do not try to build an empty array */
1081 if (g_hash_table_size (priv->options) == 0)
1082 return g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
1083
1084 /* add all the keys in the dictionary to the variant builder */
1085 list = g_hash_table_get_keys (priv->options);
1086 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1087 for (l = list; l != NULL; l = l->next) {
1088 g_variant_builder_add (&builder,
1089 "{sv}",
1090 l->data,
1091 g_hash_table_lookup (priv->options,
1092 l->data));
1093 }
1094 return g_variant_builder_end (&builder);
1095 }
1096
1097 static GVariant *
1098 cd_sensor_get_metadata_as_variant (CdSensor *sensor)
1099 {
1100 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1101 GList *l;
1102 GVariantBuilder builder;
1103 g_autoptr(GList) list = NULL;
1104
1105 /* we always must have at least one bit of metadata */
1106 if (g_hash_table_size (priv->metadata) == 0)
1107 return g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
1108
1109 /* add all the keys in the dictionary to the variant builder */
1110 list = g_hash_table_get_keys (priv->metadata);
1111 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
1112 for (l = list; l != NULL; l = l->next) {
1113 g_variant_builder_add (&builder,
1114 "{ss}",
1115 l->data,
1116 g_hash_table_lookup (priv->metadata,
1117 l->data));
1118 }
1119 return g_variant_builder_end (&builder);
1120 }
1121
1122 static GVariant *
1123 cd_sensor_get_nullable_for_string (const gchar *value)
1124 {
1125 if (value == NULL)
1126 return g_variant_new_string ("");
1127 return g_variant_new_string (value);
1128 }
1129
1130 static GVariant *
1131 cd_sensor_get_variant_for_caps (guint64 caps)
1132 {
1133 guint i;
1134 GVariantBuilder builder;
1135
1136 g_variant_builder_init (&builder, G_VARIANT_TYPE ("as"));
1137 for (i = 0; i < CD_SENSOR_CAP_LAST; i++) {
1138 if (!cd_bitfield_contain (caps, i))
1139 continue;
1140 g_variant_builder_add (&builder, "s",
1141 cd_sensor_cap_to_string (i));
1142 }
1143 return g_variant_new ("as", &builder);
1144 }
1145
1146 static GVariant *
1147 cd_sensor_dbus_get_property (GDBusConnection *connection_, const gchar *sender,
1148 const gchar *object_path, const gchar *interface_name,
1149 const gchar *property_name, GError **error,
1150 gpointer user_data)
1151 {
1152 CdSensor *sensor = CD_SENSOR (user_data);
1153 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1154
1155 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_ID) == 0)
1156 return g_variant_new_string (priv->id);
1157 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_KIND) == 0)
1158 return g_variant_new_string (cd_sensor_kind_to_string (priv->kind));
1159 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_STATE) == 0)
1160 return g_variant_new_string (cd_sensor_state_to_string (priv->state));
1161 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_MODE) == 0)
1162 return g_variant_new_string (cd_sensor_cap_to_string (priv->mode));
1163 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_SERIAL) == 0)
1164 return cd_sensor_get_nullable_for_string (priv->serial);
1165 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_MODEL) == 0)
1166 return cd_sensor_get_nullable_for_string (priv->model);
1167 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_VENDOR) == 0)
1168 return cd_sensor_get_nullable_for_string (priv->vendor);
1169 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_NATIVE) == 0)
1170 return g_variant_new_boolean (priv->native);
1171 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_LOCKED) == 0)
1172 return g_variant_new_boolean (priv->locked);
1173 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_EMBEDDED) == 0)
1174 return g_variant_new_boolean (priv->embedded);
1175 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_CAPABILITIES) == 0)
1176 return cd_sensor_get_variant_for_caps (priv->caps);
1177 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_OPTIONS) == 0)
1178 return cd_sensor_get_options_as_variant (sensor);
1179 if (g_strcmp0 (property_name, CD_SENSOR_PROPERTY_METADATA) == 0)
1180 return cd_sensor_get_metadata_as_variant (sensor);
1181
1182 /* return an error */
1183 g_set_error (error,
1184 CD_SENSOR_ERROR,
1185 CD_SENSOR_ERROR_INTERNAL,
1186 "failed to get sensor property %s",
1187 property_name);
1188 return NULL;
1189 }
1190
1191 gboolean
1192 cd_sensor_register_object (CdSensor *sensor,
1193 GDBusConnection *connection,
1194 GDBusInterfaceInfo *info,
1195 GError **error)
1196 {
1197 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1198 g_autoptr(GError) error_local = NULL;
1199
1200 static const GDBusInterfaceVTable interface_vtable = {
1201 cd_sensor_dbus_method_call,
1202 cd_sensor_dbus_get_property,
1203 NULL
1204 };
1205
1206 priv->connection = connection;
1207 priv->registration_id = g_dbus_connection_register_object (
1208 connection,
1209 priv->object_path,
1210 info,
1211 &interface_vtable,
1212 sensor, /* user_data */
1213 NULL, /* user_data_free_func */
1214 &error_local); /* GError** */
1215 if (priv->registration_id == 0) {
1216 g_set_error (error,
1217 CD_SENSOR_ERROR,
1218 CD_SENSOR_ERROR_INTERNAL,
1219 "failed to register object: %s",
1220 error_local->message);
1221 return FALSE;
1222 }
1223 g_debug ("CdSensor: Register interface %u on %s",
1224 priv->registration_id,
1225 priv->object_path);
1226 return TRUE;
1227 }
1228
1229 const gchar *
1230 cd_sensor_get_device_path (CdSensor *sensor)
1231 {
1232 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1233 return g_udev_device_get_sysfs_path (priv->device);
1234 }
1235
1236 const gchar *
1237 cd_sensor_get_usb_path (CdSensor *sensor)
1238 {
1239 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1240 return priv->usb_path;
1241 }
1242
1243 GUsbDevice *
1244 cd_sensor_open_usb_device (CdSensor *sensor,
1245 gint config,
1246 gint interface,
1247 GError **error)
1248 {
1249 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1250 guint8 busnum;
1251 guint8 devnum;
1252 g_autoptr(GUsbDevice) device = NULL;
1253
1254 /* convert from GUdevDevice to GUsbDevice */
1255 busnum = g_udev_device_get_sysfs_attr_as_int (priv->device, "busnum");
1256 devnum = g_udev_device_get_sysfs_attr_as_int (priv->device, "devnum");
1257 device = g_usb_context_find_by_bus_address (priv->usb_ctx,
1258 busnum, devnum, error);
1259 if (device == NULL)
1260 return NULL;
1261
1262 /* open device, set config and claim interface */
1263 if (!g_usb_device_open (device, error))
1264 return NULL;
1265 if (!g_usb_device_set_configuration (device, config, error))
1266 return NULL;
1267 if (!g_usb_device_claim_interface (device, interface,
1268 G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
1269 error)) {
1270 return NULL;
1271 }
1272 return g_object_ref (device);
1273 }
1274
1275 void
1276 cd_sensor_add_cap (CdSensor *sensor, CdSensorCap cap)
1277 {
1278 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1279 cd_bitfield_add (priv->caps, cap);
1280 cd_sensor_dbus_emit_property_changed (sensor,
1281 "Capabilities",
1282 cd_sensor_get_variant_for_caps (priv->caps));
1283
1284 }
1285
1286 GUdevDevice *
1287 cd_sensor_get_device (CdSensor *sensor)
1288 {
1289 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1290 return priv->device;
1291 }
1292
1293 static void
1294 cd_sensor_set_model (CdSensor *sensor,
1295 const gchar *model)
1296 {
1297 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1298 if (g_strcmp0 (model, "colormunki") == 0)
1299 model = "ColorMunki";
1300 priv->model = g_strdup (model);
1301 }
1302
1303 gboolean
1304 cd_sensor_set_from_device (CdSensor *sensor,
1305 GUdevDevice *device,
1306 GError **error)
1307 {
1308 CdSensorCap cap;
1309 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1310 const gchar *images[] = { "attach", "calibrate", "screen", NULL };
1311 const gchar *images_md[] = { CD_SENSOR_METADATA_IMAGE_ATTACH,
1312 CD_SENSOR_METADATA_IMAGE_CALIBRATE,
1313 CD_SENSOR_METADATA_IMAGE_SCREEN,
1314 NULL };
1315 const gchar *kind_str;
1316 const gchar *model_tmp = NULL;
1317 const gchar *vendor_tmp = NULL;
1318 const gchar * const *caps_str;
1319 gboolean use_database;
1320 gchar *tmp;
1321 guint i;
1322 guint8 busnum;
1323 guint8 devnum;
1324
1325 /* only use the database if we found both the VID and the PID */
1326 use_database = g_udev_device_has_property (device, "ID_VENDOR_FROM_DATABASE") &&
1327 g_udev_device_has_property (device, "ID_MODEL_FROM_DATABASE");
1328
1329 /* vendor */
1330 if (use_database)
1331 vendor_tmp = g_udev_device_get_property (device, "ID_VENDOR_FROM_DATABASE");
1332 if (vendor_tmp == NULL)
1333 vendor_tmp = g_udev_device_get_property (device, "ID_VENDOR");
1334 if (vendor_tmp == NULL)
1335 vendor_tmp = g_udev_device_get_sysfs_attr (device, "manufacturer");
1336 if (vendor_tmp == NULL)
1337 vendor_tmp = "unknown";
1338 priv->vendor = g_strdup (vendor_tmp);
1339
1340 /* make name sane */
1341 g_strdelimit (priv->vendor, "_", ' ');
1342
1343 /* model */
1344 if (use_database)
1345 model_tmp = g_udev_device_get_property (device, "ID_MODEL_FROM_DATABASE");
1346 if (model_tmp == NULL)
1347 model_tmp = g_udev_device_get_property (device, "ID_MODEL");
1348 if (model_tmp == NULL)
1349 model_tmp = g_udev_device_get_sysfs_attr (device, "product");
1350 if (model_tmp == NULL)
1351 model_tmp = "Unknown";
1352 cd_sensor_set_model (sensor, model_tmp);
1353
1354 /* make name sane */
1355 g_strdelimit (priv->model, "_", ' ');
1356
1357 /* try to get type */
1358 kind_str = g_udev_device_get_property (device, "COLORD_SENSOR_KIND");
1359 priv->kind = cd_sensor_kind_from_string (kind_str);
1360 if (priv->kind == CD_SENSOR_KIND_UNKNOWN) {
1361 g_set_error (error, 1, 0,
1362 "failed to recognize color device: %s - %s",
1363 vendor_tmp, model_tmp);
1364 return FALSE;
1365 }
1366
1367 /* get caps */
1368 caps_str = g_udev_device_get_property_as_strv (device,
1369 "COLORD_SENSOR_CAPS");
1370 if (caps_str != NULL) {
1371 for (i = 0; caps_str[i] != NULL; i++) {
1372 cap = cd_sensor_cap_from_string (caps_str[i]);
1373 if (cap != CD_SENSOR_CAP_UNKNOWN) {
1374 cd_bitfield_add (priv->caps, cap);
1375 } else {
1376 g_warning ("Unknown sensor cap %s on %s",
1377 caps_str[i], kind_str);
1378 }
1379 }
1380 }
1381
1382 /* is the sensor embedded, e.g. on the W700? */
1383 if (g_udev_device_get_property_as_boolean (device, "COLORD_SENSOR_EMBEDDED"))
1384 priv->embedded = TRUE;
1385
1386 /* add image metadata if the files exist */
1387 for (i = 0; images[i] != NULL; i++) {
1388 tmp = g_strdup_printf ("%s/colord/icons/%s-%s.svg",
1389 DATADIR, kind_str, images[i]);
1390 if (g_file_test (tmp, G_FILE_TEST_EXISTS)) {
1391 g_debug ("helper image %s found", tmp);
1392 g_hash_table_insert (priv->metadata,
1393 g_strdup (images_md[i]),
1394 tmp);
1395 } else {
1396 g_debug ("helper image %s not found", tmp);
1397 g_free (tmp);
1398 }
1399 }
1400
1401 /* some properties might not be valid in the GUdevDevice if the
1402 * device changes as this is only a snapshot */
1403 priv->device = g_object_ref (device);
1404
1405 /* create USB path */
1406 busnum = g_udev_device_get_sysfs_attr_as_int (priv->device, "busnum");
1407 devnum = g_udev_device_get_sysfs_attr_as_int (priv->device, "devnum");
1408 priv->usb_path = g_strdup_printf ("/dev/bus/usb/%03i/%03i",
1409 busnum, devnum);
1410
1411 /* success */
1412 return TRUE;
1413 }
1414
1415 void
1416 cd_sensor_set_index (CdSensor *sensor,
1417 guint idx)
1418 {
1419 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1420 g_autofree gchar *id = NULL;
1421 id = g_strdup_printf ("%s-%02u",
1422 cd_sensor_kind_to_string (priv->kind),
1423 idx);
1424 cd_sensor_set_id (sensor, id);
1425 }
1426
1427 void
1428 cd_sensor_add_option (CdSensor *sensor,
1429 const gchar *key,
1430 GVariant *value)
1431 {
1432 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1433 GVariant *options;
1434 g_hash_table_insert (priv->options,
1435 g_strdup (key),
1436 g_variant_ref_sink (value));
1437
1438 /* update clients */
1439 options = cd_sensor_get_options_as_variant (sensor);
1440 cd_sensor_dbus_emit_property_changed (sensor,
1441 "Options",
1442 options);
1443 }
1444
1445 /**
1446 * cd_sensor_debug_data:
1447 * @debug_mode: the debug mode, e.g %CD_SENSOR_DEBUG_MODE_REQUEST
1448 * @data: the data of size @length
1449 * @length: the size of data
1450 *
1451 * Prints some debugging of the request to the console if debugging mode
1452 * is enabled.
1453 **/
1454 void
1455 cd_sensor_debug_data (CdSensorDebugMode debug_mode,
1456 const guint8 *data,
1457 gsize length)
1458 {
1459 guint i;
1460 if (debug_mode == CD_SENSOR_DEBUG_MODE_REQUEST)
1461 g_print ("%c[%dm request\t", 0x1B, 31);
1462 else if (debug_mode == CD_SENSOR_DEBUG_MODE_RESPONSE)
1463 g_print ("%c[%dm response\t", 0x1B, 34);
1464 for (i = 0; i < length; i++)
1465 g_print ("%02x [%c]\t", data[i], g_ascii_isprint (data[i]) ? data[i] : '?');
1466 g_print ("%c[%dm\n", 0x1B, 0);
1467 }
1468
1469 static void
1470 cd_sensor_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
1471 {
1472 CdSensor *sensor = CD_SENSOR (object);
1473 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1474
1475 switch (prop_id) {
1476 case PROP_OBJECT_PATH:
1477 g_value_set_string (value, priv->object_path);
1478 break;
1479 default:
1480 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1481 break;
1482 }
1483 }
1484
1485 static void
1486 cd_sensor_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
1487 {
1488 CdSensor *sensor = CD_SENSOR (object);
1489 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1490
1491 switch (prop_id) {
1492 case PROP_OBJECT_PATH:
1493 g_free (priv->object_path);
1494 priv->object_path = g_strdup (g_value_get_string (value));
1495 break;
1496 case PROP_MODEL:
1497 g_free (priv->model);
1498 priv->model = g_strdup (g_value_get_string (value));
1499 break;
1500 case PROP_VENDOR:
1501 g_free (priv->vendor);
1502 priv->vendor = g_strdup (g_value_get_string (value));
1503 break;
1504 case PROP_ID:
1505 cd_sensor_set_id (sensor, g_value_get_string (value));
1506 break;
1507 case PROP_NATIVE:
1508 priv->native = g_value_get_boolean (value);
1509 break;
1510 case PROP_LOCKED:
1511 priv->locked = g_value_get_boolean (value);
1512 break;
1513 case PROP_STATE:
1514 priv->state = g_value_get_uint (value);
1515 break;
1516 case PROP_MODE:
1517 priv->mode = g_value_get_uint (value);
1518 break;
1519 case PROP_KIND:
1520 cd_sensor_set_kind (sensor, g_value_get_uint (value));
1521 break;
1522 case PROP_CAPS:
1523 priv->caps = g_value_get_uint64 (value);
1524 break;
1525 case PROP_SERIAL:
1526 cd_sensor_set_serial (sensor, g_value_get_string (value));
1527 break;
1528 default:
1529 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1530 break;
1531 }
1532 }
1533
1534 static void
1535 cd_sensor_class_init (CdSensorClass *klass)
1536 {
1537 GParamSpec *pspec;
1538 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1539 object_class->finalize = cd_sensor_finalize;
1540 object_class->get_property = cd_sensor_get_property;
1541 object_class->set_property = cd_sensor_set_property;
1542
1543 /**
1544 * CdSensor:object-path:
1545 */
1546 pspec = g_param_spec_string ("object-path", NULL, NULL,
1547 NULL,
1548 G_PARAM_READWRITE);
1549 g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec);
1550
1551 /**
1552 * CdSensor:id:
1553 */
1554 pspec = g_param_spec_string ("id", NULL, NULL,
1555 NULL,
1556 G_PARAM_READWRITE);
1557 g_object_class_install_property (object_class, PROP_ID, pspec);
1558
1559 /**
1560 * CdSensor:serial:
1561 */
1562 pspec = g_param_spec_string ("serial", NULL, NULL,
1563 NULL,
1564 G_PARAM_READWRITE);
1565 g_object_class_install_property (object_class, PROP_SERIAL, pspec);
1566
1567 /**
1568 * CdSensor:model:
1569 */
1570 pspec = g_param_spec_string ("model", NULL, NULL,
1571 NULL,
1572 G_PARAM_READWRITE);
1573 g_object_class_install_property (object_class, PROP_MODEL, pspec);
1574
1575 /**
1576 * CdSensor:vendor:
1577 */
1578 pspec = g_param_spec_string ("vendor", NULL, NULL,
1579 NULL,
1580 G_PARAM_READWRITE);
1581 g_object_class_install_property (object_class, PROP_VENDOR, pspec);
1582
1583 /**
1584 * CdSensor:caps:
1585 */
1586 pspec = g_param_spec_uint64 ("caps", NULL, NULL,
1587 0, G_MAXUINT64, 0,
1588 G_PARAM_READWRITE);
1589 g_object_class_install_property (object_class, PROP_CAPS, pspec);
1590
1591 /**
1592 * CdSensor:kind:
1593 */
1594 pspec = g_param_spec_uint ("kind", NULL, NULL,
1595 0, G_MAXUINT, CD_SENSOR_KIND_UNKNOWN,
1596 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
1597 g_object_class_install_property (object_class, PROP_KIND, pspec);
1598
1599 /**
1600 * CdSensor:native:
1601 */
1602 pspec = g_param_spec_boolean ("native", NULL, NULL,
1603 FALSE,
1604 G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
1605 g_object_class_install_property (object_class, PROP_NATIVE, pspec);
1606
1607 /**
1608 * CdSensor:locked:
1609 */
1610 pspec = g_param_spec_boolean ("locked", NULL, NULL,
1611 FALSE,
1612 G_PARAM_READWRITE);
1613 g_object_class_install_property (object_class, PROP_LOCKED, pspec);
1614 }
1615
1616 static void
1617 cd_sensor_init (CdSensor *sensor)
1618 {
1619 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1620 priv->state = CD_SENSOR_STATE_IDLE;
1621 priv->mode = CD_SENSOR_CAP_UNKNOWN;
1622 priv->usb_ctx = g_usb_context_new (NULL);
1623 priv->options = g_hash_table_new_full (g_str_hash,
1624 g_str_equal,
1625 (GDestroyNotify) g_free,
1626 (GDestroyNotify) g_variant_unref);
1627 priv->metadata = g_hash_table_new_full (g_str_hash,
1628 g_str_equal,
1629 g_free,
1630 g_free);
1631 }
1632
1633 static void
1634 cd_sensor_finalize (GObject *object)
1635 {
1636 CdSensor *sensor = CD_SENSOR (object);
1637 CdSensorPrivate *priv = GET_PRIVATE (sensor);
1638
1639 if (priv->registration_id > 0) {
1640 g_debug ("CdSensor: Unregister interface %u on %s",
1641 priv->registration_id,
1642 priv->object_path);
1643 g_dbus_connection_unregister_object (priv->connection,
1644 priv->registration_id);
1645 }
1646 if (priv->watcher_id != 0)
1647 g_bus_unwatch_name (priv->watcher_id);
1648 if (priv->set_state_id > 0)
1649 g_source_remove (priv->set_state_id);
1650 g_free (priv->model);
1651 g_free (priv->vendor);
1652 g_free (priv->serial);
1653 g_free (priv->id);
1654 g_free (priv->object_path);
1655 g_free (priv->usb_path);
1656 g_hash_table_unref (priv->options);
1657 g_hash_table_unref (priv->metadata);
1658 g_object_unref (priv->usb_ctx);
1659 if (priv->device != NULL)
1660 g_object_unref (priv->device);
1661
1662 G_OBJECT_CLASS (cd_sensor_parent_class)->finalize (object);
1663 }
1664
1665 CdSensor *
1666 cd_sensor_new (void)
1667 {
1668 CdSensor *sensor;
1669 sensor = g_object_new (CD_TYPE_SENSOR, NULL);
1670 return CD_SENSOR (sensor);
1671 }
1672