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.
5 #ifndef CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_
6 #define CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_
8 #include <CoreFoundation/CoreFoundation.h>
9 #include <IOKit/IOKitLib.h>
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"
19 class XboxController
{
24 // 2 quick flashes, then a series of slow flashes (about 1 per second).
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,
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,
43 LED_FLASH_SLOW
= 12, // Flash about once per 3 seconds
45 // Flash alternating LEDs for a few seconds, then flash all LEDs about once
47 LED_ALTERNATE_PATTERN
= 13,
49 // 14 is just another boring flashing speed.
51 // Flash all LEDs once then go black.
65 virtual void XboxControllerGotData(XboxController
* controller
,
66 const Data
& data
) = 0;
67 virtual void XboxControllerError(XboxController
* controller
) = 0;
70 explicit XboxController(Delegate
* delegate_
);
71 virtual ~XboxController();
73 bool OpenDevice(io_service_t service
);
75 void SetLEDPattern(LEDPattern pattern
);
77 UInt32
location_id() { return location_id_
; }
78 int GetVendorId() const;
79 int GetProductId() const;
82 static void WriteComplete(void* context
, IOReturn result
, void* arg0
);
83 static void GotData(void* context
, IOReturn result
, void* arg0
);
85 void ProcessPacket(size_t length
);
90 // Handle for the USB device. IOUSBDeviceStruct320 is the latest version of
91 // the device API that is supported on Mac OS 10.6.
92 base::mac::ScopedIOPluginInterface
<struct IOUSBDeviceStruct320
> device_
;
94 // Handle for the interface on the device which sends button and analog data.
95 // The other interfaces (for the ChatPad and headset) are ignored.
96 base::mac::ScopedIOPluginInterface
<struct IOUSBInterfaceStruct300
> interface_
;
99 bool interface_is_open_
;
101 base::ScopedCFTypeRef
<CFRunLoopSourceRef
> source_
;
103 // This will be set to the max packet size reported by the interface, which
104 // is 32 bytes. I would have expected USB to do message framing itself, but
105 // somehow we still sometimes (rarely!) get packets off the interface which
106 // aren't correctly framed. The 360 controller frames its packets with a 2
107 // byte header (type, total length) so we can reframe the packet data
109 uint16 read_buffer_size_
;
110 scoped_ptr
<uint8
[]> read_buffer_
;
112 // The pattern that the LEDs on the device are currently displaying, or
113 // LED_NUM_PATTERNS if unknown.
114 LEDPattern led_pattern_
;
120 DISALLOW_COPY_AND_ASSIGN(XboxController
);
123 class XboxDataFetcher
: public XboxController::Delegate
{
127 virtual void XboxDeviceAdd(XboxController
* device
) = 0;
128 virtual void XboxDeviceRemove(XboxController
* device
) = 0;
129 virtual void XboxValueChanged(XboxController
* device
,
130 const XboxController::Data
& data
) = 0;
133 explicit XboxDataFetcher(Delegate
* delegate
);
134 virtual ~XboxDataFetcher();
136 bool RegisterForNotifications();
137 void UnregisterFromNotifications();
139 XboxController
* ControllerForLocation(UInt32 location_id
);
142 static void DeviceAdded(void* context
, io_iterator_t iterator
);
143 static void DeviceRemoved(void* context
, io_iterator_t iterator
);
144 void AddController(XboxController
* controller
);
145 void RemoveController(XboxController
* controller
);
146 void RemoveControllerByLocationID(uint32 id
);
147 virtual void XboxControllerGotData(XboxController
* controller
,
148 const XboxController::Data
& data
) OVERRIDE
;
149 virtual void XboxControllerError(XboxController
* controller
) OVERRIDE
;
153 std::set
<XboxController
*> controllers_
;
157 // 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.
159 CFRunLoopSourceRef source_
;
160 IONotificationPortRef port_
;
161 base::mac::ScopedIOObject
<io_iterator_t
> device_added_iter_
;
162 base::mac::ScopedIOObject
<io_iterator_t
> device_removed_iter_
;
164 DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher
);
167 #endif // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_