| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2012 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 |
#include <gio/gio.h> |
| 24 |
#include <cd-plugin.h> |
| 25 |
#include <gudev/gudev.h> |
| 26 |
|
| 27 |
#include <cd-device.h> |
| 28 |
|
| 29 |
struct CdPluginPrivate { |
| 30 |
GUdevClient *udev_client; |
| 31 |
GHashTable *devices; |
| 32 |
}; |
| 33 |
|
| 34 |
const gchar * |
| 35 |
cd_plugin_get_description (void) |
| 36 |
{ |
| 37 |
return "Add and remove camera devices using info from video4linux"; |
| 38 |
} |
| 39 |
|
| 40 |
static gchar * |
| 41 |
cd_plugin_get_camera_id_for_udev_device (GUdevDevice *udev_device) |
| 42 |
{ |
| 43 |
GString *string; |
| 44 |
const gchar *tmp; |
| 45 |
|
| 46 |
/* get id */ |
| 47 |
string = g_string_new ("sysfs"); |
| 48 |
tmp = g_udev_device_get_property (udev_device, "ID_VENDOR"); |
| 49 |
if (tmp != NULL) |
| 50 |
g_string_append_printf (string, "-%s", tmp); |
| 51 |
tmp = g_udev_device_get_property (udev_device, "ID_MODEL"); |
| 52 |
if (tmp != NULL) |
| 53 |
g_string_append_printf (string, "-%s", tmp); |
| 54 |
|
| 55 |
/* fallback */ |
| 56 |
if (string->len == 5) { |
| 57 |
tmp = g_udev_device_get_device_file (udev_device); |
| 58 |
g_string_append_printf (string, "-%s", tmp); |
| 59 |
} |
| 60 |
|
| 61 |
return g_string_free (string, FALSE); |
| 62 |
} |
| 63 |
|
| 64 |
static gboolean |
| 65 |
cd_plugin_is_device_embedded (GUdevDevice *device) |
| 66 |
{ |
| 67 |
GUdevDevice *p = device; |
| 68 |
const gchar *removable; |
| 69 |
gboolean embedded = FALSE; |
| 70 |
guint i; |
| 71 |
g_autoptr(GPtrArray) array = NULL; |
| 72 |
|
| 73 |
/* get a chain of all the parent devices */ |
| 74 |
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 75 |
do { |
| 76 |
p = g_udev_device_get_parent (p); |
| 77 |
if (p == NULL) |
| 78 |
break; |
| 79 |
g_ptr_array_add (array, p); |
| 80 |
} while (TRUE); |
| 81 |
|
| 82 |
/* find a parent with a removable sysfs file */ |
| 83 |
for (i = 0; i < array->len; i++) { |
| 84 |
p = g_ptr_array_index (array, i); |
| 85 |
removable = g_udev_device_get_sysfs_attr (p, "removable"); |
| 86 |
if (removable != NULL) { |
| 87 |
if (g_strcmp0 (removable, "fixed") == 0) |
| 88 |
embedded = TRUE; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
return embedded; |
| 93 |
} |
| 94 |
|
| 95 |
static void |
| 96 |
cd_plugin_add (CdPlugin *plugin, GUdevDevice *udev_device) |
| 97 |
{ |
| 98 |
const gchar *seat; |
| 99 |
g_autofree gchar *id = NULL; |
| 100 |
g_autofree gchar *model = NULL; |
| 101 |
g_autofree gchar *vendor = NULL; |
| 102 |
g_autoptr(CdDevice) device = NULL; |
| 103 |
|
| 104 |
/* is a proper camera and not a webcam */ |
| 105 |
if (!g_udev_device_has_property (udev_device, "ID_GPHOTO2")) |
| 106 |
return; |
| 107 |
|
| 108 |
/* is a scanner? */ |
| 109 |
if (!g_udev_device_has_property (udev_device, "COLORD_DEVICE")) |
| 110 |
return; |
| 111 |
|
| 112 |
/* replace underscores with spaces */ |
| 113 |
model = g_strdup (g_udev_device_get_property (udev_device, |
| 114 |
"ID_MODEL")); |
| 115 |
if (model != NULL) { |
| 116 |
g_strdelimit (model, "_\r\n", ' '); |
| 117 |
g_strchomp (model); |
| 118 |
} |
| 119 |
vendor = g_strdup (g_udev_device_get_property (udev_device, |
| 120 |
"ID_VENDOR")); |
| 121 |
if (vendor != NULL) { |
| 122 |
g_strdelimit (vendor, "_\r\n", ' '); |
| 123 |
g_strchomp (vendor); |
| 124 |
} |
| 125 |
|
| 126 |
/* generate ID */ |
| 127 |
id = cd_plugin_get_camera_id_for_udev_device (udev_device); |
| 128 |
|
| 129 |
/* assume device belongs to "seat0" if not tagged */ |
| 130 |
seat = g_udev_device_get_property (udev_device, "ID_SEAT"); |
| 131 |
if (seat == NULL) |
| 132 |
seat = "seat0"; |
| 133 |
|
| 134 |
/* create new device */ |
| 135 |
device = cd_device_new (); |
| 136 |
cd_device_set_id (device, id); |
| 137 |
cd_device_set_property_internal (device, |
| 138 |
CD_DEVICE_PROPERTY_KIND, |
| 139 |
cd_device_kind_to_string (CD_DEVICE_KIND_CAMERA), |
| 140 |
FALSE, |
| 141 |
NULL); |
| 142 |
if (model != NULL) { |
| 143 |
cd_device_set_property_internal (device, |
| 144 |
CD_DEVICE_PROPERTY_MODEL, |
| 145 |
model, |
| 146 |
FALSE, |
| 147 |
NULL); |
| 148 |
} |
| 149 |
if (vendor != NULL) { |
| 150 |
cd_device_set_property_internal (device, |
| 151 |
CD_DEVICE_PROPERTY_VENDOR, |
| 152 |
vendor, |
| 153 |
FALSE, |
| 154 |
NULL); |
| 155 |
} |
| 156 |
cd_device_set_property_internal (device, |
| 157 |
CD_DEVICE_PROPERTY_COLORSPACE, |
| 158 |
"rgb", |
| 159 |
FALSE, |
| 160 |
NULL); |
| 161 |
cd_device_set_property_internal (device, |
| 162 |
CD_DEVICE_PROPERTY_SERIAL, |
| 163 |
g_udev_device_get_sysfs_path (udev_device), |
| 164 |
FALSE, |
| 165 |
NULL); |
| 166 |
cd_device_set_property_internal (device, |
| 167 |
CD_DEVICE_PROPERTY_SEAT, |
| 168 |
seat, |
| 169 |
FALSE, |
| 170 |
NULL); |
| 171 |
if (cd_plugin_is_device_embedded (udev_device)) { |
| 172 |
cd_device_set_property_internal (device, |
| 173 |
CD_DEVICE_PROPERTY_EMBEDDED, |
| 174 |
NULL, |
| 175 |
FALSE, |
| 176 |
NULL); |
| 177 |
} |
| 178 |
|
| 179 |
/* keep track so we can remove with the same device */ |
| 180 |
g_hash_table_insert (plugin->priv->devices, |
| 181 |
g_strdup (g_udev_device_get_sysfs_path (udev_device)), |
| 182 |
g_object_ref (device)); |
| 183 |
|
| 184 |
g_debug ("CdPlugin: emit add: %s", id); |
| 185 |
cd_plugin_device_added (plugin, device); |
| 186 |
} |
| 187 |
|
| 188 |
static void |
| 189 |
cd_plugin_uevent_cb (GUdevClient *udev_client, |
| 190 |
const gchar *action, |
| 191 |
GUdevDevice *udev_device, |
| 192 |
CdPlugin *plugin) |
| 193 |
{ |
| 194 |
const gchar *sysfs_path; |
| 195 |
CdDevice *device; |
| 196 |
|
| 197 |
/* remove */ |
| 198 |
if (g_strcmp0 (action, "remove") == 0) { |
| 199 |
|
| 200 |
/* is this a camera device we added */ |
| 201 |
sysfs_path = g_udev_device_get_sysfs_path (udev_device); |
| 202 |
device = g_hash_table_lookup (plugin->priv->devices, sysfs_path); |
| 203 |
if (device == NULL) |
| 204 |
return; |
| 205 |
|
| 206 |
g_debug ("CdPlugin: remove %s", sysfs_path); |
| 207 |
cd_plugin_device_removed (plugin, device); |
| 208 |
g_hash_table_remove (plugin->priv->devices, sysfs_path); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
/* add */ |
| 213 |
if (g_strcmp0 (action, "add") == 0) { |
| 214 |
cd_plugin_add (plugin, udev_device); |
| 215 |
return; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
void |
| 220 |
cd_plugin_coldplug (CdPlugin *plugin) |
| 221 |
{ |
| 222 |
GList *devices; |
| 223 |
GList *l; |
| 224 |
GUdevDevice *udev_device; |
| 225 |
|
| 226 |
/* add all USB scanner devices */ |
| 227 |
devices = g_udev_client_query_by_subsystem (plugin->priv->udev_client, |
| 228 |
"usb"); |
| 229 |
for (l = devices; l != NULL; l = l->next) { |
| 230 |
udev_device = l->data; |
| 231 |
cd_plugin_add (plugin, udev_device); |
| 232 |
} |
| 233 |
g_list_foreach (devices, (GFunc) g_object_unref, NULL); |
| 234 |
g_list_free (devices); |
| 235 |
|
| 236 |
/* add all video4linux devices */ |
| 237 |
devices = g_udev_client_query_by_subsystem (plugin->priv->udev_client, |
| 238 |
"video4linux"); |
| 239 |
for (l = devices; l != NULL; l = l->next) { |
| 240 |
udev_device = l->data; |
| 241 |
cd_plugin_add (plugin, udev_device); |
| 242 |
} |
| 243 |
g_list_foreach (devices, (GFunc) g_object_unref, NULL); |
| 244 |
g_list_free (devices); |
| 245 |
|
| 246 |
/* watch udev for changes */ |
| 247 |
g_signal_connect (plugin->priv->udev_client, "uevent", |
| 248 |
G_CALLBACK (cd_plugin_uevent_cb), plugin); |
| 249 |
} |
| 250 |
|
| 251 |
void |
| 252 |
cd_plugin_initialize (CdPlugin *plugin) |
| 253 |
{ |
| 254 |
const gchar *subsystems[] = { "usb", "video4linux", NULL }; |
| 255 |
|
| 256 |
/* create private */ |
| 257 |
plugin->priv = CD_PLUGIN_GET_PRIVATE (CdPluginPrivate); |
| 258 |
plugin->priv->devices = g_hash_table_new_full (g_str_hash, |
| 259 |
g_str_equal, |
| 260 |
g_free, |
| 261 |
(GDestroyNotify) g_object_unref); |
| 262 |
plugin->priv->udev_client = g_udev_client_new (subsystems); |
| 263 |
} |
| 264 |
|
| 265 |
void |
| 266 |
cd_plugin_destroy (CdPlugin *plugin) |
| 267 |
{ |
| 268 |
g_object_unref (plugin->priv->udev_client); |
| 269 |
g_hash_table_unref (plugin->priv->devices); |
| 270 |
} |