Prevent a device to be added/deleted more than once on Android

cherry-picked from fb5a601217
This commit is contained in:
Xavier Sellier 2018-04-05 10:15:57 -04:00 committed by George Marques
parent 6cc116d415
commit 7dcf779a8b
No known key found for this signature in database
GPG Key ID: 046BD46A3201E43D
1 changed files with 100 additions and 74 deletions

View File

@ -204,48 +204,65 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
return i; return i;
} }
} }
onInputDeviceAdded(device_id);
return joy_devices.size() - 1; return -1;
} }
@Override @Override
public void onInputDeviceAdded(int deviceId) { public void onInputDeviceAdded(int deviceId) {
joystick joy = new joystick(); int id = find_joy_device(deviceId);
joy.device_id = deviceId;
final int id = joy_devices.size(); // Check if the device has not been already added
InputDevice device = mInputManager.getInputDevice(deviceId); if (id < 0) {
final String name = device.getName(); InputDevice device = mInputManager.getInputDevice(deviceId);
joy.name = device.getName();
joy.axes = new ArrayList<InputDevice.MotionRange>(); id = joy_devices.size();
joy.hats = new ArrayList<InputDevice.MotionRange>();
List<InputDevice.MotionRange> ranges = device.getMotionRanges(); joystick joy = new joystick();
Collections.sort(ranges, new RangeComparator()); joy.device_id = deviceId;
for (InputDevice.MotionRange range : ranges) { joy.name = device.getName();
if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) { joy.axes = new ArrayList<InputDevice.MotionRange>();
joy.hats.add(range); joy.hats = new ArrayList<InputDevice.MotionRange>();
} else {
joy.axes.add(range); List<InputDevice.MotionRange> ranges = device.getMotionRanges();
Collections.sort(ranges, new RangeComparator());
for (InputDevice.MotionRange range : ranges) {
if (range.getAxis() == MotionEvent.AXIS_HAT_X || range.getAxis() == MotionEvent.AXIS_HAT_Y) {
joy.hats.add(range);
} else {
joy.axes.add(range);
}
} }
joy_devices.add(joy);
final int device_id = id;
final String name = joy.name;
queueEvent(new Runnable() {
@Override
public void run() {
GodotLib.joyconnectionchanged(device_id, true, name);
}
});
} }
joy_devices.add(joy);
queueEvent(new Runnable() {
@Override
public void run() {
GodotLib.joyconnectionchanged(id, true, name);
}
});
} }
@Override @Override
public void onInputDeviceRemoved(int deviceId) { public void onInputDeviceRemoved(int deviceId) {
final int id = find_joy_device(deviceId); final int device_id = find_joy_device(deviceId);
joy_devices.remove(id);
queueEvent(new Runnable() { // Check if the evice has not been already removed
@Override if (device_id > -1) {
public void run() { joy_devices.remove(device_id);
GodotLib.joyconnectionchanged(id, false, "");
} queueEvent(new Runnable() {
}); @Override
public void run() {
GodotLib.joyconnectionchanged(device_id, false, "");
}
});
}
} }
@Override @Override
@ -266,15 +283,18 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
if ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK || (source & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD || (source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) { if ((source & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK || (source & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD || (source & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
final int button = get_godot_button(keyCode); final int button = get_godot_button(keyCode);
final int device = find_joy_device(event.getDeviceId()); final int device_id = find_joy_device(event.getDeviceId());
queueEvent(new Runnable() { // Check if the device exists
@Override if (device_id > -1) {
public void run() { queueEvent(new Runnable() {
GodotLib.joybutton(device, button, false); @Override
} public void run() {
}); GodotLib.joybutton(device_id, button, false);
return true; }
});
return true;
}
} else { } else {
final int chr = event.getUnicodeChar(0); final int chr = event.getUnicodeChar(0);
queueEvent(new Runnable() { queueEvent(new Runnable() {
@ -284,6 +304,7 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
} }
}); });
}; };
return super.onKeyUp(keyCode, event); return super.onKeyUp(keyCode, event);
}; };
@ -308,18 +329,20 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
if (event.getRepeatCount() > 0) // ignore key echo if (event.getRepeatCount() > 0) // ignore key echo
return true; return true;
final int button = get_godot_button(keyCode); final int button = get_godot_button(keyCode);
final int device = find_joy_device(event.getDeviceId()); final int device_id = find_joy_device(event.getDeviceId());
//Log.e(TAG, String.format("joy button down! button %x, %d, device %d", keyCode, button, device));
queueEvent(new Runnable() {
@Override
public void run() {
GodotLib.joybutton(device, button, true);
}
});
return true;
// Check if the device exists
if (device_id > -1) {
queueEvent(new Runnable() {
@Override
public void run() {
GodotLib.joybutton(device_id, button, true);
}
});
return true;
}
} else { } else {
final int chr = event.getUnicodeChar(0); final int chr = event.getUnicodeChar(0);
queueEvent(new Runnable() { queueEvent(new Runnable() {
@ -329,6 +352,7 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
} }
}); });
}; };
return super.onKeyDown(keyCode, event); return super.onKeyDown(keyCode, event);
} }
@ -338,33 +362,35 @@ public class GodotView extends GLSurfaceView implements InputDeviceListener {
if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && event.getAction() == MotionEvent.ACTION_MOVE) { if ((event.getSource() & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK && event.getAction() == MotionEvent.ACTION_MOVE) {
final int device_id = find_joy_device(event.getDeviceId()); final int device_id = find_joy_device(event.getDeviceId());
joystick joy = joy_devices.get(device_id);
for (int i = 0; i < joy.axes.size(); i++) { // Check if the device exists
InputDevice.MotionRange range = joy.axes.get(i); if (device_id > -1) {
final float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f; joystick joy = joy_devices.get(device_id);
//Log.e(TAG, String.format("axis event: %d, value %f", i, value));
final int idx = i;
queueEvent(new Runnable() {
@Override
public void run() {
GodotLib.joyaxis(device_id, idx, value);
}
});
}
for (int i = 0; i < joy.hats.size(); i += 2) { for (int i = 0; i < joy.axes.size(); i++) {
final int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis())); InputDevice.MotionRange range = joy.axes.get(i);
final int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis())); final float value = (event.getAxisValue(range.getAxis()) - range.getMin()) / range.getRange() * 2.0f - 1.0f;
//Log.e(TAG, String.format("HAT EVENT %d, %d", hatX, hatY)); final int idx = i;
queueEvent(new Runnable() { queueEvent(new Runnable() {
@Override @Override
public void run() { public void run() {
GodotLib.joyhat(device_id, hatX, hatY); GodotLib.joyaxis(device_id, idx, value);
} }
}); });
}
for (int i = 0; i < joy.hats.size(); i += 2) {
final int hatX = Math.round(event.getAxisValue(joy.hats.get(i).getAxis()));
final int hatY = Math.round(event.getAxisValue(joy.hats.get(i + 1).getAxis()));
queueEvent(new Runnable() {
@Override
public void run() {
GodotLib.joyhat(device_id, hatX, hatY);
}
});
}
return true;
} }
return true;
}; };
return super.onGenericMotionEvent(event); return super.onGenericMotionEvent(event);