Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(486)

Side by Side Diff: trunk/src/device/hid/hid_service_mac.cc

Issue 364213005: Revert 281282 "Revert 281133 "chrome.hid: enrich model with repo..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « trunk/src/device/hid/hid_service_linux.cc ('k') | trunk/src/device/hid/hid_service_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
16 #include "base/mac/foundation_util.h"
15 #include "base/message_loop/message_loop_proxy.h" 17 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/stl_util.h" 18 #include "base/stl_util.h"
17 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/sys_string_conversions.h"
18 #include "base/threading/thread_restrictions.h" 21 #include "base/threading/thread_restrictions.h"
19 #include "device/hid/hid_connection_mac.h" 22 #include "device/hid/hid_connection_mac.h"
20 #include "device/hid/hid_utils_mac.h"
21 23
22 namespace device { 24 namespace device {
23 25
24 class HidServiceMac; 26 class HidServiceMac;
25 27
26 namespace { 28 namespace {
27 29
28 typedef std::vector<IOHIDDeviceRef> HidDeviceList; 30 typedef std::vector<IOHIDDeviceRef> HidDeviceList;
29 31
30 HidServiceMac* HidServiceFromContext(void* context) { 32 HidServiceMac* HidServiceFromContext(void* context) {
(...skipping 10 matching lines...) Expand all
41 43
42 void EnumerateHidDevices(IOHIDManagerRef hid_manager, 44 void EnumerateHidDevices(IOHIDManagerRef hid_manager,
43 HidDeviceList* device_list) { 45 HidDeviceList* device_list) {
44 DCHECK(device_list->size() == 0); 46 DCHECK(device_list->size() == 0);
45 // Note that our ownership of each copied device is implied. 47 // Note that our ownership of each copied device is implied.
46 base::ScopedCFTypeRef<CFSetRef> devices(IOHIDManagerCopyDevices(hid_manager)); 48 base::ScopedCFTypeRef<CFSetRef> devices(IOHIDManagerCopyDevices(hid_manager));
47 if (devices) 49 if (devices)
48 CFSetApplyFunction(devices, HidEnumerationBackInserter, device_list); 50 CFSetApplyFunction(devices, HidEnumerationBackInserter, device_list);
49 } 51 }
50 52
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
51 } // namespace 136 } // namespace
52 137
53 HidServiceMac::HidServiceMac() { 138 HidServiceMac::HidServiceMac() {
54 DCHECK(thread_checker_.CalledOnValidThread()); 139 DCHECK(thread_checker_.CalledOnValidThread());
55 message_loop_ = base::MessageLoopProxy::current(); 140 message_loop_ = base::MessageLoopProxy::current();
56 DCHECK(message_loop_); 141 DCHECK(message_loop_);
57 hid_manager_.reset(IOHIDManagerCreate(NULL, 0)); 142 hid_manager_.reset(IOHIDManagerCreate(NULL, 0));
58 if (!hid_manager_) { 143 if (!hid_manager_) {
59 LOG(ERROR) << "Failed to initialize HidManager"; 144 LOG(ERROR) << "Failed to initialize HidManager";
60 return; 145 return;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 ++iter) { 214 ++iter) {
130 IOHIDDeviceRef hid_device = *iter; 215 IOHIDDeviceRef hid_device = *iter;
131 PlatformAddDevice(hid_device); 216 PlatformAddDevice(hid_device);
132 } 217 }
133 } 218 }
134 219
135 void HidServiceMac::PlatformAddDevice(IOHIDDeviceRef hid_device) { 220 void HidServiceMac::PlatformAddDevice(IOHIDDeviceRef hid_device) {
136 // Note that our ownership of hid_device is implied if calling this method. 221 // Note that our ownership of hid_device is implied if calling this method.
137 // It is balanced in PlatformRemoveDevice. 222 // It is balanced in PlatformRemoveDevice.
138 DCHECK(thread_checker_.CalledOnValidThread()); 223 DCHECK(thread_checker_.CalledOnValidThread());
139
140 HidDeviceInfo device_info; 224 HidDeviceInfo device_info;
141 device_info.device_id = hid_device; 225 device_info.device_id = hid_device;
142 device_info.vendor_id = 226 device_info.vendor_id =
143 GetHidIntProperty(hid_device, CFSTR(kIOHIDVendorIDKey)); 227 GetHidIntProperty(hid_device, CFSTR(kIOHIDVendorIDKey));
144 device_info.product_id = 228 device_info.product_id =
145 GetHidIntProperty(hid_device, CFSTR(kIOHIDProductIDKey)); 229 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 = 230 device_info.product_name =
171 GetHidStringProperty(hid_device, CFSTR(kIOHIDProductKey)); 231 GetHidStringProperty(hid_device, CFSTR(kIOHIDProductKey));
172 device_info.serial_number = 232 device_info.serial_number =
173 GetHidStringProperty(hid_device, CFSTR(kIOHIDSerialNumberKey)); 233 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));
174 AddDevice(device_info); 241 AddDevice(device_info);
175 } 242 }
176 243
177 void HidServiceMac::PlatformRemoveDevice(IOHIDDeviceRef hid_device) { 244 void HidServiceMac::PlatformRemoveDevice(IOHIDDeviceRef hid_device) {
178 DCHECK(thread_checker_.CalledOnValidThread()); 245 DCHECK(thread_checker_.CalledOnValidThread());
179 RemoveDevice(hid_device); 246 RemoveDevice(hid_device);
180 CFRelease(hid_device); 247 CFRelease(hid_device);
181 } 248 }
182 249
183 scoped_refptr<HidConnection> HidServiceMac::Connect( 250 scoped_refptr<HidConnection> HidServiceMac::Connect(
184 const HidDeviceId& device_id) { 251 const HidDeviceId& device_id) {
185 DCHECK(thread_checker_.CalledOnValidThread()); 252 DCHECK(thread_checker_.CalledOnValidThread());
186 HidDeviceInfo device_info; 253 HidDeviceInfo device_info;
187 if (!GetDeviceInfo(device_id, &device_info)) 254 if (!GetDeviceInfo(device_id, &device_info))
188 return NULL; 255 return NULL;
189 return scoped_refptr<HidConnection>(new HidConnectionMac(device_info)); 256 return scoped_refptr<HidConnection>(new HidConnectionMac(device_info));
190 } 257 }
191 258
192 } // namespace device 259 } // namespace device
OLDNEW
« no previous file with comments | « trunk/src/device/hid/hid_service_linux.cc ('k') | trunk/src/device/hid/hid_service_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698