|
|
@ -39,27 +39,18 @@ w(1000).m('SHIFT_L', r(2, k('a'))).w(10).k('b'): <1s> 'A' 'A' <10ms> 'b'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
|
|
|
|
import re
|
|
|
|
import random
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
from rich.traceback import install
|
|
|
|
|
|
|
|
install(show_locals=True)
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# TODO parse securely
|
|
|
|
from keymapper.logger import logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def m(*args):
|
|
|
|
|
|
|
|
return Macro().m(*args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def r(*args):
|
|
|
|
|
|
|
|
return Macro().r(*args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def k(*args):
|
|
|
|
|
|
|
|
return Macro().k(*args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def w(*args):
|
|
|
|
|
|
|
|
return Macro().w(*args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.setLevel(5)
|
|
|
|
|
|
|
|
|
|
|
|
class Macro:
|
|
|
|
class Macro:
|
|
|
|
"""Supports chaining and preparing actions."""
|
|
|
|
"""Supports chaining and preparing actions."""
|
|
|
@ -76,7 +67,7 @@ class Macro:
|
|
|
|
"""Stop the macro."""
|
|
|
|
"""Stop the macro."""
|
|
|
|
# TODO
|
|
|
|
# TODO
|
|
|
|
|
|
|
|
|
|
|
|
def m(self, modifier, macro):
|
|
|
|
def modify(self, modifier, macro):
|
|
|
|
"""Do stuff while a modifier is activated.
|
|
|
|
"""Do stuff while a modifier is activated.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
Parameters
|
|
|
@ -89,7 +80,7 @@ class Macro:
|
|
|
|
# TODO release modifier
|
|
|
|
# TODO release modifier
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def r(self, repeats, macro):
|
|
|
|
def repeat(self, repeats, macro):
|
|
|
|
"""Repeat actions.
|
|
|
|
"""Repeat actions.
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
Parameters
|
|
|
@ -101,30 +92,160 @@ class Macro:
|
|
|
|
self.tasks.append(macro.run)
|
|
|
|
self.tasks.append(macro.run)
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def k(self, character):
|
|
|
|
def keycode(self, character):
|
|
|
|
"""Write the character."""
|
|
|
|
"""Write the character."""
|
|
|
|
# TODO write character
|
|
|
|
# TODO write character
|
|
|
|
self.tasks.append(lambda: print(character))
|
|
|
|
self.tasks.append(lambda: print(character))
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
def w(self, min, max=None):
|
|
|
|
def wait(self, min, max=None):
|
|
|
|
"""Wait a random time in milliseconds"""
|
|
|
|
"""Wait a random time in milliseconds"""
|
|
|
|
# TODO random
|
|
|
|
# TODO random
|
|
|
|
self.tasks.append(lambda: time.sleep(min / 1000))
|
|
|
|
self.tasks.append(lambda: time.sleep(min / 1000))
|
|
|
|
return self
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO make these into tests
|
|
|
|
def parse(macro):
|
|
|
|
|
|
|
|
"""parse and generate a Macro that can be run as often as you want.
|
|
|
|
print()
|
|
|
|
|
|
|
|
r(3, k('a').w(200)).run()
|
|
|
|
Parameters
|
|
|
|
|
|
|
|
----------
|
|
|
|
print()
|
|
|
|
macro : string
|
|
|
|
r(2, k('a').k('-')).k('b').run()
|
|
|
|
"r(3, k(a).w(10))"
|
|
|
|
|
|
|
|
"r(2, k(a).k(-)).k(b)"
|
|
|
|
print()
|
|
|
|
"w(1000).m(SHIFT_L, r(2, k(a))).w(10, 20).k(b)"
|
|
|
|
w(400).m('SHIFT_L', r(2, k('a'))).w(10).k('b').run()
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
print()
|
|
|
|
return parse_recurse(macro)
|
|
|
|
# prints nothing yet
|
|
|
|
except Exception as e:
|
|
|
|
k('a').r(3, k('b'))
|
|
|
|
logger.error(e)
|
|
|
|
|
|
|
|
# parsing unsuccessful
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_params(inner):
|
|
|
|
|
|
|
|
"""Extract parameters from the inner contents of a call.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
inner : string
|
|
|
|
|
|
|
|
for example 'r, r(2, k(a))' should result in ['r', 'r(2, k(a)']
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
inner = inner.strip()
|
|
|
|
|
|
|
|
brackets = 0
|
|
|
|
|
|
|
|
params = []
|
|
|
|
|
|
|
|
start = 0
|
|
|
|
|
|
|
|
for position, char in enumerate(inner):
|
|
|
|
|
|
|
|
if char == '(':
|
|
|
|
|
|
|
|
brackets += 1
|
|
|
|
|
|
|
|
if char == ')':
|
|
|
|
|
|
|
|
brackets -= 1
|
|
|
|
|
|
|
|
if (char == ',') and brackets == 0:
|
|
|
|
|
|
|
|
# , potentially starts another parameter, but only if
|
|
|
|
|
|
|
|
# the current brackets are all closed.
|
|
|
|
|
|
|
|
params.append(inner[start:position].strip())
|
|
|
|
|
|
|
|
# skip the comma
|
|
|
|
|
|
|
|
start = position + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if brackets == 0 and start != len(inner):
|
|
|
|
|
|
|
|
# one last parameter
|
|
|
|
|
|
|
|
params.append(inner[start:].strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_recurse(macro, macro_instance=None, depth=0):
|
|
|
|
|
|
|
|
"""Handle a subset of the macro, e.g. one parameter or function call.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
macro : string
|
|
|
|
|
|
|
|
Just like parse
|
|
|
|
|
|
|
|
macro_instance : Macro or None
|
|
|
|
|
|
|
|
A macro instance to add tasks to
|
|
|
|
|
|
|
|
depth : int
|
|
|
|
|
|
|
|
For logging and debugging purposes
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
# to anyone who knows better about compilers and thinks this is horrible:
|
|
|
|
|
|
|
|
# please make a pull request. Because it probably is.
|
|
|
|
|
|
|
|
# not using eval for security reasons ofc. And this syntax doesn't need
|
|
|
|
|
|
|
|
# string quotes for its params.
|
|
|
|
|
|
|
|
if macro_instance is None:
|
|
|
|
|
|
|
|
macro_instance = Macro()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
macro = macro.strip()
|
|
|
|
|
|
|
|
logger.spam('%sinput %s', ' ' * depth, macro)
|
|
|
|
|
|
|
|
space = ' ' * depth
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# is it another macro?
|
|
|
|
|
|
|
|
call_match = re.match(r'^(\w+)\(.+?', macro)
|
|
|
|
|
|
|
|
call = call_match[1] if call_match else None
|
|
|
|
|
|
|
|
if call is not None:
|
|
|
|
|
|
|
|
# available functions in the macro
|
|
|
|
|
|
|
|
functions = {
|
|
|
|
|
|
|
|
'm': macro_instance.modify,
|
|
|
|
|
|
|
|
'r': macro_instance.repeat,
|
|
|
|
|
|
|
|
'k': macro_instance.keycode,
|
|
|
|
|
|
|
|
'w': macro_instance.wait
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if functions.get(call) is None:
|
|
|
|
|
|
|
|
logger.error(f'Unknown function %s', call)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# get all the stuff inbetween
|
|
|
|
|
|
|
|
brackets = 0
|
|
|
|
|
|
|
|
position = 0
|
|
|
|
|
|
|
|
for char in macro:
|
|
|
|
|
|
|
|
position += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if char == '(':
|
|
|
|
|
|
|
|
brackets += 1
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if char == ')':
|
|
|
|
|
|
|
|
brackets -= 1
|
|
|
|
|
|
|
|
if brackets < 0:
|
|
|
|
|
|
|
|
logger.error(f'There is one ")" too much at %s', position)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
if brackets == 0:
|
|
|
|
|
|
|
|
# the closing bracket of the call
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if brackets != 0:
|
|
|
|
|
|
|
|
logger.error(f'There are %s closing brackets missing', brackets)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inner = macro[2:position - 1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# split "3, k(a).w(10)" into parameters
|
|
|
|
|
|
|
|
string_params = extract_params(inner)
|
|
|
|
|
|
|
|
logger.spam('%scalls %s with %s', space, call, string_params)
|
|
|
|
|
|
|
|
# evaluate the params
|
|
|
|
|
|
|
|
params = [
|
|
|
|
|
|
|
|
parse_recurse(param.strip(), None, depth + 1)
|
|
|
|
|
|
|
|
for param in string_params
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.spam('%scalling %s with %s', space, call, params)
|
|
|
|
|
|
|
|
functions[call](*params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# is after this another call? Chain it to the macro_instance
|
|
|
|
|
|
|
|
if len(macro) > position and macro[position] == '.':
|
|
|
|
|
|
|
|
chain = macro[position + 1:]
|
|
|
|
|
|
|
|
logger.spam('%sfollowed by %s', space, chain)
|
|
|
|
|
|
|
|
parse_recurse(chain, macro_instance, depth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return macro_instance
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# probably a parameter for an outer function
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
macro = int(macro)
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
return macro
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
parse("k(1).k(2).k(3)").run()
|
|
|
|
|
|
|
|
parse("r(1, k(2))").run()
|
|
|
|
|
|
|
|
parse("r(3, k(a).w(10))").run()
|
|
|
|
|
|
|
|
parse("r(2, k(a).k(-)).k(b)").run()
|
|
|
|
|
|
|
|
parse("w(1000).m(SHIFT_L, r(2, k(a))).w(10, 20).k(b)").run()
|
|
|
|