mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 05:46:03 +00:00
Merge upstream main into unified input profiles
Integrate screen keys, portrait controls, and Home/Back shortcuts with routed input. Preserve remapped axes, neutral gating, and explicit Back bindings; discard stale queued chord reports when routing changes. Validation: 2,632 debug JVM tests across 385 suites pass with Java 17 and skipRust. Device testing and latency measurements were not repeated for this merge.
This commit is contained in:
commit
92b4361100
525 files changed
+40912
-3336
No files matched your search
@@ -0,0 +1,93 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.view.KeyEvent
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
|
||||
class BackQamInput(private val setPressed: (Boolean) -> Unit, private val onTap: (Boolean) -> Unit,
|
||||
private val doubleTapMs: () -> Long = { 500 }, private val onCancel: () -> Unit = {}) {
|
||||
private class Press(val hold: Runnable, var held: Boolean = false, var used: Boolean = false)
|
||||
private val handler = Handler(Looper.getMainLooper())
|
||||
private val presses = mutableMapOf<Int, Press>()
|
||||
private val cancelled = mutableSetOf<Int>()
|
||||
private var tapPending = false
|
||||
private val singleTap = Runnable { tapPending = false; onTap(false) }
|
||||
|
||||
private fun cancelTap() {
|
||||
handler.removeCallbacks(singleTap)
|
||||
tapPending = false
|
||||
}
|
||||
|
||||
fun onChordInput() {
|
||||
if (presses.isEmpty()) return
|
||||
val alreadyHeld = presses.values.any { it.held }
|
||||
presses.values.forEach {
|
||||
handler.removeCallbacks(it.hold)
|
||||
it.held = true
|
||||
it.used = true
|
||||
}
|
||||
cancelTap()
|
||||
if (!alreadyHeld) setPressed(true)
|
||||
}
|
||||
|
||||
fun onKeyEvent(event: KeyEvent, enabled: Boolean): Boolean {
|
||||
if (!enabled) cancel()
|
||||
if (event.keyCode != KeyEvent.KEYCODE_BACK) return false
|
||||
val device = event.deviceId
|
||||
return when (event.action) {
|
||||
KeyEvent.ACTION_DOWN -> {
|
||||
if (event.repeatCount > 0) return enabled || device in presses || device in cancelled
|
||||
cancelled.remove(device)
|
||||
if (!enabled || event.isCanceled) return false
|
||||
if (device in presses) return true
|
||||
handler.removeCallbacks(singleTap)
|
||||
val hold = Runnable {
|
||||
val press = presses[device] ?: return@Runnable
|
||||
val alreadyHeld = presses.values.any { it.held }
|
||||
press.held = true
|
||||
presses.values.forEach { it.used = true }
|
||||
cancelTap()
|
||||
if (!alreadyHeld) setPressed(true)
|
||||
}
|
||||
presses[device] = Press(hold, used = presses.values.any { it.held })
|
||||
handler.postDelayed(hold, 700)
|
||||
true
|
||||
}
|
||||
KeyEvent.ACTION_UP -> {
|
||||
val press = presses.remove(device)
|
||||
val wasCancelled = cancelled.remove(device)
|
||||
if (press != null) {
|
||||
handler.removeCallbacks(press.hold)
|
||||
if (press.held && presses.values.none { it.held }) setPressed(false)
|
||||
if (!press.used && !event.isCanceled) {
|
||||
if (tapPending) { cancelTap(); onTap(true) }
|
||||
else { tapPending = true; handler.postDelayed(singleTap, doubleTapMs()) }
|
||||
} else if (event.isCanceled) cancelTap()
|
||||
}
|
||||
press != null || wasCancelled || enabled
|
||||
}
|
||||
else -> device in presses || device in cancelled
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
cancelTap()
|
||||
val wasHeld = presses.values.any { it.held }
|
||||
presses.values.forEach { handler.removeCallbacks(it.hold) }
|
||||
cancelled.addAll(presses.keys)
|
||||
presses.clear()
|
||||
onCancel()
|
||||
if (wasHeld) setPressed(false)
|
||||
}
|
||||
|
||||
fun onDeviceRemoved(device: Int) {
|
||||
cancelled.remove(device)
|
||||
val press = presses.remove(device) ?: return
|
||||
handler.removeCallbacks(press.hold)
|
||||
cancelTap()
|
||||
if (press.held && presses.values.none { it.held }) {
|
||||
onCancel()
|
||||
setPressed(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import com.droiddeck.launcher.ui.focusGlide
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.Gravity
|
||||
@@ -104,6 +105,7 @@ class ControllerEditorActivity : ComponentActivity() {
|
||||
else -> Color.White.copy(alpha = 0.06f)
|
||||
},
|
||||
)
|
||||
.focusGlide(RoundedCornerShape(50))
|
||||
.then(
|
||||
if (selected == null) Modifier.clickable(onClick = onClick)
|
||||
else Modifier.toggleable(value = selected, role = Role.Switch, onValueChange = { onClick() }),
|
||||
|
||||
@@ -157,6 +157,56 @@ object ControllerPrefs {
|
||||
prefs(context).edit().remove("layout.${width}x$height").apply()
|
||||
}
|
||||
|
||||
fun screenKeysShown(context: Context): Boolean = prefs(context).getBoolean("screenKeysShown", true)
|
||||
|
||||
fun setScreenKeysShown(context: Context, shown: Boolean) {
|
||||
prefs(context).edit().putBoolean("screenKeysShown", shown).apply()
|
||||
}
|
||||
|
||||
fun screenKeys(context: Context): List<ScreenKey> = decodeScreenKeys(prefs(context).getString("screenKeys", null).orEmpty())
|
||||
|
||||
fun setScreenKeys(context: Context, keys: List<ScreenKey>) {
|
||||
prefs(context).edit().putString("screenKeys", encodeScreenKeys(keys)).apply()
|
||||
}
|
||||
|
||||
fun encodeScreenKeys(keys: List<ScreenKey>): String = keys.joinToString(";") {
|
||||
"${it.codes.joinToString("+")}:${it.x},${it.y},${it.size}:${escape(it.glyph)}:${escape(it.text)}:${escape(it.image)}:${it.opacity}:${it.shape}:${if (it.toggle) 1 else 0}:${if (it.labels) 1 else 0}"
|
||||
}
|
||||
|
||||
fun decodeScreenKeys(raw: String): List<ScreenKey> = raw.split(';').mapNotNull { entry ->
|
||||
val parts = entry.split(':')
|
||||
if (parts.size < 2) return@mapNotNull null
|
||||
val keys = parts[0].split('+').mapNotNull { it.toIntOrNull() }.takeIf { it.isNotEmpty() } ?: return@mapNotNull null
|
||||
val nums = parts[1].split(',').mapNotNull { it.toFloatOrNull() }.takeIf { it.size == 3 } ?: return@mapNotNull null
|
||||
ScreenKey(
|
||||
keys, nums[0], nums[1], nums[2].toInt(),
|
||||
parts.getOrNull(2)?.let(::unescape).orEmpty(),
|
||||
parts.getOrNull(3)?.let(::unescape).orEmpty(),
|
||||
parts.getOrNull(4)?.let(::unescape).orEmpty(),
|
||||
parts.getOrNull(5)?.toFloatOrNull()?.coerceIn(0.2f, 1f) ?: 1f,
|
||||
parts.getOrNull(6)?.toIntOrNull()?.coerceIn(SCREEN_KEY_PILL, SCREEN_KEY_STICK) ?: SCREEN_KEY_PILL,
|
||||
parts.getOrNull(7) == "1",
|
||||
parts.getOrNull(8) != "0",
|
||||
)
|
||||
}
|
||||
|
||||
fun screenLayouts(context: Context): Map<String, String> =
|
||||
prefs(context).all.mapNotNull { (key, value) ->
|
||||
if (!key.startsWith("screenLayout.") || value !is String) null else key.removePrefix("screenLayout.") to value
|
||||
}.toMap()
|
||||
|
||||
fun saveScreenLayout(context: Context, name: String, raw: String) {
|
||||
prefs(context).edit().putString("screenLayout.$name", raw).apply()
|
||||
}
|
||||
|
||||
fun deleteScreenLayout(context: Context, name: String) {
|
||||
prefs(context).edit().remove("screenLayout.$name").apply()
|
||||
}
|
||||
|
||||
private fun escape(value: String) = value.replace("%", "%25").replace(";", "%3B").replace(":", "%3A").replace(",", "%2C")
|
||||
|
||||
private fun unescape(value: String) = value.replace("%2C", ",").replace("%3A", ":").replace("%3B", ";").replace("%25", "%")
|
||||
|
||||
fun resetAll(context: Context) {
|
||||
prefs(context).edit().clear().putBoolean("rumble", true).apply()
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ class DirectTouchHold(
|
||||
private var y = 0f
|
||||
private var consumed = false
|
||||
private var pressed = false
|
||||
private var delayMs = 0L
|
||||
private val release = Runnable {
|
||||
if (pressed) listener.onButton(PointerGestures.BTN_RIGHT, false, x, y)
|
||||
pressed = false
|
||||
@@ -28,6 +29,11 @@ class DirectTouchHold(
|
||||
handler.postDelayed(release, 50L)
|
||||
}
|
||||
|
||||
fun configure(config: TouchGestureConfig) {
|
||||
cancel()
|
||||
delayMs = if (config.longPressEnabled) config.longPressDelay.toLong().coerceAtLeast(1L) else 0L
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
handler.removeCallbacks(hold)
|
||||
handler.removeCallbacks(release)
|
||||
@@ -40,7 +46,7 @@ class DirectTouchHold(
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
cancel()
|
||||
x = event.x; y = event.y
|
||||
handler.postDelayed(hold, 500L)
|
||||
if (delayMs > 0L) handler.postDelayed(hold, delayMs)
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> if (event.pointerCount > 1 ||
|
||||
abs(event.x - x) > slopPx || abs(event.y - y) > slopPx) handler.removeCallbacks(hold)
|
||||
|
||||
@@ -525,8 +525,8 @@ public class FakeInputWriter {
|
||||
}
|
||||
}
|
||||
|
||||
public static void setLowLatency(boolean controller, boolean touch) {
|
||||
int flags = (controller ? LATENCY_CONTROLLER : 0) | (touch ? LATENCY_TOUCH : 0);
|
||||
public static void setLowLatency(boolean enabled) {
|
||||
int flags = enabled ? LATENCY_CONTROLLER | LATENCY_TOUCH : 0;
|
||||
latency = flags;
|
||||
RingSlot ringSlot;
|
||||
synchronized (RING_LOCK) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.view.MotionEvent
|
||||
|
||||
/** Keeps split Android touch targets from restarting one shared gesture recognizer. */
|
||||
internal class GestureTouchStreamGate {
|
||||
enum class Source { SURFACE, SCREEN_KEYS, ON_SCREEN_CONTROLS }
|
||||
|
||||
private var owner: Source? = null
|
||||
|
||||
fun accepts(source: Source, event: MotionEvent): Boolean {
|
||||
if (owner == null && event.actionMasked == MotionEvent.ACTION_DOWN) owner = source
|
||||
val accepted = owner == source
|
||||
if (accepted && (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_CANCEL)) {
|
||||
owner = null
|
||||
}
|
||||
return accepted
|
||||
}
|
||||
|
||||
fun reset() { owner = null }
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.accessibilityservice.AccessibilityService
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.hardware.input.InputManager
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.view.KeyEvent
|
||||
import android.view.accessibility.AccessibilityEvent
|
||||
import com.droiddeck.launcher.HomeApp
|
||||
|
||||
object HomeButtonShortcuts {
|
||||
private var owner: Any? = null
|
||||
private var enabled: () -> Boolean = { false }
|
||||
private var setPressed: (Boolean) -> Unit = {}
|
||||
private var onCancel: () -> Unit = {}
|
||||
private val input = HomeGuideInput({ setPressed(it) }, { onCancel() })
|
||||
|
||||
fun attach(owner: Any, enabled: () -> Boolean, setPressed: (Boolean) -> Unit, onCancel: () -> Unit) {
|
||||
input.cancel()
|
||||
this.owner = owner
|
||||
this.enabled = enabled
|
||||
this.setPressed = setPressed
|
||||
this.onCancel = onCancel
|
||||
}
|
||||
|
||||
fun cancel(owner: Any) { if (this.owner === owner) input.cancel() }
|
||||
|
||||
fun detach(owner: Any) {
|
||||
if (this.owner !== owner) return
|
||||
input.cancel()
|
||||
this.owner = null
|
||||
enabled = { false }
|
||||
setPressed = {}
|
||||
onCancel = {}
|
||||
}
|
||||
|
||||
fun onKeyEvent(context: Context, event: KeyEvent): Boolean {
|
||||
// AYN maps its system-assistant key (gpio-keys KEY_F24) to HOME too; only the pad's Home is Guide.
|
||||
if (event.keyCode != KeyEvent.KEYCODE_HOME || !PadBridge.isControllerKeyEvent(event)) return false
|
||||
return input.onKeyEvent(event, enabled() && HomeApp.isDefault(context))
|
||||
}
|
||||
|
||||
fun release() = input.cancel()
|
||||
fun onDeviceRemoved(device: Int) = input.onDeviceRemoved(device)
|
||||
}
|
||||
|
||||
class HomeButtonService : AccessibilityService(), InputManager.InputDeviceListener {
|
||||
override fun onServiceConnected() {
|
||||
getSystemService(InputManager::class.java).registerInputDeviceListener(this, Handler(Looper.getMainLooper()))
|
||||
}
|
||||
|
||||
override fun onKeyEvent(event: KeyEvent) = HomeButtonShortcuts.onKeyEvent(this, event)
|
||||
override fun onAccessibilityEvent(event: AccessibilityEvent?) {}
|
||||
override fun onInterrupt() { HomeButtonShortcuts.release() }
|
||||
override fun onInputDeviceAdded(deviceId: Int) {}
|
||||
override fun onInputDeviceChanged(deviceId: Int) {}
|
||||
override fun onInputDeviceRemoved(deviceId: Int) { HomeButtonShortcuts.onDeviceRemoved(deviceId) }
|
||||
|
||||
override fun onUnbind(intent: Intent?): Boolean {
|
||||
HomeButtonShortcuts.release()
|
||||
return super.onUnbind(intent)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
HomeButtonShortcuts.release()
|
||||
getSystemService(InputManager::class.java).unregisterInputDeviceListener(this)
|
||||
super.onDestroy()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.view.KeyEvent
|
||||
|
||||
class HomeGuideInput(private val setPressed: (Boolean) -> Unit, private val onCancel: () -> Unit = {}) {
|
||||
private val presses = mutableSetOf<Int>()
|
||||
private val cancelled = mutableSetOf<Int>()
|
||||
|
||||
fun onKeyEvent(event: KeyEvent, enabled: Boolean): Boolean {
|
||||
if (event.keyCode != KeyEvent.KEYCODE_HOME) return false
|
||||
if (!enabled) cancel()
|
||||
val device = event.deviceId
|
||||
return when (event.action) {
|
||||
KeyEvent.ACTION_DOWN -> {
|
||||
if (event.repeatCount > 0) return device in presses || device in cancelled
|
||||
cancelled.remove(device)
|
||||
if (!enabled || event.isCanceled) return false
|
||||
if (presses.add(device) && presses.size == 1) setPressed(true)
|
||||
true
|
||||
}
|
||||
KeyEvent.ACTION_UP -> {
|
||||
val owned = presses.remove(device)
|
||||
val wasCancelled = cancelled.remove(device)
|
||||
if (owned && presses.isEmpty()) setPressed(false)
|
||||
owned || wasCancelled
|
||||
}
|
||||
else -> device in presses || device in cancelled
|
||||
}
|
||||
}
|
||||
|
||||
fun cancel() {
|
||||
val held = presses.isNotEmpty()
|
||||
cancelled.addAll(presses)
|
||||
presses.clear()
|
||||
if (held) { onCancel(); setPressed(false) }
|
||||
}
|
||||
|
||||
fun onDeviceRemoved(device: Int) {
|
||||
cancelled.remove(device)
|
||||
if (presses.remove(device) && presses.isEmpty()) { onCancel(); setPressed(false) }
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,8 @@ class OnScreenControls(
|
||||
private var quickHidden = false
|
||||
private var quickPressedBy = -1
|
||||
private var keyboardPressedBy = -1
|
||||
private var forwardedPointerBits = 0
|
||||
var onTouchMiss: ((MotionEvent) -> Unit)? = null
|
||||
private var settings = ControllerPrefs.read(context)
|
||||
private var snap = settings.snap
|
||||
private var safe = Rect()
|
||||
@@ -516,6 +518,9 @@ class OnScreenControls(
|
||||
val index = event.actionIndex
|
||||
val x = event.getX(index)
|
||||
val y = event.getY(index)
|
||||
if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointerBits = 0
|
||||
if (event.actionMasked == MotionEvent.ACTION_POINTER_DOWN && forwardMiss(event, x, y)) return true
|
||||
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
|
||||
if (event.actionMasked == MotionEvent.ACTION_DOWN && keyboardContains(x, y)) {
|
||||
keyboardPressedBy = event.getPointerId(index)
|
||||
invalidate()
|
||||
@@ -543,11 +548,13 @@ class OnScreenControls(
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
|
||||
if (keyboardPressedBy != -1) return true
|
||||
if (quickPressedBy != -1) return true
|
||||
var changed = false
|
||||
for (index in 0 until event.pointerCount) {
|
||||
val pointer = event.getPointerId(index)
|
||||
if (forwardedPointerBits and (1 shl pointer) != 0) continue
|
||||
val x = event.getX(index)
|
||||
val y = event.getY(index)
|
||||
val stick = controls.firstOrNull { it.stick >= 0 && it.pressedBy == pointer }
|
||||
@@ -569,10 +576,16 @@ class OnScreenControls(
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
val pointer = event.getPointerId(event.actionIndex)
|
||||
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
|
||||
if (event.actionMasked == MotionEvent.ACTION_CANCEL) {
|
||||
forwardedPointerBits = 0
|
||||
releaseAll()
|
||||
return true
|
||||
}
|
||||
if (forwardedPointerBits and (1 shl pointer) != 0) {
|
||||
forwardedPointerBits = forwardedPointerBits and (1 shl pointer).inv()
|
||||
return true
|
||||
}
|
||||
if (keyboardPressedBy == pointer) {
|
||||
val tapped = event.actionMasked == MotionEvent.ACTION_UP && keyboardContains(event.x, event.y)
|
||||
keyboardPressedBy = -1
|
||||
@@ -605,6 +618,23 @@ class OnScreenControls(
|
||||
return false
|
||||
}
|
||||
|
||||
private fun forwardMiss(event: MotionEvent, x: Float, y: Float): Boolean {
|
||||
if (onTouchMiss == null || keyboardPressedBy != -1 || quickPressedBy != -1) return false
|
||||
if (!quickHidden && (controlAt(x, y) ?: adaptiveStickAt(x, y)) != null) return false
|
||||
forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(event.actionIndex))
|
||||
forwardPointers(event, forwardedPointerBits)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) {
|
||||
val split = event.pointerSubset(pointerIdBits) ?: return
|
||||
try {
|
||||
onTouchMiss?.invoke(split)
|
||||
} finally {
|
||||
split.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onEditTouch(event: MotionEvent): Boolean {
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.droiddeck.launcher.input;
|
||||
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
@@ -73,6 +74,14 @@ public final class PadBridge {
|
||||
private final PadState routedTouch = new PadState();
|
||||
private boolean systemGuidePressed;
|
||||
private boolean systemQamPressed;
|
||||
private boolean backQamPressed;
|
||||
private boolean homeGuidePressed;
|
||||
private final PadState lastBackQamReport = new PadState();
|
||||
private final PadState lastWrittenReport = new PadState();
|
||||
private final PadState liveReport = new PadState();
|
||||
private boolean backQamAxesBlocked;
|
||||
private final java.util.Set<Runnable> backQamWrites = new java.util.HashSet<>();
|
||||
private long backQamDelayMs, backQamWriteAt;
|
||||
private boolean qamChordActive;
|
||||
private int chordButton = -1;
|
||||
private boolean chordButtonPressed;
|
||||
@@ -84,6 +93,7 @@ public final class PadBridge {
|
||||
private boolean routedQamPressed;
|
||||
private int qamChordGeneration;
|
||||
private volatile Runnable onPlayerInput;
|
||||
private Runnable onChordInput;
|
||||
private final java.util.concurrent.atomic.AtomicBoolean playerInputPosted = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
|
||||
private static final long STATS_WINDOW_MS = 10_000;
|
||||
@@ -128,6 +138,7 @@ public final class PadBridge {
|
||||
inputRouter = router;
|
||||
for (int slot = 0; slot < SLOTS; slot++) physicalRoutes[slot] = router == null ? null : router.pad(InputRole.PHYSICAL, stateFor(slot));
|
||||
for (TouchSource source : touchSources.values()) source.route = router == null ? null : router.pad(source.role, source.raw);
|
||||
clearBackQamWrites();
|
||||
if (open) publish();
|
||||
for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot);
|
||||
}
|
||||
@@ -137,6 +148,7 @@ public final class PadBridge {
|
||||
public synchronized void setInputProfile(InputProfile profile) {
|
||||
if (inputRouter == null) return;
|
||||
inputRouter.setProfile(profile);
|
||||
clearBackQamWrites();
|
||||
if (open) publish();
|
||||
for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot);
|
||||
}
|
||||
@@ -144,6 +156,7 @@ public final class PadBridge {
|
||||
public synchronized void setInputEnabled(boolean enabled) {
|
||||
if (inputRouter == null) return;
|
||||
inputRouter.setEnabled(enabled);
|
||||
if (!enabled) clearBackQamWrites();
|
||||
if (open) publish();
|
||||
for (int slot = 1; slot < SLOTS; slot++) if (playerWriters[slot] != null) publish(slot);
|
||||
DeckControls.INSTANCE.refresh();
|
||||
@@ -170,6 +183,8 @@ public final class PadBridge {
|
||||
}
|
||||
|
||||
private void resetBuiltin() {
|
||||
homeGuidePressed = false;
|
||||
clearBackQamWrites();
|
||||
routedTouch.clear();
|
||||
for (TouchSource source : touchSources.values()) {
|
||||
source.raw.clear();
|
||||
@@ -177,6 +192,7 @@ public final class PadBridge {
|
||||
}
|
||||
systemGuidePressed = false;
|
||||
systemQamPressed = false;
|
||||
backQamPressed = false;
|
||||
qamTapPressed = false;
|
||||
guideTapPressed = false;
|
||||
guideTapGeneration++;
|
||||
@@ -209,6 +225,8 @@ public final class PadBridge {
|
||||
for (InputRouter.PadSource route : physicalRoutes) if (route != null) route.close();
|
||||
Arrays.fill(physicalRoutes, null);
|
||||
inputRouter = null;
|
||||
homeGuidePressed = false;
|
||||
clearBackQamWrites();
|
||||
releaseBackPaddles();
|
||||
activeControllerId = NO_CONTROLLER;
|
||||
for (int slot = 0; slot < SLOTS; slot++) {
|
||||
@@ -224,6 +242,7 @@ public final class PadBridge {
|
||||
}
|
||||
systemGuidePressed = false;
|
||||
systemQamPressed = false;
|
||||
backQamPressed = false;
|
||||
qamChordActive = false;
|
||||
chordButtonPressed = false;
|
||||
qamTapPressed = false;
|
||||
@@ -332,6 +351,14 @@ public final class PadBridge {
|
||||
onPlayerInput = listener;
|
||||
}
|
||||
|
||||
public synchronized void setOnChordInput(Runnable listener) {
|
||||
onChordInput = listener;
|
||||
}
|
||||
|
||||
private void noteChordInput(int slot) {
|
||||
if (slot == 0 && onChordInput != null) onChordInput.run();
|
||||
}
|
||||
|
||||
private void notePlayerInput() {
|
||||
Runnable listener = onPlayerInput;
|
||||
if (listener == null || !playerInputPosted.compareAndSet(false, true)) return;
|
||||
@@ -366,10 +393,10 @@ public final class PadBridge {
|
||||
case KeyEvent.KEYCODE_BUTTON_MODE:
|
||||
case KeyEvent.KEYCODE_HOME: pad.press(PadState.GUIDE, pressed); break;
|
||||
case KeyEvent.KEYCODE_BUTTON_Z:
|
||||
if (isPrimary(event)) DeckControls.INSTANCE.setGrip(DeckControls.L4, pressed);
|
||||
if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(event.getDevice()) ? DeckControls.R4 : DeckControls.L4, pressed);
|
||||
break;
|
||||
case KeyEvent.KEYCODE_BUTTON_C:
|
||||
if (isPrimary(event)) DeckControls.INSTANCE.setGrip(DeckControls.R4, pressed);
|
||||
if (isPrimary(event)) DeckControls.INSTANCE.setGrip(isNovaController(event.getDevice()) ? DeckControls.L4 : DeckControls.R4, pressed);
|
||||
break;
|
||||
case KeyEvent.KEYCODE_BUTTON_L2: pad.leftTrigger = pressed ? 1f : 0f; break;
|
||||
case KeyEvent.KEYCODE_BUTTON_R2: pad.rightTrigger = pressed ? 1f : 0f; break;
|
||||
@@ -380,6 +407,7 @@ public final class PadBridge {
|
||||
default: return false;
|
||||
}
|
||||
noteDevice(event.getDevice(), slot);
|
||||
if (pressed && event.getRepeatCount() == 0 && !event.isCanceled()) noteChordInput(slot);
|
||||
if (pressed) notePlayerInput();
|
||||
statButtons++;
|
||||
scheduleStats();
|
||||
@@ -411,6 +439,7 @@ public final class PadBridge {
|
||||
pad.rightX = x;
|
||||
pad.rightY = y;
|
||||
}
|
||||
if (x != 0f || y != 0f) noteChordInput(slot);
|
||||
if (Math.max(Math.abs(x), Math.abs(y)) > 0.5f) notePlayerInput();
|
||||
publish(slot);
|
||||
InputTiming.end(timing);
|
||||
@@ -442,6 +471,8 @@ 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
|
||||
|| 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) {
|
||||
notePlayerInput();
|
||||
@@ -537,6 +568,10 @@ public final class PadBridge {
|
||||
routedTouch.clear();
|
||||
DeckControls.INSTANCE.suspend();
|
||||
if (inputRouter == null) state.clear();
|
||||
homeGuidePressed = false;
|
||||
clearBackQamWrites();
|
||||
backQamPressed = false;
|
||||
if (inputRouter == null) releaseBackPaddles();
|
||||
publish();
|
||||
for (int slot = 1; slot < SLOTS; slot++) {
|
||||
if (playerWriters[slot] == null) continue;
|
||||
@@ -555,6 +590,149 @@ public final class PadBridge {
|
||||
return SessionState.getDeckPad() && FakeInputWriter.padHasQam();
|
||||
}
|
||||
|
||||
public boolean canHoldQam() {
|
||||
return builtinPad && nativeQam();
|
||||
}
|
||||
|
||||
public boolean canHoldGuide() {
|
||||
return builtinPad;
|
||||
}
|
||||
|
||||
public synchronized void setHomeGuidePressed(boolean pressed) {
|
||||
if (pressed && !canHoldGuide()) return;
|
||||
if (homeGuidePressed == pressed) return;
|
||||
homeGuidePressed = pressed;
|
||||
updateHeldModifier(PadState.GUIDE, pressed);
|
||||
}
|
||||
|
||||
public synchronized void cancelHomeGuide() {
|
||||
if (!homeGuidePressed) return;
|
||||
clearBackQamWrites();
|
||||
homeGuidePressed = false;
|
||||
if (inputRouter == null) state.clear();
|
||||
else {
|
||||
inputRouter.releaseAll();
|
||||
DeckControls.INSTANCE.suspend();
|
||||
}
|
||||
publish();
|
||||
}
|
||||
|
||||
public synchronized void setBackQamPressed(boolean pressed) {
|
||||
if (pressed && !canHoldQam()) return;
|
||||
if (backQamPressed == pressed) return;
|
||||
backQamPressed = pressed;
|
||||
updateHeldModifier(PadState.QAM, pressed);
|
||||
}
|
||||
|
||||
private void updateHeldModifier(int button, boolean pressed) {
|
||||
if (pressed && (open || start())) {
|
||||
backQamAxesBlocked = false;
|
||||
long now = android.os.SystemClock.uptimeMillis();
|
||||
long modifierAt = Math.max(now, backQamWriteAt);
|
||||
backQamDelayMs = modifierAt - now + QAM_GUIDE_LEAD_MIN_MS;
|
||||
PadState modifier = new PadState();
|
||||
modifier.copyFrom(lastBackQamReport);
|
||||
modifier.press(button, true);
|
||||
writeBackQamReport(modifier, modifierAt);
|
||||
}
|
||||
if (!pressed && !backQamPressed && !homeGuidePressed) {
|
||||
backQamAxesBlocked = hasAxes(liveReport);
|
||||
if (!backQamWrites.isEmpty()) {
|
||||
PadState neutral = new PadState();
|
||||
neutral.copyFrom(lastWrittenReport);
|
||||
clearAxes(neutral);
|
||||
writer.writePad(neutral);
|
||||
lastWrittenReport.copyFrom(neutral);
|
||||
}
|
||||
}
|
||||
publish();
|
||||
}
|
||||
|
||||
public synchronized void cancelBackQam() {
|
||||
if (!backQamPressed && backQamWrites.isEmpty()) return;
|
||||
clearBackQamWrites();
|
||||
backQamPressed = false;
|
||||
if (inputRouter == null) state.clear();
|
||||
else {
|
||||
inputRouter.releaseAll();
|
||||
DeckControls.INSTANCE.suspend();
|
||||
}
|
||||
publish();
|
||||
}
|
||||
|
||||
private void clearBackQamWrites() {
|
||||
for (Runnable pending : backQamWrites) mainHandler.removeCallbacks(pending);
|
||||
backQamWrites.clear();
|
||||
backQamDelayMs = backQamWriteAt = 0;
|
||||
lastBackQamReport.clear();
|
||||
lastWrittenReport.clear();
|
||||
backQamAxesBlocked = false;
|
||||
}
|
||||
|
||||
private void writeBackQamReport(PadState report, long at) {
|
||||
lastBackQamReport.copyFrom(report);
|
||||
if (backQamWrites.isEmpty() && at <= android.os.SystemClock.uptimeMillis()) {
|
||||
writeReport(report, false);
|
||||
return;
|
||||
}
|
||||
PadState queued = new PadState();
|
||||
queued.copyFrom(report);
|
||||
Runnable pending = new Runnable() {
|
||||
@Override public void run() {
|
||||
synchronized (PadBridge.this) {
|
||||
if (!backQamWrites.remove(this)) return;
|
||||
writeReport(queued, true);
|
||||
if (backQamWrites.isEmpty() && !hasButtons(lastBackQamReport)) backQamDelayMs = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
backQamWrites.add(pending);
|
||||
backQamWriteAt = at;
|
||||
mainHandler.postAtTime(pending, at);
|
||||
}
|
||||
|
||||
private void writePad(PadState report) {
|
||||
liveReport.copyFrom(report);
|
||||
if (backQamAxesBlocked && !hasAxes(liveReport)) backQamAxesBlocked = false;
|
||||
long at = android.os.SystemClock.uptimeMillis();
|
||||
// Steam must see the modifier before a partner; preserve quick press/release pairs in order.
|
||||
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 static void clearAxes(PadState pad) {
|
||||
pad.leftX = pad.leftY = pad.rightX = pad.rightY = pad.leftTrigger = pad.rightTrigger = 0f;
|
||||
}
|
||||
|
||||
private static boolean hasButtons(PadState pad) {
|
||||
for (int i = 0; i < 10; i++) if (pad.isDown(i)) return true;
|
||||
return pad.isDown(PadState.GUIDE) || pad.up || pad.right || pad.down || pad.left;
|
||||
}
|
||||
|
||||
private void writeReport(PadState report, boolean queued) {
|
||||
if (!queued && !backQamAxesBlocked) {
|
||||
lastWrittenReport.copyFrom(report);
|
||||
writer.writePad(report);
|
||||
return;
|
||||
}
|
||||
PadState live = new PadState();
|
||||
live.copyFrom(report);
|
||||
if (queued) {
|
||||
live.leftX = liveReport.leftX; live.leftY = liveReport.leftY;
|
||||
live.rightX = liveReport.rightX; live.rightY = liveReport.rightY;
|
||||
live.leftTrigger = liveReport.leftTrigger; live.rightTrigger = liveReport.rightTrigger;
|
||||
}
|
||||
boolean held = (backQamPressed && report.isDown(PadState.QAM)) || (homeGuidePressed && report.isDown(PadState.GUIDE));
|
||||
if ((queued && !held) || backQamAxesBlocked) clearAxes(live);
|
||||
lastWrittenReport.copyFrom(live);
|
||||
writer.writePad(live);
|
||||
}
|
||||
|
||||
public synchronized void triggerQam() {
|
||||
if (!builtinPad) return;
|
||||
if (!nativeQam()) {
|
||||
@@ -661,7 +839,9 @@ public final class PadBridge {
|
||||
slotJoyCons.set(slot * 2 + side, id);
|
||||
slotJoyCons.set(slot * 2 + 1 - side, owner.getId());
|
||||
if (slot == firstPlayerSlot()) releaseBackPaddles();
|
||||
if (slot == 0) clearBackQamWrites();
|
||||
stateFor(slot).clear();
|
||||
if (slot == 0) publish();
|
||||
Log.i(TAG, "\"" + device.getName() + "\" (id " + id + ") joins \"" + owner.getName() + "\" as player " + (slot + 1));
|
||||
return slot;
|
||||
}
|
||||
@@ -678,6 +858,12 @@ public final class PadBridge {
|
||||
return slot == 0 ? state : playerStates[slot];
|
||||
}
|
||||
|
||||
private static boolean isNovaController(InputDevice device) {
|
||||
return "Retroid Pocket Nova".equalsIgnoreCase(Build.MODEL)
|
||||
&& device.getVendorId() == 0x2022
|
||||
&& (device.getProductId() == 0x3001 || device.getProductId() == 0x3002);
|
||||
}
|
||||
|
||||
private boolean isPrimary(KeyEvent event) {
|
||||
int id = event.getDevice().getId();
|
||||
int first = firstPlayerSlot();
|
||||
@@ -690,6 +876,7 @@ public final class PadBridge {
|
||||
}
|
||||
|
||||
private void leave(int slot, int id) {
|
||||
if (slot == 0) clearBackQamWrites();
|
||||
if (partners[slot] == NO_CONTROLLER) {
|
||||
free(slot);
|
||||
return;
|
||||
@@ -797,7 +984,7 @@ public final class PadBridge {
|
||||
output = combinedState;
|
||||
}
|
||||
if (mouseRoute) touchRoute.accept(routedTouch);
|
||||
boolean guide = systemGuidePressed || qamChordActive || guideTapPressed;
|
||||
boolean guide = systemGuidePressed || qamChordActive || guideTapPressed || homeGuidePressed;
|
||||
boolean routedQam = output.isDown(PadState.QAM);
|
||||
boolean routedQamStarted = routedQam && !routedQamPressed;
|
||||
routedQamPressed = routedQam;
|
||||
@@ -805,13 +992,13 @@ public final class PadBridge {
|
||||
startChord(PadState.A);
|
||||
return;
|
||||
}
|
||||
boolean qam = nativeQam() && (systemQamPressed || qamTapPressed);
|
||||
boolean qam = nativeQam() && (systemQamPressed || qamTapPressed || backQamPressed);
|
||||
if (!open && !start()) {
|
||||
statDropped++;
|
||||
return;
|
||||
}
|
||||
if (!guide && !qam && !vrStickTapPressed && (!routedQam || nativeQam())) {
|
||||
writer.writePad(output);
|
||||
writePad(output);
|
||||
return;
|
||||
}
|
||||
effectiveState.copyFrom(output);
|
||||
@@ -823,7 +1010,7 @@ public final class PadBridge {
|
||||
effectiveState.press(PadState.L3, true);
|
||||
effectiveState.press(PadState.R3, true);
|
||||
}
|
||||
writer.writePad(effectiveState);
|
||||
writePad(effectiveState);
|
||||
}
|
||||
|
||||
public static boolean wiredXboxLayout(MotionEvent event) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.view.MotionEvent
|
||||
|
||||
internal fun MotionEvent.pointerSubset(pointerIdBits: Int): MotionEvent? {
|
||||
val indices = (0 until pointerCount).filter { index ->
|
||||
pointerIdBits and (1 shl getPointerId(index)) != 0
|
||||
}
|
||||
if (indices.isEmpty()) return null
|
||||
val splitIndex = indices.indexOf(actionIndex)
|
||||
val splitAction = when (actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> if (splitIndex >= 0) MotionEvent.ACTION_DOWN else MotionEvent.ACTION_MOVE
|
||||
MotionEvent.ACTION_POINTER_DOWN -> if (splitIndex >= 0) {
|
||||
if (indices.size == 1) MotionEvent.ACTION_DOWN
|
||||
else MotionEvent.ACTION_POINTER_DOWN or (splitIndex shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
|
||||
} else MotionEvent.ACTION_MOVE
|
||||
MotionEvent.ACTION_UP -> if (splitIndex >= 0) MotionEvent.ACTION_UP else MotionEvent.ACTION_MOVE
|
||||
MotionEvent.ACTION_POINTER_UP -> if (splitIndex >= 0) {
|
||||
if (indices.size == 1) MotionEvent.ACTION_UP
|
||||
else MotionEvent.ACTION_POINTER_UP or (splitIndex shl MotionEvent.ACTION_POINTER_INDEX_SHIFT)
|
||||
} else MotionEvent.ACTION_MOVE
|
||||
else -> action
|
||||
}
|
||||
val properties = Array(indices.size) { MotionEvent.PointerProperties() }
|
||||
val coordinates = Array(indices.size) { MotionEvent.PointerCoords() }
|
||||
indices.forEachIndexed { target, index ->
|
||||
getPointerProperties(index, properties[target])
|
||||
getPointerCoords(index, coordinates[target])
|
||||
}
|
||||
return MotionEvent.obtain(
|
||||
downTime, eventTime, splitAction, indices.size, properties, coordinates,
|
||||
metaState, buttonState, xPrecision, yPrecision,
|
||||
deviceId, edgeFlags, source, flags,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.content.Context
|
||||
|
||||
fun screenKeyLabel(context: Context, codes: List<Int>): String =
|
||||
codes.filter { it != 0 }.joinToString("+") { short(context, it) }
|
||||
|
||||
private fun short(context: Context, code: Int): String = when (code) {
|
||||
28 -> "⏎"
|
||||
14 -> "⌫"
|
||||
15 -> "Tab"
|
||||
57 -> "Space"
|
||||
58 -> "Caps"
|
||||
42, 54 -> "Shift"
|
||||
29, 97 -> "Ctrl"
|
||||
56 -> "Alt"
|
||||
100 -> "AltGr"
|
||||
125 -> "Super"
|
||||
else -> pcKeyLabel(context, code)
|
||||
}
|
||||
|
||||
const val SCREEN_KEY_PILL = 0
|
||||
const val SCREEN_KEY_ROUND = 1
|
||||
const val SCREEN_KEY_CIRCLE = 2
|
||||
const val SCREEN_KEY_RECT = 3
|
||||
const val SCREEN_KEY_TRIGGER = 4
|
||||
const val SCREEN_KEY_DPAD = 5
|
||||
const val SCREEN_KEY_STICK = 6
|
||||
|
||||
val SCREEN_KEY_DPAD_DIRS = listOf("↑", "→", "↓", "←")
|
||||
val SCREEN_KEY_STICK_DIRS = listOf("↑", "↗", "→", "↘", "↓", "↙", "←", "↖")
|
||||
|
||||
fun screenKeyDirections(shape: Int): List<String> = when (shape) {
|
||||
SCREEN_KEY_DPAD -> SCREEN_KEY_DPAD_DIRS
|
||||
SCREEN_KEY_STICK -> SCREEN_KEY_STICK_DIRS
|
||||
else -> emptyList()
|
||||
}
|
||||
|
||||
val SCREEN_KEY_SHAPES =
|
||||
listOf(
|
||||
SCREEN_KEY_PILL, SCREEN_KEY_ROUND, SCREEN_KEY_CIRCLE, SCREEN_KEY_RECT,
|
||||
SCREEN_KEY_TRIGGER, SCREEN_KEY_DPAD, SCREEN_KEY_STICK,
|
||||
)
|
||||
|
||||
data class ScreenKey(
|
||||
val codes: List<Int>,
|
||||
val x: Float,
|
||||
val y: Float,
|
||||
val size: Int,
|
||||
val glyph: String = "",
|
||||
val text: String = "",
|
||||
val image: String = "",
|
||||
val opacity: Float = 1f,
|
||||
val shape: Int = SCREEN_KEY_PILL,
|
||||
val toggle: Boolean = false,
|
||||
val labels: Boolean = true,
|
||||
) {
|
||||
fun shownLabel(context: Context): String = when {
|
||||
text.isNotEmpty() -> text
|
||||
glyph.isNotEmpty() -> glyph
|
||||
else -> screenKeyLabel(context, codes)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.Paint
|
||||
import android.graphics.Path
|
||||
|
||||
object ScreenKeyIcons {
|
||||
fun has(code: Int): Boolean = code in SET
|
||||
|
||||
private val SET = setOf(28, 14, 15, 57, 42, 54, 29, 97, 125, 103, 108, 105, 106)
|
||||
|
||||
fun draw(canvas: Canvas, code: Int, cx: Float, cy: Float, box: Float, paint: Paint): Boolean {
|
||||
if (!has(code)) return false
|
||||
val h = box / 2f
|
||||
val stroke = box * 0.085f
|
||||
val path = Path()
|
||||
paint.style = Paint.Style.STROKE
|
||||
paint.strokeWidth = stroke
|
||||
paint.strokeCap = Paint.Cap.ROUND
|
||||
paint.strokeJoin = Paint.Join.ROUND
|
||||
when (code) {
|
||||
28 -> {
|
||||
path.moveTo(cx - 0.34f * h, cy - 0.22f * h)
|
||||
path.lineTo(cx + 0.30f * h, cy - 0.22f * h)
|
||||
path.lineTo(cx + 0.30f * h, cy + 0.24f * h)
|
||||
path.lineTo(cx - 0.04f * h, cy + 0.24f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx + 0.12f * h, cy + 0.06f * h)
|
||||
path.lineTo(cx - 0.04f * h, cy + 0.24f * h)
|
||||
path.lineTo(cx + 0.12f * h, cy + 0.42f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
14 -> {
|
||||
path.moveTo(cx - 0.72f * h, cy)
|
||||
path.lineTo(cx - 0.10f * h, cy - 0.50f * h)
|
||||
path.lineTo(cx + 0.74f * h, cy - 0.50f * h)
|
||||
path.lineTo(cx + 0.74f * h, cy + 0.50f * h)
|
||||
path.lineTo(cx - 0.10f * h, cy + 0.50f * h)
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx + 0.10f * h, cy - 0.20f * h)
|
||||
path.lineTo(cx + 0.44f * h, cy + 0.20f * h)
|
||||
path.moveTo(cx + 0.44f * h, cy - 0.20f * h)
|
||||
path.lineTo(cx + 0.10f * h, cy + 0.20f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
15 -> {
|
||||
path.moveTo(cx - 0.72f * h, cy)
|
||||
path.lineTo(cx + 0.44f * h, cy)
|
||||
path.moveTo(cx + 0.14f * h, cy - 0.30f * h)
|
||||
path.lineTo(cx + 0.46f * h, cy)
|
||||
path.lineTo(cx + 0.14f * h, cy + 0.30f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx + 0.70f * h, cy - 0.48f * h)
|
||||
path.lineTo(cx + 0.70f * h, cy + 0.48f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
57 -> {
|
||||
path.moveTo(cx - 0.64f * h, cy - 0.22f * h)
|
||||
path.lineTo(cx - 0.64f * h, cy + 0.20f * h)
|
||||
path.lineTo(cx + 0.64f * h, cy + 0.20f * h)
|
||||
path.lineTo(cx + 0.64f * h, cy - 0.22f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
42, 54 -> {
|
||||
paint.style = Paint.Style.FILL
|
||||
path.moveTo(cx, cy - 0.55f * h)
|
||||
path.lineTo(cx + 0.62f * h, cy + 0.02f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.02f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.32f * h)
|
||||
path.lineTo(cx - 0.26f * h, cy + 0.32f * h)
|
||||
path.lineTo(cx - 0.26f * h, cy + 0.02f * h)
|
||||
path.lineTo(cx - 0.62f * h, cy + 0.02f * h)
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
path.reset()
|
||||
path.moveTo(cx - 0.26f * h, cy + 0.44f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.44f * h)
|
||||
path.lineTo(cx + 0.26f * h, cy + 0.58f * h)
|
||||
path.lineTo(cx - 0.26f * h, cy + 0.58f * h)
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
29, 97 -> {
|
||||
path.moveTo(cx - 0.58f * h, cy + 0.26f * h)
|
||||
path.lineTo(cx, cy - 0.28f * h)
|
||||
path.lineTo(cx + 0.58f * h, cy + 0.26f * h)
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
125 -> {
|
||||
paint.style = Paint.Style.FILL
|
||||
val gap = 0.07f * h
|
||||
val side = 0.34f * h
|
||||
for (dxi in -1..1 step 2) for (dyi in -1..1 step 2) {
|
||||
val left = cx + dxi * (gap + side / 2f) - side / 2f
|
||||
val top = cy + dyi * (gap + side / 2f) - side / 2f
|
||||
path.addRect(left, top, left + side, top + side, Path.Direction.CW)
|
||||
}
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
|
||||
103, 108, 105, 106 -> {
|
||||
paint.style = Paint.Style.FILL
|
||||
val r = 0.52f * h
|
||||
when (code) {
|
||||
103 -> {
|
||||
path.moveTo(cx, cy - r)
|
||||
path.lineTo(cx + r * 0.80f, cy + r * 0.55f)
|
||||
path.lineTo(cx - r * 0.80f, cy + r * 0.55f)
|
||||
}
|
||||
|
||||
108 -> {
|
||||
path.moveTo(cx, cy + r)
|
||||
path.lineTo(cx + r * 0.80f, cy - r * 0.55f)
|
||||
path.lineTo(cx - r * 0.80f, cy - r * 0.55f)
|
||||
}
|
||||
|
||||
105 -> {
|
||||
path.moveTo(cx - r, cy)
|
||||
path.lineTo(cx + r * 0.55f, cy - r * 0.80f)
|
||||
path.lineTo(cx + r * 0.55f, cy + r * 0.80f)
|
||||
}
|
||||
|
||||
else -> {
|
||||
path.moveTo(cx + r, cy)
|
||||
path.lineTo(cx - r * 0.55f, cy - r * 0.80f)
|
||||
path.lineTo(cx - r * 0.55f, cy + r * 0.80f)
|
||||
}
|
||||
}
|
||||
path.close()
|
||||
canvas.drawPath(path, paint)
|
||||
}
|
||||
}
|
||||
paint.style = Paint.Style.FILL
|
||||
paint.strokeCap = Paint.Cap.BUTT
|
||||
paint.strokeJoin = Paint.Join.MITER
|
||||
return true
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,140 @@
|
||||
package com.droiddeck.launcher.input
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
private val ICP_NAMES: Map<Int, String> = mapOf(
|
||||
103 to "KEY_UP", 106 to "KEY_RIGHT", 108 to "KEY_DOWN", 105 to "KEY_LEFT",
|
||||
28 to "KEY_ENTER", 1 to "KEY_ESC", 14 to "KEY_BKSP", 111 to "KEY_DEL", 15 to "KEY_TAB",
|
||||
57 to "KEY_SPACE", 29 to "KEY_CTRL_L", 97 to "KEY_CTRL_R", 110 to "KEY_INSERT",
|
||||
42 to "KEY_SHIFT_L", 54 to "KEY_SHIFT_R", 56 to "KEY_ALT_L", 100 to "KEY_ALT_R",
|
||||
102 to "KEY_HOME", 107 to "KEY_END", 99 to "KEY_PRTSCN", 104 to "KEY_PG_UP", 109 to "KEY_PG_DOWN",
|
||||
58 to "KEY_CAPS_LOCK", 69 to "KEY_NUM_LOCK", 41 to "KEY_GRAVE", 12 to "KEY_MINUS",
|
||||
11 to "KEY_0", 2 to "KEY_1", 3 to "KEY_2", 4 to "KEY_3", 5 to "KEY_4", 6 to "KEY_5",
|
||||
7 to "KEY_6", 8 to "KEY_7", 9 to "KEY_8", 10 to "KEY_9",
|
||||
30 to "KEY_A", 48 to "KEY_B", 46 to "KEY_C", 32 to "KEY_D", 18 to "KEY_E", 33 to "KEY_F",
|
||||
34 to "KEY_G", 35 to "KEY_H", 23 to "KEY_I", 36 to "KEY_J", 37 to "KEY_K", 38 to "KEY_L",
|
||||
50 to "KEY_M", 49 to "KEY_N", 24 to "KEY_O", 25 to "KEY_P", 16 to "KEY_Q", 19 to "KEY_R",
|
||||
31 to "KEY_S", 20 to "KEY_T", 22 to "KEY_U", 47 to "KEY_V", 17 to "KEY_W", 45 to "KEY_X",
|
||||
21 to "KEY_Y", 44 to "KEY_Z",
|
||||
26 to "KEY_BRACKET_LEFT", 27 to "KEY_BRACKET_RIGHT", 43 to "KEY_BACKSLASH", 53 to "KEY_SLASH",
|
||||
39 to "KEY_SEMICOLON", 51 to "KEY_COMMA", 52 to "KEY_PERIOD", 40 to "KEY_APOSTROPHE",
|
||||
59 to "KEY_F1", 60 to "KEY_F2", 61 to "KEY_F3", 62 to "KEY_F4", 63 to "KEY_F5", 64 to "KEY_F6",
|
||||
65 to "KEY_F7", 66 to "KEY_F8", 67 to "KEY_F9", 68 to "KEY_F10", 87 to "KEY_F11", 88 to "KEY_F12",
|
||||
79 to "KEY_KP_1", 80 to "KEY_KP_2", 81 to "KEY_KP_3", 75 to "KEY_KP_4", 76 to "KEY_KP_5",
|
||||
77 to "KEY_KP_6", 71 to "KEY_KP_7", 72 to "KEY_KP_8", 73 to "KEY_KP_9", 82 to "KEY_KP_0",
|
||||
)
|
||||
|
||||
private val ICP_CODES: Map<String, Int> = ICP_NAMES.entries.associate { (code, name) -> name to code }
|
||||
|
||||
private fun icpShape(shape: Int): String = when (shape) {
|
||||
SCREEN_KEY_CIRCLE -> "CIRCLE"
|
||||
SCREEN_KEY_RECT -> "RECT"
|
||||
SCREEN_KEY_ROUND -> "ROUND_RECT"
|
||||
SCREEN_KEY_DPAD -> "ROUND_RECT"
|
||||
SCREEN_KEY_STICK -> "ROUND_RECT"
|
||||
SCREEN_KEY_TRIGGER -> "ROUND_RECT"
|
||||
else -> "ROUND_RECT"
|
||||
}
|
||||
|
||||
private fun screenKeyShape(shape: String): Int = when (shape) {
|
||||
"CIRCLE" -> SCREEN_KEY_CIRCLE
|
||||
"RECT" -> SCREEN_KEY_RECT
|
||||
"SQUARE" -> SCREEN_KEY_ROUND
|
||||
else -> SCREEN_KEY_PILL
|
||||
}
|
||||
|
||||
fun exportScreenLayout(keys: List<ScreenKey>, widthPx: Int, density: Float): String {
|
||||
val snap = widthPx / 100f
|
||||
val elements = JSONArray()
|
||||
keys.forEach { key ->
|
||||
val binds = JSONArray()
|
||||
val directional = key.shape == SCREEN_KEY_DPAD || key.shape == SCREEN_KEY_STICK
|
||||
key.codes.forEach { code ->
|
||||
val name = ICP_NAMES[code]
|
||||
if (name != null) binds.put(name) else if (directional) binds.put("")
|
||||
}
|
||||
if (binds.length() == 0) return@forEach
|
||||
val shape = icpShape(key.shape)
|
||||
val sizePx = key.size * density
|
||||
val scale = when {
|
||||
key.shape == SCREEN_KEY_DPAD -> sizePx * 2.3f / (14f * snap)
|
||||
key.shape == SCREEN_KEY_STICK -> sizePx * 2.3f / (14f * snap)
|
||||
shape == "CIRCLE" -> sizePx / (6f * snap)
|
||||
else -> sizePx / (4f * snap)
|
||||
}
|
||||
elements.put(
|
||||
JSONObject()
|
||||
.put(
|
||||
"type",
|
||||
when (key.shape) {
|
||||
SCREEN_KEY_DPAD -> "D_PAD"
|
||||
SCREEN_KEY_STICK -> "STICK"
|
||||
else -> "BUTTON"
|
||||
},
|
||||
)
|
||||
.put("shape", shape)
|
||||
.put("customColor", 0)
|
||||
.put("bindings", binds)
|
||||
.put("scale", scale)
|
||||
.put("x", key.x)
|
||||
.put("y", key.y)
|
||||
.put("toggleSwitch", key.toggle)
|
||||
.put("swipeable", false)
|
||||
.put("text", key.text)
|
||||
.put("iconId", 0)
|
||||
.apply { if (key.opacity < 1f) put("opacity", key.opacity) },
|
||||
)
|
||||
}
|
||||
return JSONObject()
|
||||
.put("id", 0)
|
||||
.put("name", "DroidDeck")
|
||||
.put("cursorSpeed", 1.5f)
|
||||
.put("elements", elements)
|
||||
.toString()
|
||||
}
|
||||
|
||||
fun importScreenLayout(raw: String, widthPx: Int, density: Float): List<ScreenKey>? {
|
||||
val root = runCatching { JSONObject(raw) }.getOrNull() ?: return null
|
||||
val elements = root.optJSONArray("elements") ?: return null
|
||||
val snap = widthPx / 100f
|
||||
val keys = mutableListOf<ScreenKey>()
|
||||
for (i in 0 until elements.length()) {
|
||||
val element = elements.optJSONObject(i) ?: continue
|
||||
val type = element.optString("type", "BUTTON")
|
||||
if (type != "BUTTON" && type != "D_PAD" && type != "STICK") continue
|
||||
val binds = element.optJSONArray("bindings") ?: continue
|
||||
val raw = (0 until binds.length()).map { ICP_CODES[binds.optString(it)] ?: 0 }
|
||||
val codes = if (type == "D_PAD" || type == "STICK") {
|
||||
raw.dropLastWhile { it == 0 }.ifEmpty { listOf(0) }
|
||||
} else {
|
||||
raw.filter { it != 0 }
|
||||
}
|
||||
if (codes.isEmpty()) continue
|
||||
val dpad = type == "D_PAD"
|
||||
val stick = type == "STICK"
|
||||
val shape = when {
|
||||
dpad -> SCREEN_KEY_DPAD
|
||||
stick -> SCREEN_KEY_STICK
|
||||
else -> screenKeyShape(element.optString("shape"))
|
||||
}
|
||||
val scale = element.optDouble("scale", 1.0).toFloat().takeIf { it > 0f } ?: 1f
|
||||
val sizePx = when {
|
||||
dpad || stick -> 14f * snap * scale / 2.3f
|
||||
shape == SCREEN_KEY_CIRCLE -> 6f * snap * scale
|
||||
else -> 4f * snap * scale
|
||||
}
|
||||
keys += ScreenKey(
|
||||
codes = codes,
|
||||
x = element.optDouble("x", 0.5).toFloat().coerceIn(0f, 1f),
|
||||
y = element.optDouble("y", 0.5).toFloat().coerceIn(0f, 1f),
|
||||
size = (sizePx / density).roundToInt().coerceIn(ScreenKeys.MIN_SIZE, ScreenKeys.MAX_SIZE),
|
||||
text = element.optString("text").take(24),
|
||||
opacity = element.optDouble("opacity", 1.0).toFloat().coerceIn(0.2f, 1f),
|
||||
shape = shape,
|
||||
toggle = element.optBoolean("toggleSwitch"),
|
||||
)
|
||||
}
|
||||
return keys
|
||||
}
|
||||
@@ -93,13 +93,18 @@ class SteamTouchControls(
|
||||
private var opened = 0
|
||||
private var elements: List<Element> = emptyList()
|
||||
private var tint = Color.WHITE
|
||||
private var alphaScale = 0.45f
|
||||
private var alphaScale = 0.99f
|
||||
var style: Pair<Int, Float> = Color.WHITE to 0.99f
|
||||
private set
|
||||
var onStyle: (() -> Unit)? = null
|
||||
|
||||
private class Visual(val ref: SteamTouchBindings.Ref?, val binding: SteamTouchBindings.Binding?)
|
||||
private var visuals: Map<Int, List<Visual>> = emptyMap()
|
||||
|
||||
private class Finger(val element: Element?, var x: Float, var y: Float, val toolbar: Int = -1)
|
||||
private val fingers = HashMap<Int, Finger>()
|
||||
private var forwardedPointerBits = 0
|
||||
var onTouchMiss: ((MotionEvent) -> Unit)? = null
|
||||
|
||||
private var editing = false
|
||||
private var editElements: MutableList<Element> = mutableListOf()
|
||||
@@ -191,6 +196,8 @@ class SteamTouchControls(
|
||||
private fun applyColor(color: FloatArray?) {
|
||||
tint = if (color != null) Color.rgb((color[0] * 255).toInt(), (color[1] * 255).toInt(), (color[2] * 255).toInt()) else Color.WHITE
|
||||
alphaScale = ((color?.get(3) ?: 0.45f) * 2.2f).coerceIn(0.15f, 1f)
|
||||
style = tint to alphaScale
|
||||
onStyle?.invoke()
|
||||
}
|
||||
|
||||
private fun rebuildVisuals() {
|
||||
@@ -624,7 +631,17 @@ class SteamTouchControls(
|
||||
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> {
|
||||
val x = event.getX(index)
|
||||
val y = event.getY(index)
|
||||
val e = hit(live(), x, y) ?: return event.actionMasked == MotionEvent.ACTION_POINTER_DOWN
|
||||
if (event.actionMasked == MotionEvent.ACTION_DOWN) forwardedPointerBits = 0
|
||||
val e = hit(live(), x, y)
|
||||
if (e == null) {
|
||||
if (event.actionMasked == MotionEvent.ACTION_DOWN) return false
|
||||
if (onTouchMiss != null) {
|
||||
forwardedPointerBits = forwardedPointerBits or (1 shl event.getPointerId(index))
|
||||
forwardPointers(event, forwardedPointerBits)
|
||||
}
|
||||
return true
|
||||
}
|
||||
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
|
||||
fingers[event.getPointerId(index)] = Finger(e, x, y)
|
||||
if (feedback) performHapticFeedback(android.view.HapticFeedbackConstants.VIRTUAL_KEY)
|
||||
requestUnbufferedDispatch(event)
|
||||
@@ -632,6 +649,7 @@ class SteamTouchControls(
|
||||
return true
|
||||
}
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
|
||||
for (i in 0 until event.pointerCount) {
|
||||
val f = fingers[event.getPointerId(i)] ?: continue
|
||||
f.x = event.getX(i)
|
||||
@@ -650,12 +668,14 @@ class SteamTouchControls(
|
||||
}
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
val id = event.getPointerId(index)
|
||||
if (forwardedPointerBits != 0) forwardPointers(event, forwardedPointerBits)
|
||||
forwardedPointerBits = if (event.actionMasked == MotionEvent.ACTION_CANCEL) 0 else forwardedPointerBits and (1 shl id).inv()
|
||||
val f = fingers.remove(id)
|
||||
if (event.actionMasked == MotionEvent.ACTION_CANCEL) fingers.clear()
|
||||
val e = f?.element
|
||||
if (event.actionMasked != MotionEvent.ACTION_CANCEL && e != null && reach(e, event.getX(index), event.getY(index)) <= 1.3f) {
|
||||
when (e.type) {
|
||||
SteamTouchConfig.THUMB -> openMenu()
|
||||
SteamTouchConfig.THUMB -> { cancelForwarded(event); openMenu() }
|
||||
SteamTouchConfig.KEYBOARD -> onKeyboard()
|
||||
SteamTouchConfig.PASTE -> onPaste()
|
||||
}
|
||||
@@ -668,6 +688,25 @@ class SteamTouchControls(
|
||||
}
|
||||
|
||||
|
||||
private fun cancelForwarded(event: MotionEvent) {
|
||||
val split = event.pointerSubset(forwardedPointerBits).also { forwardedPointerBits = 0 } ?: return
|
||||
split.action = MotionEvent.ACTION_CANCEL
|
||||
try {
|
||||
onTouchMiss?.invoke(split)
|
||||
} finally {
|
||||
split.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
private fun forwardPointers(event: MotionEvent, pointerIdBits: Int) {
|
||||
val split = event.pointerSubset(pointerIdBits) ?: return
|
||||
try {
|
||||
onTouchMiss?.invoke(split)
|
||||
} finally {
|
||||
split.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
val isEditing get() = editing
|
||||
|
||||
fun startEditing() {
|
||||
|
||||
Reference in new issue
Block a user