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.content.Context; |
| 8 import android.hardware.input.InputManager; |
| 9 import android.hardware.input.InputManager.InputDeviceListener; |
| 10 import android.view.InputDevice; |
| 11 import android.view.InputEvent; |
| 12 import android.view.KeyEvent; |
| 13 import android.view.MotionEvent; |
| 14 |
| 15 import org.chromium.base.CalledByNative; |
| 16 import org.chromium.base.JNINamespace; |
| 17 |
| 18 /* |
| 19 * Class to manage connected gamepad devices list. |
| 20 */ |
| 21 @JNINamespace("content") |
| 22 class GamepadList implements InputDeviceListener { |
| 23 private static final int MAX_GAMEPADS = 4; |
| 24 private GamepadDevice[] mGamepadDevices = new GamepadDevice[MAX_GAMEPADS]; |
| 25 protected boolean mHaveDevicesBeenInteractedWith = false; |
| 26 private boolean mIsGamepadAccessed = false; |
| 27 private InputManager mInputManager; |
| 28 // Native pointer to C++ GamepadReader object which will be set by CreateJav
aObject(). |
| 29 private long mNativeGamepadReader = 0; |
| 30 |
| 31 private GamepadList(Context context) { |
| 32 |
| 33 mInputManager = (InputManager) context.getSystemService(Context.INPUT_S
ERVICE); |
| 34 // Get list of all the attached input devices. |
| 35 int[] deviceIds = mInputManager.getInputDeviceIds(); |
| 36 |
| 37 for (int i = 0; i < deviceIds.length; i++) { |
| 38 InputDevice inputDevice = InputDevice.getDevice(deviceIds[i]); |
| 39 // Check for gamepad device |
| 40 if (isGamepadDevice(inputDevice)) { |
| 41 // Register a new gamepad device. |
| 42 registerGamepad(inputDevice); |
| 43 } |
| 44 } |
| 45 // Register an input device listener. |
| 46 mInputManager.registerInputDeviceListener(this, null); |
| 47 } |
| 48 |
| 49 private static GamepadList sGamepadList = null; |
| 50 |
| 51 // Override InputDeviceListener methods |
| 52 @Override |
| 53 public void onInputDeviceChanged(int deviceId) { |
| 54 } |
| 55 |
| 56 @Override |
| 57 public void onInputDeviceRemoved(int deviceId) { |
| 58 unregisterGamepad(deviceId); |
| 59 } |
| 60 |
| 61 @Override |
| 62 public void onInputDeviceAdded(int deviceId) { |
| 63 InputDevice inputDevice = InputDevice.getDevice(deviceId); |
| 64 if (isGamepadDevice(inputDevice)) |
| 65 registerGamepad(inputDevice); |
| 66 } |
| 67 |
| 68 public static GamepadList createInstance(Context context) { |
| 69 if (sGamepadList != null) { |
| 70 return sGamepadList; |
| 71 } else { |
| 72 sGamepadList = new GamepadList(context); |
| 73 return sGamepadList; |
| 74 } |
| 75 } |
| 76 |
| 77 private int getDeviceCount() { |
| 78 int count = 0; |
| 79 for (int i = 0; i < MAX_GAMEPADS; ++i) { |
| 80 if (getDevice(i) != null) { |
| 81 count++; |
| 82 } |
| 83 } |
| 84 return count; |
| 85 } |
| 86 |
| 87 private String getDeviceName(int index) { |
| 88 return getDevice(index).getName(); |
| 89 } |
| 90 |
| 91 private long getDeviceTimestamp(int index) { |
| 92 return getDevice(index).getTimestamp(); |
| 93 } |
| 94 |
| 95 private float[] getDeviceAxes(int index) { |
| 96 return getDevice(index).getAxes(); |
| 97 } |
| 98 |
| 99 private float[] getDeviceButtons(int index) { |
| 100 return getDevice(index).getButtons(); |
| 101 } |
| 102 |
| 103 private boolean isDeviceConnected(int index) { |
| 104 if (index < MAX_GAMEPADS && getDevice(index) != null) { |
| 105 return true; |
| 106 } |
| 107 return false; |
| 108 } |
| 109 |
| 110 private boolean isKnownGamepadLayout(int index) { |
| 111 return getDevice(index).isKnownGamepadLayout(); |
| 112 } |
| 113 |
| 114 public GamepadDevice getDeviceById(int deviceId) { |
| 115 for (int i = 0; i < MAX_GAMEPADS; ++i) { |
| 116 GamepadDevice gamepad = mGamepadDevices[i]; |
| 117 if (gamepad != null && gamepad.getId() == deviceId) { |
| 118 return gamepad; |
| 119 } |
| 120 } |
| 121 return null; |
| 122 } |
| 123 |
| 124 public GamepadDevice getDevice(int index) { |
| 125 // Maximum 4 Gamepads can be connected at a time starting at index zero. |
| 126 assert index >= 0 && index <= MAX_GAMEPADS - 1; |
| 127 return mGamepadDevices[index]; |
| 128 } |
| 129 |
| 130 public boolean updateForEvent(KeyEvent event) { |
| 131 GamepadDevice gamepad = getGamepadForEvent(event); |
| 132 if (gamepad != null) { |
| 133 mHaveDevicesBeenInteractedWith = true; |
| 134 return gamepad.updateForEvent(event); |
| 135 } |
| 136 return false; |
| 137 } |
| 138 |
| 139 public boolean updateForEvent(MotionEvent event) { |
| 140 GamepadDevice gamepad = getGamepadForEvent(event); |
| 141 if (gamepad != null) { |
| 142 mHaveDevicesBeenInteractedWith = true; |
| 143 return gamepad.updateForEvent(event); |
| 144 } |
| 145 return false; |
| 146 } |
| 147 |
| 148 protected int getNextAvailableIndex() { |
| 149 // When multiple gamepads are connected to a user agent, indices must be
assigned on a |
| 150 // first-come first-serve basis, starting at zero. If a gamepad is disco
nnected, previously |
| 151 // assigned indices must not be reassigned to gamepads that continue to
be connected. |
| 152 // However, if a gamepad is disconnected, and subsequently the same or a
different |
| 153 // gamepad is then connected, index entries must be reused. |
| 154 |
| 155 for (int i = 0; i < MAX_GAMEPADS; ++i) { |
| 156 if (getDevice(i) == null) { |
| 157 return i; |
| 158 } |
| 159 } |
| 160 // Reached maximum gamepads limit. |
| 161 return -1; |
| 162 } |
| 163 |
| 164 protected boolean registerGamepad(InputDevice inputDevice) { |
| 165 int index = getNextAvailableIndex(); |
| 166 if (index == -1) { |
| 167 return false; // invalid index |
| 168 } |
| 169 |
| 170 GamepadDevice gamepad = new GamepadDevice(index, inputDevice); |
| 171 mGamepadDevices[index] = gamepad; |
| 172 return true; |
| 173 } |
| 174 |
| 175 protected void unregisterGamepad(int deviceId) { |
| 176 GamepadDevice gamepadDevice = getDeviceById(deviceId); |
| 177 if (gamepadDevice == null) { |
| 178 return; // Not a registered device. |
| 179 } |
| 180 int index = gamepadDevice.getIndex(); |
| 181 mGamepadDevices[index] = null; |
| 182 } |
| 183 |
| 184 private boolean isGamepadDevice(InputDevice inputDevice) { |
| 185 int sources = inputDevice.getSources(); |
| 186 if ((sources & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JO
YSTICK ) { |
| 187 //Found gamepad device |
| 188 return true; |
| 189 } |
| 190 return false; |
| 191 } |
| 192 |
| 193 private GamepadDevice getGamepadForEvent(InputEvent event) { |
| 194 int deviceId = event.getDeviceId(); |
| 195 GamepadDevice gamepad = getDeviceById(deviceId); |
| 196 return gamepad; |
| 197 } |
| 198 |
| 199 public boolean isGamepadAccessed() { |
| 200 return mIsGamepadAccessed; |
| 201 } |
| 202 |
| 203 public boolean isGamepadEvent(MotionEvent event) { |
| 204 if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOU
RCE_JOYSTICK) { |
| 205 return true; |
| 206 } |
| 207 return false; |
| 208 } |
| 209 |
| 210 // Returns true if event's keycode corresponds to a gamepad key. |
| 211 // Specific handling for dpad keys is required because |
| 212 // KeyEvent.isGamepadButton doesn't consider dpad keys. |
| 213 public boolean isGamepadEvent(KeyEvent event) { |
| 214 int keyCode = event.getKeyCode(); |
| 215 switch (keyCode) { |
| 216 case KeyEvent.KEYCODE_DPAD_UP: |
| 217 case KeyEvent.KEYCODE_DPAD_DOWN: |
| 218 case KeyEvent.KEYCODE_DPAD_LEFT: |
| 219 case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 220 return true; |
| 221 default: |
| 222 return KeyEvent.isGamepadButton(keyCode); |
| 223 } |
| 224 } |
| 225 |
| 226 @CalledByNative |
| 227 private void getGamepadData() { |
| 228 int devicecount = getDeviceCount(); |
| 229 if (devicecount == 0 || !mHaveDevicesBeenInteractedWith) |
| 230 nativeUpdateDeviceCount(mNativeGamepadReader, 0); |
| 231 else |
| 232 nativeUpdateDeviceCount(mNativeGamepadReader, devicecount); |
| 233 for (int i = 0; i < MAX_GAMEPADS; i++) { |
| 234 if (isDeviceConnected(i)) |
| 235 nativeSetGamepadData(mNativeGamepadReader, i, true, getDevic
eName(i), |
| 236 getDeviceTimestamp(i), getDeviceAxes(i)
, |
| 237 getDeviceButtons(i)); |
| 238 else |
| 239 nativeSetGamepadData(mNativeGamepadReader, i, false, null, 0
, null, null); |
| 240 } |
| 241 } |
| 242 |
| 243 @CalledByNative |
| 244 public static GamepadList getInstance(long nativeGamepadReader) { |
| 245 if (sGamepadList != null) { |
| 246 sGamepadList.mNativeGamepadReader = nativeGamepadReader; |
| 247 return sGamepadList; |
| 248 } else { |
| 249 return null; |
| 250 } |
| 251 } |
| 252 |
| 253 @CalledByNative |
| 254 private void notifyForGamepadsAccess(boolean isaccesspaused) { |
| 255 mIsGamepadAccessed = !isaccesspaused; |
| 256 } |
| 257 |
| 258 private native void nativeSetGamepadData(long nativeGamepadsReader, |
| 259 int index, |
| 260 boolean connected, |
| 261 String devicename, |
| 262 long timestamp, |
| 263 float[] axes, |
| 264 float[] buttons); |
| 265 |
| 266 private native void nativeUpdateDeviceCount(long nativeGamepadsReader, int d
evicecount); |
| 267 |
| 268 } |
OLD | NEW |