-
Notifications
You must be signed in to change notification settings - Fork 1
/
hid_device_info.c
200 lines (157 loc) · 5.14 KB
/
hid_device_info.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
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
#include "hid_device_info.h"
#include <stdio.h>
#include <linux/input.h>
struct _LiquidHidDeviceInfo
{
GObject parent;
gchar *hidraw_path;
guint32 vendor_id;
guint32 product_id;
};
G_DEFINE_FINAL_TYPE(LiquidHidDeviceInfo, liquid_hid_device_info, G_TYPE_OBJECT)
enum
{
PROP_0,
PROP_HIDRAW_PATH,
PROP_VENDOR_ID,
PROP_PRODUCT_ID,
N_PROPERTIES
};
static GParamSpec *pspecs[N_PROPERTIES];
static void
liquid_hid_device_info_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
LiquidHidDeviceInfo *info = LIQUID_HID_DEVICE_INFO(object);
switch (property_id)
{
case PROP_HIDRAW_PATH:
g_value_set_string(value, info->hidraw_path);
break;
case PROP_VENDOR_ID:
g_value_set_uint(value, info->vendor_id);
break;
case PROP_PRODUCT_ID:
g_value_set_uint(value, info->product_id);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
liquid_hid_device_info_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
LiquidHidDeviceInfo *info = LIQUID_HID_DEVICE_INFO(object);
switch (property_id)
{
case PROP_HIDRAW_PATH:
g_clear_pointer(&info->hidraw_path, g_free);
info->hidraw_path = g_value_dup_string(value);
break;
case PROP_VENDOR_ID:
info->vendor_id = g_value_get_uint(value);
break;
case PROP_PRODUCT_ID:
info->product_id = g_value_get_uint(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
}
}
static void
liquid_hid_device_info_finalize(GObject *object)
{
LiquidHidDeviceInfo *info = LIQUID_HID_DEVICE_INFO(object);
g_clear_pointer(&info->hidraw_path, g_free);
G_OBJECT_CLASS(liquid_hid_device_info_parent_class)->finalize(object);
}
static void
liquid_hid_device_info_init(LiquidHidDeviceInfo *info G_GNUC_UNUSED)
{
}
static void
liquid_hid_device_info_class_init(LiquidHidDeviceInfoClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(class);
gobject_class->finalize = liquid_hid_device_info_finalize;
gobject_class->get_property = liquid_hid_device_info_get_property;
gobject_class->set_property = liquid_hid_device_info_set_property;
pspecs[PROP_HIDRAW_PATH]
= g_param_spec_string("hidraw-path", /* name */
"hidraw device path", /* nick */
"hidraw device path", /* blurb */
NULL, /* default_value */
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
pspecs[PROP_VENDOR_ID]
= g_param_spec_uint("vendor-id", /* name */
"Vendor ID", /* nick */
"Vendor ID", /* blurb */
0, /* minimum */
G_MAXUINT, /* maximum */
0, /* default_value */
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
pspecs[PROP_PRODUCT_ID]
= g_param_spec_uint("product-id", /* name */
"Product ID", /* nick */
"Product ID", /* blurb */
0, /* minimum */
G_MAXUINT, /* maximum */
0, /* default_value */
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties(gobject_class, N_PROPERTIES, pspecs);
}
const gchar *
liquid_hid_device_info_get_hidraw_path(LiquidHidDeviceInfo *info)
{
g_return_val_if_fail(LIQUID_IS_HID_DEVICE_INFO(info), NULL);
return info->hidraw_path;
}
unsigned int
liquid_hid_device_info_get_vendor_id(LiquidHidDeviceInfo *info)
{
g_return_val_if_fail(LIQUID_IS_HID_DEVICE_INFO(info), 0);
return info->vendor_id;
}
unsigned int
liquid_hid_device_info_get_product_id(LiquidHidDeviceInfo *info)
{
g_return_val_if_fail(LIQUID_IS_HID_DEVICE_INFO(info), 0);
return info->product_id;
}
LiquidHidDeviceInfo *
liquid_hid_device_info_new_for_udev_device(GUdevDevice *udev_device)
{
const gchar *hidraw_path = g_udev_device_get_device_file(udev_device);
if (hidraw_path == NULL)
{
return NULL;
}
g_autoptr(GUdevDevice) parent = g_udev_device_get_parent_with_subsystem(udev_device, "hid", NULL);
if (parent == NULL)
{
return NULL;
}
const gchar *hid_id = g_udev_device_get_property(parent, "HID_ID");
if (hid_id == NULL)
{
return NULL;
}
unsigned int bus;
unsigned int vendor;
unsigned int product;
if (sscanf(hid_id, "%x:%x:%x", &bus, &vendor, &product) != 3)
{
return NULL;
}
if (bus != BUS_USB)
{
return NULL;
}
return g_object_new(LIQUID_TYPE_HID_DEVICE_INFO,
"hidraw-path",
hidraw_path,
"vendor-id",
vendor,
"product-id",
product,
NULL);
}