-
Notifications
You must be signed in to change notification settings - Fork 43
/
adb-event-mirror.main.kts
executable file
·232 lines (206 loc) · 7.22 KB
/
adb-event-mirror.main.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env -S kotlinc -script --
@file:DependsOn("com.github.ajalt:clikt:2.8.0")
@file:DependsOn("junit:junit:4.13")
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.arguments.transformAll
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.JUnitCore
import kotlin.concurrent.thread
if (args.contentEquals(arrayOf("--test"))) {
JUnitCore.main(AdbEventMirrorTest::class.java.name.toString())
} else {
AdbEventMirrorCommand.main(args)
}
object AdbEventMirrorCommand : CliktCommand(name = "adb-event-mirror") {
private val debug by option("--debug", help = "Enable debug logging").flag()
private val showTouches by option("--show-touches").flag("--hide-touches", default = true)
private val mirrorSerials by argument(name = "MIRROR_SERIAL")
.multiple(true)
.transformAll { it.toSet() }
override fun run() {
val mirrors = mirrorSerials.map(::createConnection)
println("ready!\n")
System.`in`
.bufferedReader()
.lineSequence()
.onEach { line ->
if (debug) {
println("[host] $line")
}
}
.mapNotNull(eventLine::matchEntire)
.forEach { match ->
val (input, typeHex, codeHex, valueHex) = match.destructured
val type = typeHex.toInt(16)
val code = codeHex.toLong(16)
val value = valueHex.toLong(16)
if (!debug) {
println("EVENT $input $type $code $value")
}
for (mirror in mirrors) {
mirror.sendEvent(input, type, code, value)
}
}
for (mirror in mirrors) {
mirror.detach()
}
}
fun parseInputDevices(output: String): Map<Int, String> {
fun isDeviceNumberLower(old: String, new: String): Boolean {
val oldNumber = old.substringAfter("/event").toInt()
val newNumber = new.substringAfter("/event").toInt()
return newNumber < oldNumber
}
val inputDevices = mutableMapOf<Int, String>()
var lastInputDevice: String? = null
for (line in output.lines()) {
addDeviceLine.matchEntire(line)?.let { match ->
lastInputDevice = match.groupValues[1]
}
eventTypeLine.matchEntire(line)?.let { match ->
if (lastInputDevice!!.endsWith("/event0")) {
// Ignore 'event0' as a quick hack. TODO actually map event codes too.
return@let
}
val type = match.groupValues[1].toInt(16) // TODO is this actually hex here?
val previous = inputDevices[type]
if (previous == null || isDeviceNumberLower(previous, lastInputDevice!!)) {
inputDevices[type] = lastInputDevice!!
}
}
}
return inputDevices
}
private interface DeviceConnection {
fun sendEvent(hostInputDevice: String, type: Int, code: Long, value: Long)
fun detach()
}
private fun createConnection(serial: String): DeviceConnection {
val inputDevicesOutput = ProcessBuilder()
.command("adb", "-s", serial, "shell", "getevent -pl")
.start()
.inputStream
.bufferedReader()
.readText()
val inputDevices = parseInputDevices(inputDevicesOutput)
if (debug) {
println("[$serial] devices: $inputDevices")
}
val showTouchesOriginalValue = ProcessBuilder()
.command("adb", "-s", serial, "shell", "settings get system show_touches")
.start()
.inputStream
.bufferedReader()
.readText()
.trim() == "1"
if (showTouches) {
ProcessBuilder()
.command("adb", "-s", serial, "shell", "settings put system show_touches 1")
.start()
.waitFor()
}
val process = ProcessBuilder()
.command("adb", "-s", serial, "shell")
.start()
Runtime.getRuntime().addShutdownHook(thread(start = false) {
process.destroy()
if (showTouches) {
val oldValue = if (showTouchesOriginalValue) "1" else "0"
ProcessBuilder()
.command("adb", "-s", serial, "shell", "settings put system show_touches $oldValue")
.start()
.waitFor()
}
})
val outputStream = process.outputStream
fun sendCommand(command: String) {
if (debug) {
println("[$serial] $ $command")
}
outputStream.write("$command\n".toByteArray())
outputStream.flush()
}
sendCommand("su")
return object : DeviceConnection {
private val hostInputToSelfInput = mutableMapOf<String, String>()
override fun sendEvent(hostInputDevice: String, type: Int, code: Long, value: Long) {
val inputDevice = hostInputToSelfInput.computeIfAbsent(hostInputDevice) {
val result = inputDevices.getValue(type)
if (debug) {
println("[$serial] $result will be used for host $it")
}
result
}
sendCommand("sendevent $inputDevice $type $code $value")
}
override fun detach() {
sendCommand("exit") // From 'su'
sendCommand("exit") // From 'shell'
process.waitFor()
}
}
}
private val addDeviceLine = Regex("""add device \d+: (.+)""")
private val eventTypeLine = Regex(""" [A-Z]+ \((\d+)\): .*""")
private val eventLine = Regex("""(/dev/input/[^:]+): ([0-9a-f]+) ([0-9a-f]+) ([0-9a-f]+)""")
}
class AdbEventMirrorTest {
@Test fun hello() {
val actual = AdbEventMirrorCommand.parseInputDevices("""
|add device 1: /dev/input/event10
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 2: /dev/input/event0
| KEY (0001): 0074
|add device 3: /dev/input/event8
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 4: /dev/input/event11
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 5: /dev/input/event5
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 6: /dev/input/event2
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 7: /dev/input/event12
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 8: /dev/input/event13
| KEY (0001): 0001 0002 0003 0004 0005 0006 0007 0008
| LED (0011): 0000 0001 0002
|add device 9: /dev/input/event6
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 10: /dev/input/event3
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 11: /dev/input/event1
| KEY (0001): 0001 0002 0003 0004 0005 0006 0007 0008
| MSC (0004): 0004
| LED (0011): 0000 0001 0002
|add device 12: /dev/input/event7
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 13: /dev/input/event4
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|add device 14: /dev/input/event9
| ABS (0003): 0000 : value 0, min 0, max 32767, fuzz 0, flat 0, resolution 0
| SW (0005): 0000
|""".trimMargin())
val expected = mapOf(
1 to "/dev/input/event1",
3 to "/dev/input/event2",
4 to "/dev/input/event1",
17 to "/dev/input/event1"
)
assertEquals(expected, actual)
}
}