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