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 #ifndef CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | |
| 6 #define CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | |
| 7 | |
| 8 #include <IOKit/IOKitLib.h> | |
| 9 #include <CoreFoundation/CoreFoundation.h> | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
abcde...
| |
| 10 | |
| 11 #include <set> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/mac/scoped_ioobject.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 | |
| 17 class XboxController { | |
| 18 public: | |
| 19 typedef enum { | |
| 20 STATUS_MESSAGE_BUTTONS = 0, | |
| 21 STATUS_MESSAGE_LED = 1, | |
| 22 STATUS_MESSAGE_UNKNOWN = 2, | |
| 23 | |
| 24 // Apparently this message tells you if the rumble pack is disabled in the | |
| 25 // controller. If the rumble pack is disabled, vibration control messages | |
| 26 // have no effect. | |
| 27 STATUS_MESSAGE_RUMBLE = 3, | |
| 28 } StatusMessageType; | |
| 29 | |
| 30 // The LEDs are numbered top left, top right, bottom left, bottom right. | |
| 31 typedef enum { | |
| 32 LED_OFF = 0, | |
| 33 | |
| 34 // 2 quick flashes, then a series of slow flashes (about 1 per second). | |
| 35 LED_FLASH = 1, | |
| 36 | |
| 37 // Flash three times then hold the LED on. This is the standard way to tell | |
| 38 // the player which player number they are. | |
| 39 LED_FLASH_1 = 2, | |
| 40 LED_FLASH_2 = 3, | |
| 41 LED_FLASH_3 = 4, | |
| 42 LED_FLASH_4 = 5, | |
| 43 | |
| 44 // Simply turn on the numbered LED and turn all other LEDs off. | |
| 45 LED_HOLD_1 = 6, | |
| 46 LED_HOLD_2 = 7, | |
| 47 LED_HOLD_3 = 8, | |
| 48 LED_HOLD_4 = 9, | |
| 49 | |
| 50 LED_ROTATE = 10, | |
| 51 | |
| 52 LED_FLASH_FAST = 11, | |
| 53 LED_FLASH_SLOW = 12, // Flash about once per 3 seconds | |
| 54 | |
| 55 // Flash alternating LEDs for a few seconds, then flash all LEDs about once | |
| 56 // per second | |
| 57 LED_ALTERNATE_PATTERN = 13, | |
| 58 | |
| 59 // 14 is just another boring flashing speed. | |
| 60 | |
| 61 // Flash all LEDs once then go black. | |
| 62 LED_FLASH_ONCE = 15, | |
| 63 | |
| 64 LED_NUM_PATTERNS | |
| 65 } LEDPattern; | |
| 66 | |
| 67 struct Data { | |
| 68 float buttons[17]; | |
| 69 float axes[4]; | |
| 70 }; | |
| 71 | |
| 72 class Delegate { | |
| 73 public: | |
| 74 virtual void XboxControllerGotData(XboxController* controller, | |
| 75 const Data& data) = 0; | |
| 76 virtual void XboxControllerError(XboxController* controller) = 0; | |
| 77 }; | |
| 78 | |
| 79 XboxController(Delegate* delegate_); | |
| 80 virtual ~XboxController(); | |
| 81 | |
| 82 bool OpenDevice(io_service_t service); | |
| 83 | |
| 84 void SetLEDPattern(LEDPattern pattern); | |
| 85 | |
| 86 UInt32 location_id() { return location_id_; } | |
| 87 int GetVendorId() const; | |
| 88 int GetProductId() const; | |
| 89 | |
| 90 private: | |
| 91 static void WriteComplete(void* context, IOReturn result, void* arg0); | |
| 92 static void GotData(void* context, IOReturn result, void* arg0); | |
| 93 | |
| 94 void ProcessPacket(uint32 length); | |
| 95 void QueueRead(); | |
| 96 | |
| 97 void IOError(); | |
| 98 | |
| 99 // The device itself. Available on MacOS 10.5.4 and up. | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
The device is available on 10.5.4 up? Or IOUSBDevi
jeremya
2013/04/24 00:18:52
I guess there's not really any need to call it out
| |
| 100 struct IOUSBDeviceStruct320** device_; | |
| 101 | |
| 102 // InterfaceInterface300 is available from MacOS 10.5 up. We only care about | |
| 103 // the interface which can receive commands. The other interfaces (for | |
| 104 // the ChatPad and headset) are ignored for now. | |
| 105 struct IOUSBInterfaceStruct300** interface_; | |
| 106 | |
| 107 bool device_is_open_; | |
| 108 bool interface_is_open_; | |
| 109 | |
| 110 CFRunLoopSourceRef source_; | |
| 111 | |
| 112 // This will be set to the max packet size reported by the interface, which | |
| 113 // is 32 bytes. I would have expected USB to do message framing itself, but | |
| 114 // somehow we still sometimes (rarely!) get packets off the interface which | |
| 115 // aren't correctly framed. The 360 controller frames its packets with a 2 | |
| 116 // byte header (type, total length) so we can reframe the packet data | |
| 117 // ourselves. | |
| 118 uint16 read_buffer_size_; | |
| 119 scoped_array<uint8> read_buffer_; | |
|
Avi (use Gerrit)
2013/04/23 15:12:38
You really need to get off scoped_array. scoped_pt
jeremya
2013/04/24 00:18:52
Done.
| |
| 120 | |
| 121 // The pattern that the LEDs on the device are currently displaying, or | |
| 122 // LED_NUM_PATTERNS if unknown. | |
| 123 LEDPattern led_pattern_; | |
| 124 | |
| 125 UInt32 location_id_; | |
| 126 | |
| 127 Delegate* delegate_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(XboxController); | |
| 130 }; | |
| 131 | |
| 132 class XboxDataFetcher : public XboxController::Delegate { | |
| 133 public: | |
| 134 class Delegate { | |
| 135 public: | |
| 136 virtual void XboxDeviceAdd(XboxController* device) = 0; | |
| 137 virtual void XboxDeviceRemove(XboxController* device) = 0; | |
| 138 virtual void XboxValueChanged(XboxController* device, | |
| 139 const XboxController::Data& data) = 0; | |
| 140 }; | |
| 141 | |
| 142 XboxDataFetcher(Delegate* delegate); | |
| 143 virtual ~XboxDataFetcher(); | |
| 144 | |
| 145 void RegisterForNotifications(); | |
| 146 void UnregisterFromNotifications(); | |
| 147 | |
| 148 private: | |
| 149 static void DeviceAdded(void* context, io_iterator_t iterator); | |
| 150 static void DeviceRemoved(void* context, io_iterator_t iterator); | |
| 151 void AddController(XboxController* controller); | |
| 152 void RemoveController(XboxController* controller); | |
| 153 void RemoveControllerByLocationID(uint32 id); | |
| 154 virtual void XboxControllerGotData(XboxController* controller, | |
| 155 const XboxController::Data& data) OVERRIDE; | |
| 156 virtual void XboxControllerError(XboxController* controller) OVERRIDE; | |
| 157 | |
| 158 Delegate* delegate_; | |
| 159 | |
| 160 std::set<XboxController*> controllers_; | |
| 161 | |
| 162 bool listening_; | |
| 163 | |
| 164 IONotificationPortRef port_; | |
| 165 CFRunLoopSourceRef source_; | |
| 166 base::mac::ScopedIOObject<io_iterator_t> device_added_iter_; | |
| 167 base::mac::ScopedIOObject<io_iterator_t> device_removed_iter_; | |
| 168 | |
| 169 DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher); | |
| 170 }; | |
| 171 | |
| 172 #endif // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | |
| OLD | NEW |