Home | History | Annotate | Download | only in gamepad
      1 // Copyright (c) 2012 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_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
      6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/compiler_specific.h"
     10 #include "base/mac/scoped_cftyperef.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "build/build_config.h"
     13 #include "content/browser/gamepad/gamepad_data_fetcher.h"
     14 #include "content/browser/gamepad/gamepad_standard_mappings.h"
     15 #include "content/browser/gamepad/xbox_data_fetcher_mac.h"
     16 #include "content/common/gamepad_hardware_buffer.h"
     17 #include "third_party/WebKit/public/platform/WebGamepads.h"
     18 
     19 #include <CoreFoundation/CoreFoundation.h>
     20 #include <IOKit/hid/IOHIDManager.h>
     21 
     22 #if defined(__OBJC__)
     23 @class NSArray;
     24 #else
     25 class NSArray;
     26 #endif
     27 
     28 namespace content {
     29 
     30 class GamepadPlatformDataFetcherMac : public GamepadDataFetcher,
     31                                       public XboxDataFetcher::Delegate {
     32  public:
     33   GamepadPlatformDataFetcherMac();
     34   virtual ~GamepadPlatformDataFetcherMac();
     35   virtual void GetGamepadData(WebKit::WebGamepads* pads,
     36                               bool devices_changed_hint) OVERRIDE;
     37   virtual void PauseHint(bool paused) OVERRIDE;
     38 
     39  private:
     40   bool enabled_;
     41   base::ScopedCFTypeRef<IOHIDManagerRef> hid_manager_ref_;
     42 
     43   static GamepadPlatformDataFetcherMac* InstanceFromContext(void* context);
     44   static void DeviceAddCallback(void* context,
     45                                 IOReturn result,
     46                                 void* sender,
     47                                 IOHIDDeviceRef ref);
     48   static void DeviceRemoveCallback(void* context,
     49                                    IOReturn result,
     50                                    void* sender,
     51                                    IOHIDDeviceRef ref);
     52   static void ValueChangedCallback(void* context,
     53                                    IOReturn result,
     54                                    void* sender,
     55                                    IOHIDValueRef ref);
     56 
     57   size_t GetEmptySlot();
     58   size_t GetSlotForDevice(IOHIDDeviceRef device);
     59   size_t GetSlotForXboxDevice(XboxController* device);
     60 
     61   void DeviceAdd(IOHIDDeviceRef device);
     62   void AddButtonsAndAxes(NSArray* elements, size_t slot);
     63   void DeviceRemove(IOHIDDeviceRef device);
     64   void ValueChanged(IOHIDValueRef value);
     65 
     66   virtual void XboxDeviceAdd(XboxController* device) OVERRIDE;
     67   virtual void XboxDeviceRemove(XboxController* device) OVERRIDE;
     68   virtual void XboxValueChanged(XboxController* device,
     69                                 const XboxController::Data& data) OVERRIDE;
     70 
     71   void RegisterForNotifications();
     72   void UnregisterFromNotifications();
     73 
     74   scoped_ptr<XboxDataFetcher> xbox_fetcher_;
     75 
     76   WebKit::WebGamepads data_;
     77 
     78   // Side-band data that's not passed to the consumer, but we need to maintain
     79   // to update data_.
     80   struct AssociatedData {
     81     bool is_xbox;
     82     union {
     83       struct {
     84         IOHIDDeviceRef device_ref;
     85         IOHIDElementRef button_elements[WebKit::WebGamepad::buttonsLengthCap];
     86         IOHIDElementRef axis_elements[WebKit::WebGamepad::buttonsLengthCap];
     87         CFIndex axis_minimums[WebKit::WebGamepad::axesLengthCap];
     88         CFIndex axis_maximums[WebKit::WebGamepad::axesLengthCap];
     89         // Function to map from device data to standard layout, if available.
     90         // May be null if no mapping is available.
     91         GamepadStandardMappingFunction mapper;
     92       } hid;
     93       struct {
     94         XboxController* device;
     95         UInt32 location_id;
     96       } xbox;
     97     };
     98   };
     99   AssociatedData associated_[WebKit::WebGamepads::itemsLengthCap];
    100 
    101   DISALLOW_COPY_AND_ASSIGN(GamepadPlatformDataFetcherMac);
    102 };
    103 
    104 }  // namespace content
    105 
    106 #endif  // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
    107