2020-11-27 20:27:15 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
# -*- coding: utf-8 -*-
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper - GUI for device specific keyboard mappings
|
2022-01-01 12:52:33 +00:00
|
|
|
# Copyright (C) 2022 sezanzeb <proxima@sezanzeb.de>
|
2020-11-27 20:27:15 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# This file is part of input-remapper.
|
2020-11-27 20:27:15 +00:00
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is free software: you can redistribute it and/or modify
|
2020-11-27 20:27:15 +00:00
|
|
|
# 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.
|
|
|
|
#
|
2022-01-01 12:00:49 +00:00
|
|
|
# input-remapper is distributed in the hope that it will be useful,
|
2020-11-27 20:27:15 +00:00
|
|
|
# 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
|
2022-01-01 12:00:49 +00:00
|
|
|
# along with input-remapper. If not, see <https://www.gnu.org/licenses/>.
|
2020-11-27 20:27:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
"""Executes more complex patterns of keystrokes.
|
|
|
|
|
2021-04-25 17:28:02 +00:00
|
|
|
To keep it short on the UI, basic functions are one letter long.
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2020-11-28 19:59:24 +00:00
|
|
|
The outermost macro (in the examples below the one created by 'r',
|
2020-11-27 20:27:15 +00:00
|
|
|
'r' and 'w') will be started, which triggers a chain reaction to execute
|
|
|
|
all of the configured stuff.
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
2020-11-28 19:59:24 +00:00
|
|
|
r(3, k(a).w(10)): a <10ms> a <10ms> a
|
2021-10-05 23:12:21 +00:00
|
|
|
r(2, k(a).k(KEY_A)).k(b): a - a - b
|
2020-12-01 23:58:48 +00:00
|
|
|
w(1000).m(Shift_L, r(2, k(a))).w(10).k(b): <1s> A A <10ms> b
|
2020-11-27 20:27:15 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2020-11-28 19:59:24 +00:00
|
|
|
import asyncio
|
2021-03-19 22:03:03 +00:00
|
|
|
import copy
|
2022-04-17 10:19:23 +00:00
|
|
|
import math
|
2021-10-02 18:16:07 +00:00
|
|
|
import re
|
2022-04-17 10:19:23 +00:00
|
|
|
from typing import Optional, List, Callable, Awaitable, Tuple
|
|
|
|
|
|
|
|
import evdev
|
|
|
|
from evdev.ecodes import (
|
|
|
|
ecodes,
|
|
|
|
EV_KEY,
|
|
|
|
EV_REL,
|
|
|
|
REL_X,
|
|
|
|
REL_Y,
|
|
|
|
REL_WHEEL_HI_RES,
|
|
|
|
REL_HWHEEL_HI_RES,
|
|
|
|
REL_WHEEL,
|
|
|
|
REL_HWHEEL,
|
|
|
|
)
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.logger import logger
|
2022-01-31 19:58:37 +00:00
|
|
|
from inputremapper.configs.system_mapping import system_mapping
|
2022-01-01 12:00:49 +00:00
|
|
|
from inputremapper.ipc.shared_dict import SharedDict
|
2022-04-17 10:19:23 +00:00
|
|
|
from inputremapper.exceptions import MacroParsingError
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
Handler = Callable[[Tuple[int, int, int]], None]
|
|
|
|
MacroTask = Callable[[Handler], Awaitable]
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2021-04-25 17:28:02 +00:00
|
|
|
macro_variables = SharedDict()
|
|
|
|
|
|
|
|
|
2021-10-05 23:12:21 +00:00
|
|
|
class Variable:
|
|
|
|
"""Can be used as function parameter in the various add_... functions.
|
|
|
|
|
|
|
|
Parsed from strings like `$foo` in `repeat($foo, k(KEY_A))`
|
|
|
|
|
|
|
|
Its value is unknown during construction and needs to be set using the `set` macro
|
|
|
|
during runtime.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def resolve(self):
|
|
|
|
"""Get the variables value from memory."""
|
|
|
|
return macro_variables.get(self.name)
|
|
|
|
|
2021-10-06 22:09:57 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return f'<Variable "{self.name}">'
|
|
|
|
|
2021-10-05 23:12:21 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def _type_check(value, allowed_types, display_name=None, position=None):
|
|
|
|
"""Validate a parameter used in a macro.
|
|
|
|
|
2021-10-05 23:12:21 +00:00
|
|
|
If the value is a Variable, it will be returned and should be resolved
|
2021-10-01 22:55:10 +00:00
|
|
|
during runtime with _resolve.
|
|
|
|
"""
|
2021-10-05 23:12:21 +00:00
|
|
|
if isinstance(value, Variable):
|
2021-10-01 22:55:10 +00:00
|
|
|
# it is a variable and will be read at runtime
|
|
|
|
return value
|
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
for allowed_type in allowed_types:
|
|
|
|
if allowed_type is None:
|
|
|
|
if value is None:
|
|
|
|
return value
|
2021-10-01 22:55:10 +00:00
|
|
|
|
|
|
|
continue
|
2021-09-29 21:00:50 +00:00
|
|
|
|
|
|
|
# try to parse "1" as 1 if possible
|
2022-04-17 10:19:23 +00:00
|
|
|
if allowed_type != Macro:
|
|
|
|
# the macro constructor with a single argument always succeeds,
|
|
|
|
# but will definitely not result in the correct macro
|
|
|
|
try:
|
|
|
|
return allowed_type(value)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
pass
|
2020-12-05 12:08:07 +00:00
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
if isinstance(value, allowed_type):
|
|
|
|
return value
|
2021-02-12 20:43:40 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
if display_name is not None and position is not None:
|
2022-04-17 10:19:23 +00:00
|
|
|
raise MacroParsingError(
|
|
|
|
msg=f"Expected parameter {position} for {display_name} to be "
|
2021-10-01 22:55:10 +00:00
|
|
|
f"one of {allowed_types}, but got {value}"
|
|
|
|
)
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
raise MacroParsingError(
|
|
|
|
msg=f"Expected parameter to be one of {allowed_types}, but got {value}"
|
|
|
|
)
|
2021-10-01 22:55:10 +00:00
|
|
|
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
def _type_check_symbol(keyname):
|
2021-10-01 22:55:10 +00:00
|
|
|
"""Same as _type_check, but checks if the key-name is valid."""
|
2021-10-05 23:12:21 +00:00
|
|
|
if isinstance(keyname, Variable):
|
2021-10-01 22:55:10 +00:00
|
|
|
# it is a variable and will be read at runtime
|
2021-10-05 23:12:21 +00:00
|
|
|
return keyname
|
2021-10-01 22:55:10 +00:00
|
|
|
|
2021-10-05 23:12:21 +00:00
|
|
|
symbol = str(keyname)
|
2021-10-01 22:55:10 +00:00
|
|
|
code = system_mapping.get(symbol)
|
|
|
|
|
|
|
|
if code is None:
|
2022-04-17 10:19:23 +00:00
|
|
|
raise MacroParsingError(msg=f'Unknown key "{symbol}"')
|
2021-10-01 22:55:10 +00:00
|
|
|
|
|
|
|
return code
|
|
|
|
|
|
|
|
|
2021-10-02 18:16:07 +00:00
|
|
|
def _type_check_variablename(name):
|
|
|
|
"""Check if this is a legit variable name.
|
|
|
|
|
|
|
|
Because they could clash with language features. If the macro is able to be
|
|
|
|
parsed at all due to a problematic choice of a variable name.
|
|
|
|
|
|
|
|
Allowed examples: "foo", "Foo1234_", "_foo_1234"
|
|
|
|
Not allowed: "1_foo", "foo=blub", "$foo", "foo,1234", "foo()"
|
|
|
|
"""
|
|
|
|
if not isinstance(name, str) or not re.match(r"^[A-Za-z_][A-Za-z_0-9]*$", name):
|
2022-04-17 10:19:23 +00:00
|
|
|
raise MacroParsingError(msg=f'"{name}" is not a legit variable name')
|
2021-10-02 18:16:07 +00:00
|
|
|
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def _resolve(argument, allowed_types=None):
|
2021-10-05 23:12:21 +00:00
|
|
|
"""If the argument is a variable, figure out its value and cast it.
|
2021-10-01 22:55:10 +00:00
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
Variables are prefixed with `$` in the syntax.
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
Use this just-in-time when you need the actual value of the variable
|
|
|
|
during runtime.
|
|
|
|
"""
|
2021-10-05 23:12:21 +00:00
|
|
|
if isinstance(argument, Variable):
|
|
|
|
value = argument.resolve()
|
2021-10-01 22:55:10 +00:00
|
|
|
logger.debug('"%s" is "%s"', argument, value)
|
|
|
|
if allowed_types:
|
|
|
|
return _type_check(value, allowed_types)
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
|
|
|
|
return argument
|
2020-12-04 13:38:41 +00:00
|
|
|
|
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
class Macro:
|
2020-12-05 10:13:10 +00:00
|
|
|
"""Supports chaining and preparing actions.
|
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
Calling functions like keycode on Macro doesn't inject any events yet,
|
2021-04-25 17:28:02 +00:00
|
|
|
it means that once .run is used it will be executed along with all other
|
|
|
|
queued tasks.
|
|
|
|
|
|
|
|
Those functions need to construct an asyncio coroutine and append it to
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks. This makes parameter checking during compile time possible, as long
|
|
|
|
as they are not variables that are resolved durig runtime. Coroutines receive a
|
|
|
|
handler as argument, which is a function that can be used to inject input events
|
|
|
|
into the system.
|
|
|
|
|
|
|
|
1. A few parameters of any time are thrown into a macro function like `repeat`
|
|
|
|
2. `Macro.repeat` will verify the parameter types if possible using `_type_check`
|
|
|
|
(it can't for $variables). This helps debugging macros before the injection
|
|
|
|
starts, but is not mandatory to make things work.
|
|
|
|
3. `Macro.repeat`
|
|
|
|
- adds a task to self.tasks. This task resolves any variables with `_resolve`
|
|
|
|
and does what the macro is supposed to do once `macro.run` is called.
|
|
|
|
- also adds the child macro to self.child_macros.
|
|
|
|
- adds the used keys to the capabilities
|
|
|
|
4. `Macro.run` will run all tasks in self.tasks
|
2020-12-05 10:13:10 +00:00
|
|
|
"""
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def __init__(self, code: str, context=None, mapping=None):
|
2020-11-28 17:27:28 +00:00
|
|
|
"""Create a macro instance that can be populated with tasks.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-03-19 22:03:03 +00:00
|
|
|
code : string or None
|
2020-12-02 15:17:52 +00:00
|
|
|
The original parsed code, for logging purposes.
|
2022-01-14 17:50:57 +00:00
|
|
|
context : Context, or None for use in frontend
|
2020-11-28 17:27:28 +00:00
|
|
|
"""
|
2020-12-02 15:17:52 +00:00
|
|
|
self.code = code
|
2021-09-29 18:17:45 +00:00
|
|
|
self.context = context
|
2022-04-17 10:19:23 +00:00
|
|
|
self.mapping = mapping
|
2020-12-05 10:13:10 +00:00
|
|
|
|
2021-04-25 17:28:02 +00:00
|
|
|
# List of coroutines that will be called sequentially.
|
|
|
|
# This is the compiled code
|
2022-04-17 10:19:23 +00:00
|
|
|
self.tasks: List[MacroTask] = []
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-09-29 18:17:45 +00:00
|
|
|
# can be used to wait for the release of the event
|
2021-10-16 09:38:34 +00:00
|
|
|
self._trigger_release_event = asyncio.Event()
|
|
|
|
self._trigger_press_event = asyncio.Event()
|
|
|
|
# released by default
|
|
|
|
self._trigger_release_event.set()
|
|
|
|
self._trigger_press_event.clear()
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2020-12-06 13:23:00 +00:00
|
|
|
self.running = False
|
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.child_macros: List[Macro] = []
|
2021-04-25 17:28:02 +00:00
|
|
|
self.keystroke_sleep_ms = None
|
2020-12-04 13:38:41 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def is_holding(self):
|
|
|
|
"""Check if the macro is waiting for a key to be released."""
|
|
|
|
return not self._trigger_release_event.is_set()
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
def get_capabilities(self):
|
|
|
|
"""Get the merged capabilities of the macro and its children."""
|
|
|
|
capabilities = copy.deepcopy(self.capabilities)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
for macro in self.child_macros:
|
|
|
|
macro_capabilities = macro.get_capabilities()
|
|
|
|
for ev_type in macro_capabilities:
|
|
|
|
if ev_type not in capabilities:
|
|
|
|
capabilities[ev_type] = set()
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
capabilities[ev_type].update(macro_capabilities[ev_type])
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
return capabilities
|
2021-01-24 17:36:31 +00:00
|
|
|
|
2021-02-13 19:19:31 +00:00
|
|
|
async def run(self, handler):
|
|
|
|
"""Run the macro.
|
2020-12-04 13:38:41 +00:00
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-02-13 19:19:31 +00:00
|
|
|
handler : function
|
2021-03-19 22:03:03 +00:00
|
|
|
Will receive int type, code and value for an event to write
|
2020-12-04 13:38:41 +00:00
|
|
|
"""
|
2022-01-14 17:50:57 +00:00
|
|
|
if not callable(handler):
|
|
|
|
raise ValueError("handler is not callable")
|
|
|
|
|
2021-02-20 13:15:40 +00:00
|
|
|
if self.running:
|
|
|
|
logger.error('Tried to run already running macro "%s"', self.code)
|
|
|
|
return
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.keystroke_sleep_ms = self.mapping.macro_key_sleep_ms
|
2021-02-20 13:15:40 +00:00
|
|
|
|
2020-12-06 13:23:00 +00:00
|
|
|
self.running = True
|
2021-03-19 22:03:03 +00:00
|
|
|
for task in self.tasks:
|
2021-10-01 22:55:10 +00:00
|
|
|
try:
|
|
|
|
coroutine = task(handler)
|
|
|
|
if asyncio.iscoroutine(coroutine):
|
|
|
|
await coroutine
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f'Macro "%s" failed: %s', self.code, e)
|
|
|
|
break
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2020-12-06 13:23:00 +00:00
|
|
|
# done
|
|
|
|
self.running = False
|
|
|
|
|
2021-10-16 09:38:34 +00:00
|
|
|
def press_trigger(self):
|
|
|
|
"""The user pressed the trigger key down."""
|
2021-01-24 17:36:31 +00:00
|
|
|
if self.is_holding():
|
2021-09-26 10:44:56 +00:00
|
|
|
logger.error("Already holding")
|
2021-01-24 17:36:31 +00:00
|
|
|
return
|
|
|
|
|
2021-10-16 09:38:34 +00:00
|
|
|
self._trigger_release_event.clear()
|
|
|
|
self._trigger_press_event.set()
|
2021-01-24 17:36:31 +00:00
|
|
|
|
2020-12-05 11:05:17 +00:00
|
|
|
for macro in self.child_macros:
|
2021-10-16 09:38:34 +00:00
|
|
|
macro.press_trigger()
|
2020-12-05 10:13:10 +00:00
|
|
|
|
2021-10-16 09:38:34 +00:00
|
|
|
def release_trigger(self):
|
|
|
|
"""The user released the trigger key."""
|
|
|
|
self._trigger_release_event.set()
|
|
|
|
self._trigger_press_event.clear()
|
2021-01-24 17:36:31 +00:00
|
|
|
|
2020-12-05 10:58:29 +00:00
|
|
|
for macro in self.child_macros:
|
2021-10-16 09:38:34 +00:00
|
|
|
macro.release_trigger()
|
2020-12-05 10:58:29 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def _keycode_pause(self, _=None):
|
2022-03-03 22:42:37 +00:00
|
|
|
"""To add a pause between keystrokes.
|
2021-10-01 22:55:10 +00:00
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
This was needed at some point because it appeared that injecting keys too
|
|
|
|
fast will prevent them from working. It probably depends on the environment.
|
|
|
|
"""
|
|
|
|
await asyncio.sleep(self.keystroke_sleep_ms / 1000)
|
2021-10-01 22:55:10 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return f'<Macro "{self.code}">'
|
|
|
|
|
|
|
|
"""Functions that prepare the macro"""
|
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
def add_key(self, symbol):
|
|
|
|
"""Write the symbol."""
|
|
|
|
# This is done to figure out if the macro is broken at compile time, because
|
|
|
|
# if KEY_A was unknown we can show this in the gui before the injection starts.
|
|
|
|
_type_check_symbol(symbol)
|
|
|
|
|
|
|
|
async def task(handler):
|
|
|
|
# if the code is $foo, figure out the correct code now.
|
|
|
|
resolved_symbol = _resolve(symbol, [str])
|
|
|
|
code = _type_check_symbol(resolved_symbol)
|
|
|
|
|
|
|
|
resolved_code = _resolve(code, [int])
|
|
|
|
handler(EV_KEY, resolved_code, 1)
|
|
|
|
await self._keycode_pause()
|
|
|
|
handler(EV_KEY, resolved_code, 0)
|
|
|
|
await self._keycode_pause()
|
|
|
|
|
|
|
|
self.tasks.append(task)
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def add_hold(self, macro=None):
|
2020-12-05 10:13:10 +00:00
|
|
|
"""Loops the execution until key release."""
|
2022-03-03 22:42:37 +00:00
|
|
|
_type_check(macro, [Macro, str, None], "hold", 1)
|
2021-10-01 22:55:10 +00:00
|
|
|
|
2021-01-24 17:36:31 +00:00
|
|
|
if macro is None:
|
2021-10-16 09:38:34 +00:00
|
|
|
self.tasks.append(lambda _: self._trigger_release_event.wait())
|
2021-04-25 17:28:02 +00:00
|
|
|
return
|
2021-01-24 17:36:31 +00:00
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
if not isinstance(macro, Macro):
|
2021-09-29 18:17:45 +00:00
|
|
|
# if macro is a key name, hold down the key while the
|
|
|
|
# keyboard key is physically held down
|
2022-03-03 22:42:37 +00:00
|
|
|
symbol = macro
|
|
|
|
_type_check_symbol(symbol)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
2022-03-03 22:42:37 +00:00
|
|
|
resolved_symbol = _resolve(symbol, [str])
|
|
|
|
code = _type_check_symbol(resolved_symbol)
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
resolved_code = _resolve(code, [int])
|
|
|
|
handler(EV_KEY, resolved_code, 1)
|
2021-10-16 09:38:34 +00:00
|
|
|
await self._trigger_release_event.wait()
|
2021-10-01 22:55:10 +00:00
|
|
|
handler(EV_KEY, resolved_code, 0)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|
2021-01-24 17:36:31 +00:00
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
if isinstance(macro, Macro):
|
2021-04-25 17:28:02 +00:00
|
|
|
# repeat the macro forever while the key is held down
|
2021-02-13 19:19:31 +00:00
|
|
|
async def task(handler):
|
2021-01-24 17:36:31 +00:00
|
|
|
while self.is_holding():
|
|
|
|
# run the child macro completely to avoid
|
|
|
|
# not-releasing any key
|
2021-02-13 19:19:31 +00:00
|
|
|
await macro.run(handler)
|
2021-01-24 17:36:31 +00:00
|
|
|
|
2021-03-19 22:03:03 +00:00
|
|
|
self.tasks.append(task)
|
2021-01-24 17:36:31 +00:00
|
|
|
self.child_macros.append(macro)
|
2020-12-05 10:58:29 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def add_modify(self, modifier, macro):
|
2020-11-27 20:27:15 +00:00
|
|
|
"""Do stuff while a modifier is activated.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
modifier : str
|
2021-09-29 21:00:50 +00:00
|
|
|
macro : Macro
|
2020-11-27 20:27:15 +00:00
|
|
|
"""
|
2022-03-03 22:42:37 +00:00
|
|
|
_type_check(macro, [Macro], "modify", 2)
|
|
|
|
_type_check_symbol(modifier)
|
2020-12-04 13:38:41 +00:00
|
|
|
|
2020-12-05 10:58:29 +00:00
|
|
|
self.child_macros.append(macro)
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
2022-03-03 22:42:37 +00:00
|
|
|
# TODO test var
|
|
|
|
resolved_modifier = _resolve(modifier, [str])
|
|
|
|
code = _type_check_symbol(resolved_modifier)
|
|
|
|
|
|
|
|
handler(EV_KEY, code, 1)
|
2021-10-01 22:55:10 +00:00
|
|
|
await self._keycode_pause()
|
|
|
|
await macro.run(handler)
|
|
|
|
await self._keycode_pause()
|
2022-03-03 22:42:37 +00:00
|
|
|
handler(EV_KEY, code, 0)
|
2021-10-01 22:55:10 +00:00
|
|
|
await self._keycode_pause()
|
|
|
|
|
|
|
|
self.tasks.append(task)
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2022-03-03 22:42:37 +00:00
|
|
|
def add_hold_keys(self, *symbols):
|
|
|
|
"""Hold down multiple keys, equivalent to `a + b + c + ...`."""
|
|
|
|
for symbol in symbols:
|
|
|
|
_type_check_symbol(symbol)
|
|
|
|
|
|
|
|
async def task(handler):
|
|
|
|
resolved_symbols = [_resolve(symbol, [str]) for symbol in symbols]
|
|
|
|
codes = [_type_check_symbol(symbol) for symbol in resolved_symbols]
|
|
|
|
|
|
|
|
for code in codes:
|
|
|
|
handler(EV_KEY, code, 1)
|
|
|
|
await self._keycode_pause()
|
|
|
|
|
|
|
|
await self._trigger_release_event.wait()
|
|
|
|
|
|
|
|
for code in codes[::-1]:
|
|
|
|
handler(EV_KEY, code, 0)
|
|
|
|
await self._keycode_pause()
|
|
|
|
|
|
|
|
self.tasks.append(task)
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def add_repeat(self, repeats, macro):
|
2020-11-27 20:27:15 +00:00
|
|
|
"""Repeat actions.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2021-09-29 21:00:50 +00:00
|
|
|
repeats : int or Macro
|
|
|
|
macro : Macro
|
2020-11-27 20:27:15 +00:00
|
|
|
"""
|
2022-03-03 22:42:37 +00:00
|
|
|
repeats = _type_check(repeats, [int], "repeat", 1)
|
|
|
|
_type_check(macro, [Macro], "repeat", 2)
|
2020-12-04 13:52:08 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
|
|
|
for _ in range(_resolve(repeats, [int])):
|
2021-04-25 17:28:02 +00:00
|
|
|
await macro.run(handler)
|
2020-12-05 10:58:29 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|
2020-12-05 10:58:29 +00:00
|
|
|
self.child_macros.append(macro)
|
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
def add_event(self, type_, code, value):
|
2021-03-19 22:03:03 +00:00
|
|
|
"""Write any event.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2022-02-08 14:53:59 +00:00
|
|
|
type_: str or int
|
2021-03-19 22:03:03 +00:00
|
|
|
examples: 2, 'EV_KEY'
|
2022-03-03 22:42:37 +00:00
|
|
|
code : str or int
|
2021-03-19 22:03:03 +00:00
|
|
|
examples: 52, 'KEY_A'
|
|
|
|
value : int
|
|
|
|
"""
|
2022-03-03 22:42:37 +00:00
|
|
|
type_ = _type_check(type_, [int, str], "event", 1)
|
|
|
|
code = _type_check(code, [int, str], "event", 2)
|
|
|
|
value = _type_check(value, [int, str], "event", 3)
|
2021-10-06 22:09:57 +00:00
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
if isinstance(type_, str):
|
|
|
|
type_ = ecodes[type_.upper()]
|
2021-03-19 22:03:03 +00:00
|
|
|
if isinstance(code, str):
|
|
|
|
code = ecodes[code.upper()]
|
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
self.tasks.append(lambda handler: handler(type_, code, value))
|
2021-04-25 17:28:02 +00:00
|
|
|
self.tasks.append(self._keycode_pause)
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def add_mouse(self, direction, speed):
|
|
|
|
"""Move the mouse cursor."""
|
|
|
|
_type_check(direction, [str], "mouse", 1)
|
|
|
|
speed = _type_check(speed, [int], "mouse", 2)
|
2021-09-29 21:00:50 +00:00
|
|
|
|
2021-03-19 22:03:03 +00:00
|
|
|
code, value = {
|
2021-09-26 10:44:56 +00:00
|
|
|
"up": (REL_Y, -1),
|
|
|
|
"down": (REL_Y, 1),
|
|
|
|
"left": (REL_X, -1),
|
|
|
|
"right": (REL_X, 1),
|
2021-03-19 22:03:03 +00:00
|
|
|
}[direction.lower()]
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
|
|
|
resolved_speed = value * _resolve(speed, [int])
|
|
|
|
while self.is_holding():
|
|
|
|
handler(EV_REL, code, resolved_speed)
|
|
|
|
await self._keycode_pause()
|
|
|
|
|
|
|
|
self.tasks.append(task)
|
|
|
|
|
|
|
|
def add_wheel(self, direction, speed):
|
|
|
|
"""Move the scroll wheel."""
|
|
|
|
_type_check(direction, [str], "wheel", 1)
|
|
|
|
speed = _type_check(speed, [int], "wheel", 2)
|
2021-09-29 21:00:50 +00:00
|
|
|
|
2021-03-19 22:03:03 +00:00
|
|
|
code, value = {
|
2022-04-17 10:19:23 +00:00
|
|
|
"up": ([REL_WHEEL, REL_WHEEL_HI_RES], [1 / 120, 1]),
|
|
|
|
"down": ([REL_WHEEL, REL_WHEEL_HI_RES], [-1 / 120, -1]),
|
|
|
|
"left": ([REL_HWHEEL, REL_HWHEEL_HI_RES], [1 / 120, 1]),
|
|
|
|
"right": ([REL_HWHEEL, REL_HWHEEL_HI_RES], [-1 / 120, -1]),
|
2021-03-19 22:03:03 +00:00
|
|
|
}[direction.lower()]
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
|
|
|
resolved_speed = _resolve(speed, [int])
|
2022-04-17 10:19:23 +00:00
|
|
|
remainder = [0.0, 0.0]
|
2021-10-01 22:55:10 +00:00
|
|
|
while self.is_holding():
|
2022-04-17 10:19:23 +00:00
|
|
|
for i in range(0, 2):
|
|
|
|
float_value = value[i] * resolved_speed + remainder[i]
|
|
|
|
remainder[i] = math.fmod(float_value, 1)
|
|
|
|
if abs(float_value) >= 1:
|
|
|
|
handler(EV_REL, code[i], int(float_value))
|
|
|
|
await asyncio.sleep(1 / self.mapping.rate)
|
2021-10-01 22:55:10 +00:00
|
|
|
|
|
|
|
self.tasks.append(task)
|
|
|
|
|
|
|
|
def add_wait(self, time):
|
2020-11-28 21:54:22 +00:00
|
|
|
"""Wait time in milliseconds."""
|
2021-10-01 22:55:10 +00:00
|
|
|
time = _type_check(time, [int, float], "wait", 1)
|
2020-11-28 19:59:24 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(_):
|
|
|
|
await asyncio.sleep(_resolve(time, [int, float]) / 1000)
|
2020-11-28 17:52:19 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
def add_set(self, variable, value):
|
2021-04-25 17:28:02 +00:00
|
|
|
"""Set a variable to a certain value."""
|
2021-10-02 20:40:05 +00:00
|
|
|
_type_check_variablename(variable)
|
2021-09-26 10:44:56 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(_):
|
|
|
|
# can also copy with set(a, $b)
|
|
|
|
resolved_value = _resolve(value)
|
|
|
|
logger.debug('"%s" set to "%s"', variable, resolved_value)
|
2021-04-25 17:28:02 +00:00
|
|
|
macro_variables[variable] = value
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
def add_ifeq(self, variable, value, then=None, else_=None):
|
2021-10-01 22:55:10 +00:00
|
|
|
"""Old version of if_eq, kept for compatibility reasons.
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
This can't support a comparison like ifeq("foo", $blub) with blub containing
|
|
|
|
"foo" without breaking old functionality, because "foo" is treated as a
|
|
|
|
variable name.
|
2021-04-25 17:28:02 +00:00
|
|
|
"""
|
2021-10-01 22:55:10 +00:00
|
|
|
_type_check(then, [Macro, None], "ifeq", 3)
|
2022-02-08 14:53:59 +00:00
|
|
|
_type_check(else_, [Macro, None], "ifeq", 4)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
2021-04-25 17:28:02 +00:00
|
|
|
set_value = macro_variables.get(variable)
|
|
|
|
logger.debug('"%s" is "%s"', variable, set_value)
|
|
|
|
if set_value == value:
|
2021-09-29 18:17:45 +00:00
|
|
|
if then is not None:
|
|
|
|
await then.run(handler)
|
2022-02-08 14:53:59 +00:00
|
|
|
elif else_ is not None:
|
|
|
|
await else_.run(handler)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-09-29 21:00:50 +00:00
|
|
|
if isinstance(then, Macro):
|
2021-09-29 18:17:45 +00:00
|
|
|
self.child_macros.append(then)
|
2022-02-08 14:53:59 +00:00
|
|
|
if isinstance(else_, Macro):
|
|
|
|
self.child_macros.append(else_)
|
2021-04-25 17:28:02 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|
2020-11-27 20:27:15 +00:00
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
def add_if_eq(self, value_1, value_2, then=None, else_=None):
|
2021-10-01 22:55:10 +00:00
|
|
|
"""Compare two values."""
|
|
|
|
_type_check(then, [Macro, None], "if_eq", 3)
|
2022-02-08 14:53:59 +00:00
|
|
|
_type_check(else_, [Macro, None], "if_eq", 4)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
|
|
|
resolved_value_1 = _resolve(value_1)
|
|
|
|
resolved_value_2 = _resolve(value_2)
|
|
|
|
if resolved_value_1 == resolved_value_2:
|
|
|
|
if then is not None:
|
|
|
|
await then.run(handler)
|
2022-02-08 14:53:59 +00:00
|
|
|
elif else_ is not None:
|
|
|
|
await else_.run(handler)
|
2021-09-29 21:00:50 +00:00
|
|
|
|
|
|
|
if isinstance(then, Macro):
|
2021-09-29 18:17:45 +00:00
|
|
|
self.child_macros.append(then)
|
2022-02-08 14:53:59 +00:00
|
|
|
if isinstance(else_, Macro):
|
|
|
|
self.child_macros.append(else_)
|
2021-10-01 22:55:10 +00:00
|
|
|
|
|
|
|
self.tasks.append(task)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
def add_if_tap(self, then=None, else_=None, timeout=300):
|
2021-10-16 09:38:34 +00:00
|
|
|
"""If a key was pressed quickly.
|
|
|
|
|
|
|
|
macro key pressed -> if_tap starts -> key released -> then
|
|
|
|
|
|
|
|
macro key pressed -> released (does other stuff in the meantime)
|
|
|
|
-> if_tap starts -> pressed -> released -> then
|
|
|
|
"""
|
2021-10-01 22:55:10 +00:00
|
|
|
_type_check(then, [Macro, None], "if_tap", 1)
|
2022-02-08 14:53:59 +00:00
|
|
|
_type_check(else_, [Macro, None], "if_tap", 2)
|
2021-10-01 22:55:10 +00:00
|
|
|
timeout = _type_check(timeout, [int, float], "if_tap", 3)
|
|
|
|
|
|
|
|
if isinstance(then, Macro):
|
|
|
|
self.child_macros.append(then)
|
2022-02-08 14:53:59 +00:00
|
|
|
if isinstance(else_, Macro):
|
|
|
|
self.child_macros.append(else_)
|
2021-10-01 22:55:10 +00:00
|
|
|
|
2021-10-16 09:38:34 +00:00
|
|
|
async def wait():
|
|
|
|
"""Wait for a release, or if nothing pressed yet, a press and release."""
|
|
|
|
if self.is_holding():
|
|
|
|
await self._trigger_release_event.wait()
|
|
|
|
else:
|
|
|
|
await self._trigger_press_event.wait()
|
|
|
|
await self._trigger_release_event.wait()
|
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
2021-10-02 20:40:05 +00:00
|
|
|
resolved_timeout = _resolve(timeout, [int, float]) / 1000
|
2021-09-29 18:17:45 +00:00
|
|
|
try:
|
2021-10-16 09:38:34 +00:00
|
|
|
await asyncio.wait_for(wait(), resolved_timeout)
|
2021-09-29 18:17:45 +00:00
|
|
|
if then:
|
|
|
|
await then.run(handler)
|
|
|
|
except asyncio.TimeoutError:
|
2022-02-08 14:53:59 +00:00
|
|
|
if else_:
|
|
|
|
await else_.run(handler)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-02-08 14:53:59 +00:00
|
|
|
def add_if_single(self, then, else_, timeout=None):
|
2021-10-01 22:55:10 +00:00
|
|
|
"""If a key was pressed without combining it."""
|
|
|
|
_type_check(then, [Macro, None], "if_single", 1)
|
2022-02-08 14:53:59 +00:00
|
|
|
_type_check(else_, [Macro, None], "if_single", 2)
|
2021-09-29 21:00:50 +00:00
|
|
|
|
|
|
|
if isinstance(then, Macro):
|
2021-09-29 18:17:45 +00:00
|
|
|
self.child_macros.append(then)
|
2022-02-08 14:53:59 +00:00
|
|
|
if isinstance(else_, Macro):
|
|
|
|
self.child_macros.append(else_)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
async def task(handler):
|
2022-04-17 10:19:23 +00:00
|
|
|
listener_done = asyncio.Event()
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
async def listener(event):
|
|
|
|
if event.type != EV_KEY:
|
|
|
|
# ignore anything that is not a key
|
|
|
|
return
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
if event.value == 1:
|
|
|
|
# another key was pressed, trigger else
|
|
|
|
listener_done.set()
|
|
|
|
return
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.context.listeners.add(listener)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
resolved_timeout = _resolve(timeout, allowed_types=[int, float, None])
|
|
|
|
await asyncio.wait(
|
|
|
|
[listener_done.wait(), self._trigger_release_event.wait()],
|
|
|
|
timeout=resolved_timeout / 1000 if resolved_timeout else None,
|
|
|
|
return_when=asyncio.FIRST_COMPLETED,
|
|
|
|
)
|
2022-01-10 19:37:22 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
self.context.listeners.remove(listener)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2022-04-17 10:19:23 +00:00
|
|
|
if not listener_done.is_set() and self._trigger_release_event.is_set():
|
|
|
|
await then.run(handler) # was trigger release
|
|
|
|
else:
|
2022-02-08 14:53:59 +00:00
|
|
|
await else_.run(handler)
|
2021-09-29 18:17:45 +00:00
|
|
|
|
2021-10-01 22:55:10 +00:00
|
|
|
self.tasks.append(task)
|