@Override public void pollInputDevices() { int[] deviceIds = InputDevice.getDeviceIds(); // It helps processing the device ids in reverse order // For example, in the case of the XBox 360 wireless dongle, // so the first controller seen by SDL matches what the receiver // considers to be the first controller for (int i = deviceIds.length - 1; i > -1; i--) { SDLJoystick joystick = getJoystick(deviceIds[i]); if (joystick == null) { joystick = new SDLJoystick(); InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]); if ((joystickDevice.getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { joystick.device_id = deviceIds[i]; joystick.name = joystickDevice.getName(); joystick.axes = new ArrayList<InputDevice.MotionRange>(); for (InputDevice.MotionRange range : joystickDevice.getMotionRanges()) { if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { joystick.axes.add(range); } } mJoysticks.add(joystick); SDLActivity.nativeAddJoystick( joystick.device_id, joystick.name, 0, -1, joystick.axes.size(), 0, 0); } } } /* Check removed devices */ ArrayList<Integer> removedDevices = new ArrayList<Integer>(); for (int i = 0; i < mJoysticks.size(); i++) { int device_id = mJoysticks.get(i).device_id; int j; for (j = 0; j < deviceIds.length; j++) { if (device_id == deviceIds[j]) break; } if (j == deviceIds.length) { removedDevices.add(device_id); } } for (int i = 0; i < removedDevices.size(); i++) { int device_id = removedDevices.get(i); SDLActivity.nativeRemoveJoystick(device_id); for (int j = 0; j < mJoysticks.size(); j++) { if (mJoysticks.get(j).device_id == device_id) { mJoysticks.remove(j); break; } } } }
/** @return an array which may be empty but is never null. */ public static int[] inputGetInputDeviceIds(int sources) { int[] ids = InputDevice.getDeviceIds(); int[] filtered = new int[ids.length]; int used = 0; for (int i = 0; i < ids.length; ++i) { InputDevice device = InputDevice.getDevice(ids[i]); if ((device != null) && ((device.getSources() & sources) != 0)) { filtered[used++] = device.getId(); } } return Arrays.copyOf(filtered, used); }