aboutsummaryrefslogtreecommitdiff
path: root/src/udev-mgr.c
blob: 31ccb63891546601990a9cd342be1a91f0be6d14 (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
/*
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);                                   
struct _UdevMgr
{
	GObject parent_instance;
  DbusmenuMenuitem* scanner_item;
  DbusmenuMenuitem* webcam_item;  
  GUdevClient* client;  
  GHashTable* supported_scanners;
};

const char *subsystems[1] = {"usb"};
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->client = g_udev_client_new (subsystems);  
  self->supported_scanners = g_hash_table_new (g_str_hash, g_str_equal);
  populate_usb_scanners(self->supported_scanners);
  GList* epsom = g_hash_table_lookup(self->supported_scanners, "04b8");
  g_list_foreach(epsom, test_usb_scanners, NULL);
  GList* devices_available  = g_udev_client_query_by_subsystem (self->client,
                                                                usb_subsystem);
  
  g_list_foreach (devices_available, udevice_mgr_device_list_iterator, self);
  g_list_free (devices_available);
  g_signal_connect (G_OBJECT (self->client),
                   "uevent",
                    G_CALLBACK (udev_mgr_uevent_cb),
                    self);
}
 

static void
udev_mgr_finalize (GObject *object)
{
	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));
  GUdevDevice* device = G_UDEV_DEVICE (data);
  const gchar* name = g_udev_device_get_name (device);
  
  g_debug ("UDEV MGR - the name of the device = %s", name);
  // for now tidy up here.
  g_object_unref (device);
}

static void udev_mgr_uevent_cb (GUdevClient *client,
                                gchar       *action,
                                GUdevDevice *device,
                                gpointer     user_data)   
{
  g_return_if_fail (UDEV_IS_MGR (user_data));

  g_debug ("just received a UEVENT with an action :  %s", 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);
  }  
}


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;
  return mgr;
}