[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 | |
dcheng | 59826e3 | 2017-02-22 10:31:36 | [diff] [blame] | 5 | #include "content/shell/test_runner/gamepad_controller.h" |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <string.h> |
| 8 | |
| 9 | #include "base/macros.h" |
dcheng | 59826e3 | 2017-02-22 10:31:36 | [diff] [blame] | 10 | #include "content/shell/test_runner/web_test_delegate.h" |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 11 | #include "gin/arguments.h" |
| 12 | #include "gin/handle.h" |
| 13 | #include "gin/object_template_builder.h" |
| 14 | #include "gin/wrappable.h" |
Blink Reformat | a30d423 | 2018-04-07 15:31:06 | [diff] [blame^] | 15 | #include "third_party/blink/public/platform/web_gamepad_listener.h" |
| 16 | #include "third_party/blink/public/web/blink.h" |
| 17 | #include "third_party/blink/public/web/web_local_frame.h" |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 18 | #include "v8/include/v8.h" |
| 19 | |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 20 | using device::Gamepad; |
| 21 | using device::Gamepads; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 22 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 23 | namespace test_runner { |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 24 | |
| 25 | class GamepadControllerBindings |
| 26 | : public gin::Wrappable<GamepadControllerBindings> { |
| 27 | public: |
| 28 | static gin::WrapperInfo kWrapperInfo; |
| 29 | |
| 30 | static void Install(base::WeakPtr<GamepadController> controller, |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 31 | blink::WebLocalFrame* frame); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 32 | |
| 33 | private: |
| 34 | explicit GamepadControllerBindings( |
| 35 | base::WeakPtr<GamepadController> controller); |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 36 | ~GamepadControllerBindings() override; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 37 | |
| 38 | // gin::Wrappable. |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 39 | gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
anand.ratn | 449f39a4 | 2014-10-06 13:45:57 | [diff] [blame] | 40 | v8::Isolate* isolate) override; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 41 | |
| 42 | void Connect(int index); |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 43 | void DispatchConnected(int index); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 44 | void Disconnect(int index); |
| 45 | void SetId(int index, const std::string& src); |
| 46 | void SetButtonCount(int index, int buttons); |
| 47 | void SetButtonData(int index, int button, double data); |
| 48 | void SetAxisCount(int index, int axes); |
| 49 | void SetAxisData(int index, int axis, double data); |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 50 | void SetDualRumbleVibrationActuator(int index, bool enabled); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 51 | |
| 52 | base::WeakPtr<GamepadController> controller_; |
| 53 | |
| 54 | DISALLOW_COPY_AND_ASSIGN(GamepadControllerBindings); |
| 55 | }; |
| 56 | |
| 57 | gin::WrapperInfo GamepadControllerBindings::kWrapperInfo = { |
| 58 | gin::kEmbedderNativeGin}; |
| 59 | |
| 60 | // static |
| 61 | void GamepadControllerBindings::Install( |
| 62 | base::WeakPtr<GamepadController> controller, |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 63 | blink::WebLocalFrame* frame) { |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 64 | v8::Isolate* isolate = blink::MainThreadIsolate(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 65 | v8::HandleScope handle_scope(isolate); |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 66 | v8::Local<v8::Context> context = frame->MainWorldScriptContext(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 67 | if (context.IsEmpty()) |
| 68 | return; |
| 69 | |
| 70 | v8::Context::Scope context_scope(context); |
| 71 | |
| 72 | gin::Handle<GamepadControllerBindings> bindings = |
| 73 | gin::CreateHandle(isolate, new GamepadControllerBindings(controller)); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 74 | if (bindings.IsEmpty()) |
| 75 | return; |
deepak.s | 750d68f | 2015-04-30 07:32:41 | [diff] [blame] | 76 | v8::Local<v8::Object> global = context->Global(); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 77 | global->Set(gin::StringToV8(isolate, "gamepadController"), bindings.ToV8()); |
| 78 | } |
| 79 | |
| 80 | GamepadControllerBindings::GamepadControllerBindings( |
| 81 | base::WeakPtr<GamepadController> controller) |
| 82 | : controller_(controller) {} |
| 83 | |
| 84 | GamepadControllerBindings::~GamepadControllerBindings() {} |
| 85 | |
| 86 | gin::ObjectTemplateBuilder GamepadControllerBindings::GetObjectTemplateBuilder( |
| 87 | v8::Isolate* isolate) { |
| 88 | return gin::Wrappable<GamepadControllerBindings>::GetObjectTemplateBuilder( |
| 89 | isolate) |
| 90 | .SetMethod("connect", &GamepadControllerBindings::Connect) |
jochen | 73e711c | 2015-06-03 10:01:46 | [diff] [blame] | 91 | .SetMethod("dispatchConnected", |
| 92 | &GamepadControllerBindings::DispatchConnected) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 93 | .SetMethod("disconnect", &GamepadControllerBindings::Disconnect) |
| 94 | .SetMethod("setId", &GamepadControllerBindings::SetId) |
| 95 | .SetMethod("setButtonCount", &GamepadControllerBindings::SetButtonCount) |
| 96 | .SetMethod("setButtonData", &GamepadControllerBindings::SetButtonData) |
| 97 | .SetMethod("setAxisCount", &GamepadControllerBindings::SetAxisCount) |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 98 | .SetMethod("setAxisData", &GamepadControllerBindings::SetAxisData) |
| 99 | .SetMethod("setDualRumbleVibrationActuator", |
| 100 | &GamepadControllerBindings::SetDualRumbleVibrationActuator); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void GamepadControllerBindings::Connect(int index) { |
| 104 | if (controller_) |
| 105 | controller_->Connect(index); |
| 106 | } |
| 107 | |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 108 | void GamepadControllerBindings::DispatchConnected(int index) { |
| 109 | if (controller_) |
| 110 | controller_->DispatchConnected(index); |
| 111 | } |
| 112 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 113 | void GamepadControllerBindings::Disconnect(int index) { |
| 114 | if (controller_) |
| 115 | controller_->Disconnect(index); |
| 116 | } |
| 117 | |
| 118 | void GamepadControllerBindings::SetId(int index, const std::string& src) { |
| 119 | if (controller_) |
| 120 | controller_->SetId(index, src); |
| 121 | } |
| 122 | |
| 123 | void GamepadControllerBindings::SetButtonCount(int index, int buttons) { |
| 124 | if (controller_) |
| 125 | controller_->SetButtonCount(index, buttons); |
| 126 | } |
| 127 | |
| 128 | void GamepadControllerBindings::SetButtonData(int index, |
| 129 | int button, |
| 130 | double data) { |
| 131 | if (controller_) |
| 132 | controller_->SetButtonData(index, button, data); |
| 133 | } |
| 134 | |
| 135 | void GamepadControllerBindings::SetAxisCount(int index, int axes) { |
| 136 | if (controller_) |
| 137 | controller_->SetAxisCount(index, axes); |
| 138 | } |
| 139 | |
| 140 | void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { |
| 141 | if (controller_) |
| 142 | controller_->SetAxisData(index, axis, data); |
| 143 | } |
| 144 | |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 145 | void GamepadControllerBindings::SetDualRumbleVibrationActuator(int index, |
| 146 | bool enabled) { |
| 147 | if (controller_) |
| 148 | controller_->SetDualRumbleVibrationActuator(index, enabled); |
| 149 | } |
| 150 | |
[email protected] | 9c41b46 | 2014-08-19 15:51:34 | [diff] [blame] | 151 | // static |
jochen | c833781 | 2015-05-14 01:11:58 | [diff] [blame] | 152 | base::WeakPtr<GamepadController> GamepadController::Create( |
| 153 | WebTestDelegate* delegate) { |
[email protected] | 9c41b46 | 2014-08-19 15:51:34 | [diff] [blame] | 154 | CHECK(delegate); |
| 155 | |
| 156 | GamepadController* controller = new GamepadController(); |
jochen | c833781 | 2015-05-14 01:11:58 | [diff] [blame] | 157 | delegate->SetGamepadProvider(controller); |
[email protected] | 9c41b46 | 2014-08-19 15:51:34 | [diff] [blame] | 158 | return controller->weak_factory_.GetWeakPtr(); |
| 159 | } |
| 160 | |
[email protected] | 078780b | 2014-06-20 16:57:06 | [diff] [blame] | 161 | GamepadController::GamepadController() |
jochen | c833781 | 2015-05-14 01:11:58 | [diff] [blame] | 162 | : listener_(nullptr), weak_factory_(this) { |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 163 | Reset(); |
| 164 | } |
| 165 | |
dcheng | 59826e3 | 2017-02-22 10:31:36 | [diff] [blame] | 166 | GamepadController::~GamepadController() {} |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 167 | |
| 168 | void GamepadController::Reset() { |
| 169 | memset(&gamepads_, 0, sizeof(gamepads_)); |
| 170 | } |
| 171 | |
lukasza | df18ba76 | 2017-06-09 22:24:30 | [diff] [blame] | 172 | void GamepadController::Install(blink::WebLocalFrame* frame) { |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 173 | GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 174 | } |
| 175 | |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 176 | void GamepadController::SampleGamepads(Gamepads& gamepads) { |
| 177 | memcpy(&gamepads, &gamepads_, sizeof(Gamepads)); |
[email protected] | 078780b | 2014-06-20 16:57:06 | [diff] [blame] | 178 | } |
| 179 | |
jochen | c833781 | 2015-05-14 01:11:58 | [diff] [blame] | 180 | void GamepadController::SetListener(blink::WebGamepadListener* listener) { |
| 181 | listener_ = listener; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void GamepadController::Connect(int index) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 185 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 186 | return; |
| 187 | gamepads_.items[index].connected = true; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 188 | } |
| 189 | |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 190 | void GamepadController::DispatchConnected(int index) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 191 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap) || |
dcheng | 59826e3 | 2017-02-22 10:31:36 | [diff] [blame] | 192 | !gamepads_.items[index].connected) |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 193 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 194 | const Gamepad& pad = gamepads_.items[index]; |
jochen | c833781 | 2015-05-14 01:11:58 | [diff] [blame] | 195 | if (listener_) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 196 | listener_->DidConnectGamepad(index, pad); |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 199 | void GamepadController::Disconnect(int index) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 200 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 201 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 202 | Gamepad& pad = gamepads_.items[index]; |
[email protected] | 85603cbb | 2014-03-25 02:20:01 | [diff] [blame] | 203 | pad.connected = false; |
jochen | c833781 | 2015-05-14 01:11:58 | [diff] [blame] | 204 | if (listener_) |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 205 | listener_->DidDisconnectGamepad(index, pad); |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void GamepadController::SetId(int index, const std::string& src) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 209 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 210 | return; |
| 211 | const char* p = src.c_str(); |
| 212 | memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 213 | for (unsigned i = 0; *p && i < Gamepad::kIdLengthCap - 1; ++i) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 214 | gamepads_.items[index].id[i] = *p++; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void GamepadController::SetButtonCount(int index, int buttons) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 218 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 219 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 220 | if (buttons < 0 || buttons >= static_cast<int>(Gamepad::kButtonsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 221 | return; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 222 | gamepads_.items[index].buttons_length = buttons; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | void GamepadController::SetButtonData(int index, int button, double data) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 226 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 227 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 228 | if (button < 0 || button >= static_cast<int>(Gamepad::kButtonsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 229 | return; |
[email protected] | 723db33 | 2014-02-26 23:28:28 | [diff] [blame] | 230 | gamepads_.items[index].buttons[button].value = data; |
| 231 | gamepads_.items[index].buttons[button].pressed = data > 0.1f; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void GamepadController::SetAxisCount(int index, int axes) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 235 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 236 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 237 | if (axes < 0 || axes >= static_cast<int>(Gamepad::kAxesLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 238 | return; |
Blink Reformat | 1c4d759e | 2017-04-09 16:34:54 | [diff] [blame] | 239 | gamepads_.items[index].axes_length = axes; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void GamepadController::SetAxisData(int index, int axis, double data) { |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 243 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 244 | return; |
juncai | 2f298a8 | 2017-04-18 03:51:39 | [diff] [blame] | 245 | if (axis < 0 || axis >= static_cast<int>(Gamepad::kAxesLengthCap)) |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 246 | return; |
| 247 | gamepads_.items[index].axes[axis] = data; |
[email protected] | cf78600 | 2014-02-11 02:05:54 | [diff] [blame] | 248 | } |
| 249 | |
Matt Reynolds | 6e9187e | 2017-10-23 18:32:01 | [diff] [blame] | 250 | void GamepadController::SetDualRumbleVibrationActuator(int index, |
| 251 | bool enabled) { |
| 252 | if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 253 | return; |
| 254 | gamepads_.items[index].vibration_actuator.type = |
| 255 | device::GamepadHapticActuatorType::kDualRumble; |
| 256 | gamepads_.items[index].vibration_actuator.not_null = enabled; |
| 257 | } |
| 258 | |
jochen | f5f3175 | 2015-06-03 12:06:34 | [diff] [blame] | 259 | } // namespace test_runner |