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