Vuo  2.0.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 HID device.",
19  "keywords" : [ ],
20  "version" : "1.0.0",
21  "dependencies" : [
22  "VuoHidDevices",
23  "VuoHidIo",
24  "VuoInteger",
25  "VuoText"
26  ]
27  });
28 #endif
29 
35 {
36  if (strcmp(json_object_get_string(js), "location") == 0)
37  return VuoHidDevice_MatchLocation;
38 
39  return VuoHidDevice_MatchName;
40 }
41 
46 {
47  if (type == VuoHidDevice_MatchLocation)
48  return "location";
49 
50  return "name";
51 }
52 
65 {
66  return (VuoHidDevice){
67  VuoJson_getObjectValue(VuoHidDevice_MatchType, js, "matchType", VuoHidDevice_MatchName),
68  VuoJson_getObjectValue(VuoText, js, "name", NULL),
69  VuoJson_getObjectValue(VuoInteger, js, "location", 0),
70  VuoJson_getObjectValue(VuoList_VuoHidControl, js, "controls", NULL),
71  VuoJson_getObjectValue(VuoInteger, js, "vendorID", 0),
72  VuoJson_getObjectValue(VuoInteger, js, "productID", 0),
73  VuoJson_getObjectValue(VuoInteger, js, "usagePage", 0),
74  VuoJson_getObjectValue(VuoInteger, js, "usage", 0)
75  };
76 }
77 
82 {
83  json_object *js = json_object_new_object();
84 
85  json_object *matchTypeObject = json_object_new_string(VuoHidDevice_getStringForMatchType(value.matchType));
86  json_object_object_add(js, "matchType", matchTypeObject);
87 
88  if (value.name)
89  {
90  json_object *o = VuoText_getJson(value.name);
91  json_object_object_add(js, "name", o);
92  }
93 
94  if (value.location)
95  {
97  json_object_object_add(js, "location", o);
98  }
99 
100  if (value.controls)
101  {
103  json_object_object_add(js, "controls", o);
104  }
105 
106  if (value.vendorID)
107  {
108  json_object *o = VuoInteger_getJson(value.vendorID);
109  json_object_object_add(js, "vendorID", o);
110  }
111 
112  if (value.productID)
113  {
114  json_object *o = VuoInteger_getJson(value.productID);
115  json_object_object_add(js, "productID", o);
116  }
117 
118  if (value.usagePage)
119  {
120  json_object *o = VuoInteger_getJson(value.usagePage);
121  json_object_object_add(js, "usagePage", o);
122  }
123 
124  if (value.usage)
125  {
126  json_object *o = VuoInteger_getJson(value.usage);
127  json_object_object_add(js, "usage", o);
128  }
129 
130  return js;
131 }
132 
136 bool VuoHidDevice_areEqual(const VuoHidDevice valueA, const VuoHidDevice valueB)
137 {
138  if (valueA.matchType != valueB.matchType)
139  return false;
140 
141  if (!VuoText_areEqual(valueA.name, valueB.name))
142  return false;
143 
144  if (!VuoInteger_areEqual(valueA.location, valueB.location))
145  return false;
146 
147 // if (!VuoList_VuoHidControl_areEqual(valueA.controls, valueB.controls))
148 // return false;
149 
150  if (!VuoInteger_areEqual(valueA.vendorID, valueB.vendorID))
151  return false;
152 
153  if (!VuoInteger_areEqual(valueA.productID, valueB.productID))
154  return false;
155 
156  if (!VuoInteger_areEqual(valueA.usagePage, valueB.usagePage))
157  return false;
158 
159  if (!VuoInteger_areEqual(valueA.usage, valueB.usage))
160  return false;
161 
162  return true;
163 }
164 
169 {
170  return a.location < b.location;
171 }
172 
182 bool VuoHidDevice_realize(VuoHidDevice device, VuoHidDevice *realizedDevice)
183 {
184 // VLog("%s (%08llx) realized=%d",device.name,device.location,device.matchType==VuoHidDevice_MatchLocation);
185 
186  // Already have a location; nothing to do.
187  if (device.matchType == VuoHidDevice_MatchLocation)
188  {
189  realizedDevice->matchType = device.matchType;
190  realizedDevice->name = VuoText_make(device.name);
191  realizedDevice->location = device.location;
192  realizedDevice->controls = VuoListCopy_VuoHidControl(device.controls);
193  realizedDevice->vendorID = device.vendorID;
194  realizedDevice->productID = device.productID;
195  realizedDevice->usagePage = device.usagePage;
196  realizedDevice->usage = device.usage;
197  return true;
198  }
199 
200  // Otherwise, try to find a matching name.
201 
202  if (!device.name)
203  return false;
204 
206  VuoRetain(devices);
207 
208  unsigned long deviceCount = VuoListGetCount_VuoHidDevice(devices);
209  if (deviceCount == 0)
210  {
211  VUserLog("Warning: No HID devices found.");
212  VuoRelease(devices);
213  return false;
214  }
215 
216  VuoBoolean found = false;
217  for (unsigned long i = 1; i <= deviceCount; ++i)
218  {
220  if (strstr(d.name, device.name))
221  {
222  realizedDevice->matchType = VuoHidDevice_MatchLocation;
223  realizedDevice->name = VuoText_make(d.name);
224  realizedDevice->location = d.location;
225  realizedDevice->controls = VuoListCopy_VuoHidControl(d.controls);
226  realizedDevice->vendorID = d.vendorID;
227  realizedDevice->productID = d.productID;
228  realizedDevice->usagePage = d.usagePage;
229  realizedDevice->usage = d.usage;
230  found = true;
231  break;
232  }
233  }
234 
235  if (found)
236  {
237  VuoRelease(devices);
238  return true;
239  }
240 
241 // VLog("Warning: Didn't find a HID device matching '%s'.", device.name);
242  VuoRelease(devices);
243  return false;
244 }
245 
250 {
251  VuoHidDevice realizedDevice;
252  if (VuoHidDevice_realize(value, &realizedDevice))
253  {
254  VuoHidDevice_retain(realizedDevice);
255  char *outputText = strdup(realizedDevice.name);
256  VuoHidDevice_release(realizedDevice);
257  return outputText;
258  }
259 
260  if (value.name)
261  return strdup(value.name);
262 
263  return strdup("Unknown device");
264 }