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