Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/gamepad/xbox_data_fetcher_mac.h" | |
| 6 | |
| 7 #include <CoreFoundation/CoreFoundation.h> | |
| 8 #include <IOKit/IOCFPlugIn.h> | |
| 9 #include <IOKit/IOKitLib.h> | |
| 10 #include <IOKit/usb/IOUSBLib.h> | |
| 11 #include <IOKit/usb/USB.h> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 14 #include "base/mac/scoped_cftyperef.h" | |
| 15 | |
| 16 namespace { | |
| 17 const int kVendorMicrosoft = 0x045e; | |
| 18 const int kProduct360Controller = 0x028e; | |
| 19 | |
| 20 const int kReadEndpoint = 1; | |
| 21 const int kControlEndpoint = 2; | |
| 22 | |
| 23 #pragma pack(push, 1) | |
| 24 struct ButtonData { | |
| 25 bool dpad_up : 1; | |
| 26 bool dpad_down : 1; | |
| 27 bool dpad_left : 1; | |
| 28 bool dpad_right : 1; | |
| 29 | |
| 30 bool start : 1; | |
| 31 bool back : 1; | |
| 32 bool stick_left_click : 1; | |
| 33 bool stick_right_click : 1; | |
| 34 | |
| 35 bool bumper_left : 1; | |
| 36 bool bumper_right : 1; | |
| 37 bool guide : 1; | |
| 38 bool dummy1 : 1; // Always 0. | |
| 39 | |
| 40 bool a : 1; | |
| 41 bool b : 1; | |
| 42 bool x : 1; | |
| 43 bool y : 1; | |
| 44 | |
| 45 uint8 trigger_left; | |
| 46 uint8 trigger_right; | |
| 47 | |
| 48 int16 stick_left_x; | |
| 49 int16 stick_left_y; | |
| 50 int16 stick_right_x; | |
| 51 int16 stick_right_y; | |
| 52 | |
| 53 // Always 0. | |
| 54 uint32 dummy2; | |
| 55 uint16 dummy3; | |
| 56 }; | |
| 57 #pragma pack(pop) | |
| 58 | |
| 59 COMPILE_ASSERT(sizeof(ButtonData) == 0x12, xbox_button_data_wrong_size); | |
| 60 | |
| 61 enum { | |
| 62 CONTROL_MESSAGE_SET_RUMBLE = 0, | |
| 63 CONTROL_MESSAGE_SET_LED = 1, | |
| 64 }; | |
| 65 | |
| 66 // From MSDN: | |
| 67 // http://msdn.microsoft.com/en-us/library/windows/desktop/ee417001(v=vs.85).asp x#dead_zone | |
| 68 const int16 kLeftThumbDeadzone = 7849; | |
| 69 const int16 kRightThumbDeadzone = 8689; | |
| 70 const uint8 kTriggerDeadzone = 30; | |
| 71 | |
| 72 void NormalizeAxis(int16 x, | |
| 73 int16 y, | |
| 74 int16 deadzone, | |
| 75 float* x_out, | |
| 76 float* y_out) { | |
| 77 float x_val = (float)x; | |
| 78 float y_val = (float)y; | |
| 79 | |
| 80 // Determine how far the stick is pushed. | |
| 81 float real_magnitude = sqrtf(x_val * x_val + y_val * y_val); | |
| 82 | |
| 83 // Check if the controller is outside a circular dead zone. | |
| 84 if (real_magnitude > deadzone) { | |
| 85 // Clip the magnitude at its expected maximum value. | |
| 86 float magnitude = real_magnitude > 32767 ? 32767 : real_magnitude; | |
| 87 | |
| 88 // Adjust magnitude relative to the end of the dead zone. | |
| 89 magnitude -= deadzone; | |
| 90 | |
| 91 // Normalize the magnitude with respect to its expected range giving a | |
| 92 // magnitude value of 0.0 to 1.0 | |
| 93 float ratio = (magnitude / (32767 - deadzone)) / real_magnitude; | |
| 94 | |
| 95 // Y is negated because xbox controllers have an opposite sign from | |
| 96 // the 'standard controller' recommendations. | |
| 97 *x_out = x_val * ratio; | |
| 98 *y_out = -y_val * ratio; | |
| 99 } else { | |
| 100 // If the controller is in the deadzone zero out the magnitude. | |
| 101 *x_out = *y_out = 0.0f; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 static float NormalizeTrigger(uint8 value) { | |
| 106 return value < kTriggerDeadzone ? 0 : | |
| 107 (float)(value - kTriggerDeadzone) / (kuint8max - kTriggerDeadzone); | |
| 108 } | |
| 109 | |
| 110 void NormalizeButtonData(const ButtonData& data, | |
| 111 XboxController::Data* normalized_data) { | |
| 112 normalized_data->buttons[0] = data.a ? 1.f : 0.f; | |
| 113 normalized_data->buttons[1] = data.b ? 1.f : 0.f; | |
| 114 normalized_data->buttons[2] = data.x ? 1.f : 0.f; | |
| 115 normalized_data->buttons[3] = data.y ? 1.f : 0.f; | |
| 116 normalized_data->buttons[4] = data.bumper_left ? 1.f : 0.f; | |
| 117 normalized_data->buttons[5] = data.bumper_right ? 1.f : 0.f; | |
| 118 normalized_data->buttons[6] = NormalizeTrigger(data.trigger_left); | |
| 119 normalized_data->buttons[7] = NormalizeTrigger(data.trigger_right); | |
| 120 normalized_data->buttons[8] = data.back ? 1.f : 0.f; | |
| 121 normalized_data->buttons[9] = data.start ? 1.f : 0.f; | |
| 122 normalized_data->buttons[10] = data.stick_left_click ? 1.f : 0.f; | |
| 123 normalized_data->buttons[11] = data.stick_right_click ? 1.f : 0.f; | |
| 124 normalized_data->buttons[12] = data.dpad_up ? 1.f : 0.f; | |
| 125 normalized_data->buttons[13] = data.dpad_down ? 1.f : 0.f; | |
| 126 normalized_data->buttons[14] = data.dpad_left ? 1.f : 0.f; | |
| 127 normalized_data->buttons[15] = data.dpad_right ? 1.f : 0.f; | |
| 128 normalized_data->buttons[16] = data.guide ? 1.f : 0.f; | |
| 129 NormalizeAxis(data.stick_left_x, | |
| 130 data.stick_left_y, | |
| 131 kLeftThumbDeadzone, | |
| 132 &normalized_data->axes[0], | |
| 133 &normalized_data->axes[1]); | |
| 134 NormalizeAxis(data.stick_right_x, | |
| 135 data.stick_right_y, | |
| 136 kRightThumbDeadzone, | |
| 137 &normalized_data->axes[2], | |
| 138 &normalized_data->axes[3]); | |
| 139 } | |
| 140 } // namespace | |
| 141 | |
| 142 XboxController::XboxController(Delegate* delegate) | |
| 143 : device_(NULL), | |
| 144 interface_(NULL), | |
| 145 device_is_open_(false), | |
| 146 interface_is_open_(false), | |
| 147 source_(NULL), | |
| 148 read_buffer_size_(0), | |
| 149 led_pattern_(LED_NUM_PATTERNS), | |
| 150 location_id_(0), | |
| 151 delegate_(delegate) { | |
| 152 } | |
| 153 | |
| 154 XboxController::~XboxController() { | |
| 155 if (source_) | |
| 156 CFRunLoopSourceInvalidate(source_); | |
| 157 if (interface_) { | |
| 158 if (interface_is_open_) | |
| 159 (*interface_)->USBInterfaceClose(interface_); | |
| 160 (*interface_)->Release(interface_); | |
| 161 } | |
| 162 if (device_) { | |
| 163 if (device_is_open_) | |
| 164 (*device_)->USBDeviceClose(device_); | |
| 165 (*device_)->Release(device_); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 bool XboxController::OpenDevice(io_service_t service) { | |
| 170 kern_return_t kr; | |
| 171 HRESULT res; | |
| 172 | |
| 173 IOCFPlugInInterface **plugin; | |
| 174 SInt32 score; // Unused, but required for IOCreatePlugInInterfaceForService. | |
| 175 kr = IOCreatePlugInInterfaceForService(service, | |
| 176 kIOUSBDeviceUserClientTypeID, | |
| 177 kIOCFPlugInInterfaceID, | |
| 178 &plugin, | |
| 179 &score); | |
| 180 service = 0; | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
Why are you zeroing out |service|? It's a local va
jeremya
2013/04/24 00:18:52
Not sure. Removed.
| |
| 181 if (kr != KERN_SUCCESS) | |
| 182 return false; | |
| 183 | |
| 184 // IOUSBDeviceStruct320 is the latest version of the device interface | |
| 185 // that is supported on Mac OS 10.6. | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
This comment, which answers the legitimate questio
jeremya
2013/04/24 00:18:52
Some other comments generally, or some other comme
| |
| 186 res = (*plugin)->QueryInterface( | |
| 187 plugin, | |
| 188 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID320), | |
| 189 (LPVOID *)&device_); | |
| 190 (*plugin)->Release(plugin); | |
| 191 plugin = NULL; | |
| 192 if (res || !device_) | |
| 193 return false; | |
| 194 | |
| 195 // Open the device and configure it. | |
| 196 kr = (*device_)->USBDeviceOpen(device_); | |
| 197 if (kr != KERN_SUCCESS) | |
| 198 return false; | |
| 199 device_is_open_ = true; | |
| 200 | |
| 201 // Xbox controllers have one configuration option which has configuration | |
| 202 // value 1. Try to set it and fail out if it couldn't be configured. | |
| 203 IOUSBConfigurationDescriptorPtr config_desc; | |
| 204 kr = (*device_)->GetConfigurationDescriptorPtr(device_, 0, &config_desc); | |
| 205 if (kr != KERN_SUCCESS) | |
| 206 return false; | |
| 207 kr = (*device_)->SetConfiguration(device_, config_desc->bConfigurationValue); | |
| 208 if (kr != KERN_SUCCESS) | |
| 209 return false; | |
| 210 | |
| 211 // The device has 4 interfaces. They are as follows: | |
| 212 // Protocol 1: | |
| 213 // - Endpoint 1 (in) : Controller events, including button presses. | |
| 214 // - Endpoint 2 (out): Rumble pack and LED control | |
| 215 // Protocol 2 has a single endpoint to read from a connected ChatPad device. | |
| 216 // Protocol 3 is used by a connected headset device. | |
| 217 // The device also has an interface on subclass 253, protocol 10 with no | |
| 218 // endpoints. It is unused. | |
| 219 // | |
| 220 // We don't currently support the ChatPad or headset, so protocol 1 is the | |
| 221 // only protocol we care about. | |
| 222 // | |
| 223 // For more detail, see https://github.com/Grumbel/xboxdrv/blob/master/PROTOCO L | |
| 224 IOUSBFindInterfaceRequest request; | |
| 225 request.bInterfaceClass = 255; | |
| 226 request.bInterfaceSubClass = 93; | |
| 227 request.bInterfaceProtocol = 1; | |
| 228 request.bAlternateSetting = kIOUSBFindInterfaceDontCare; | |
| 229 io_iterator_t iter; | |
| 230 kr = (*device_)->CreateInterfaceIterator(device_, &request, &iter); | |
| 231 if (kr != KERN_SUCCESS) | |
| 232 return false; | |
| 233 | |
| 234 // There should be exactly one usb interface which matches the requested | |
| 235 // settings. | |
| 236 io_service_t usb_interface = IOIteratorNext(iter); | |
| 237 if (!usb_interface) | |
| 238 return false; | |
| 239 | |
| 240 // We need to make an InterfaceInterface to communicate with the device | |
| 241 // endpoint. This is the same process as earlier: first make a | |
| 242 // PluginInterface from the io_service then make the InterfaceInterface from | |
| 243 // that. | |
| 244 IOCFPlugInInterface **plugin_interface; | |
| 245 kr = IOCreatePlugInInterfaceForService(usb_interface, | |
| 246 kIOUSBInterfaceUserClientTypeID, | |
| 247 kIOCFPlugInInterfaceID, | |
| 248 &plugin_interface, | |
| 249 &score); | |
| 250 if (kr != KERN_SUCCESS || !plugin_interface) | |
| 251 return false; | |
| 252 | |
| 253 // Release the usb interface, and any subsequent interfaces returned by the | |
| 254 // iterator. (There shouldn't be any, but in case a future device does | |
| 255 // contain more interfaces, this will serve to avoid memory leaks.) | |
| 256 do { | |
| 257 IOObjectRelease(usb_interface); | |
| 258 } while ((usb_interface = IOIteratorNext(iter))); | |
| 259 | |
| 260 // Actually create the interface. | |
| 261 kr = (*plugin_interface)->QueryInterface( | |
| 262 plugin_interface, | |
| 263 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID300), | |
| 264 (LPVOID *)&interface_); | |
| 265 | |
| 266 (*plugin_interface)->Release(plugin_interface); | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
You nulled out |plugin| above on line 191 when you
jeremya
2013/04/24 00:18:52
These two can be released pretty much as soon as t
| |
| 267 | |
| 268 if (kr != KERN_SUCCESS || !interface_) | |
| 269 return false; | |
| 270 | |
| 271 // Actually open the interface. | |
| 272 kr = (*interface_)->USBInterfaceOpen(interface_); | |
| 273 if (kr != KERN_SUCCESS) | |
| 274 return false; | |
| 275 interface_is_open_ = true; | |
| 276 | |
| 277 kr = (*interface_)->CreateInterfaceAsyncEventSource(interface_, &source_); | |
| 278 if (kr != KERN_SUCCESS || !source_) | |
| 279 return false; | |
| 280 CFRunLoopAddSource(CFRunLoopGetMain(), source_, kCFRunLoopDefaultMode); | |
| 281 // The CFRunLoop retains the source. | |
| 282 CFRelease(source_); | |
| 283 | |
| 284 // The interface should have two pipes. Pipe 1 with direction kUSBIn and pipe | |
| 285 // 2 with direction kUSBOut. Both pipes should have type kUSBInterrupt. | |
| 286 uint8 num_endpoints; | |
| 287 kr = (*interface_)->GetNumEndpoints(interface_, &num_endpoints); | |
| 288 if (kr != KERN_SUCCESS || num_endpoints < 2) | |
| 289 return false; | |
| 290 | |
| 291 for (int i = 1; i <= num_endpoints; i++) { | |
| 292 uint8 direction; | |
| 293 uint8 number; | |
| 294 uint8 transfer_type; | |
| 295 uint16 max_packet_size; | |
| 296 uint8 interval; | |
| 297 | |
| 298 kr = (*interface_)->GetPipeProperties(interface_, | |
| 299 i, | |
| 300 &direction, | |
| 301 &number, | |
| 302 &transfer_type, | |
| 303 &max_packet_size, | |
| 304 &interval); | |
| 305 if (kr != KERN_SUCCESS || transfer_type != kUSBInterrupt) | |
| 306 return false; | |
| 307 if (i == kReadEndpoint) { | |
| 308 if (direction != kUSBIn) | |
| 309 return false; | |
| 310 if (max_packet_size > 32) | |
| 311 return false; | |
| 312 read_buffer_.reset(new uint8[max_packet_size]); | |
| 313 read_buffer_size_ = max_packet_size; | |
| 314 QueueRead(); | |
| 315 } else if (i == kControlEndpoint) { | |
| 316 if (direction != kUSBOut) | |
| 317 return false; | |
| 318 } | |
| 319 } | |
| 320 | |
| 321 // If its useful, the deviceId can be used to track controllers through | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
s/its/it's/
| |
| 322 // reconnections. | |
| 323 kr = (*device_)->GetLocationID(device_, &location_id_); | |
| 324 if (kr != KERN_SUCCESS) | |
| 325 return false; | |
| 326 | |
| 327 return true; | |
| 328 } | |
| 329 | |
| 330 void XboxController::SetLEDPattern(LEDPattern pattern) { | |
| 331 led_pattern_ = pattern; | |
| 332 const UInt8 length = 3; | |
| 333 UInt8* buffer = new UInt8[length]; | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
Can you comment that this is released in WriteComp
jeremya
2013/04/24 00:18:52
Done.
| |
| 334 buffer[0] = (UInt8)CONTROL_MESSAGE_SET_LED; | |
| 335 buffer[1] = length; | |
| 336 buffer[2] = (UInt8)pattern; | |
| 337 kern_return_t kr = (*interface_)->WritePipeAsync(interface_, | |
| 338 kControlEndpoint, | |
| 339 buffer, | |
| 340 (UInt32)length, | |
| 341 WriteComplete, | |
| 342 buffer); | |
| 343 if (kr != KERN_SUCCESS) { | |
| 344 IOError(); | |
| 345 return; | |
| 346 } | |
| 347 } | |
| 348 | |
| 349 int XboxController::GetVendorId() const { | |
| 350 return kVendorMicrosoft; | |
| 351 } | |
| 352 | |
| 353 int XboxController::GetProductId() const { | |
| 354 return kProduct360Controller; | |
| 355 } | |
| 356 | |
| 357 void XboxController::WriteComplete(void* context, IOReturn result, void* arg0) { | |
| 358 // Ignoring any errors sending data, because they will usually only occur | |
| 359 // when the device is disconnected, in which case it really doesn't matter if | |
| 360 // the data got to the controller or not. | |
| 361 if (result != KERN_SUCCESS) | |
| 362 return; | |
| 363 | |
| 364 UInt8* buffer = (UInt8*)context; | |
| 365 delete[] buffer; | |
| 366 } | |
| 367 | |
| 368 void XboxController::GotData(void* context, IOReturn result, void* arg0) { | |
| 369 uint32 bytesRead = (uint32)arg0; | |
| 370 XboxController* controller = static_cast<XboxController*>(context); | |
| 371 | |
| 372 if (result != kIOReturnSuccess) { | |
| 373 // This will happen if the device was disconnected. The gamepad has | |
| 374 // probably been destroyed by a meteorite. | |
| 375 controller->IOError(); | |
| 376 return; | |
| 377 } | |
| 378 | |
| 379 controller->ProcessPacket(bytesRead); | |
| 380 | |
| 381 // Queue up another read. | |
| 382 controller->QueueRead(); | |
| 383 } | |
| 384 | |
| 385 void XboxController::ProcessPacket(uint32 length) { | |
| 386 if (length < 3) return; | |
| 387 DCHECK(length <= read_buffer_size_); | |
| 388 if (length > read_buffer_size_) { | |
| 389 IOError(); | |
| 390 return; | |
| 391 } | |
| 392 uint8* buffer = read_buffer_.get(); | |
| 393 | |
| 394 if (buffer[1] != length) | |
| 395 // Length in packet doesn't match length reported by USB. | |
| 396 return; | |
| 397 | |
| 398 uint8 type = buffer[0]; | |
| 399 buffer += 2; | |
| 400 length -= 2; | |
| 401 switch (type) { | |
| 402 case STATUS_MESSAGE_BUTTONS: { | |
| 403 if (length != sizeof(ButtonData)) | |
| 404 return; | |
| 405 ButtonData data; | |
| 406 memcpy(&data, buffer, sizeof(data)); | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
Perhaps you can reinterpret cast to get a ButtonDa
jeremya
2013/04/24 00:18:52
I had that initially, but comments in base/basicty
| |
| 407 Data normalized_data; | |
| 408 NormalizeButtonData(data, &normalized_data); | |
| 409 delegate_->XboxControllerGotData(this, normalized_data); | |
| 410 break; | |
| 411 } | |
| 412 case STATUS_MESSAGE_LED: | |
| 413 // The controller sends one of these messages every time the LED pattern | |
| 414 // is set, as well as once when it is plugged in. | |
| 415 if (led_pattern_ == LED_NUM_PATTERNS && buffer[0] < LED_NUM_PATTERNS) | |
| 416 led_pattern_ = (LEDPattern)buffer[0]; | |
| 417 break; | |
| 418 default: | |
| 419 // Unknown packet: ignore! | |
| 420 break; | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 void XboxController::QueueRead() { | |
| 425 kern_return_t kr = (*interface_)->ReadPipeAsync(interface_, | |
| 426 kReadEndpoint, | |
| 427 read_buffer_.get(), | |
| 428 read_buffer_size_, | |
| 429 GotData, | |
| 430 this); | |
| 431 if (kr != KERN_SUCCESS) | |
| 432 IOError(); | |
| 433 } | |
| 434 | |
| 435 void XboxController::IOError() { | |
| 436 delegate_->XboxControllerError(this); | |
| 437 } | |
| 438 | |
| 439 //----------------------------------------------------------------------------- | |
| 440 | |
| 441 XboxDataFetcher::XboxDataFetcher(Delegate* delegate) | |
| 442 : delegate_(delegate), | |
| 443 listening_(false), | |
| 444 port_(NULL), | |
| 445 source_(NULL) { | |
| 446 } | |
| 447 | |
| 448 XboxDataFetcher::~XboxDataFetcher() { | |
| 449 while (!controllers_.empty()) { | |
| 450 RemoveController(*controllers_.begin()); | |
| 451 } | |
| 452 UnregisterFromNotifications(); | |
| 453 } | |
| 454 | |
| 455 void XboxDataFetcher::DeviceAdded(void* context, io_iterator_t iterator) { | |
| 456 DCHECK(context); | |
| 457 XboxDataFetcher* fetcher = static_cast<XboxDataFetcher*>(context); | |
| 458 io_service_t ref; | |
| 459 while ((ref = IOIteratorNext(iterator))) { | |
| 460 base::mac::ScopedIOObject<io_service_t> scoped_ref(ref); | |
| 461 XboxController* controller = new XboxController(fetcher); | |
| 462 if (controller->OpenDevice(ref)) { | |
| 463 fetcher->AddController(controller); | |
| 464 } else { | |
| 465 delete controller; | |
| 466 } | |
| 467 } | |
| 468 } | |
| 469 | |
| 470 void XboxDataFetcher::DeviceRemoved(void* context, io_iterator_t iterator) { | |
| 471 DCHECK(context); | |
| 472 XboxDataFetcher* fetcher = static_cast<XboxDataFetcher*>(context); | |
| 473 io_service_t ref; | |
| 474 while ((ref = IOIteratorNext(iterator))) { | |
| 475 base::mac::ScopedIOObject<io_service_t> scoped_ref(ref); | |
| 476 base::mac::ScopedCFTypeRef<CFNumberRef> number( | |
| 477 (CFNumberRef)IORegistryEntryCreateCFProperty( | |
| 478 ref, | |
| 479 CFSTR(kUSBDevicePropertyLocationID), | |
| 480 kCFAllocatorDefault, | |
| 481 kNilOptions)); | |
| 482 UInt32 location_id = 0; | |
| 483 CFNumberGetValue(number, kCFNumberSInt32Type, &location_id); | |
| 484 fetcher->RemoveControllerByLocationID(location_id); | |
| 485 } | |
| 486 } | |
| 487 | |
| 488 void XboxDataFetcher::RegisterForNotifications() { | |
| 489 if (listening_) | |
| 490 return; | |
| 491 base::mac::ScopedCFTypeRef<CFNumberRef> vendor_cf( | |
| 492 CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, | |
| 493 &kVendorMicrosoft)); | |
| 494 base::mac::ScopedCFTypeRef<CFNumberRef> product_cf( | |
| 495 CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, | |
| 496 &kProduct360Controller)); | |
| 497 base::mac::ScopedCFTypeRef<CFMutableDictionaryRef> matching_dict( | |
| 498 IOServiceMatching(kIOUSBDeviceClassName)); | |
| 499 if (!matching_dict) | |
| 500 return; | |
| 501 CFDictionarySetValue(matching_dict, CFSTR(kUSBVendorID), vendor_cf); | |
| 502 CFDictionarySetValue(matching_dict, CFSTR(kUSBProductID), product_cf); | |
| 503 port_ = IONotificationPortCreate(kIOMasterPortDefault); | |
| 504 if (!port_) | |
| 505 return; | |
| 506 source_ = IONotificationPortGetRunLoopSource(port_); | |
| 507 if (!source_) | |
| 508 return; | |
| 509 CFRunLoopAddSource(CFRunLoopGetMain(), source_, kCFRunLoopDefaultMode); | |
| 510 // The CFRunLoop retains the source. | |
| 511 CFRelease(source_); | |
| 512 | |
| 513 listening_ = true; | |
| 514 | |
| 515 IOReturn ret; | |
| 516 | |
| 517 // IOServiceAddMatchingNotification() releases the dictionary when it's done. | |
| 518 // Retain it before each call to IOServiceAddMatchingNotification to keep | |
| 519 // things balanced. | |
| 520 CFRetain(matching_dict); | |
| 521 io_iterator_t device_added_iter; | |
| 522 ret = IOServiceAddMatchingNotification(port_, | |
| 523 kIOFirstMatchNotification, | |
| 524 matching_dict, | |
| 525 DeviceAdded, | |
| 526 this, | |
| 527 &device_added_iter); | |
| 528 device_added_iter_.reset(device_added_iter); | |
| 529 if (ret != kIOReturnSuccess) { | |
| 530 LOG(ERROR) << "Error listening for Xbox controller add events: " << ret; | |
| 531 return; | |
| 532 } | |
| 533 DeviceAdded(this, device_added_iter_.get()); | |
| 534 | |
| 535 CFRetain(matching_dict); | |
| 536 io_iterator_t device_removed_iter; | |
| 537 ret = IOServiceAddMatchingNotification(port_, | |
| 538 kIOTerminatedNotification, | |
| 539 matching_dict, | |
| 540 DeviceRemoved, | |
| 541 this, | |
| 542 &device_removed_iter); | |
| 543 device_removed_iter_.reset(device_removed_iter); | |
| 544 if (ret != kIOReturnSuccess) { | |
| 545 LOG(ERROR) << "Error listening for Xbox controller remove events: " << ret; | |
| 546 return; | |
| 547 } | |
| 548 DeviceRemoved(this, device_removed_iter_.get()); | |
| 549 } | |
| 550 | |
| 551 void XboxDataFetcher::UnregisterFromNotifications() { | |
| 552 if (!listening_) | |
| 553 return; | |
| 554 listening_ = false; | |
| 555 if (source_) | |
| 556 CFRunLoopSourceInvalidate(source_); | |
| 557 source_ = NULL; | |
| 558 if (port_) | |
| 559 IONotificationPortDestroy(port_); | |
| 560 port_ = NULL; | |
| 561 } | |
| 562 | |
| 563 void XboxDataFetcher::AddController(XboxController* controller) { | |
| 564 controllers_.insert(controller); | |
| 565 delegate_->XboxDeviceAdd(controller); | |
| 566 } | |
| 567 | |
| 568 void XboxDataFetcher::RemoveController(XboxController* controller) { | |
| 569 controllers_.erase(controller); | |
| 570 delegate_->XboxDeviceRemove(controller); | |
| 571 delete controller; | |
| 572 } | |
| 573 | |
| 574 void XboxDataFetcher::RemoveControllerByLocationID(uint32 location_id) { | |
| 575 XboxController* controller = NULL; | |
| 576 for (std::set<XboxController*>::iterator i = controllers_.begin(); | |
| 577 i != controllers_.end(); | |
| 578 ++i) { | |
| 579 if ((*i)->location_id() == location_id) { | |
| 580 controller = *i; | |
| 581 break; | |
| 582 } | |
| 583 } | |
| 584 if (controller) | |
| 585 RemoveController(controller); | |
| 586 } | |
| 587 | |
| 588 void XboxDataFetcher::XboxControllerGotData(XboxController* controller, | |
| 589 const XboxController::Data& data) { | |
| 590 delegate_->XboxValueChanged(controller, data); | |
| 591 } | |
| 592 | |
| 593 void XboxDataFetcher::XboxControllerError(XboxController* controller) { | |
| 594 RemoveController(controller); | |
| 595 } | |
| OLD | NEW |