| 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 |
#include <cd-device.h> |
| 27 |
|
| 28 |
struct CdPluginPrivate { |
| 29 |
GUdevClient *udev_client; |
| 30 |
GHashTable *devices; |
| 31 |
}; |
| 32 |
|
| 33 |
const gchar * |
| 34 |
cd_plugin_get_description (void) |
| 35 |
{ |
| 36 |
return "Add and remove scanner devices using the SANE udev database"; |
| 37 |
} |
| 38 |
|
| 39 |
gboolean |
| 40 |
cd_plugin_config_enabled (void) |
| 41 |
{ |
| 42 |
#ifdef HAVE_SANE |
| 43 |
return FALSE; |
| 44 |
#else |
| 45 |
return TRUE; |
| 46 |
#endif |
| 47 |
} |
| 48 |
|
| 49 |
static gchar * |
| 50 |
cd_plugin_get_scanner_id_for_udev_device (GUdevDevice *udev_device) |
| 51 |
{ |
| 52 |
GString *string; |
| 53 |
const gchar *tmp; |
| 54 |
|
| 55 |
/* get id */ |
| 56 |
string = g_string_new ("sysfs"); |
| 57 |
tmp = g_udev_device_get_property (udev_device, "ID_VENDOR"); |
| 58 |
if (tmp != NULL) |
| 59 |
g_string_append_printf (string, "-%s", tmp); |
| 60 |
tmp = g_udev_device_get_property (udev_device, "ID_MODEL"); |
| 61 |
if (tmp != NULL) |
| 62 |
g_string_append_printf (string, "-%s", tmp); |
| 63 |
|
| 64 |
/* fallback */ |
| 65 |
if (string->len == 5) { |
| 66 |
tmp = g_udev_device_get_device_file (udev_device); |
| 67 |
g_string_append_printf (string, "-%s", tmp); |
| 68 |
} |
| 69 |
|
| 70 |
return g_string_free (string, FALSE); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
cd_plugin_add (CdPlugin *plugin, GUdevDevice *udev_device) |
| 75 |
{ |
| 76 |
const gchar *devclass; |
| 77 |
const gchar *seat; |
| 78 |
g_autofree gchar *id = NULL; |
| 79 |
g_autofree gchar *model = NULL; |
| 80 |
g_autofree gchar *vendor = NULL; |
| 81 |
g_autoptr(CdDevice) device = NULL; |
| 82 |
|
| 83 |
/* is a scanner? */ |
| 84 |
if (!g_udev_device_has_property (udev_device, "libsane_matched")) |
| 85 |
return; |
| 86 |
|
| 87 |
/* skip hubs */ |
| 88 |
devclass = g_udev_device_get_sysfs_attr (udev_device, "bDeviceClass"); |
| 89 |
if (devclass == NULL || g_strcmp0 (devclass, "09") == 0) |
| 90 |
return; |
| 91 |
|
| 92 |
/* replace underscores with spaces */ |
| 93 |
model = g_strdup (g_udev_device_get_property (udev_device, |
| 94 |
"ID_MODEL")); |
| 95 |
if (model != NULL) { |
| 96 |
g_strdelimit (model, "_\r\n", ' '); |
| 97 |
g_strchomp (model); |
| 98 |
} |
| 99 |
vendor = g_strdup (g_udev_device_get_property (udev_device, |
| 100 |
"ID_VENDOR")); |
| 101 |
if (vendor != NULL) { |
| 102 |
g_strdelimit (vendor, "_\r\n", ' '); |
| 103 |
g_strchomp (vendor); |
| 104 |
} |
| 105 |
|
| 106 |
/* generate ID */ |
| 107 |
id = cd_plugin_get_scanner_id_for_udev_device (udev_device); |
| 108 |
|
| 109 |
/* assume device belongs to "seat0" if not tagged */ |
| 110 |
seat = g_udev_device_get_property (udev_device, "ID_SEAT"); |
| 111 |
if (seat == NULL) |
| 112 |
seat = "seat0"; |
| 113 |
|
| 114 |
/* create new device */ |
| 115 |
device = cd_device_new (); |
| 116 |
cd_device_set_id (device, id); |
| 117 |
cd_device_set_property_internal (device, |
| 118 |
"Kind", |
| 119 |
"scanner", |
| 120 |
FALSE, |
| 121 |
NULL); |
| 122 |
if (model != NULL) { |
| 123 |
cd_device_set_property_internal (device, |
| 124 |
"Model", |
| 125 |
model, |
| 126 |
FALSE, |
| 127 |
NULL); |
| 128 |
} |
| 129 |
if (vendor != NULL) { |
| 130 |
cd_device_set_property_internal (device, |
| 131 |
"Vendor", |
| 132 |
vendor, |
| 133 |
FALSE, |
| 134 |
NULL); |
| 135 |
} |
| 136 |
cd_device_set_property_internal (device, |
| 137 |
"Colorspace", |
| 138 |
"rgb", |
| 139 |
FALSE, |
| 140 |
NULL); |
| 141 |
cd_device_set_property_internal (device, |
| 142 |
"Serial", |
| 143 |
g_udev_device_get_sysfs_path (udev_device), |
| 144 |
FALSE, |
| 145 |
NULL); |
| 146 |
cd_device_set_property_internal (device, |
| 147 |
"Seat", |
| 148 |
seat, |
| 149 |
FALSE, |
| 150 |
NULL); |
| 151 |
|
| 152 |
/* keep track so we can remove with the same device */ |
| 153 |
g_hash_table_insert (plugin->priv->devices, |
| 154 |
g_strdup (g_udev_device_get_sysfs_path (udev_device)), |
| 155 |
g_object_ref (device)); |
| 156 |
|
| 157 |
g_debug ("CdPlugin: emit add: %s", id); |
| 158 |
cd_plugin_device_added (plugin, device); |
| 159 |
} |
| 160 |
|
| 161 |
static void |
| 162 |
cd_plugin_uevent_cb (GUdevClient *udev_client, |
| 163 |
const gchar *action, |
| 164 |
GUdevDevice *udev_device, |
| 165 |
CdPlugin *plugin) |
| 166 |
{ |
| 167 |
const gchar *sysfs_path; |
| 168 |
CdDevice *device; |
| 169 |
|
| 170 |
/* remove */ |
| 171 |
if (g_strcmp0 (action, "remove") == 0) { |
| 172 |
|
| 173 |
/* is this a scanner device we added */ |
| 174 |
sysfs_path = g_udev_device_get_sysfs_path (udev_device); |
| 175 |
device = g_hash_table_lookup (plugin->priv->devices, sysfs_path); |
| 176 |
if (device == NULL) |
| 177 |
return; |
| 178 |
|
| 179 |
g_debug ("CdPlugin: remove %s", sysfs_path); |
| 180 |
cd_plugin_device_removed (plugin, device); |
| 181 |
g_hash_table_remove (plugin->priv->devices, sysfs_path); |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
/* add */ |
| 186 |
if (g_strcmp0 (action, "add") == 0) { |
| 187 |
cd_plugin_add (plugin, udev_device); |
| 188 |
return; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void |
| 193 |
cd_plugin_coldplug (CdPlugin *plugin) |
| 194 |
{ |
| 195 |
GList *devices; |
| 196 |
GList *l; |
| 197 |
GUdevDevice *udev_device; |
| 198 |
|
| 199 |
/* add all USB scanner devices */ |
| 200 |
devices = g_udev_client_query_by_subsystem (plugin->priv->udev_client, |
| 201 |
"usb"); |
| 202 |
for (l = devices; l != NULL; l = l->next) { |
| 203 |
udev_device = l->data; |
| 204 |
cd_plugin_add (plugin, udev_device); |
| 205 |
} |
| 206 |
g_list_foreach (devices, (GFunc) g_object_unref, NULL); |
| 207 |
g_list_free (devices); |
| 208 |
|
| 209 |
/* watch udev for changes */ |
| 210 |
g_signal_connect (plugin->priv->udev_client, "uevent", |
| 211 |
G_CALLBACK (cd_plugin_uevent_cb), plugin); |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
cd_plugin_initialize (CdPlugin *plugin) |
| 216 |
{ |
| 217 |
const gchar *subsystems[] = { "usb", NULL }; |
| 218 |
|
| 219 |
/* create private */ |
| 220 |
plugin->priv = CD_PLUGIN_GET_PRIVATE (CdPluginPrivate); |
| 221 |
plugin->priv->devices = g_hash_table_new_full (g_str_hash, |
| 222 |
g_str_equal, |
| 223 |
g_free, |
| 224 |
(GDestroyNotify) g_object_unref); |
| 225 |
plugin->priv->udev_client = g_udev_client_new (subsystems); |
| 226 |
} |
| 227 |
|
| 228 |
void |
| 229 |
cd_plugin_destroy (CdPlugin *plugin) |
| 230 |
{ |
| 231 |
g_object_unref (plugin->priv->udev_client); |
| 232 |
g_hash_table_unref (plugin->priv->devices); |
| 233 |
} |