Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
fix(android): the phone's keyboard comes back after typing from the PC (#93)
Bee37ac6b7f6729
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.ktindex af7cdfb..3f5a38a 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt@@ -812,6 +812,14 @@ object InputServer {private const val UHID_EVENT_SIZE = 4380private const val BUS_USB = 0x03+ // While a hardware keyboard is attached Android hides the on-screen+ // one, so the virtual keyboard only stays while the PC is typing.+ private const val KEYBOARD_IDLE_MS = 10_000L+ private val idle = java.util.concurrent.Executors.newSingleThreadScheduledExecutor { r ->+ Thread(r, "nectarlink-uhid-idle").apply { isDaemon = true }+ }+ private var idleClose: java.util.concurrent.ScheduledFuture<*>? = null+// Standard 8-byte boot HID keyboard report descriptor.private val KEYBOARD_RD = byteArrayOf(0x05, 0x01, // Usage Page (Generic Desktop)@@ -917,16 +925,40 @@ object InputServer {@Synchronizedfun sendKeyTap(androidKeyCode: Int): Boolean {val usage = androidKeyCodeToHidUsage(androidKeyCode) ?: return false- if (!keyboardAttempted) {+ val created = !keyboardAttempted+ if (created) {keyboardAttempted = truekeyboardFd = openDevice("NectarLink Virtual Keyboard", 0x4EE7, KEYBOARD_RD)}val fd = keyboardFd ?: return false+ idleClose?.cancel(false)+ idleClose = idle.schedule({ closeKeyboard() }, KEYBOARD_IDLE_MS, java.util.concurrent.TimeUnit.MILLISECONDS)+ // A device that was just created may not be registered in time for+ // its first report: that key goes the injected way.+ if (created) return falseval down = ByteArray(8).apply { this[2] = usage.toByte() }val up = ByteArray(8)return writeInputReport(fd, down) && writeInputReport(fd, up)}+ /** Removes the virtual keyboard; the next key from the PC adds it again. */+ @Synchronized+ private fun closeKeyboard() {+ keyboardFd?.let(::destroy)+ keyboardFd = null+ keyboardAttempted = false+ idleClose = null+ }++ private fun destroy(fd: java.io.RandomAccessFile) {+ runCatching {+ val buf = java.nio.ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN)+ buf.putInt(UHID_DESTROY)+ fd.write(buf.array())+ fd.close()+ }+ }+@Synchronizedfun sendGamepadReport(buttons: Int, lx: Int, ly: Int, rx: Int, ry: Int, lt: Int, rt: Int): Boolean {if (!gamepadAttempted) {@@ -948,14 +980,9 @@ object InputServer {@Synchronizedfun closeAll() {- for (fd in listOfNotNull(keyboardFd, gamepadFd)) {- runCatching {- val buf = java.nio.ByteBuffer.allocate(4).order(java.nio.ByteOrder.LITTLE_ENDIAN)- buf.putInt(UHID_DESTROY)- fd.write(buf.array())- fd.close()- }- }+ idleClose?.cancel(false)+ idleClose = null+ listOfNotNull(keyboardFd, gamepadFd).forEach(::destroy)keyboardFd = nullgamepadFd = null}