Merge latest upstream main before Controller UI integration

# Conflicts:
#	app/src/main/java/com/droiddeck/launcher/SessionActivity.kt
#	app/src/main/java/com/droiddeck/launcher/input/OnScreenControls.kt
#	app/src/main/java/com/droiddeck/launcher/input/PadBridge.java
#	app/src/main/java/com/droiddeck/launcher/ui/DeckControlsPanel.kt
#	app/src/test/java/com/droiddeck/launcher/input/PadBridgeQamTest.kt
This commit is contained in:
Delete228 committed 2026-11-06 14:37:50 +03:00
commit ba5876bf6d
360 files changed
+34610 -1088

No files matched your search

@@ -22,6 +22,9 @@ object ControllerPrefs {
val sizes = listOf(80, 90, 100, 110, 125)
val deadZones = listOf(0, 5, 8, 12, 15, 20, 25)
const val DEFAULT_DEAD_ZONE_PERCENT = 12
val mappable = listOf(
"a" to R.string.ctrl_button_a, "b" to R.string.ctrl_button_b, "x" to R.string.ctrl_button_x, "y" to R.string.ctrl_button_y,
"lb" to R.string.ctrl_button_lb, "rb" to R.string.ctrl_button_rb, "lt" to R.string.ctrl_button_lt, "rt" to R.string.ctrl_button_rt,
@@ -51,6 +54,7 @@ object ControllerPrefs {
val snap: Boolean,
val customLayout: Boolean,
val mapping: Map<String, String>,
val deadZone: Int,
)
internal fun prefs(context: Context) = context.getSharedPreferences("controller", Context.MODE_PRIVATE)
@@ -72,6 +76,7 @@ object ControllerPrefs {
snap = p.getBoolean("snap", true),
customLayout = p.all.keys.any { it.startsWith("layout.") },
mapping = mappable.associate { (id, _) -> id to target(context, id) },
deadZone = deadZone(context),
)
}
@@ -109,6 +114,13 @@ object ControllerPrefs {
fun rumbleEnabled(context: Context): Boolean = prefs(context).getBoolean("rumble", true)
fun deadZone(context: Context): Int =
prefs(context).getInt("deadZone", DEFAULT_DEAD_ZONE_PERCENT).takeIf { it in deadZones } ?: DEFAULT_DEAD_ZONE_PERCENT
fun setDeadZone(context: Context, percent: Int) {
prefs(context).edit().putInt("deadZone", percent).apply()
}
fun setRumble(context: Context, on: Boolean) {
prefs(context).edit().putBoolean("rumble", on).apply()
}
@@ -0,0 +1,54 @@
package com.droiddeck.launcher.input
import android.view.InputDevice
import android.view.KeyCharacterMap
import android.view.KeyEvent
/**
* Some firmware turns a mouse's right button into Back. Most report it from the mouse itself; REDMAGIC's
* cast mode injects it from no device and without the virtual-hard-key flag that the navigation bar and
* back gesture set, so that form only counts while a mouse is connected.
*/
class MouseBackInput(private val sendRight: (Boolean) -> Unit, private val mouseAttached: () -> Boolean) {
private val held = mutableSetOf<Int>()
fun onKeyEvent(event: KeyEvent, toGuest: Boolean): Boolean {
if (event.keyCode != KeyEvent.KEYCODE_BACK) return false
val device = event.deviceId
val fromMouse = isFromMouse(event)
val injected = isInjectedBack(event)
if (device in held && (fromMouse || injected)) {
if (event.action == KeyEvent.ACTION_UP) {
held.remove(device)
if (held.isEmpty()) sendRight(false)
}
return true
}
if (event.action != KeyEvent.ACTION_DOWN || event.repeatCount > 0 || !toGuest) return false
if (!fromMouse && !(injected && mouseAttached())) return false
if (held.isEmpty()) sendRight(true)
held.add(device)
return true
}
fun cancel() {
if (held.isEmpty()) return
held.clear()
sendRight(false)
}
fun onDeviceRemoved(deviceId: Int) {
if (held.isEmpty()) return
held.remove(deviceId)
if (!mouseAttached()) held.remove(KeyCharacterMap.VIRTUAL_KEYBOARD)
if (held.isEmpty()) sendRight(false)
}
companion object {
fun isFromMouse(event: KeyEvent): Boolean =
event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)
fun isInjectedBack(event: KeyEvent): Boolean =
event.deviceId == KeyCharacterMap.VIRTUAL_KEYBOARD && (event.flags and KeyEvent.FLAG_VIRTUAL_HARD_KEY) == 0
}
}
@@ -550,6 +550,7 @@ class OnScreenControls(
control.ax = if (adaptive) event.getX(index) else control.cx
control.ay = if (adaptive) event.getY(index) else control.cy
control.drag(event.getX(index), event.getY(index))
control.dirty = true
}
if (SessionPrefs.lowLatency(context)) requestUnbufferedDispatch(event)
apply()
@@ -694,15 +695,18 @@ class OnScreenControls(
private fun adaptiveStickAt(x: Float, y: Float): Control? {
if (!settings.adaptiveSticks || editing || buttonsOnly || quickHidden) return null
if (x < safe.left || x >= width - safe.right || y < safe.top || y >= height - safe.bottom) return null
if (controls.any { it.stick < 0 && isVisible(it) && it.contains(x, y, it.radius, 5f) }) return null
return controls.filter {
val dx = x - it.cx
val dy = y - it.cy
val reach = it.radius * 1.4f * sqrt(1.5f)
it.stick >= 0 && isVisible(it) && it.pressedBy == -1 && dx * dx + dy * dy <= reach * reach
}.minByOrNull {
val dx = x - it.cx
val dy = y - it.cy
if (controls.any { control ->
control.stick < 0 && isVisible(control) && control.contains(x, y, control.radius, 5f)
}) return null
return controls.filter { control ->
val dx = if (control.stick == 0) max(0f, x - control.cx) else x - control.cx
val dy = y - control.cy
val reach = control.radius * 1.4f * sqrt(1.5f)
control.stick >= 0 && isVisible(control) && control.pressedBy == -1 &&
dx * dx + dy * dy <= reach * reach
}.minByOrNull { control ->
val dx = x - control.cx
val dy = y - control.cy
dx * dx + dy * dy
}
}
@@ -737,7 +741,10 @@ class OnScreenControls(
bridge.setSystemButtons(controls.any { it.id == "guide" && it.pressedBy != -1 }, qamHeld)
} else {
bridge.setSystemButtons(false, qamHeld)
bridge.applyTouch(this, InputRole.PRIMARY) { state ->
val stickWrites = controls.filter { it.stick >= 0 && it.dirty }.fold(0) { mask, control ->
mask or if (control.stick == 0) PadBridge.LEFT_STICK else PadBridge.RIGHT_STICK
}
bridge.applyTouch(this, InputRole.PRIMARY, stickWrites) { state ->
val used = HashSet<String>()
val held = HashSet<String>()
for (control in controls) {
@@ -17,7 +17,10 @@ import java.util.concurrent.atomic.AtomicIntegerArray;
public final class PadBridge {
private static final String TAG = "PadBridge";
public static final int SLOTS = 4;
public static final int LEFT_STICK = 1;
public static final int RIGHT_STICK = 2;
private static final float DEAD_ZONE = 0.12f;
private float stickDeadZone = DEAD_ZONE;
private static final int[] JOYSTICK_AXES = {
MotionEvent.AXIS_X, MotionEvent.AXIS_Y, MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ,
MotionEvent.AXIS_RX, MotionEvent.AXIS_RY,
@@ -52,6 +55,8 @@ public final class PadBridge {
private volatile java.util.function.IntPredicate excluded = id -> false;
private final FakeInputWriter[] playerWriters = new FakeInputWriter[SLOTS];
private final PadState[] playerStates = new PadState[SLOTS];
private final float[][] rawSticks = new float[SLOTS][4];
private final int[] retainedSticks = new int[SLOTS];
private final PadState effectiveState = new PadState();
private final PadState homeGuideReport = new PadState();
private final PadState combinedState = new PadState();
@@ -203,7 +208,7 @@ public final class PadBridge {
chordButtonPressed = false;
qamChordGeneration++;
chordHandler.removeCallbacksAndMessages(null);
state.clear();
clearPlayerState(0);
publish();
}
@@ -240,7 +245,7 @@ public final class PadBridge {
playerWriters[slot].destroy();
playerWriters[slot] = null;
}
if (playerStates[slot] != null) playerStates[slot].clear();
clearPlayerState(slot);
}
systemGuidePressed = false;
systemQamPressed = false;
@@ -250,7 +255,7 @@ public final class PadBridge {
qamTapPressed = false;
qamChordGeneration++;
if (open) {
state.clear();
clearPlayerState(0);
writer.writePad(state);
writer.close();
open = false;
@@ -415,6 +420,65 @@ public final class PadBridge {
return true;
}
public synchronized void setStickDeadZone(int percent) {
float value = Math.max(0, Math.min(25, percent)) / 100f;
if (stickDeadZone == value) return;
stickDeadZone = value;
for (int slot = 0; slot < SLOTS; slot++) {
if (retainedSticks[slot] == 0) continue;
int owner = owners[slot], partner = partners[slot];
if (owner != NO_CONTROLLER && (InputDevice.getDevice(owner) == null || excluded.test(owner))) leave(slot, owner);
if (partner != NO_CONTROLLER && (InputDevice.getDevice(partner) == null || excluded.test(partner))) leave(slot, partner);
if (retainedSticks[slot] == 0) continue;
projectSticks(slot);
publish(slot);
}
}
private void retainStick(int slot, int side, float x, float y) {
rawSticks[slot][side * 2] = x;
rawSticks[slot][side * 2 + 1] = y;
retainedSticks[slot] |= 1 << side;
}
private void projectSticks(int slot) {
PadState pad = stateFor(slot);
for (int side = 0; side < 2; side++) {
if ((retainedSticks[slot] & (1 << side)) == 0) continue;
float x = rawSticks[slot][side * 2];
float y = rawSticks[slot][side * 2 + 1];
boolean centred = inStickDeadZone(x, y, stickDeadZone);
if (side == JoyCon.LEFT) {
pad.leftX = centred ? 0f : x;
pad.leftY = centred ? 0f : y;
} else {
pad.rightX = centred ? 0f : x;
pad.rightY = centred ? 0f : y;
}
}
}
private void clearPlayerState(int slot) {
stateFor(slot).clear();
retainedSticks[slot] = 0;
Arrays.fill(rawSticks[slot], 0f);
}
private void mutateState(java.util.function.Consumer<PadState> mutation) {
float lx = state.leftX, ly = state.leftY, rx = state.rightX, ry = state.rightY;
mutation.accept(state);
if (state.leftX != lx || state.leftY != ly) retainedSticks[0] &= ~1;
if (state.rightX != rx || state.rightY != ry) retainedSticks[0] &= ~2;
}
static boolean inStickDeadZone(float x, float y, float deadZone) {
return x * x + y * y < deadZone * deadZone;
}
private static boolean hasChordStickInput(float x, float y) {
return Math.max(Math.abs(x), Math.abs(y)) >= DEAD_ZONE;
}
public synchronized boolean onMotionEvent(MotionEvent event) {
if (!isControllerMotionEvent(event) || excluded.test(event.getDeviceId())) return false;
if (event.getAction() != MotionEvent.ACTION_MOVE) return false;
@@ -429,26 +493,25 @@ public final class PadBridge {
int side = JoyCon.side(device);
if (side >= 0) {
int[] stick = JoyCon.stickAxes(side, device, event.getSource());
float x = axis(event, stick[0]);
float y = axis(event, stick[1]);
if (side == JoyCon.LEFT) {
pad.leftX = x;
pad.leftY = y;
} else {
pad.rightX = x;
pad.rightY = y;
}
if (x != 0f || y != 0f) noteChordInput(slot);
float x = event.getAxisValue(stick[0]);
float y = event.getAxisValue(stick[1]);
boolean chordStickInput = hasChordStickInput(x, y);
retainStick(slot, side, x, y);
projectSticks(slot);
if (chordStickInput) noteChordInput(slot);
if (Math.max(Math.abs(x), Math.abs(y)) > 0.5f) notePlayerInput();
publish(slot);
InputTiming.end(timing);
return true;
}
pad.leftX = axis(event, MotionEvent.AXIS_X);
pad.leftY = axis(event, MotionEvent.AXIS_Y);
float lx = event.getAxisValue(MotionEvent.AXIS_X);
float ly = event.getAxisValue(MotionEvent.AXIS_Y);
boolean wired = wiredXboxLayout(event);
pad.rightX = rightX(event, wired);
pad.rightY = rightY(event, wired);
float rx = event.getAxisValue(wired ? MotionEvent.AXIS_RX : MotionEvent.AXIS_Z);
float ry = event.getAxisValue(wired ? MotionEvent.AXIS_RY : MotionEvent.AXIS_RZ);
retainStick(slot, JoyCon.LEFT, lx, ly);
retainStick(slot, JoyCon.RIGHT, rx, ry);
projectSticks(slot);
int source = event.getSource();
float lt = 0f;
float rt = 0f;
@@ -470,7 +533,7 @@ public final class PadBridge {
pad.up = hatY < -0.5f;
pad.down = hatY > 0.5f;
}
if (pad.leftX != 0f || pad.leftY != 0f || pad.rightX != 0f || pad.rightY != 0f
if (hasChordStickInput(lx, ly) || hasChordStickInput(rx, ry)
|| lt > 0f || rt > 0f || pad.up || pad.right || pad.down || pad.left) noteChordInput(slot);
if (Math.max(Math.max(Math.abs(pad.leftX), Math.abs(pad.leftY)), Math.max(Math.abs(pad.rightX), Math.abs(pad.rightY))) > 0.5f
|| lt > 0.5f || rt > 0.5f || pad.up || pad.right || pad.down || pad.left) {
@@ -500,10 +563,18 @@ public final class PadBridge {
}
public synchronized void applyTouch(java.util.function.Consumer<PadState> mutation) {
applyTouch(defaultTouchSource, InputRole.PRIMARY, mutation);
applyTouch(defaultTouchSource, InputRole.PRIMARY, 0, mutation);
}
public synchronized void applyTouch(int stickWrites, java.util.function.Consumer<PadState> mutation) {
applyTouch(defaultTouchSource, InputRole.PRIMARY, stickWrites, mutation);
}
public synchronized void applyTouch(Object owner, InputRole role, java.util.function.Consumer<PadState> mutation) {
applyTouch(owner, role, 0, mutation);
}
public synchronized void applyTouch(Object owner, InputRole role, int stickWrites, java.util.function.Consumer<PadState> mutation) {
if (!builtinPad) return;
long timing = InputTiming.begin();
TouchSource source = touchSources.get(owner);
@@ -512,7 +583,18 @@ public final class PadBridge {
if (inputRouter != null) source.route = inputRouter.pad(role);
touchSources.put(owner, source);
}
float lx = source.raw.leftX, ly = source.raw.leftY, rx = source.raw.rightX, ry = source.raw.rightY;
mutation.accept(source.raw);
if (inputRouter == null && (touchRoute == null || !touchRoute.active())) {
if ((stickWrites & LEFT_STICK) != 0 || source.raw.leftX != lx || source.raw.leftY != ly) {
retainedSticks[0] &= ~LEFT_STICK;
state.leftX = state.leftY = 0f;
}
if ((stickWrites & RIGHT_STICK) != 0 || source.raw.rightX != rx || source.raw.rightY != ry) {
retainedSticks[0] &= ~RIGHT_STICK;
state.rightX = state.rightY = 0f;
}
}
activeControllerId = NO_CONTROLLER;
slotControllers.set(0, NO_CONTROLLER);
notePlayerInput();
@@ -530,7 +612,7 @@ public final class PadBridge {
}
public synchronized void applyExternal(java.util.function.Consumer<PadState> mutation, boolean changed) {
mutation.accept(state);
mutateState(mutation);
if (changed) {
activeControllerId = NO_CONTROLLER;
notePlayerInput();
@@ -566,7 +648,7 @@ public final class PadBridge {
if (inputRouter == null) for (TouchSource source : touchSources.values()) source.raw.clear();
routedTouch.clear();
DeckControls.INSTANCE.suspend();
if (inputRouter == null) state.clear();
if (inputRouter == null) clearPlayerState(0);
homeGuidePressed = false;
homeGuideSlot = -1;
clearBackQamWrites();
@@ -574,8 +656,8 @@ public final class PadBridge {
if (inputRouter == null) releaseBackPaddles();
publish();
for (int slot = 1; slot < SLOTS; slot++) {
if (inputRouter == null) clearPlayerState(slot);
if (playerWriters[slot] == null) continue;
if (inputRouter == null) playerStates[slot].clear();
publish(slot);
}
}
@@ -615,7 +697,7 @@ public final class PadBridge {
if (!homeGuidePressed) return;
clearBackQamWrites();
homeGuidePressed = false;
if (inputRouter == null) state.clear();
if (inputRouter == null) clearPlayerState(0);
else {
inputRouter.releaseAll();
DeckControls.INSTANCE.suspend();
@@ -642,7 +724,7 @@ public final class PadBridge {
writeBackQamReport(modifier, modifierAt);
}
if (!pressed && !backQamPressed && !homeGuidePressed) {
backQamAxesBlocked = hasAxes(liveReport);
backQamAxesBlocked = hasQamAxes();
if (!backQamWrites.isEmpty()) {
PadState neutral = new PadState();
neutral.copyFrom(lastWrittenReport);
@@ -658,7 +740,7 @@ public final class PadBridge {
if (!backQamPressed && backQamWrites.isEmpty()) return;
clearBackQamWrites();
backQamPressed = false;
if (inputRouter == null) state.clear();
if (inputRouter == null) clearPlayerState(0);
else {
inputRouter.releaseAll();
DeckControls.INSTANCE.suspend();
@@ -699,15 +781,18 @@ public final class PadBridge {
private void writePad(PadState report) {
liveReport.copyFrom(report);
if (backQamAxesBlocked && !hasAxes(liveReport)) backQamAxesBlocked = false;
if (backQamAxesBlocked && !hasQamAxes()) backQamAxesBlocked = false;
long at = android.os.SystemClock.uptimeMillis();
if (backQamPressed || homeGuidePressed || !backQamWrites.isEmpty()) at += backQamDelayMs;
writeBackQamReport(report, at);
}
private static boolean hasAxes(PadState pad) {
return pad.leftX != 0f || pad.leftY != 0f || pad.rightX != 0f || pad.rightY != 0f
|| pad.leftTrigger != 0f || pad.rightTrigger != 0f;
private boolean hasQamAxes() {
boolean left = (retainedSticks[0] & 1) != 0 && liveReport.leftX == state.leftX && liveReport.leftY == state.leftY
? hasChordStickInput(rawSticks[0][0], rawSticks[0][1]) : liveReport.leftX != 0f || liveReport.leftY != 0f;
boolean right = (retainedSticks[0] & 2) != 0 && liveReport.rightX == state.rightX && liveReport.rightY == state.rightY
? hasChordStickInput(rawSticks[0][2], rawSticks[0][3]) : liveReport.rightX != 0f || liveReport.rightY != 0f;
return left || right || liveReport.leftTrigger != 0f || liveReport.rightTrigger != 0f;
}
private static void clearAxes(PadState pad) {
@@ -845,7 +930,7 @@ public final class PadBridge {
slotJoyCons.set(slot * 2 + 1 - side, owner.getId());
if (slot == firstPlayerSlot()) releaseBackPaddles();
if (slot == 0) clearBackQamWrites();
stateFor(slot).clear();
clearPlayerState(slot);
if (slot == 0) publish();
Log.i(TAG, "\"" + device.getName() + "\" (id " + id + ") joins \"" + owner.getName() + "\" as player " + (slot + 1));
return slot;
@@ -893,7 +978,7 @@ public final class PadBridge {
if (slotControllers.get(slot) == id) slotControllers.set(slot, owners[slot]);
if (activeControllerId == id) activeControllerId = owners[slot];
if (slot == firstPlayerSlot()) releaseBackPaddles();
stateFor(slot).clear();
clearPlayerState(slot);
publish(slot);
}
@@ -912,11 +997,11 @@ public final class PadBridge {
if (slot == homeGuideSlot) homeGuideSlot = -1;
if (slot == firstPlayerSlot()) releaseBackPaddles();
if (slot == 0) {
state.clear();
clearPlayerState(0);
publish();
return;
}
playerStates[slot].clear();
clearPlayerState(slot);
if (playerWriters[slot] != null) {
playerWriters[slot].destroy();
playerWriters[slot] = null;
@@ -1035,14 +1120,6 @@ public final class PadBridge {
&& device.getMotionRange(MotionEvent.AXIS_RZ, event.getSource()) != null;
}
public static float rightX(MotionEvent event, boolean wired) {
return axis(event, wired ? MotionEvent.AXIS_RX : MotionEvent.AXIS_Z);
}
public static float rightY(MotionEvent event, boolean wired) {
return axis(event, wired ? MotionEvent.AXIS_RY : MotionEvent.AXIS_RZ);
}
public static float leftTrigger(MotionEvent event, boolean wired) {
if (wired) return Math.max(0f, Math.min(1f, (event.getAxisValue(MotionEvent.AXIS_Z) + 1f) / 2f));
float value = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
@@ -1055,8 +1132,4 @@ public final class PadBridge {
return value != 0f ? value : event.getAxisValue(MotionEvent.AXIS_GAS);
}
private static float axis(MotionEvent event, int axis) {
float value = event.getAxisValue(axis);
return Math.abs(value) < DEAD_ZONE ? 0f : value;
}
}
@@ -90,8 +90,8 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
val wired = wiredLayout.getOrPut(event.deviceId) { PadBridge.wiredXboxLayout(event) }
sticks.leftX = event.getAxisValue(MotionEvent.AXIS_X)
sticks.leftY = event.getAxisValue(MotionEvent.AXIS_Y)
sticks.rightX = PadBridge.rightX(event, wired)
sticks.rightY = PadBridge.rightY(event, wired)
sticks.rightX = scrollAxis(event, if (wired) MotionEvent.AXIS_RX else MotionEvent.AXIS_Z)
sticks.rightY = scrollAxis(event, if (wired) MotionEvent.AXIS_RY else MotionEvent.AXIS_RZ)
if (hasTriggerAxes(event)) {
sticks.leftTrigger = PadBridge.leftTrigger(event, wired)
sticks.rightTrigger = PadBridge.rightTrigger(event, wired)
@@ -295,6 +295,7 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
}
companion object {
private const val SCROLL_AXIS_DEAD_ZONE = 0.12f
private const val DEAD_ZONE = 0.15f
private const val STICK_NAVIGATE = 0.5f
private const val REPEAT_FIRST_MS = 300L
@@ -348,6 +349,9 @@ class PadMouse(private val listener: Listener, private val clock: () -> Long = S
return scaled * scaled / length
}
private fun scrollAxis(event: MotionEvent, axis: Int): Float =
event.getAxisValue(axis).let { if (abs(it) < SCROLL_AXIS_DEAD_ZONE) 0f else it }
private fun source(deviceId: Int, kind: Int): Int = deviceId * 2 + kind
@@ -0,0 +1,20 @@
package com.droiddeck.launcher.input
import android.graphics.RectF
/**
* The area of the session view that drives the guest pointer.
*
* The view is also the compositor output only while it shows the picture: the phone, or the
* bottom screen. With the glasses owning the output the view is a touchpad for a scene drawn
* elsewhere, so the whole view has to drive the pointer. Using the picture rect there would
* confine it to the letterboxed band the scene would occupy on the phone, and the pointer could
* not reach the scene edges.
*/
object PointerSpace {
fun area(viewWidth: Int, viewHeight: Int, glassesOwnOutput: Boolean, picture: RectF?): RectF? {
if (viewWidth <= 0 || viewHeight <= 0) return null
if (glassesOwnOutput) return RectF(0f, 0f, viewWidth.toFloat(), viewHeight.toFloat())
return picture
}
}