| 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 <IOKit/IOKitLib.h> | 
 |    9 #include <CoreFoundation/CoreFoundation.h> | 
 |   10  | 
 |   11 #include <set> | 
 |   12  | 
 |   13 #include "base/basictypes.h" | 
 |   14 #include "base/memory/scoped_ptr.h" | 
 |   15  | 
 |   16 class XboxController { | 
 |   17  public: | 
 |   18   typedef enum { | 
 |   19     STATUS_MESSAGE_BUTTONS = 0, | 
 |   20     STATUS_MESSAGE_LED = 1, | 
 |   21     STATUS_MESSAGE_UNKNOWN = 2, | 
 |   22  | 
 |   23     // Apparently this message tells you if the rumble pack is disabled in the | 
 |   24     // controller. If the rumble pack is disabled, vibration control messages | 
 |   25     // have no effect. | 
 |   26     STATUS_MESSAGE_RUMBLE = 3, | 
 |   27   } StatusMessageType; | 
 |   28  | 
 |   29   // The LEDs are numbered top left, top right, bottom left, bottom right. | 
 |   30   typedef enum { | 
 |   31     LED_OFF = 0, | 
 |   32  | 
 |   33     // 2 quick flashes, then a series of slow flashes (about 1 per second). | 
 |   34     LED_FLASH = 1, | 
 |   35  | 
 |   36     // Flash three times then hold the LED on. This is the standard way to tell | 
 |   37     // the player which player number they are. | 
 |   38     LED_FLASH_1 = 2, | 
 |   39     LED_FLASH_2 = 3, | 
 |   40     LED_FLASH_3 = 4, | 
 |   41     LED_FLASH_4 = 5, | 
 |   42  | 
 |   43     // Simply turn on the numbered LED and turn all other LEDs off. | 
 |   44     LED_HOLD_1 = 6, | 
 |   45     LED_HOLD_2 = 7, | 
 |   46     LED_HOLD_3 = 8, | 
 |   47     LED_HOLD_4 = 9, | 
 |   48  | 
 |   49     LED_ROTATE = 10, | 
 |   50  | 
 |   51     LED_FLASH_FAST = 11, | 
 |   52     LED_FLASH_SLOW = 12, // Flash about once per 3 seconds | 
 |   53  | 
 |   54     // Flash alternating LEDs for a few seconds, then flash all LEDs about once | 
 |   55     // per second | 
 |   56     LED_ALTERNATE_PATTERN = 13, | 
 |   57  | 
 |   58     // 14 is just another boring flashing speed. | 
 |   59  | 
 |   60     // Flash all LEDs once then go black. | 
 |   61     LED_FLASH_ONCE = 15, | 
 |   62  | 
 |   63     LED_NUM_PATTERNS | 
 |   64   } LEDPattern; | 
 |   65  | 
 |   66   struct Data { | 
 |   67     float buttons[17]; | 
 |   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   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  | 
 |   87  private: | 
 |   88   static void GotData(void* context, IOReturn result, void* arg0); | 
 |   89   static void WriteComplete(void* context, IOReturn result, void* arg0); | 
 |   90  | 
 |   91   void ProcessPacket(uint32 length); | 
 |   92   void QueueRead(); | 
 |   93  | 
 |   94   void IOError(); | 
 |   95  | 
 |   96   // The device itself. Available on MacOS 10.5.4 and up. | 
 |   97   struct IOUSBDeviceStruct320** device_; | 
 |   98  | 
 |   99   // InterfaceInterface300 is available from MacOS 10.5 up. We only care about | 
 |  100   // the interface which can receive commands. The other interfaces (for | 
 |  101   // the ChatPad and headset) are ignored for now. | 
 |  102   struct IOUSBInterfaceStruct300** interface_; | 
 |  103  | 
 |  104   bool device_is_open_; | 
 |  105   bool interface_is_open_; | 
 |  106  | 
 |  107   CFRunLoopSourceRef source_; | 
 |  108  | 
 |  109   // This will be set to the max packet size reported by the interface, which | 
 |  110   // is 32 bytes. I would have expected USB to do message framing itself, but | 
 |  111   // somehow we still sometimes (rarely!) get packets off the interface which | 
 |  112   // aren't correctly framed. The 360 controller frames its packets with a 2 | 
 |  113   // byte header (type, total length) so we can reframe the packet data | 
 |  114   // ourselves. | 
 |  115   uint16 read_buffer_size_; | 
 |  116   scoped_array<uint8> read_buffer_; | 
 |  117  | 
 |  118   // The pattern that the LEDs on the device are currently displaying, or | 
 |  119   // LED_NUM_PATTERNS if unknown. | 
 |  120   LEDPattern led_pattern_; | 
 |  121  | 
 |  122   UInt32 location_id_; | 
 |  123  | 
 |  124   Delegate* delegate_; | 
 |  125  | 
 |  126   DISALLOW_COPY_AND_ASSIGN(XboxController); | 
 |  127 }; | 
 |  128  | 
 |  129 class XboxDataFetcher : public XboxController::Delegate { | 
 |  130  public: | 
 |  131   class Delegate { | 
 |  132    public: | 
 |  133     virtual void XboxDeviceAdd(XboxController* device) = 0; | 
 |  134     virtual void XboxDeviceRemove(XboxController* device) = 0; | 
 |  135     virtual void XboxValueChanged(XboxController* device, | 
 |  136                                   const XboxController::Data& data) = 0; | 
 |  137   }; | 
 |  138  | 
 |  139   XboxDataFetcher(Delegate* delegate); | 
 |  140   virtual ~XboxDataFetcher(); | 
 |  141  | 
 |  142   void RegisterForNotifications(); | 
 |  143   void UnregisterFromNotifications(); | 
 |  144  | 
 |  145  private: | 
 |  146   static void DeviceAdded(void* context, io_iterator_t iterator); | 
 |  147   static void DeviceRemoved(void* context, io_iterator_t iterator); | 
 |  148   void AddController(XboxController* controller); | 
 |  149   void RemoveController(XboxController* controller); | 
 |  150   void RemoveControllerByLocationID(uint32 id); | 
 |  151   virtual void XboxControllerGotData(XboxController* controller, | 
 |  152                                      const XboxController::Data& data) OVERRIDE; | 
 |  153   virtual void XboxControllerError(XboxController* controller) OVERRIDE; | 
 |  154  | 
 |  155   Delegate* delegate_; | 
 |  156  | 
 |  157   std::set<XboxController*> controllers_; | 
 |  158  | 
 |  159   bool listening_; | 
 |  160  | 
 |  161   IONotificationPortRef port_; | 
 |  162   CFRunLoopSourceRef source_; | 
 |  163   io_iterator_t device_added_iter_; | 
 |  164   io_iterator_t device_removed_iter_; | 
 |  165  | 
 |  166   DISALLOW_COPY_AND_ASSIGN(XboxDataFetcher); | 
 |  167 }; | 
 |  168  | 
 |  169 #endif  // CONTENT_BROWSER_GAMEPAD_XBOX_DATA_FETCHER_MAC_H_ | 
| OLD | NEW |