Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional wait variance #967

Closed
wants to merge 10 commits into from
Closed
16 changes: 13 additions & 3 deletions inputremapper/injection/macros/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import copy
import math
import re
import random
from typing import List, Callable, Awaitable, Tuple, Optional, Union, Any

from evdev.ecodes import (
Expand Down Expand Up @@ -540,12 +541,21 @@ async def task(handler: Callable):

self.tasks.append(task)

def add_wait(self, time: Union[int, float]):
def add_wait(self, time: Union[str, int], max_time=None):
"""Wait time in milliseconds."""
time = _type_check(time, [int, float], "wait", 1)
time = _type_check(time, [int], "wait", 1)
Copy link
Owner

@sezanzeb sezanzeb Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for removing float as supported datatype? If this feature is released and someone used wait(10.5), then I believe their macro will suddenly crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to make the wait macro accept a variable as input as it either didn't or I'd broken it somehow.

max_time = _type_check(max_time, [int,None], "wait", 2)

async def task(_):
await asyncio.sleep(_resolve(time, [int, float]) / 1000)
resolved_min_time = _resolve(time, [int])
resolved_max_time = _resolve(max_time, [int])

if resolved_max_time is not None and resolved_max_time > resolved_min_time:
variabletime = random.randint(resolved_min_time, resolved_max_time)
else:
variabletime = resolved_min_time

await asyncio.sleep(variabletime / 1000)

self.tasks.append(task)

Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,39 @@ async def test_6(self):
self.assertIsInstance(macro, Macro)
self.assertListEqual(self.result, [])

async def test_wait_1(self):
macro = parse("wait(10).key(1)", self.context, DummyMapping, True)
one_code = system_mapping.get("1")

await macro.run(self.handler)
self.assertListEqual(
self.result,
[(EV_KEY, one_code, 1), (EV_KEY, one_code, 0)],
)
self.assertEqual(len(macro.child_macros), 0)

async def test_wait_2(self):
macro = parse("wait(10,100).key(1)", self.context, DummyMapping, True)
one_code = system_mapping.get("1")

await macro.run(self.handler)
self.assertListEqual(
self.result,
[(EV_KEY, one_code, 1), (EV_KEY, one_code, 0)],
)
self.assertEqual(len(macro.child_macros), 0)

async def test_wait_3(self):
macro = parse("set(a,100).wait(10, $a).key(1)", self.context, DummyMapping, True)
one_code = system_mapping.get("1")

await macro.run(self.handler)
self.assertListEqual(
self.result,
[(EV_KEY, one_code, 1), (EV_KEY, one_code, 0)],
)
self.assertEqual(len(macro.child_macros), 0)

async def test_duplicate_run(self):
# it won't restart the macro, because that may screw up the
# internal state (in particular the _trigger_release_event).
Expand Down