OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/gamepad/gamepad_platform_data_fetcher_mac.h" | 5 #include "content/browser/gamepad/gamepad_platform_data_fetcher_mac.h" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/memory/scoped_nsobject.h" | 8 #include "base/memory/scoped_nsobject.h" |
9 #include "base/string16.h" | 9 #include "base/string16.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/time.h" | |
11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
12 | 13 |
13 #include <IOKit/hid/IOHIDKeys.h> | 14 #include <IOKit/hid/IOHIDKeys.h> |
14 #import <Foundation/Foundation.h> | 15 #import <Foundation/Foundation.h> |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 NSDictionary* DeviceMatching(uint32_t usage_page, uint32_t usage) { | 21 NSDictionary* DeviceMatching(uint32_t usage_page, uint32_t usage) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 | 54 |
54 scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects: | 55 scoped_nsobject<NSArray> criteria([[NSArray alloc] initWithObjects: |
55 DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber), | 56 DeviceMatching(kGenericDesktopUsagePage, kJoystickUsageNumber), |
56 DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber), | 57 DeviceMatching(kGenericDesktopUsagePage, kGameUsageNumber), |
57 DeviceMatching(kGenericDesktopUsagePage, kMultiAxisUsageNumber), | 58 DeviceMatching(kGenericDesktopUsagePage, kMultiAxisUsageNumber), |
58 nil]); | 59 nil]); |
59 IOHIDManagerSetDeviceMatchingMultiple( | 60 IOHIDManagerSetDeviceMatchingMultiple( |
60 hid_manager_ref_, | 61 hid_manager_ref_, |
61 base::mac::NSToCFCast(criteria)); | 62 base::mac::NSToCFCast(criteria)); |
62 | 63 |
64 xbox_fetcher_.reset(new XboxDataFetcher(this)); | |
65 | |
63 RegisterForNotifications(); | 66 RegisterForNotifications(); |
64 } | 67 } |
65 | 68 |
66 void GamepadPlatformDataFetcherMac::RegisterForNotifications() { | 69 void GamepadPlatformDataFetcherMac::RegisterForNotifications() { |
67 // Register for plug/unplug notifications. | 70 // Register for plug/unplug notifications. |
68 IOHIDManagerRegisterDeviceMatchingCallback( | 71 IOHIDManagerRegisterDeviceMatchingCallback( |
69 hid_manager_ref_, | 72 hid_manager_ref_, |
70 &DeviceAddCallback, | 73 &DeviceAddCallback, |
71 this); | 74 this); |
72 IOHIDManagerRegisterDeviceRemovalCallback( | 75 IOHIDManagerRegisterDeviceRemovalCallback( |
73 hid_manager_ref_, | 76 hid_manager_ref_, |
74 DeviceRemoveCallback, | 77 DeviceRemoveCallback, |
75 this); | 78 this); |
76 | 79 |
77 // Register for value change notifications. | 80 // Register for value change notifications. |
78 IOHIDManagerRegisterInputValueCallback( | 81 IOHIDManagerRegisterInputValueCallback( |
79 hid_manager_ref_, | 82 hid_manager_ref_, |
80 ValueChangedCallback, | 83 ValueChangedCallback, |
81 this); | 84 this); |
82 | 85 |
83 IOHIDManagerScheduleWithRunLoop( | 86 IOHIDManagerScheduleWithRunLoop( |
84 hid_manager_ref_, | 87 hid_manager_ref_, |
85 CFRunLoopGetMain(), | 88 CFRunLoopGetMain(), |
86 kCFRunLoopDefaultMode); | 89 kCFRunLoopDefaultMode); |
87 | 90 |
88 enabled_ = IOHIDManagerOpen(hid_manager_ref_, | 91 enabled_ = IOHIDManagerOpen(hid_manager_ref_, |
89 kIOHIDOptionsTypeNone) == kIOReturnSuccess; | 92 kIOHIDOptionsTypeNone) == kIOReturnSuccess; |
93 | |
94 xbox_fetcher_->RegisterForNotifications(); | |
90 } | 95 } |
91 | 96 |
92 void GamepadPlatformDataFetcherMac::UnregisterFromNotifications() { | 97 void GamepadPlatformDataFetcherMac::UnregisterFromNotifications() { |
93 IOHIDManagerUnscheduleFromRunLoop( | 98 IOHIDManagerUnscheduleFromRunLoop( |
94 hid_manager_ref_, | 99 hid_manager_ref_, |
95 CFRunLoopGetCurrent(), | 100 CFRunLoopGetCurrent(), |
96 kCFRunLoopDefaultMode); | 101 kCFRunLoopDefaultMode); |
97 IOHIDManagerClose(hid_manager_ref_, kIOHIDOptionsTypeNone); | 102 IOHIDManagerClose(hid_manager_ref_, kIOHIDOptionsTypeNone); |
103 xbox_fetcher_->UnregisterFromNotifications(); | |
98 } | 104 } |
99 | 105 |
100 void GamepadPlatformDataFetcherMac::PauseHint(bool pause) { | 106 void GamepadPlatformDataFetcherMac::PauseHint(bool pause) { |
101 if (pause) | 107 if (pause) |
102 UnregisterFromNotifications(); | 108 UnregisterFromNotifications(); |
103 else | 109 else |
104 RegisterForNotifications(); | 110 RegisterForNotifications(); |
105 } | 111 } |
106 | 112 |
107 GamepadPlatformDataFetcherMac::~GamepadPlatformDataFetcherMac() { | 113 GamepadPlatformDataFetcherMac::~GamepadPlatformDataFetcherMac() { |
108 UnregisterFromNotifications(); | 114 UnregisterFromNotifications(); |
109 } | 115 } |
110 | 116 |
111 GamepadPlatformDataFetcherMac* | 117 GamepadPlatformDataFetcherMac* |
112 GamepadPlatformDataFetcherMac::InstanceFromContext( | 118 GamepadPlatformDataFetcherMac::InstanceFromContext(void* context) { |
113 void* context) { | |
114 return reinterpret_cast<GamepadPlatformDataFetcherMac*>(context); | 119 return reinterpret_cast<GamepadPlatformDataFetcherMac*>(context); |
115 } | 120 } |
116 | 121 |
117 void GamepadPlatformDataFetcherMac::DeviceAddCallback(void* context, | 122 void GamepadPlatformDataFetcherMac::DeviceAddCallback(void* context, |
118 IOReturn result, | 123 IOReturn result, |
119 void* sender, | 124 void* sender, |
120 IOHIDDeviceRef ref) { | 125 IOHIDDeviceRef ref) { |
121 InstanceFromContext(context)->DeviceAdd(ref); | 126 InstanceFromContext(context)->DeviceAdd(ref); |
122 } | 127 } |
123 | 128 |
124 void GamepadPlatformDataFetcherMac::DeviceRemoveCallback(void* context, | 129 void GamepadPlatformDataFetcherMac::DeviceRemoveCallback(void* context, |
125 IOReturn result, | 130 IOReturn result, |
126 void* sender, | 131 void* sender, |
127 IOHIDDeviceRef ref) { | 132 IOHIDDeviceRef ref) { |
128 InstanceFromContext(context)->DeviceRemove(ref); | 133 InstanceFromContext(context)->DeviceRemove(ref); |
129 } | 134 } |
130 | 135 |
131 void GamepadPlatformDataFetcherMac::ValueChangedCallback(void* context, | 136 void GamepadPlatformDataFetcherMac::ValueChangedCallback(void* context, |
132 IOReturn result, | 137 IOReturn result, |
133 void* sender, | 138 void* sender, |
134 IOHIDValueRef ref) { | 139 IOHIDValueRef ref) { |
135 InstanceFromContext(context)->ValueChanged(ref); | 140 InstanceFromContext(context)->ValueChanged(ref); |
136 } | 141 } |
137 | 142 |
138 void GamepadPlatformDataFetcherMac::AddButtonsAndAxes(NSArray* elements, | 143 void GamepadPlatformDataFetcherMac::AddButtonsAndAxes(NSArray* elements, |
139 size_t slot) { | 144 size_t slot) { |
140 WebKit::WebGamepad& pad = data_.items[slot]; | 145 WebKit::WebGamepad& pad = data_.items[slot]; |
141 AssociatedData& associated = associated_[slot]; | 146 AssociatedData& associated = associated_[slot]; |
147 CHECK(!associated.is_xbox); | |
142 | 148 |
143 pad.axesLength = 0; | 149 pad.axesLength = 0; |
144 pad.buttonsLength = 0; | 150 pad.buttonsLength = 0; |
145 pad.timestamp = 0; | 151 pad.timestamp = 0; |
146 memset(pad.axes, 0, sizeof(pad.axes)); | 152 memset(pad.axes, 0, sizeof(pad.axes)); |
147 memset(pad.buttons, 0, sizeof(pad.buttons)); | 153 memset(pad.buttons, 0, sizeof(pad.buttons)); |
148 | 154 |
149 for (id elem in elements) { | 155 for (id elem in elements) { |
150 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); | 156 IOHIDElementRef element = reinterpret_cast<IOHIDElementRef>(elem); |
151 uint32_t usagePage = IOHIDElementGetUsagePage(element); | 157 uint32_t usagePage = IOHIDElementGetUsagePage(element); |
152 uint32_t usage = IOHIDElementGetUsage(element); | 158 uint32_t usage = IOHIDElementGetUsage(element); |
153 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button && | 159 if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Button && |
154 usagePage == kButtonUsagePage) { | 160 usagePage == kButtonUsagePage) { |
155 uint32_t button_index = usage - 1; | 161 uint32_t button_index = usage - 1; |
156 if (button_index < WebKit::WebGamepad::buttonsLengthCap) { | 162 if (button_index < WebKit::WebGamepad::buttonsLengthCap) { |
157 associated.button_elements[button_index] = element; | 163 associated.hid.button_elements[button_index] = element; |
158 pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1); | 164 pad.buttonsLength = std::max(pad.buttonsLength, button_index + 1); |
159 } | 165 } |
160 } | 166 } |
161 else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) { | 167 else if (IOHIDElementGetType(element) == kIOHIDElementTypeInput_Misc) { |
162 uint32_t axis_index = usage - kAxisMinimumUsageNumber; | 168 uint32_t axis_index = usage - kAxisMinimumUsageNumber; |
163 if (axis_index < WebKit::WebGamepad::axesLengthCap) { | 169 if (axis_index < WebKit::WebGamepad::axesLengthCap) { |
164 associated.axis_minimums[axis_index] = | 170 associated.hid.axis_minimums[axis_index] = |
165 IOHIDElementGetLogicalMin(element); | 171 IOHIDElementGetLogicalMin(element); |
166 associated.axis_maximums[axis_index] = | 172 associated.hid.axis_maximums[axis_index] = |
167 IOHIDElementGetLogicalMax(element); | 173 IOHIDElementGetLogicalMax(element); |
168 associated.axis_elements[axis_index] = element; | 174 associated.hid.axis_elements[axis_index] = element; |
169 pad.axesLength = std::max(pad.axesLength, axis_index + 1); | 175 pad.axesLength = std::max(pad.axesLength, axis_index + 1); |
170 } | 176 } |
171 } | 177 } |
172 } | 178 } |
173 } | 179 } |
174 | 180 |
175 void GamepadPlatformDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) { | 181 void GamepadPlatformDataFetcherMac::DeviceAdd(IOHIDDeviceRef device) { |
176 using WebKit::WebGamepad; | 182 using WebKit::WebGamepad; |
177 using WebKit::WebGamepads; | 183 using WebKit::WebGamepads; |
178 using base::mac::CFToNSCast; | 184 using base::mac::CFToNSCast; |
179 using base::mac::CFCastStrict; | 185 using base::mac::CFCastStrict; |
180 size_t slot; | 186 size_t slot; |
181 | 187 |
182 if (!enabled_) | 188 if (!enabled_) |
183 return; | 189 return; |
184 | 190 |
185 // Find an index for this device. | 191 // Find an index for this device. |
186 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | 192 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { |
187 // If we already have this device, and it's already connected, don't do | 193 // If we already have this device, and it's already connected, don't do |
188 // anything now. | 194 // anything now. |
189 if (associated_[slot].device_ref == device && data_.items[slot].connected) | 195 if (data_.items[slot].connected && |
196 !associated_[slot].is_xbox && | |
197 associated_[slot].hid.device_ref == device) | |
190 return; | 198 return; |
191 if (!data_.items[slot].connected) | 199 if (!data_.items[slot].connected) |
192 break; | 200 break; |
193 } | 201 } |
194 | 202 |
195 // We can't handle this many connected devices. | 203 // We can't handle this many connected devices. |
196 if (slot == WebGamepads::itemsLengthCap) | 204 if (slot == WebGamepads::itemsLengthCap) |
197 return; | 205 return; |
198 | 206 |
199 NSNumber* vendor_id = CFToNSCast(CFCastStrict<CFNumberRef>( | 207 NSNumber* vendor_id = CFToNSCast(CFCastStrict<CFNumberRef>( |
200 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)))); | 208 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDVendorIDKey)))); |
201 NSNumber* product_id = CFToNSCast(CFCastStrict<CFNumberRef>( | 209 NSNumber* product_id = CFToNSCast(CFCastStrict<CFNumberRef>( |
202 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)))); | 210 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductIDKey)))); |
203 NSString* product = CFToNSCast(CFCastStrict<CFStringRef>( | 211 NSString* product = CFToNSCast(CFCastStrict<CFStringRef>( |
204 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); | 212 IOHIDDeviceGetProperty(device, CFSTR(kIOHIDProductKey)))); |
205 int vendor_int = [vendor_id intValue]; | 213 int vendor_int = [vendor_id intValue]; |
206 int product_int = [product_id intValue]; | 214 int product_int = [product_id intValue]; |
207 | 215 |
208 char vendor_as_str[5], product_as_str[5]; | 216 char vendor_as_str[5], product_as_str[5]; |
209 snprintf(vendor_as_str, sizeof(vendor_as_str), "%04x", vendor_int); | 217 snprintf(vendor_as_str, sizeof(vendor_as_str), "%04x", vendor_int); |
210 snprintf(product_as_str, sizeof(product_as_str), "%04x", product_int); | 218 snprintf(product_as_str, sizeof(product_as_str), "%04x", product_int); |
211 associated_[slot].mapper = | 219 associated_[slot].hid.mapper = |
212 GetGamepadStandardMappingFunction(vendor_as_str, product_as_str); | 220 GetGamepadStandardMappingFunction(vendor_as_str, product_as_str); |
213 | 221 |
214 NSString* ident = [NSString stringWithFormat: | 222 NSString* ident = [NSString stringWithFormat: |
215 @"%@ (%sVendor: %04x Product: %04x)", | 223 @"%@ (%sVendor: %04x Product: %04x)", |
216 product, | 224 product, |
217 associated_[slot].mapper ? "STANDARD GAMEPAD " : "", | 225 associated_[slot].hid.mapper ? "STANDARD GAMEPAD " : "", |
218 vendor_int, | 226 vendor_int, |
219 product_int]; | 227 product_int]; |
220 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; | 228 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; |
221 | 229 |
222 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); | 230 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); |
223 memset(&data_.items[slot].id, 0, kOutputLengthBytes); | 231 memset(&data_.items[slot].id, 0, kOutputLengthBytes); |
224 [as16 getBytes:data_.items[slot].id | 232 [as16 getBytes:data_.items[slot].id |
225 length:kOutputLengthBytes - sizeof(WebKit::WebUChar)]; | 233 length:kOutputLengthBytes - sizeof(WebKit::WebUChar)]; |
226 | 234 |
227 base::mac::ScopedCFTypeRef<CFArrayRef> elements( | 235 base::mac::ScopedCFTypeRef<CFArrayRef> elements( |
228 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); | 236 IOHIDDeviceCopyMatchingElements(device, NULL, kIOHIDOptionsTypeNone)); |
229 AddButtonsAndAxes(CFToNSCast(elements), slot); | 237 AddButtonsAndAxes(CFToNSCast(elements), slot); |
230 | 238 |
231 associated_[slot].device_ref = device; | 239 associated_[slot].hid.device_ref = device; |
232 data_.items[slot].connected = true; | 240 data_.items[slot].connected = true; |
233 if (slot >= data_.length) | 241 if (slot >= data_.length) |
234 data_.length = slot + 1; | 242 data_.length = slot + 1; |
235 } | 243 } |
236 | 244 |
237 void GamepadPlatformDataFetcherMac::DeviceRemove(IOHIDDeviceRef device) { | 245 void GamepadPlatformDataFetcherMac::DeviceRemove(IOHIDDeviceRef device) { |
238 using WebKit::WebGamepads; | 246 using WebKit::WebGamepads; |
239 size_t slot; | 247 size_t slot; |
240 if (!enabled_) | 248 if (!enabled_) |
241 return; | 249 return; |
242 | 250 |
243 // Find the index for this device. | 251 // Find the index for this device. |
244 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | 252 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { |
245 if (associated_[slot].device_ref == device && data_.items[slot].connected) | 253 if (data_.items[slot].connected && |
254 !associated_[slot].is_xbox && | |
255 associated_[slot].hid.device_ref == device) | |
246 break; | 256 break; |
247 } | 257 } |
248 DCHECK(slot < WebGamepads::itemsLengthCap); | 258 DCHECK(slot < WebGamepads::itemsLengthCap); |
249 // Leave associated device_ref so that it will be reconnected in the same | 259 // Leave associated device_ref so that it will be reconnected in the same |
250 // location. Simply mark it as disconnected. | 260 // location. Simply mark it as disconnected. |
251 data_.items[slot].connected = false; | 261 data_.items[slot].connected = false; |
252 } | 262 } |
253 | 263 |
254 void GamepadPlatformDataFetcherMac::ValueChanged(IOHIDValueRef value) { | 264 void GamepadPlatformDataFetcherMac::ValueChanged(IOHIDValueRef value) { |
255 if (!enabled_) | 265 if (!enabled_) |
256 return; | 266 return; |
257 | 267 |
258 IOHIDElementRef element = IOHIDValueGetElement(value); | 268 IOHIDElementRef element = IOHIDValueGetElement(value); |
259 IOHIDDeviceRef device = IOHIDElementGetDevice(element); | 269 IOHIDDeviceRef device = IOHIDElementGetDevice(element); |
260 | 270 |
261 // Find device slot. | 271 // Find device slot. |
262 size_t slot; | 272 size_t slot; |
263 for (slot = 0; slot < data_.length; ++slot) { | 273 for (slot = 0; slot < data_.length; ++slot) { |
264 if (data_.items[slot].connected && associated_[slot].device_ref == device) | 274 if (data_.items[slot].connected && |
275 !associated_[slot].is_xbox && | |
276 associated_[slot].hid.device_ref == device) | |
265 break; | 277 break; |
266 } | 278 } |
267 if (slot == data_.length) | 279 if (slot == data_.length) |
268 return; | 280 return; |
269 | 281 |
270 WebKit::WebGamepad& pad = data_.items[slot]; | 282 WebKit::WebGamepad& pad = data_.items[slot]; |
271 AssociatedData& associated = associated_[slot]; | 283 AssociatedData& associated = associated_[slot]; |
272 | 284 |
273 // Find and fill in the associated button event, if any. | 285 // Find and fill in the associated button event, if any. |
274 for (size_t i = 0; i < pad.buttonsLength; ++i) { | 286 for (size_t i = 0; i < pad.buttonsLength; ++i) { |
275 if (associated.button_elements[i] == element) { | 287 if (associated.hid.button_elements[i] == element) { |
276 pad.buttons[i] = IOHIDValueGetIntegerValue(value) ? 1.f : 0.f; | 288 pad.buttons[i] = IOHIDValueGetIntegerValue(value) ? 1.f : 0.f; |
277 pad.timestamp = std::max(pad.timestamp, IOHIDValueGetTimeStamp(value)); | 289 pad.timestamp = std::max(pad.timestamp, IOHIDValueGetTimeStamp(value)); |
278 return; | 290 return; |
279 } | 291 } |
280 } | 292 } |
281 | 293 |
282 // Find and fill in the associated axis event, if any. | 294 // Find and fill in the associated axis event, if any. |
283 for (size_t i = 0; i < pad.axesLength; ++i) { | 295 for (size_t i = 0; i < pad.axesLength; ++i) { |
284 if (associated.axis_elements[i] == element) { | 296 if (associated.hid.axis_elements[i] == element) { |
285 pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value), | 297 pad.axes[i] = NormalizeAxis(IOHIDValueGetIntegerValue(value), |
286 associated.axis_minimums[i], | 298 associated.hid.axis_minimums[i], |
287 associated.axis_maximums[i]); | 299 associated.hid.axis_maximums[i]); |
288 pad.timestamp = std::max(pad.timestamp, IOHIDValueGetTimeStamp(value)); | 300 pad.timestamp = std::max(pad.timestamp, IOHIDValueGetTimeStamp(value)); |
289 return; | 301 return; |
290 } | 302 } |
291 } | 303 } |
292 } | 304 } |
293 | 305 |
306 void GamepadPlatformDataFetcherMac::XboxDeviceAdd(XboxController* device) { | |
307 using WebKit::WebGamepad; | |
308 using WebKit::WebGamepads; | |
Avi (use Gerrit)
2013/04/22 21:00:51
It seems a bit silly that every function puts thes
jeremya
2013/04/23 04:38:25
Done.
| |
309 size_t slot; | |
Mark Mentovai
2013/04/23 19:46:59
Don’t declare stuff ’til you use it. You can decla
jeremya
2013/04/24 00:18:51
Done.
| |
310 | |
311 if (!enabled_) | |
312 return; | |
313 | |
314 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | |
315 if (associated_[slot].is_xbox && | |
316 !data_.items[slot].connected && | |
317 associated_[slot].xbox.location_id == device->location_id()) { | |
318 break; | |
319 } | |
320 } | |
321 | |
322 if (slot == WebGamepads::itemsLengthCap) { | |
323 // Find an index for this device. | |
324 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | |
325 // If we already have this device, and it's already connected, don't do | |
326 // anything now. | |
327 if (associated_[slot].is_xbox && | |
328 data_.items[slot].connected && | |
329 associated_[slot].xbox.device == device) | |
330 return; | |
331 // XXX looks like there's a bug here? what if the first slot is empty, | |
332 // but the device is connected in slot 3? hm. also in DeviceAdd. | |
scottmg
2013/04/19 17:03:50
yup, you're right :(
jeremya
2013/04/21 00:33:09
Cool, I'll fix up this logic (and maybe see if I c
| |
333 if (!data_.items[slot].connected) | |
334 break; | |
335 } | |
336 } | |
337 | |
338 // We can't handle this many connected devices. | |
339 if (slot == WebGamepads::itemsLengthCap) | |
340 return; | |
341 | |
342 device->SetLEDPattern( | |
343 (XboxController::LEDPattern)(XboxController::LED_FLASH_1 + slot)); | |
344 | |
345 NSString* ident = | |
346 @"Xbox360 Controller (STANDARD GAMEPAD Vendor: 045e Product: 028e)"; | |
scottmg
2013/04/19 17:03:50
space after Xbox to match https://code.google.com/
jeremya
2013/04/23 04:38:25
Done, also changed to not duplicate the vendor/pro
| |
347 NSData* as16 = [ident dataUsingEncoding:NSUTF16StringEncoding]; | |
348 const size_t kOutputLengthBytes = sizeof(data_.items[slot].id); | |
349 memset(&data_.items[slot].id, 0, kOutputLengthBytes); | |
350 [as16 getBytes:data_.items[slot].id | |
351 length:kOutputLengthBytes - sizeof(WebKit::WebUChar)]; | |
352 | |
353 associated_[slot].is_xbox = true; | |
354 associated_[slot].xbox.device = device; | |
355 associated_[slot].xbox.location_id = device->location_id(); | |
356 data_.items[slot].connected = true; | |
357 data_.items[slot].axesLength = 4; | |
358 data_.items[slot].buttonsLength = 17; | |
359 data_.items[slot].timestamp = 0; | |
360 if (slot >= data_.length) | |
361 data_.length = slot + 1; | |
362 } | |
363 | |
364 void GamepadPlatformDataFetcherMac::XboxDeviceRemove(XboxController* device) { | |
365 using WebKit::WebGamepads; | |
366 size_t slot; | |
367 if (!enabled_) | |
368 return; | |
369 | |
370 // Find the index for this device. | |
371 for (slot = 0; slot < WebGamepads::itemsLengthCap; ++slot) { | |
372 if (data_.items[slot].connected && | |
373 associated_[slot].is_xbox && | |
374 associated_[slot].xbox.device == device) | |
375 break; | |
376 } | |
377 DCHECK(slot < WebGamepads::itemsLengthCap); | |
378 // Leave associated device_ref so that it will be reconnected in the same | |
379 // location. Simply mark it as disconnected. | |
380 // TODO(jeremya): use location id to match replugged controllers. | |
381 data_.items[slot].connected = false; | |
382 } | |
383 | |
384 void GamepadPlatformDataFetcherMac::XboxValueChanged( | |
385 XboxController* device, const XboxController::Data& data) { | |
386 // XXX should have xbox_enabled_ too. | |
387 if (!enabled_) | |
388 return; | |
389 | |
390 // Find device slot. | |
391 size_t slot; | |
392 for (slot = 0; slot < data_.length; ++slot) { | |
393 if (data_.items[slot].connected && | |
394 associated_[slot].is_xbox && | |
395 associated_[slot].xbox.device == device) | |
396 break; | |
397 } | |
398 if (slot == data_.length) | |
399 return; | |
400 | |
401 WebKit::WebGamepad& pad = data_.items[slot]; | |
402 | |
403 // Find and fill in the associated button event, if any. | |
404 for (size_t i = 0; i < arraysize(data.buttons); i++) { | |
405 pad.buttons[i] = data.buttons[i]; | |
406 } | |
407 for (size_t i = 0; i < arraysize(data.axes); i++) { | |
408 pad.axes[i] = data.axes[i]; | |
409 } | |
410 pad.timestamp = base::TimeTicks::Now().ToInternalValue(); | |
411 } | |
412 | |
294 void GamepadPlatformDataFetcherMac::GetGamepadData( | 413 void GamepadPlatformDataFetcherMac::GetGamepadData( |
295 WebKit::WebGamepads* pads, | 414 WebKit::WebGamepads* pads, |
296 bool) { | 415 bool) { |
297 if (!enabled_) { | 416 if (!enabled_) { |
298 pads->length = 0; | 417 pads->length = 0; |
299 return; | 418 return; |
300 } | 419 } |
301 | 420 |
302 // Copy to the current state to the output buffer, using the mapping | 421 // Copy to the current state to the output buffer, using the mapping |
303 // function, if there is one available. | 422 // function, if there is one available. |
304 pads->length = WebKit::WebGamepads::itemsLengthCap; | 423 pads->length = WebKit::WebGamepads::itemsLengthCap; |
305 for (size_t i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { | 424 for (size_t i = 0; i < WebKit::WebGamepads::itemsLengthCap; ++i) { |
306 if (associated_[i].mapper) | 425 if (!associated_[i].is_xbox && associated_[i].hid.mapper) |
307 associated_[i].mapper(data_.items[i], &pads->items[i]); | 426 associated_[i].hid.mapper(data_.items[i], &pads->items[i]); |
308 else | 427 else |
309 pads->items[i] = data_.items[i]; | 428 pads->items[i] = data_.items[i]; |
310 } | 429 } |
311 } | 430 } |
312 | 431 |
313 } // namespace content | 432 } // namespace content |
OLD | NEW |