Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6e7f8dbe5748dccede5925752dc14407106af57c |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| @@ -0,0 +1,191 @@ |
| +// 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.os.SystemClock; |
| +import android.view.InputDevice; |
| +import android.view.InputDevice.MotionRange; |
| +import android.view.KeyEvent; |
| +import android.view.MotionEvent; |
| + |
| +import java.util.Arrays; |
| +import java.util.Iterator; |
| +import java.util.LinkedHashMap; |
| + |
| +import org.chromium.content.browser.input.gamepad_mapping.CanonicalAxisIndex; |
| +import org.chromium.content.browser.input.gamepad_mapping.CanonicalButtonIndex; |
| + |
| +/* |
| + * Class to manage information related to each connected gamepad device. |
| + */ |
| + |
| +class GamepadDevice { |
| + // An id for the gamepad. |
| + protected int mDeviceId; |
|
jdduke (slow)
2014/03/24 17:38:15
Do you plan on subclassing this? If not, these mem
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
Done.
|
| + // The index of the gamepad in the Navigator. |
| + protected int mDeviceIndex; |
| + // Last time the data for this gamepad was updated. |
| + protected long mTimestamp; |
| + // If this gamepad is mapped to standard gamepad? |
| + protected String mMapping; |
| + |
| + // Array of values for all axes of the gamepad. |
| + // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. |
| + // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 |
| + // should correspond to "down" or "right". |
| + protected float[] mAxisValues; |
| + |
| + protected float[] mButtonsValues; |
| + |
| + // When the user agent recognizes the attached inputDevice, it is recommended |
| + // 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. |
| + protected float[] mRawButtons; |
| + protected float[] mRawAxes; |
| + |
| + // An identification string for the gamepad. |
| + protected String mDeviceName; |
| + |
| + // Array of axes ids. |
| + private int[] mAxes; |
| + |
| + // List of keycodes that can be sent by a standard gamepad. Standard gamepad can have |
| + // buttons as per the spec: |
| + // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping |
| + // Need to maintain this list to get input device capabilities. We query |
| + // input devices with this list and device returns list mentioning which |
| + // Keycodes out of these can be sent by a gamepad device. |
| + private static final int[] standardGamepadKeyCodes = { |
| + KeyEvent.KEYCODE_BUTTON_A, // Button 0 |
| + KeyEvent.KEYCODE_BUTTON_B, // Button 1 |
| + KeyEvent.KEYCODE_BUTTON_X, // Button 2 |
| + KeyEvent.KEYCODE_BUTTON_Y, // Button 3 |
| + KeyEvent.KEYCODE_BUTTON_L1, // Button 4 |
| + KeyEvent.KEYCODE_BUTTON_R1, // Button 5 |
| + KeyEvent.KEYCODE_BUTTON_L2, // Button 6 |
| + KeyEvent.KEYCODE_BUTTON_R2, // Button 7 |
| + KeyEvent.KEYCODE_BUTTON_SELECT, // Button 8 |
| + KeyEvent.KEYCODE_BUTTON_START, // Button 9 |
| + KeyEvent.KEYCODE_BUTTON_THUMBL, // Button 10 |
| + KeyEvent.KEYCODE_BUTTON_THUMBR, // Button 11 |
| + KeyEvent.KEYCODE_DPAD_UP, // Button 12 |
| + KeyEvent.KEYCODE_DPAD_DOWN, // Button 13 |
| + KeyEvent.KEYCODE_DPAD_LEFT, // Button 14 |
| + KeyEvent.KEYCODE_DPAD_RIGHT, // Button 15 |
| + KeyEvent.KEYCODE_BUTTON_MODE, // Button 16?? |
| + }; |
| + |
| + // Hash map of supported keycodes and their current value linearly |
| + // normalized to the range [0.0 .. 1.0]. |
| + // 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. |
| + protected LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap<Integer, Float>(); |
| + |
| + GamepadDevice(int index, InputDevice inputDevice) { |
| + mDeviceIndex = index; |
| + mDeviceId = inputDevice.getId(); |
| + mDeviceName = inputDevice.getName(); |
| + mTimestamp = SystemClock.uptimeMillis(); |
| + mMapping = " "; |
| + mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS]; |
| + mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES]; |
| + // Get the total number of axes supported by gamepad. |
| + int totalAxes = 0; |
| + for (MotionRange range : inputDevice.getMotionRanges()) { |
| + if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
| + totalAxes++; |
| + } |
| + } |
| + // Get axis ids and initialize axes values. |
| + mAxes = new int[totalAxes]; |
| + mRawAxes = new float[totalAxes]; |
| + int i = 0; |
| + for (MotionRange range : inputDevice.getMotionRanges()) { |
| + if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { |
| + mAxes[i] = range.getAxis(); |
| + mRawAxes[i] = 0.0f; |
| + i++; |
| + } |
| + } |
| + // Update supported buttons list. |
| + boolean[] deviceKeys = inputDevice.hasKeys(standardGamepadKeyCodes); |
| + for (int j = 0; j < standardGamepadKeyCodes.length; ++j) { |
| + if (deviceKeys[j]) { |
| + mKeyCodeValueMap.put(standardGamepadKeyCodes[j], 0.0f); |
| + } |
| + } |
| + mRawButtons = new float[mKeyCodeValueMap.size()]; |
| + } |
| + |
| + GamepadDevice() {} |
| + |
| + public void mapButtonsAndAxes() { |
| + int i = 0; |
| + Iterator itr = mKeyCodeValueMap.values().iterator(); |
| + while (itr.hasNext()) { |
| + mRawButtons[i++] = (Float)itr.next(); |
| + } |
| + mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsValues, mRawAxes, |
|
jdduke (slow)
2014/03/24 17:38:15
GamepadMappings.mapToStandardGamepad returns a Boo
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
I am settin
|
| + mRawButtons, |
| + mDeviceName) ? "standard" : " "; |
| + } |
| + |
| + public int getId() { return mDeviceId; } |
| + |
| + public String getMapping() { return mMapping; } |
| + |
| + public String getName() { return mDeviceName; } |
| + |
| + public int getIndex() { return mDeviceIndex; } |
| + |
| + public long getTimestamp() { return mTimestamp; } |
| + |
| + public float[] getAxes() { |
| + if (mMapping.equals("standard")) |
| + return mAxisValues; |
| + else |
| + return mRawAxes; |
| + } |
| + |
| + public float[] getButtons() { |
| + if (mMapping.equals("standard")) |
| + return mButtonsValues; |
| + else |
| + return mRawButtons; |
| + } |
| + |
| + public void clearData() { |
|
jdduke (slow)
2014/03/24 17:38:15
I'm going to led tedchoc@ or another Java speciali
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
SO, shall i
jdduke (slow)
2014/03/26 16:37:58
No, you should definitely comment this and all oth
SaurabhK
2014/03/27 13:32:31
On 2014/03/26 16:37:58, jdduke wrote:
Done.
|
| + Arrays.fill(mAxisValues, 0); |
| + Arrays.fill(mRawAxes, 0); |
| + Arrays.fill(mButtonsValues, 0); |
| + Arrays.fill(mRawButtons, 0); |
| + } |
| + |
| + public boolean updateForEvent(KeyEvent event) { |
| + mTimestamp = SystemClock.uptimeMillis(); |
|
jdduke (slow)
2014/03/24 17:38:15
Do you want really want to update mTimestamp if it
SaurabhK
2014/03/26 16:10:07
On 2014/03/24 17:38:15, jdduke wrote:
Changeing it
|
| + // Ignore event if it is not for standard gamepad key. |
| + int keyCode = event.getKeyCode(); |
| + if (mKeyCodeValueMap.get(keyCode) == null) { |
| + return false; |
| + } |
| + // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. |
| + if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| + mKeyCodeValueMap.put(keyCode, 1.0f); |
| + } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| + mKeyCodeValueMap.put(keyCode, 0.0f); |
| + } |
| + |
| + return true; |
| + } |
| + |
| + public boolean updateForEvent(MotionEvent event) { |
| + mTimestamp = SystemClock.uptimeMillis(); |
| + // Update axes values. |
| + for (int i = 0; i < mAxes.length; ++i) { |
| + mRawAxes[i] = event.getAxisValue(mAxes[i]); |
| + } |
| + return true; |
| + } |
| +} |