OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content.browser; | |
6 | |
7 import android.os.SystemClock; | |
8 import android.view.InputDevice; | |
9 import android.view.InputDevice.MotionRange; | |
10 import android.view.KeyEvent; | |
11 import android.view.MotionEvent; | |
12 | |
13 import java.util.Iterator; | |
14 import java.util.LinkedHashMap; | |
15 | |
16 /* | |
17 * Class to manage information related to each connected gamepad device. | |
18 */ | |
19 | |
20 class GamepadDevice { | |
21 // An id for the gamepad. | |
22 protected int mDeviceId; | |
23 // The index of the gamepad in the Navigator. | |
24 protected int mDeviceIndex; | |
25 // Last time the data for this gamepad was updated. | |
26 protected long mTimestamp; | |
27 // If this gamepad is mapped to standard gamepad? | |
28 protected String mMapping; | |
29 | |
30 // Array of values for all axes of the gamepad. | |
31 // All axis values must be linearly normalized to the range [-1.0 .. 1.0]. | |
32 // As appropriate, -1.0 should correspond to "up" or "left", and 1.0 | |
33 // should correspond to "down" or "right". | |
34 protected float[] mAxisValues; | |
35 | |
36 protected float[] mButtonsValues; | |
37 | |
38 // When the user agent recognizes the attached inputDevice, it is recommende d | |
39 // that it be remapped to a canonical ordering when possible. Devices that a re | |
40 // not recognized should still be exposed in their raw form. Therefore we mu st | |
41 // pass the raw Button and raw Axis values. | |
42 protected float[] mRawButtons; | |
43 protected float[] mRawAxes; | |
44 | |
45 // An identification string for the gamepad. | |
46 protected String mDeviceName; | |
47 | |
48 // Array of axes ids. | |
49 private int[] mAxes; | |
50 | |
51 // List of keycodes that can be sent by a standard gamepad. Standard gamepad can have | |
52 // buttons as per the spec: | |
53 // https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html#remapping | |
54 // Need to maintain this list to get input device capabilities. We query | |
55 // input devices with this list and device returns list mentioning which | |
56 // Keycodes out of these can be sent by a gamepad device. | |
57 private static final int[] standardGamepadKeyCodes = { | |
58 KeyEvent.KEYCODE_BUTTON_A, // Button 0 | |
59 KeyEvent.KEYCODE_BUTTON_B, // Button 1 | |
60 KeyEvent.KEYCODE_BUTTON_X, // Button 2 | |
61 KeyEvent.KEYCODE_BUTTON_Y, // Button 3 | |
62 KeyEvent.KEYCODE_BUTTON_L1, // Button 4 | |
63 KeyEvent.KEYCODE_BUTTON_R1, // Button 5 | |
64 KeyEvent.KEYCODE_BUTTON_L2, // Button 6 | |
65 KeyEvent.KEYCODE_BUTTON_R2, // Button 7 | |
66 KeyEvent.KEYCODE_BUTTON_SELECT, // Button 8 | |
67 KeyEvent.KEYCODE_BUTTON_START, // Button 9 | |
68 KeyEvent.KEYCODE_BUTTON_THUMBL, // Button 10 | |
69 KeyEvent.KEYCODE_BUTTON_THUMBR, // Button 11 | |
70 KeyEvent.KEYCODE_DPAD_UP, // Button 12 | |
71 KeyEvent.KEYCODE_DPAD_DOWN, // Button 13 | |
72 KeyEvent.KEYCODE_DPAD_LEFT, // Button 14 | |
73 KeyEvent.KEYCODE_DPAD_RIGHT, // Button 15 | |
74 KeyEvent.KEYCODE_BUTTON_MODE, // Button 16?? | |
75 }; | |
76 | |
77 // Hash map of supported keycodes and their current value linearly | |
78 // normalized to the range [0.0 .. 1.0]. | |
79 // 0.0 must mean fully unpressed, and 1.0 must mean fully pressed. | |
80 protected LinkedHashMap<Integer, Float> mKeyCodeValueMap = new LinkedHashMap <Integer, Float>(); | |
81 | |
82 GamepadDevice(int index, InputDevice inputDevice) { | |
83 mDeviceIndex = index; | |
84 mDeviceId = inputDevice.getId(); | |
85 mDeviceName = inputDevice.getName(); | |
86 mTimestamp = SystemClock.uptimeMillis(); | |
87 mMapping = " "; | |
88 mButtonsValues = new float[GamepadMappings.StandardButtons.MAX_BUTTONS]; | |
89 mAxisValues = new float[GamepadMappings.StandardAxes.MAX_AXES]; | |
90 // Get the total number of axes supported by gamepad. | |
91 int totalAxes = 0; | |
92 for (MotionRange range : inputDevice.getMotionRanges()) { | |
93 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
94 totalAxes++; | |
95 } | |
96 } | |
97 // Get axis ids and initialize axes values. | |
98 mAxes = new int[totalAxes]; | |
99 mRawAxes = new float[totalAxes]; | |
100 int i = 0; | |
101 for (MotionRange range : inputDevice.getMotionRanges()) { | |
102 if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { | |
103 mAxes[i] = range.getAxis(); | |
104 mRawAxes[i] = 0.0f; | |
105 i++; | |
106 } | |
107 } | |
108 // Update supported buttons list. | |
109 boolean[] deviceKeys = inputDevice.hasKeys(standardGamepadKeyCodes); | |
110 for (int j = 0; j < standardGamepadKeyCodes.length; ++j) { | |
111 if (deviceKeys[j]) { | |
112 mKeyCodeValueMap.put(standardGamepadKeyCodes[j], 0.0f); | |
113 } | |
114 } | |
115 mRawButtons = new float[mKeyCodeValueMap.size()]; | |
116 } | |
117 | |
118 GamepadDevice() {} | |
119 | |
120 public void mapButtonsAndAxes() { | |
121 int i = 0; | |
122 Iterator itr = mKeyCodeValueMap.values().iterator(); | |
123 while (itr.hasNext()) { | |
124 mRawButtons[i++] = (Float)itr.next(); | |
125 } | |
126 mMapping = GamepadMappings.mapToStandardGamepad(mAxisValues, mButtonsVal ues, mRawAxes, | |
127 mRawButtons, | |
128 mDeviceName) ? "standard " : " "; | |
129 } | |
130 | |
131 public int getId() { return mDeviceId; } | |
132 | |
133 public String getMapping() { return mMapping; } | |
134 | |
135 public String getName() { return mDeviceName; } | |
136 | |
137 public int getIndex() { return mDeviceIndex; } | |
138 | |
139 public long getTimestamp() { return mTimestamp; } | |
140 | |
141 public float[] getAxes() { | |
142 if (mMapping.equals("standard")) | |
143 return mAxisValues; | |
144 else | |
145 return mRawAxes; | |
146 } | |
147 | |
148 public float[] getButtons() { | |
149 if (mMapping.equals("standard")) | |
150 return mButtonsValues; | |
151 else | |
152 return mRawButtons; | |
153 } | |
154 | |
155 public boolean updateForEvent(KeyEvent event) { | |
156 mTimestamp = SystemClock.uptimeMillis(); | |
157 // Ignore event if it is not for standard gamepad key. | |
158 int keyCode = event.getKeyCode(); | |
159 if (mKeyCodeValueMap.get(keyCode) == null) { | |
160 return true; | |
jdduke (slow)
2014/03/19 09:44:58
Why is this true and not false?
SaurabhK
2014/03/19 13:15:52
On 2014/03/19 09:44:58, jdduke wrote:
Oops, i thi
| |
161 } | |
162 // Button value 0.0 must mean fully unpressed, and 1.0 must mean fully p ressed. | |
163 if (event.getAction() == KeyEvent.ACTION_DOWN) { | |
164 mKeyCodeValueMap.put(keyCode, 1.0f); | |
165 } else if (event.getAction() == KeyEvent.ACTION_UP) { | |
166 mKeyCodeValueMap.put(keyCode, 0.0f); | |
167 } | |
168 | |
169 return true; | |
170 } | |
171 | |
172 public boolean updateForEvent(MotionEvent event) { | |
173 mTimestamp = SystemClock.uptimeMillis(); | |
174 // Update axes values. | |
175 for (int i = 0; i < mAxes.length; ++i) { | |
176 mRawAxes[i] = event.getAxisValue(mAxes[i]); | |
177 } | |
178 return true; | |
179 } | |
180 } | |
OLD | NEW |