Skip to content

Commit 5d913e4

Browse files
committed
fix: do not grab evdev devices with extra key codes if key event actions do not use system bridge
1 parent 9dd7cda commit 5d913e4

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ import io.github.sds100.keymapper.base.trigger.RecordTriggerController
1414
import io.github.sds100.keymapper.base.trigger.RecordTriggerState
1515
import io.github.sds100.keymapper.common.models.EvdevDeviceInfo
1616
import io.github.sds100.keymapper.common.models.GrabTargetKeyCode
17+
import io.github.sds100.keymapper.data.Keys
18+
import io.github.sds100.keymapper.data.PreferenceDefaults
19+
import io.github.sds100.keymapper.data.repositories.PreferenceRepository
1720
import io.github.sds100.keymapper.system.inputevents.KMEvdevEvent
1821
import io.github.sds100.keymapper.system.inputevents.KMInputEvent
1922
import kotlinx.coroutines.CoroutineScope
2023
import kotlinx.coroutines.flow.SharingStarted
2124
import kotlinx.coroutines.flow.StateFlow
2225
import kotlinx.coroutines.flow.combine
2326
import kotlinx.coroutines.flow.launchIn
27+
import kotlinx.coroutines.flow.map
2428
import kotlinx.coroutines.flow.stateIn
2529
import kotlinx.coroutines.launch
2630
import timber.log.Timber
@@ -33,11 +37,15 @@ class KeyMapDetectionController(
3337
private val inputEventHub: InputEventHub,
3438
private val pauseKeyMapsUseCase: PauseKeyMapsUseCase,
3539
private val recordTriggerController: RecordTriggerController,
40+
private val preferences: PreferenceRepository,
3641
) : InputEventHubCallback {
3742
companion object {
3843
private const val INPUT_EVENT_HUB_ID = "key_map_controller"
3944

40-
fun getEvdevGrabRequests(algorithm: KeyMapAlgorithm): List<GrabTargetKeyCode> {
45+
fun getEvdevGrabRequests(
46+
algorithm: KeyMapAlgorithm,
47+
injectKeyEventActionsWithSystemBridge: Boolean = true,
48+
): List<GrabTargetKeyCode> {
4149
val deviceKeyEventMap = mutableMapOf<EvdevDeviceInfo, MutableSet<Int>>()
4250

4351
for ((index, trigger) in algorithm.triggers.withIndex()) {
@@ -54,12 +62,16 @@ class KeyMapDetectionController(
5462
.map { actionIndex -> algorithm.actionMap[actionIndex]?.data }
5563
.filterNotNull()
5664

57-
val extraKeyCodes = actions
58-
.filterIsInstance<ActionData.InputKeyEvent>()
59-
.map { it.keyCode }
65+
val extraKeyCodes = if (injectKeyEventActionsWithSystemBridge) {
66+
actions
67+
.filterIsInstance<ActionData.InputKeyEvent>()
68+
.map { it.keyCode }
69+
} else {
70+
emptyList()
71+
}
6072

6173
for (device in evdevDevices) {
62-
deviceKeyEventMap.getOrPut(device, { mutableSetOf() }).addAll(extraKeyCodes)
74+
deviceKeyEventMap.getOrPut(device) { mutableSetOf() }.addAll(extraKeyCodes)
6375
}
6476
}
6577

@@ -75,6 +87,11 @@ class KeyMapDetectionController(
7587
}
7688
}
7789

90+
private val injectKeyEventsWithSystemBridge: StateFlow<Boolean> =
91+
preferences.get(Keys.keyEventActionsUseSystemBridge)
92+
.map { it ?: PreferenceDefaults.KEY_EVENT_ACTIONS_USE_SYSTEM_BRIDGE }
93+
.stateIn(coroutineScope, SharingStarted.Eagerly, false)
94+
7895
private val algorithm: KeyMapAlgorithm =
7996
KeyMapAlgorithm(coroutineScope, detectUseCase, performActionsUseCase, detectConstraints)
8097

@@ -103,7 +120,8 @@ class KeyMapDetectionController(
103120
algorithm.loadKeyMaps(keyMapList)
104121
// Determine which evdev devices need to be grabbed depending on the state
105122
// of the algorithm.
106-
val grabRequests = getEvdevGrabRequests(algorithm)
123+
val grabRequests =
124+
getEvdevGrabRequests(algorithm, injectKeyEventsWithSystemBridge.value)
107125

108126
Timber.i(
109127
"Grab evdev devices for key map detection: ${grabRequests.joinToString()}",

base/src/main/java/io/github/sds100/keymapper/base/system/accessibility/BaseAccessibilityServiceController.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ abstract class BaseAccessibilityServiceController(
9494
inputEventHub,
9595
pauseKeyMapsUseCase,
9696
recordTriggerController,
97+
settingsRepository,
9798
)
9899

99100
val triggerKeyMapFromOtherAppsController = TriggerKeyMapFromOtherAppsController(

base/src/test/java/io/github/sds100/keymapper/base/detection/KeyMapDetectionControllerTest.kt

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,58 @@ class KeyMapDetectionControllerTest {
169169
)
170170
}
171171

172+
@Test
173+
fun `Do not grab evdev devices with extra key codes if key event actions do not use system bridge`() {
174+
loadKeyMaps(
175+
KeyMap(
176+
trigger = singleKeyTrigger(
177+
EvdevTriggerKey(
178+
scanCode = Scancode.BTN_A,
179+
keyCode = KeyEvent.KEYCODE_BUTTON_A,
180+
clickType = ClickType.SHORT_PRESS,
181+
device = FAKE_CONTROLLER_EVDEV_DEVICE,
182+
),
183+
),
184+
actionList = listOf(
185+
Action(data = ActionData.OpenCamera),
186+
buildKeyEventAction(KeyEvent.KEYCODE_BUTTON_X),
187+
),
188+
),
189+
KeyMap(
190+
trigger = singleKeyTrigger(
191+
EvdevTriggerKey(
192+
scanCode = Scancode.BTN_B,
193+
keyCode = KeyEvent.KEYCODE_BUTTON_B,
194+
clickType = ClickType.SHORT_PRESS,
195+
device = FAKE_CONTROLLER_EVDEV_DEVICE_2,
196+
),
197+
),
198+
actionList = listOf(
199+
buildKeyEventAction(KeyEvent.KEYCODE_BUTTON_Y),
200+
),
201+
),
202+
)
203+
204+
val grabRequests = KeyMapDetectionController.getEvdevGrabRequests(
205+
algorithm,
206+
injectKeyEventActionsWithSystemBridge = false,
207+
)
208+
209+
assertThat(
210+
grabRequests,
211+
contains(
212+
GrabTargetKeyCode(
213+
device = FAKE_CONTROLLER_EVDEV_DEVICE,
214+
extraKeyCodes = intArrayOf(),
215+
),
216+
GrabTargetKeyCode(
217+
device = FAKE_CONTROLLER_EVDEV_DEVICE_2,
218+
extraKeyCodes = intArrayOf(),
219+
),
220+
),
221+
)
222+
}
223+
172224
@Test
173225
fun `Grab multiple evdev devices from multiple triggers`() {
174226
loadKeyMaps(

0 commit comments

Comments
 (0)