| Index: content/public/android/java/src/org/chromium/content/browser/GamepadList.java
|
| diff --git a/content/public/android/java/src/org/chromium/content/browser/GamepadList.java b/content/public/android/java/src/org/chromium/content/browser/GamepadList.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6faa755d59451ee13b11ba4cdcf3f9880f00eac2
|
| --- /dev/null
|
| +++ b/content/public/android/java/src/org/chromium/content/browser/GamepadList.java
|
| @@ -0,0 +1,268 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.content.browser;
|
| +
|
| +import android.content.Context;
|
| +import android.hardware.input.InputManager;
|
| +import android.hardware.input.InputManager.InputDeviceListener;
|
| +import android.view.InputDevice;
|
| +import android.view.InputEvent;
|
| +import android.view.KeyEvent;
|
| +import android.view.MotionEvent;
|
| +
|
| +import org.chromium.base.CalledByNative;
|
| +import org.chromium.base.JNINamespace;
|
| +
|
| +/*
|
| + * Class to manage connected gamepad devices list.
|
| + */
|
| +@JNINamespace("content")
|
| +class GamepadList implements InputDeviceListener {
|
| + private static final int MAX_GAMEPADS = 4;
|
| + private GamepadDevice[] mGamepadDevices = new GamepadDevice[MAX_GAMEPADS];
|
| + protected boolean mHaveDevicesBeenInteractedWith = false;
|
| + private boolean mIsGamepadAccessed = false;
|
| + private InputManager mInputManager;
|
| + // Native pointer to C++ GamepadReader object which will be set by CreateJavaObject().
|
| + private long mNativeGamepadReader = 0;
|
| +
|
| + private GamepadList(Context context) {
|
| +
|
| + mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
|
| + // Get list of all the attached input devices.
|
| + int[] deviceIds = mInputManager.getInputDeviceIds();
|
| +
|
| + for (int i = 0; i < deviceIds.length; i++) {
|
| + InputDevice inputDevice = InputDevice.getDevice(deviceIds[i]);
|
| + // Check for gamepad device
|
| + if (isGamepadDevice(inputDevice)) {
|
| + // Register a new gamepad device.
|
| + registerGamepad(inputDevice);
|
| + }
|
| + }
|
| + // Register an input device listener.
|
| + mInputManager.registerInputDeviceListener(this, null);
|
| + }
|
| +
|
| + private static GamepadList sGamepadList = null;
|
| +
|
| + // Override InputDeviceListener methods
|
| + @Override
|
| + public void onInputDeviceChanged(int deviceId) {
|
| + }
|
| +
|
| + @Override
|
| + public void onInputDeviceRemoved(int deviceId) {
|
| + unregisterGamepad(deviceId);
|
| + }
|
| +
|
| + @Override
|
| + public void onInputDeviceAdded(int deviceId) {
|
| + InputDevice inputDevice = InputDevice.getDevice(deviceId);
|
| + if (isGamepadDevice(inputDevice))
|
| + registerGamepad(inputDevice);
|
| + }
|
| +
|
| + public static GamepadList createInstance(Context context) {
|
| + if (sGamepadList != null) {
|
| + return sGamepadList;
|
| + } else {
|
| + sGamepadList = new GamepadList(context);
|
| + return sGamepadList;
|
| + }
|
| + }
|
| +
|
| + private int getDeviceCount() {
|
| + int count = 0;
|
| + for (int i = 0; i < MAX_GAMEPADS; ++i) {
|
| + if (getDevice(i) != null) {
|
| + count++;
|
| + }
|
| + }
|
| + return count;
|
| + }
|
| +
|
| + private String getDeviceName(int index) {
|
| + return getDevice(index).getName();
|
| + }
|
| +
|
| + private long getDeviceTimestamp(int index) {
|
| + return getDevice(index).getTimestamp();
|
| + }
|
| +
|
| + private float[] getDeviceAxes(int index) {
|
| + return getDevice(index).getAxes();
|
| + }
|
| +
|
| + private float[] getDeviceButtons(int index) {
|
| + return getDevice(index).getButtons();
|
| + }
|
| +
|
| + private boolean isDeviceConnected(int index) {
|
| + if (index < MAX_GAMEPADS && getDevice(index) != null) {
|
| + return true;
|
| + }
|
| + return false;
|
| + }
|
| +
|
| + private boolean isKnownGamepadLayout(int index) {
|
| + return getDevice(index).isKnownGamepadLayout();
|
| + }
|
| +
|
| + public GamepadDevice getDeviceById(int deviceId) {
|
| + for (int i = 0; i < MAX_GAMEPADS; ++i) {
|
| + GamepadDevice gamepad = mGamepadDevices[i];
|
| + if (gamepad != null && gamepad.getId() == deviceId) {
|
| + return gamepad;
|
| + }
|
| + }
|
| + return null;
|
| + }
|
| +
|
| + public GamepadDevice getDevice(int index) {
|
| + // Maximum 4 Gamepads can be connected at a time starting at index zero.
|
| + assert index >= 0 && index <= MAX_GAMEPADS - 1;
|
| + return mGamepadDevices[index];
|
| + }
|
| +
|
| + public boolean updateForEvent(KeyEvent event) {
|
| + GamepadDevice gamepad = getGamepadForEvent(event);
|
| + if (gamepad != null) {
|
| + mHaveDevicesBeenInteractedWith = true;
|
| + return gamepad.updateForEvent(event);
|
| + }
|
| + return false;
|
| + }
|
| +
|
| + public boolean updateForEvent(MotionEvent event) {
|
| + GamepadDevice gamepad = getGamepadForEvent(event);
|
| + if (gamepad != null) {
|
| + mHaveDevicesBeenInteractedWith = true;
|
| + return gamepad.updateForEvent(event);
|
| + }
|
| + return false;
|
| + }
|
| +
|
| + protected int getNextAvailableIndex() {
|
| + // When multiple gamepads are connected to a user agent, indices must be assigned on a
|
| + // first-come first-serve basis, starting at zero. If a gamepad is disconnected, previously
|
| + // assigned indices must not be reassigned to gamepads that continue to be connected.
|
| + // However, if a gamepad is disconnected, and subsequently the same or a different
|
| + // gamepad is then connected, index entries must be reused.
|
| +
|
| + for (int i = 0; i < MAX_GAMEPADS; ++i) {
|
| + if (getDevice(i) == null) {
|
| + return i;
|
| + }
|
| + }
|
| + // Reached maximum gamepads limit.
|
| + return -1;
|
| + }
|
| +
|
| + protected boolean registerGamepad(InputDevice inputDevice) {
|
| + int index = getNextAvailableIndex();
|
| + if (index == -1) {
|
| + return false; // invalid index
|
| + }
|
| +
|
| + GamepadDevice gamepad = new GamepadDevice(index, inputDevice);
|
| + mGamepadDevices[index] = gamepad;
|
| + return true;
|
| + }
|
| +
|
| + protected void unregisterGamepad(int deviceId) {
|
| + GamepadDevice gamepadDevice = getDeviceById(deviceId);
|
| + if (gamepadDevice == null) {
|
| + return; // Not a registered device.
|
| + }
|
| + int index = gamepadDevice.getIndex();
|
| + mGamepadDevices[index] = null;
|
| + }
|
| +
|
| + private boolean isGamepadDevice(InputDevice inputDevice) {
|
| + int sources = inputDevice.getSources();
|
| + if ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK ) {
|
| + //Found gamepad device
|
| + return true;
|
| + }
|
| + return false;
|
| + }
|
| +
|
| + private GamepadDevice getGamepadForEvent(InputEvent event) {
|
| + int deviceId = event.getDeviceId();
|
| + GamepadDevice gamepad = getDeviceById(deviceId);
|
| + return gamepad;
|
| + }
|
| +
|
| + public boolean isGamepadAccessed() {
|
| + return mIsGamepadAccessed;
|
| + }
|
| +
|
| + public boolean isGamepadEvent(MotionEvent event) {
|
| + if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) {
|
| + return true;
|
| + }
|
| + return false;
|
| + }
|
| +
|
| + // Returns true if event's keycode corresponds to a gamepad key.
|
| + // Specific handling for dpad keys is required because
|
| + // KeyEvent.isGamepadButton doesn't consider dpad keys.
|
| + public boolean isGamepadEvent(KeyEvent event) {
|
| + int keyCode = event.getKeyCode();
|
| + switch (keyCode) {
|
| + case KeyEvent.KEYCODE_DPAD_UP:
|
| + case KeyEvent.KEYCODE_DPAD_DOWN:
|
| + case KeyEvent.KEYCODE_DPAD_LEFT:
|
| + case KeyEvent.KEYCODE_DPAD_RIGHT:
|
| + return true;
|
| + default:
|
| + return KeyEvent.isGamepadButton(keyCode);
|
| + }
|
| + }
|
| +
|
| + @CalledByNative
|
| + private void getGamepadData() {
|
| + int devicecount = getDeviceCount();
|
| + if (devicecount == 0 || !mHaveDevicesBeenInteractedWith)
|
| + nativeUpdateDeviceCount(mNativeGamepadReader, 0);
|
| + else
|
| + nativeUpdateDeviceCount(mNativeGamepadReader, devicecount);
|
| + for (int i = 0; i < MAX_GAMEPADS; i++) {
|
| + if (isDeviceConnected(i))
|
| + nativeSetGamepadData(mNativeGamepadReader, i, true, getDeviceName(i),
|
| + getDeviceTimestamp(i), getDeviceAxes(i),
|
| + getDeviceButtons(i));
|
| + else
|
| + nativeSetGamepadData(mNativeGamepadReader, i, false, null, 0, null, null);
|
| + }
|
| + }
|
| +
|
| + @CalledByNative
|
| + public static GamepadList getInstance(long nativeGamepadReader) {
|
| + if (sGamepadList != null) {
|
| + sGamepadList.mNativeGamepadReader = nativeGamepadReader;
|
| + return sGamepadList;
|
| + } else {
|
| + return null;
|
| + }
|
| + }
|
| +
|
| + @CalledByNative
|
| + private void notifyForGamepadsAccess(boolean isaccesspaused) {
|
| + mIsGamepadAccessed = !isaccesspaused;
|
| + }
|
| +
|
| + private native void nativeSetGamepadData(long nativeGamepadsReader,
|
| + int index,
|
| + boolean connected,
|
| + String devicename,
|
| + long timestamp,
|
| + float[] axes,
|
| + float[] buttons);
|
| +
|
| + private native void nativeUpdateDeviceCount(long nativeGamepadsReader, int devicecount);
|
| +
|
| +}
|
|
|