mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
feat: Epic Games Launcher for Linux (#928)
* feat: Epic Games Launcher for Linux Install Epic's official Linux launcher (EGLv2) from Epic's CDN with Valve's Steam Runtime 4 and run it under FEX, signed in with the linked Epic account. Epic games launch through a hidden launcher instance so they get the launcher's own arguments, with the exchange-code launch as the fallback, and DroidDeck installs show up in the launcher as installed. In the launcher, touch scrolls like a phone app and the gamepad drives the UI: D-pad and left stick move the selection, A opens, B goes back, the right stick and triggers scroll proportionally, X opens the options menu, Y the keyboard and LB/RB switch tabs. * feat: Epic Games Launcher for Linux Install Epic's official Linux launcher (EGLv2) from Epic's CDN with Valve's Steam Runtime 4 and run it under FEX, signed in with the linked Epic account. Epic games launch through a hidden launcher instance so they get the launcher's own arguments, with the exchange-code launch as the fallback, and DroidDeck installs show up in the launcher as installed. In the launcher, touch scrolls like a phone app and the gamepad drives the UI: D-pad and left stick move the selection, A opens, B goes back, the right stick and triggers scroll proportionally, X opens the options menu, Y the keyboard and LB/RB switch tabs. * Route Epic games through Steam, import launcher installs, offer the second library - Every Epic launch goes Steam -> Epic Games Launcher -> game; Play inside the open launcher hands the game to Steam - Games installed from inside the launcher get a Games-tab entry and launch through Steam; the launcher installs to the last Stores target - Stores install picker offers the Steam second library, with side-by-side choices - uruntime xattr-only failures no longer fail the launcher unpack (app-extract) * Hand launcher Play and Steam links to Steam, wait for FEX on first run Play on a game the launcher just installed is matched to its install record and handed to Steam. Steam games listed in the launcher open steam://rungameid links, which now start the game in a Steam session. Opening the launcher before Steam has downloaded FEX asks Steam for it and starts Steam instead of failing with no-fex. * Address review: FEX check, play-in-Steam action, touch route thread, scroll scale fexReady needs a FEX binary in Steam's FEX tool, not just its folder. The play-in-Steam action stays until the link is handled, so Run Steam at startup can't start a session first. The pad touch route reads the mouse mode without side effects off the main thread and posts the pad update there. Vertical scroll uses the output height. A status-2 unpack counts only when AppRun is there. The xdg-open command is tested end to end for Steam links. * Open the launcher through Steam, recover dropped shortcuts, sign out of both The Epic Games Launcher now runs as its own Steam shortcut, so opening it starts Steam, which opens the launcher, and Play inside it goes to the running Steam. Steam dropped a running shortcut from its library when the game exited after a sleep/wake replay or a server re-logon, and launching it then waited forever at ShowInterstitials. login1 only replays sleep/wake when Steam is asleep. The session restarts Steam when a requested shortcut is missing, and when a game exits with its shortcut gone and nothing else runs. Resuming after a long pause no longer reports failure while traced processes are still waking, so "Steam is paused" goes away on the first tap. Signing out of the launcher signs DroidDeck's Epic store out, and signing out in DroidDeck restarts the launcher signed out and clears the Epic web cookies. * Address review: one session-switch poll, Epic setup can't block a session A new switch confirmation replaces the pending poll instead of adding a second one that could start the session again. Epic library sync and install-folder setup at session start are each guarded, so a bad preference no longer stops the guest from launching. * Uninstall the Epic Games Launcher from the Games tab An Uninstall button next to Launch asks first, closes a running session, and removes the launcher and its runtime. The launcher's sign-in and install records stay, so its games keep their Games-tab entries and a reinstall comes back signed in. Its Steam shortcut goes away on the next Steam start. * Address review: uninstall only after the session has ended The launcher's files are removed only once the session is not running and its phase has ended; a shutdown still in progress after the wait reports failure instead. The confirmation reads the session state from the front-end state. * Find FEX that Steam installed on the second library Steam's tools on a second library live in DroidDeck's private copy, which the session binds over the library. The launcher's FEX check only looked on the shared storage, so with FEX there it always asked Steam for FEX and only Steam opened. It now checks the private copy too. * Start games without Steam, per-game Settings menu with Wine sync "Start without Steam" now really leaves the Steam client out: the game runs in its own Proton/FEX through droiddeck-proton-run in a run session, and the Epic Games Launcher opens on its own with its games playing in it. Run sessions for these games are not pinned to the big cores and get DirectAudio and the added-games list like Steam. The Games tab card is now "Settings", titled "<game> Settings": Start without Steam, FEX preset, Wine sync (ntsync, fsync, esync, wineserver or the app default) and Default, which puts everything changed for the game back to the app's settings. The app-wide Wine sync choice is shown in the Steam settings Session tab. * Address review: uninstall guard, routed games, shortcut matching Opening or reinstalling the Epic Games Launcher waits while it is being uninstalled. Added games that are Steam installs keep their Start without Steam setting under their Steam app id and start as that Steam game. Live shortcut lookups wait for Steam's library to load and match a same-name shortcut by its executable, so no duplicate is added. The session treats the open launcher as itself even when it could not tell its Steam app id. * Address review: drop a queued launcher switch when it is uninstalled A confirmed switch to the Epic Games Launcher that is still waiting for the old session to end is cancelled when the launcher is uninstalled, and never starts while the uninstall runs. The launcher id test sets its environment for its own runs only.
This commit is contained in:
1 parent
16f9916825
commit
2fe4d5303c
107 files changed
+8860
-371
No files matched your search
@@ -30,6 +30,7 @@ public final class PadBridge {
|
||||
KeyEvent.KEYCODE_BUTTON_THUMBL, KeyEvent.KEYCODE_BUTTON_THUMBR,
|
||||
KeyEvent.KEYCODE_BUTTON_MODE, KeyEvent.KEYCODE_BUTTON_L2, KeyEvent.KEYCODE_BUTTON_R2
|
||||
};
|
||||
private static final int[] ROUTED_PASS_THROUGH = {PadState.GUIDE, PadState.QAM, PadState.SELECT};
|
||||
private static final long QAM_GUIDE_LEAD_MIN_MS = 80;
|
||||
private static final long QAM_GUIDE_LEAD_MAX_MS = 1500;
|
||||
private static final int QAM_GUIDE_LEAD_FRAMES = 8;
|
||||
@@ -54,6 +55,8 @@ public final class PadBridge {
|
||||
private final Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private boolean open;
|
||||
private volatile boolean selectSteam;
|
||||
private TouchRoute touchRoute;
|
||||
private final PadState routedTouch = new PadState();
|
||||
private boolean systemGuidePressed;
|
||||
private boolean systemQamPressed;
|
||||
private boolean qamChordActive;
|
||||
@@ -313,26 +316,11 @@ public final class PadBridge {
|
||||
}
|
||||
pad.leftX = axis(event, MotionEvent.AXIS_X);
|
||||
pad.leftY = axis(event, MotionEvent.AXIS_Y);
|
||||
boolean wiredXboxLayout = device != null
|
||||
&& ((device.getVendorId() == 0x20d6 && device.getProductId() == 0x2063)
|
||||
|| (device.getVendorId() == 0x0e6f && device.getProductId() == 0x02f0))
|
||||
&& device.getMotionRange(MotionEvent.AXIS_RX, event.getSource()) != null
|
||||
&& device.getMotionRange(MotionEvent.AXIS_RY, event.getSource()) != null
|
||||
&& device.getMotionRange(MotionEvent.AXIS_Z, event.getSource()) != null
|
||||
&& device.getMotionRange(MotionEvent.AXIS_RZ, event.getSource()) != null;
|
||||
pad.rightX = axis(event, wiredXboxLayout ? MotionEvent.AXIS_RX : MotionEvent.AXIS_Z);
|
||||
pad.rightY = axis(event, wiredXboxLayout ? MotionEvent.AXIS_RY : MotionEvent.AXIS_RZ);
|
||||
float lt;
|
||||
float rt;
|
||||
if (wiredXboxLayout) {
|
||||
lt = Math.max(0f, Math.min(1f, (event.getAxisValue(MotionEvent.AXIS_Z) + 1f) / 2f));
|
||||
rt = Math.max(0f, Math.min(1f, (event.getAxisValue(MotionEvent.AXIS_RZ) + 1f) / 2f));
|
||||
} else {
|
||||
lt = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
|
||||
rt = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
|
||||
if (lt == 0f) lt = event.getAxisValue(MotionEvent.AXIS_BRAKE);
|
||||
if (rt == 0f) rt = event.getAxisValue(MotionEvent.AXIS_GAS);
|
||||
}
|
||||
boolean wired = wiredXboxLayout(event);
|
||||
pad.rightX = rightX(event, wired);
|
||||
pad.rightY = rightY(event, wired);
|
||||
float lt = leftTrigger(event, wired);
|
||||
float rt = rightTrigger(event, wired);
|
||||
pad.leftTrigger = lt;
|
||||
pad.rightTrigger = rt;
|
||||
float hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X);
|
||||
@@ -349,7 +337,36 @@ public final class PadBridge {
|
||||
return true;
|
||||
}
|
||||
|
||||
public interface TouchRoute {
|
||||
boolean active();
|
||||
|
||||
void accept(PadState state);
|
||||
}
|
||||
|
||||
public synchronized void setTouchRoute(TouchRoute route) {
|
||||
touchRoute = route;
|
||||
routedTouch.clear();
|
||||
}
|
||||
|
||||
public synchronized void applyTouch(java.util.function.Consumer<PadState> mutation) {
|
||||
if (touchRoute != null && touchRoute.active()) {
|
||||
mutation.accept(routedTouch);
|
||||
touchRoute.accept(routedTouch);
|
||||
boolean changed = false;
|
||||
for (int button : ROUTED_PASS_THROUGH) {
|
||||
boolean down = routedTouch.isDown(button);
|
||||
if (state.isDown(button) == down) continue;
|
||||
state.press(button, down);
|
||||
changed = true;
|
||||
}
|
||||
if (changed) {
|
||||
activeControllerId = NO_CONTROLLER;
|
||||
slotControllers.set(0, NO_CONTROLLER);
|
||||
publish();
|
||||
}
|
||||
return;
|
||||
}
|
||||
routedTouch.clear();
|
||||
mutation.accept(state);
|
||||
activeControllerId = NO_CONTROLLER;
|
||||
slotControllers.set(0, NO_CONTROLLER);
|
||||
@@ -380,6 +397,7 @@ public final class PadBridge {
|
||||
}
|
||||
|
||||
public synchronized void releaseAll() {
|
||||
routedTouch.clear();
|
||||
releaseBackPaddles();
|
||||
state.clear();
|
||||
publish();
|
||||
@@ -607,6 +625,37 @@ public final class PadBridge {
|
||||
writer.writePad(effectiveState);
|
||||
}
|
||||
|
||||
public static boolean wiredXboxLayout(MotionEvent event) {
|
||||
InputDevice device = event.getDevice();
|
||||
return device != null
|
||||
&& ((device.getVendorId() == 0x20d6 && device.getProductId() == 0x2063)
|
||||
|| (device.getVendorId() == 0x0e6f && device.getProductId() == 0x02f0))
|
||||
&& device.getMotionRange(MotionEvent.AXIS_RX, event.getSource()) != null
|
||||
&& device.getMotionRange(MotionEvent.AXIS_RY, event.getSource()) != null
|
||||
&& device.getMotionRange(MotionEvent.AXIS_Z, event.getSource()) != null
|
||||
&& 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);
|
||||
return value != 0f ? value : event.getAxisValue(MotionEvent.AXIS_BRAKE);
|
||||
}
|
||||
|
||||
public static float rightTrigger(MotionEvent event, boolean wired) {
|
||||
if (wired) return Math.max(0f, Math.min(1f, (event.getAxisValue(MotionEvent.AXIS_RZ) + 1f) / 2f));
|
||||
float value = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.os.SystemClock
|
||||
import android.view.Choreographer
|
||||
import android.view.KeyEvent
|
||||
import android.view.MotionEvent
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.hypot
|
||||
|
||||
class PadMouse(private val listener: Listener, private val clock: () -> Long = SystemClock::uptimeMillis) {
|
||||
interface Listener {
|
||||
fun active(): Boolean
|
||||
fun onScrollStart()
|
||||
fun onScroll(dx: Float, dy: Float)
|
||||
fun onKey(evdev: Int, pressed: Boolean)
|
||||
fun onKeyboard()
|
||||
fun onNavigate()
|
||||
fun scrollSpeed(): Float
|
||||
}
|
||||
|
||||
private class Sticks {
|
||||
var leftX = 0f
|
||||
var leftY = 0f
|
||||
var rightX = 0f
|
||||
var rightY = 0f
|
||||
var leftTrigger = 0f
|
||||
var rightTrigger = 0f
|
||||
|
||||
fun clear() {
|
||||
leftX = 0f
|
||||
leftY = 0f
|
||||
rightX = 0f
|
||||
rightY = 0f
|
||||
leftTrigger = 0f
|
||||
rightTrigger = 0f
|
||||
}
|
||||
}
|
||||
|
||||
private val choreographer = Choreographer.getInstance()
|
||||
private val physical = HashMap<Int, Sticks>()
|
||||
private val touch = Sticks()
|
||||
private val total = Sticks()
|
||||
private val triggerAxes = HashMap<Int, Boolean>()
|
||||
private val wiredLayout = HashMap<Int, Boolean>()
|
||||
private val down = HashMap<Int, MutableSet<Int>>()
|
||||
private val sent = HashMap<Int, IntArray>()
|
||||
private val held = LinkedHashMap<Int, Int>()
|
||||
private var scrolling = false
|
||||
private var stickDirection = NONE
|
||||
private var stickRepeatAt = 0L
|
||||
private var frameAt = 0L
|
||||
private var running = false
|
||||
|
||||
private val frame = object : Choreographer.FrameCallback {
|
||||
override fun doFrame(frameTimeNanos: Long) {
|
||||
if (!running) return
|
||||
if (!listener.active()) {
|
||||
reset()
|
||||
return
|
||||
}
|
||||
val dt = if (frameAt == 0L) 1f / 60f else ((frameTimeNanos - frameAt) / 1e9f).coerceIn(0f, 0.05f)
|
||||
frameAt = frameTimeNanos
|
||||
step(dt)
|
||||
if (moving()) choreographer.postFrameCallback(this) else stop()
|
||||
}
|
||||
}
|
||||
|
||||
fun onKey(event: KeyEvent): Boolean {
|
||||
if (event.keyCode == KeyEvent.KEYCODE_BUTTON_L2 || event.keyCode == KeyEvent.KEYCODE_BUTTON_R2) {
|
||||
if (event.repeatCount == 0 && !hasTriggerAxes(event)) {
|
||||
val sticks = physical.getOrPut(event.deviceId) { Sticks() }
|
||||
val value = if (event.action == KeyEvent.ACTION_DOWN) 1f else 0f
|
||||
if (event.keyCode == KeyEvent.KEYCODE_BUTTON_L2) sticks.leftTrigger = value else sticks.rightTrigger = value
|
||||
wake()
|
||||
}
|
||||
return true
|
||||
}
|
||||
val button = BUTTONS[event.keyCode] ?: return false
|
||||
if (event.repeatCount > 0) return true
|
||||
when (event.action) {
|
||||
KeyEvent.ACTION_DOWN -> set(source(event.deviceId, SOURCE_KEYS), button, true)
|
||||
KeyEvent.ACTION_UP -> set(source(event.deviceId, SOURCE_KEYS), button, false)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun onMotion(event: MotionEvent): Boolean {
|
||||
if (event.actionMasked != MotionEvent.ACTION_MOVE) return true
|
||||
val sticks = physical.getOrPut(event.deviceId) { Sticks() }
|
||||
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)
|
||||
if (hasTriggerAxes(event)) {
|
||||
sticks.leftTrigger = PadBridge.leftTrigger(event, wired)
|
||||
sticks.rightTrigger = PadBridge.rightTrigger(event, wired)
|
||||
}
|
||||
val hatX = event.getAxisValue(MotionEvent.AXIS_HAT_X)
|
||||
val hatY = event.getAxisValue(MotionEvent.AXIS_HAT_Y)
|
||||
val hat = source(event.deviceId, SOURCE_HAT)
|
||||
set(hat, UP, hatY < -0.5f)
|
||||
set(hat, DOWN, hatY > 0.5f)
|
||||
set(hat, LEFT, hatX < -0.5f)
|
||||
set(hat, RIGHT, hatX > 0.5f)
|
||||
wake()
|
||||
return true
|
||||
}
|
||||
|
||||
fun onTouchPad(state: PadState) {
|
||||
touch.leftX = state.leftX
|
||||
touch.leftY = state.leftY
|
||||
touch.rightX = state.rightX
|
||||
touch.rightY = state.rightY
|
||||
touch.leftTrigger = state.leftTrigger
|
||||
touch.rightTrigger = state.rightTrigger
|
||||
for (button in TOUCH_BUTTONS) {
|
||||
set(SOURCE_TOUCH, button, state.isDown(button))
|
||||
}
|
||||
set(SOURCE_TOUCH, UP, state.up)
|
||||
set(SOURCE_TOUCH, DOWN, state.down)
|
||||
set(SOURCE_TOUCH, LEFT, state.left)
|
||||
set(SOURCE_TOUCH, RIGHT, state.right)
|
||||
wake()
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
stop()
|
||||
physical.clear()
|
||||
triggerAxes.clear()
|
||||
wiredLayout.clear()
|
||||
touch.clear()
|
||||
down.clear()
|
||||
sent.clear()
|
||||
stickDirection = NONE
|
||||
scrolling = false
|
||||
held.keys.toList().reversed().forEach { code ->
|
||||
held.remove(code)
|
||||
send(code, false)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun step(dt: Float) {
|
||||
sum()
|
||||
val lx = total.leftX
|
||||
val ly = total.leftY
|
||||
val held = heldDirection()
|
||||
val direction = when {
|
||||
held != NONE -> held
|
||||
hypot(lx, ly) < STICK_NAVIGATE -> NONE
|
||||
abs(lx) > abs(ly) -> if (lx > 0) RIGHT else LEFT
|
||||
else -> if (ly > 0) DOWN else UP
|
||||
}
|
||||
val now = clock()
|
||||
if (direction != stickDirection) {
|
||||
stickDirection = direction
|
||||
if (direction != NONE) {
|
||||
tap(direction)
|
||||
stickRepeatAt = now + REPEAT_FIRST_MS
|
||||
}
|
||||
} else if (direction != NONE && now >= stickRepeatAt) {
|
||||
tap(direction)
|
||||
stickRepeatAt = now + REPEAT_MS
|
||||
}
|
||||
val gain = gain(total.rightX, total.rightY)
|
||||
val sx = total.rightX * gain
|
||||
val triggers = trigger(total.rightTrigger) - trigger(total.leftTrigger)
|
||||
val vertical = (total.rightY * gain + triggers).coerceIn(-1f, 1f)
|
||||
if (sx != 0f || vertical != 0f) {
|
||||
if (!scrolling) {
|
||||
scrolling = true
|
||||
listener.onScrollStart()
|
||||
}
|
||||
val speed = listener.scrollSpeed() * dt
|
||||
listener.onScroll(sx * speed, vertical * speed)
|
||||
} else if (scrolling) {
|
||||
scrolling = false
|
||||
press(KEY_SETTLE)
|
||||
release(KEY_SETTLE)
|
||||
}
|
||||
}
|
||||
|
||||
private fun set(source: Int, button: Int, pressed: Boolean) {
|
||||
val sources = down.getOrPut(button) { HashSet() }
|
||||
val was = sources.isNotEmpty()
|
||||
if (pressed) sources.add(source) else sources.remove(source)
|
||||
val now = sources.isNotEmpty()
|
||||
if (was == now) return
|
||||
if (button in DIRECTIONS) {
|
||||
if (now) step(0f)
|
||||
wake()
|
||||
return
|
||||
}
|
||||
if (now) {
|
||||
if (button == PadState.Y) {
|
||||
listener.onKeyboard()
|
||||
return
|
||||
}
|
||||
val codes = codesFor(button)
|
||||
sent[button] = codes
|
||||
codes.forEach { press(it) }
|
||||
} else {
|
||||
sent.remove(button)?.reversed()?.forEach { release(it) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun hasTriggerAxes(event: android.view.InputEvent): Boolean = triggerAxes.getOrPut(event.deviceId) {
|
||||
val device = event.device ?: return@getOrPut true
|
||||
device.getMotionRange(MotionEvent.AXIS_LTRIGGER) != null || device.getMotionRange(MotionEvent.AXIS_BRAKE) != null ||
|
||||
(event is MotionEvent && wiredLayout.getOrPut(event.deviceId) { PadBridge.wiredXboxLayout(event) })
|
||||
}
|
||||
|
||||
private fun heldDirection(): Int {
|
||||
for (direction in DIRECTIONS) if (down[direction]?.isNotEmpty() == true) return direction
|
||||
return NONE
|
||||
}
|
||||
|
||||
private fun sum() {
|
||||
total.leftX = touch.leftX
|
||||
total.leftY = touch.leftY
|
||||
total.rightX = touch.rightX
|
||||
total.rightY = touch.rightY
|
||||
total.leftTrigger = touch.leftTrigger
|
||||
total.rightTrigger = touch.rightTrigger
|
||||
for (sticks in physical.values) {
|
||||
total.leftX += sticks.leftX
|
||||
total.leftY += sticks.leftY
|
||||
total.rightX += sticks.rightX
|
||||
total.rightY += sticks.rightY
|
||||
total.leftTrigger += sticks.leftTrigger
|
||||
total.rightTrigger += sticks.rightTrigger
|
||||
}
|
||||
}
|
||||
|
||||
private fun tap(direction: Int) {
|
||||
listener.onNavigate()
|
||||
val codes = codesFor(direction)
|
||||
codes.forEach { press(it) }
|
||||
codes.reversed().forEach { release(it) }
|
||||
}
|
||||
|
||||
private fun codesFor(button: Int): IntArray = when (button) {
|
||||
PadState.A -> intArrayOf(KEY_ENTER)
|
||||
PadState.X -> intArrayOf(KEY_MENU)
|
||||
PadState.B -> intArrayOf(KEY_ESC, KEY_BACK)
|
||||
PadState.START -> intArrayOf(KEY_ENTER)
|
||||
PadState.LB -> intArrayOf(KEY_LEFTCTRL, KEY_PAGEUP)
|
||||
PadState.RB -> intArrayOf(KEY_LEFTCTRL, KEY_PAGEDOWN)
|
||||
UP -> intArrayOf(KEY_UP)
|
||||
DOWN -> intArrayOf(KEY_DOWN)
|
||||
LEFT -> intArrayOf(KEY_LEFT)
|
||||
RIGHT -> intArrayOf(KEY_RIGHT)
|
||||
else -> IntArray(0)
|
||||
}
|
||||
|
||||
private fun wake() {
|
||||
if (moving() && !running) {
|
||||
running = true
|
||||
frameAt = 0L
|
||||
choreographer.postFrameCallback(frame)
|
||||
}
|
||||
}
|
||||
|
||||
private fun moving(): Boolean {
|
||||
if (heldDirection() != NONE || stickDirection != NONE || scrolling) return true
|
||||
sum()
|
||||
return hypot(total.leftX, total.leftY) >= STICK_NAVIGATE || gain(total.rightX, total.rightY) != 0f ||
|
||||
trigger(total.leftTrigger) != 0f || trigger(total.rightTrigger) != 0f
|
||||
}
|
||||
|
||||
private fun stop() {
|
||||
running = false
|
||||
frameAt = 0L
|
||||
choreographer.removeFrameCallback(frame)
|
||||
}
|
||||
|
||||
private fun press(code: Int) {
|
||||
val count = held[code] ?: 0
|
||||
held[code] = count + 1
|
||||
if (count == 0) send(code, true)
|
||||
}
|
||||
|
||||
private fun release(code: Int) {
|
||||
val count = held[code] ?: return
|
||||
if (count > 1) {
|
||||
held[code] = count - 1
|
||||
return
|
||||
}
|
||||
held.remove(code)
|
||||
send(code, false)
|
||||
}
|
||||
|
||||
private fun send(code: Int, pressed: Boolean) {
|
||||
listener.onKey(code, pressed)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val DEAD_ZONE = 0.15f
|
||||
private const val STICK_NAVIGATE = 0.5f
|
||||
private const val REPEAT_FIRST_MS = 300L
|
||||
private const val REPEAT_MS = 120L
|
||||
private const val SOURCE_KEYS = 0
|
||||
private const val SOURCE_HAT = 1
|
||||
private const val SOURCE_TOUCH = Int.MIN_VALUE
|
||||
private const val NONE = -1
|
||||
private const val UP = 100
|
||||
private const val DOWN = 101
|
||||
private const val LEFT = 102
|
||||
private const val RIGHT = 103
|
||||
private val DIRECTIONS = intArrayOf(UP, DOWN, LEFT, RIGHT)
|
||||
private val TOUCH_BUTTONS = intArrayOf(PadState.A, PadState.B, PadState.X, PadState.Y, PadState.LB, PadState.RB, PadState.START)
|
||||
private const val KEY_ESC = 1
|
||||
private const val KEY_ENTER = 28
|
||||
private const val KEY_UP = 103
|
||||
private const val KEY_LEFT = 105
|
||||
private const val KEY_RIGHT = 106
|
||||
private const val KEY_DOWN = 108
|
||||
private const val KEY_BACK = 158
|
||||
private const val KEY_LEFTCTRL = 29
|
||||
private const val KEY_PAGEUP = 104
|
||||
private const val KEY_PAGEDOWN = 109
|
||||
private const val KEY_MENU = 127
|
||||
private const val KEY_SETTLE = 183
|
||||
|
||||
private val BUTTONS = mapOf(
|
||||
KeyEvent.KEYCODE_BUTTON_A to PadState.A,
|
||||
KeyEvent.KEYCODE_BUTTON_B to PadState.B,
|
||||
KeyEvent.KEYCODE_BUTTON_X to PadState.X,
|
||||
KeyEvent.KEYCODE_BUTTON_Y to PadState.Y,
|
||||
KeyEvent.KEYCODE_BUTTON_L1 to PadState.LB,
|
||||
KeyEvent.KEYCODE_BUTTON_R1 to PadState.RB,
|
||||
KeyEvent.KEYCODE_BUTTON_START to PadState.START,
|
||||
KeyEvent.KEYCODE_DPAD_UP to UP,
|
||||
KeyEvent.KEYCODE_DPAD_DOWN to DOWN,
|
||||
KeyEvent.KEYCODE_DPAD_LEFT to LEFT,
|
||||
KeyEvent.KEYCODE_DPAD_RIGHT to RIGHT,
|
||||
)
|
||||
|
||||
internal fun curve(x: Float, y: Float): Pair<Float, Float> {
|
||||
val gain = gain(x, y)
|
||||
return x * gain to y * gain
|
||||
}
|
||||
|
||||
private fun gain(x: Float, y: Float): Float {
|
||||
val length = hypot(x, y)
|
||||
if (length <= DEAD_ZONE) return 0f
|
||||
val scaled = (length.coerceAtMost(1f) - DEAD_ZONE) / (1f - DEAD_ZONE)
|
||||
return scaled * scaled / length
|
||||
}
|
||||
|
||||
private fun source(deviceId: Int, kind: Int): Int = deviceId * 2 + kind
|
||||
|
||||
|
||||
|
||||
private fun trigger(value: Float): Float =
|
||||
if (value <= DEAD_ZONE) 0f else (value.coerceAtMost(1f) - DEAD_ZONE) / (1f - DEAD_ZONE)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.Choreographer
|
||||
import android.view.MotionEvent
|
||||
import android.view.VelocityTracker
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.hypot
|
||||
import kotlin.math.pow
|
||||
|
||||
class ScrollTouchGestures(
|
||||
private val slopPx: Float,
|
||||
private val minFlingPx: Float,
|
||||
private val listener: Listener,
|
||||
) {
|
||||
interface Listener {
|
||||
fun onMove(x: Float, y: Float)
|
||||
fun onButton(button: Int, pressed: Boolean)
|
||||
fun onScroll(dx: Float, dy: Float)
|
||||
fun onLongPress()
|
||||
}
|
||||
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private val choreographer = Choreographer.getInstance()
|
||||
private var tracker: VelocityTracker? = null
|
||||
private var pointerId = -1
|
||||
private var startX = 0f
|
||||
private var startY = 0f
|
||||
private var lastX = 0f
|
||||
private var lastY = 0f
|
||||
private var movedAt = 0L
|
||||
private var scrolling = false
|
||||
private var vertical = true
|
||||
private var held = false
|
||||
private var dragging = false
|
||||
private var stoppedFling = false
|
||||
private var clicked = 0
|
||||
private var flingX = 0f
|
||||
private var flingY = 0f
|
||||
private var flingAt = 0L
|
||||
private var flinging = false
|
||||
|
||||
private val longPress = Runnable {
|
||||
held = true
|
||||
listener.onLongPress()
|
||||
}
|
||||
private val clickUp = Runnable { releaseClick() }
|
||||
private val flingFrame = object : Choreographer.FrameCallback {
|
||||
override fun doFrame(frameTimeNanos: Long) {
|
||||
if (!flinging) return
|
||||
val dt = if (flingAt == 0L) 1f / 60f else ((frameTimeNanos - flingAt) / 1e9f).coerceIn(0f, 0.05f)
|
||||
flingAt = frameTimeNanos
|
||||
val decay = exp(-dt / FLING_TIME_S)
|
||||
listener.onScroll(flingX * dt, flingY * dt)
|
||||
flingX *= decay
|
||||
flingY *= decay
|
||||
if (hypot(flingX, flingY) < FLING_STOP_PX) flinging = false else choreographer.postFrameCallback(this)
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
handler.removeCallbacks(longPress)
|
||||
stopFling()
|
||||
releaseClick()
|
||||
if (dragging) listener.onButton(PointerGestures.BTN_LEFT, false)
|
||||
dragging = false
|
||||
held = false
|
||||
scrolling = false
|
||||
pointerId = -1
|
||||
tracker?.recycle()
|
||||
tracker = null
|
||||
}
|
||||
|
||||
fun onTouch(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
stoppedFling = flinging
|
||||
cancel()
|
||||
pointerId = event.getPointerId(0)
|
||||
startX = event.x
|
||||
startY = event.y
|
||||
lastX = startX
|
||||
lastY = startY
|
||||
tracker = VelocityTracker.obtain().also { it.addMovement(event) }
|
||||
listener.onMove(startX, startY)
|
||||
handler.postDelayed(longPress, HOLD_MS)
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
val index = event.findPointerIndex(pointerId).takeIf { it >= 0 } ?: return true
|
||||
tracker?.addMovement(event)
|
||||
val x = event.getX(index)
|
||||
val y = event.getY(index)
|
||||
if (!scrolling && !dragging && hypot(x - startX, y - startY) > slopPx) {
|
||||
handler.removeCallbacks(longPress)
|
||||
if (held) {
|
||||
dragging = true
|
||||
listener.onButton(PointerGestures.BTN_LEFT, true)
|
||||
} else {
|
||||
scrolling = true
|
||||
vertical = abs(y - startY) >= abs(x - startX)
|
||||
lastX = startX
|
||||
lastY = startY
|
||||
}
|
||||
}
|
||||
if (dragging) listener.onMove(x, y)
|
||||
follow(event, x, y)
|
||||
}
|
||||
MotionEvent.ACTION_POINTER_UP -> {
|
||||
if (event.getPointerId(event.actionIndex) == pointerId) cancel()
|
||||
}
|
||||
MotionEvent.ACTION_UP -> {
|
||||
handler.removeCallbacks(longPress)
|
||||
if (pointerId == -1 || event.getPointerId(0) != pointerId) return true
|
||||
tracker?.addMovement(event)
|
||||
follow(event, event.x, event.y)
|
||||
when {
|
||||
scrolling -> tracker?.takeIf { event.eventTime - movedAt <= FLING_STILL_MS }?.let { t ->
|
||||
t.computeCurrentVelocity(1000)
|
||||
val vx = if (vertical) 0f else -t.getXVelocity(pointerId)
|
||||
val vy = if (vertical) -t.getYVelocity(pointerId) else 0f
|
||||
if (hypot(vx, vy) >= minFlingPx) startFling(vx, vy)
|
||||
}
|
||||
dragging -> listener.onButton(PointerGestures.BTN_LEFT, false)
|
||||
held -> click(PointerGestures.BTN_RIGHT)
|
||||
!stoppedFling -> click(PointerGestures.BTN_LEFT)
|
||||
}
|
||||
dragging = false
|
||||
held = false
|
||||
scrolling = false
|
||||
pointerId = -1
|
||||
tracker?.recycle()
|
||||
tracker = null
|
||||
}
|
||||
MotionEvent.ACTION_CANCEL -> cancel()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun follow(event: MotionEvent, x: Float, y: Float) {
|
||||
if (!scrolling || (x == lastX && y == lastY)) return
|
||||
movedAt = event.eventTime
|
||||
if (vertical) listener.onScroll(0f, lastY - y) else listener.onScroll(lastX - x, 0f)
|
||||
lastX = x
|
||||
lastY = y
|
||||
}
|
||||
|
||||
private fun click(button: Int) {
|
||||
releaseClick()
|
||||
listener.onButton(button, true)
|
||||
clicked = button
|
||||
handler.postDelayed(clickUp, CLICK_MS)
|
||||
}
|
||||
|
||||
private fun releaseClick() {
|
||||
handler.removeCallbacks(clickUp)
|
||||
if (clicked != 0) listener.onButton(clicked, false)
|
||||
clicked = 0
|
||||
}
|
||||
|
||||
private fun startFling(vx: Float, vy: Float) {
|
||||
flingX = vx
|
||||
flingY = vy
|
||||
flingAt = 0L
|
||||
flinging = true
|
||||
choreographer.postFrameCallback(flingFrame)
|
||||
}
|
||||
|
||||
private fun stopFling() {
|
||||
flinging = false
|
||||
choreographer.removeFrameCallback(flingFrame)
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun wheelUnits(pixels: Int, viewSize: Int): Int {
|
||||
if (pixels == 0) return 0
|
||||
val step = floor(viewSize.toDouble().pow(2.0 / 3.0)).coerceAtLeast(1.0)
|
||||
val units = (abs(pixels) * 120.0 / step).toInt() + 1
|
||||
return if (pixels < 0) -units else units
|
||||
}
|
||||
|
||||
private const val HOLD_MS = 500L
|
||||
private const val CLICK_MS = 50L
|
||||
private const val FLING_TIME_S = 0.45f
|
||||
private const val FLING_STOP_PX = 40f
|
||||
private const val FLING_STILL_MS = 60L
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user