diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt index e3daeb5..03ad430 100644 --- a/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/GearslipRunner.kt @@ -433,7 +433,12 @@ class GearslipRunner( } private fun onVersionRequest(body: ByteArray) { - if (state != State.WAIT_VERSION) return + // Android Auto answers every version request that comes before TLS. Open Headunit asks + // for 6.1, then again for 1.6 when it gets 1.7 back, and both sides waited while + // Gearslip ignored the second one (issue #163). + val again = state == State.TLS_HANDSHAKE && !tlsBytesSeen + if (state != State.WAIT_VERSION && !again) return + if (again) log.i("another VersionRequest before TLS - answering it too") if (body.size < 4) { log.w("VersionRequest too short (${body.size} bytes)") return @@ -458,8 +463,12 @@ class GearslipRunner( log.i("awaiting ClientHello - the head unit is the TLS client, we are the server") } + /** The car has started TLS, so a VersionRequest now would be out of turn. */ + @Volatile private var tlsBytesSeen = false + private fun onHandshake(body: ByteArray) { if (state != State.TLS_HANDSHAKE && state != State.WAIT_AUTH) return + tlsBytesSeen = true log.i("<- EncapsulatedSSL (${body.size} bytes)") val replies = try { val preparedTls = tls @@ -811,12 +820,12 @@ class GearslipRunner( openChannel(channel) } - /** The turn card on the instrument cluster, for a car with a cluster screen: experimental, off by default. */ + /** The turn card on the instrument cluster, for a car with a cluster screen: off by default. */ private fun startClusterVideo(serviceDiscoveryResponse: ByteArray) { val sink = ServiceDiscovery.findClusterVideoService(serviceDiscoveryResponse) ?: return log.i("cluster video sink: channel=${sink.serviceId} display=${sink.displayId} type=${sink.displayType}") - if (!CarSettings.experimentalFeaturesEnabled.value || !CarSettings.clusterVideoEnabled.value) { - log.i("cluster screen is off in the car's Settings (experimental) - leaving that sink alone") + if (!CarSettings.clusterVideoEnabled.value) { + log.i("cluster screen is off in the car's Settings - leaving that sink alone") return } val context = app.seb3thehacker.gearslip.car.CarEnvironment.appContext ?: return @@ -1100,9 +1109,17 @@ class GearslipRunner( } // Time the next frame against this tap: see [reportTouchLatency]. if (action == ACTION_DOWN || action == ACTION_UP) touchAtUs = System.nanoTime() / 1000 - projection?.onTouch(action, actionIndex, points) + val before = touchSequence.fixed + val sent = touchSequence.next(InputProtocol.Touch(action, actionIndex, points)) + if (touchSequence.fixed > before && touchSequence.fixed <= 3) { + log.i("touch: the car's gesture had no press, sent as ${sent.joinToString { it.action.toString() }}") + } + sent.forEach { projection?.onTouch(it.action, it.actionIndex, it.points) } } + /** Repairs gestures from cars that never send a press (issue #81). */ + private val touchSequence = app.seb3thehacker.gearslip.protocol.TouchSequence() + /** This drive's taps came in past the edge of the picture, so the car reports in its panel's size. */ @Volatile private var touchPanelCoordinates = false diff --git a/gearslip/src/main/java/app/seb3thehacker/gearslip/protocol/TouchSequence.kt b/gearslip/src/main/java/app/seb3thehacker/gearslip/protocol/TouchSequence.kt new file mode 100644 index 0000000..46ab489 --- /dev/null +++ b/gearslip/src/main/java/app/seb3thehacker/gearslip/protocol/TouchSequence.kt @@ -0,0 +1,55 @@ +package app.seb3thehacker.gearslip.protocol + +/** + * Turns the car's touches into gestures Android accepts. Android only treats a touch as a press + * when it starts with ACTION_DOWN, and some head units never send one: a 2020 Subaru (Harman, + * issue #81) starts each press with POINTER_DOWN, sends quick taps as a bare UP, and lifts the + * last of two fingers with UP twice. Nothing on the screen reacted to its taps. This keeps track + * of which fingers are down and rewrites, or adds, the actions a well-formed gesture needs. A car + * that already sends proper gestures passes through unchanged. + */ +internal class TouchSequence { + private val down = mutableSetOf() + + /** How many touches this drive needed fixing, for the log. */ + var fixed = 0 + private set + + /** The touches to hand on, in order: usually [touch] alone, sometimes a press added before it. */ + fun next(touch: InputProtocol.Touch): List { + val one = touch.points.size == 1 + val out = when (touch.action) { + DOWN -> listOf(touch) + POINTER_DOWN -> if (down.isEmpty() && one) listOf(touch.copy(action = DOWN)) else listOf(touch) + MOVE -> if (down.isEmpty() && one) listOf(touch.copy(action = DOWN), touch) else listOf(touch) + UP -> when { + !one -> listOf(touch.copy(action = POINTER_UP)) + down.isEmpty() -> listOf(touch.copy(action = DOWN), touch) + else -> listOf(touch) + } + POINTER_UP -> if (one) listOf(touch.copy(action = UP)) else listOf(touch) + else -> listOf(touch) + } + if (out.size != 1 || out[0].action != touch.action) fixed++ + out.forEach(::track) + return out + } + + private fun track(touch: InputProtocol.Touch) { + val id = touch.points[touch.actionIndex].id + when (touch.action) { + DOWN -> { down.clear(); down += touch.points.map { it.id } } + POINTER_DOWN -> down += id + POINTER_UP -> down -= id + UP -> down.clear() + } + } + + private companion object { + const val DOWN = 0 + const val UP = 1 + const val MOVE = 2 + const val POINTER_DOWN = 5 + const val POINTER_UP = 6 + } +} diff --git a/gearslip/src/test/java/app/seb3thehacker/gearslip/protocol/TouchSequenceTest.kt b/gearslip/src/test/java/app/seb3thehacker/gearslip/protocol/TouchSequenceTest.kt new file mode 100644 index 0000000..6853940 --- /dev/null +++ b/gearslip/src/test/java/app/seb3thehacker/gearslip/protocol/TouchSequenceTest.kt @@ -0,0 +1,37 @@ +package app.seb3thehacker.gearslip.protocol + +import app.seb3thehacker.gearslip.TouchPoint +import org.junit.Assert.assertEquals +import org.junit.Test + +class TouchSequenceTest { + private fun t(action: Int, vararg ids: Int, index: Int = 0) = + InputProtocol.Touch(action, index, ids.map { TouchPoint(it, 10f, 20f) }) + + private fun TouchSequence.actions(vararg touches: InputProtocol.Touch) = + touches.flatMap { next(it) }.map { it.action } + + @Test fun `a proper gesture passes through unchanged`() { + val s = TouchSequence() + assertEquals(listOf(0, 2, 5, 6, 1), s.actions(t(0, 1), t(2, 1), t(5, 1, 2, index = 1), t(6, 1, 2, index = 1), t(1, 1))) + assertEquals(0, s.fixed) + } + + @Test fun `a press that starts with a second finger becomes a press`() { + // The Subaru in issue #81: 5, 2, 1. + assertEquals(listOf(0, 2, 1), TouchSequence().actions(t(5, 1), t(2, 1), t(1, 1))) + } + + @Test fun `a bare lift gets a press before it`() { + assertEquals(listOf(0, 1, 0, 1), TouchSequence().actions(t(1, 1), t(1, 1))) + } + + @Test fun `two fingers lifted as two ups end as pointer up then up`() { + val s = TouchSequence() + assertEquals(listOf(0, 5, 6, 1), s.actions(t(0, 1), t(5, 1, 2, index = 1), t(1, 1, 2, index = 1), t(1, 1))) + } + + @Test fun `a move with nothing down starts a press`() { + assertEquals(listOf(0, 2, 1), TouchSequence().actions(t(2, 1), t(1, 1))) + } +}