Android: better readibility for pollInputDevices()

and move isDeviceSDLJoystick() test at first since there are
always non-joystick devices present.
This commit is contained in:
Sylvain Becker 2020-10-07 14:41:37 +02:00
parent 003a16980c
commit 20c070d869

View File

@ -177,13 +177,14 @@ class SDLJoystickHandler_API16 extends SDLJoystickHandler {
@Override @Override
public void pollInputDevices() { public void pollInputDevices() {
int[] deviceIds = InputDevice.getDeviceIds(); int[] deviceIds = InputDevice.getDeviceIds();
for(int i=0; i < deviceIds.length; ++i) {
SDLJoystick joystick = getJoystick(deviceIds[i]); for (int device_id : deviceIds) {
if (SDLControllerManager.isDeviceSDLJoystick(device_id)) {
SDLJoystick joystick = getJoystick(device_id);
if (joystick == null) { if (joystick == null) {
InputDevice joystickDevice = InputDevice.getDevice(deviceIds[i]); InputDevice joystickDevice = InputDevice.getDevice(device_id);
if (SDLControllerManager.isDeviceSDLJoystick(deviceIds[i])) {
joystick = new SDLJoystick(); joystick = new SDLJoystick();
joystick.device_id = deviceIds[i]; joystick.device_id = device_id;
joystick.name = joystickDevice.getName(); joystick.name = joystickDevice.getName();
joystick.desc = getJoystickDescriptor(joystickDevice); joystick.desc = getJoystickDescriptor(joystickDevice);
joystick.axes = new ArrayList<InputDevice.MotionRange>(); joystick.axes = new ArrayList<InputDevice.MotionRange>();
@ -193,51 +194,55 @@ class SDLJoystickHandler_API16 extends SDLJoystickHandler {
Collections.sort(ranges, new RangeComparator()); Collections.sort(ranges, new RangeComparator());
for (InputDevice.MotionRange range : ranges) { for (InputDevice.MotionRange range : ranges) {
if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) { if ((range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
if (range.getAxis() == MotionEvent.AXIS_HAT_X || if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
range.getAxis() == MotionEvent.AXIS_HAT_Y) {
joystick.hats.add(range); joystick.hats.add(range);
} } else {
else {
joystick.axes.add(range); joystick.axes.add(range);
} }
} }
} }
mJoysticks.add(joystick); mJoysticks.add(joystick);
SDLControllerManager.nativeAddJoystick(joystick.device_id, joystick.name, joystick.desc, getVendorId(joystickDevice), getProductId(joystickDevice), false, getButtonMask(joystickDevice), joystick.axes.size(), joystick.hats.size()/2, 0); SDLControllerManager.nativeAddJoystick(joystick.device_id, joystick.name, joystick.desc,
getVendorId(joystickDevice), getProductId(joystickDevice), false,
getButtonMask(joystickDevice), joystick.axes.size(), joystick.hats.size()/2, 0);
} }
} }
} }
/* Check removed devices */ /* Check removed devices */
ArrayList<Integer> removedDevices = new ArrayList<Integer>(); ArrayList<Integer> removedDevices = null;
for(int i=0; i < mJoysticks.size(); i++) { for (SDLJoystick joystick : mJoysticks) {
int device_id = mJoysticks.get(i).device_id; int device_id = joystick.device_id;
int j; int i;
for (j=0; j < deviceIds.length; j++) { for (i = 0; i < deviceIds.length; i++) {
if (device_id == deviceIds[j]) break; if (device_id == deviceIds[i]) break;
} }
if (j == deviceIds.length) { if (i == deviceIds.length) {
removedDevices.add(Integer.valueOf(device_id)); if (removedDevices == null) {
removedDevices = new ArrayList<Integer>();
}
removedDevices.add(device_id);
} }
} }
for(int i=0; i < removedDevices.size(); i++) { if (removedDevices != null) {
int device_id = removedDevices.get(i).intValue(); for (int device_id : removedDevices) {
SDLControllerManager.nativeRemoveJoystick(device_id); SDLControllerManager.nativeRemoveJoystick(device_id);
for (int j=0; j < mJoysticks.size(); j++) { for (int i = 0; i < mJoysticks.size(); i++) {
if (mJoysticks.get(j).device_id == device_id) { if (mJoysticks.get(i).device_id == device_id) {
mJoysticks.remove(j); mJoysticks.remove(i);
break; break;
} }
} }
} }
} }
}
protected SDLJoystick getJoystick(int device_id) { protected SDLJoystick getJoystick(int device_id) {
for(int i=0; i < mJoysticks.size(); i++) { for (SDLJoystick joystick : mJoysticks) {
if (mJoysticks.get(i).device_id == device_id) { if (joystick.device_id == device_id) {
return mJoysticks.get(i); return joystick;
} }
} }
return null; return null;