Skip to content

Commit 96c6e04

Browse files
committed
#1926 fix: PRO mode key maps work after disconnecting and connecting the devices
1 parent d4a2903 commit 96c6e04

2 files changed

Lines changed: 45 additions & 53 deletions

File tree

base/src/main/java/io/github/sds100/keymapper/base/input/EvdevDevicesDelegate.kt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,22 @@ class EvdevDevicesDelegate @Inject constructor(
5151

5252
init {
5353
coroutineScope.launch {
54-
coroutineScope.launch {
55-
// Only listen to changes in the connected input devices if the system bridge
56-
// is connected.
57-
systemBridgeConnectionManager.connectionState.flatMapLatest { connectionState ->
58-
when (connectionState) {
59-
is SystemBridgeConnectionState.Connected -> {
60-
devicesAdapter.connectedInputDevices.onEach {
61-
allDevices.value = fetchAllDevices()
62-
}
63-
}
64-
is SystemBridgeConnectionState.Disconnected -> {
65-
allDevices.value = emptyList()
66-
grabbedDevicesById.value = emptyMap()
67-
emptyFlow()
54+
// Only listen to changes in the connected input devices if the system bridge
55+
// is connected.
56+
systemBridgeConnectionManager.connectionState.flatMapLatest { connectionState ->
57+
when (connectionState) {
58+
is SystemBridgeConnectionState.Connected -> {
59+
devicesAdapter.connectedInputDevices.onEach {
60+
allDevices.value = fetchAllDevices()
6861
}
6962
}
70-
}.collect()
71-
}
63+
is SystemBridgeConnectionState.Disconnected -> {
64+
allDevices.value = emptyList()
65+
grabbedDevicesById.value = emptyMap()
66+
emptyFlow()
67+
}
68+
}
69+
}.collect()
7270
}
7371

7472
// Process on another thread because system bridge grabbing calls are blocking

system/src/main/java/io/github/sds100/keymapper/system/devices/AndroidDevicesAdapter.kt

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.bluetooth.BluetoothDevice
44
import android.bluetooth.BluetoothManager
55
import android.content.Context
66
import android.hardware.input.InputManager
7-
import android.os.Handler
8-
import android.os.Looper
97
import android.view.InputDevice
108
import androidx.core.content.getSystemService
119
import dagger.hilt.android.qualifiers.ApplicationContext
@@ -37,7 +35,8 @@ class AndroidDevicesAdapter @Inject constructor(
3735
private val bluetoothAdapter: io.github.sds100.keymapper.system.bluetooth.BluetoothAdapter,
3836
private val permissionAdapter: PermissionAdapter,
3937
private val coroutineScope: CoroutineScope,
40-
) : DevicesAdapter {
38+
) : DevicesAdapter,
39+
InputManager.InputDeviceListener {
4140

4241
private val ctx = context.applicationContext
4342
private val inputManager = ctx.getSystemService<InputManager>()
@@ -69,40 +68,7 @@ class AndroidDevicesAdapter @Inject constructor(
6968
}
7069
}
7170

72-
inputManager?.apply {
73-
registerInputDeviceListener(
74-
object : InputManager.InputDeviceListener {
75-
override fun onInputDeviceAdded(deviceId: Int) {
76-
coroutineScope.launch {
77-
val device = InputDevice.getDevice(deviceId) ?: return@launch
78-
onInputDeviceConnect.emit(
79-
InputDeviceUtils.createInputDeviceInfo(device),
80-
)
81-
82-
updateInputDevices()
83-
}
84-
}
85-
86-
override fun onInputDeviceRemoved(deviceId: Int) {
87-
coroutineScope.launch {
88-
connectedInputDevices.value.ifIsData { connectedInputDevices ->
89-
val device = connectedInputDevices.find { it.id == deviceId }
90-
?: return@ifIsData
91-
92-
onInputDeviceDisconnect.emit(device)
93-
}
94-
95-
updateInputDevices()
96-
}
97-
}
98-
99-
override fun onInputDeviceChanged(deviceId: Int) {
100-
updateInputDevices()
101-
}
102-
},
103-
Handler(Looper.getMainLooper()),
104-
)
105-
}
71+
inputManager?.registerInputDeviceListener(this, null)
10672

10773
bluetoothAdapter.onDeviceConnect.onEach { device ->
10874
val currentValue = connectedBluetoothDevices.value
@@ -146,6 +112,34 @@ class AndroidDevicesAdapter @Inject constructor(
146112
return InputDevice.getDevice(deviceId)?.let { InputDeviceUtils.createInputDeviceInfo(it) }
147113
}
148114

115+
override fun onInputDeviceAdded(deviceId: Int) {
116+
coroutineScope.launch {
117+
val device = InputDevice.getDevice(deviceId) ?: return@launch
118+
onInputDeviceConnect.emit(
119+
InputDeviceUtils.createInputDeviceInfo(device),
120+
)
121+
122+
updateInputDevices()
123+
}
124+
}
125+
126+
override fun onInputDeviceRemoved(deviceId: Int) {
127+
coroutineScope.launch {
128+
connectedInputDevices.value.ifIsData { connectedInputDevices ->
129+
val device = connectedInputDevices.find { it.id == deviceId }
130+
?: return@ifIsData
131+
132+
onInputDeviceDisconnect.emit(device)
133+
}
134+
135+
updateInputDevices()
136+
}
137+
}
138+
139+
override fun onInputDeviceChanged(deviceId: Int) {
140+
updateInputDevices()
141+
}
142+
149143
private fun updateInputDevices() {
150144
val devices = mutableListOf<InputDeviceInfo>()
151145

0 commit comments

Comments
 (0)