Skip to content

Commit

Permalink
#1274 fix errors for free build
Browse files Browse the repository at this point in the history
  • Loading branch information
sds100 committed Oct 15, 2024
1 parent 155bde5 commit 9c8582d
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import io.github.sds100.keymapper.BaseMainActivity
package io.github.sds100.keymapper

class MainActivity : BaseMainActivity()
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.github.sds100.keymapper.mappings.keymaps.KeyMap
import io.github.sds100.keymapper.mappings.keymaps.KeyMapAction
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyCodeTriggerKey
import io.github.sds100.keymapper.mappings.keymaps.trigger.Trigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKey
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKeyDevice
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerMode
import io.github.sds100.keymapper.system.devices.InputDeviceInfo
Expand Down Expand Up @@ -110,47 +111,51 @@ class KeyMapController(
val parallelTriggerModifierKeyIndices = mutableListOf<Pair<Int, Int>>()

// Only process key maps that can be triggered
val validKeyMaps = value.filter {
it.actionList.isNotEmpty() && it.isEnabled
val validKeyMaps = value.filter { keyMap ->
keyMap.actionList.isNotEmpty() &&
keyMap.isEnabled &&
keyMap.trigger.keys.all { it is KeyCodeTriggerKey }
}

for ((triggerIndex, keyMap) in validKeyMaps.withIndex()) {

// TRIGGER STUFF
keyMap.trigger.keys.forEachIndexed { keyIndex, key ->
if (keyMap.trigger.mode == TriggerMode.Sequence &&
key.clickType == ClickType.LONG_PRESS
) {

if (keyMap.trigger.keys.size > 1) {
longPressSequenceTriggerKeys.add(key)
keyMap.trigger.keys
.mapNotNull { it as? KeyCodeTriggerKey }
.forEachIndexed { keyIndex, key ->
if (keyMap.trigger.mode == TriggerMode.Sequence &&
key.clickType == ClickType.LONG_PRESS
) {

if (keyMap.trigger.keys.size > 1) {
longPressSequenceTriggerKeys.add(key)
}
}
}

if ((
keyMap.trigger.mode == TriggerMode.Sequence ||
keyMap.trigger.mode == TriggerMode.Undefined
) &&
key.clickType == ClickType.DOUBLE_PRESS
) {
doublePressKeys.add(TriggerKeyLocation(triggerIndex, keyIndex))
}

when (key.device) {
TriggerKeyDevice.Internal -> {
detectInternalEvents = true
if ((
keyMap.trigger.mode == TriggerMode.Sequence ||
keyMap.trigger.mode == TriggerMode.Undefined
) &&
key.clickType == ClickType.DOUBLE_PRESS
) {
doublePressKeys.add(TriggerKeyLocation(triggerIndex, keyIndex))
}

TriggerKeyDevice.Any -> {
detectInternalEvents = true
detectExternalEvents = true
}
when (key.device) {
TriggerKeyDevice.Internal -> {
detectInternalEvents = true
}

TriggerKeyDevice.Any -> {
detectInternalEvents = true
detectExternalEvents = true
}

is TriggerKeyDevice.External -> {
detectExternalEvents = true
is TriggerKeyDevice.External -> {
detectExternalEvents = true
}
}
}
}

val encodedActionList = encodeActionList(keyMap.actionList)

Expand Down Expand Up @@ -328,11 +333,13 @@ class KeyMapController(
for (triggerIndex in parallelTriggers) {
val trigger = triggers[triggerIndex]

trigger.keys.forEachIndexed { keyIndex, key ->
if (isModifierKey(key.keyCode)) {
parallelTriggerModifierKeyIndices.add(triggerIndex to keyIndex)
trigger.keys
.mapNotNull { it as? KeyCodeTriggerKey }
.forEachIndexed { keyIndex, key ->
if (isModifierKey(key.keyCode)) {
parallelTriggerModifierKeyIndices.add(triggerIndex to keyIndex)
}
}
}
}

reset()
Expand Down Expand Up @@ -571,14 +578,18 @@ class KeyMapController(
metaStateFromKeyEvent = metaState

// remove the metastate from any modifier keys that remapped and are pressed down
parallelTriggerModifierKeyIndices.forEach {
for (it in parallelTriggerModifierKeyIndices) {
val triggerIndex = it.first
val eventIndex = it.second
val event = triggers[triggerIndex].keys[eventIndex]
val key = triggers[triggerIndex].keys[eventIndex]

if (key !is KeyCodeTriggerKey) {
continue
}

if (parallelTriggerEventsAwaitingRelease[triggerIndex][eventIndex]) {
metaStateFromKeyEvent =
metaStateFromKeyEvent.minusFlag(KeyEventUtils.modifierKeycodeToMetaState(event.keyCode))
metaStateFromKeyEvent.minusFlag(KeyEventUtils.modifierKeycodeToMetaState(key.keyCode))
}
}

Expand Down Expand Up @@ -644,6 +655,10 @@ class KeyMapController(

// consume the event if the trigger contains this keycode.
for ((keyIndex, key) in triggerKeys.withIndex()) {
if (key !is KeyCodeTriggerKey) {
continue
}

if (key.keyCode == event.keyCode && triggerKeys[keyIndex].consumeKeyEvent) {
consumeEvent = true
}
Expand Down Expand Up @@ -1478,22 +1493,32 @@ class KeyMapController(
return key.matchesEvent(event)
}

private fun KeyCodeTriggerKey.matchesEvent(event: Event): Boolean = when (this.device) {
TriggerKeyDevice.Any -> this.keyCode == event.keyCode && this.clickType == event.clickType
is TriggerKeyDevice.External ->
this.keyCode == event.keyCode &&
event.descriptor != null &&
event.descriptor == this.device.descriptor &&
this.clickType == event.clickType

TriggerKeyDevice.Internal ->
this.keyCode == event.keyCode &&
event.descriptor == null &&
this.clickType == event.clickType
private fun TriggerKey.matchesEvent(event: Event): Boolean {
if (this !is KeyCodeTriggerKey) {
return false
}

return when (this.device) {
TriggerKeyDevice.Any -> this.keyCode == event.keyCode && this.clickType == event.clickType
is TriggerKeyDevice.External ->
this.keyCode == event.keyCode &&
event.descriptor != null &&
event.descriptor == this.device.descriptor &&
this.clickType == event.clickType

TriggerKeyDevice.Internal ->
this.keyCode == event.keyCode &&
event.descriptor == null &&
this.clickType == event.clickType
}
}

private fun KeyCodeTriggerKey.matchesWithOtherKey(otherKey: KeyCodeTriggerKey): Boolean =
when (this.device) {
private fun TriggerKey.matchesWithOtherKey(otherKey: TriggerKey): Boolean {
if (!(this is KeyCodeTriggerKey && otherKey is KeyCodeTriggerKey)) {
return false
}

return when (this.device) {
TriggerKeyDevice.Any ->
this.keyCode == otherKey.keyCode &&
this.clickType == otherKey.clickType
Expand All @@ -1508,6 +1533,7 @@ class KeyMapController(
otherKey.device == TriggerKeyDevice.Internal &&
this.clickType == otherKey.clickType
}
}

private fun longPressDelay(trigger: Trigger): Long =
trigger.longPressDelay?.toLong() ?: defaultLongPressDelay.value
Expand Down Expand Up @@ -1571,4 +1597,11 @@ class KeyMapController(
)

private data class TriggerKeyLocation(val triggerIndex: Int, val keyIndex: Int)

private val TriggerKey.consumeKeyEvent: Boolean
get() = if (this is KeyCodeTriggerKey) {
this.consumeEvent
} else {
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import kotlinx.coroutines.launch
@Composable
fun AdvancedTriggersBottomSheet(
modifier: Modifier = Modifier,
viewModel: ConfigTriggerViewModel,
onDismissRequest: () -> Unit,
sheetState: SheetState,
) {
Expand Down

0 comments on commit 9c8582d

Please sign in to comment.