| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 5 #ifndef CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| 6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 #include <utility> | 8 #include <utility> |
| 10 #include <vector> | 9 #include <vector> |
| 11 | 10 |
| 12 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" | 12 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
| 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/system_monitor/system_monitor.h" | 18 #include "base/system_monitor/system_monitor.h" |
| 19 #include "content/browser/gamepad/gamepad_standard_mappings.h" | |
| 20 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 21 #include "third_party/WebKit/public/platform/WebGamepads.h" | 20 #include "third_party/WebKit/public/platform/WebGamepads.h" |
| 22 | 21 |
| 23 namespace base { | 22 namespace base { |
| 24 class SingleThreadTaskRunner; | 23 class SingleThreadTaskRunner; |
| 25 class Thread; | 24 class Thread; |
| 26 } | 25 } |
| 27 | 26 |
| 28 namespace content { | 27 namespace content { |
| 29 | 28 |
| 30 class GamepadDataFetcher; | 29 class GamepadDataFetcher; |
| 31 struct GamepadHardwareBuffer; | 30 struct GamepadHardwareBuffer; |
| 32 | 31 |
| 33 enum GamepadSource { | |
| 34 GAMEPAD_SOURCE_NONE = 0, | |
| 35 GAMEPAD_SOURCE_ANDROID, | |
| 36 GAMEPAD_SOURCE_LINUX_UDEV, | |
| 37 GAMEPAD_SOURCE_MAC_HID, | |
| 38 GAMEPAD_SOURCE_MAC_XBOX, | |
| 39 GAMEPAD_SOURCE_TEST, | |
| 40 GAMEPAD_SOURCE_WIN_XINPUT, | |
| 41 GAMEPAD_SOURCE_WIN_RAW, | |
| 42 }; | |
| 43 | |
| 44 enum GamepadActiveState { | |
| 45 GAMEPAD_INACTIVE = 0, | |
| 46 GAMEPAD_ACTIVE, | |
| 47 GAMEPAD_NEWLY_ACTIVE, | |
| 48 }; | |
| 49 | |
| 50 struct PadState { | |
| 51 // Which data fetcher provided this gamepad's data. | |
| 52 GamepadSource source; | |
| 53 | |
| 54 // Data fetcher-specific identifier for this gamepad. | |
| 55 int source_id; | |
| 56 | |
| 57 // Indicates whether or not the gamepad is actively being updated | |
| 58 GamepadActiveState active_state; | |
| 59 | |
| 60 // Gamepad data, unmapped. | |
| 61 blink::WebGamepad data; | |
| 62 | |
| 63 // Functions to map from device data to standard layout, if available. May | |
| 64 // be null if no mapping is available or needed. | |
| 65 GamepadStandardMappingFunction mapper; | |
| 66 | |
| 67 // Sanitization masks | |
| 68 // axis_mask and button_mask are bitfields that represent the reset state of | |
| 69 // each input. If a button or axis has ever reported 0 in the past the | |
| 70 // corresponding bit will be set to 1. | |
| 71 | |
| 72 // If we ever increase the max axis count this will need to be updated. | |
| 73 static_assert(blink::WebGamepad::axesLengthCap <= | |
| 74 std::numeric_limits<uint32_t>::digits, | |
| 75 "axis_mask is not large enough"); | |
| 76 uint32_t axis_mask; | |
| 77 | |
| 78 // If we ever increase the max button count this will need to be updated. | |
| 79 static_assert(blink::WebGamepad::buttonsLengthCap <= | |
| 80 std::numeric_limits<uint32_t>::digits, | |
| 81 "button_mask is not large enough"); | |
| 82 uint32_t button_mask; | |
| 83 }; | |
| 84 | |
| 85 class CONTENT_EXPORT GamepadProvider : | 32 class CONTENT_EXPORT GamepadProvider : |
| 86 public base::SystemMonitor::DevicesChangedObserver { | 33 public base::SystemMonitor::DevicesChangedObserver { |
| 87 public: | 34 public: |
| 88 GamepadProvider(); | 35 GamepadProvider(); |
| 89 | 36 |
| 90 // Manually specifies the data fetcher. Used for testing. | 37 // Manually specifies the data fetcher. Used for testing. |
| 91 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); | 38 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); |
| 92 | 39 |
| 93 ~GamepadProvider() override; | 40 ~GamepadProvider() override; |
| 94 | 41 |
| 95 // Returns the shared memory handle of the gamepad data duplicated into the | 42 // Returns the shared memory handle of the gamepad data duplicated into the |
| 96 // given process. | 43 // given process. |
| 97 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 44 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
| 98 base::ProcessHandle renderer_process); | 45 base::ProcessHandle renderer_process); |
| 99 | 46 |
| 100 void GetCurrentGamepadData(blink::WebGamepads* data); | 47 void GetCurrentGamepadData(blink::WebGamepads* data); |
| 101 | 48 |
| 102 // Pause and resume the background polling thread. Can be called from any | 49 // Pause and resume the background polling thread. Can be called from any |
| 103 // thread. | 50 // thread. |
| 104 void Pause(); | 51 void Pause(); |
| 105 void Resume(); | 52 void Resume(); |
| 106 | 53 |
| 107 // Registers the given closure for calling when the user has interacted with | 54 // Registers the given closure for calling when the user has interacted with |
| 108 // the device. This callback will only be issued once. | 55 // the device. This callback will only be issued once. |
| 109 void RegisterForUserGesture(const base::Closure& closure); | 56 void RegisterForUserGesture(const base::Closure& closure); |
| 110 | 57 |
| 111 // base::SystemMonitor::DevicesChangedObserver implementation. | 58 // base::SystemMonitor::DevicesChangedObserver implementation. |
| 112 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; | 59 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; |
| 113 | 60 |
| 114 // Add a gamepad data fetcher. Takes ownership of |fetcher|. | |
| 115 void AddGamepadDataFetcher(scoped_ptr<GamepadDataFetcher> fetcher); | |
| 116 | |
| 117 // Gets a PadState object for the given source and id. If the device hasn't | |
| 118 // been encountered before one of the remaining slots will be reserved for it. | |
| 119 // If no slots are available will return NULL. | |
| 120 PadState* GetPadState(GamepadSource source, int source_id); | |
| 121 | |
| 122 void SetSanitizationEnabled(bool sanitize) { sanitize_ = sanitize; } | |
| 123 | |
| 124 private: | 61 private: |
| 125 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); | 62 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); |
| 126 | 63 |
| 127 void DoAddGamepadDataFetcher(scoped_ptr<GamepadDataFetcher> fetcher); | 64 // Method for setting up the platform-specific data fetcher. Takes ownership |
| 65 // of |fetcher|. |
| 66 void DoInitializePollingThread(scoped_ptr<GamepadDataFetcher> fetcher); |
| 128 | 67 |
| 129 // Method for sending pause hints to the low-level data fetcher. Runs on | 68 // Method for sending pause hints to the low-level data fetcher. Runs on |
| 130 // polling_thread_. | 69 // polling_thread_. |
| 131 void SendPauseHint(bool paused); | 70 void SendPauseHint(bool paused); |
| 132 | 71 |
| 133 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. | 72 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. |
| 134 void DoPoll(); | 73 void DoPoll(); |
| 135 void ScheduleDoPoll(); | 74 void ScheduleDoPoll(); |
| 136 | 75 |
| 137 void OnGamepadConnectionChange(bool connected, | 76 void OnGamepadConnectionChange(bool connected, |
| 138 int index, | 77 int index, |
| 139 const blink::WebGamepad& pad); | 78 const blink::WebGamepad& pad); |
| 140 void DispatchGamepadConnectionChange(bool connected, | 79 void DispatchGamepadConnectionChange(bool connected, |
| 141 int index, | 80 int index, |
| 142 const blink::WebGamepad& pad); | 81 const blink::WebGamepad& pad); |
| 143 | 82 |
| 144 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); | 83 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); |
| 145 | 84 |
| 146 // Checks the gamepad state to see if the user has interacted with it. | 85 // Checks the gamepad state to see if the user has interacted with it. |
| 147 void CheckForUserGesture(); | 86 void CheckForUserGesture(); |
| 148 void ClearPadState(PadState& state); | |
| 149 | |
| 150 void MapAndSanitizeGamepadData(PadState* pad_state, | |
| 151 blink::WebGamepad* pad); | |
| 152 | 87 |
| 153 enum { kDesiredSamplingIntervalMs = 16 }; | 88 enum { kDesiredSamplingIntervalMs = 16 }; |
| 154 | 89 |
| 155 // Keeps track of when the background thread is paused. Access to is_paused_ | 90 // Keeps track of when the background thread is paused. Access to is_paused_ |
| 156 // must be guarded by is_paused_lock_. | 91 // must be guarded by is_paused_lock_. |
| 157 base::Lock is_paused_lock_; | 92 base::Lock is_paused_lock_; |
| 158 bool is_paused_; | 93 bool is_paused_; |
| 159 | 94 |
| 160 // Keep track of when a polling task is scheduled, so as to prevent us from | 95 // Keep track of when a polling task is schedlued, so as to prevent us from |
| 161 // accidentally scheduling more than one at any time, when rapidly toggling | 96 // accidentally scheduling more than one at any time, when rapidly toggling |
| 162 // |is_paused_|. | 97 // |is_paused_|. |
| 163 bool have_scheduled_do_poll_; | 98 bool have_scheduled_do_poll_; |
| 164 | 99 |
| 165 // Lists all observers registered for user gestures, and the thread which | 100 // Lists all observers registered for user gestures, and the thread which |
| 166 // to issue the callbacks on. Since we always issue the callback on the | 101 // to issue the callbacks on. Since we always issue the callback on the |
| 167 // thread which the registration happened, and this class lives on the I/O | 102 // thread which the registration happened, and this class lives on the I/O |
| 168 // thread, the message loop proxies will normally just be the I/O thread. | 103 // thread, the message loop proxies will normally just be the I/O thread. |
| 169 // However, this will be the main thread for unit testing. | 104 // However, this will be the main thread for unit testing. |
| 170 base::Lock user_gesture_lock_; | 105 base::Lock user_gesture_lock_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 181 | 116 |
| 182 // Updated based on notification from SystemMonitor when the system devices | 117 // Updated based on notification from SystemMonitor when the system devices |
| 183 // have been updated, and this notification is passed on to the data fetcher | 118 // have been updated, and this notification is passed on to the data fetcher |
| 184 // to enable it to avoid redundant (and possibly expensive) is-connected | 119 // to enable it to avoid redundant (and possibly expensive) is-connected |
| 185 // tests. Access to devices_changed_ must be guarded by | 120 // tests. Access to devices_changed_ must be guarded by |
| 186 // devices_changed_lock_. | 121 // devices_changed_lock_. |
| 187 base::Lock devices_changed_lock_; | 122 base::Lock devices_changed_lock_; |
| 188 bool devices_changed_; | 123 bool devices_changed_; |
| 189 | 124 |
| 190 bool ever_had_user_gesture_; | 125 bool ever_had_user_gesture_; |
| 191 bool sanitize_; | |
| 192 | 126 |
| 193 // Tracks the state of each gamepad slot. | 127 class PadState { |
| 128 public: |
| 129 PadState() { |
| 130 SetDisconnected(); |
| 131 } |
| 132 |
| 133 bool Match(const blink::WebGamepad& pad) const; |
| 134 void SetPad(const blink::WebGamepad& pad); |
| 135 void SetDisconnected(); |
| 136 void AsWebGamepad(blink::WebGamepad* pad); |
| 137 |
| 138 bool connected() const { return connected_; } |
| 139 |
| 140 private: |
| 141 bool connected_; |
| 142 unsigned axes_length_; |
| 143 unsigned buttons_length_; |
| 144 blink::WebUChar id_[blink::WebGamepad::idLengthCap]; |
| 145 blink::WebUChar mapping_[blink::WebGamepad::mappingLengthCap]; |
| 146 }; |
| 147 |
| 148 // Used to detect connections and disconnections. |
| 194 scoped_ptr<PadState[]> pad_states_; | 149 scoped_ptr<PadState[]> pad_states_; |
| 195 | 150 |
| 196 // Only used on the polling thread. | 151 // Only used on the polling thread. |
| 197 typedef std::vector<scoped_ptr<GamepadDataFetcher>> GamepadFetcherVector; | 152 scoped_ptr<GamepadDataFetcher> data_fetcher_; |
| 198 GamepadFetcherVector data_fetchers_; | |
| 199 | 153 |
| 200 base::Lock shared_memory_lock_; | 154 base::Lock shared_memory_lock_; |
| 201 base::SharedMemory gamepad_shared_memory_; | 155 base::SharedMemory gamepad_shared_memory_; |
| 202 | 156 |
| 203 // Polling is done on this background thread. | 157 // Polling is done on this background thread. |
| 204 scoped_ptr<base::Thread> polling_thread_; | 158 scoped_ptr<base::Thread> polling_thread_; |
| 205 | 159 |
| 160 static GamepadProvider* instance_; |
| 161 |
| 206 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); | 162 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); |
| 207 }; | 163 }; |
| 208 | 164 |
| 209 } // namespace content | 165 } // namespace content |
| 210 | 166 |
| 211 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 167 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
| OLD | NEW |