Skip to content

Commit

Permalink
simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Oct 17, 2024
1 parent d8068b3 commit 99f2140
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
34 changes: 16 additions & 18 deletions inputremapper/injection/mapping_handlers/combination_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,17 @@ def notify(
# will forward the release.
return not self.should_release_event(event)

if is_activated:
event = event.modify(value=1)
else:
# If not activated. All required keys are not yet pressed.
if self._output_active or self.mapping.is_axis_mapping():
# we ignore the `suppress` argument for release events
# otherwise we might end up with stuck keys
# (test_event_pipeline.test_combination)

# we also ignore it if the mapping specifies an output axis
# this will enable us to activate multiple axis with the same button
suppress = False
event = event.modify(value=0)
# State changed
# This depends on wether the key was pressed or released, therefore those are
# equal
assert is_activated == is_pressed

if not is_activated:
# We ignore the `suppress` argument for release events. Otherwise, we
# might end up with stuck keys (test_event_pipeline.test_combination).
# In the case of output axis, this will enable us to activate multiple
# axis with the same button.
suppress = False

if not suppress:
if is_activated:
Expand All @@ -148,15 +146,15 @@ def notify(
self._output_active = bool(event.value)
sub_handler_result = self._sub_handler.notify(event, source, suppress)

if is_pressed:
if is_activated:
# Only if the sub-handler return False, we need a release-event later.
# If it handled the event, the user never sees this key-down 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)

# 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

Expand Down
51 changes: 51 additions & 0 deletions tests/unit/test_event_pipeline/test_event_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from tests.lib.logger import logger
from tests.lib.constants import MAX_ABS, MIN_ABS
from tests.lib.fixtures import Fixture, fixtures
from tests.lib.pipes import uinput_write_history
from tests.lib.test_setup import test_setup


Expand Down Expand Up @@ -540,6 +541,56 @@ 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_releases_before_triggering(self):
origin = fixtures.foo_device_2_keyboard
origin_hash = origin.get_device_hash()

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

mapping = Mapping(
input_combination=input_combination.to_config(),
target_uinput="keyboard",
output_symbol="1",
release_combination_keys=True,
)

preset = Preset()
preset.add(mapping)

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),
],
event_reader,
)

# Other tests check forwarded_history and keyboard_history individually,
# so here is one that looks at the order in uinput_write_history
self.assertListEqual(
uinput_write_history,
[
(EV_KEY, KEY_A, 1),
(EV_KEY, KEY_A, 0),
(EV_KEY, KEY_1, 1),
],
)

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

0 comments on commit 99f2140

Please sign in to comment.