diff --git a/README.md b/README.md index 79d0700..0265b93 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Simply write your own service and run it as a python script: ```python # /etc/wayremap.config.py -from wayremap import ecodes as e, run, WayremapConfig, Binding +from wayremap import ecodes as e, run, WayremapConfig, Binding, wait_sway import uinput as k wayremap_config = WayremapConfig( @@ -89,6 +89,10 @@ wayremap_config = WayremapConfig( Binding([e.KEY_LEFTALT, e.KEY_X], [[k.KEY_LEFTCTRL, k.KEY_K]]), ]) + +# Required if you want to use wayremap as a startup service. +wait_sway() + # Finally, run wayremap. run(wayremap_config) diff --git a/example/multiple-keyboards.py b/example/multiple-keyboards.py index e300b1d..4b41a18 100644 --- a/example/multiple-keyboards.py +++ b/example/multiple-keyboards.py @@ -1,14 +1,45 @@ import uinput as k -from wayremap import ecodes as e, WayremapConfig, Binding, run +from wayremap import ecodes as e, WayremapConfig, Binding, run, wait_sway applications = [ 'Brave-browser', ] bindings = [ + # Emacs-like key binding + Binding([e.KEY_LEFTCTRL, e.KEY_LEFTALT, e.KEY_A], + [[k.KEY_LEFTCTRL, k.KEY_HOME]]), + Binding([e.KEY_LEFTCTRL, e.KEY_LEFTALT, e.KEY_E], + [[k.KEY_LEFTCTRL, k.KEY_END]]), + Binding([e.KEY_LEFTCTRL, e.KEY_LEFTALT, e.KEY_H], + [[k.KEY_LEFTCTRL, k.KEY_BACKSPACE]]), + Binding([e.KEY_LEFTCTRL, e.KEY_F], [[k.KEY_RIGHT]]), + Binding([e.KEY_LEFTCTRL, e.KEY_B], [[k.KEY_LEFT]]), + Binding([e.KEY_LEFTCTRL, e.KEY_P], [[k.KEY_UP]]), + Binding([e.KEY_LEFTCTRL, e.KEY_N], [[k.KEY_DOWN]]), + Binding([e.KEY_LEFTCTRL, e.KEY_K], + [[k.KEY_LEFTSHIFT, k.KEY_END], [k.KEY_LEFTCTRL, k.KEY_X]]), + Binding([e.KEY_LEFTCTRL, e.KEY_A], [[k.KEY_HOME]]), + Binding([e.KEY_LEFTCTRL, e.KEY_E], [[k.KEY_END]]), + Binding([e.KEY_LEFTCTRL, e.KEY_Y], [[k.KEY_LEFTCTRL, k.KEY_V]]), + Binding([e.KEY_LEFTALT, e.KEY_F], [[k.KEY_LEFTCTRL, k.KEY_RIGHT]]), + Binding([e.KEY_LEFTALT, e.KEY_B], [[k.KEY_LEFTCTRL, k.KEY_LEFT]]), + Binding([e.KEY_LEFTALT, e.KEY_D], [[k.KEY_LEFTCTRL, k.KEY_DELETE]]), + Binding([e.KEY_LEFTCTRL, e.KEY_H], [[k.KEY_BACKSPACE]]), + Binding([e.KEY_LEFTCTRL, e.KEY_D], [[k.KEY_DELETE]]), Binding([e.KEY_LEFTCTRL, e.KEY_S], [[k.KEY_LEFTCTRL, k.KEY_F]]), + + # OSX-like key binding + Binding([e.KEY_LEFTALT, e.KEY_A], [[k.KEY_LEFTCTRL, k.KEY_A]]), + Binding([e.KEY_LEFTALT, e.KEY_C], [[k.KEY_LEFTCTRL, k.KEY_C]]), + Binding([e.KEY_LEFTALT, e.KEY_V], [[k.KEY_LEFTCTRL, k.KEY_V]]), + + # Slack helm! + Binding([e.KEY_LEFTALT, e.KEY_X], [[k.KEY_LEFTCTRL, k.KEY_K]]), ] +wait_sway() + run( WayremapConfig(input_identifier= 'Lenovo TrackPoint Keyboard II usb-0000:00:14.0-1/input0', diff --git a/wayremap/main.py b/wayremap/main.py index 6e4dab5..2add3c6 100644 --- a/wayremap/main.py +++ b/wayremap/main.py @@ -53,6 +53,20 @@ def find_sway_ipc_path() -> str: raise Exception('Cannot find sway socket under /run/user/') +def wait_sway(tried=0): + try: + find_sway_ipc_path() + except: + if tried > 30: + raise Exception( + 'Cannot find sway socket under `/run/user/`, tried 30 seconds.' + ) + else: + print(f"Waiting for sway... {tried}") + time.sleep(1) + wait_sway(tried + 1) + + def subscribe_sway(apps: list[str]): if apps is None or len(apps) == 0: print(