Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(589)

Unified Diff: content/browser/gamepad/xbox_data_fetcher_mac.h

Issue 14328036: Implement support for USB Xbox360 controllers without a driver on Mac. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/gamepad/xbox_data_fetcher_mac.h
diff --git a/content/browser/gamepad/xbox_data_fetcher_mac.h b/content/browser/gamepad/xbox_data_fetcher_mac.h
new file mode 100644
index 0000000000000000000000000000000000000000..cb7fb5d3a21aae02c0188da27e059ec4c8ce38aa
--- /dev/null
+++ b/content/browser/gamepad/xbox_data_fetcher_mac.h
@@ -0,0 +1,169 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_
+#define CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_
+
+#include <IOKit/IOKitLib.h>
+#include <CoreFoundation/CoreFoundation.h>
+
+#include <set>
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+
+class XboxController {
+ public:
+ typedef enum {
+ STATUS_MESSAGE_BUTTONS = 0,
+ STATUS_MESSAGE_LED = 1,
+ STATUS_MESSAGE_UNKNOWN = 2,
+
+ // Apparently this message tells you if the rumble pack is disabled in the
+ // controller. If the rumble pack is disabled, vibration control messages
+ // have no effect.
+ STATUS_MESSAGE_RUMBLE = 3,
+ } StatusMessageType;
+
+ // The LEDs are numbered top left, top right, bottom left, bottom right.
+ typedef enum {
+ LED_OFF = 0,
+
+ // 2 quick flashes, then a series of slow flashes (about 1 per second).
+ LED_FLASH = 1,
+
+ // Flash three times then hold the LED on. This is the standard way to tell
+ // the player which player number they are.
+ LED_FLASH_1 = 2,
+ LED_FLASH_2 = 3,
+ LED_FLASH_3 = 4,
+ LED_FLASH_4 = 5,
+
+ // Simply turn on the numbered LED and turn all other LEDs off.
+ LED_HOLD_1 = 6,
+ LED_HOLD_2 = 7,
+ LED_HOLD_3 = 8,
+ LED_HOLD_4 = 9,
+
+ LED_ROTATE = 10,
+
+ LED_FLASH_FAST = 11,
+ LED_FLASH_SLOW = 12, // Flash about once per 3 seconds
+
+ // Flash alternating LEDs for a few seconds, then flash all LEDs about once
+ // per second
+ LED_ALTERNATE_PATTERN = 13,
+
+ // 14 is just another boring flashing speed.
+
+ // Flash all LEDs once then go black.
+ LED_FLASH_ONCE = 15,
+
+ LED_NUM_PATTERNS
+ } LEDPattern;
+
+ struct Data {
+ float buttons[17];
+ float axes[4];
+ };
+
+ class Delegate {
+ public:
+ virtual void XboxControllerGotData(XboxController* controller,
+ const Data& data) = 0;
+ virtual void XboxControllerError(XboxController* controller) = 0;
+ };
+
+ XboxController(Delegate* delegate_);
+ virtual ~XboxController();
+
+ bool OpenDevice(io_service_t service);
+
+ void SetLEDPattern(LEDPattern pattern);
+
+ UInt32 location_id() { return location_id_; }
+
+ private:
+ static void GotData(void* context, IOReturn result, void* arg0);
+ static void WriteComplete(void* context, IOReturn result, void* arg0);
+
+ void ProcessPacket(uint32 length);
+ void QueueRead();
+
+ void IOError();
+
+ // The device itself. Available on MacOS 10.5.4 and up.
+ struct IOUSBDeviceStruct320** device_;
+
+ // InterfaceInterface300 is available from MacOS 10.5 up. We only care about
+ // the interface which can receive commands. The other interfaces (for
+ // the ChatPad and headset) are ignored for now.
+ struct IOUSBInterfaceStruct300** interface_;
+
+ bool device_is_open_;
+ bool interface_is_open_;
+
+ CFRunLoopSourceRef source_;
+
+ // This will be set to the max packet size reported by the interface, which
+ // is 32 bytes. I would have expected USB to do message framing itself, but
+ // somehow we still sometimes (rarely!) get packets off the interface which
+ // aren't correctly framed. The 360 controller frames its packets with a 2
+ // byte header (type, total length) so we can reframe the packet data
+ // ourselves.
+ uint16 read_buffer_size_;
+ scoped_array<uint8> read_buffer_;
+
+ // The pattern that the LEDs on the device are currently displaying, or
+ // LED_NUM_PATTERNS if unknown.
+ LEDPattern led_pattern_;
+
+ UInt32 location_id_;
+
+ Delegate* delegate_;
+
+ DISALLOW_COPY_AND_ASSIGN(XboxController);
+};
+
+class XboxDataFetcher : public XboxController::Delegate {
+ public:
+ class Delegate {
+ public:
+ virtual void XboxDeviceAdd(XboxController* device) = 0;
+ virtual void XboxDeviceRemove(XboxController* device) = 0;
+ virtual void XboxValueChanged(XboxController* device,
+ const XboxController::Data& data) = 0;
+ };
+
+ XboxDataFetcher(Delegate* delegate);
+ virtual ~XboxDataFetcher();
+
+ void RegisterForNotifications();
+ void UnregisterFromNotifications();
+
+ private:
+ static void DeviceAdded(void* context, io_iterator_t iterator);
+ static void DeviceRemoved(void* context, io_iterator_t iterator);
+ void AddController(XboxController* controller);
+ void RemoveController(XboxController* controller);
+ void RemoveControllerByLocationID(uint32 id);
+ virtual void XboxControllerGotData(XboxController* controller,
+ const XboxController::Data& data) OVERRIDE;
+ virtual void XboxControllerError(XboxController* controller) OVERRIDE;
+
+ Delegate* delegate_;
+
+ std::set<XboxController*> controllers_;
+
+ bool listening_;
+
+ IONotificationPortRef port_;
+ CFRunLoopSourceRef source_;
+ io_iterator_t device_added_iter_;
+ io_iterator_t device_removed_iter_;
+
+ DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher);
+};
+
+#endif // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_

Powered by Google App Engine
This is Rietveld 408576698