From d0a44840233b474038833096ecbe85ba7baa6431 Mon Sep 17 00:00:00 2001 From: Kay Gosho Date: Sat, 19 Feb 2022 14:23:20 +0900 Subject: [PATCH 1/2] enable to wait sway --- example/multiple-keyboards.py | 33 ++++++++++++++++++++++++++++++++- wayremap/main.py | 12 ++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) 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..80fc1fe 100644 --- a/wayremap/main.py +++ b/wayremap/main.py @@ -53,6 +53,18 @@ 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 == 10: + raise Exception('Cannot find sway socket under /run/user/') + 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( From 8327df7cdeee6b662abe7ddc3c7f14647d1aa9ba Mon Sep 17 00:00:00 2001 From: Kay Gosho Date: Sat, 19 Feb 2022 14:24:57 +0900 Subject: [PATCH 2/2] add wait_sway --- README.md | 6 +++++- wayremap/main.py | 6 ++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5188ef5..0393bc3 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Simply write your own service and run it as 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/wayremap/main.py b/wayremap/main.py index 80fc1fe..2add3c6 100644 --- a/wayremap/main.py +++ b/wayremap/main.py @@ -57,8 +57,10 @@ def wait_sway(tried=0): try: find_sway_ipc_path() except: - if tried == 10: - raise Exception('Cannot find sway socket under /run/user/') + 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)