| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | 5 #ifndef CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ |
| 6 #define CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | 6 #define CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ |
| 7 | 7 |
| 8 #include <CoreFoundation/CoreFoundation.h> | 8 #include <CoreFoundation/CoreFoundation.h> |
| 9 #include <IOKit/IOKitLib.h> | 9 #include <IOKit/IOKitLib.h> |
| 10 | 10 |
| 11 #include <set> | 11 #include <set> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 15 #include "base/mac/scoped_ioobject.h" | 15 #include "base/mac/scoped_ioobject.h" |
| 16 #include "base/mac/scoped_ioplugininterface.h" | 16 #include "base/mac/scoped_ioplugininterface.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 | 18 |
| 19 class XboxController { | 19 class XboxController { |
| 20 public: | 20 public: |
| 21 enum ControllerType { |
| 22 UNKNOWN_CONTROLLER, |
| 23 XBOX_360_CONTROLLER, |
| 24 XBOX_ONE_CONTROLLER |
| 25 }; |
| 26 |
| 21 enum LEDPattern { | 27 enum LEDPattern { |
| 22 LED_OFF = 0, | 28 LED_OFF = 0, |
| 23 | 29 |
| 24 // 2 quick flashes, then a series of slow flashes (about 1 per second). | 30 // 2 quick flashes, then a series of slow flashes (about 1 per second). |
| 25 LED_FLASH = 1, | 31 LED_FLASH = 1, |
| 26 | 32 |
| 27 // Flash three times then hold the LED on. This is the standard way to tell | 33 // Flash three times then hold the LED on. This is the standard way to tell |
| 28 // the player which player number they are. | 34 // the player which player number they are. |
| 29 LED_FLASH_TOP_LEFT = 2, | 35 LED_FLASH_TOP_LEFT = 2, |
| 30 LED_FLASH_TOP_RIGHT = 3, | 36 LED_FLASH_TOP_RIGHT = 3, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 explicit XboxController(Delegate* delegate_); | 76 explicit XboxController(Delegate* delegate_); |
| 71 virtual ~XboxController(); | 77 virtual ~XboxController(); |
| 72 | 78 |
| 73 bool OpenDevice(io_service_t service); | 79 bool OpenDevice(io_service_t service); |
| 74 | 80 |
| 75 void SetLEDPattern(LEDPattern pattern); | 81 void SetLEDPattern(LEDPattern pattern); |
| 76 | 82 |
| 77 UInt32 location_id() { return location_id_; } | 83 UInt32 location_id() { return location_id_; } |
| 78 int GetVendorId() const; | 84 int GetVendorId() const; |
| 79 int GetProductId() const; | 85 int GetProductId() const; |
| 86 ControllerType GetControllerType() const; |
| 80 | 87 |
| 81 private: | 88 private: |
| 82 static void WriteComplete(void* context, IOReturn result, void* arg0); | 89 static void WriteComplete(void* context, IOReturn result, void* arg0); |
| 83 static void GotData(void* context, IOReturn result, void* arg0); | 90 static void GotData(void* context, IOReturn result, void* arg0); |
| 84 | 91 |
| 85 void ProcessPacket(size_t length); | 92 void ProcessXbox360Packet(size_t length); |
| 93 void ProcessXboxOnePacket(size_t length); |
| 86 void QueueRead(); | 94 void QueueRead(); |
| 87 | 95 |
| 88 void IOError(); | 96 void IOError(); |
| 89 | 97 |
| 98 void WriteXboxOneInit(); |
| 99 |
| 90 // Handle for the USB device. IOUSBDeviceStruct320 is the latest version of | 100 // Handle for the USB device. IOUSBDeviceStruct320 is the latest version of |
| 91 // the device API that is supported on Mac OS 10.6. | 101 // the device API that is supported on Mac OS 10.6. |
| 92 base::mac::ScopedIOPluginInterface<struct IOUSBDeviceStruct320> device_; | 102 base::mac::ScopedIOPluginInterface<struct IOUSBDeviceStruct320> device_; |
| 93 | 103 |
| 94 // Handle for the interface on the device which sends button and analog data. | 104 // Handle for the interface on the device which sends button and analog data. |
| 95 // The other interfaces (for the ChatPad and headset) are ignored. | 105 // The other interfaces (for the ChatPad and headset) are ignored. |
| 96 base::mac::ScopedIOPluginInterface<struct IOUSBInterfaceStruct300> interface_; | 106 base::mac::ScopedIOPluginInterface<struct IOUSBInterfaceStruct300> interface_; |
| 97 | 107 |
| 98 bool device_is_open_; | 108 bool device_is_open_; |
| 99 bool interface_is_open_; | 109 bool interface_is_open_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 110 scoped_ptr<uint8[]> read_buffer_; | 120 scoped_ptr<uint8[]> read_buffer_; |
| 111 | 121 |
| 112 // The pattern that the LEDs on the device are currently displaying, or | 122 // The pattern that the LEDs on the device are currently displaying, or |
| 113 // LED_NUM_PATTERNS if unknown. | 123 // LED_NUM_PATTERNS if unknown. |
| 114 LEDPattern led_pattern_; | 124 LEDPattern led_pattern_; |
| 115 | 125 |
| 116 UInt32 location_id_; | 126 UInt32 location_id_; |
| 117 | 127 |
| 118 Delegate* delegate_; | 128 Delegate* delegate_; |
| 119 | 129 |
| 130 ControllerType controller_type_; |
| 131 int read_endpoint_; |
| 132 int control_endpoint_; |
| 133 |
| 120 DISALLOW_COPY_AND_ASSIGN(XboxController); | 134 DISALLOW_COPY_AND_ASSIGN(XboxController); |
| 121 }; | 135 }; |
| 122 | 136 |
| 123 class XboxDataFetcher : public XboxController::Delegate { | 137 class XboxDataFetcher : public XboxController::Delegate { |
| 124 public: | 138 public: |
| 125 class Delegate { | 139 class Delegate { |
| 126 public: | 140 public: |
| 127 virtual void XboxDeviceAdd(XboxController* device) = 0; | 141 virtual void XboxDeviceAdd(XboxController* device) = 0; |
| 128 virtual void XboxDeviceRemove(XboxController* device) = 0; | 142 virtual void XboxDeviceRemove(XboxController* device) = 0; |
| 129 virtual void XboxValueChanged(XboxController* device, | 143 virtual void XboxValueChanged(XboxController* device, |
| 130 const XboxController::Data& data) = 0; | 144 const XboxController::Data& data) = 0; |
| 131 }; | 145 }; |
| 132 | 146 |
| 133 explicit XboxDataFetcher(Delegate* delegate); | 147 explicit XboxDataFetcher(Delegate* delegate); |
| 134 virtual ~XboxDataFetcher(); | 148 virtual ~XboxDataFetcher(); |
| 135 | 149 |
| 136 bool RegisterForNotifications(); | 150 bool RegisterForNotifications(); |
| 151 bool RegisterForDeviceNotifications( |
| 152 int vendor_id, |
| 153 int product_id, |
| 154 base::mac::ScopedIOObject<io_iterator_t>* added_iter, |
| 155 base::mac::ScopedIOObject<io_iterator_t>* removed_iter); |
| 137 void UnregisterFromNotifications(); | 156 void UnregisterFromNotifications(); |
| 138 | 157 |
| 139 XboxController* ControllerForLocation(UInt32 location_id); | 158 XboxController* ControllerForLocation(UInt32 location_id); |
| 140 | 159 |
| 141 private: | 160 private: |
| 142 static void DeviceAdded(void* context, io_iterator_t iterator); | 161 static void DeviceAdded(void* context, io_iterator_t iterator); |
| 143 static void DeviceRemoved(void* context, io_iterator_t iterator); | 162 static void DeviceRemoved(void* context, io_iterator_t iterator); |
| 144 void AddController(XboxController* controller); | 163 void AddController(XboxController* controller); |
| 145 void RemoveController(XboxController* controller); | 164 void RemoveController(XboxController* controller); |
| 146 void RemoveControllerByLocationID(uint32 id); | 165 void RemoveControllerByLocationID(uint32 id); |
| 147 virtual void XboxControllerGotData(XboxController* controller, | 166 virtual void XboxControllerGotData(XboxController* controller, |
| 148 const XboxController::Data& data) OVERRIDE; | 167 const XboxController::Data& data) OVERRIDE; |
| 149 virtual void XboxControllerError(XboxController* controller) OVERRIDE; | 168 virtual void XboxControllerError(XboxController* controller) OVERRIDE; |
| 150 | 169 |
| 151 Delegate* delegate_; | 170 Delegate* delegate_; |
| 152 | 171 |
| 153 std::set<XboxController*> controllers_; | 172 std::set<XboxController*> controllers_; |
| 154 | 173 |
| 155 bool listening_; | 174 bool listening_; |
| 156 | 175 |
| 157 // port_ owns source_, so this doesn't need to be a ScopedCFTypeRef, but we | 176 // port_ owns source_, so this doesn't need to be a ScopedCFTypeRef, but we |
| 158 // do need to maintain a reference to it so we can invalidate it. | 177 // do need to maintain a reference to it so we can invalidate it. |
| 159 CFRunLoopSourceRef source_; | 178 CFRunLoopSourceRef source_; |
| 160 IONotificationPortRef port_; | 179 IONotificationPortRef port_; |
| 161 base::mac::ScopedIOObject<io_iterator_t> device_added_iter_; | 180 base::mac::ScopedIOObject<io_iterator_t> xbox_360_device_added_iter_; |
| 162 base::mac::ScopedIOObject<io_iterator_t> device_removed_iter_; | 181 base::mac::ScopedIOObject<io_iterator_t> xbox_360_device_removed_iter_; |
| 182 base::mac::ScopedIOObject<io_iterator_t> xbox_one_device_added_iter_; |
| 183 base::mac::ScopedIOObject<io_iterator_t> xbox_one_device_removed_iter_; |
| 163 | 184 |
| 164 DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher); | 185 DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher); |
| 165 }; | 186 }; |
| 166 | 187 |
| 167 #endif // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | 188 #endif // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ |
| OLD | NEW |