| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2009-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 <gudev/gudev.h> |
| 27 |
|
| 28 |
#include "cd-sensor-client.h" |
| 29 |
#include "cd-sensor.h" |
| 30 |
|
| 31 |
static void cd_sensor_client_finalize (GObject *object); |
| 32 |
|
| 33 |
#define GET_PRIVATE(o) (cd_sensor_client_get_instance_private (o)) |
| 34 |
|
| 35 |
typedef struct |
| 36 |
{ |
| 37 |
GUdevClient *gudev_client; |
| 38 |
GPtrArray *array_sensors; |
| 39 |
guint idx; |
| 40 |
} CdSensorClientPrivate; |
| 41 |
|
| 42 |
enum { |
| 43 |
SIGNAL_SENSOR_ADDED, |
| 44 |
SIGNAL_SENSOR_REMOVED, |
| 45 |
SIGNAL_LAST |
| 46 |
}; |
| 47 |
|
| 48 |
static guint signals[SIGNAL_LAST] = { 0 }; |
| 49 |
|
| 50 |
G_DEFINE_TYPE_WITH_PRIVATE (CdSensorClient, cd_sensor_client, G_TYPE_OBJECT) |
| 51 |
|
| 52 |
CdSensor * |
| 53 |
cd_sensor_client_get_by_id (CdSensorClient *sensor_client, |
| 54 |
const gchar *sensor_id) |
| 55 |
{ |
| 56 |
CdSensorClientPrivate *priv = GET_PRIVATE (sensor_client); |
| 57 |
CdSensor *sensor = NULL; |
| 58 |
CdSensor *sensor_tmp; |
| 59 |
guint i; |
| 60 |
|
| 61 |
for (i = 0; i < priv->array_sensors->len; i++) { |
| 62 |
sensor_tmp = g_ptr_array_index (priv->array_sensors, i); |
| 63 |
if (g_strcmp0 (cd_sensor_get_id (sensor_tmp), sensor_id) == 0) { |
| 64 |
sensor = g_object_ref (sensor_tmp); |
| 65 |
break; |
| 66 |
} |
| 67 |
} |
| 68 |
return sensor; |
| 69 |
} |
| 70 |
|
| 71 |
static gboolean |
| 72 |
cd_sensor_client_add (CdSensorClient *sensor_client, |
| 73 |
GUdevDevice *device) |
| 74 |
{ |
| 75 |
CdSensorClientPrivate *priv = GET_PRIVATE (sensor_client); |
| 76 |
CdSensor *sensor = NULL; |
| 77 |
const gchar *device_file; |
| 78 |
const gchar *tmp; |
| 79 |
gboolean ret = FALSE; |
| 80 |
g_autoptr(GError) error = NULL; |
| 81 |
|
| 82 |
/* interesting device? */ |
| 83 |
tmp = g_udev_device_get_property (device, "COLORD_SENSOR_KIND"); |
| 84 |
if (tmp == NULL) |
| 85 |
goto out; |
| 86 |
tmp = g_udev_device_get_property (device, "COLORD_IGNORE"); |
| 87 |
if (tmp != NULL) |
| 88 |
goto out; |
| 89 |
|
| 90 |
/* actual device? */ |
| 91 |
device_file = g_udev_device_get_device_file (device); |
| 92 |
if (device_file == NULL) |
| 93 |
goto out; |
| 94 |
|
| 95 |
/* get data */ |
| 96 |
g_debug ("adding color management device: %s [%s]", |
| 97 |
g_udev_device_get_sysfs_path (device), |
| 98 |
device_file); |
| 99 |
sensor = cd_sensor_new (); |
| 100 |
ret = cd_sensor_set_from_device (sensor, device, &error); |
| 101 |
if (!ret) { |
| 102 |
g_warning ("CdSensorClient: failed to set CM sensor: %s", |
| 103 |
error->message); |
| 104 |
goto out; |
| 105 |
} |
| 106 |
|
| 107 |
/* set the index */ |
| 108 |
cd_sensor_set_index (sensor, priv->idx); |
| 109 |
|
| 110 |
/* load the sensor */ |
| 111 |
ret = cd_sensor_load (sensor, &error); |
| 112 |
if (!ret) { |
| 113 |
/* not fatal, non-native devices are still usable */ |
| 114 |
g_debug ("CdSensorClient: failed to load native sensor: %s", |
| 115 |
error->message); |
| 116 |
g_clear_error (&error); |
| 117 |
} |
| 118 |
|
| 119 |
/* signal the addition */ |
| 120 |
g_debug ("emit: added"); |
| 121 |
g_signal_emit (sensor_client, signals[SIGNAL_SENSOR_ADDED], 0, sensor); |
| 122 |
priv->idx++; |
| 123 |
|
| 124 |
/* keep track so we can remove with the same device */ |
| 125 |
g_ptr_array_add (priv->array_sensors, g_object_ref (sensor)); |
| 126 |
out: |
| 127 |
if (sensor != NULL) |
| 128 |
g_object_unref (sensor); |
| 129 |
return ret; |
| 130 |
} |
| 131 |
|
| 132 |
static void |
| 133 |
cd_sensor_client_remove (CdSensorClient *sensor_client, |
| 134 |
GUdevDevice *device) |
| 135 |
{ |
| 136 |
CdSensorClientPrivate *priv = GET_PRIVATE (sensor_client); |
| 137 |
CdSensor *sensor; |
| 138 |
const gchar *device_file; |
| 139 |
const gchar *device_path; |
| 140 |
const gchar *tmp; |
| 141 |
guint i; |
| 142 |
|
| 143 |
/* interesting device? */ |
| 144 |
tmp = g_udev_device_get_property (device, "COLORD_SENSOR_KIND"); |
| 145 |
if (tmp == NULL) |
| 146 |
goto out; |
| 147 |
|
| 148 |
/* actual device? */ |
| 149 |
device_file = g_udev_device_get_device_file (device); |
| 150 |
if (device_file == NULL) |
| 151 |
goto out; |
| 152 |
|
| 153 |
/* get data */ |
| 154 |
device_path = g_udev_device_get_sysfs_path (device); |
| 155 |
g_debug ("removing color management device: %s [%s]", |
| 156 |
device_path, device_file); |
| 157 |
for (i = 0; i < priv->array_sensors->len; i++) { |
| 158 |
sensor = g_ptr_array_index (priv->array_sensors, i); |
| 159 |
if (g_strcmp0 (cd_sensor_get_device_path (sensor), device_path) == 0) { |
| 160 |
g_debug ("emit: removed"); |
| 161 |
g_signal_emit (sensor_client, signals[SIGNAL_SENSOR_REMOVED], 0, sensor); |
| 162 |
g_ptr_array_remove_index_fast (priv->array_sensors, i); |
| 163 |
goto out; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/* nothing found */ |
| 168 |
g_warning ("removed CM sensor that was never added"); |
| 169 |
out: |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
cd_sensor_client_uevent_cb (GUdevClient *gudev_client, |
| 175 |
const gchar *action, |
| 176 |
GUdevDevice *udev_device, |
| 177 |
CdSensorClient *sensor_client) |
| 178 |
{ |
| 179 |
gboolean ret; |
| 180 |
|
| 181 |
/* remove */ |
| 182 |
if (g_strcmp0 (action, "remove") == 0) { |
| 183 |
g_debug ("CdSensorClient: remove %s", |
| 184 |
g_udev_device_get_sysfs_path (udev_device)); |
| 185 |
ret = g_udev_device_has_property (udev_device, "COLORD_SENSOR_KIND"); |
| 186 |
if (ret) { |
| 187 |
cd_sensor_client_remove (sensor_client, |
| 188 |
udev_device); |
| 189 |
goto out; |
| 190 |
} |
| 191 |
goto out; |
| 192 |
} |
| 193 |
|
| 194 |
/* add */ |
| 195 |
if (g_strcmp0 (action, "add") == 0) { |
| 196 |
g_debug ("CdSensorClient: add %s", |
| 197 |
g_udev_device_get_sysfs_path (udev_device)); |
| 198 |
ret = g_udev_device_has_property (udev_device, "COLORD_SENSOR_KIND"); |
| 199 |
if (ret) { |
| 200 |
cd_sensor_client_add (sensor_client, |
| 201 |
udev_device); |
| 202 |
goto out; |
| 203 |
} |
| 204 |
goto out; |
| 205 |
} |
| 206 |
out: |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
void |
| 211 |
cd_sensor_client_coldplug (CdSensorClient *sensor_client) |
| 212 |
{ |
| 213 |
CdSensorClientPrivate *priv = GET_PRIVATE (sensor_client); |
| 214 |
GList *devices; |
| 215 |
GList *l; |
| 216 |
GUdevDevice *udev_device; |
| 217 |
|
| 218 |
/* get all video4linux devices */ |
| 219 |
devices = g_udev_client_query_by_subsystem (priv->gudev_client, |
| 220 |
"usb"); |
| 221 |
for (l = devices; l != NULL; l = l->next) { |
| 222 |
udev_device = l->data; |
| 223 |
cd_sensor_client_add (sensor_client, udev_device); |
| 224 |
} |
| 225 |
g_list_foreach (devices, (GFunc) g_object_unref, NULL); |
| 226 |
g_list_free (devices); |
| 227 |
} |
| 228 |
|
| 229 |
static void |
| 230 |
cd_sensor_client_class_init (CdSensorClientClass *klass) |
| 231 |
{ |
| 232 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 233 |
object_class->finalize = cd_sensor_client_finalize; |
| 234 |
signals[SIGNAL_SENSOR_ADDED] = |
| 235 |
g_signal_new ("sensor-added", |
| 236 |
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 237 |
G_STRUCT_OFFSET (CdSensorClientClass, sensor_added), |
| 238 |
NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 239 |
G_TYPE_NONE, 1, CD_TYPE_SENSOR); |
| 240 |
signals[SIGNAL_SENSOR_REMOVED] = |
| 241 |
g_signal_new ("sensor-removed", |
| 242 |
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 243 |
G_STRUCT_OFFSET (CdSensorClientClass, sensor_removed), |
| 244 |
NULL, NULL, g_cclosure_marshal_VOID__OBJECT, |
| 245 |
G_TYPE_NONE, 1, CD_TYPE_SENSOR); |
| 246 |
} |
| 247 |
|
| 248 |
static void |
| 249 |
cd_sensor_client_init (CdSensorClient *sensor_client) |
| 250 |
{ |
| 251 |
CdSensorClientPrivate *priv = GET_PRIVATE (sensor_client); |
| 252 |
const gchar *subsystems[] = {"usb", "video4linux", NULL}; |
| 253 |
priv->array_sensors = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 254 |
priv->gudev_client = g_udev_client_new (subsystems); |
| 255 |
g_signal_connect (priv->gudev_client, "uevent", |
| 256 |
G_CALLBACK (cd_sensor_client_uevent_cb), sensor_client); |
| 257 |
} |
| 258 |
|
| 259 |
static void |
| 260 |
cd_sensor_client_finalize (GObject *object) |
| 261 |
{ |
| 262 |
CdSensorClient *sensor_client = CD_SENSOR_CLIENT (object); |
| 263 |
CdSensorClientPrivate *priv = GET_PRIVATE (sensor_client); |
| 264 |
|
| 265 |
g_object_unref (priv->gudev_client); |
| 266 |
g_ptr_array_unref (priv->array_sensors); |
| 267 |
|
| 268 |
G_OBJECT_CLASS (cd_sensor_client_parent_class)->finalize (object); |
| 269 |
} |
| 270 |
|
| 271 |
CdSensorClient * |
| 272 |
cd_sensor_client_new (void) |
| 273 |
{ |
| 274 |
CdSensorClient *sensor_client; |
| 275 |
sensor_client = g_object_new (CD_TYPE_SENSOR_CLIENT, NULL); |
| 276 |
return CD_SENSOR_CLIENT (sensor_client); |
| 277 |
} |
| 278 |
|