Skip to content

Commit

Permalink
Fix event macro
Browse files Browse the repository at this point in the history
  • Loading branch information
sezanzeb committed Dec 16, 2024
1 parent 001a9af commit d902622
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 23 deletions.
1 change: 0 additions & 1 deletion inputremapper/injection/macros/tasks/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ async def run(self, callback) -> None:
if isinstance(type_, str):
type_ = ecodes[type_.upper()]
if isinstance(code, str):
self.get_argument("code").assert_is_symbol(code)
code = ecodes[code.upper()]

callback(type_, code, value)
Expand Down
7 changes: 5 additions & 2 deletions readme/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Bear in mind that anti-cheat software might detect macros in games.
> Moves the mouse cursor
>
> If `acceleration` is provided then the cursor will accelerate from zero to a maximum speed of `speed`.
> If `acceleration` is provided then the cursor will accelerate from zero to a maximum speed of `speed`.
>
> ```ts
> mouse(direction: str, speed: int, acceleration: float | None)
Expand Down Expand Up @@ -183,7 +183,9 @@ Bear in mind that anti-cheat software might detect macros in games.
### event
> Writes an event. Examples for `type`, `code` and `value` can be found via the
> `sudo evtest` command
> `sudo evtest` command. Also check out [input-event-codes.h](https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h).
> `EV_KEY` for keys, `EV_REL` for mouse movements, `EV_ABS` for gamepad events among
> others.
>
> ```ts
> event(type: str | int, code: str | int, value: int)
Expand All @@ -193,6 +195,7 @@ Bear in mind that anti-cheat software might detect macros in games.
>
> ```ts
> event(EV_KEY, KEY_A, 1)
> event(EV_REL, REL_X, -10)
> event(2, 8, 1)
> ```
Expand Down
67 changes: 67 additions & 0 deletions tests/unit/test_macros/test_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# input-remapper - GUI for device specific keyboard mappings
# Copyright (C) 2024 sezanzeb <b8x45ygc9@mozmail.com>
#
# This file is part of input-remapper.
#
# input-remapper is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# input-remapper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.


import unittest

from evdev.ecodes import (
EV_REL,
EV_KEY,
REL_X,
)

from inputremapper.configs.keyboard_layout import keyboard_layout
from inputremapper.injection.macros.parse import Parser
from tests.lib.test_setup import test_setup
from tests.unit.test_macros.macro_test_base import MacroTestBase, DummyMapping


@test_setup
class TestEvent(MacroTestBase):
async def test_event_1(self):
macro = Parser.parse("e(EV_KEY, KEY_A, 1)", self.context, DummyMapping)
a_code = keyboard_layout.get("a")

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, a_code, 1)])
self.assertEqual(self.count_child_macros(macro), 0)

async def test_event_2(self):
macro = Parser.parse(
"repeat(1, event(type=5421, code=324, value=154))",
self.context,
DummyMapping,
)
code = 324

await macro.run(self.handler)
self.assertListEqual(self.result, [(5421, code, 154)])
self.assertEqual(self.count_child_macros(macro), 1)

async def test_event_mouse(self):
macro = Parser.parse("e(EV_REL, REL_X, 10)", self.context, DummyMapping)

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_REL, REL_X, 10)])
self.assertEqual(self.count_child_macros(macro), 0)


if __name__ == "__main__":
unittest.main()
20 changes: 0 additions & 20 deletions tests/unit/test_macros/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,26 +632,6 @@ async def test_mouse_accel(self):
self.result,
)

async def test_event_1(self):
macro = Parser.parse("e(EV_KEY, KEY_A, 1)", self.context, DummyMapping)
a_code = keyboard_layout.get("a")

await macro.run(self.handler)
self.assertListEqual(self.result, [(EV_KEY, a_code, 1)])
self.assertEqual(self.count_child_macros(macro), 0)

async def test_event_2(self):
macro = Parser.parse(
"repeat(1, event(type=5421, code=324, value=154))",
self.context,
DummyMapping,
)
code = 324

await macro.run(self.handler)
self.assertListEqual(self.result, [(5421, code, 154)])
self.assertEqual(self.count_child_macros(macro), 1)

async def test_macro_breaks(self):
# the first parameter for `repeat` requires an integer, not "foo",
# which makes `repeat` throw
Expand Down

0 comments on commit d902622

Please sign in to comment.