diff --git a/.gitignore b/.gitignore index 31d10a1..ac5a70f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ **/*.sw* -**/testdata **/__pycache__ diff --git a/poc/py-ratio1024-log2/buttons.py b/poc/py-ratio1024-log2/buttons.py new file mode 100644 index 0000000..ce35b87 --- /dev/null +++ b/poc/py-ratio1024-log2/buttons.py @@ -0,0 +1,36 @@ +from Xlib.display import Display +from Xlib.ext.xtest import fake_input +from Xlib import X +from os import environ + +_display = Display(environ['DISPLAY']) + +def tap(keycode): + down(keycode) + up(keycode) + +def down(keycode): + fake_input(_display, X.KeyPress, keycode) + _display.sync() + +def up(keycode): + fake_input(_display, X.KeyRelease, keycode) + _display.sync() + +def __init_keys__(): + import subprocess + _p = subprocess.run( + "xmodmap -pke".split(), + capture_output=True, + ) + assert(_p.returncode == 0) + stdout = _p.stdout + result = [] + for line in stdout.split("\n".encode()): + if line: + words = line.split() + key = int(words[1]) + if len(words) < 4: + result.append(key) + return result +keys = __init_keys__() diff --git a/poc/py-ratio1024-log2/state_to_buttons.py b/poc/py-ratio1024-log2/state_to_buttons.py index cf041ec..ad314b4 100644 --- a/poc/py-ratio1024-log2/state_to_buttons.py +++ b/poc/py-ratio1024-log2/state_to_buttons.py @@ -2,6 +2,8 @@ import argparse import os from time import sleep +import buttons + def main(): args = get_args() buckets = {} @@ -14,16 +16,16 @@ def main(): continue key = line.strip("/").split()[0] if not key in buckets: - buckets[key] = False - buckets[key] = False if line[0] == "/" else float(line.split()[-1]) - result = sorted([ - (int(100*buckets[k]), k) for k in buckets if buckets[k] - ], reverse=True) - print("[", - " ".join([ - "{}={:2}".format(i[1], i[0]) for i in result - ]), - "]") + buckets[key] = [False, len(buckets)] + buckets[key][0] = False if line[0] == "/" else float(line.split()[-1]) + for key in bucket: + keycode = buttons.keys[bucket[key][1]] + if bucket[key][0]: + print("down", key, button[key]) + buttons.down(keycode) + else: + print("up", key, button[key]) + buttons.up(keycode) def get_args(): ap = argparse.ArgumentParser()