aboutsummaryrefslogtreecommitdiff
path: root/src/udev-mgr.c
blob: f4b357d0c54a8d641dd97d7aeda19673d2b9d524 (plain)
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
/*
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_device_is_supported (UdevMgr* self, 
                                                   GUdevDevice *device,
                                                   UdevMgrDeviceAction action);
                                                                                        
struct _UdevMgr
{
	GObject parent_instance;
  DbusmenuMenuitem* scanner_item;
  DbusmenuMenuitem* webcam_item;  
  GUdevClient* client;  
  GHashTable* supported_scanners;
  GHashTable* scanners_present;
};

const char *subsystems[2] = {"usb", "scsi"};
const gchar* usb_subsystem = "usb";

G_DEFINE_TYPE (UdevMgr, udev_mgr, G_TYPE_OBJECT);

/*static void
test_usb_scanners(gpointer data, gpointer user_data)
{
  gchar* model = (gchar*)data;
  g_debug ("in hash table for epsom model %s was found", model);
}*/

static void
udev_mgr_init (UdevMgr* self)
{
  self->client = NULL;
  self->supported_scanners = NULL;
  self->scanners_present = NULL;
    
  self->client = g_udev_client_new (subsystems);  
  self->supported_scanners = g_hash_table_new (g_str_hash, g_str_equal);
  self->scanners_present = g_hash_table_new (g_str_hash, g_str_equal);
  
  populate_usb_scanners(self->supported_scanners);
  g_signal_connect (G_OBJECT (self->client),
                   "uevent",
                    G_CALLBACK (udev_mgr_uevent_cb),
                    self);
}

static void
udev_mgr_finalize (GObject *object)
{
  // TODO tidy up hashtables. 
	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);
  udev_mgr_check_if_device_is_supported (self, device, ADD);

  /*const gchar* vendor = NULL;
  const gchar* product = NULL;
  GList* vendor_list = NULL;
  
	vendor = g_udev_device_get_property (device, "ID_VENDOR_ID");
  
  if (vendor == NULL){
    g_object_unref (device);
    return;
  }

	product = g_udev_device_get_property (device, "ID_MODEL_ID");
  vendor_list = g_hash_table_lookup(self->supported_scanners, (gpointer)vendor);
  
  GList* model = NULL;
  
  if (vendor_list != NULL){
    model = g_list_find_custom(vendor_list, product, (GCompareFunc)g_strcmp0);
    if (model == NULL){
      g_debug ("CANT FIND THE MODEL %s FOR  VENDOR %s", product, vendor);
    }
    else{
      self->scanners_present += 1;    
      g_debug ("WE HAVE A SUCCESSFUL MATCH!");
    }    
  }
  g_debug ("JUST SET SCANNERS TO TRUE");*/                                                 
  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);
}

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_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;
  }
  udev_mgr_check_if_device_is_supported (self, device, udev_mgr_action);
  const gchar* vendor;
  const gchar* product;
  const gchar* number;
  
	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);
 
  g_debug ("device vendor id %s and product id of %s and number of %s",
           g_strdup(vendor),
           g_strdup(product),
           g_strdup(number));
           
  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_check_if_device_is_supported (UdevMgr* self, 
                                       GUdevDevice *device,
                                       UdevMgrDeviceAction action)
{
  const gchar* vendor = NULL;
	vendor = g_udev_device_get_property (device, "ID_VENDOR_ID");
  
  if (vendor == NULL)
    return;

  GList* vendor_list = NULL;
  vendor_list = g_hash_table_lookup (self->supported_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){
      // TODO handle the case where its removed
      // remove it if present from the hash and call update_menuitems
    } 
    else{      
      g_hash_table_insert (self->scanners_present,
                           g_strdup(vendor),
                           g_strdup(model_id)); 
    }
  }
  // DEBUG purposes.  
  if (model_entry == NULL){
    g_debug ("CANT FIND THE MODEL %s FOR  VENDOR %s", model_id, vendor);
  }
  else{
    g_debug ("WE HAVE A SUCCESSFUL MATCH!");
  }    
}

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;

  GList* devices_available  = g_udev_client_query_by_subsystem (mgr->client,
                                                                usb_subsystem);
  
  g_list_foreach (devices_available,
                  udevice_mgr_device_list_iterator,
                  mgr);
  g_list_free (devices_available);
  udev_mgr_update_menuitems (mgr);  
  return mgr;
}