| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/shell/test_runner/gamepad_controller.h" | 5 #include "content/shell/test_runner/gamepad_controller.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "content/shell/test_runner/web_test_delegate.h" | 10 #include "content/shell/test_runner/web_test_delegate.h" |
| 11 #include "gin/arguments.h" | 11 #include "gin/arguments.h" |
| 12 #include "gin/handle.h" | 12 #include "gin/handle.h" |
| 13 #include "gin/object_template_builder.h" | 13 #include "gin/object_template_builder.h" |
| 14 #include "gin/wrappable.h" | 14 #include "gin/wrappable.h" |
| 15 #include "third_party/WebKit/public/platform/WebGamepadListener.h" | 15 #include "third_party/WebKit/public/platform/WebGamepadListener.h" |
| 16 #include "third_party/WebKit/public/web/WebFrame.h" | 16 #include "third_party/WebKit/public/web/WebFrame.h" |
| 17 #include "third_party/WebKit/public/web/WebKit.h" | 17 #include "third_party/WebKit/public/web/WebKit.h" |
| 18 #include "v8/include/v8.h" | 18 #include "v8/include/v8.h" |
| 19 | 19 |
| 20 using blink::WebFrame; | 20 using blink::WebFrame; |
| 21 using blink::WebGamepad; | 21 using device::Gamepad; |
| 22 using blink::WebGamepads; | 22 using device::Gamepads; |
| 23 | 23 |
| 24 namespace test_runner { | 24 namespace test_runner { |
| 25 | 25 |
| 26 class GamepadControllerBindings | 26 class GamepadControllerBindings |
| 27 : public gin::Wrappable<GamepadControllerBindings> { | 27 : public gin::Wrappable<GamepadControllerBindings> { |
| 28 public: | 28 public: |
| 29 static gin::WrapperInfo kWrapperInfo; | 29 static gin::WrapperInfo kWrapperInfo; |
| 30 | 30 |
| 31 static void Install(base::WeakPtr<GamepadController> controller, | 31 static void Install(base::WeakPtr<GamepadController> controller, |
| 32 blink::WebFrame* frame); | 32 blink::WebFrame* frame); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 GamepadController::~GamepadController() {} | 158 GamepadController::~GamepadController() {} |
| 159 | 159 |
| 160 void GamepadController::Reset() { | 160 void GamepadController::Reset() { |
| 161 memset(&gamepads_, 0, sizeof(gamepads_)); | 161 memset(&gamepads_, 0, sizeof(gamepads_)); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void GamepadController::Install(WebFrame* frame) { | 164 void GamepadController::Install(WebFrame* frame) { |
| 165 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | 165 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) { | 168 void GamepadController::SampleGamepads(Gamepads& gamepads) { |
| 169 memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads)); | 169 memcpy(&gamepads, &gamepads_, sizeof(Gamepads)); |
| 170 } | 170 } |
| 171 | 171 |
| 172 void GamepadController::SetListener(blink::WebGamepadListener* listener) { | 172 void GamepadController::SetListener(blink::WebGamepadListener* listener) { |
| 173 listener_ = listener; | 173 listener_ = listener; |
| 174 } | 174 } |
| 175 | 175 |
| 176 void GamepadController::Connect(int index) { | 176 void GamepadController::Connect(int index) { |
| 177 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 177 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 178 return; | 178 return; |
| 179 gamepads_.items[index].connected = true; | 179 gamepads_.items[index].connected = true; |
| 180 } | 180 } |
| 181 | 181 |
| 182 void GamepadController::DispatchConnected(int index) { | 182 void GamepadController::DispatchConnected(int index) { |
| 183 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap) || | 183 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap) || |
| 184 !gamepads_.items[index].connected) | 184 !gamepads_.items[index].connected) |
| 185 return; | 185 return; |
| 186 const WebGamepad& pad = gamepads_.items[index]; | 186 const Gamepad& pad = gamepads_.items[index]; |
| 187 if (listener_) | 187 if (listener_) |
| 188 listener_->DidConnectGamepad(index, pad); | 188 listener_->DidConnectGamepad(index, pad); |
| 189 } | 189 } |
| 190 | 190 |
| 191 void GamepadController::Disconnect(int index) { | 191 void GamepadController::Disconnect(int index) { |
| 192 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 192 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 193 return; | 193 return; |
| 194 WebGamepad& pad = gamepads_.items[index]; | 194 Gamepad& pad = gamepads_.items[index]; |
| 195 pad.connected = false; | 195 pad.connected = false; |
| 196 if (listener_) | 196 if (listener_) |
| 197 listener_->DidDisconnectGamepad(index, pad); | 197 listener_->DidDisconnectGamepad(index, pad); |
| 198 } | 198 } |
| 199 | 199 |
| 200 void GamepadController::SetId(int index, const std::string& src) { | 200 void GamepadController::SetId(int index, const std::string& src) { |
| 201 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 201 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 202 return; | 202 return; |
| 203 const char* p = src.c_str(); | 203 const char* p = src.c_str(); |
| 204 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); | 204 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); |
| 205 for (unsigned i = 0; *p && i < WebGamepad::kIdLengthCap - 1; ++i) | 205 for (unsigned i = 0; *p && i < Gamepad::kIdLengthCap - 1; ++i) |
| 206 gamepads_.items[index].id[i] = *p++; | 206 gamepads_.items[index].id[i] = *p++; |
| 207 } | 207 } |
| 208 | 208 |
| 209 void GamepadController::SetButtonCount(int index, int buttons) { | 209 void GamepadController::SetButtonCount(int index, int buttons) { |
| 210 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 210 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 211 return; | 211 return; |
| 212 if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::kButtonsLengthCap)) | 212 if (buttons < 0 || buttons >= static_cast<int>(Gamepad::kButtonsLengthCap)) |
| 213 return; | 213 return; |
| 214 gamepads_.items[index].buttons_length = buttons; | 214 gamepads_.items[index].buttons_length = buttons; |
| 215 } | 215 } |
| 216 | 216 |
| 217 void GamepadController::SetButtonData(int index, int button, double data) { | 217 void GamepadController::SetButtonData(int index, int button, double data) { |
| 218 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 218 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 219 return; | 219 return; |
| 220 if (button < 0 || button >= static_cast<int>(WebGamepad::kButtonsLengthCap)) | 220 if (button < 0 || button >= static_cast<int>(Gamepad::kButtonsLengthCap)) |
| 221 return; | 221 return; |
| 222 gamepads_.items[index].buttons[button].value = data; | 222 gamepads_.items[index].buttons[button].value = data; |
| 223 gamepads_.items[index].buttons[button].pressed = data > 0.1f; | 223 gamepads_.items[index].buttons[button].pressed = data > 0.1f; |
| 224 } | 224 } |
| 225 | 225 |
| 226 void GamepadController::SetAxisCount(int index, int axes) { | 226 void GamepadController::SetAxisCount(int index, int axes) { |
| 227 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 227 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 228 return; | 228 return; |
| 229 if (axes < 0 || axes >= static_cast<int>(WebGamepad::kAxesLengthCap)) | 229 if (axes < 0 || axes >= static_cast<int>(Gamepad::kAxesLengthCap)) |
| 230 return; | 230 return; |
| 231 gamepads_.items[index].axes_length = axes; | 231 gamepads_.items[index].axes_length = axes; |
| 232 } | 232 } |
| 233 | 233 |
| 234 void GamepadController::SetAxisData(int index, int axis, double data) { | 234 void GamepadController::SetAxisData(int index, int axis, double data) { |
| 235 if (index < 0 || index >= static_cast<int>(WebGamepads::kItemsLengthCap)) | 235 if (index < 0 || index >= static_cast<int>(Gamepads::kItemsLengthCap)) |
| 236 return; | 236 return; |
| 237 if (axis < 0 || axis >= static_cast<int>(WebGamepad::kAxesLengthCap)) | 237 if (axis < 0 || axis >= static_cast<int>(Gamepad::kAxesLengthCap)) |
| 238 return; | 238 return; |
| 239 gamepads_.items[index].axes[axis] = data; | 239 gamepads_.items[index].axes[axis] = data; |
| 240 } | 240 } |
| 241 | 241 |
| 242 } // namespace test_runner | 242 } // namespace test_runner |
| OLD | NEW |