| 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/renderer/test_runner/gamepad_controller.h" | 5 #include "content/shell/renderer/test_runner/gamepad_controller.h" |
| 6 | 6 |
| 7 #include "content/shell/renderer/test_runner/web_test_delegate.h" | 7 #include "content/shell/renderer/test_runner/web_test_delegate.h" |
| 8 #include "gin/arguments.h" | 8 #include "gin/arguments.h" |
| 9 #include "gin/handle.h" | 9 #include "gin/handle.h" |
| 10 #include "gin/object_template_builder.h" | 10 #include "gin/object_template_builder.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 if (controller_) | 130 if (controller_) |
| 131 controller_->SetAxisCount(index, axes); | 131 controller_->SetAxisCount(index, axes); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { | 134 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { |
| 135 if (controller_) | 135 if (controller_) |
| 136 controller_->SetAxisData(index, axis, data); | 136 controller_->SetAxisData(index, axis, data); |
| 137 } | 137 } |
| 138 | 138 |
| 139 // static | 139 // static |
| 140 base::WeakPtr<GamepadController> GamepadController::Create(WebTestDelegate* dele
gate) { | 140 base::WeakPtr<GamepadController> GamepadController::Create( |
| 141 WebTestDelegate* delegate) { |
| 141 CHECK(delegate); | 142 CHECK(delegate); |
| 142 | 143 |
| 143 GamepadController* controller = new GamepadController(); | 144 GamepadController* controller = new GamepadController(); |
| 144 delegate->SetGamepadProvider(scoped_ptr<RendererGamepadProvider>(controller)); | 145 delegate->SetGamepadProvider(controller); |
| 145 return controller->weak_factory_.GetWeakPtr(); | 146 return controller->weak_factory_.GetWeakPtr(); |
| 146 } | 147 } |
| 147 | 148 |
| 148 GamepadController::GamepadController() | 149 GamepadController::GamepadController() |
| 149 : RendererGamepadProvider(0), | 150 : listener_(nullptr), weak_factory_(this) { |
| 150 weak_factory_(this) { | |
| 151 Reset(); | 151 Reset(); |
| 152 } | 152 } |
| 153 | 153 |
| 154 GamepadController::~GamepadController() { | 154 GamepadController::~GamepadController() { |
| 155 StopIfObserving(); | |
| 156 } | 155 } |
| 157 | 156 |
| 158 void GamepadController::Reset() { | 157 void GamepadController::Reset() { |
| 159 memset(&gamepads_, 0, sizeof(gamepads_)); | 158 memset(&gamepads_, 0, sizeof(gamepads_)); |
| 160 } | 159 } |
| 161 | 160 |
| 162 void GamepadController::Install(WebFrame* frame) { | 161 void GamepadController::Install(WebFrame* frame) { |
| 163 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | 162 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
| 164 } | 163 } |
| 165 | 164 |
| 166 | |
| 167 void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) { | 165 void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) { |
| 168 memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads)); | 166 memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads)); |
| 169 } | 167 } |
| 170 | 168 |
| 171 bool GamepadController::OnControlMessageReceived(const IPC::Message& msg) { | 169 void GamepadController::SetListener(blink::WebGamepadListener* listener) { |
| 172 return false; | 170 listener_ = listener; |
| 173 } | |
| 174 | |
| 175 void GamepadController::SendStartMessage() { | |
| 176 } | |
| 177 | |
| 178 void GamepadController::SendStopMessage() { | |
| 179 } | 171 } |
| 180 | 172 |
| 181 void GamepadController::Connect(int index) { | 173 void GamepadController::Connect(int index) { |
| 182 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 174 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
| 183 return; | 175 return; |
| 184 gamepads_.items[index].connected = true; | 176 gamepads_.items[index].connected = true; |
| 185 gamepads_.length = 0; | 177 gamepads_.length = 0; |
| 186 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 178 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 187 if (gamepads_.items[i].connected) | 179 if (gamepads_.items[i].connected) |
| 188 gamepads_.length = i + 1; | 180 gamepads_.length = i + 1; |
| 189 } | 181 } |
| 190 } | 182 } |
| 191 | 183 |
| 192 void GamepadController::DispatchConnected(int index) { | 184 void GamepadController::DispatchConnected(int index) { |
| 193 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap) | 185 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap) |
| 194 || !gamepads_.items[index].connected) | 186 || !gamepads_.items[index].connected) |
| 195 return; | 187 return; |
| 196 const WebGamepad& pad = gamepads_.items[index]; | 188 const WebGamepad& pad = gamepads_.items[index]; |
| 197 if (listener()) | 189 if (listener_) |
| 198 listener()->didConnectGamepad(index, pad); | 190 listener_->didConnectGamepad(index, pad); |
| 199 } | 191 } |
| 200 | 192 |
| 201 void GamepadController::Disconnect(int index) { | 193 void GamepadController::Disconnect(int index) { |
| 202 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 194 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
| 203 return; | 195 return; |
| 204 WebGamepad& pad = gamepads_.items[index]; | 196 WebGamepad& pad = gamepads_.items[index]; |
| 205 pad.connected = false; | 197 pad.connected = false; |
| 206 gamepads_.length = 0; | 198 gamepads_.length = 0; |
| 207 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 199 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
| 208 if (gamepads_.items[i].connected) | 200 if (gamepads_.items[i].connected) |
| 209 gamepads_.length = i + 1; | 201 gamepads_.length = i + 1; |
| 210 } | 202 } |
| 211 if (listener()) | 203 if (listener_) |
| 212 listener()->didDisconnectGamepad(index, pad); | 204 listener_->didDisconnectGamepad(index, pad); |
| 213 } | 205 } |
| 214 | 206 |
| 215 void GamepadController::SetId(int index, const std::string& src) { | 207 void GamepadController::SetId(int index, const std::string& src) { |
| 216 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 208 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
| 217 return; | 209 return; |
| 218 const char* p = src.c_str(); | 210 const char* p = src.c_str(); |
| 219 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); | 211 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); |
| 220 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i) | 212 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i) |
| 221 gamepads_.items[index].id[i] = *p++; | 213 gamepads_.items[index].id[i] = *p++; |
| 222 } | 214 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 248 | 240 |
| 249 void GamepadController::SetAxisData(int index, int axis, double data) { | 241 void GamepadController::SetAxisData(int index, int axis, double data) { |
| 250 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 242 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
| 251 return; | 243 return; |
| 252 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap)) | 244 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap)) |
| 253 return; | 245 return; |
| 254 gamepads_.items[index].axes[axis] = data; | 246 gamepads_.items[index].axes[axis] = data; |
| 255 } | 247 } |
| 256 | 248 |
| 257 } // namespace content | 249 } // namespace content |
| OLD | NEW |