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