mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 20:45:43 +00:00
fix(input): count uinput key holds across devices and drop late clients
This commit is contained in:
1 parent
0591a9d506
commit
869d730996
3 files changed
+62
-2
No files matched your search
@@ -16,6 +16,25 @@ class DesktopUinputDevice(
|
||||
fun wheel(vertical120: Int, horizontal120: Int)
|
||||
}
|
||||
|
||||
class SharedSink(private val out: Sink) : Sink by out {
|
||||
private val keys = IntArray(KEY_MAX + 1)
|
||||
private val buttons = IntArray(KEY_MAX + 1)
|
||||
|
||||
@Synchronized
|
||||
override fun key(code: Int, down: Boolean) { if (hold(keys, code, down)) out.key(code, down) }
|
||||
|
||||
@Synchronized
|
||||
override fun button(code: Int, down: Boolean) { if (hold(buttons, code, down)) out.button(code, down) }
|
||||
|
||||
private fun hold(holds: IntArray, code: Int, down: Boolean): Boolean {
|
||||
if (code !in holds.indices) return false
|
||||
val before = holds[code]
|
||||
if (!down && before == 0) return false
|
||||
holds[code] = before + if (down) 1 else -1
|
||||
return if (down) before == 0 else before == 1
|
||||
}
|
||||
}
|
||||
|
||||
private val heldKeys = HeldInput(sink::key)
|
||||
private val heldButtons = HeldInput(sink::button)
|
||||
private var dx = 0
|
||||
|
||||
@@ -15,6 +15,7 @@ import java.nio.ByteOrder
|
||||
class UinputDesktopComponent : SessionPart() {
|
||||
@Volatile private var server: LocalServerSocket? = null
|
||||
private val clients = HashSet<LocalSocket>()
|
||||
private val sink = DesktopUinputDevice.SharedSink(CompositorSink)
|
||||
|
||||
override fun start() {
|
||||
val s = try { LocalServerSocket(NAME) } catch (e: Exception) { Log.w(TAG, "uinput: no listener ($e)"); return }
|
||||
@@ -26,7 +27,10 @@ class UinputDesktopComponent : SessionPart() {
|
||||
runCatching { client.close() }
|
||||
continue
|
||||
}
|
||||
synchronized(clients) { clients.add(client) }
|
||||
if (!synchronized(clients) { server === s && clients.add(client) }) {
|
||||
runCatching { client.close() }
|
||||
continue
|
||||
}
|
||||
Thread({ serve(client) }, "uinput-device").apply { isDaemon = true; start() }
|
||||
}
|
||||
}, "uinput-desktop").apply { isDaemon = true; start() }
|
||||
@@ -45,7 +49,7 @@ class UinputDesktopComponent : SessionPart() {
|
||||
|
||||
private fun serve(client: LocalSocket) {
|
||||
try {
|
||||
read(client.inputStream, CompositorSink)
|
||||
read(client.inputStream, sink)
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "uinput: device stream ended ($e)")
|
||||
} finally {
|
||||
|
||||
@@ -94,6 +94,43 @@ class UinputDesktopComponentTest {
|
||||
assertEquals(listOf("key 42 true", "button 273 true", "key 42 false", "button 273 false"), r.calls)
|
||||
}
|
||||
|
||||
@Test fun aKeyOrButtonHeldByTwoDevicesIsReleasedOnlyWhenBothLetGo() {
|
||||
val r = Recorder()
|
||||
val shared = DesktopUinputDevice.SharedSink(r)
|
||||
val keyboard = DesktopUinputDevice(null, null, shared)
|
||||
val mouse = DesktopUinputDevice(null, null, shared)
|
||||
keyboard.event(EV_KEY, 42, 1)
|
||||
mouse.event(EV_KEY, 42, 1)
|
||||
keyboard.event(EV_KEY, BTN_LEFT, 1)
|
||||
mouse.event(EV_KEY, BTN_LEFT, 1)
|
||||
keyboard.event(EV_KEY, 42, 0)
|
||||
keyboard.release()
|
||||
assertEquals(listOf("key 42 true", "button $BTN_LEFT true"), r.calls)
|
||||
mouse.release()
|
||||
assertEquals(listOf("key 42 true", "button $BTN_LEFT true", "key 42 false", "button $BTN_LEFT false"), r.calls)
|
||||
}
|
||||
|
||||
@Test fun aSharedSinkPassesASingleDeviceThroughUnchanged() {
|
||||
val direct = Recorder()
|
||||
val shared = Recorder()
|
||||
val events = arrayOf(ev(EV_KEY, 30, 1), syn, ev(EV_KEY, 30, 0), ev(EV_REL, REL_X, 4), ev(EV_KEY, BTN_LEFT, 1), syn,
|
||||
ev(EV_KEY, BTN_LEFT, 0), ev(EV_REL, REL_WHEEL, 1), syn, ev(EV_KEY, 42, 1), syn)
|
||||
UinputDesktopComponent.read(stream(0, 0, 0, 0, *events), direct)
|
||||
UinputDesktopComponent.read(stream(0, 0, 0, 0, *events), DesktopUinputDevice.SharedSink(shared))
|
||||
assertEquals(direct.calls, shared.calls)
|
||||
}
|
||||
|
||||
@Test fun aSharedSinkDropsReleasesNothingHeld() {
|
||||
val r = Recorder()
|
||||
val shared = DesktopUinputDevice.SharedSink(r)
|
||||
shared.key(30, false)
|
||||
shared.button(BTN_LEFT, false)
|
||||
shared.key(30, true)
|
||||
shared.key(30, false)
|
||||
shared.key(30, false)
|
||||
assertEquals(listOf("key 30 true", "key 30 false"), r.calls)
|
||||
}
|
||||
|
||||
@Test fun aStreamWithoutTheHeaderIsIgnored() {
|
||||
val r = Recorder()
|
||||
UinputDesktopComponent.read(stream(0, 0, 0, 0, ev(EV_KEY, 30, 1), syn, magic = 0x1234), r)
|
||||
|
||||
Reference in new issue
Block a user