diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index cb203d5f5..8e238babc 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -1317,13 +1317,15 @@ __attribute__((visibility("hidden"))) static bool is_deck_hidraw_path(const char return pathname && fake_deck_enabled() && !strcmp(pathname, DECK_HIDRAW_PATH); } -// The app's own pads are the Deck now; their evdev nodes are withdrawn from everyone. +// The app's first pad is the Deck now: its evdev node is withdrawn from everyone. Further players' +// pads are left to the client alone, which hands them to games through Steam Input. __attribute__((visibility("hidden"))) static bool is_withdrawn_pad_path(const char *pathname) { if (!fake_deck_enabled() || !pathname || strncmp(pathname, "/dev/input/", 11)) return false; const char *event = strrchr(pathname, '/') + 1; if (strncmp(event, "event", 5) && strncmp(event, "js", 2)) return false; int number = get_event_number(event); - return number >= 0 && number < UINPUT_EVENT_BASE; + if (number < 0 || number >= UINPUT_EVENT_BASE) return false; + return number == 0 || !process_is_steam_client(); } static inline void put16(uint8_t *at, int value) { diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index cc79cbe15..be0757e3a 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -300,6 +300,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } override fun onInputDeviceRemoved(deviceId: Int) { Log.i(TAG, "input device $deviceId disconnected") + padBridge?.onDeviceRemoved(deviceId) updateOnScreenControls() updatePointerCapture() } diff --git a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java index 08328b5d3..f3bb7b9c5 100644 --- a/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java +++ b/app/src/main/java/com/droiddeck/launcher/input/PadBridge.java @@ -10,6 +10,8 @@ import android.view.MotionEvent; import com.droiddeck.launcher.session.SessionState; import java.io.File; +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicIntegerArray; /** * A physical controller, republished as a synthetic evdev device the Steam client can see. @@ -25,11 +27,14 @@ import java.io.File; * to the client instead ({@link com.droiddeck.launcher.session.SteamDeckPad}), which has a Quick * Access button of its own; an Xbox pad has none, so there the client's menu is reached with the * Guide-then-A chord the client itself answers. + * + *
Each further controller is another player: slots 1 to 3, an Xbox 360 pad each, which in a
+ * Steam session only the client reads (libfakeinput) and hands games through Steam Input.
*/
public final class PadBridge {
private static final String TAG = "PadBridge";
- /** Slot 0 is the one the session exports; extra slots would each show as another pad. */
- private static final int SLOT = 0;
+ /** Player slots: the session prepares a ring for each (SessionService). */
+ public static final int SLOTS = 4;
private static final float DEAD_ZONE = 0.12f;
// The client takes A as part of the chord only once it has had Guide held for a while, and a
// client starved of CPU needs longer. Too short, and it acts on A as well, selecting whatever it
@@ -50,8 +55,13 @@ public final class PadBridge {
chordHandler = new Handler(thread.getLooper());
}
+ private final File fakeInputDir;
private final FakeInputWriter writer;
private final PadState state = new PadState();
+ /** The input device id holding each slot, or {@link #NO_CONTROLLER}. */
+ private final int[] owners = new int[SLOTS];
+ private final FakeInputWriter[] playerWriters = new FakeInputWriter[SLOTS];
+ private final PadState[] playerStates = new PadState[SLOTS];
private final PadState effectiveState = new PadState();
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private boolean open;
@@ -79,29 +89,50 @@ public final class PadBridge {
/** No physical controller is driving the pad: nothing yet, or the on-screen controls were last. */
public static final int NO_CONTROLLER = -1;
/**
- * The Android input device id of the physical controller that last drove the pad, or
- * {@link #NO_CONTROLLER}. Every pad feeds the one exported slot, so this is how a game's rumble
- * on that slot finds the motors in the player's hands (RumbleComponent).
+ * The Android input device id of the physical controller that last drove any slot, or
+ * {@link #NO_CONTROLLER}: where rumble goes when it names no slot of ours (RumbleComponent).
*/
private static volatile int activeControllerId = NO_CONTROLLER;
+ /** Per slot, the controller its rumble belongs to; slot 0's is none while the on-screen controls lead. */
+ private static final AtomicIntegerArray slotControllers = new AtomicIntegerArray(SLOTS);
+ static {
+ for (int slot = 0; slot < SLOTS; slot++) slotControllers.set(slot, NO_CONTROLLER);
+ }
public static int activeControllerId() { return activeControllerId; }
+ /** The controller playing in this ring slot, or the last one used for any other slot. */
+ public static int controllerForSlot(int slot) {
+ return slot >= 0 && slot < SLOTS ? slotControllers.get(slot) : activeControllerId;
+ }
+
public PadBridge(File fakeInputDir) {
- writer = new FakeInputWriter(fakeInputDir.getAbsolutePath(), SLOT);
+ this.fakeInputDir = fakeInputDir;
+ writer = new FakeInputWriter(fakeInputDir.getAbsolutePath(), 0);
+ Arrays.fill(owners, NO_CONTROLLER);
+ for (int slot = 1; slot < SLOTS; slot++) playerStates[slot] = new PadState();
}
/** Opens the ring; safe to call more than once. */
public synchronized boolean start() {
if (!open) {
open = writer.open();
- Log.i(TAG, "ring slot " + SLOT + (open ? " open" : " NOT open"));
+ Log.i(TAG, "ring slot 0" + (open ? " open" : " NOT open"));
}
return open;
}
public synchronized void stop() {
activeControllerId = NO_CONTROLLER;
+ for (int slot = 0; slot < SLOTS; slot++) {
+ owners[slot] = NO_CONTROLLER;
+ slotControllers.set(slot, NO_CONTROLLER);
+ if (playerWriters[slot] != null) {
+ playerWriters[slot].destroy();
+ playerWriters[slot] = null;
+ }
+ if (playerStates[slot] != null) playerStates[slot].clear();
+ }
systemGuidePressed = false;
systemQamPressed = false;
qamChordActive = false;
@@ -155,38 +186,40 @@ public final class PadBridge {
/** @return true when the event was a pad button and has been consumed. */
public synchronized boolean onKeyEvent(KeyEvent event) {
- if (!isFromController(event.getDevice())) return false;
- noteDevice(event.getDevice());
+ if (!isFromController(event.getDevice()) || !isPadKey(event.getKeyCode())) return false;
+ int slot = slotFor(event.getDevice());
+ PadState pad = stateFor(slot);
+ noteDevice(event.getDevice(), slot);
boolean pressed = event.getAction() == KeyEvent.ACTION_DOWN;
switch (event.getKeyCode()) {
- case KeyEvent.KEYCODE_BUTTON_A: state.press(0, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_B: state.press(1, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_X: state.press(2, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_Y: state.press(3, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_L1: state.press(4, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_R1: state.press(5, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_A: pad.press(0, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_B: pad.press(1, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_X: pad.press(2, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_Y: pad.press(3, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_L1: pad.press(4, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_R1: pad.press(5, pressed); break;
case KeyEvent.KEYCODE_BUTTON_SELECT:
- case KeyEvent.KEYCODE_BACK: state.press(6, pressed); break;
+ case KeyEvent.KEYCODE_BACK: pad.press(6, pressed); break;
case KeyEvent.KEYCODE_BUTTON_START:
- case KeyEvent.KEYCODE_MENU: state.press(7, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_THUMBL: state.press(8, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_THUMBR: state.press(9, pressed); break;
+ case KeyEvent.KEYCODE_MENU: pad.press(7, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_THUMBL: pad.press(8, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_THUMBR: pad.press(9, pressed); break;
// The client's own in-game menu is opened by this one; the interposer publishes it as
// BTN_MODE, which SDL reports as the "guide" button.
case KeyEvent.KEYCODE_BUTTON_MODE:
- case KeyEvent.KEYCODE_HOME: state.press(PadState.GUIDE, pressed); break;
- case KeyEvent.KEYCODE_BUTTON_L2: state.leftTrigger = pressed ? 1f : 0f; break;
- case KeyEvent.KEYCODE_BUTTON_R2: state.rightTrigger = pressed ? 1f : 0f; break;
- case KeyEvent.KEYCODE_DPAD_UP: state.up = pressed; break;
- case KeyEvent.KEYCODE_DPAD_RIGHT: state.right = pressed; break;
- case KeyEvent.KEYCODE_DPAD_DOWN: state.down = pressed; break;
- case KeyEvent.KEYCODE_DPAD_LEFT: state.left = pressed; break;
+ case KeyEvent.KEYCODE_HOME: pad.press(PadState.GUIDE, pressed); break;
+ case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = pressed ? 1f : 0f; break;
+ case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = pressed ? 1f : 0f; break;
+ case KeyEvent.KEYCODE_DPAD_UP: pad.up = pressed; break;
+ case KeyEvent.KEYCODE_DPAD_RIGHT: pad.right = pressed; break;
+ case KeyEvent.KEYCODE_DPAD_DOWN: pad.down = pressed; break;
+ case KeyEvent.KEYCODE_DPAD_LEFT: pad.left = pressed; break;
default: return false;
}
if (pressed) notePlayerInput();
statButtons++;
scheduleStats();
- publish();
+ publish(slot);
return true;
}
@@ -194,33 +227,35 @@ public final class PadBridge {
public synchronized boolean onMotionEvent(MotionEvent event) {
if (!isFromController(event.getDevice())) return false;
if (event.getAction() != MotionEvent.ACTION_MOVE) return false;
- noteDevice(event.getDevice());
+ int slot = slotFor(event.getDevice());
+ PadState pad = stateFor(slot);
+ noteDevice(event.getDevice(), slot);
statAxes++;
scheduleStats();
- state.leftX = axis(event, MotionEvent.AXIS_X);
+ pad.leftX = axis(event, MotionEvent.AXIS_X);
// Android's Y axis grows downwards and evdev's ABS_Y does too, so no flip here: what the
// pad reports as "down" is what the client is told.
- state.leftY = axis(event, MotionEvent.AXIS_Y);
- state.rightX = axis(event, MotionEvent.AXIS_Z);
- state.rightY = axis(event, MotionEvent.AXIS_RZ);
+ pad.leftY = axis(event, MotionEvent.AXIS_Y);
+ pad.rightX = axis(event, MotionEvent.AXIS_Z);
+ pad.rightY = axis(event, MotionEvent.AXIS_RZ);
float lt = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
float rt = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
// Some pads only report the triggers on BRAKE/GAS.
if (lt == 0f) lt = event.getAxisValue(MotionEvent.AXIS_BRAKE);
if (rt == 0f) rt = event.getAxisValue(MotionEvent.AXIS_GAS);
- state.leftTrigger = lt;
- state.rightTrigger = rt;
+ pad.leftTrigger = lt;
+ pad.rightTrigger = rt;
float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
float hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y);
- state.up = hatY < -0.5f;
- state.right = hatX > 0.5f;
- state.down = hatY > 0.5f;
- state.left = hatX < -0.5f;
- if (Math.max(Math.max(Math.abs(state.leftX), Math.abs(state.leftY)), Math.max(Math.abs(state.rightX), Math.abs(state.rightY))) > 0.5f
- || lt > 0.5f || rt > 0.5f || state.up || state.right || state.down || state.left) {
+ pad.up = hatY < -0.5f;
+ pad.right = hatX > 0.5f;
+ pad.down = hatY > 0.5f;
+ pad.left = hatX < -0.5f;
+ 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) {
notePlayerInput();
}
- publish();
+ publish(slot);
return true;
}
@@ -231,6 +266,7 @@ public final class PadBridge {
public synchronized void applyTouch(java.util.function.Consumer