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 DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ |
6 #define CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 6 #define DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/shared_memory.h" | 15 #include "base/memory/shared_memory.h" |
16 #include "base/memory/weak_ptr.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/common/content_export.h" | 19 #include "device/gamepad/gamepad_export.h" |
| 20 #include "device/gamepad/gamepad_shared_buffer.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 device { |
28 | 29 |
29 class GamepadDataFetcher; | 30 class GamepadDataFetcher; |
30 struct GamepadHardwareBuffer; | |
31 | 31 |
32 class CONTENT_EXPORT GamepadProvider : | 32 class DEVICE_GAMEPAD_EXPORT GamepadConnectionChangeClient { |
33 public base::SystemMonitor::DevicesChangedObserver { | |
34 public: | 33 public: |
35 GamepadProvider(); | 34 virtual void OnGamepadConnectionChange(bool connected, |
| 35 int index, |
| 36 const blink::WebGamepad& pad) = 0; |
| 37 }; |
| 38 |
| 39 class DEVICE_GAMEPAD_EXPORT GamepadProvider |
| 40 : public base::SystemMonitor::DevicesChangedObserver { |
| 41 public: |
| 42 explicit GamepadProvider( |
| 43 std::unique_ptr<GamepadSharedBuffer> buffer, |
| 44 GamepadConnectionChangeClient* connection_change_client); |
36 | 45 |
37 // Manually specifies the data fetcher. Used for testing. | 46 // Manually specifies the data fetcher. Used for testing. |
38 explicit GamepadProvider(std::unique_ptr<GamepadDataFetcher> fetcher); | 47 explicit GamepadProvider( |
| 48 std::unique_ptr<GamepadSharedBuffer> buffer, |
| 49 GamepadConnectionChangeClient* connection_change_client, |
| 50 std::unique_ptr<GamepadDataFetcher> fetcher); |
39 | 51 |
40 ~GamepadProvider() override; | 52 ~GamepadProvider() override; |
41 | 53 |
42 // Returns the shared memory handle of the gamepad data duplicated into the | 54 // Returns the shared memory handle of the gamepad data duplicated into the |
43 // given process. | 55 // given process. |
44 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( | 56 base::SharedMemoryHandle GetSharedMemoryHandleForProcess( |
45 base::ProcessHandle renderer_process); | 57 base::ProcessHandle renderer_process); |
46 | 58 |
47 void GetCurrentGamepadData(blink::WebGamepads* data); | 59 void GetCurrentGamepadData(blink::WebGamepads* data); |
48 | 60 |
(...skipping 20 matching lines...) Expand all Loading... |
69 // polling_thread_. | 81 // polling_thread_. |
70 void SendPauseHint(bool paused); | 82 void SendPauseHint(bool paused); |
71 | 83 |
72 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. | 84 // Method for polling a GamepadDataFetcher. Runs on the polling_thread_. |
73 void DoPoll(); | 85 void DoPoll(); |
74 void ScheduleDoPoll(); | 86 void ScheduleDoPoll(); |
75 | 87 |
76 void OnGamepadConnectionChange(bool connected, | 88 void OnGamepadConnectionChange(bool connected, |
77 int index, | 89 int index, |
78 const blink::WebGamepad& pad); | 90 const blink::WebGamepad& pad); |
79 void DispatchGamepadConnectionChange(bool connected, | |
80 int index, | |
81 const blink::WebGamepad& pad); | |
82 | |
83 GamepadHardwareBuffer* SharedMemoryAsHardwareBuffer(); | |
84 | 91 |
85 // Checks the gamepad state to see if the user has interacted with it. | 92 // Checks the gamepad state to see if the user has interacted with it. |
86 void CheckForUserGesture(); | 93 void CheckForUserGesture(); |
87 | 94 |
88 enum { kDesiredSamplingIntervalMs = 16 }; | 95 enum { kDesiredSamplingIntervalMs = 16 }; |
89 | 96 |
90 // Keeps track of when the background thread is paused. Access to is_paused_ | 97 // Keeps track of when the background thread is paused. Access to is_paused_ |
91 // must be guarded by is_paused_lock_. | 98 // must be guarded by is_paused_lock_. |
92 base::Lock is_paused_lock_; | 99 base::Lock is_paused_lock_; |
93 bool is_paused_; | 100 bool is_paused_; |
(...skipping 26 matching lines...) Expand all Loading... |
120 // to enable it to avoid redundant (and possibly expensive) is-connected | 127 // to enable it to avoid redundant (and possibly expensive) is-connected |
121 // tests. Access to devices_changed_ must be guarded by | 128 // tests. Access to devices_changed_ must be guarded by |
122 // devices_changed_lock_. | 129 // devices_changed_lock_. |
123 base::Lock devices_changed_lock_; | 130 base::Lock devices_changed_lock_; |
124 bool devices_changed_; | 131 bool devices_changed_; |
125 | 132 |
126 bool ever_had_user_gesture_; | 133 bool ever_had_user_gesture_; |
127 | 134 |
128 class PadState { | 135 class PadState { |
129 public: | 136 public: |
130 PadState() { | 137 PadState() { SetDisconnected(); } |
131 SetDisconnected(); | |
132 } | |
133 | 138 |
134 bool Match(const blink::WebGamepad& pad) const; | 139 bool Match(const blink::WebGamepad& pad) const; |
135 void SetPad(const blink::WebGamepad& pad); | 140 void SetPad(const blink::WebGamepad& pad); |
136 void SetDisconnected(); | 141 void SetDisconnected(); |
137 void AsWebGamepad(blink::WebGamepad* pad); | 142 void AsWebGamepad(blink::WebGamepad* pad); |
138 | 143 |
139 bool connected() const { return connected_; } | 144 bool connected() const { return connected_; } |
140 | 145 |
141 private: | 146 private: |
142 bool connected_; | 147 bool connected_; |
143 unsigned axes_length_; | 148 unsigned axes_length_; |
144 unsigned buttons_length_; | 149 unsigned buttons_length_; |
145 blink::WebUChar id_[blink::WebGamepad::idLengthCap]; | 150 blink::WebUChar id_[blink::WebGamepad::idLengthCap]; |
146 blink::WebUChar mapping_[blink::WebGamepad::mappingLengthCap]; | 151 blink::WebUChar mapping_[blink::WebGamepad::mappingLengthCap]; |
147 }; | 152 }; |
148 | 153 |
149 // Used to detect connections and disconnections. | 154 // Used to detect connections and disconnections. |
150 std::unique_ptr<PadState[]> pad_states_; | 155 std::unique_ptr<PadState[]> pad_states_; |
151 | 156 |
152 // Only used on the polling thread. | 157 // Only used on the polling thread. |
153 std::unique_ptr<GamepadDataFetcher> data_fetcher_; | 158 std::unique_ptr<GamepadDataFetcher> data_fetcher_; |
154 | 159 |
155 base::Lock shared_memory_lock_; | 160 base::Lock shared_memory_lock_; |
156 base::SharedMemory gamepad_shared_memory_; | 161 std::unique_ptr<GamepadSharedBuffer> gamepad_shared_buffer_; |
157 | 162 |
158 // Polling is done on this background thread. | 163 // Polling is done on this background thread. |
159 std::unique_ptr<base::Thread> polling_thread_; | 164 std::unique_ptr<base::Thread> polling_thread_; |
160 | 165 |
| 166 GamepadConnectionChangeClient* connection_change_client_; |
| 167 |
161 static GamepadProvider* instance_; | 168 static GamepadProvider* instance_; |
162 | 169 |
163 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); | 170 DISALLOW_COPY_AND_ASSIGN(GamepadProvider); |
164 }; | 171 }; |
165 | 172 |
166 } // namespace content | 173 } // namespace device |
167 | 174 |
168 #endif // CONTENT_BROWSER_GAMEPAD_GAMEPAD_PROVIDER_H_ | 175 #endif // DEVICE_GAMEPAD_GAMEPAD_PROVIDER_H_ |
OLD | NEW |