Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Nasko Oskov | ee48dfb4 | 2024-06-08 05:13:06 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/342213636): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
danakj | 89f4708 | 2020-09-02 17:53:43 | [diff] [blame] | 10 | #include "content/web_test/renderer/gamepad_controller.h" |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 11 | |
Henrique Ferreiro | f464d9f1 | 2019-11-04 20:47:17 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <utility> |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 14 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
Oksana Zhuravlova | 1165185 | 2018-07-16 20:07:50 | [diff] [blame] | 16 | #include "content/public/renderer/render_frame.h" |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 17 | #include "gin/arguments.h" |
| 18 | #include "gin/handle.h" |
| 19 | #include "gin/object_template_builder.h" |
| 20 | #include "gin/wrappable.h" |
Alexandr Ilin | 1ce67150 | 2018-07-19 20:59:35 | [diff] [blame] | 21 | #include "mojo/public/cpp/system/platform_handle.h" |
Lukasz Anforowicz | 9bc03e7 | 2024-07-14 20:25:02 | [diff] [blame] | 22 | #include "third_party/blink/public/platform/browser_interface_broker_proxy.h" |
Dave Tapuska | d1aaf8a | 2023-09-12 14:50:34 | [diff] [blame] | 23 | #include "third_party/blink/public/platform/scheduler/web_agent_group_scheduler.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame] | 24 | #include "third_party/blink/public/web/web_local_frame.h" |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 25 | #include "v8/include/v8.h" |
| 26 | |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 27 | using device::Gamepad; |
| 28 | using device::Gamepads; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 29 | |
danakj | 741848a | 2020-04-07 22:48:06 | [diff] [blame] | 30 | namespace content { |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 31 | |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // Set button.pressed if the button value is above a threshold. The threshold is |
| 35 | // chosen to match XInput's trigger deadzone. |
| 36 | constexpr float kButtonPressedThreshold = 30.f / 255.f; |
| 37 | |
| 38 | int64_t CurrentTimeInMicroseconds() { |
| 39 | return base::TimeTicks::Now().since_origin().InMicroseconds(); |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 44 | class GamepadControllerBindings |
| 45 | : public gin::Wrappable<GamepadControllerBindings> { |
| 46 | public: |
| 47 | static gin::WrapperInfo kWrapperInfo; |
| 48 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 49 | GamepadControllerBindings(const GamepadControllerBindings&) = delete; |
| 50 | GamepadControllerBindings& operator=(const GamepadControllerBindings&) = |
| 51 | delete; |
| 52 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 53 | static void Install(base::WeakPtr<GamepadController> controller, |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 54 | blink::WebLocalFrame* frame); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | explicit GamepadControllerBindings( |
| 58 | base::WeakPtr<GamepadController> controller); |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 59 | ~GamepadControllerBindings() override; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 60 | |
| 61 | // gin::Wrappable. |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 62 | gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
anand.ratn | 449f39a4 | 2014-10-06 13:45:57 | [diff] [blame] | 63 | v8::Isolate* isolate) override; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 64 | |
| 65 | void Connect(int index); |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 66 | void DispatchConnected(int index); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 67 | void Disconnect(int index); |
| 68 | void SetId(int index, const std::string& src); |
| 69 | void SetButtonCount(int index, int buttons); |
| 70 | void SetButtonData(int index, int button, double data); |
| 71 | void SetAxisCount(int index, int axes); |
| 72 | void SetAxisData(int index, int axis, double data); |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 73 | void SetDualRumbleVibrationActuator(int index, bool enabled); |
Gabriel Brito | d5667f0 | 2022-07-05 01:29:29 | [diff] [blame] | 74 | void SetTriggerRumbleVibrationActuator(int index, bool enabled); |
Bradley Needham | 835f0f49 | 2023-04-04 23:00:54 | [diff] [blame] | 75 | void SetTouchCount(int index, int touches); |
| 76 | void SetTouchData(int index, |
| 77 | int touch, |
| 78 | unsigned int touch_id, |
| 79 | float position_x, |
| 80 | float position_y); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 81 | |
| 82 | base::WeakPtr<GamepadController> controller_; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | gin::WrapperInfo GamepadControllerBindings::kWrapperInfo = { |
| 86 | gin::kEmbedderNativeGin}; |
| 87 | |
| 88 | // static |
| 89 | void GamepadControllerBindings::Install( |
| 90 | base::WeakPtr<GamepadController> controller, |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 91 | blink::WebLocalFrame* frame) { |
Dave Tapuska | d1aaf8a | 2023-09-12 14:50:34 | [diff] [blame] | 92 | v8::Isolate* isolate = frame->GetAgentGroupScheduler()->Isolate(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 93 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 94 | v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 95 | if (context.IsEmpty()) |
| 96 | return; |
| 97 | |
| 98 | v8::Context::Scope context_scope(context); |
| 99 | |
| 100 | gin::Handle<GamepadControllerBindings> bindings = |
| 101 | gin::CreateHandle(isolate, new GamepadControllerBindings(controller)); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 102 | if (bindings.IsEmpty()) |
| 103 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 104 | v8::Local<v8::Object> global = context->Global(); |
Dan Elphick | a83be51 | 2019-02-05 15:57:23 | [diff] [blame] | 105 | global |
| 106 | ->Set(context, gin::StringToV8(isolate, "gamepadController"), |
| 107 | bindings.ToV8()) |
| 108 | .Check(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | GamepadControllerBindings::GamepadControllerBindings( |
| 112 | base::WeakPtr<GamepadController> controller) |
| 113 | : controller_(controller) {} |
| 114 | |
| 115 | GamepadControllerBindings::~GamepadControllerBindings() {} |
| 116 | |
| 117 | gin::ObjectTemplateBuilder GamepadControllerBindings::GetObjectTemplateBuilder( |
| 118 | v8::Isolate* isolate) { |
| 119 | return gin::Wrappable<GamepadControllerBindings>::GetObjectTemplateBuilder( |
| 120 | isolate) |
| 121 | .SetMethod("connect", &GamepadControllerBindings::Connect) |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 122 | .SetMethod("dispatchConnected", |
| 123 | &GamepadControllerBindings::DispatchConnected) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 124 | .SetMethod("disconnect", &GamepadControllerBindings::Disconnect) |
| 125 | .SetMethod("setId", &GamepadControllerBindings::SetId) |
| 126 | .SetMethod("setButtonCount", &GamepadControllerBindings::SetButtonCount) |
| 127 | .SetMethod("setButtonData", &GamepadControllerBindings::SetButtonData) |
| 128 | .SetMethod("setAxisCount", &GamepadControllerBindings::SetAxisCount) |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 129 | .SetMethod("setAxisData", &GamepadControllerBindings::SetAxisData) |
| 130 | .SetMethod("setDualRumbleVibrationActuator", |
Gabriel Brito | d5667f0 | 2022-07-05 01:29:29 | [diff] [blame] | 131 | &GamepadControllerBindings::SetDualRumbleVibrationActuator) |
| 132 | .SetMethod("setTriggerRumbleVibrationActuator", |
Bradley Needham | 835f0f49 | 2023-04-04 23:00:54 | [diff] [blame] | 133 | &GamepadControllerBindings::SetTriggerRumbleVibrationActuator) |
| 134 | .SetMethod("setTouchCount", &GamepadControllerBindings::SetTouchCount) |
| 135 | .SetMethod("setTouchData", &GamepadControllerBindings::SetTouchData); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void GamepadControllerBindings::Connect(int index) { |
| 139 | if (controller_) |
| 140 | controller_->Connect(index); |
| 141 | } |
| 142 | |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 143 | void GamepadControllerBindings::DispatchConnected(int index) { |
| 144 | if (controller_) |
| 145 | controller_->DispatchConnected(index); |
| 146 | } |
| 147 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 148 | void GamepadControllerBindings::Disconnect(int index) { |
| 149 | if (controller_) |
| 150 | controller_->Disconnect(index); |
| 151 | } |
| 152 | |
| 153 | void GamepadControllerBindings::SetId(int index, const std::string& src) { |
| 154 | if (controller_) |
| 155 | controller_->SetId(index, src); |
| 156 | } |
| 157 | |
| 158 | void GamepadControllerBindings::SetButtonCount(int index, int buttons) { |
| 159 | if (controller_) |
| 160 | controller_->SetButtonCount(index, buttons); |
| 161 | } |
| 162 | |
| 163 | void GamepadControllerBindings::SetButtonData(int index, |
| 164 | int button, |
| 165 | double data) { |
| 166 | if (controller_) |
| 167 | controller_->SetButtonData(index, button, data); |
| 168 | } |
| 169 | |
| 170 | void GamepadControllerBindings::SetAxisCount(int index, int axes) { |
| 171 | if (controller_) |
| 172 | controller_->SetAxisCount(index, axes); |
| 173 | } |
| 174 | |
| 175 | void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { |
| 176 | if (controller_) |
| 177 | controller_->SetAxisData(index, axis, data); |
| 178 | } |
| 179 | |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 180 | void GamepadControllerBindings::SetDualRumbleVibrationActuator(int index, |
| 181 | bool enabled) { |
| 182 | if (controller_) |
| 183 | controller_->SetDualRumbleVibrationActuator(index, enabled); |
| 184 | } |
| 185 | |
Gabriel Brito | d5667f0 | 2022-07-05 01:29:29 | [diff] [blame] | 186 | void GamepadControllerBindings::SetTriggerRumbleVibrationActuator( |
| 187 | int index, |
| 188 | bool enabled) { |
| 189 | if (controller_) |
| 190 | controller_->SetTriggerRumbleVibrationActuator(index, enabled); |
| 191 | } |
| 192 | |
Bradley Needham | 835f0f49 | 2023-04-04 23:00:54 | [diff] [blame] | 193 | void GamepadControllerBindings::SetTouchData(int index, |
| 194 | int touch, |
| 195 | unsigned int touch_id, |
| 196 | float position_x, |
| 197 | float position_y) { |
| 198 | if (controller_) { |
| 199 | controller_->SetTouchData(index, touch, touch_id, position_x, position_y); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void GamepadControllerBindings::SetTouchCount(int index, int touches) { |
| 204 | if (controller_) { |
| 205 | controller_->SetTouchCount(index, touches); |
| 206 | } |
| 207 | } |
| 208 | |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 209 | GamepadController::MonitorImpl::MonitorImpl( |
| 210 | GamepadController* controller, |
Gyuyoung Kim | 7959faf | 2019-08-15 12:10:20 | [diff] [blame] | 211 | mojo::PendingReceiver<device::mojom::GamepadMonitor> receiver) |
| 212 | : controller_(controller) { |
| 213 | receiver_.Bind(std::move(receiver)); |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | GamepadController::MonitorImpl::~MonitorImpl() = default; |
| 217 | |
| 218 | bool GamepadController::MonitorImpl::HasPendingConnect(int index) { |
| 219 | return missed_dispatches_.test(index); |
| 220 | } |
| 221 | |
| 222 | void GamepadController::MonitorImpl::GamepadStartPolling( |
| 223 | GamepadStartPollingCallback callback) { |
| 224 | std::move(callback).Run(controller_->GetSharedMemoryRegion()); |
| 225 | } |
| 226 | |
| 227 | void GamepadController::MonitorImpl::GamepadStopPolling( |
| 228 | GamepadStopPollingCallback callback) { |
| 229 | std::move(callback).Run(); |
| 230 | } |
| 231 | |
| 232 | void GamepadController::MonitorImpl::SetObserver( |
Gyuyoung Kim | 7959faf | 2019-08-15 12:10:20 | [diff] [blame] | 233 | mojo::PendingRemote<device::mojom::GamepadObserver> observer) { |
| 234 | observer_remote_.Bind(std::move(observer)); |
| 235 | observer_remote_.set_disconnect_handler( |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 236 | base::BindOnce(&GamepadController::OnConnectionError, |
| 237 | base::Unretained(controller_), base::Unretained(this))); |
| 238 | |
| 239 | // Notify the new observer of any GamepadConnected RPCs that it missed because |
| 240 | // the SetObserver RPC wasn't processed in time. This happens during layout |
| 241 | // tests because SetObserver is async, so the test can continue to the |
| 242 | // DispatchConnected call before the SetObserver RPC was processed. This isn't |
| 243 | // an issue in the real implementation because the 'gamepadconnected' event |
| 244 | // doesn't fire until user input is detected, so even if a GamepadConnected |
| 245 | // event is missed, another will be picked up after the next user input. |
| 246 | controller_->NotifyForMissedDispatches(this); |
| 247 | missed_dispatches_.reset(); |
| 248 | } |
| 249 | |
| 250 | void GamepadController::MonitorImpl::DispatchConnected( |
| 251 | int index, |
| 252 | const device::Gamepad& pad) { |
Gyuyoung Kim | 7959faf | 2019-08-15 12:10:20 | [diff] [blame] | 253 | if (observer_remote_) { |
| 254 | observer_remote_->GamepadConnected(index, pad); |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 255 | } else { |
| 256 | // Record that there wasn't an observer to get the GamepadConnected RPC so |
| 257 | // we can send it when SetObserver gets called. |
| 258 | missed_dispatches_.set(index); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void GamepadController::MonitorImpl::DispatchDisconnected( |
| 263 | int index, |
| 264 | const device::Gamepad& pad) { |
Gyuyoung Kim | 7959faf | 2019-08-15 12:10:20 | [diff] [blame] | 265 | if (observer_remote_) |
| 266 | observer_remote_->GamepadDisconnected(index, pad); |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void GamepadController::MonitorImpl::Reset() { |
| 270 | missed_dispatches_.reset(); |
| 271 | } |
| 272 | |
| 273 | GamepadController::GamepadController() { |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 274 | size_t buffer_size = sizeof(device::GamepadHardwareBuffer); |
Alexandr Ilin | 1ce67150 | 2018-07-19 20:59:35 | [diff] [blame] | 275 | // Use mojo to delegate the creation of shared memory to the broker process. |
| 276 | mojo::ScopedSharedBufferHandle mojo_buffer = |
| 277 | mojo::SharedBufferHandle::Create(buffer_size); |
| 278 | base::WritableSharedMemoryRegion writable_region = |
| 279 | mojo::UnwrapWritableSharedMemoryRegion(std::move(mojo_buffer)); |
| 280 | shared_memory_mapping_ = writable_region.Map(); |
| 281 | shared_memory_region_ = base::WritableSharedMemoryRegion::ConvertToReadOnly( |
| 282 | std::move(writable_region)); |
danakj | a0c51ca | 2020-08-26 20:54:59 | [diff] [blame] | 283 | if (!shared_memory_region_.IsValid()) { |
| 284 | // Log an error instead of crashing, as this can flakily happen in |
| 285 | // clusterfuzz. If it happened in the wild, the test would be retried. |
| 286 | LOG(ERROR) << "GamepadController shared memory region is not valid"; |
| 287 | } else if (!shared_memory_mapping_.IsValid()) { |
| 288 | // Log an error instead of crashing, as this can flakily happen in |
| 289 | // clusterfuzz. If it happened in the wild, the test would be retried. |
| 290 | LOG(ERROR) << "GamepadController shared memory mapping is not valid"; |
| 291 | } else { |
| 292 | gamepads_ = |
| 293 | new (shared_memory_mapping_.memory()) device::GamepadHardwareBuffer(); |
| 294 | } |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 295 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 296 | Reset(); |
| 297 | } |
| 298 | |
danakj | c989ac7 | 2020-04-15 20:31:23 | [diff] [blame] | 299 | GamepadController::~GamepadController() = default; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 300 | |
| 301 | void GamepadController::Reset() { |
danakj | a0c51ca | 2020-08-26 20:54:59 | [diff] [blame] | 302 | if (!gamepads_) |
| 303 | return; // Shared memory failed. |
| 304 | |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 305 | memset(gamepads_, 0, sizeof(*gamepads_)); |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 306 | for (auto& monitor : monitors_) |
| 307 | monitor->Reset(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 308 | } |
| 309 | |
danakj | a0c51ca | 2020-08-26 20:54:59 | [diff] [blame] | 310 | void GamepadController::Install(RenderFrame* frame) { |
| 311 | if (!gamepads_) |
| 312 | return; // Shared memory failed. |
Oksana Zhuravlova | 1165185 | 2018-07-16 20:07:50 | [diff] [blame] | 313 | |
Lukasz Anforowicz | 21b92848 | 2024-06-26 17:02:51 | [diff] [blame] | 314 | frame->GetBrowserInterfaceBroker().SetBinderForTesting( |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 315 | device::mojom::GamepadMonitor::Name_, |
| 316 | base::BindRepeating(&GamepadController::OnInterfaceRequest, |
| 317 | base::Unretained(this))); |
danakj | a0c51ca | 2020-08-26 20:54:59 | [diff] [blame] | 318 | GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), |
| 319 | frame->GetWebFrame()); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 320 | } |
| 321 | |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 322 | void GamepadController::OnInterfaceRequest( |
| 323 | mojo::ScopedMessagePipeHandle handle) { |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 324 | monitors_.insert(std::make_unique<MonitorImpl>( |
Miyoung Shin | c9f4dac | 2019-09-26 15:14:10 | [diff] [blame] | 325 | this, |
| 326 | mojo::PendingReceiver<device::mojom::GamepadMonitor>(std::move(handle)))); |
[email protected] | 078780b | 2014-06-20 16:57:06 | [diff] [blame] | 327 | } |
| 328 | |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 329 | base::ReadOnlySharedMemoryRegion GamepadController::GetSharedMemoryRegion() |
| 330 | const { |
| 331 | return shared_memory_region_.Duplicate(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 332 | } |
| 333 | |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 334 | void GamepadController::OnConnectionError( |
| 335 | GamepadController::MonitorImpl* monitor) { |
| 336 | monitors_.erase(monitors_.find(monitor)); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 337 | } |
| 338 | |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 339 | void GamepadController::NotifyForMissedDispatches( |
| 340 | GamepadController::MonitorImpl* monitor) { |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 341 | gamepads_->seqlock.WriteBegin(); |
| 342 | for (size_t index = 0; index < Gamepads::kItemsLengthCap; index++) { |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 343 | if (monitor->HasPendingConnect(index)) |
| 344 | monitor->DispatchConnected(index, gamepads_->data.items[index]); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 345 | } |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 346 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void GamepadController::Connect(int index) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 350 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 351 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 352 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 353 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 354 | Gamepad& pad = gamepads_->data.items[index]; |
| 355 | pad.connected = true; |
| 356 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 357 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 360 | void GamepadController::DispatchConnected(int index) { |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 361 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 362 | return; |
| 363 | const Gamepad& pad = gamepads_->data.items[index]; |
| 364 | if (!pad.connected) |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 365 | return; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 366 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 367 | for (auto& monitor : monitors_) |
| 368 | monitor->DispatchConnected(index, pad); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 369 | gamepads_->seqlock.WriteEnd(); |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 372 | void GamepadController::Disconnect(int index) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 373 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 374 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 375 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 376 | gamepads_->seqlock.WriteBegin(); |
| 377 | Gamepad& pad = gamepads_->data.items[index]; |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 378 | pad.connected = false; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 379 | pad.timestamp = now; |
Matt Reynolds | 093c89f | 2019-05-03 00:58:59 | [diff] [blame] | 380 | for (auto& monitor : monitors_) |
| 381 | monitor->DispatchDisconnected(index, pad); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 382 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | void GamepadController::SetId(int index, const std::string& src) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 386 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 387 | return; |
| 388 | const char* p = src.c_str(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 389 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 390 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 391 | Gamepad& pad = gamepads_->data.items[index]; |
| 392 | memset(pad.id, 0, sizeof(pad.id)); |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 393 | for (unsigned i = 0; *p && i < Gamepad::kIdLengthCap - 1; ++i) |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 394 | pad.id[i] = *p++; |
| 395 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 396 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void GamepadController::SetButtonCount(int index, int buttons) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 400 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 401 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 402 | if (buttons < 0 || buttons >= static_cast<int>(Gamepad::kButtonsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 403 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 404 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 405 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 406 | Gamepad& pad = gamepads_->data.items[index]; |
| 407 | pad.buttons_length = buttons; |
| 408 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 409 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void GamepadController::SetButtonData(int index, int button, double data) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 413 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 414 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 415 | if (button < 0 || button >= static_cast<int>(Gamepad::kButtonsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 416 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 417 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 418 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 419 | Gamepad& pad = gamepads_->data.items[index]; |
| 420 | pad.buttons[button].value = data; |
| 421 | pad.buttons[button].pressed = data > kButtonPressedThreshold; |
| 422 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 423 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void GamepadController::SetAxisCount(int index, int axes) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 427 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 428 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 429 | if (axes < 0 || axes >= static_cast<int>(Gamepad::kAxesLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 430 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 431 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 432 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 433 | Gamepad& pad = gamepads_->data.items[index]; |
| 434 | pad.axes_length = axes; |
| 435 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 436 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void GamepadController::SetAxisData(int index, int axis, double data) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 440 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 441 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 442 | if (axis < 0 || axis >= static_cast<int>(Gamepad::kAxesLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 443 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 444 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 445 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 446 | Gamepad& pad = gamepads_->data.items[index]; |
| 447 | pad.axes[axis] = data; |
| 448 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 449 | gamepads_->seqlock.WriteEnd(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 450 | } |
| 451 | |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 452 | void GamepadController::SetDualRumbleVibrationActuator(int index, |
| 453 | bool enabled) { |
| 454 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 455 | return; |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 456 | const int64_t now = CurrentTimeInMicroseconds(); |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 457 | gamepads_->seqlock.WriteBegin(); |
Matt Reynolds | 99e2f5ef | 2019-01-18 23:40:55 | [diff] [blame] | 458 | Gamepad& pad = gamepads_->data.items[index]; |
| 459 | pad.vibration_actuator.type = device::GamepadHapticActuatorType::kDualRumble; |
| 460 | pad.vibration_actuator.not_null = enabled; |
| 461 | pad.timestamp = now; |
Robbie McElrath | f7b2d117 | 2018-06-11 22:35:45 | [diff] [blame] | 462 | gamepads_->seqlock.WriteEnd(); |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 463 | } |
| 464 | |
Gabriel Brito | d5667f0 | 2022-07-05 01:29:29 | [diff] [blame] | 465 | void GamepadController::SetTriggerRumbleVibrationActuator(int index, |
| 466 | bool enabled) { |
| 467 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 468 | return; |
| 469 | const int64_t now = CurrentTimeInMicroseconds(); |
| 470 | gamepads_->seqlock.WriteBegin(); |
| 471 | Gamepad& pad = gamepads_->data.items[index]; |
| 472 | pad.vibration_actuator.type = |
| 473 | device::GamepadHapticActuatorType::kTriggerRumble; |
| 474 | pad.vibration_actuator.not_null = enabled; |
| 475 | pad.timestamp = now; |
| 476 | gamepads_->seqlock.WriteEnd(); |
| 477 | } |
| 478 | |
Bradley Needham | 835f0f49 | 2023-04-04 23:00:54 | [diff] [blame] | 479 | void GamepadController::SetTouchCount(int index, int touches) { |
| 480 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) { |
| 481 | return; |
| 482 | } |
| 483 | if (touches < 0 || |
| 484 | touches >= static_cast<int>(Gamepad::kTouchEventsLengthCap)) { |
| 485 | return; |
| 486 | } |
| 487 | const int64_t now = CurrentTimeInMicroseconds(); |
| 488 | gamepads_->seqlock.WriteBegin(); |
| 489 | Gamepad& pad = gamepads_->data.items[index]; |
| 490 | pad.supports_touch_events_ = true; |
| 491 | pad.touch_events_length = touches; |
| 492 | pad.timestamp = now; |
| 493 | gamepads_->seqlock.WriteEnd(); |
| 494 | } |
| 495 | |
| 496 | void GamepadController::SetTouchData(int index, |
| 497 | int touch, |
| 498 | unsigned int touch_id, |
| 499 | float position_x, |
| 500 | float position_y) { |
| 501 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) { |
| 502 | return; |
| 503 | } |
| 504 | if (touch < 0 || touch >= static_cast<int>(Gamepad::kTouchEventsLengthCap)) { |
| 505 | return; |
| 506 | } |
| 507 | const int64_t now = CurrentTimeInMicroseconds(); |
| 508 | gamepads_->seqlock.WriteBegin(); |
| 509 | Gamepad& pad = gamepads_->data.items[index]; |
| 510 | pad.supports_touch_events_ = true; |
| 511 | pad.touch_events[touch].touch_id = touch_id; |
| 512 | pad.touch_events[touch].x = position_x; |
| 513 | pad.touch_events[touch].y = position_y; |
| 514 | pad.timestamp = now; |
| 515 | gamepads_->seqlock.WriteEnd(); |
| 516 | } |
| 517 | |
danakj | 741848a | 2020-04-07 22:48:06 | [diff] [blame] | 518 | } // namespace content |