Vuo 2.4.4
Loading...
Searching...
No Matches
VuoHidDevice.c
Go to the documentation of this file.
1
10#include "VuoHidDevice.h"
11#include "VuoHid.h"
12
14#ifdef VUO_COMPILER
16 "title" : "HID Device",
17 "description" : "Information about a USB or Bluetooth HID device.",
18 "keywords" : [ ],
19 "version" : "1.0.0",
20 "dependencies" : [
21 "VuoHidDevices",
22 "VuoHidIo",
23 "VuoInteger",
24 "VuoText"
25 ]
26 });
27#endif
29
34{
35 if (strcmp(json_object_get_string(js), "location") == 0)
36 return VuoHidDevice_MatchLocation;
37 else if (strcmp(json_object_get_string(js), "vendor-product") == 0)
38 return VuoHidDevice_MatchVendorAndProduct;
39 else if (strcmp(json_object_get_string(js), "usage") == 0)
40 return VuoHidDevice_MatchUsage;
41
42 return VuoHidDevice_MatchName;
43}
44
49{
50 if (type == VuoHidDevice_MatchLocation)
51 return "location";
52 else if (type == VuoHidDevice_MatchVendorAndProduct)
53 return "vendor-product";
54 else if (type == VuoHidDevice_MatchUsage)
55 return "usage";
56
57 return "name";
58}
59
72{
73 return (VuoHidDevice){
74 VuoJson_getObjectValue(VuoHidDevice_MatchType, js, "matchType", VuoHidDevice_MatchName),
75 VuoJson_getObjectValue(VuoText, js, "name", NULL),
76 VuoJson_getObjectValue(VuoInteger, js, "location", 0),
77 VuoJson_getObjectValue(VuoList_VuoHidControl, js, "controls", NULL),
78 VuoJson_getObjectValue(VuoInteger, js, "vendorID", 0),
79 VuoJson_getObjectValue(VuoInteger, js, "productID", 0),
80 VuoJson_getObjectValue(VuoInteger, js, "usagePage", 0),
81 VuoJson_getObjectValue(VuoInteger, js, "usage", 0)
82 };
83}
84
88json_object *VuoHidDevice_getJson(const VuoHidDevice value)
89{
90 json_object *js = json_object_new_object();
91
92 json_object *matchTypeObject = json_object_new_string(VuoHidDevice_getStringForMatchType(value.matchType));
93 json_object_object_add(js, "matchType", matchTypeObject);
94
95 if (value.name)
96 {
97 json_object *o = VuoText_getJson(value.name);
98 json_object_object_add(js, "name", o);
99 }
100
101 if (value.location)
102 {
103 json_object *o = VuoInteger_getJson(value.location);
104 json_object_object_add(js, "location", o);
105 }
106
107 if (value.controls)
108 {
109 json_object *o = VuoList_VuoHidControl_getJson(value.controls);
110 json_object_object_add(js, "controls", o);
111 }
112
113 if (value.vendorID)
114 {
115 json_object *o = VuoInteger_getJson(value.vendorID);
116 json_object_object_add(js, "vendorID", o);
117 }
118
119 if (value.productID)
120 {
121 json_object *o = VuoInteger_getJson(value.productID);
122 json_object_object_add(js, "productID", o);
123 }
124
125 if (value.usagePage)
126 {
127 json_object *o = VuoInteger_getJson(value.usagePage);
128 json_object_object_add(js, "usagePage", o);
129 }
130
131 if (value.usage)
132 {
133 json_object *o = VuoInteger_getJson(value.usage);
134 json_object_object_add(js, "usage", o);
135 }
136
137 return js;
138}
139
143bool VuoHidDevice_areEqual(const VuoHidDevice valueA, const VuoHidDevice valueB)
144{
145 if (valueA.matchType != valueB.matchType)
146 return false;
147
148 if (!VuoText_areEqual(valueA.name, valueB.name))
149 return false;
150
151 if (!VuoInteger_areEqual(valueA.location, valueB.location))
152 return false;
153
154// if (!VuoList_VuoHidControl_areEqual(valueA.controls, valueB.controls))
155// return false;
156
157 if (!VuoInteger_areEqual(valueA.vendorID, valueB.vendorID))
158 return false;
159
160 if (!VuoInteger_areEqual(valueA.productID, valueB.productID))
161 return false;
162
163 if (!VuoInteger_areEqual(valueA.usagePage, valueB.usagePage))
164 return false;
165
166 if (!VuoInteger_areEqual(valueA.usage, valueB.usage))
167 return false;
168
169 return true;
170}
171
176{
177 return a.location < b.location;
178}
179
189bool VuoHidDevice_realize(VuoHidDevice device, VuoHidDevice *realizedDevice)
190{
191 VUserLog("Matching by %s: name=%s location=%08llx product=%04llx:%04llx usage=%04llx:%04llx", VuoHidDevice_getStringForMatchType(device.matchType), device.name, device.location, device.vendorID, device.productID, device.usagePage, device.usage);
192
193 // Already have a location; nothing to do.
194 if (device.matchType == VuoHidDevice_MatchLocation)
195 {
196 realizedDevice->matchType = device.matchType;
197 realizedDevice->name = VuoText_make(device.name);
198 realizedDevice->location = device.location;
199 realizedDevice->controls = VuoListCopy_VuoHidControl(device.controls);
200 realizedDevice->vendorID = device.vendorID;
201 realizedDevice->productID = device.productID;
202 realizedDevice->usagePage = device.usagePage;
203 realizedDevice->usage = device.usage;
204 return true;
205 }
206
207 // Otherwise, try to find a matching name, productAndVendor, or usage.
208
209 if ((device.matchType == VuoHidDevice_MatchName && !device.name)
210 || (device.matchType == VuoHidDevice_MatchVendorAndProduct && (!device.vendorID || !device.productID))
211 || (device.matchType == VuoHidDevice_MatchUsage && (!device.usagePage || !device.usage)))
212 return false;
213
215 VuoRetain(devices);
216
217 unsigned long deviceCount = VuoListGetCount_VuoHidDevice(devices);
218 if (deviceCount == 0)
219 {
220 VUserLog("Warning: No HID devices found.");
221 VuoRelease(devices);
222 return false;
223 }
224
225 VuoBoolean found = false;
226 for (unsigned long i = 1; i <= deviceCount; ++i)
227 {
229 if ((device.matchType == VuoHidDevice_MatchName && strstr(d.name, device.name))
230 || (device.matchType == VuoHidDevice_MatchVendorAndProduct && d.vendorID == device.vendorID && d.productID == device.productID)
231 || (device.matchType == VuoHidDevice_MatchUsage && d.usagePage == device.usagePage && d.usage == device.usage))
232 {
233 realizedDevice->matchType = VuoHidDevice_MatchLocation;
234 realizedDevice->name = VuoText_make(d.name);
235 realizedDevice->location = d.location;
236 realizedDevice->controls = VuoListCopy_VuoHidControl(d.controls);
237 realizedDevice->vendorID = d.vendorID;
238 realizedDevice->productID = d.productID;
239 realizedDevice->usagePage = d.usagePage;
240 realizedDevice->usage = d.usage;
241 found = true;
242 break;
243 }
244 }
245
246 if (found)
247 {
248 VUserLog("Matched \"%s\".", realizedDevice->name);
249 VuoRelease(devices);
250 return true;
251 }
252
253 VUserLog("Warning: Didn't find a device matching those criteria.");
254 VuoRelease(devices);
255 return false;
256}
257
262{
263 VuoHidDevice realizedDevice;
264 if (VuoHidDevice_realize(value, &realizedDevice))
265 {
266 VuoHidDevice_retain(realizedDevice);
267 char *outputText = strdup(realizedDevice.name);
268 VuoHidDevice_release(realizedDevice);
269 return outputText;
270 }
271
272 if (value.name)
273 return strdup(value.name);
274
275 return strdup("Unknown device");
276}