Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / gamepad / gamepad_platform_data_fetcher_mac.h
blob15de8d1cc4be0d6533717325750ab15dcd217a88
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.
5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_
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"
19 #include <CoreFoundation/CoreFoundation.h>
20 #include <IOKit/hid/IOHIDManager.h>
22 #if defined(__OBJC__)
23 @class NSArray;
24 #else
25 class NSArray;
26 #endif
28 namespace content {
30 class GamepadPlatformDataFetcherMac : public GamepadDataFetcher,
31 public XboxDataFetcher::Delegate {
32 public:
33 GamepadPlatformDataFetcherMac();
34 ~GamepadPlatformDataFetcherMac() override;
35 void GetGamepadData(blink::WebGamepads* pads,
36 bool devices_changed_hint) override;
37 void PauseHint(bool paused) override;
39 private:
40 bool enabled_;
41 bool paused_;
42 base::ScopedCFTypeRef<IOHIDManagerRef> hid_manager_ref_;
44 static GamepadPlatformDataFetcherMac* InstanceFromContext(void* context);
45 static void DeviceAddCallback(void* context,
46 IOReturn result,
47 void* sender,
48 IOHIDDeviceRef ref);
49 static void DeviceRemoveCallback(void* context,
50 IOReturn result,
51 void* sender,
52 IOHIDDeviceRef ref);
53 static void ValueChangedCallback(void* context,
54 IOReturn result,
55 void* sender,
56 IOHIDValueRef ref);
58 size_t GetEmptySlot();
59 size_t GetSlotForDevice(IOHIDDeviceRef device);
60 size_t GetSlotForXboxDevice(XboxController* device);
62 void DeviceAdd(IOHIDDeviceRef device);
63 bool AddButtonsAndAxes(NSArray* elements, size_t slot);
64 void DeviceRemove(IOHIDDeviceRef device);
65 void ValueChanged(IOHIDValueRef value);
67 void XboxDeviceAdd(XboxController* device) override;
68 void XboxDeviceRemove(XboxController* device) override;
69 void XboxValueChanged(XboxController* device,
70 const XboxController::Data& data) override;
72 void RegisterForNotifications();
73 void UnregisterFromNotifications();
75 scoped_ptr<XboxDataFetcher> xbox_fetcher_;
77 blink::WebGamepads data_;
79 // Side-band data that's not passed to the consumer, but we need to maintain
80 // to update data_.
81 struct AssociatedData {
82 bool is_xbox;
83 union {
84 struct {
85 IOHIDDeviceRef device_ref;
86 IOHIDElementRef button_elements[blink::WebGamepad::buttonsLengthCap];
87 IOHIDElementRef axis_elements[blink::WebGamepad::axesLengthCap];
88 CFIndex axis_minimums[blink::WebGamepad::axesLengthCap];
89 CFIndex axis_maximums[blink::WebGamepad::axesLengthCap];
90 // Function to map from device data to standard layout, if available.
91 // May be null if no mapping is available.
92 GamepadStandardMappingFunction mapper;
93 } hid;
94 struct {
95 XboxController* device;
96 UInt32 location_id;
97 } xbox;
100 AssociatedData associated_[blink::WebGamepads::itemsLengthCap];
102 DISALLOW_COPY_AND_ASSIGN(GamepadPlatformDataFetcherMac);
105 } // namespace content
107 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PLATFORM_DATA_FETCHER_MAC_H_