-
Notifications
You must be signed in to change notification settings - Fork 1
/
hid_manager.c
164 lines (127 loc) · 4.35 KB
/
hid_manager.c
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
#include "hid_manager.h"
#include "hid_device_info.h"
struct _LiquidHidManager
{
GObject parent;
GUdevClient *udev_client;
GHashTable *devices;
};
G_DEFINE_FINAL_TYPE(LiquidHidManager, liquid_hid_manager, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_UDEV_CLIENT,
N_PROPERTIES
};
static GParamSpec *pspecs[N_PROPERTIES];
static void
liquid_hid_manager_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
LiquidHidManager *manager = LIQUID_HID_MANAGER(object);
switch (property_id)
{
case PROP_UDEV_CLIENT:
g_value_set_object(value, manager->udev_client);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
liquid_hid_manager_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
LiquidHidManager *manager = LIQUID_HID_MANAGER(object);
switch (property_id)
{
case PROP_UDEV_CLIENT:
g_set_object(&manager->udev_client, g_value_get_object(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
liquid_hid_manager_dispose(GObject *object)
{
LiquidHidManager *manager = LIQUID_HID_MANAGER(object);
g_hash_table_remove_all(manager->devices);
g_clear_object(&manager->udev_client);
G_OBJECT_CLASS(liquid_hid_manager_parent_class)->dispose(object);
}
static void
liquid_hid_manager_finalize(GObject *object)
{
LiquidHidManager *manager = LIQUID_HID_MANAGER(object);
g_clear_pointer(&manager->devices, g_hash_table_unref);
G_OBJECT_CLASS(liquid_hid_manager_parent_class)->finalize(object);
}
static void
liquid_hid_manager_add_udev_device(LiquidHidManager *manager, GUdevDevice *udev_device)
{
g_autoptr(LiquidHidDeviceInfo) info = liquid_hid_device_info_new_for_udev_device(udev_device);
if (info == NULL)
{
return;
}
const gchar *key = liquid_hid_device_info_get_hidraw_path(info);
g_return_if_fail(key != NULL);
g_hash_table_insert(manager->devices,
g_strdup(key),
g_steal_pointer(&info));
}
static void
liquid_hid_manager_constructed(GObject *object)
{
LiquidHidManager *manager = LIQUID_HID_MANAGER(object);
g_autolist(GUdevDevice) devices = g_udev_client_query_by_subsystem(manager->udev_client, "hidraw");
for (GList *i = devices; i; i = i->next)
{
liquid_hid_manager_add_udev_device(manager, G_UDEV_DEVICE(i->data));
}
G_OBJECT_CLASS(liquid_hid_manager_parent_class)->constructed(object);
}
static void
liquid_hid_manager_init(LiquidHidManager *info)
{
info->devices = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);
}
static void
liquid_hid_manager_class_init(LiquidHidManagerClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(class);
gobject_class->dispose = liquid_hid_manager_dispose;
gobject_class->finalize = liquid_hid_manager_finalize;
gobject_class->get_property = liquid_hid_manager_get_property;
gobject_class->set_property = liquid_hid_manager_set_property;
gobject_class->constructed = liquid_hid_manager_constructed;
pspecs[PROP_UDEV_CLIENT]
= g_param_spec_object("udev-client", /* name */
"UDev client instance", /* nick */
"UDev client instance", /* blurb */
G_UDEV_TYPE_CLIENT, /* object_type */
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(gobject_class, N_PROPERTIES, pspecs);
}
LiquidHidManager *
liquid_hid_manager_new(GUdevClient *udev_client)
{
return g_object_new(LIQUID_TYPE_HID_MANAGER,
"udev-client",
udev_client,
NULL);
}
void
liquid_hid_manager_for_each_device(LiquidHidManager *manager,
LiquidHidManagerForEachDeviceCallback callback,
gpointer user_data)
{
g_return_if_fail(LIQUID_IS_HID_MANAGER(manager));
GHashTableIter iter;
g_hash_table_iter_init(&iter, manager->devices);
gchar *key;
LiquidHidDeviceInfo *value;
while (g_hash_table_iter_next(&iter, (gpointer)&key, (gpointer)&value))
{
callback(manager, value, user_data);
}
}