blob: 18acbc4a6cdadee69001620a8b049fcf9a35ab9b [file] [log] [blame]
[email protected]cf786002014-02-11 02:05:541// 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
dcheng59826e32017-02-22 10:31:365#include "content/shell/test_runner/gamepad_controller.h"
[email protected]cf786002014-02-11 02:05:546
avi5dd91f82015-12-25 22:30:467#include <string.h>
8
9#include "base/macros.h"
dcheng59826e32017-02-22 10:31:3610#include "content/shell/test_runner/web_test_delegate.h"
[email protected]cf786002014-02-11 02:05:5411#include "gin/arguments.h"
12#include "gin/handle.h"
13#include "gin/object_template_builder.h"
14#include "gin/wrappable.h"
Blink Reformata30d4232018-04-07 15:31:0615#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]cf786002014-02-11 02:05:5418#include "v8/include/v8.h"
19
juncai2f298a82017-04-18 03:51:3920using device::Gamepad;
21using device::Gamepads;
[email protected]cf786002014-02-11 02:05:5422
jochenf5f31752015-06-03 12:06:3423namespace test_runner {
[email protected]cf786002014-02-11 02:05:5424
25class GamepadControllerBindings
26 : public gin::Wrappable<GamepadControllerBindings> {
27 public:
28 static gin::WrapperInfo kWrapperInfo;
29
30 static void Install(base::WeakPtr<GamepadController> controller,
lukaszadf18ba762017-06-09 22:24:3031 blink::WebLocalFrame* frame);
[email protected]cf786002014-02-11 02:05:5432
33 private:
34 explicit GamepadControllerBindings(
35 base::WeakPtr<GamepadController> controller);
dchenge933b3e2014-10-21 11:44:0936 ~GamepadControllerBindings() override;
[email protected]cf786002014-02-11 02:05:5437
38 // gin::Wrappable.
dchenge933b3e2014-10-21 11:44:0939 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
anand.ratn449f39a42014-10-06 13:45:5740 v8::Isolate* isolate) override;
[email protected]cf786002014-02-11 02:05:5441
42 void Connect(int index);
[email protected]85603cbb2014-03-25 02:20:0143 void DispatchConnected(int index);
[email protected]cf786002014-02-11 02:05:5444 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 Reynolds6e9187e2017-10-23 18:32:0150 void SetDualRumbleVibrationActuator(int index, bool enabled);
[email protected]cf786002014-02-11 02:05:5451
52 base::WeakPtr<GamepadController> controller_;
53
54 DISALLOW_COPY_AND_ASSIGN(GamepadControllerBindings);
55};
56
57gin::WrapperInfo GamepadControllerBindings::kWrapperInfo = {
58 gin::kEmbedderNativeGin};
59
60// static
61void GamepadControllerBindings::Install(
62 base::WeakPtr<GamepadController> controller,
lukaszadf18ba762017-06-09 22:24:3063 blink::WebLocalFrame* frame) {
Blink Reformat1c4d759e2017-04-09 16:34:5464 v8::Isolate* isolate = blink::MainThreadIsolate();
[email protected]cf786002014-02-11 02:05:5465 v8::HandleScope handle_scope(isolate);
Blink Reformat1c4d759e2017-04-09 16:34:5466 v8::Local<v8::Context> context = frame->MainWorldScriptContext();
[email protected]cf786002014-02-11 02:05:5467 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]ad4d2032014-04-28 13:50:5974 if (bindings.IsEmpty())
75 return;
deepak.s750d68f2015-04-30 07:32:4176 v8::Local<v8::Object> global = context->Global();
[email protected]cf786002014-02-11 02:05:5477 global->Set(gin::StringToV8(isolate, "gamepadController"), bindings.ToV8());
78}
79
80GamepadControllerBindings::GamepadControllerBindings(
81 base::WeakPtr<GamepadController> controller)
82 : controller_(controller) {}
83
84GamepadControllerBindings::~GamepadControllerBindings() {}
85
86gin::ObjectTemplateBuilder GamepadControllerBindings::GetObjectTemplateBuilder(
87 v8::Isolate* isolate) {
88 return gin::Wrappable<GamepadControllerBindings>::GetObjectTemplateBuilder(
89 isolate)
90 .SetMethod("connect", &GamepadControllerBindings::Connect)
jochen73e711c2015-06-03 10:01:4691 .SetMethod("dispatchConnected",
92 &GamepadControllerBindings::DispatchConnected)
[email protected]cf786002014-02-11 02:05:5493 .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 Reynolds6e9187e2017-10-23 18:32:0198 .SetMethod("setAxisData", &GamepadControllerBindings::SetAxisData)
99 .SetMethod("setDualRumbleVibrationActuator",
100 &GamepadControllerBindings::SetDualRumbleVibrationActuator);
[email protected]cf786002014-02-11 02:05:54101}
102
103void GamepadControllerBindings::Connect(int index) {
104 if (controller_)
105 controller_->Connect(index);
106}
107
[email protected]85603cbb2014-03-25 02:20:01108void GamepadControllerBindings::DispatchConnected(int index) {
109 if (controller_)
110 controller_->DispatchConnected(index);
111}
112
[email protected]cf786002014-02-11 02:05:54113void GamepadControllerBindings::Disconnect(int index) {
114 if (controller_)
115 controller_->Disconnect(index);
116}
117
118void GamepadControllerBindings::SetId(int index, const std::string& src) {
119 if (controller_)
120 controller_->SetId(index, src);
121}
122
123void GamepadControllerBindings::SetButtonCount(int index, int buttons) {
124 if (controller_)
125 controller_->SetButtonCount(index, buttons);
126}
127
128void GamepadControllerBindings::SetButtonData(int index,
129 int button,
130 double data) {
131 if (controller_)
132 controller_->SetButtonData(index, button, data);
133}
134
135void GamepadControllerBindings::SetAxisCount(int index, int axes) {
136 if (controller_)
137 controller_->SetAxisCount(index, axes);
138}
139
140void GamepadControllerBindings::SetAxisData(int index, int axis, double data) {
141 if (controller_)
142 controller_->SetAxisData(index, axis, data);
143}
144
Matt Reynolds6e9187e2017-10-23 18:32:01145void GamepadControllerBindings::SetDualRumbleVibrationActuator(int index,
146 bool enabled) {
147 if (controller_)
148 controller_->SetDualRumbleVibrationActuator(index, enabled);
149}
150
[email protected]9c41b462014-08-19 15:51:34151// static
jochenc8337812015-05-14 01:11:58152base::WeakPtr<GamepadController> GamepadController::Create(
153 WebTestDelegate* delegate) {
[email protected]9c41b462014-08-19 15:51:34154 CHECK(delegate);
155
156 GamepadController* controller = new GamepadController();
jochenc8337812015-05-14 01:11:58157 delegate->SetGamepadProvider(controller);
[email protected]9c41b462014-08-19 15:51:34158 return controller->weak_factory_.GetWeakPtr();
159}
160
[email protected]078780b2014-06-20 16:57:06161GamepadController::GamepadController()
jochenc8337812015-05-14 01:11:58162 : listener_(nullptr), weak_factory_(this) {
[email protected]cf786002014-02-11 02:05:54163 Reset();
164}
165
dcheng59826e32017-02-22 10:31:36166GamepadController::~GamepadController() {}
[email protected]cf786002014-02-11 02:05:54167
168void GamepadController::Reset() {
169 memset(&gamepads_, 0, sizeof(gamepads_));
170}
171
lukaszadf18ba762017-06-09 22:24:30172void GamepadController::Install(blink::WebLocalFrame* frame) {
[email protected]cf786002014-02-11 02:05:54173 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame);
174}
175
juncai2f298a82017-04-18 03:51:39176void GamepadController::SampleGamepads(Gamepads& gamepads) {
177 memcpy(&gamepads, &gamepads_, sizeof(Gamepads));
[email protected]078780b2014-06-20 16:57:06178}
179
jochenc8337812015-05-14 01:11:58180void GamepadController::SetListener(blink::WebGamepadListener* listener) {
181 listener_ = listener;
[email protected]cf786002014-02-11 02:05:54182}
183
184void GamepadController::Connect(int index) {
juncai2f298a82017-04-18 03:51:39185 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54186 return;
187 gamepads_.items[index].connected = true;
[email protected]cf786002014-02-11 02:05:54188}
189
[email protected]85603cbb2014-03-25 02:20:01190void GamepadController::DispatchConnected(int index) {
juncai2f298a82017-04-18 03:51:39191 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap) ||
dcheng59826e32017-02-22 10:31:36192 !gamepads_.items[index].connected)
[email protected]85603cbb2014-03-25 02:20:01193 return;
juncai2f298a82017-04-18 03:51:39194 const Gamepad& pad = gamepads_.items[index];
jochenc8337812015-05-14 01:11:58195 if (listener_)
Blink Reformat1c4d759e2017-04-09 16:34:54196 listener_->DidConnectGamepad(index, pad);
[email protected]85603cbb2014-03-25 02:20:01197}
198
[email protected]cf786002014-02-11 02:05:54199void GamepadController::Disconnect(int index) {
juncai2f298a82017-04-18 03:51:39200 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54201 return;
juncai2f298a82017-04-18 03:51:39202 Gamepad& pad = gamepads_.items[index];
[email protected]85603cbb2014-03-25 02:20:01203 pad.connected = false;
jochenc8337812015-05-14 01:11:58204 if (listener_)
Blink Reformat1c4d759e2017-04-09 16:34:54205 listener_->DidDisconnectGamepad(index, pad);
[email protected]cf786002014-02-11 02:05:54206}
207
208void GamepadController::SetId(int index, const std::string& src) {
juncai2f298a82017-04-18 03:51:39209 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54210 return;
211 const char* p = src.c_str();
212 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id));
juncai2f298a82017-04-18 03:51:39213 for (unsigned i = 0; *p && i < Gamepad::kIdLengthCap - 1; ++i)
[email protected]cf786002014-02-11 02:05:54214 gamepads_.items[index].id[i] = *p++;
[email protected]cf786002014-02-11 02:05:54215}
216
217void GamepadController::SetButtonCount(int index, int buttons) {
juncai2f298a82017-04-18 03:51:39218 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54219 return;
juncai2f298a82017-04-18 03:51:39220 if (buttons < 0 || buttons >= static_cast<int>(Gamepad::kButtonsLengthCap))
[email protected]cf786002014-02-11 02:05:54221 return;
Blink Reformat1c4d759e2017-04-09 16:34:54222 gamepads_.items[index].buttons_length = buttons;
[email protected]cf786002014-02-11 02:05:54223}
224
225void GamepadController::SetButtonData(int index, int button, double data) {
juncai2f298a82017-04-18 03:51:39226 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54227 return;
juncai2f298a82017-04-18 03:51:39228 if (button < 0 || button >= static_cast<int>(Gamepad::kButtonsLengthCap))
[email protected]cf786002014-02-11 02:05:54229 return;
[email protected]723db332014-02-26 23:28:28230 gamepads_.items[index].buttons[button].value = data;
231 gamepads_.items[index].buttons[button].pressed = data > 0.1f;
[email protected]cf786002014-02-11 02:05:54232}
233
234void GamepadController::SetAxisCount(int index, int axes) {
juncai2f298a82017-04-18 03:51:39235 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54236 return;
juncai2f298a82017-04-18 03:51:39237 if (axes < 0 || axes >= static_cast<int>(Gamepad::kAxesLengthCap))
[email protected]cf786002014-02-11 02:05:54238 return;
Blink Reformat1c4d759e2017-04-09 16:34:54239 gamepads_.items[index].axes_length = axes;
[email protected]cf786002014-02-11 02:05:54240}
241
242void GamepadController::SetAxisData(int index, int axis, double data) {
juncai2f298a82017-04-18 03:51:39243 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap))
[email protected]cf786002014-02-11 02:05:54244 return;
juncai2f298a82017-04-18 03:51:39245 if (axis < 0 || axis >= static_cast<int>(Gamepad::kAxesLengthCap))
[email protected]cf786002014-02-11 02:05:54246 return;
247 gamepads_.items[index].axes[axis] = data;
[email protected]cf786002014-02-11 02:05:54248}
249
Matt Reynolds6e9187e2017-10-23 18:32:01250void 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
jochenf5f31752015-06-03 12:06:34259} // namespace test_runner