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.device.gamepad; |
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; |
11 import android.view.InputDevice; | 11 import android.view.InputDevice; |
12 import android.view.InputEvent; | 12 import android.view.InputEvent; |
13 import android.view.KeyEvent; | 13 import android.view.KeyEvent; |
14 import android.view.MotionEvent; | 14 import android.view.MotionEvent; |
15 | 15 |
16 import org.chromium.base.ThreadUtils; | 16 import org.chromium.base.ThreadUtils; |
17 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
18 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
19 import org.chromium.content.browser.ContentView; | |
20 | 19 |
21 /** | 20 /** |
22 * Class to manage connected gamepad devices list. | 21 * Class to manage connected gamepad devices list. |
23 * | 22 * |
24 * It is a Java counterpart of GamepadPlatformDataFetcherAndroid and feeds Gamep
ad API with input | 23 * It is a Java counterpart of GamepadPlatformDataFetcherAndroid and feeds Gamep
ad API with input |
25 * data. | 24 * data. |
26 */ | 25 */ |
27 @JNINamespace("content") | 26 @JNINamespace("device") |
28 public class GamepadList { | 27 public class GamepadList { |
29 private static final int MAX_GAMEPADS = 4; | 28 private static final int MAX_GAMEPADS = 4; |
30 | 29 |
31 private final Object mLock = new Object(); | 30 private final Object mLock = new Object(); |
32 | 31 |
33 private final GamepadDevice[] mGamepadDevices = new GamepadDevice[MAX_GAMEPA
DS]; | 32 private final GamepadDevice[] mGamepadDevices = new GamepadDevice[MAX_GAMEPA
DS]; |
34 private InputManager mInputManager; | 33 private InputManager mInputManager; |
35 private int mAttachedToWindowCounter; | 34 private int mAttachedToWindowCounter; |
36 private boolean mIsGamepadAPIActive; | 35 private boolean mIsGamepadAPIActive; |
37 private InputDeviceListener mInputDeviceListener; | 36 private InputDeviceListener mInputDeviceListener; |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 | 230 |
232 private void unregisterGamepad(int deviceId) { | 231 private void unregisterGamepad(int deviceId) { |
233 GamepadDevice gamepadDevice = getDeviceById(deviceId); | 232 GamepadDevice gamepadDevice = getDeviceById(deviceId); |
234 if (gamepadDevice == null) return; // Not a registered device. | 233 if (gamepadDevice == null) return; // Not a registered device. |
235 int index = gamepadDevice.getIndex(); | 234 int index = gamepadDevice.getIndex(); |
236 mGamepadDevices[index] = null; | 235 mGamepadDevices[index] = null; |
237 } | 236 } |
238 | 237 |
239 private static boolean isGamepadDevice(InputDevice inputDevice) { | 238 private static boolean isGamepadDevice(InputDevice inputDevice) { |
240 if (inputDevice == null) return false; | 239 if (inputDevice == null) return false; |
241 return ((inputDevice.getSources() | 240 return ((inputDevice.getSources() & InputDevice.SOURCE_JOYSTICK) |
242 & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK); | 241 == InputDevice.SOURCE_JOYSTICK); |
243 } | 242 } |
244 | 243 |
245 private GamepadDevice getGamepadForEvent(InputEvent event) { | 244 private GamepadDevice getGamepadForEvent(InputEvent event) { |
246 return getDeviceById(event.getDeviceId()); | 245 return getDeviceById(event.getDeviceId()); |
247 } | 246 } |
248 | 247 |
249 /** | 248 /** |
250 * @return True if HTML5 gamepad API is active. | 249 * @return True if HTML5 gamepad API is active. |
251 */ | 250 */ |
252 public static boolean isGamepadAPIActive() { | 251 public static boolean isGamepadAPIActive() { |
253 return getInstance().mIsGamepadAPIActive; | 252 return getInstance().mIsGamepadAPIActive; |
254 } | 253 } |
255 | 254 |
256 /** | 255 /** |
257 * @return True if the motion event corresponds to a gamepad event. | 256 * @return True if the motion event corresponds to a gamepad event. |
258 */ | 257 */ |
259 public static boolean isGamepadEvent(MotionEvent event) { | 258 public static boolean isGamepadEvent(MotionEvent event) { |
260 return ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice
.SOURCE_JOYSTICK); | 259 return ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice
.SOURCE_JOYSTICK); |
261 } | 260 } |
262 | 261 |
263 /** | 262 /** |
264 * @return True if event's keycode corresponds to a gamepad key. | 263 * @return True if event's keycode corresponds to a gamepad key. |
265 */ | 264 */ |
266 public static boolean isGamepadEvent(KeyEvent event) { | 265 public static boolean isGamepadEvent(KeyEvent event) { |
267 int keyCode = event.getKeyCode(); | 266 int keyCode = event.getKeyCode(); |
268 switch (keyCode) { | 267 switch (keyCode) { |
269 // Specific handling for dpad keys is required because | 268 // Specific handling for dpad keys is required because |
270 // KeyEvent.isGamepadButton doesn't consider dpad keys. | 269 // KeyEvent.isGamepadButton doesn't consider dpad keys. |
271 case KeyEvent.KEYCODE_DPAD_UP: | 270 case KeyEvent.KEYCODE_DPAD_UP: |
272 case KeyEvent.KEYCODE_DPAD_DOWN: | 271 case KeyEvent.KEYCODE_DPAD_DOWN: |
273 case KeyEvent.KEYCODE_DPAD_LEFT: | 272 case KeyEvent.KEYCODE_DPAD_LEFT: |
274 case KeyEvent.KEYCODE_DPAD_RIGHT: | 273 case KeyEvent.KEYCODE_DPAD_RIGHT: |
275 return true; | 274 return true; |
276 default: | 275 default: |
277 return KeyEvent.isGamepadButton(keyCode); | 276 return KeyEvent.isGamepadButton(keyCode); |
278 } | 277 } |
279 } | 278 } |
280 | 279 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 } | 315 } |
317 } | 316 } |
318 } | 317 } |
319 | 318 |
320 private native void nativeSetGamepadData(long webGamepadsPtr, int index, boo
lean mapping, | 319 private native void nativeSetGamepadData(long webGamepadsPtr, int index, boo
lean mapping, |
321 boolean connected, String devicename, long timestamp, float[] axes,
float[] buttons); | 320 boolean connected, String devicename, long timestamp, float[] axes,
float[] buttons); |
322 | 321 |
323 private static class LazyHolder { | 322 private static class LazyHolder { |
324 private static final GamepadList INSTANCE = new GamepadList(); | 323 private static final GamepadList INSTANCE = new GamepadList(); |
325 } | 324 } |
326 | |
327 } | 325 } |
OLD | NEW |