| 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 #include <stddef.h> | |
| 11 #include <stdint.h> | |
| 12 | |
| 13 #include <memory> | |
| 14 #include <set> | |
| 15 | |
| 16 #include "base/mac/scoped_cftyperef.h" | |
| 17 #include "base/mac/scoped_ioobject.h" | |
| 18 #include "base/mac/scoped_ioplugininterface.h" | |
| 19 #include "base/macros.h" | |
| 20 | |
| 21 class XboxController { | |
| 22 public: | |
| 23 enum ControllerType { | |
| 24 UNKNOWN_CONTROLLER, | |
| 25 XBOX_360_CONTROLLER, | |
| 26 XBOX_ONE_CONTROLLER | |
| 27 }; | |
| 28 | |
| 29 enum LEDPattern { | |
| 30 LED_OFF = 0, | |
| 31 | |
| 32 // 2 quick flashes, then a series of slow flashes (about 1 per second). | |
| 33 LED_FLASH = 1, | |
| 34 | |
| 35 // Flash three times then hold the LED on. This is the standard way to tell | |
| 36 // the player which player number they are. | |
| 37 LED_FLASH_TOP_LEFT = 2, | |
| 38 LED_FLASH_TOP_RIGHT = 3, | |
| 39 LED_FLASH_BOTTOM_LEFT = 4, | |
| 40 LED_FLASH_BOTTOM_RIGHT = 5, | |
| 41 | |
| 42 // Simply turn on the specified LED and turn all other LEDs off. | |
| 43 LED_HOLD_TOP_LEFT = 6, | |
| 44 LED_HOLD_TOP_RIGHT = 7, | |
| 45 LED_HOLD_BOTTOM_LEFT = 8, | |
| 46 LED_HOLD_BOTTOM_RIGHT = 9, | |
| 47 | |
| 48 LED_ROTATE = 10, | |
| 49 | |
| 50 LED_FLASH_FAST = 11, | |
| 51 LED_FLASH_SLOW = 12, // Flash about once per 3 seconds | |
| 52 | |
| 53 // Flash alternating LEDs for a few seconds, then flash all LEDs about once | |
| 54 // per second | |
| 55 LED_ALTERNATE_PATTERN = 13, | |
| 56 | |
| 57 // 14 is just another boring flashing speed. | |
| 58 | |
| 59 // Flash all LEDs once then go black. | |
| 60 LED_FLASH_ONCE = 15, | |
| 61 | |
| 62 LED_NUM_PATTERNS | |
| 63 }; | |
| 64 | |
| 65 struct Data { | |
| 66 bool buttons[15]; | |
| 67 float triggers[2]; | |
| 68 float axes[4]; | |
| 69 }; | |
| 70 | |
| 71 class Delegate { | |
| 72 public: | |
| 73 virtual void XboxControllerGotData(XboxController* controller, | |
| 74 const Data& data) = 0; | |
| 75 virtual void XboxControllerError(XboxController* controller) = 0; | |
| 76 }; | |
| 77 | |
| 78 explicit XboxController(Delegate* delegate_); | |
| 79 virtual ~XboxController(); | |
| 80 | |
| 81 bool OpenDevice(io_service_t service); | |
| 82 | |
| 83 void SetLEDPattern(LEDPattern pattern); | |
| 84 | |
| 85 UInt32 location_id() { return location_id_; } | |
| 86 int GetVendorId() const; | |
| 87 int GetProductId() const; | |
| 88 ControllerType GetControllerType() 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 ProcessXbox360Packet(size_t length); | |
| 95 void ProcessXboxOnePacket(size_t length); | |
| 96 void QueueRead(); | |
| 97 | |
| 98 void IOError(); | |
| 99 | |
| 100 void WriteXboxOneInit(); | |
| 101 | |
| 102 // Handle for the USB device. IOUSBDeviceStruct320 is the latest version of | |
| 103 // the device API that is supported on Mac OS 10.6. | |
| 104 base::mac::ScopedIOPluginInterface<struct IOUSBDeviceStruct320> device_; | |
| 105 | |
| 106 // Handle for the interface on the device which sends button and analog data. | |
| 107 // The other interfaces (for the ChatPad and headset) are ignored. | |
| 108 base::mac::ScopedIOPluginInterface<struct IOUSBInterfaceStruct300> interface_; | |
| 109 | |
| 110 bool device_is_open_; | |
| 111 bool interface_is_open_; | |
| 112 | |
| 113 base::ScopedCFTypeRef<CFRunLoopSourceRef> source_; | |
| 114 | |
| 115 // This will be set to the max packet size reported by the interface, which | |
| 116 // is 32 bytes. I would have expected USB to do message framing itself, but | |
| 117 // somehow we still sometimes (rarely!) get packets off the interface which | |
| 118 // aren't correctly framed. The 360 controller frames its packets with a 2 | |
| 119 // byte header (type, total length) so we can reframe the packet data | |
| 120 // ourselves. | |
| 121 uint16_t read_buffer_size_; | |
| 122 std::unique_ptr<uint8_t[]> read_buffer_; | |
| 123 | |
| 124 // The pattern that the LEDs on the device are currently displaying, or | |
| 125 // LED_NUM_PATTERNS if unknown. | |
| 126 LEDPattern led_pattern_; | |
| 127 | |
| 128 UInt32 location_id_; | |
| 129 | |
| 130 Delegate* delegate_; | |
| 131 | |
| 132 ControllerType controller_type_; | |
| 133 int read_endpoint_; | |
| 134 int control_endpoint_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(XboxController); | |
| 137 }; | |
| 138 | |
| 139 class XboxDataFetcher : public XboxController::Delegate { | |
| 140 public: | |
| 141 class Delegate { | |
| 142 public: | |
| 143 virtual void XboxDeviceAdd(XboxController* device) = 0; | |
| 144 virtual void XboxDeviceRemove(XboxController* device) = 0; | |
| 145 virtual void XboxValueChanged(XboxController* device, | |
| 146 const XboxController::Data& data) = 0; | |
| 147 }; | |
| 148 | |
| 149 explicit XboxDataFetcher(Delegate* delegate); | |
| 150 virtual ~XboxDataFetcher(); | |
| 151 | |
| 152 bool RegisterForNotifications(); | |
| 153 bool RegisterForDeviceNotifications( | |
| 154 int vendor_id, | |
| 155 int product_id, | |
| 156 base::mac::ScopedIOObject<io_iterator_t>* added_iter, | |
| 157 base::mac::ScopedIOObject<io_iterator_t>* removed_iter); | |
| 158 void UnregisterFromNotifications(); | |
| 159 | |
| 160 XboxController* ControllerForLocation(UInt32 location_id); | |
| 161 | |
| 162 private: | |
| 163 static void DeviceAdded(void* context, io_iterator_t iterator); | |
| 164 static void DeviceRemoved(void* context, io_iterator_t iterator); | |
| 165 void AddController(XboxController* controller); | |
| 166 void RemoveController(XboxController* controller); | |
| 167 void RemoveControllerByLocationID(uint32_t id); | |
| 168 void XboxControllerGotData(XboxController* controller, | |
| 169 const XboxController::Data& data) override; | |
| 170 void XboxControllerError(XboxController* controller) override; | |
| 171 | |
| 172 Delegate* delegate_; | |
| 173 | |
| 174 std::set<XboxController*> controllers_; | |
| 175 | |
| 176 bool listening_; | |
| 177 | |
| 178 // port_ owns source_, so this doesn't need to be a ScopedCFTypeRef, but we | |
| 179 // do need to maintain a reference to it so we can invalidate it. | |
| 180 CFRunLoopSourceRef source_; | |
| 181 IONotificationPortRef port_; | |
| 182 base::mac::ScopedIOObject<io_iterator_t> xbox_360_device_added_iter_; | |
| 183 base::mac::ScopedIOObject<io_iterator_t> xbox_360_device_removed_iter_; | |
| 184 base::mac::ScopedIOObject<io_iterator_t> xbox_one_device_added_iter_; | |
| 185 base::mac::ScopedIOObject<io_iterator_t> xbox_one_device_removed_iter_; | |
| 186 | |
| 187 DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher); | |
| 188 }; | |
| 189 | |
| 190 #endif // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | |
| OLD | NEW |