OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/hid/hid_service_win.h" | 5 #include "device/hid/hid_service_win.h" |
6 | 6 |
7 #include <cstdlib> | 7 #include <cstdlib> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 device_info.product_id = attrib.ProductID; | 180 device_info.product_id = attrib.ProductID; |
181 | 181 |
182 for (ULONG i = 32; | 182 for (ULONG i = 32; |
183 HidD_SetNumInputBuffers(device_handle.Get(), i); | 183 HidD_SetNumInputBuffers(device_handle.Get(), i); |
184 i <<= 1); | 184 i <<= 1); |
185 | 185 |
186 // Get usage and usage page (optional). | 186 // Get usage and usage page (optional). |
187 PHIDP_PREPARSED_DATA preparsed_data; | 187 PHIDP_PREPARSED_DATA preparsed_data; |
188 if (HidD_GetPreparsedData(device_handle.Get(), &preparsed_data) && | 188 if (HidD_GetPreparsedData(device_handle.Get(), &preparsed_data) && |
189 preparsed_data) { | 189 preparsed_data) { |
190 HIDP_CAPS capabilities = {0}; | 190 HIDP_CAPS capabilities; |
191 if (HidP_GetCaps(preparsed_data, &capabilities) == HIDP_STATUS_SUCCESS) { | 191 if (HidP_GetCaps(preparsed_data, &capabilities) == HIDP_STATUS_SUCCESS) { |
192 device_info.max_input_report_size = capabilities.InputReportByteLength; | 192 device_info.input_report_size = capabilities.InputReportByteLength; |
193 device_info.max_output_report_size = capabilities.OutputReportByteLength; | 193 device_info.output_report_size = capabilities.OutputReportByteLength; |
194 device_info.max_feature_report_size = | 194 device_info.feature_report_size = capabilities.FeatureReportByteLength; |
195 capabilities.FeatureReportByteLength; | 195 device_info.usages.push_back(HidUsageAndPage( |
196 HidCollectionInfo collection_info; | 196 capabilities.Usage, |
197 collection_info.usage = HidUsageAndPage( | 197 static_cast<HidUsageAndPage::Page>(capabilities.UsagePage))); |
198 capabilities.Usage, | 198 } |
199 static_cast<HidUsageAndPage::Page>(capabilities.UsagePage)); | 199 // Detect if the device supports report ids. |
| 200 if (capabilities.NumberInputValueCaps > 0) { |
| 201 scoped_ptr<HIDP_VALUE_CAPS[]> value_caps( |
| 202 new HIDP_VALUE_CAPS[capabilities.NumberInputValueCaps]); |
| 203 USHORT value_caps_length = capabilities.NumberInputValueCaps; |
| 204 if (HidP_GetValueCaps(HidP_Input, &value_caps[0], &value_caps_length, |
| 205 preparsed_data) == HIDP_STATUS_SUCCESS) { |
| 206 device_info.has_report_id = (value_caps[0].ReportID != 0); |
| 207 } |
| 208 } |
| 209 if (!device_info.has_report_id && capabilities.NumberInputButtonCaps > 0) |
| 210 { |
| 211 scoped_ptr<HIDP_BUTTON_CAPS[]> button_caps( |
| 212 new HIDP_BUTTON_CAPS[capabilities.NumberInputButtonCaps]); |
200 USHORT button_caps_length = capabilities.NumberInputButtonCaps; | 213 USHORT button_caps_length = capabilities.NumberInputButtonCaps; |
201 if (button_caps_length > 0) { | 214 if (HidP_GetButtonCaps(HidP_Input, |
202 scoped_ptr<HIDP_BUTTON_CAPS[]> button_caps( | 215 &button_caps[0], |
203 new HIDP_BUTTON_CAPS[button_caps_length]); | 216 &button_caps_length, |
204 if (HidP_GetButtonCaps(HidP_Input, | 217 preparsed_data) == HIDP_STATUS_SUCCESS) { |
205 &button_caps[0], | 218 device_info.has_report_id = (button_caps[0].ReportID != 0); |
206 &button_caps_length, | |
207 preparsed_data) == HIDP_STATUS_SUCCESS) { | |
208 for (int i = 0; i < button_caps_length; i++) { | |
209 int report_id = button_caps[i].ReportID; | |
210 if (report_id != 0) { | |
211 collection_info.report_ids.insert(report_id); | |
212 } | |
213 } | |
214 } | |
215 } | 219 } |
216 USHORT value_caps_length = capabilities.NumberInputValueCaps; | |
217 if (value_caps_length > 0) { | |
218 scoped_ptr<HIDP_VALUE_CAPS[]> value_caps( | |
219 new HIDP_VALUE_CAPS[value_caps_length]); | |
220 if (HidP_GetValueCaps(HidP_Input, | |
221 &value_caps[0], | |
222 &value_caps_length, | |
223 preparsed_data) == HIDP_STATUS_SUCCESS) { | |
224 for (int i = 0; i < value_caps_length; i++) { | |
225 int report_id = value_caps[i].ReportID; | |
226 if (report_id != 0) { | |
227 collection_info.report_ids.insert(report_id); | |
228 } | |
229 } | |
230 } | |
231 } | |
232 device_info.collections.push_back(collection_info); | |
233 } | 220 } |
| 221 |
234 HidD_FreePreparsedData(preparsed_data); | 222 HidD_FreePreparsedData(preparsed_data); |
235 } | 223 } |
236 | 224 |
237 AddDevice(device_info); | 225 AddDevice(device_info); |
238 } | 226 } |
239 | 227 |
240 void HidServiceWin::PlatformRemoveDevice(const std::string& device_path) { | 228 void HidServiceWin::PlatformRemoveDevice(const std::string& device_path) { |
241 RemoveDevice(device_path); | 229 RemoveDevice(device_path); |
242 } | 230 } |
243 | 231 |
244 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) { | 232 void HidServiceWin::GetDevices(std::vector<HidDeviceInfo>* devices) { |
245 Enumerate(); | 233 Enumerate(); |
246 HidService::GetDevices(devices); | 234 HidService::GetDevices(devices); |
247 } | 235 } |
248 | 236 |
249 scoped_refptr<HidConnection> HidServiceWin::Connect( | 237 scoped_refptr<HidConnection> HidServiceWin::Connect( |
250 const HidDeviceId& device_id) { | 238 const HidDeviceId& device_id) { |
251 HidDeviceInfo device_info; | 239 HidDeviceInfo device_info; |
252 if (!GetDeviceInfo(device_id, &device_info)) | 240 if (!GetDeviceInfo(device_id, &device_info)) |
253 return NULL; | 241 return NULL; |
254 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info)); | 242 scoped_refptr<HidConnectionWin> connection(new HidConnectionWin(device_info)); |
255 if (!connection->available()) { | 243 if (!connection->available()) { |
256 PLOG(ERROR) << "Failed to open device."; | 244 PLOG(ERROR) << "Failed to open device."; |
257 return NULL; | 245 return NULL; |
258 } | 246 } |
259 return connection; | 247 return connection; |
260 } | 248 } |
261 | 249 |
262 } // namespace device | 250 } // namespace device |
OLD | NEW |