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 package org.chromium.content.browser.input; | 5 package org.chromium.content.browser.input; |
6 | 6 |
7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.hardware.input.InputManager; | 9 import android.hardware.input.InputManager; |
10 import android.hardware.input.InputManager.InputDeviceListener; | 10 import android.hardware.input.InputManager.InputDeviceListener; |
(...skipping 15 matching lines...) Expand all Loading... |
26 */ | 26 */ |
27 @JNINamespace("content") | 27 @JNINamespace("content") |
28 public class GamepadList { | 28 public class GamepadList { |
29 private static final int MAX_GAMEPADS = 4; | 29 private static final int MAX_GAMEPADS = 4; |
30 | 30 |
31 private final Object mLock = new Object(); | 31 private final Object mLock = new Object(); |
32 | 32 |
33 private final GamepadDevice[] mGamepadDevices = new GamepadDevice[MAX_GAMEPA
DS]; | 33 private final GamepadDevice[] mGamepadDevices = new GamepadDevice[MAX_GAMEPA
DS]; |
34 private InputManager mInputManager; | 34 private InputManager mInputManager; |
35 private int mAttachedToWindowCounter; | 35 private int mAttachedToWindowCounter; |
36 private boolean mIsGamepadAccessed; | 36 private boolean mIsGamepadAPIActive; |
37 private InputDeviceListener mInputDeviceListener; | 37 private InputDeviceListener mInputDeviceListener; |
38 | 38 |
39 private GamepadList() { | 39 private GamepadList() { |
40 mInputDeviceListener = new InputDeviceListener() { | 40 mInputDeviceListener = new InputDeviceListener() { |
41 // Override InputDeviceListener methods | 41 // Override InputDeviceListener methods |
42 @Override | 42 @Override |
43 public void onInputDeviceChanged(int deviceId) { | 43 public void onInputDeviceChanged(int deviceId) { |
44 onInputDeviceChangedImpl(deviceId); | 44 onInputDeviceChangedImpl(deviceId); |
45 } | 45 } |
46 | 46 |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 * Handles key events from the gamepad devices. | 172 * Handles key events from the gamepad devices. |
173 * @return True if the event has been consumed. | 173 * @return True if the event has been consumed. |
174 */ | 174 */ |
175 public static boolean dispatchKeyEvent(KeyEvent event) { | 175 public static boolean dispatchKeyEvent(KeyEvent event) { |
176 if (!isGamepadEvent(event)) return false; | 176 if (!isGamepadEvent(event)) return false; |
177 return getInstance().handleKeyEvent(event); | 177 return getInstance().handleKeyEvent(event); |
178 } | 178 } |
179 | 179 |
180 private boolean handleKeyEvent(KeyEvent event) { | 180 private boolean handleKeyEvent(KeyEvent event) { |
181 synchronized (mLock) { | 181 synchronized (mLock) { |
182 if (!mIsGamepadAccessed) return false; | 182 if (!mIsGamepadAPIActive) return false; |
183 GamepadDevice gamepad = getGamepadForEvent(event); | 183 GamepadDevice gamepad = getGamepadForEvent(event); |
184 if (gamepad == null) return false; | 184 if (gamepad == null) return false; |
185 return gamepad.handleKeyEvent(event); | 185 return gamepad.handleKeyEvent(event); |
186 } | 186 } |
187 } | 187 } |
188 | 188 |
189 /** | 189 /** |
190 * Handles motion events from the gamepad devices. | 190 * Handles motion events from the gamepad devices. |
191 * @return True if the event has been consumed. | 191 * @return True if the event has been consumed. |
192 */ | 192 */ |
193 public static boolean onGenericMotionEvent(MotionEvent event) { | 193 public static boolean onGenericMotionEvent(MotionEvent event) { |
194 if (!isGamepadEvent(event)) return false; | 194 if (!isGamepadEvent(event)) return false; |
195 return getInstance().handleMotionEvent(event); | 195 return getInstance().handleMotionEvent(event); |
196 } | 196 } |
197 | 197 |
198 private boolean handleMotionEvent(MotionEvent event) { | 198 private boolean handleMotionEvent(MotionEvent event) { |
199 synchronized (mLock) { | 199 synchronized (mLock) { |
200 if (!mIsGamepadAccessed) return false; | 200 if (!mIsGamepadAPIActive) return false; |
201 GamepadDevice gamepad = getGamepadForEvent(event); | 201 GamepadDevice gamepad = getGamepadForEvent(event); |
202 if (gamepad == null) return false; | 202 if (gamepad == null) return false; |
203 return gamepad.handleMotionEvent(event); | 203 return gamepad.handleMotionEvent(event); |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 private int getNextAvailableIndex() { | 207 private int getNextAvailableIndex() { |
208 // When multiple gamepads are connected to a user agent, indices must be
assigned on a | 208 // When multiple gamepads are connected to a user agent, indices must be
assigned on a |
209 // first-come first-serve basis, starting at zero. If a gamepad is disco
nnected, previously | 209 // first-come first-serve basis, starting at zero. If a gamepad is disco
nnected, previously |
210 // assigned indices must not be reassigned to gamepads that continue to
be connected. | 210 // assigned indices must not be reassigned to gamepads that continue to
be connected. |
(...skipping 29 matching lines...) Expand all Loading... |
240 if (inputDevice == null) return false; | 240 if (inputDevice == null) return false; |
241 return ((inputDevice.getSources() | 241 return ((inputDevice.getSources() |
242 & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK); | 242 & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK); |
243 } | 243 } |
244 | 244 |
245 private GamepadDevice getGamepadForEvent(InputEvent event) { | 245 private GamepadDevice getGamepadForEvent(InputEvent event) { |
246 return getDeviceById(event.getDeviceId()); | 246 return getDeviceById(event.getDeviceId()); |
247 } | 247 } |
248 | 248 |
249 /** | 249 /** |
| 250 * @return True if HTML5 gamepad API is active. |
| 251 */ |
| 252 public static boolean isGamepadAPIActive() { |
| 253 return getInstance().mIsGamepadAPIActive; |
| 254 } |
| 255 |
| 256 /** |
250 * @return True if the motion event corresponds to a gamepad event. | 257 * @return True if the motion event corresponds to a gamepad event. |
251 */ | 258 */ |
252 public static boolean isGamepadEvent(MotionEvent event) { | 259 public static boolean isGamepadEvent(MotionEvent event) { |
253 return ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice
.SOURCE_JOYSTICK); | 260 return ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice
.SOURCE_JOYSTICK); |
254 } | 261 } |
255 | 262 |
256 /** | 263 /** |
257 * @return True if event's keycode corresponds to a gamepad key. | 264 * @return True if event's keycode corresponds to a gamepad key. |
258 */ | 265 */ |
259 public static boolean isGamepadEvent(KeyEvent event) { | 266 public static boolean isGamepadEvent(KeyEvent event) { |
(...skipping 26 matching lines...) Expand all Loading... |
286 device.getName(), device.getTimestamp(), device.getA
xes(), | 293 device.getName(), device.getTimestamp(), device.getA
xes(), |
287 device.getButtons()); | 294 device.getButtons()); |
288 } else { | 295 } else { |
289 nativeSetGamepadData(webGamepadsPtr, i, false, false, null,
0, null, null); | 296 nativeSetGamepadData(webGamepadsPtr, i, false, false, null,
0, null, null); |
290 } | 297 } |
291 } | 298 } |
292 } | 299 } |
293 } | 300 } |
294 | 301 |
295 @CalledByNative | 302 @CalledByNative |
296 static void notifyForGamepadsAccess(boolean isAccessPaused) { | 303 static void setGamepadAPIActive(boolean isActive) { |
297 getInstance().setIsGamepadAccessed(!isAccessPaused); | 304 getInstance().setIsGamepadActive(isActive); |
298 } | 305 } |
299 | 306 |
300 private void setIsGamepadAccessed(boolean isGamepadAccessed) { | 307 private void setIsGamepadActive(boolean isGamepadActive) { |
301 synchronized (mLock) { | 308 synchronized (mLock) { |
302 mIsGamepadAccessed = isGamepadAccessed; | 309 mIsGamepadAPIActive = isGamepadActive; |
303 if (isGamepadAccessed) { | 310 if (isGamepadActive) { |
304 for (int i = 0; i < MAX_GAMEPADS; i++) { | 311 for (int i = 0; i < MAX_GAMEPADS; i++) { |
305 GamepadDevice gamepadDevice = getDevice(i); | 312 GamepadDevice gamepadDevice = getDevice(i); |
306 if (gamepadDevice == null) continue; | 313 if (gamepadDevice == null) continue; |
307 gamepadDevice.clearData(); | 314 gamepadDevice.clearData(); |
308 } | 315 } |
309 } | 316 } |
310 } | 317 } |
311 } | 318 } |
312 | 319 |
313 private native void nativeSetGamepadData(long webGamepadsPtr, int index, boo
lean mapping, | 320 private native void nativeSetGamepadData(long webGamepadsPtr, int index, boo
lean mapping, |
314 boolean connected, String devicename, long timestamp, float[] axes,
float[] buttons); | 321 boolean connected, String devicename, long timestamp, float[] axes,
float[] buttons); |
315 | 322 |
316 private static class LazyHolder { | 323 private static class LazyHolder { |
317 private static final GamepadList INSTANCE = new GamepadList(); | 324 private static final GamepadList INSTANCE = new GamepadList(); |
318 } | 325 } |
319 | 326 |
320 } | 327 } |
OLD | NEW |