1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
/*
Copyright 2011 Canonical Ltd.
Authors:
Conor Curran <conor.curran@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gudev/gudev.h>
// TEMP
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include "udev-mgr.h"
#include "sane-rules.h"
static void udevice_mgr_device_list_iterator (gpointer data,
gpointer userdata);
static void udev_mgr_uevent_cb (GUdevClient *client,
gchar *action,
GUdevDevice *device,
gpointer user_data);
static void udev_mgr_update_menuitems (UdevMgr* self);
static void udev_mgr_check_if_usb_device_is_supported (UdevMgr* self,
GUdevDevice *device,
UdevMgrDeviceAction action);
static void udev_mgr_handle_webcam (UdevMgr* self,
GUdevDevice* device,
UdevMgrDeviceAction action);
static void udev_mgr_handle_scsi_device (UdevMgr* self,
GUdevDevice* device,
UdevMgrDeviceAction action);
static void udev_mgr_cleanup_lists(gpointer data, gpointer self);
static void udev_mgr_cleanup_entries(gpointer data, gpointer self);
static void debug_device (UdevMgr* self,
GUdevDevice* device,
UdevMgrDeviceAction action);
struct _UdevMgr
{
GObject parent_instance;
DbusmenuMenuitem* scanner_item;
DbusmenuMenuitem* webcam_item;
GUdevClient* client;
GHashTable* supported_usb_scanners;
GHashTable* supported_scsi_scanners;
GHashTable* scanners_present;
GHashTable* webcams_present;
};
const char *subsystems[3] = {"usb", "scsi", "video4linux"};
const gchar* usb_subsystem = "usb";
const gchar* scsi_subsystem = "scsi";
const gchar* video4linux_subsystem = "video4linux";
G_DEFINE_TYPE (UdevMgr, udev_mgr, G_TYPE_OBJECT);
static void
udev_mgr_init (UdevMgr* self)
{
self->client = NULL;
self->supported_usb_scanners = NULL;
self->scanners_present = NULL;
self->webcams_present = NULL;
self->client = g_udev_client_new (subsystems);
self->supported_usb_scanners = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify)udev_mgr_cleanup_lists);
self->supported_scsi_scanners = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
(GDestroyNotify)udev_mgr_cleanup_lists);
self->scanners_present = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_free);
self->webcams_present = g_hash_table_new_full (g_str_hash,
g_str_equal,
g_free,
g_free);
// load into memory all supported scanners ...
populate_usb_scanners (self->supported_usb_scanners);
populate_scsi_scanners (self->supported_scsi_scanners);
g_signal_connect (G_OBJECT (self->client),
"uevent",
G_CALLBACK (udev_mgr_uevent_cb),
self);
}
static void
udev_mgr_cleanup_lists(gpointer data, gpointer self)
{
GList* scanners = (GList*)data;
g_list_foreach (scanners, udev_mgr_cleanup_entries, NULL);
g_list_free(scanners);
}
static void
udev_mgr_cleanup_entries(gpointer data, gpointer self)
{
gchar* entry = (gchar*)data;
g_free(entry);
}
static void
udev_mgr_finalize (GObject *object)
{
UdevMgr* self = UDEV_MGR (object);
g_hash_table_destroy (self->supported_scsi_scanners);
g_hash_table_destroy (self->supported_usb_scanners);
g_hash_table_destroy (self->scanners_present);
g_hash_table_destroy (self->webcams_present);
G_OBJECT_CLASS (udev_mgr_parent_class)->finalize (object);
}
static void
udev_mgr_class_init (UdevMgrClass *klass)
{
GObjectClass* object_class = G_OBJECT_CLASS (klass);
object_class->finalize = udev_mgr_finalize;
}
static void
udevice_mgr_device_list_iterator (gpointer data, gpointer userdata)
{
g_return_if_fail (G_UDEV_IS_DEVICE (data));
g_return_if_fail (UDEV_IS_MGR (userdata));
UdevMgr* self = UDEV_MGR (userdata);
GUdevDevice* device = G_UDEV_DEVICE (data);
const gchar* subsystem = NULL;
subsystem = g_udev_device_get_subsystem (device);
if (g_strcmp0 (subsystem, "usb") == 0){
udev_mgr_check_if_usb_device_is_supported (self, device, ADD);
}
else if (g_strcmp0 (subsystem, "video4linux") == 0){
udev_mgr_handle_webcam (self, device, ADD);
}
else if (g_strcmp0 (subsystem, "scsi") == 0){
udev_mgr_handle_scsi_device (self, device, ADD);
}
g_object_unref (device);
}
static void udev_mgr_update_menuitems (UdevMgr* self)
{
dbusmenu_menuitem_property_set_bool (self->scanner_item,
DBUSMENU_MENUITEM_PROP_VISIBLE,
g_hash_table_size (self->scanners_present) > 0);
dbusmenu_menuitem_property_set_bool (self->webcam_item,
DBUSMENU_MENUITEM_PROP_VISIBLE,
g_hash_table_size (self->webcams_present) > 0);
}
static void udev_mgr_uevent_cb (GUdevClient *client,
gchar *action,
GUdevDevice *device,
gpointer user_data)
{
g_return_if_fail (UDEV_IS_MGR (user_data));
UdevMgr* self = UDEV_MGR (user_data);
g_return_if_fail (device != NULL);
g_debug ("just received a UEVENT with an action : %s", action);
UdevMgrDeviceAction udev_mgr_action = ADD;
if (g_strcmp0 (action, "remove") == 0){
udev_mgr_action = REMOVE;
}
const gchar* subsystem = NULL;
subsystem = g_udev_device_get_subsystem (device);
if (g_strcmp0 (subsystem, "usb") == 0){
udev_mgr_check_if_usb_device_is_supported (self,
device,
udev_mgr_action);
}
else if (g_strcmp0 (subsystem, "video4linux") == 0){
udev_mgr_handle_webcam (self, device, udev_mgr_action);
}
else if (g_strcmp0 (subsystem, "scsi") == 0){
udev_mgr_handle_scsi_device (self, device, udev_mgr_action);
}
}
static void
udev_mgr_handle_webcam (UdevMgr* self,
GUdevDevice* device,
UdevMgrDeviceAction action)
{
if (FALSE)
debug_device (self, device, action);
const gchar* vendor;
const gchar* product;
vendor = g_udev_device_get_property (device, "ID_VENDOR_ID");
product = g_udev_device_get_property (device, "ID_MODEL_ID");
if (action == REMOVE){
if (g_hash_table_lookup (self->webcams_present, product) == NULL){
g_warning ("Got a remove event on a webcam device but we don't have that device in our webcam cache");
return;
}
g_hash_table_remove (self->webcams_present,
product);
}
else {
if (g_hash_table_lookup (self->webcams_present, product) != NULL){
g_warning ("Got an ADD event on a webcam device but we already have that device in our webcam cache");
return;
}
g_hash_table_insert (self->webcams_present,
g_strdup (product),
g_strdup (vendor));
}
udev_mgr_update_menuitems (self);
}
static void
debug_device (UdevMgr* self,
GUdevDevice* device,
UdevMgrDeviceAction action)
{
/*const gchar* vendor;
const gchar* product;
const gchar* number;
const gchar* name;
vendor = g_udev_device_get_property (device, "ID_VENDOR_ID");
product = g_udev_device_get_property (device, "ID_MODEL_ID");
number = g_udev_device_get_number (device);
name = g_udev_device_get_name (device);
g_debug ("device vendor id %s , product id of %s , number of %s and name of %s",
g_strdup(vendor),
g_strdup(product),
g_strdup(number),
g_strdup(name));
const gchar *const *list;
const gchar *const *iter;
char propstr[500];
guint32 namelen = 0, i;
list = g_udev_device_get_property_keys(device);
for (iter = list; iter && *iter; iter++) {
if (strlen(*iter) > namelen)
namelen = strlen(*iter);
}
namelen++;
for (iter = list; iter && *iter; iter++) {
strcpy(propstr, *iter);
strcat(propstr, ":");
for (i = 0; i < namelen - strlen(*iter); i++)
strcat(propstr, " ");
strcat(propstr, g_udev_device_get_property(device, *iter));
g_debug("%s", propstr);
}*/
}
static void udev_mgr_handle_scsi_device (UdevMgr* self,
GUdevDevice* device,
UdevMgrDeviceAction action)
{
const gchar* type = NULL;
type = g_udev_device_get_property (device, "TYPE");
// apparently anything thats type 3 and SCSI is a Scanner
if (g_strcmp0 (type, "6") == 0){
gchar* random_scanner_name = g_strdup_printf("%p--scanner", self);
g_hash_table_insert (self->scanners_present,
random_scanner_name,
g_strdup("Scanner"));
udev_mgr_update_menuitems (self);
return;
}
// We only care about type 3 for the special cases below
if (g_strcmp0 (type, "3") != 0){
return;
}
const gchar* vendor = NULL;
vendor = g_udev_device_get_property (device, "VENDOR");
if (vendor == NULL)
return;
GList* vendor_list = NULL;
vendor_list = g_hash_table_lookup (self->supported_scsi_scanners,
(gpointer)vendor);
if (vendor_list == NULL)
return;
const gchar* model_id = NULL;
model_id = g_udev_device_get_property (device, "MODEL");
if (model_id == NULL)
return;
GList* model_entry = NULL;
model_entry = g_list_find_custom (vendor_list,
model_id,
(GCompareFunc)g_strcmp0);
if (model_entry != NULL){
if (action == REMOVE){
if (g_hash_table_lookup (self->scanners_present, g_strdup(vendor)) == NULL){
g_warning ("Got an REMOVE event on a scanner device but we dont have that device in our scanners cache");
}
else{
g_hash_table_remove (self->scanners_present, vendor);
}
}
else{
if (g_hash_table_lookup (self->scanners_present, g_strdup(vendor)) != NULL){
g_warning ("Got an ADD event on a scanner device but we already have that device in our scanners cache");
}
else{
g_hash_table_insert (self->scanners_present,
g_strdup(vendor),
g_strdup(model_id));
}
}
udev_mgr_update_menuitems (self);
}
}
static void
udev_mgr_check_if_usb_device_is_supported (UdevMgr* self,
GUdevDevice *device,
UdevMgrDeviceAction action)
{
const gchar* vendor = NULL;
debug_device (self, device, action);
vendor = g_udev_device_get_property (device, "ID_VENDOR_ID");
if (vendor == NULL)
return;
//g_debug ("vendor = %s", vendor);
GList* vendor_list = NULL;
vendor_list = g_hash_table_lookup (self->supported_usb_scanners,
(gpointer)vendor);
if (vendor_list == NULL)
return;
const gchar* model_id = NULL;
model_id = g_udev_device_get_property (device, "ID_MODEL_ID");
if (model_id == NULL)
return;
GList* model_entry = NULL;
model_entry = g_list_find_custom(vendor_list, model_id, (GCompareFunc)g_strcmp0);
if (model_entry != NULL){
if (action == REMOVE){
if (g_hash_table_lookup (self->scanners_present, g_strdup(vendor)) == NULL){
g_warning ("Got an REMOVE event on a scanner device but we dont have that device in our scanners cache");
}
else{
g_hash_table_remove (self->scanners_present, vendor);
}
}
else{
if (g_hash_table_lookup (self->scanners_present, g_strdup(vendor)) != NULL){
g_warning ("Got an ADD event on a scanner device but we already have that device in our scanners cache");
}
else{
g_hash_table_insert (self->scanners_present,
g_strdup(vendor),
g_strdup(model_id));
}
}
udev_mgr_update_menuitems (self);
}
}
UdevMgr* udev_mgr_new (DbusmenuMenuitem* scanner,
DbusmenuMenuitem* webcam)
{
UdevMgr* mgr = g_object_new (UDEV_TYPE_MGR, NULL);
mgr->scanner_item = scanner;
mgr->webcam_item = webcam;
// Check for USB devices
GList* usb_devices_available = NULL;
usb_devices_available = g_udev_client_query_by_subsystem (mgr->client,
usb_subsystem);
if (usb_devices_available != NULL){
g_list_foreach (usb_devices_available,
udevice_mgr_device_list_iterator,
mgr);
g_list_free (usb_devices_available);
}
// Check for webcams
GList* video_devices_available = NULL;
video_devices_available = g_udev_client_query_by_subsystem (mgr->client,
video4linux_subsystem);
if (video_devices_available != NULL){
g_list_foreach (video_devices_available,
udevice_mgr_device_list_iterator,
mgr);
g_list_free (video_devices_available);
}
// Check for SCSI devices
GList* scsi_devices_available = NULL;
scsi_devices_available = g_udev_client_query_by_subsystem (mgr->client,
scsi_subsystem);
if (scsi_devices_available != NULL){
g_list_foreach (scsi_devices_available,
udevice_mgr_device_list_iterator,
mgr);
g_list_free (scsi_devices_available);
}
return mgr;
}
|