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