Commit 983206f1 authored by mstrum's avatar mstrum Committed by Commit bot

Gamepad: Add support for the Amazon Fire Game Controller

Manual verification of the mapping was done using:
http://www.html5rocks.com/en/tutorials/doodles/gamepad/gamepad-tester/tester.html

This also adds tests to check for mapping regressions for all gamepads.

BUG=453178,373942
TEST=GamepadMappingsTest

Review URL: https://codereview.chromium.org/875813003

Cr-Commit-Position: refs/heads/master@{#313994}
parent 3e48c23a
......@@ -10,6 +10,8 @@ import android.view.InputDevice.MotionRange;
import android.view.KeyEvent;
import android.view.MotionEvent;
import org.chromium.base.VisibleForTesting;
import java.util.Arrays;
import java.util.List;
......@@ -17,6 +19,16 @@ import java.util.List;
* Manages information related to each connected gamepad device.
*/
class GamepadDevice {
// Axis ids are used as indices which are empirically always smaller than 256 so this allows
// us to create cheap associative arrays.
@VisibleForTesting
static final int MAX_RAW_AXIS_VALUES = 256;
// Keycodes are used as indices which are empirically always smaller than 256 so this allows
// us to create cheap associative arrays.
@VisibleForTesting
static final int MAX_RAW_BUTTON_VALUES = 256;
// An id for the gamepad.
private int mDeviceId;
// The index of the gamepad in the Navigator.
......@@ -38,8 +50,8 @@ class GamepadDevice {
// that it be remapped to a canonical ordering when possible. Devices that are
// not recognized should still be exposed in their raw form. Therefore we must
// pass the raw Button and raw Axis values.
private final float[] mRawButtons = new float[256];
private final float[] mRawAxes = new float[256];
private final float[] mRawButtons = new float[MAX_RAW_BUTTON_VALUES];
private final float[] mRawAxes = new float[MAX_RAW_AXIS_VALUES];
// An identification string for the gamepad.
private String mDeviceName;
......@@ -59,7 +71,7 @@ class GamepadDevice {
for (MotionRange range : ranges) {
if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
int axis = range.getAxis();
assert axis < 256;
assert axis < MAX_RAW_AXIS_VALUES;
mAxes[i++] = axis;
}
}
......@@ -141,7 +153,7 @@ class GamepadDevice {
// Ignore event if it is not for standard gamepad key.
if (!GamepadList.isGamepadEvent(event)) return false;
int keyCode = event.getKeyCode();
assert keyCode < 256;
assert keyCode < MAX_RAW_BUTTON_VALUES;
// Button value 0.0 must mean fully unpressed, and 1.0 must mean fully pressed.
if (event.getAction() == KeyEvent.ACTION_DOWN) {
mRawButtons[keyCode] = 1.0f;
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Copyright 2015 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.
......@@ -8,17 +8,23 @@ import android.view.KeyEvent;
import android.view.MotionEvent;
import org.chromium.base.JNINamespace;
import org.chromium.base.VisibleForTesting;
/**
* Class to manage mapping information related to each supported gamepad controller device.
*/
@JNINamespace("content")
class GamepadMappings {
private static final String NVIDIA_SHIELD_DEVICE_NAME_PREFIX =
"NVIDIA Corporation NVIDIA Controller";
private static final String MICROSOFT_XBOX_PAD_DEVICE_NAME = "Microsoft X-Box 360 pad";
private static final String PS3_SIXAXIS_DEVICE_NAME = "Sony PLAYSTATION(R)3 Controller";
private static final String SAMSUNG_EI_GP20_DEVICE_NAME = "Samsung Game Pad EI-GP20";
@VisibleForTesting
static final String NVIDIA_SHIELD_DEVICE_NAME_PREFIX = "NVIDIA Corporation NVIDIA Controller";
@VisibleForTesting
static final String MICROSOFT_XBOX_PAD_DEVICE_NAME = "Microsoft X-Box 360 pad";
@VisibleForTesting
static final String PS3_SIXAXIS_DEVICE_NAME = "Sony PLAYSTATION(R)3 Controller";
@VisibleForTesting
static final String SAMSUNG_EI_GP20_DEVICE_NAME = "Samsung Game Pad EI-GP20";
@VisibleForTesting
static final String AMAZON_FIRE_DEVICE_NAME = "Amazon Fire Game Controller";
public static boolean mapToStandardGamepad(float[] mappedAxes, float[] mappedButtons,
float[] rawAxes, float[] rawButtons, String deviceName) {
......@@ -34,6 +40,9 @@ class GamepadMappings {
} else if (deviceName.equals(SAMSUNG_EI_GP20_DEVICE_NAME)) {
mapSamsungEIGP20Gamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
return true;
} else if (deviceName.equals(AMAZON_FIRE_DEVICE_NAME)) {
mapAmazonFireGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
return true;
}
mapUnknownGamepad(mappedButtons, rawButtons, mappedAxes, rawAxes);
......@@ -115,6 +124,13 @@ class GamepadMappings {
mappedButtons[CanonicalButtonIndex.RIGHT_SHOULDER] = rTrigger;
}
private static void mapPedalAxesToBottomShoulder(float[] mappedButtons, float[] rawAxes) {
float lTrigger = rawAxes[MotionEvent.AXIS_BRAKE];
float rTrigger = rawAxes[MotionEvent.AXIS_GAS];
mappedButtons[CanonicalButtonIndex.LEFT_TRIGGER] = lTrigger;
mappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER] = rTrigger;
}
private static void mapTriggerAxesToBottomShoulder(float[] mappedButtons, float[] rawAxes) {
float lTrigger = rawAxes[MotionEvent.AXIS_LTRIGGER];
float rTrigger = rawAxes[MotionEvent.AXIS_RTRIGGER];
......@@ -122,11 +138,13 @@ class GamepadMappings {
mappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER] = rTrigger;
}
private static float negativeAxisValueAsButton(float input) {
@VisibleForTesting
static float negativeAxisValueAsButton(float input) {
return (input < -0.5f) ? 1.f : 0.f;
}
private static float positiveAxisValueAsButton(float input) {
@VisibleForTesting
static float positiveAxisValueAsButton(float input) {
return (input > 0.5f) ? 1.f : 0.f;
}
......@@ -139,6 +157,23 @@ class GamepadMappings {
mappedButtons[CanonicalButtonIndex.DPAD_DOWN] = positiveAxisValueAsButton(hatY);
}
/**
* Method for mapping Amazon Fire gamepad axis and button values
* to standard gamepad button and axes values.
*/
private static void mapAmazonFireGamepad(
float[] mappedButtons, float[] rawButtons, float[] mappedAxes, float[] rawAxes) {
mapCommonXYABButtons(mappedButtons, rawButtons);
mapTriggerButtonsToTopShoulder(mappedButtons, rawButtons);
mapCommonThumbstickButtons(mappedButtons, rawButtons);
mapCommonStartSelectMetaButtons(mappedButtons, rawButtons);
mapPedalAxesToBottomShoulder(mappedButtons, rawAxes);
mapHatAxisToDpadButtons(mappedButtons, rawAxes);
mapXYAxes(mappedAxes, rawAxes);
mapZAndRZAxesToRightStick(mappedAxes, rawAxes);
}
/**
* Method for mapping Nvidia gamepad axis and button values
* to standard gamepad button and axes values.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment