| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2010-2015 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 <gusb.h> |
| 27 |
#include <colord-private.h> |
| 28 |
|
| 29 |
#include "../src/cd-sensor.h" |
| 30 |
|
| 31 |
#include "huey-ctx.h" |
| 32 |
#include "huey-device.h" |
| 33 |
#include "huey-enum.h" |
| 34 |
|
| 35 |
typedef struct |
| 36 |
{ |
| 37 |
GUsbDevice *device; |
| 38 |
HueyCtx *ctx; |
| 39 |
} CdSensorHueyPrivate; |
| 40 |
|
| 41 |
static CdSensorHueyPrivate * |
| 42 |
cd_sensor_huey_get_private (CdSensor *sensor) |
| 43 |
{ |
| 44 |
return g_object_get_data (G_OBJECT (sensor), "priv"); |
| 45 |
} |
| 46 |
|
| 47 |
static void |
| 48 |
cd_sensor_huey_get_ambient_thread_cb (GTask *task, |
| 49 |
gpointer source_object, |
| 50 |
gpointer task_data, |
| 51 |
GCancellable *cancellable) |
| 52 |
{ |
| 53 |
g_autoptr(GError) error = NULL; |
| 54 |
CdSensor *sensor = CD_SENSOR (source_object); |
| 55 |
CdSensorHueyPrivate *priv = cd_sensor_huey_get_private (sensor); |
| 56 |
CdColorXYZ sample; |
| 57 |
|
| 58 |
/* set state */ |
| 59 |
cd_sensor_set_state_in_idle (sensor, CD_SENSOR_STATE_MEASURING); |
| 60 |
|
| 61 |
/* hit hardware */ |
| 62 |
cd_color_xyz_clear (&sample); |
| 63 |
sample.X = huey_device_get_ambient (priv->device, &error); |
| 64 |
if (sample.X < 0) { |
| 65 |
g_task_return_new_error (task, |
| 66 |
CD_SENSOR_ERROR, |
| 67 |
CD_SENSOR_ERROR_NO_DATA, |
| 68 |
"%s", error->message); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
/* success */ |
| 73 |
g_task_return_pointer (task, |
| 74 |
cd_color_xyz_dup (&sample), |
| 75 |
(GDestroyNotify) cd_color_xyz_free); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
cd_sensor_huey_sample_thread_cb (GTask *task, |
| 80 |
gpointer source_object, |
| 81 |
gpointer task_data, |
| 82 |
GCancellable *cancellable) |
| 83 |
{ |
| 84 |
CdSensor *sensor = CD_SENSOR (source_object); |
| 85 |
CdSensorHueyPrivate *priv = cd_sensor_huey_get_private (sensor); |
| 86 |
CdSensorCap cap = GPOINTER_TO_UINT (task_data); |
| 87 |
CdColorXYZ *sample; |
| 88 |
g_autoptr(GError) error = NULL; |
| 89 |
|
| 90 |
/* measure */ |
| 91 |
cd_sensor_set_state_in_idle (sensor, CD_SENSOR_STATE_MEASURING); |
| 92 |
sample = huey_ctx_take_sample (priv->ctx, cap, &error); |
| 93 |
if (sample == NULL) { |
| 94 |
g_task_return_new_error (task, |
| 95 |
CD_SENSOR_ERROR, |
| 96 |
CD_SENSOR_ERROR_NO_DATA, |
| 97 |
"%s", error->message); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
/* save result */ |
| 102 |
g_task_return_pointer (task, sample, NULL); |
| 103 |
} |
| 104 |
|
| 105 |
void |
| 106 |
cd_sensor_get_sample_async (CdSensor *sensor, |
| 107 |
CdSensorCap cap, |
| 108 |
GCancellable *cancellable, |
| 109 |
GAsyncReadyCallback callback, |
| 110 |
gpointer user_data) |
| 111 |
{ |
| 112 |
g_autoptr(GTask) task = NULL; |
| 113 |
g_return_if_fail (CD_IS_SENSOR (sensor)); |
| 114 |
task = g_task_new (sensor, cancellable, callback, user_data); |
| 115 |
g_task_set_task_data (task, GUINT_TO_POINTER (cap), NULL); |
| 116 |
if (cap == CD_SENSOR_CAP_AMBIENT) { |
| 117 |
g_task_run_in_thread (task, cd_sensor_huey_get_ambient_thread_cb); |
| 118 |
} else { |
| 119 |
g_task_run_in_thread (task, cd_sensor_huey_sample_thread_cb); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
CdColorXYZ * |
| 124 |
cd_sensor_get_sample_finish (CdSensor *sensor, GAsyncResult *res, GError **error) |
| 125 |
{ |
| 126 |
g_return_val_if_fail (g_task_is_valid (res, sensor), NULL); |
| 127 |
return g_task_propagate_pointer (G_TASK (res), error); |
| 128 |
} |
| 129 |
|
| 130 |
static void |
| 131 |
cd_sensor_huey_lock_thread_cb (GTask *task, |
| 132 |
gpointer source_object, |
| 133 |
gpointer task_data, |
| 134 |
GCancellable *cancellable) |
| 135 |
{ |
| 136 |
CdSensor *sensor = CD_SENSOR (source_object); |
| 137 |
CdSensorHueyPrivate *priv = cd_sensor_huey_get_private (sensor); |
| 138 |
const guint8 spin_leds[] = { 0x0, 0x1, 0x2, 0x4, 0x8, 0x4, 0x2, 0x1, 0x0, 0xff }; |
| 139 |
guint i; |
| 140 |
g_autoptr(GError) error = NULL; |
| 141 |
g_autofree gchar *serial_number_tmp = NULL; |
| 142 |
|
| 143 |
/* try to find the USB device */ |
| 144 |
priv->device = cd_sensor_open_usb_device (sensor, |
| 145 |
0x01, /* config */ |
| 146 |
0x00, /* interface */ |
| 147 |
&error); |
| 148 |
if (priv->device == NULL) { |
| 149 |
g_task_return_new_error (task, |
| 150 |
CD_SENSOR_ERROR, |
| 151 |
CD_SENSOR_ERROR_INTERNAL, |
| 152 |
"%s", error->message); |
| 153 |
goto out; |
| 154 |
} |
| 155 |
huey_ctx_set_device (priv->ctx, priv->device); |
| 156 |
|
| 157 |
/* set state */ |
| 158 |
cd_sensor_set_state_in_idle (sensor, CD_SENSOR_STATE_STARTING); |
| 159 |
|
| 160 |
/* unlock */ |
| 161 |
if (!huey_device_unlock (priv->device, &error)) { |
| 162 |
g_task_return_new_error (task, |
| 163 |
CD_SENSOR_ERROR, |
| 164 |
CD_SENSOR_ERROR_INTERNAL, |
| 165 |
"%s", error->message); |
| 166 |
goto out; |
| 167 |
} |
| 168 |
|
| 169 |
/* get serial number */ |
| 170 |
serial_number_tmp = huey_device_get_serial_number (priv->device, &error); |
| 171 |
if (serial_number_tmp == NULL) { |
| 172 |
g_task_return_new_error (task, |
| 173 |
CD_SENSOR_ERROR, |
| 174 |
CD_SENSOR_ERROR_NO_DATA, |
| 175 |
"%s", error->message); |
| 176 |
goto out; |
| 177 |
} |
| 178 |
cd_sensor_set_serial (sensor, serial_number_tmp); |
| 179 |
g_debug ("Serial number: %s", serial_number_tmp); |
| 180 |
|
| 181 |
/* setup sensor */ |
| 182 |
if (!huey_ctx_setup (priv->ctx, &error)) { |
| 183 |
g_task_return_new_error (task, |
| 184 |
CD_SENSOR_ERROR, |
| 185 |
CD_SENSOR_ERROR_INTERNAL, |
| 186 |
"%s", error->message); |
| 187 |
goto out; |
| 188 |
} |
| 189 |
|
| 190 |
/* spin the LEDs */ |
| 191 |
if (!cd_sensor_get_is_embedded (sensor)) { |
| 192 |
for (i = 0; spin_leds[i] != 0xff; i++) { |
| 193 |
if (!huey_device_set_leds (priv->device, spin_leds[i], &error)) { |
| 194 |
g_task_return_new_error (task, |
| 195 |
CD_SENSOR_ERROR, |
| 196 |
CD_SENSOR_ERROR_INTERNAL, |
| 197 |
"%s", error->message); |
| 198 |
goto out; |
| 199 |
} |
| 200 |
g_usleep (50000); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/* success */ |
| 205 |
g_task_return_boolean (task, TRUE); |
| 206 |
out: |
| 207 |
/* set state */ |
| 208 |
cd_sensor_set_state_in_idle (sensor, CD_SENSOR_STATE_IDLE); |
| 209 |
} |
| 210 |
|
| 211 |
void |
| 212 |
cd_sensor_lock_async (CdSensor *sensor, |
| 213 |
GCancellable *cancellable, |
| 214 |
GAsyncReadyCallback callback, |
| 215 |
gpointer user_data) |
| 216 |
{ |
| 217 |
g_autoptr(GTask) task = NULL; |
| 218 |
g_return_if_fail (CD_IS_SENSOR (sensor)); |
| 219 |
task = g_task_new (sensor, cancellable, callback, user_data); |
| 220 |
g_task_run_in_thread (task, cd_sensor_huey_lock_thread_cb); |
| 221 |
} |
| 222 |
|
| 223 |
gboolean |
| 224 |
cd_sensor_lock_finish (CdSensor *sensor, GAsyncResult *res, GError **error) |
| 225 |
{ |
| 226 |
g_return_val_if_fail (g_task_is_valid (res, sensor), FALSE); |
| 227 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 228 |
} |
| 229 |
|
| 230 |
static void |
| 231 |
cd_sensor_unlock_thread_cb (GTask *task, |
| 232 |
gpointer source_object, |
| 233 |
gpointer task_data, |
| 234 |
GCancellable *cancellable) |
| 235 |
{ |
| 236 |
CdSensor *sensor = CD_SENSOR (source_object); |
| 237 |
CdSensorHueyPrivate *priv = cd_sensor_huey_get_private (sensor); |
| 238 |
g_autoptr(GError) error = NULL; |
| 239 |
|
| 240 |
/* close */ |
| 241 |
if (priv->device != NULL) { |
| 242 |
if (!g_usb_device_close (priv->device, &error)) { |
| 243 |
g_task_return_new_error (task, |
| 244 |
CD_SENSOR_ERROR, |
| 245 |
CD_SENSOR_ERROR_INTERNAL, |
| 246 |
"%s", error->message); |
| 247 |
return; |
| 248 |
} |
| 249 |
g_clear_object (&priv->device); |
| 250 |
} |
| 251 |
|
| 252 |
/* success */ |
| 253 |
g_task_return_boolean (task, TRUE); |
| 254 |
} |
| 255 |
|
| 256 |
void |
| 257 |
cd_sensor_unlock_async (CdSensor *sensor, |
| 258 |
GCancellable *cancellable, |
| 259 |
GAsyncReadyCallback callback, |
| 260 |
gpointer user_data) |
| 261 |
{ |
| 262 |
g_autoptr(GTask) task = NULL; |
| 263 |
g_return_if_fail (CD_IS_SENSOR (sensor)); |
| 264 |
task = g_task_new (sensor, cancellable, callback, user_data); |
| 265 |
g_task_run_in_thread (task, cd_sensor_unlock_thread_cb); |
| 266 |
} |
| 267 |
|
| 268 |
gboolean |
| 269 |
cd_sensor_unlock_finish (CdSensor *sensor, GAsyncResult *res, GError **error) |
| 270 |
{ |
| 271 |
g_return_val_if_fail (g_task_is_valid (res, sensor), FALSE); |
| 272 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 273 |
} |
| 274 |
|
| 275 |
gboolean |
| 276 |
cd_sensor_dump_device (CdSensor *sensor, GString *data, GError **error) |
| 277 |
{ |
| 278 |
CdSensorHueyPrivate *priv = cd_sensor_huey_get_private (sensor); |
| 279 |
gboolean ret; |
| 280 |
guint i; |
| 281 |
guint8 value; |
| 282 |
gchar *tmp; |
| 283 |
const CdVec3 *vec; |
| 284 |
|
| 285 |
/* dump the unlock string */ |
| 286 |
g_string_append_printf (data, "huey-dump-version:%i\n", 2); |
| 287 |
g_string_append_printf (data, "unlock-string:%s\n", |
| 288 |
huey_ctx_get_unlock_string (priv->ctx)); |
| 289 |
g_string_append_printf (data, "calibration-value:%f\n", |
| 290 |
huey_ctx_get_calibration_value (priv->ctx)); |
| 291 |
vec = huey_ctx_get_dark_offset (priv->ctx); |
| 292 |
g_string_append_printf (data, "dark-offset:%f,%f,%f\n", |
| 293 |
vec->v0, vec->v1, vec->v2); |
| 294 |
|
| 295 |
/* dump the DeviceRGB to XYZ matrix */ |
| 296 |
tmp = cd_mat33_to_string (huey_ctx_get_calibration_lcd (priv->ctx)); |
| 297 |
g_string_append_printf (data, "calibration-lcd:%s\n", tmp); |
| 298 |
g_free (tmp); |
| 299 |
tmp = cd_mat33_to_string (huey_ctx_get_calibration_crt (priv->ctx)); |
| 300 |
g_string_append_printf (data, "calibration-crt:%s\n", tmp); |
| 301 |
g_free (tmp); |
| 302 |
|
| 303 |
/* read all the register space */ |
| 304 |
for (i = 0; i < 0xff; i++) { |
| 305 |
ret = huey_device_read_register_byte (priv->device, |
| 306 |
i, |
| 307 |
&value, |
| 308 |
error); |
| 309 |
if (!ret) |
| 310 |
return FALSE; |
| 311 |
|
| 312 |
/* write details */ |
| 313 |
g_string_append_printf (data, |
| 314 |
"register[0x%02x]:0x%02x\n", |
| 315 |
i, |
| 316 |
value); |
| 317 |
} |
| 318 |
return TRUE; |
| 319 |
} |
| 320 |
|
| 321 |
static void |
| 322 |
cd_sensor_unref_private (CdSensorHueyPrivate *priv) |
| 323 |
{ |
| 324 |
if (priv->device != NULL) |
| 325 |
g_object_unref (priv->device); |
| 326 |
g_object_unref (priv->ctx); |
| 327 |
g_free (priv); |
| 328 |
} |
| 329 |
|
| 330 |
gboolean |
| 331 |
cd_sensor_coldplug (CdSensor *sensor, GError **error) |
| 332 |
{ |
| 333 |
CdSensorHueyPrivate *priv; |
| 334 |
guint64 caps = cd_bitfield_from_enums (CD_SENSOR_CAP_LCD, |
| 335 |
CD_SENSOR_CAP_CRT, |
| 336 |
CD_SENSOR_CAP_AMBIENT, |
| 337 |
-1); |
| 338 |
g_object_set (sensor, |
| 339 |
"native", TRUE, |
| 340 |
"kind", CD_SENSOR_KIND_HUEY, |
| 341 |
"caps", caps, |
| 342 |
NULL); |
| 343 |
/* create private data */ |
| 344 |
priv = g_new0 (CdSensorHueyPrivate, 1); |
| 345 |
priv->ctx = huey_ctx_new (); |
| 346 |
g_object_set_data_full (G_OBJECT (sensor), "priv", priv, |
| 347 |
(GDestroyNotify) cd_sensor_unref_private); |
| 348 |
return TRUE; |
| 349 |
} |
| 350 |
|