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> |
8 #include <utility> | 9 #include <utility> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/shared_memory.h" | 16 #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" |
19 #include "content/common/content_export.h" | 20 #include "content/common/content_export.h" |
20 #include "third_party/WebKit/public/platform/WebGamepads.h" | 21 #include "third_party/WebKit/public/platform/WebGamepads.h" |
21 | 22 |
22 namespace base { | 23 namespace base { |
23 class SingleThreadTaskRunner; | 24 class SingleThreadTaskRunner; |
24 class Thread; | 25 class Thread; |
25 } | 26 } |
26 | 27 |
27 namespace content { | 28 namespace content { |
28 | 29 |
29 class GamepadDataFetcher; | 30 class GamepadDataFetcher; |
30 struct GamepadHardwareBuffer; | 31 struct GamepadHardwareBuffer; |
31 | 32 |
| 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 |
32 class CONTENT_EXPORT GamepadProvider : | 85 class CONTENT_EXPORT GamepadProvider : |
33 public base::SystemMonitor::DevicesChangedObserver { | 86 public base::SystemMonitor::DevicesChangedObserver { |
34 public: | 87 public: |
35 GamepadProvider(); | 88 GamepadProvider(); |
36 | 89 |
37 // Manually specifies the data fetcher. Used for testing. | 90 // Manually specifies the data fetcher. Used for testing. |
38 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); | 91 explicit GamepadProvider(scoped_ptr<GamepadDataFetcher> fetcher); |
39 | 92 |
40 ~GamepadProvider() override; | 93 ~GamepadProvider() override; |
41 | 94 |
42 // Returns the shared memory handle of the gamepad data duplicated into the | 95 // Returns the shared memory handle of the gamepad data duplicated into the |
43 // given process. | 96 // given process. |
44 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 97 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
45 base::ProcessHandle renderer_process); | 98 base::ProcessHandle renderer_process); |
46 | 99 |
47 void GetCurrentGamepadData(blink::WebGamepads* data); | 100 void GetCurrentGamepadData(blink::WebGamepads* data); |
48 | 101 |
49 // Pause and resume the background polling thread. Can be called from any | 102 // Pause and resume the background polling thread. Can be called from any |
50 // thread. | 103 // thread. |
51 void Pause(); | 104 void Pause(); |
52 void Resume(); | 105 void Resume(); |
53 | 106 |
54 // Registers the given closure for calling when the user has interacted with | 107 // Registers the given closure for calling when the user has interacted with |
55 // the device. This callback will only be issued once. | 108 // the device. This callback will only be issued once. |
56 void RegisterForUserGesture(const base::Closure& closure); | 109 void RegisterForUserGesture(const base::Closure& closure); |
57 | 110 |
58 // base::SystemMonitor::DevicesChangedObserver implementation. | 111 // base::SystemMonitor::DevicesChangedObserver implementation. |
59 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; | 112 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; |
60 | 113 |
| 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 |
61 private: | 124 private: |
62 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); | 125 void Initialize(scoped_ptr<GamepadDataFetcher> fetcher); |
63 | 126 |
64 // Method for setting up the platform-specific data fetcher. Takes ownership | 127 void DoAddGamepadDataFetcher(scoped_ptr<GamepadDataFetcher> fetcher); |
65 // of |fetcher|. | |
66 void DoInitializePollingThread(scoped_ptr<GamepadDataFetcher> fetcher); | |
67 | 128 |
68 // Method for sending pause hints to the low-level data fetcher. Runs on | 129 // Method for sending pause hints to the low-level data fetcher. Runs on |
69 // polling_thread_. | 130 // polling_thread_. |
70 void SendPauseHint(bool paused); | 131 void SendPauseHint(bool paused); |
71 | 132 |
72 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. | 133 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. |
73 void DoPoll(); | 134 void DoPoll(); |
74 void ScheduleDoPoll(); | 135 void ScheduleDoPoll(); |
75 | 136 |
76 void OnGamepadConnectionChange(bool connected, | 137 void OnGamepadConnectionChange(bool connected, |
77 int index, | 138 int index, |
78 const blink::WebGamepad& pad); | 139 const blink::WebGamepad& pad); |
79 void DispatchGamepadConnectionChange(bool connected, | 140 void DispatchGamepadConnectionChange(bool connected, |
80 int index, | 141 int index, |
81 const blink::WebGamepad& pad); | 142 const blink::WebGamepad& pad); |
82 | 143 |
83 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); | 144 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); |
84 | 145 |
85 // Checks the gamepad state to see if the user has interacted with it. | 146 // Checks the gamepad state to see if the user has interacted with it. |
86 void CheckForUserGesture(); | 147 void CheckForUserGesture(); |
| 148 void ClearPadState(PadState& state); |
| 149 |
| 150 void MapAndSanitizeGamepadData(PadState* pad_state, |
| 151 blink::WebGamepad* pad); |
87 | 152 |
88 enum { kDesiredSamplingIntervalMs = 16 }; | 153 enum { kDesiredSamplingIntervalMs = 16 }; |
89 | 154 |
90 // Keeps track of when the background thread is paused. Access to is_paused_ | 155 // Keeps track of when the background thread is paused. Access to is_paused_ |
91 // must be guarded by is_paused_lock_. | 156 // must be guarded by is_paused_lock_. |
92 base::Lock is_paused_lock_; | 157 base::Lock is_paused_lock_; |
93 bool is_paused_; | 158 bool is_paused_; |
94 | 159 |
95 // Keep track of when a polling task is schedlued, so as to prevent us from | 160 // Keep track of when a polling task is scheduled, so as to prevent us from |
96 // accidentally scheduling more than one at any time, when rapidly toggling | 161 // accidentally scheduling more than one at any time, when rapidly toggling |
97 // |is_paused_|. | 162 // |is_paused_|. |
98 bool have_scheduled_do_poll_; | 163 bool have_scheduled_do_poll_; |
99 | 164 |
100 // Lists all observers registered for user gestures, and the thread which | 165 // Lists all observers registered for user gestures, and the thread which |
101 // to issue the callbacks on. Since we always issue the callback on the | 166 // to issue the callbacks on. Since we always issue the callback on the |
102 // thread which the registration happened, and this class lives on the I/O | 167 // thread which the registration happened, and this class lives on the I/O |
103 // thread, the message loop proxies will normally just be the I/O thread. | 168 // thread, the message loop proxies will normally just be the I/O thread. |
104 // However, this will be the main thread for unit testing. | 169 // However, this will be the main thread for unit testing. |
105 base::Lock user_gesture_lock_; | 170 base::Lock user_gesture_lock_; |
(...skipping 10 matching lines...) Expand all Loading... |
116 | 181 |
117 // Updated based on notification from SystemMonitor when the system devices | 182 // Updated based on notification from SystemMonitor when the system devices |
118 // have been updated, and this notification is passed on to the data fetcher | 183 // have been updated, and this notification is passed on to the data fetcher |
119 // to enable it to avoid redundant (and possibly expensive) is-connected | 184 // to enable it to avoid redundant (and possibly expensive) is-connected |
120 // tests. Access to devices_changed_ must be guarded by | 185 // tests. Access to devices_changed_ must be guarded by |
121 // devices_changed_lock_. | 186 // devices_changed_lock_. |
122 base::Lock devices_changed_lock_; | 187 base::Lock devices_changed_lock_; |
123 bool devices_changed_; | 188 bool devices_changed_; |
124 | 189 |
125 bool ever_had_user_gesture_; | 190 bool ever_had_user_gesture_; |
| 191 bool sanitize_; |
126 | 192 |
127 class PadState { | 193 // Tracks the state of each gamepad slot. |
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. | |
149 scoped_ptr<PadState[]> pad_states_; | 194 scoped_ptr<PadState[]> pad_states_; |
150 | 195 |
151 // Only used on the polling thread. | 196 // Only used on the polling thread. |
152 scoped_ptr<GamepadDataFetcher> data_fetcher_; | 197 typedef std::vector<scoped_ptr<GamepadDataFetcher>> GamepadFetcherVector; |
| 198 GamepadFetcherVector data_fetchers_; |
153 | 199 |
154 base::Lock shared_memory_lock_; | 200 base::Lock shared_memory_lock_; |
155 base::SharedMemory gamepad_shared_memory_; | 201 base::SharedMemory gamepad_shared_memory_; |
156 | 202 |
157 // Polling is done on this background thread. | 203 // Polling is done on this background thread. |
158 scoped_ptr<base::Thread> polling_thread_; | 204 scoped_ptr<base::Thread> polling_thread_; |
159 | 205 |
160 static GamepadProvider* instance_; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); | 206 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); |
163 }; | 207 }; |
164 | 208 |
165 } // namespace content | 209 } // namespace content |
166 | 210 |
167 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 211 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ |
OLD | NEW |