| 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 <lcms2.h> |
| 27 |
|
| 28 |
#include "cd-sensor.h" |
| 29 |
|
| 30 |
typedef struct |
| 31 |
{ |
| 32 |
gboolean done_startup; |
| 33 |
CdColorRGB sample_fake; |
| 34 |
cmsHTRANSFORM transform_fake; |
| 35 |
} CdSensorDummyPrivate; |
| 36 |
|
| 37 |
static CdSensorDummyPrivate * |
| 38 |
cd_sensor_dummy_get_private (CdSensor *sensor) |
| 39 |
{ |
| 40 |
return g_object_get_data (G_OBJECT (sensor), "priv"); |
| 41 |
} |
| 42 |
|
| 43 |
static gboolean |
| 44 |
cd_sensor_get_ambient_wait_cb (GTask *unowned_task) |
| 45 |
{ |
| 46 |
g_autoptr(GTask) task = unowned_task; |
| 47 |
CdColorXYZ *sample = NULL; |
| 48 |
|
| 49 |
sample = cd_color_xyz_new (); |
| 50 |
sample->X = 7.7f; |
| 51 |
sample->Y = CD_SENSOR_NO_VALUE; |
| 52 |
sample->Z = CD_SENSOR_NO_VALUE; |
| 53 |
g_task_return_pointer (task, sample, (GDestroyNotify) cd_color_xyz_free); |
| 54 |
|
| 55 |
return G_SOURCE_REMOVE; |
| 56 |
} |
| 57 |
|
| 58 |
static gboolean |
| 59 |
cd_sensor_get_sample_wait_cb (GTask *unowned_task) |
| 60 |
{ |
| 61 |
g_autoptr(GTask) task = unowned_task; |
| 62 |
CdSensor *sensor = CD_SENSOR (g_task_get_source_object (task)); |
| 63 |
CdSensorDummyPrivate *priv = cd_sensor_dummy_get_private (sensor); |
| 64 |
CdColorXYZ *sample = NULL; |
| 65 |
g_autoptr(GError) error = NULL; |
| 66 |
|
| 67 |
/* never setup */ |
| 68 |
if (priv->transform_fake == NULL) { |
| 69 |
g_task_return_new_error (task, |
| 70 |
CD_SENSOR_ERROR, |
| 71 |
CD_SENSOR_ERROR_NO_SUPPORT, |
| 72 |
"no fake transfor set up"); |
| 73 |
return G_SOURCE_REMOVE; |
| 74 |
} |
| 75 |
|
| 76 |
/* run the sample through the profile */ |
| 77 |
sample = cd_color_xyz_new (); |
| 78 |
cmsDoTransform (priv->transform_fake, &priv->sample_fake, sample, 1); |
| 79 |
|
| 80 |
/* emulate */ |
| 81 |
cd_sensor_button_pressed (sensor); |
| 82 |
|
| 83 |
/* just return without a problem */ |
| 84 |
g_task_return_pointer (task, sample, (GDestroyNotify) cd_color_xyz_free); |
| 85 |
return G_SOURCE_REMOVE; |
| 86 |
} |
| 87 |
|
| 88 |
void |
| 89 |
cd_sensor_get_sample_async (CdSensor *sensor, |
| 90 |
CdSensorCap cap, |
| 91 |
GCancellable *cancellable, |
| 92 |
GAsyncReadyCallback callback, |
| 93 |
gpointer user_data) |
| 94 |
{ |
| 95 |
g_autoptr(GTask) task = NULL; |
| 96 |
|
| 97 |
g_return_if_fail (CD_IS_SENSOR (sensor)); |
| 98 |
|
| 99 |
task = g_task_new (sensor, cancellable, callback, user_data); |
| 100 |
|
| 101 |
/* set state */ |
| 102 |
cd_sensor_set_state (sensor, CD_SENSOR_STATE_MEASURING); |
| 103 |
|
| 104 |
/* just complete in idle */ |
| 105 |
if (cap != CD_SENSOR_CAP_AMBIENT) |
| 106 |
g_timeout_add_seconds (2, (GSourceFunc) cd_sensor_get_sample_wait_cb, g_steal_pointer(&task)); |
| 107 |
else |
| 108 |
g_timeout_add_seconds (2, (GSourceFunc) cd_sensor_get_ambient_wait_cb, g_steal_pointer(&task)); |
| 109 |
} |
| 110 |
|
| 111 |
CdColorXYZ * |
| 112 |
cd_sensor_get_sample_finish (CdSensor *sensor, GAsyncResult *res, GError **error) |
| 113 |
{ |
| 114 |
g_return_val_if_fail (g_task_is_valid (res, sensor), NULL); |
| 115 |
return g_task_propagate_pointer (G_TASK (res), error); |
| 116 |
} |
| 117 |
|
| 118 |
gboolean |
| 119 |
cd_sensor_set_options_finish (CdSensor *sensor, GAsyncResult *res, GError **error) |
| 120 |
{ |
| 121 |
g_return_val_if_fail (g_task_is_valid (res, sensor), FALSE); |
| 122 |
return g_task_propagate_boolean (G_TASK (res), error); |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
cd_sensor_set_options_async (CdSensor *sensor, |
| 127 |
GHashTable *options, |
| 128 |
GCancellable *cancellable, |
| 129 |
GAsyncReadyCallback callback, |
| 130 |
gpointer user_data) |
| 131 |
{ |
| 132 |
CdSensorDummyPrivate *priv = cd_sensor_dummy_get_private (sensor); |
| 133 |
GList *l; |
| 134 |
const gchar *key_name; |
| 135 |
GVariant *value; |
| 136 |
g_autoptr(GTask) task = NULL; |
| 137 |
g_autoptr(GList) keys = NULL; |
| 138 |
|
| 139 |
g_return_if_fail (CD_IS_SENSOR (sensor)); |
| 140 |
|
| 141 |
task = g_task_new (sensor, cancellable, callback, user_data); |
| 142 |
|
| 143 |
/* look for any keys we recognise */ |
| 144 |
keys = g_hash_table_get_keys (options); |
| 145 |
for (l = keys; l != NULL; l = l->next) { |
| 146 |
key_name = (const gchar *) l->data; |
| 147 |
value = g_hash_table_lookup (options, key_name); |
| 148 |
if (g_strcmp0 (g_variant_get_type_string (value), "d") != 0) { |
| 149 |
g_task_return_new_error (task, |
| 150 |
CD_SENSOR_ERROR, |
| 151 |
CD_SENSOR_ERROR_NO_SUPPORT, |
| 152 |
"unexpected type '%s' not supported", |
| 153 |
g_variant_get_type_string (value)); |
| 154 |
return; |
| 155 |
} |
| 156 |
if (g_strcmp0 (key_name, "sample[red]") == 0) { |
| 157 |
priv->sample_fake.R = g_variant_get_double (value); |
| 158 |
} else if (g_strcmp0 (key_name, "sample[green]") == 0) { |
| 159 |
priv->sample_fake.G = g_variant_get_double (value); |
| 160 |
} else if (g_strcmp0 (key_name, "sample[blue]") == 0) { |
| 161 |
priv->sample_fake.B = g_variant_get_double (value); |
| 162 |
} else { |
| 163 |
g_task_return_new_error (task, |
| 164 |
CD_SENSOR_ERROR, |
| 165 |
CD_SENSOR_ERROR_NO_SUPPORT, |
| 166 |
"option '%s' is not supported", |
| 167 |
key_name); |
| 168 |
return; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* success */ |
| 173 |
g_task_return_boolean (task, TRUE); |
| 174 |
} |
| 175 |
|
| 176 |
static void |
| 177 |
cd_sensor_unref_private (CdSensorDummyPrivate *priv) |
| 178 |
{ |
| 179 |
if (priv->transform_fake != NULL) |
| 180 |
cmsDeleteTransform (priv->transform_fake); |
| 181 |
g_free (priv); |
| 182 |
} |
| 183 |
|
| 184 |
static cmsHTRANSFORM |
| 185 |
cd_sensor_get_fake_transform (CdSensorDummyPrivate *priv) |
| 186 |
{ |
| 187 |
cmsHTRANSFORM transform; |
| 188 |
cmsHPROFILE profile_srgb; |
| 189 |
cmsHPROFILE profile_xyz; |
| 190 |
|
| 191 |
profile_srgb = cmsCreate_sRGBProfile (); |
| 192 |
profile_xyz = cmsCreateXYZProfile (); |
| 193 |
transform = cmsCreateTransform (profile_srgb, TYPE_RGB_DBL, |
| 194 |
profile_xyz, TYPE_XYZ_DBL, |
| 195 |
INTENT_RELATIVE_COLORIMETRIC, |
| 196 |
cmsFLAGS_NOOPTIMIZE); |
| 197 |
if (transform == NULL) { |
| 198 |
g_warning ("failed to setup RGB -> XYZ transform"); |
| 199 |
goto out; |
| 200 |
} |
| 201 |
out: |
| 202 |
if (profile_srgb != NULL) |
| 203 |
cmsCloseProfile (profile_srgb); |
| 204 |
if (profile_xyz != NULL) |
| 205 |
cmsCloseProfile (profile_xyz); |
| 206 |
return transform; |
| 207 |
} |
| 208 |
|
| 209 |
gboolean |
| 210 |
cd_sensor_coldplug (CdSensor *sensor, GError **error) |
| 211 |
{ |
| 212 |
CdSensorDummyPrivate *priv; |
| 213 |
guint64 caps = cd_bitfield_from_enums (CD_SENSOR_CAP_LCD, |
| 214 |
CD_SENSOR_CAP_CRT, |
| 215 |
CD_SENSOR_CAP_PROJECTOR, |
| 216 |
CD_SENSOR_CAP_SPOT, |
| 217 |
CD_SENSOR_CAP_PRINTER, |
| 218 |
CD_SENSOR_CAP_AMBIENT, |
| 219 |
-1); |
| 220 |
g_object_set (sensor, |
| 221 |
"id", "dummy", |
| 222 |
"kind", CD_SENSOR_KIND_DUMMY, |
| 223 |
"serial", "0123456789a", |
| 224 |
"model", "Dummy Sensor #1", |
| 225 |
"vendor", "Acme Corp", |
| 226 |
"caps", caps, |
| 227 |
"native", TRUE, |
| 228 |
NULL); |
| 229 |
|
| 230 |
/* create private data */ |
| 231 |
priv = g_new0 (CdSensorDummyPrivate, 1); |
| 232 |
priv->transform_fake = cd_sensor_get_fake_transform (priv); |
| 233 |
cd_color_rgb_set (&priv->sample_fake, 0.1, 0.2, 0.3); |
| 234 |
g_object_set_data_full (G_OBJECT (sensor), "priv", priv, |
| 235 |
(GDestroyNotify) cd_sensor_unref_private); |
| 236 |
return TRUE; |
| 237 |
} |
| 238 |
|