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..d94dc016a4c4ebad9215d33ef9b8a6b67cb01ca8 |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/GamepadDevice.java |
| @@ -0,0 +1,177 @@ |
| +// 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; |
|
Ted C
2014/04/04 18:52:18
for all these gamepage files, can you move them in
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + |
| +import android.os.SystemClock; |
| +import android.view.InputDevice; |
| +import android.view.InputDevice.MotionRange; |
| +import android.view.KeyEvent; |
| +import android.view.MotionEvent; |
| + |
| +import org.chromium.content.browser.input.CanonicalAxisIndex; |
| +import org.chromium.content.browser.input.CanonicalButtonIndex; |
| + |
| +import java.util.Arrays; |
| + |
| +/* |
|
Ted C
2014/04/04 18:52:18
for javadocs, the starting token is /**
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + * Class to manage information related to each connected gamepad device. |
| + */ |
| + |
|
Ted C
2014/04/04 18:52:18
remove blank line
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| +class GamepadDevice { |
| + // An id for the gamepad. |
| + private int mDeviceId; |
| + // The index of the gamepad in the Navigator. |
| + private int mDeviceIndex; |
| + // Last time the data for this gamepad was updated. |
| + private long mTimestamp; |
| + // If this gamepad is mapped to standard gamepad? |
| + private 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". |
| + private float[] mAxisValues; |
| + |
| + private 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. |
| + private float[] mRawButtons; |
| + private float[] mRawAxes; |
| + |
| + // An identification string for the gamepad. |
| + private String mDeviceName; |
| + |
| + // Array of axes ids. |
| + private int[] mAxes; |
| + |
| + GamepadDevice(int index, InputDevice inputDevice) { |
| + mDeviceIndex = index; |
| + mDeviceId = inputDevice.getId(); |
| + mDeviceName = inputDevice.getName(); |
| + mTimestamp = SystemClock.uptimeMillis(); |
| + mMapping = " "; |
|
Ted C
2014/04/04 18:52:18
can you make " " and "standard" constants.
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Now i am kee
|
| + mButtonsValues = new float[CanonicalButtonIndex.NUM_CANONICAL_BUTTONS]; |
| + mAxisValues = new float[CanonicalAxisIndex.NUM_CANONICAL_AXES]; |
| + mRawButtons = new float[256]; |
| + // 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++; |
| + } |
| + } |
| + } |
| + |
| + GamepadDevice() {} |
|
Ted C
2014/04/04 18:52:18
why do you need the empty constructor?
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Removed it.
|
| + |
| + /** |
| + * Map the axes and buttons of a gamepad device to a standard gamepad format. |
|
Ted C
2014/04/04 18:52:18
for these javadoc comments and below, the seond an
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + */ |
| + public void mapButtonsAndAxes() { |
| + mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsValues, mRawAxes, |
|
Ted C
2014/04/04 18:52:18
style nit
in java, the indenting is different tha
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Keeping all
|
| + mRawButtons, |
| + mDeviceName) ? "standard" : " "; |
| + } |
| + |
| + /** |
| + * @return Device Id of the gamepad device. |
| + */ |
| + public int getId() { return mDeviceId; } |
| + |
| + /** |
| + * @return Mapping status of the gamepad device. |
| + */ |
| + public String getMapping() { return mMapping; } |
| + |
| + /** |
| + * @return Device name of the gamepad device. |
| + */ |
| + public String getName() { return mDeviceName; } |
| + |
| + /** |
| + * @return Device index of the gamepad device. |
| + */ |
| + public int getIndex() { return mDeviceIndex; } |
| + |
| + /** |
| + * @return The timestamp when the gamepad device was last interacted. |
| + */ |
| + public long getTimestamp() { return mTimestamp; } |
| + |
| + /** |
| + * @return The axes state of the gamepad device. |
| + */ |
| + public float[] getAxes() { |
|
Ted C
2014/04/04 18:52:18
for consistency with the ones above, these two sho
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + return mAxisValues; |
| + } |
| + |
| + /** |
| + * @return The buttons state of the gamepad device. |
| + */ |
| + public float[] getButtons() { |
| + return mButtonsValues; |
| + |
|
Ted C
2014/04/04 18:52:18
remove extra blank line
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + } |
| + |
| + /** |
| + * Reset the axes and buttons data of the gamepad device everytime gamepad data access is |
| + * paused. |
| + */ |
| + public void clearData() { |
| + Arrays.fill(mAxisValues, 0); |
| + Arrays.fill(mRawAxes, 0); |
| + Arrays.fill(mButtonsValues, 0); |
| + Arrays.fill(mRawButtons, 0); |
| + } |
| + |
| + /** |
| + * Handles key event from the gamepad device. |
| + * @return True if the key event from the gamepad device has been consumed. |
| + */ |
| + public boolean updateForEvent(KeyEvent event) { |
|
Ted C
2014/04/04 18:52:18
for consistency with android, I would call this ha
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + // Ignore event if it is not for standard gamepad key. |
| + if (!GamepadList.isGamepadEvent(event)) return false; |
| + int keyCode = event.getKeyCode(); |
| + assert keyCode < 256; |
| + // 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; |
| + } else if (event.getAction() == KeyEvent.ACTION_UP) { |
| + mRawButtons[keyCode] = 0.0f; |
| + } |
| + mTimestamp = event.getEventTime(); |
| + |
| + return true; |
| + } |
| + |
| + /** |
| + * Handles motion event from the gamepad device. |
| + * @return True if the motion event from the gamepad device has been consumed. |
| + */ |
| + public boolean updateForEvent(MotionEvent event) { |
| + // Update axes values. |
|
Ted C
2014/04/04 18:52:18
does this need a corresponding isGamepadEvent like
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Added a chec
|
| + for (int i = 0; i < mAxes.length; ++i) { |
|
Ted C
2014/04/04 18:52:18
i++ in java
SaurabhK
2014/04/11 14:41:42
On 2014/04/04 18:52:18, Ted C wrote:
Done.
|
| + mRawAxes[i] = event.getAxisValue(mAxes[i]); |
| + } |
| + mTimestamp = event.getEventTime(); |
| + return true; |
| + } |
| + |
| +} |