Skip to content

Commit 7bd5e22

Browse files
committed
#1918 fix more bugs with adding key codes to grabbed devices
1 parent c1d1a93 commit 7bd5e22

5 files changed

Lines changed: 764 additions & 47 deletions

File tree

base/src/main/java/io/github/sds100/keymapper/base/detection/KeyMapDetectionController.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.github.sds100.keymapper.base.trigger.AssistantTriggerType
1212
import io.github.sds100.keymapper.base.trigger.EvdevTriggerKey
1313
import io.github.sds100.keymapper.base.trigger.RecordTriggerController
1414
import io.github.sds100.keymapper.base.trigger.RecordTriggerState
15+
import io.github.sds100.keymapper.common.models.EvdevDeviceInfo
1516
import io.github.sds100.keymapper.common.models.GrabDeviceRequest
1617
import io.github.sds100.keymapper.system.inputevents.KMEvdevEvent
1718
import io.github.sds100.keymapper.system.inputevents.KMInputEvent
@@ -37,7 +38,7 @@ class KeyMapDetectionController(
3738
private const val INPUT_EVENT_HUB_ID = "key_map_controller"
3839

3940
fun getEvdevGrabRequests(algorithm: KeyMapAlgorithm): List<GrabDeviceRequest> {
40-
val requests = mutableListOf<GrabDeviceRequest>()
41+
val deviceKeyEventMap = mutableMapOf<EvdevDeviceInfo, MutableSet<Int>>()
4142

4243
for ((index, trigger) in algorithm.triggers.withIndex()) {
4344
val evdevDevices = trigger.keys.filterIsInstance<EvdevTriggerKey>()
@@ -56,15 +57,15 @@ class KeyMapDetectionController(
5657
val extraKeyCodes = actions
5758
.filterIsInstance<ActionData.InputKeyEvent>()
5859
.map { it.keyCode }
59-
.distinct()
60-
.toIntArray()
6160

6261
for (device in evdevDevices) {
63-
requests.add(GrabDeviceRequest(device, extraKeyCodes))
62+
deviceKeyEventMap.getOrPut(device, { mutableSetOf() }).addAll(extraKeyCodes)
6463
}
6564
}
6665

67-
return requests
66+
return deviceKeyEventMap.map { (device, keyEvents) ->
67+
GrabDeviceRequest(device, keyEvents.toIntArray())
68+
}
6869
}
6970
}
7071

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
1919
import kotlinx.coroutines.channels.Channel
2020
import kotlinx.coroutines.flow.MutableStateFlow
2121
import kotlinx.coroutines.flow.collect
22-
import kotlinx.coroutines.flow.combine
2322
import kotlinx.coroutines.flow.emptyFlow
2423
import kotlinx.coroutines.flow.flatMapLatest
2524
import kotlinx.coroutines.flow.onEach
@@ -72,15 +71,11 @@ class EvdevDevicesDelegate @Inject constructor(
7271
}
7372

7473
coroutineScope.launch {
75-
combine(
76-
grabDevicesChannel.receiveAsFlow(),
77-
devicesAdapter.connectedInputDevices,
78-
) { devicesToGrab, _ -> devicesToGrab }
79-
.collect { devices ->
80-
withContext(Dispatchers.IO) {
81-
invalidateGrabbedDevices(devices)
82-
}
74+
grabDevicesChannel.receiveAsFlow().collect { devices ->
75+
withContext(Dispatchers.IO) {
76+
invalidateGrabbedDevices(devices)
8377
}
78+
}
8479
}
8580
}
8681

0 commit comments

Comments
 (0)