Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: device/gamepad/gamepad_provider.h

Issue 2129003002: Refactored gamepad polling to support dynamic sources (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed unit test issue and Mac XBoxDataFetcher constructor Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ 5 #ifndef DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_
6 #define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ 6 #define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_
7 7
8 #include <stdint.h>
9
8 #include <memory> 10 #include <memory>
9 #include <utility> 11 #include <utility>
10 #include <vector> 12 #include <vector>
11 13
12 #include "base/callback_forward.h" 14 #include "base/callback_forward.h"
13 #include "base/macros.h" 15 #include "base/macros.h"
14 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory.h" 17 #include "base/memory/shared_memory.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
18 #include "base/system_monitor/system_monitor.h" 19 #include "base/system_monitor/system_monitor.h"
19 #include "device/gamepad/gamepad_export.h" 20 #include "device/gamepad/gamepad_export.h"
20 #include "device/gamepad/gamepad_shared_buffer.h" 21 #include "device/gamepad/gamepad_shared_buffer.h"
22 #include "device/gamepad/gamepad_standard_mappings.h"
21 #include "third_party/WebKit/public/platform/WebGamepads.h" 23 #include "third_party/WebKit/public/platform/WebGamepads.h"
22 24
23 namespace base { 25 namespace base {
24 class SingleThreadTaskRunner; 26 class SingleThreadTaskRunner;
25 class Thread; 27 class Thread;
26 } 28 }
27 29
28 namespace device { 30 namespace device {
29 31
30 class GamepadDataFetcher; 32 class GamepadDataFetcher;
31 33
34 enum GamepadSource {
35 GAMEPAD_SOURCE_NONE = 0,
36 GAMEPAD_SOURCE_ANDROID,
37 GAMEPAD_SOURCE_LINUX_UDEV,
38 GAMEPAD_SOURCE_MAC_HID,
39 GAMEPAD_SOURCE_MAC_XBOX,
40 GAMEPAD_SOURCE_TEST,
41 GAMEPAD_SOURCE_WIN_XINPUT,
42 GAMEPAD_SOURCE_WIN_RAW,
43 };
44
45 enum GamepadActiveState {
46 GAMEPAD_INACTIVE = 0,
47 GAMEPAD_ACTIVE,
48 GAMEPAD_NEWLY_ACTIVE,
49 };
50
51 struct PadState {
52 // Which data fetcher provided this gamepad's data.
53 GamepadSource source;
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 DEVICE_GAMEPAD_EXPORT GamepadConnectionChangeClient { 85 class DEVICE_GAMEPAD_EXPORT GamepadConnectionChangeClient {
33 public: 86 public:
34 virtual void OnGamepadConnectionChange(bool connected, 87 virtual void OnGamepadConnectionChange(bool connected,
35 int index, 88 int index,
36 const blink::WebGamepad& pad) = 0; 89 const blink::WebGamepad& pad) = 0;
37 }; 90 };
38 91
39 class DEVICE_GAMEPAD_EXPORT GamepadProvider 92 class DEVICE_GAMEPAD_EXPORT GamepadProvider
40 : public base::SystemMonitor::DevicesChangedObserver { 93 : public base::SystemMonitor::DevicesChangedObserver {
41 public: 94 public:
42 explicit GamepadProvider( 95 explicit GamepadProvider(
43 std::unique_ptr<GamepadSharedBuffer> buffer, 96 std::unique_ptr<GamepadSharedBuffer> buffer,
44 GamepadConnectionChangeClient* connection_change_client); 97 GamepadConnectionChangeClient* connection_change_client);
45 98
46 // Manually specifies the data fetcher. Used for testing. 99 // Manually specifies the data fetcher. Used for testing.
47 explicit GamepadProvider( 100 explicit GamepadProvider(
48 std::unique_ptr<GamepadSharedBuffer> buffer, 101 std::unique_ptr<GamepadSharedBuffer> buffer,
49 GamepadConnectionChangeClient* connection_change_client, 102 GamepadConnectionChangeClient* connection_change_client,
50 std::unique_ptr<GamepadDataFetcher> fetcher); 103 std::unique_ptr<GamepadDataFetcher> fetcher);
51 104
52 ~GamepadProvider() override; 105 ~GamepadProvider() override;
53 106
54 // Returns the shared memory handle of the gamepad data duplicated into the 107 // Returns the shared memory handle of the gamepad data duplicated into the
55 // given process. 108 // given process.
56 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( 109 base::SharedMemoryHandle GetSharedMemoryHandleForProcess(
57 base::ProcessHandle renderer_process); 110 base::ProcessHandle renderer_process);
58 111
112 void AddGamepadDataFetcher(GamepadDataFetcher* fetcher);
113 void RemoveGamepadDataFetcher(GamepadDataFetcher* fetcher);
114
59 void GetCurrentGamepadData(blink::WebGamepads* data); 115 void GetCurrentGamepadData(blink::WebGamepads* data);
60 116
61 // Pause and resume the background polling thread. Can be called from any 117 // Pause and resume the background polling thread. Can be called from any
62 // thread. 118 // thread.
63 void Pause(); 119 void Pause();
64 void Resume(); 120 void Resume();
65 121
66 // Registers the given closure for calling when the user has interacted with 122 // Registers the given closure for calling when the user has interacted with
67 // the device. This callback will only be issued once. 123 // the device. This callback will only be issued once.
68 void RegisterForUserGesture(const base::Closure& closure); 124 void RegisterForUserGesture(const base::Closure& closure);
69 125
70 // base::SystemMonitor::DevicesChangedObserver implementation. 126 // base::SystemMonitor::DevicesChangedObserver implementation.
71 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override; 127 void OnDevicesChanged(base::SystemMonitor::DeviceType type) override;
72 128
129 // Add a gamepad data fetcher. Takes ownership of |fetcher|.
130 void AddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher);
131
132 // Remove gamepad data fetchers with the given source.
133 void RemoveSourceGamepadDataFetcher(GamepadSource source);
134
135 // Gets a PadState object for the given source and id. If the device hasn't
136 // been encountered before one of the remaining slots will be reserved for it.
137 // If no slots are available will return NULL.
138 PadState* GetPadState(GamepadSource source, int source_id);
139
140 void SetSanitizationEnabled(bool sanitize) { sanitize_ = sanitize; }
141
73 private: 142 private:
74 void Initialize(std::unique_ptr<GamepadDataFetcher> fetcher); 143 void Initialize(std::unique_ptr<GamepadDataFetcher> fetcher);
75 144
76 // Method for setting up the platform-specific data fetcher. Takes ownership 145 // Method for setting up the platform-specific data fetcher. Takes ownership
77 // of |fetcher|. 146 // of |fetcher|.
78 void DoInitializePollingThread(std::unique_ptr<GamepadDataFetcher> fetcher); 147 void DoAddGamepadDataFetcher(std::unique_ptr<GamepadDataFetcher> fetcher);
148 void DoRemoveSourceGamepadDataFetcher(GamepadSource source);
79 149
80 // Method for sending pause hints to the low-level data fetcher. Runs on 150 // Method for sending pause hints to the low-level data fetcher. Runs on
81 // polling_thread_. 151 // polling_thread_.
82 void SendPauseHint(bool paused); 152 void SendPauseHint(bool paused);
83 153
84 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. 154 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_.
85 void DoPoll(); 155 void DoPoll();
86 void ScheduleDoPoll(); 156 void ScheduleDoPoll();
87 157
88 void OnGamepadConnectionChange(bool connected, 158 void OnGamepadConnectionChange(bool connected,
89 int index, 159 int index,
90 const blink::WebGamepad& pad); 160 const blink::WebGamepad& pad);
91 161
92 // Checks the gamepad state to see if the user has interacted with it. 162 // Checks the gamepad state to see if the user has interacted with it.
93 void CheckForUserGesture(); 163 void CheckForUserGesture();
164 void ClearPadState(PadState& state);
165
166 void MapAndSanitizeGamepadData(PadState* pad_state, blink::WebGamepad* pad);
94 167
95 enum { kDesiredSamplingIntervalMs = 16 }; 168 enum { kDesiredSamplingIntervalMs = 16 };
96 169
97 // Keeps track of when the background thread is paused. Access to is_paused_ 170 // Keeps track of when the background thread is paused. Access to is_paused_
98 // must be guarded by is_paused_lock_. 171 // must be guarded by is_paused_lock_.
99 base::Lock is_paused_lock_; 172 base::Lock is_paused_lock_;
100 bool is_paused_; 173 bool is_paused_;
101 174
102 // Keep track of when a polling task is schedlued, so as to prevent us from 175 // Keep track of when a polling task is schedlued, so as to prevent us from
103 // accidentally scheduling more than one at any time, when rapidly toggling 176 // accidentally scheduling more than one at any time, when rapidly toggling
(...skipping 20 matching lines...) Expand all
124 197
125 // Updated based on notification from SystemMonitor when the system devices 198 // Updated based on notification from SystemMonitor when the system devices
126 // have been updated, and this notification is passed on to the data fetcher 199 // have been updated, and this notification is passed on to the data fetcher
127 // to enable it to avoid redundant (and possibly expensive) is-connected 200 // to enable it to avoid redundant (and possibly expensive) is-connected
128 // tests. Access to devices_changed_ must be guarded by 201 // tests. Access to devices_changed_ must be guarded by
129 // devices_changed_lock_. 202 // devices_changed_lock_.
130 base::Lock devices_changed_lock_; 203 base::Lock devices_changed_lock_;
131 bool devices_changed_; 204 bool devices_changed_;
132 205
133 bool ever_had_user_gesture_; 206 bool ever_had_user_gesture_;
207 bool sanitize_;
134 208
135 class PadState { 209 // Tracks the state of each gamepad slot.
136 public:
137 PadState() { SetDisconnected(); }
138
139 bool Match(const blink::WebGamepad& pad) const;
140 void SetPad(const blink::WebGamepad& pad);
141 void SetDisconnected();
142 void AsWebGamepad(blink::WebGamepad* pad);
143
144 bool connected() const { return connected_; }
145
146 private:
147 bool connected_;
148 unsigned axes_length_;
149 unsigned buttons_length_;
150 blink::WebUChar id_[blink::WebGamepad::idLengthCap];
151 blink::WebUChar mapping_[blink::WebGamepad::mappingLengthCap];
152 };
153
154 // Used to detect connections and disconnections.
155 std::unique_ptr<PadState[]> pad_states_; 210 std::unique_ptr<PadState[]> pad_states_;
156 211
157 // Only used on the polling thread. 212 // Only used on the polling thread.
158 std::unique_ptr<GamepadDataFetcher> data_fetcher_; 213 typedef std::vector<std::unique_ptr<GamepadDataFetcher>> GamepadFetcherVector;
214 GamepadFetcherVector data_fetchers_;
159 215
160 base::Lock shared_memory_lock_; 216 base::Lock shared_memory_lock_;
161 std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_; 217 std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_;
162 218
163 // Polling is done on this background thread. 219 // Polling is done on this background thread.
164 std::unique_ptr<base::Thread> polling_thread_; 220 std::unique_ptr<base::Thread> polling_thread_;
165 221
166 GamepadConnectionChangeClient* connection_change_client_; 222 GamepadConnectionChangeClient* connection_change_client_;
167 223
168 static GamepadProvider* instance_;
169
170 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); 224 DISALLOW_COPY_AND_ASSIGN(GamepadProvider);
171 }; 225 };
172 226
173 } // namespace device 227 } // namespace device
174 228
175 #endif // DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ 229 #endif // DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698