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/TestInterfaces.h" | 7 #include "content/shell/renderer/test_runner/TestInterfaces.h" |
8 #include "content/shell/renderer/test_runner/WebTestDelegate.h" | 8 #include "content/shell/renderer/test_runner/WebTestDelegate.h" |
9 #include "gin/arguments.h" | 9 #include "gin/arguments.h" |
10 #include "gin/handle.h" | 10 #include "gin/handle.h" |
11 #include "gin/object_template_builder.h" | 11 #include "gin/object_template_builder.h" |
12 #include "gin/wrappable.h" | 12 #include "gin/wrappable.h" |
| 13 #include "third_party/WebKit/public/platform/WebGamepadListener.h" |
13 #include "third_party/WebKit/public/web/WebFrame.h" | 14 #include "third_party/WebKit/public/web/WebFrame.h" |
14 #include "third_party/WebKit/public/web/WebKit.h" | 15 #include "third_party/WebKit/public/web/WebKit.h" |
15 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
16 | 17 |
17 using blink::WebFrame; | 18 using blink::WebFrame; |
18 using blink::WebGamepad; | 19 using blink::WebGamepad; |
19 using blink::WebGamepads; | 20 using blink::WebGamepads; |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 void GamepadControllerBindings::SetAxisCount(int index, int axes) { | 130 void GamepadControllerBindings::SetAxisCount(int index, int axes) { |
130 if (controller_) | 131 if (controller_) |
131 controller_->SetAxisCount(index, axes); | 132 controller_->SetAxisCount(index, axes); |
132 } | 133 } |
133 | 134 |
134 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { | 135 void GamepadControllerBindings::SetAxisData(int index, int axis, double data) { |
135 if (controller_) | 136 if (controller_) |
136 controller_->SetAxisData(index, axis, data); | 137 controller_->SetAxisData(index, axis, data); |
137 } | 138 } |
138 | 139 |
139 GamepadController::GamepadController() : delegate_(NULL), weak_factory_(this) { | 140 GamepadController::GamepadController() |
| 141 : listener_(NULL), |
| 142 weak_factory_(this) { |
140 Reset(); | 143 Reset(); |
141 } | 144 } |
142 | 145 |
143 GamepadController::~GamepadController() {} | 146 GamepadController::~GamepadController() {} |
144 | 147 |
145 void GamepadController::Reset() { | 148 void GamepadController::Reset() { |
146 memset(&gamepads_, 0, sizeof(gamepads_)); | 149 memset(&gamepads_, 0, sizeof(gamepads_)); |
147 } | 150 } |
148 | 151 |
149 void GamepadController::Install(WebFrame* frame) { | 152 void GamepadController::Install(WebFrame* frame) { |
150 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); | 153 GamepadControllerBindings::Install(weak_factory_.GetWeakPtr(), frame); |
151 } | 154 } |
152 | 155 |
153 void GamepadController::SetDelegate(WebTestDelegate* delegate) { | 156 void GamepadController::SetDelegate(WebTestDelegate* delegate) { |
154 delegate_ = delegate; | 157 delegate->setGamepadProvider(this); |
| 158 } |
| 159 |
| 160 void GamepadController::SampleGamepads(blink::WebGamepads& gamepads) { |
| 161 memcpy(&gamepads, &gamepads_, sizeof(blink::WebGamepads)); |
| 162 } |
| 163 |
| 164 void GamepadController::SetGamepadListener( |
| 165 blink::WebGamepadListener* listener) { |
| 166 listener_ = listener; |
155 } | 167 } |
156 | 168 |
157 void GamepadController::Connect(int index) { | 169 void GamepadController::Connect(int index) { |
158 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 170 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
159 return; | 171 return; |
160 gamepads_.items[index].connected = true; | 172 gamepads_.items[index].connected = true; |
161 gamepads_.length = 0; | 173 gamepads_.length = 0; |
162 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 174 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
163 if (gamepads_.items[i].connected) | 175 if (gamepads_.items[i].connected) |
164 gamepads_.length = i + 1; | 176 gamepads_.length = i + 1; |
165 } | 177 } |
166 if (delegate_) | |
167 delegate_->setGamepadData(gamepads_); | |
168 } | 178 } |
169 | 179 |
170 void GamepadController::DispatchConnected(int index) { | 180 void GamepadController::DispatchConnected(int index) { |
171 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 181 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap) |
| 182 || !gamepads_.items[index].connected) |
172 return; | 183 return; |
173 const WebGamepad& pad = gamepads_.items[index]; | 184 const WebGamepad& pad = gamepads_.items[index]; |
174 if (pad.connected && delegate_) | 185 if (listener_) |
175 delegate_->didConnectGamepad(index, pad); | 186 listener_->didConnectGamepad(index, pad); |
176 } | 187 } |
177 | 188 |
178 void GamepadController::Disconnect(int index) { | 189 void GamepadController::Disconnect(int index) { |
179 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 190 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
180 return; | 191 return; |
181 WebGamepad& pad = gamepads_.items[index]; | 192 WebGamepad& pad = gamepads_.items[index]; |
182 pad.connected = false; | 193 pad.connected = false; |
183 gamepads_.length = 0; | 194 gamepads_.length = 0; |
184 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { | 195 for (unsigned i = 0; i < WebGamepads::itemsLengthCap; ++i) { |
185 if (gamepads_.items[i].connected) | 196 if (gamepads_.items[i].connected) |
186 gamepads_.length = i + 1; | 197 gamepads_.length = i + 1; |
187 } | 198 } |
188 if (delegate_) { | 199 if (listener_) |
189 delegate_->setGamepadData(gamepads_); | 200 listener_->didDisconnectGamepad(index, pad); |
190 delegate_->didDisconnectGamepad(index, pad); | |
191 } | |
192 } | 201 } |
193 | 202 |
194 void GamepadController::SetId(int index, const std::string& src) { | 203 void GamepadController::SetId(int index, const std::string& src) { |
195 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 204 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
196 return; | 205 return; |
197 const char* p = src.c_str(); | 206 const char* p = src.c_str(); |
198 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); | 207 memset(gamepads_.items[index].id, 0, sizeof(gamepads_.items[index].id)); |
199 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i) | 208 for (unsigned i = 0; *p && i < WebGamepad::idLengthCap - 1; ++i) |
200 gamepads_.items[index].id[i] = *p++; | 209 gamepads_.items[index].id[i] = *p++; |
201 if (delegate_) | |
202 delegate_->setGamepadData(gamepads_); | |
203 } | 210 } |
204 | 211 |
205 void GamepadController::SetButtonCount(int index, int buttons) { | 212 void GamepadController::SetButtonCount(int index, int buttons) { |
206 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 213 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
207 return; | 214 return; |
208 if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::buttonsLengthCap)) | 215 if (buttons < 0 || buttons >= static_cast<int>(WebGamepad::buttonsLengthCap)) |
209 return; | 216 return; |
210 gamepads_.items[index].buttonsLength = buttons; | 217 gamepads_.items[index].buttonsLength = buttons; |
211 if (delegate_) | |
212 delegate_->setGamepadData(gamepads_); | |
213 } | 218 } |
214 | 219 |
215 void GamepadController::SetButtonData(int index, int button, double data) { | 220 void GamepadController::SetButtonData(int index, int button, double data) { |
216 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 221 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
217 return; | 222 return; |
218 if (button < 0 || button >= static_cast<int>(WebGamepad::buttonsLengthCap)) | 223 if (button < 0 || button >= static_cast<int>(WebGamepad::buttonsLengthCap)) |
219 return; | 224 return; |
220 gamepads_.items[index].buttons[button].value = data; | 225 gamepads_.items[index].buttons[button].value = data; |
221 gamepads_.items[index].buttons[button].pressed = data > 0.1f; | 226 gamepads_.items[index].buttons[button].pressed = data > 0.1f; |
222 if (delegate_) | |
223 delegate_->setGamepadData(gamepads_); | |
224 } | 227 } |
225 | 228 |
226 void GamepadController::SetAxisCount(int index, int axes) { | 229 void GamepadController::SetAxisCount(int index, int axes) { |
227 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 230 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
228 return; | 231 return; |
229 if (axes < 0 || axes >= static_cast<int>(WebGamepad::axesLengthCap)) | 232 if (axes < 0 || axes >= static_cast<int>(WebGamepad::axesLengthCap)) |
230 return; | 233 return; |
231 gamepads_.items[index].axesLength = axes; | 234 gamepads_.items[index].axesLength = axes; |
232 if (delegate_) | |
233 delegate_->setGamepadData(gamepads_); | |
234 } | 235 } |
235 | 236 |
236 void GamepadController::SetAxisData(int index, int axis, double data) { | 237 void GamepadController::SetAxisData(int index, int axis, double data) { |
237 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) | 238 if (index < 0 || index >= static_cast<int>(WebGamepads::itemsLengthCap)) |
238 return; | 239 return; |
239 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap)) | 240 if (axis < 0 || axis >= static_cast<int>(WebGamepad::axesLengthCap)) |
240 return; | 241 return; |
241 gamepads_.items[index].axes[axis] = data; | 242 gamepads_.items[index].axes[axis] = data; |
242 if (delegate_) | |
243 delegate_->setGamepadData(gamepads_); | |
244 } | 243 } |
245 | 244 |
246 } // namespace content | 245 } // namespace content |
OLD | NEW |