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 <CoreFoundation/CoreFoundation.h> |
| 9 #include <IOKit/IOKitLib.h> |
| 10 |
| 11 #include <set> |
| 12 |
| 13 #include "base/basictypes.h" |
| 14 #include "base/mac/scoped_ioobject.h" |
| 15 #include "base/mac/scoped_ioplugininterface.h" |
| 16 #include "base/memory/scoped_ptr.h" |
| 17 |
| 18 class XboxController { |
| 19 public: |
| 20 typedef enum { |
| 21 STATUS_MESSAGE_BUTTONS = 0, |
| 22 STATUS_MESSAGE_LED = 1, |
| 23 STATUS_MESSAGE_UNKNOWN = 2, |
| 24 |
| 25 // Apparently this message tells you if the rumble pack is disabled in the |
| 26 // controller. If the rumble pack is disabled, vibration control messages |
| 27 // have no effect. |
| 28 STATUS_MESSAGE_RUMBLE = 3, |
| 29 } StatusMessageType; |
| 30 |
| 31 // The LEDs are numbered top left, top right, bottom left, bottom right. |
| 32 typedef enum { |
| 33 LED_OFF = 0, |
| 34 |
| 35 // 2 quick flashes, then a series of slow flashes (about 1 per second). |
| 36 LED_FLASH = 1, |
| 37 |
| 38 // Flash three times then hold the LED on. This is the standard way to tell |
| 39 // the player which player number they are. |
| 40 LED_FLASH_1 = 2, |
| 41 LED_FLASH_2 = 3, |
| 42 LED_FLASH_3 = 4, |
| 43 LED_FLASH_4 = 5, |
| 44 |
| 45 // Simply turn on the numbered LED and turn all other LEDs off. |
| 46 LED_HOLD_1 = 6, |
| 47 LED_HOLD_2 = 7, |
| 48 LED_HOLD_3 = 8, |
| 49 LED_HOLD_4 = 9, |
| 50 |
| 51 LED_ROTATE = 10, |
| 52 |
| 53 LED_FLASH_FAST = 11, |
| 54 LED_FLASH_SLOW = 12, // Flash about once per 3 seconds |
| 55 |
| 56 // Flash alternating LEDs for a few seconds, then flash all LEDs about once |
| 57 // per second |
| 58 LED_ALTERNATE_PATTERN = 13, |
| 59 |
| 60 // 14 is just another boring flashing speed. |
| 61 |
| 62 // Flash all LEDs once then go black. |
| 63 LED_FLASH_ONCE = 15, |
| 64 |
| 65 LED_NUM_PATTERNS |
| 66 } LEDPattern; |
| 67 |
| 68 struct Data { |
| 69 float buttons[17]; |
| 70 float axes[4]; |
| 71 }; |
| 72 |
| 73 class Delegate { |
| 74 public: |
| 75 virtual void XboxControllerGotData(XboxController* controller, |
| 76 const Data& data) = 0; |
| 77 virtual void XboxControllerError(XboxController* controller) = 0; |
| 78 }; |
| 79 |
| 80 XboxController(Delegate* delegate_); |
| 81 virtual ~XboxController(); |
| 82 |
| 83 bool OpenDevice(io_service_t service); |
| 84 |
| 85 void SetLEDPattern(LEDPattern pattern); |
| 86 |
| 87 UInt32 location_id() { return location_id_; } |
| 88 int GetVendorId() const; |
| 89 int GetProductId() const; |
| 90 |
| 91 private: |
| 92 static void WriteComplete(void* context, IOReturn result, void* arg0); |
| 93 static void GotData(void* context, IOReturn result, void* arg0); |
| 94 |
| 95 void ProcessPacket(uint32 length); |
| 96 void QueueRead(); |
| 97 |
| 98 void IOError(); |
| 99 |
| 100 // Handle for the USB device. |
| 101 base::mac::ScopedIOPluginInterface<struct IOUSBDeviceStruct320> device_; |
| 102 |
| 103 // Handle for the interface on the device which sends button and analog data. |
| 104 // The other interfaces (for the ChatPad and headset) are ignored. |
| 105 base::mac::ScopedIOPluginInterface<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_ptr<uint8[]> read_buffer_; |
| 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 |