Skip to content

Commit

Permalink
fix transition 5
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Oct 16, 2024
1 parent 59ec20b commit fd0e005
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 16 deletions.
34 changes: 18 additions & 16 deletions inputremapper/injection/mapping_handlers/combination_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ def notify(
return not self.should_release_event(event)

if is_activated:
# Send key up events to the forwarded uinput if configured to do so.
self.forward_release()
event = event.modify(value=1)
else:
# If not activated. All required keys are not yet pressed.
Expand All @@ -140,22 +138,26 @@ def notify(
suppress = False
event = event.modify(value=0)

if suppress:
return False
if not suppress:
if is_activated:
# Send key up events to the forwarded uinput if configured to do so.
self.forward_release()

logger.debug("Sending %s to sub-handler", self.mapping.input_combination)
self._output_active = bool(event.value)
sub_handler_result = self._sub_handler.notify(event, source, suppress)
logger.debug("Sending %s to sub-handler", self.mapping.input_combination)
self._output_active = bool(event.value)
sub_handler_result = self._sub_handler.notify(event, source, suppress)

if is_pressed:
# If the sub-handler return True, it handled the event, so the user never
# sees this key-down event. In that case, we don't require a release event.
self.require_release_later(not sub_handler_result, event)
return sub_handler_result
else:
# Else if it is released: Returning `False` means that the event-reader
# will forward the release.
return not self.should_release_event(event)
if is_pressed:
# If the sub-handler return True, it handled the event, so the user never
# sees this key-down event. In that case, we don't require a release event.
self.require_release_later(not sub_handler_result, event)
return sub_handler_result
else:
# Else if it is released: Returning `False` means that the event-reader
# will forward the release.
return not self.should_release_event(event)

return False

def should_release_event(self, event: InputEvent) -> bool:
"""Check if the key-up event should be forwarded by the event-reader.
Expand Down
86 changes: 86 additions & 0 deletions tests/unit/test_event_pipeline/test_event_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
KEY_B,
KEY_C,
BTN_TL,
KEY_1,
)

from inputremapper.configs.mapping import (
Expand Down Expand Up @@ -538,6 +539,91 @@ async def test_combination_manual_release_in_press_order(self):
self.assertListEqual(forwarded_history, [(EV_KEY, in_1, 1), (EV_KEY, in_1, 0)])
self.assertListEqual(keyboard_history, [(EV_KEY, out, 1), (EV_KEY, out, 0)])

async def test_suppressed_doesnt_forward_releases(self):
origin = fixtures.foo_device_2_keyboard
origin_hash = origin.get_device_hash()

input_combination_1 = InputCombination(
[
InputConfig(
type=EV_KEY,
code=KEY_A,
origin_hash=origin_hash,
),
InputConfig(
type=EV_KEY,
code=KEY_B,
origin_hash=origin_hash,
),
InputConfig(
type=EV_KEY,
code=KEY_C,
origin_hash=origin_hash,
),
]
)

mapping_1 = Mapping(
input_combination=input_combination_1.to_config(),
target_uinput="keyboard",
output_symbol="1",
release_combination_keys=False,
)

input_combination_2 = InputCombination(
[
InputConfig(
type=EV_KEY,
code=KEY_A,
origin_hash=origin_hash,
),
InputConfig(
type=EV_KEY,
code=KEY_C,
origin_hash=origin_hash,
),
]
)

mapping_2 = Mapping(
input_combination=input_combination_2.to_config(),
target_uinput="keyboard",
output_symbol="2",
release_combination_keys=True,
)

preset = Preset()
preset.add(mapping_1)
preset.add(mapping_2)

event_reader = self.create_event_reader(preset, origin)
await self.send_events(
[
InputEvent.key(KEY_A, 1, origin_hash),
InputEvent.key(KEY_B, 1, origin_hash),
InputEvent.key(KEY_C, 1, origin_hash),
],
event_reader,
)

keyboard_history = global_uinputs.get_uinput("keyboard").write_history
forwarded_history = self.forward_uinput.write_history
# There used to be an incorrect KEY_A release, caused by
# `release_combination_keys` on the suppressed mapping.
self.assertListEqual(
forwarded_history,
[
(EV_KEY, KEY_A, 1),
(EV_KEY, KEY_B, 1),
],
)
self.assertListEqual(
keyboard_history,
[
(EV_KEY, KEY_1, 1),
],
)

async def test_ignore_hold(self):
# hold as in event-value 2, not in macro-hold.
# linux will generate events with value 2 after input-remapper injected
Expand Down

0 comments on commit fd0e005

Please sign in to comment.