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> | |
10 #include <string> | 11 #include <string> |
11 #include <vector> | 12 #include <vector> |
12 | 13 |
13 #include "base/bind.h" | 14 #include "base/bind.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
16 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
19 #include "device/hid/hid_connection_mac.h" | 20 #include "device/hid/hid_connection_mac.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
41 | 42 |
42 void EnumerateHidDevices(IOHIDManagerRef hid_manager, | 43 void EnumerateHidDevices(IOHIDManagerRef hid_manager, |
43 HidDeviceList* device_list) { | 44 HidDeviceList* device_list) { |
44 DCHECK(device_list->size() == 0); | 45 DCHECK(device_list->size() == 0); |
45 // Note that our ownership of each copied device is implied. | 46 // Note that our ownership of each copied device is implied. |
46 base::ScopedCFTypeRef<CFSetRef> devices(IOHIDManagerCopyDevices(hid_manager)); | 47 base::ScopedCFTypeRef<CFSetRef> devices(IOHIDManagerCopyDevices(hid_manager)); |
47 if (devices) | 48 if (devices) |
48 CFSetApplyFunction(devices, HidEnumerationBackInserter, device_list); | 49 CFSetApplyFunction(devices, HidEnumerationBackInserter, device_list); |
49 } | 50 } |
50 | 51 |
52 void GetReportIds(IOHIDElementRef element, std::set<int>& reportIDs) { | |
53 CFArrayRef children = IOHIDElementGetChildren(element); | |
54 if (!children) | |
55 return; | |
56 CFIndex childrenCount = CFArrayGetCount(children); | |
57 for (CFIndex j = 0; j < childrenCount; j++) { | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:43
nit: ++j
jracle (use Gerrit)
2014/06/07 12:56:21
I would have bet :). Had that before, jeez! You're
| |
58 IOHIDElementRef child = | |
59 (IOHIDElementRef)CFArrayGetValueAtIndex(children, j); | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:43
static_cast please
jracle (use Gerrit)
2014/06/07 12:56:22
Sorry. C-ish stuff again
On 2014/06/06 20:04:43,
| |
60 uint32_t reportID = IOHIDElementGetReportID(child); | |
61 if (reportID) { | |
62 reportIDs.insert(reportID); | |
63 } | |
64 GetReportIds(child, reportIDs); | |
65 } | |
66 } | |
67 | |
51 } // namespace | 68 } // namespace |
52 | 69 |
53 HidServiceMac::HidServiceMac() { | 70 HidServiceMac::HidServiceMac() { |
54 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
55 message_loop_ = base::MessageLoopProxy::current(); | 72 message_loop_ = base::MessageLoopProxy::current(); |
56 DCHECK(message_loop_); | 73 DCHECK(message_loop_); |
57 hid_manager_.reset(IOHIDManagerCreate(NULL, 0)); | 74 hid_manager_.reset(IOHIDManagerCreate(NULL, 0)); |
58 if (!hid_manager_) { | 75 if (!hid_manager_) { |
59 LOG(ERROR) << "Failed to initialize HidManager"; | 76 LOG(ERROR) << "Failed to initialize HidManager"; |
60 return; | 77 return; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 ++iter) { | 146 ++iter) { |
130 IOHIDDeviceRef hid_device = *iter; | 147 IOHIDDeviceRef hid_device = *iter; |
131 PlatformAddDevice(hid_device); | 148 PlatformAddDevice(hid_device); |
132 } | 149 } |
133 } | 150 } |
134 | 151 |
135 void HidServiceMac::PlatformAddDevice(IOHIDDeviceRef hid_device) { | 152 void HidServiceMac::PlatformAddDevice(IOHIDDeviceRef hid_device) { |
136 // Note that our ownership of hid_device is implied if calling this method. | 153 // Note that our ownership of hid_device is implied if calling this method. |
137 // It is balanced in PlatformRemoveDevice. | 154 // It is balanced in PlatformRemoveDevice. |
138 DCHECK(thread_checker_.CalledOnValidThread()); | 155 DCHECK(thread_checker_.CalledOnValidThread()); |
139 | |
140 HidDeviceInfo device_info; | 156 HidDeviceInfo device_info; |
141 device_info.device_id = hid_device; | 157 device_info.device_id = hid_device; |
142 device_info.vendor_id = | 158 device_info.vendor_id = |
143 GetHidIntProperty(hid_device, CFSTR(kIOHIDVendorIDKey)); | 159 GetHidIntProperty(hid_device, CFSTR(kIOHIDVendorIDKey)); |
144 device_info.product_id = | 160 device_info.product_id = |
145 GetHidIntProperty(hid_device, CFSTR(kIOHIDProductIDKey)); | 161 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 } | |
170 device_info.product_name = | 162 device_info.product_name = |
171 GetHidStringProperty(hid_device, CFSTR(kIOHIDProductKey)); | 163 GetHidStringProperty(hid_device, CFSTR(kIOHIDProductKey)); |
172 device_info.serial_number = | 164 device_info.serial_number = |
173 GetHidStringProperty(hid_device, CFSTR(kIOHIDSerialNumberKey)); | 165 GetHidStringProperty(hid_device, CFSTR(kIOHIDSerialNumberKey)); |
166 device_info.max_input_report_size = | |
167 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxInputReportSizeKey)); | |
168 device_info.max_output_report_size = | |
169 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxOutputReportSizeKey)); | |
170 device_info.max_feature_report_size = | |
171 GetHidIntProperty(hid_device, CFSTR(kIOHIDMaxFeatureReportSizeKey)); | |
172 CFMutableDictionaryRef collectionsMatch = | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:44
Can you please move this blob of code into its own
jracle (use Gerrit)
2014/06/07 12:56:22
It's like you were reading in my brains, fightenin
| |
173 CFDictionaryCreateMutable(kCFAllocatorDefault, | |
174 0, | |
175 &kCFTypeDictionaryKeyCallBacks, | |
176 &kCFTypeDictionaryValueCallBacks); | |
177 int type = kIOHIDElementTypeCollection; | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:44
How about:
const int kCollectionTypeVlaue = kIOHI
jracle (use Gerrit)
2014/06/07 12:56:21
sure
On 2014/06/06 20:04:44, Ken Rockot wrote:
| |
178 CFNumberRef typeCFNumberRef = | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:43
How about
CFNumberRef collection_type_id = CFNumb
jracle (use Gerrit)
2014/06/07 12:56:21
sorry, that's awful variable name I wished to rena
| |
179 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &type); | |
180 CFDictionarySetValue( | |
181 collectionsMatch, CFSTR(kIOHIDElementTypeKey), typeCFNumberRef); | |
182 CFRelease(typeCFNumberRef); | |
183 CFArrayRef collections = IOHIDDeviceCopyMatchingElements( | |
184 hid_device, collectionsMatch, kIOHIDOptionsTypeNone); | |
185 CFIndex collectionsCount = CFArrayGetCount(collections); | |
186 for (CFIndex i = 0; i < collectionsCount; i++) { | |
187 IOHIDElementRef collection = | |
188 (IOHIDElementRef)CFArrayGetValueAtIndex(collections, i); | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:43
static_cast
jracle (use Gerrit)
2014/06/07 12:56:21
sure
On 2014/06/06 20:04:43, Ken Rockot wrote:
| |
189 DCHECK(IOHIDElementGetType(collection) == kIOHIDElementTypeCollection); | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:43
Short of the world imploding or IOKit being fundam
jracle (use Gerrit)
2014/06/07 12:56:21
Let me think half a sec.. That's indeed stupid def
| |
190 // Top-Level Collection has no parent | |
191 if (IOHIDElementGetParent(collection) == 0) { | |
192 HidCollectionInfo collection_info; | |
193 HidUsageAndPage::Page page = | |
194 (HidUsageAndPage::Page)IOHIDElementGetUsagePage(collection); | |
Ken Rockot(use gerrit already)
2014/06/06 20:04:44
static_cast
jracle (use Gerrit)
2014/06/07 12:56:21
yes
On 2014/06/06 20:04:44, Ken Rockot wrote:
| |
195 uint16_t usage = IOHIDElementGetUsage(collection); | |
196 collection_info.usage = HidUsageAndPage(usage, page); | |
197 // Explore children recursively and retrieve their report IDs | |
198 GetReportIds(collection, collection_info.report_ids); | |
199 device_info.collections.push_back(collection_info); | |
200 } | |
201 } | |
174 AddDevice(device_info); | 202 AddDevice(device_info); |
175 } | 203 } |
176 | 204 |
177 void HidServiceMac::PlatformRemoveDevice(IOHIDDeviceRef hid_device) { | 205 void HidServiceMac::PlatformRemoveDevice(IOHIDDeviceRef hid_device) { |
178 DCHECK(thread_checker_.CalledOnValidThread()); | 206 DCHECK(thread_checker_.CalledOnValidThread()); |
179 RemoveDevice(hid_device); | 207 RemoveDevice(hid_device); |
180 CFRelease(hid_device); | 208 CFRelease(hid_device); |
181 } | 209 } |
182 | 210 |
183 scoped_refptr<HidConnection> HidServiceMac::Connect( | 211 scoped_refptr<HidConnection> HidServiceMac::Connect( |
184 const HidDeviceId& device_id) { | 212 const HidDeviceId& device_id) { |
185 DCHECK(thread_checker_.CalledOnValidThread()); | 213 DCHECK(thread_checker_.CalledOnValidThread()); |
186 HidDeviceInfo device_info; | 214 HidDeviceInfo device_info; |
187 if (!GetDeviceInfo(device_id, &device_info)) | 215 if (!GetDeviceInfo(device_id, &device_info)) |
188 return NULL; | 216 return NULL; |
189 return scoped_refptr<HidConnection>(new HidConnectionMac(device_info)); | 217 return scoped_refptr<HidConnection>(new HidConnectionMac(device_info)); |
190 } | 218 } |
191 | 219 |
192 } // namespace device | 220 } // namespace device |
OLD | NEW |