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_mac.h" | 5 #include "device/hid/hid_service_mac.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <IOKit/hid/IOHIDManager.h> | 8 #include <IOKit/hid/IOHIDManager.h> |
9 | 9 |
10 #include <set> | |
11 #include <string> | 10 #include <string> |
12 #include <vector> | 11 #include <vector> |
13 | 12 |
14 #include "base/bind.h" | 13 #include "base/bind.h" |
15 #include "base/logging.h" | 14 #include "base/logging.h" |
16 #include "base/mac/foundation_util.h" | |
17 #include "base/message_loop/message_loop_proxy.h" | 15 #include "base/message_loop/message_loop_proxy.h" |
18 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
19 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/sys_string_conversions.h" | |
21 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
22 #include "device/hid/hid_connection_mac.h" | 19 #include "device/hid/hid_connection_mac.h" |
| 20 #include "device/hid/hid_utils_mac.h" |
23 | 21 |
24 namespace device { | 22 namespace device { |
25 | 23 |
26 class HidServiceMac; | 24 class HidServiceMac; |
27 | 25 |
28 namespace { | 26 namespace { |
29 | 27 |
30 typedef std::vector<IOHIDDeviceRef> HidDeviceList; | 28 typedef std::vector<IOHIDDeviceRef> HidDeviceList; |
31 | 29 |
32 HidServiceMac* HidServiceFromContext(void* context) { | 30 HidServiceMac* HidServiceFromContext(void* context) { |
(...skipping 10 matching lines...) Expand all Loading... |
43 | 41 |
44 void EnumerateHidDevices(IOHIDManagerRef hid_manager, | 42 void EnumerateHidDevices(IOHIDManagerRef hid_manager, |
45 HidDeviceList* device_list) { | 43 HidDeviceList* device_list) { |
46 DCHECK(device_list->size() == 0); | 44 DCHECK(device_list->size() == 0); |
47 // Note that our ownership of each copied device is implied. | 45 // Note that our ownership of each copied device is implied. |
48 base::ScopedCFTypeRef<CFSetRef> devices(IOHIDManagerCopyDevices(hid_manager)); | 46 base::ScopedCFTypeRef<CFSetRef> devices(IOHIDManagerCopyDevices(hid_manager)); |
49 if (devices) | 47 if (devices) |
50 CFSetApplyFunction(devices, HidEnumerationBackInserter, device_list); | 48 CFSetApplyFunction(devices, HidEnumerationBackInserter, device_list); |
51 } | 49 } |
52 | 50 |
53 bool TryGetHidIntProperty(IOHIDDeviceRef device, | |
54 CFStringRef key, | |
55 int32_t* result) { | |
56 CFNumberRef ref = | |
57 base::mac::CFCast<CFNumberRef>(IOHIDDeviceGetProperty(device, key)); | |
58 return ref && CFNumberGetValue(ref, kCFNumberSInt32Type, result); | |
59 } | |
60 | |
61 int32_t GetHidIntProperty(IOHIDDeviceRef device, CFStringRef key) { | |
62 int32_t value; | |
63 if (TryGetHidIntProperty(device, key, &value)) | |
64 return value; | |
65 return 0; | |
66 } | |
67 | |
68 bool TryGetHidStringProperty(IOHIDDeviceRef device, | |
69 CFStringRef key, | |
70 std::string* result) { | |
71 CFStringRef ref = | |
72 base::mac::CFCast<CFStringRef>(IOHIDDeviceGetProperty(device, key)); | |
73 if (!ref) { | |
74 return false; | |
75 } | |
76 *result = base::SysCFStringRefToUTF8(ref); | |
77 return true; | |
78 } | |
79 | |
80 std::string GetHidStringProperty(IOHIDDeviceRef device, CFStringRef key) { | |
81 std::string value; | |
82 TryGetHidStringProperty(device, key, &value); | |
83 return value; | |
84 } | |
85 | |
86 void GetReportIds(IOHIDElementRef element, std::set<int>& reportIDs) { | |
87 CFArrayRef children = IOHIDElementGetChildren(element); | |
88 if (!children) | |
89 return; | |
90 CFIndex childrenCount = CFArrayGetCount(children); | |
91 for (CFIndex j = 0; j < childrenCount; ++j) { | |
92 const IOHIDElementRef child = static_cast<IOHIDElementRef>( | |
93 const_cast<void*>(CFArrayGetValueAtIndex(children, j))); | |
94 uint32_t reportID = IOHIDElementGetReportID(child); | |
95 if (reportID) { | |
96 reportIDs.insert(reportID); | |
97 } | |
98 GetReportIds(child, reportIDs); | |
99 } | |
100 } | |
101 | |
102 void GetCollectionInfos(IOHIDDeviceRef device, | |
103 std::vector<HidCollectionInfo>* top_level_collections) { | |
104 STLClearObject(top_level_collections); | |
105 CFMutableDictionaryRef collections_filter = | |
106 CFDictionaryCreateMutable(kCFAllocatorDefault, | |
107 0, | |
108 &kCFTypeDictionaryKeyCallBacks, | |
109 &kCFTypeDictionaryValueCallBacks); | |
110 const int kCollectionTypeValue = kIOHIDElementTypeCollection; | |
111 CFNumberRef collection_type_id = CFNumberCreate( | |
112 kCFAllocatorDefault, kCFNumberIntType, &kCollectionTypeValue); | |
113 CFDictionarySetValue( | |
114 collections_filter, CFSTR(kIOHIDElementTypeKey), collection_type_id); | |
115 CFRelease(collection_type_id); | |
116 CFArrayRef collections = IOHIDDeviceCopyMatchingElements( | |
117 device, collections_filter, kIOHIDOptionsTypeNone); | |
118 CFIndex collectionsCount = CFArrayGetCount(collections); | |
119 for (CFIndex i = 0; i < collectionsCount; i++) { | |
120 const IOHIDElementRef collection = static_cast<IOHIDElementRef>( | |
121 const_cast<void*>(CFArrayGetValueAtIndex(collections, i))); | |
122 // Top-Level Collection has no parent | |
123 if (IOHIDElementGetParent(collection) == 0) { | |
124 HidCollectionInfo collection_info; | |
125 HidUsageAndPage::Page page = static_cast<HidUsageAndPage::Page>( | |
126 IOHIDElementGetUsagePage(collection)); | |
127 uint16_t usage = IOHIDElementGetUsage(collection); | |
128 collection_info.usage = HidUsageAndPage(usage, page); | |
129 // Explore children recursively and retrieve their report IDs | |
130 GetReportIds(collection, collection_info.report_ids); | |
131 top_level_collections->push_back(collection_info); | |
132 } | |
133 } | |
134 } | |
135 | |
136 } // namespace | 51 } // namespace |
137 | 52 |
138 HidServiceMac::HidServiceMac() { | 53 HidServiceMac::HidServiceMac() { |
139 DCHECK(thread_checker_.CalledOnValidThread()); | 54 DCHECK(thread_checker_.CalledOnValidThread()); |
140 message_loop_ = base::MessageLoopProxy::current(); | 55 message_loop_ = base::MessageLoopProxy::current(); |
141 DCHECK(message_loop_); | 56 DCHECK(message_loop_); |
142 hid_manager_.reset(IOHIDManagerCreate(NULL, 0)); | 57 hid_manager_.reset(IOHIDManagerCreate(NULL, 0)); |
143 if (!hid_manager_) { | 58 if (!hid_manager_) { |
144 LOG(ERROR) << "Failed to initialize HidManager"; | 59 LOG(ERROR) << "Failed to initialize HidManager"; |
145 return; | 60 return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 ++iter) { | 129 ++iter) { |
215 IOHIDDeviceRef hid_device = *iter; | 130 IOHIDDeviceRef hid_device = *iter; |
216 PlatformAddDevice(hid_device); | 131 PlatformAddDevice(hid_device); |
217 } | 132 } |
218 } | 133 } |
219 | 134 |
220 void HidServiceMac::PlatformAddDevice(IOHIDDeviceRef hid_device) { | 135 void HidServiceMac::PlatformAddDevice(IOHIDDeviceRef hid_device) { |
221 // Note that our ownership of hid_device is implied if calling this method. | 136 // Note that our ownership of hid_device is implied if calling this method. |
222 // It is balanced in PlatformRemoveDevice. | 137 // It is balanced in PlatformRemoveDevice. |
223 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 |
224 HidDeviceInfo device_info; | 140 HidDeviceInfo device_info; |
225 device_info.device_id = hid_device; | 141 device_info.device_id = hid_device; |
226 device_info.vendor_id = | 142 device_info.vendor_id = |
227 GetHidIntProperty(hid_device, CFSTR(kIOHIDVendorIDKey)); | 143 GetHidIntProperty(hid_device, CFSTR(kIOHIDVendorIDKey)); |
228 device_info.product_id = | 144 device_info.product_id = |
229 GetHidIntProperty(hid_device, CFSTR(kIOHIDProductIDKey)); | 145 GetHidIntProperty(hid_device, CFSTR(kIOHIDProductIDKey)); |
| 146 device_info.input_report_size = |
| 147 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxInputReportSizeKey)); |
| 148 device_info.output_report_size = |
| 149 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxOutputReportSizeKey)); |
| 150 device_info.feature_report_size = |
| 151 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxFeatureReportSizeKey)); |
| 152 CFTypeRef deviceUsagePairsRaw = |
| 153 IOHIDDeviceGetProperty(hid_device, CFSTR(kIOHIDDeviceUsagePairsKey)); |
| 154 CFArrayRef deviceUsagePairs = |
| 155 base::mac::CFCast<CFArrayRef>(deviceUsagePairsRaw); |
| 156 CFIndex deviceUsagePairsCount = CFArrayGetCount(deviceUsagePairs); |
| 157 for (CFIndex i = 0; i < deviceUsagePairsCount; i++) { |
| 158 CFDictionaryRef deviceUsagePair = base::mac::CFCast<CFDictionaryRef>( |
| 159 CFArrayGetValueAtIndex(deviceUsagePairs, i)); |
| 160 CFNumberRef usage_raw = base::mac::CFCast<CFNumberRef>( |
| 161 CFDictionaryGetValue(deviceUsagePair, CFSTR(kIOHIDDeviceUsageKey))); |
| 162 uint16_t usage; |
| 163 CFNumberGetValue(usage_raw, kCFNumberSInt32Type, &usage); |
| 164 CFNumberRef page_raw = base::mac::CFCast<CFNumberRef>( |
| 165 CFDictionaryGetValue(deviceUsagePair, CFSTR(kIOHIDDeviceUsagePageKey))); |
| 166 HidUsageAndPage::Page page; |
| 167 CFNumberGetValue(page_raw, kCFNumberSInt32Type, &page); |
| 168 device_info.usages.push_back(HidUsageAndPage(usage, page)); |
| 169 } |
230 device_info.product_name = | 170 device_info.product_name = |
231 GetHidStringProperty(hid_device, CFSTR(kIOHIDProductKey)); | 171 GetHidStringProperty(hid_device, CFSTR(kIOHIDProductKey)); |
232 device_info.serial_number = | 172 device_info.serial_number = |
233 GetHidStringProperty(hid_device, CFSTR(kIOHIDSerialNumberKey)); | 173 GetHidStringProperty(hid_device, CFSTR(kIOHIDSerialNumberKey)); |
234 GetCollectionInfos(hid_device, &device_info.collections); | |
235 device_info.max_input_report_size = | |
236 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxInputReportSizeKey)); | |
237 device_info.max_output_report_size = | |
238 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxOutputReportSizeKey)); | |
239 device_info.max_feature_report_size = | |
240 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxFeatureReportSizeKey)); | |
241 AddDevice(device_info); | 174 AddDevice(device_info); |
242 } | 175 } |
243 | 176 |
244 void HidServiceMac::PlatformRemoveDevice(IOHIDDeviceRef hid_device) { | 177 void HidServiceMac::PlatformRemoveDevice(IOHIDDeviceRef hid_device) { |
245 DCHECK(thread_checker_.CalledOnValidThread()); | 178 DCHECK(thread_checker_.CalledOnValidThread()); |
246 RemoveDevice(hid_device); | 179 RemoveDevice(hid_device); |
247 CFRelease(hid_device); | 180 CFRelease(hid_device); |
248 } | 181 } |
249 | 182 |
250 scoped_refptr<HidConnection> HidServiceMac::Connect( | 183 scoped_refptr<HidConnection> HidServiceMac::Connect( |
251 const HidDeviceId& device_id) { | 184 const HidDeviceId& device_id) { |
252 DCHECK(thread_checker_.CalledOnValidThread()); | 185 DCHECK(thread_checker_.CalledOnValidThread()); |
253 HidDeviceInfo device_info; | 186 HidDeviceInfo device_info; |
254 if (!GetDeviceInfo(device_id, &device_info)) | 187 if (!GetDeviceInfo(device_id, &device_info)) |
255 return NULL; | 188 return NULL; |
256 return scoped_refptr<HidConnection>(new HidConnectionMac(device_info)); | 189 return scoped_refptr<HidConnection>(new HidConnectionMac(device_info)); |
257 } | 190 } |
258 | 191 |
259 } // namespace device | 192 } // namespace device |
OLD | NEW |