From b197e576fa6a5dae228bbb8d634f16478bf90e3e Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Tue, 8 Mar 2022 00:38:05 +0100 Subject: [PATCH] When sddm-greeter is active with 2 windows we always begin by focusing the other one Not intelligent, is error-prone but better than nothing. Fixes #1. --- main.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/main.py b/main.py index 73d034d..ec99043 100644 --- a/main.py +++ b/main.py @@ -21,6 +21,7 @@ logging.basicConfig( )] ) log = logging.getLogger("rich") +log.setLevel("DEBUG") # Vars @@ -53,6 +54,70 @@ log.debug(f"Printing environment vars ...") log.debug(os.environ) +def x_focus_window(window_id: str) -> bool: + log.debug(f"Focusing window ID {window_id} ...") + try: + subprocess.run( + ["xdotool", "windowfocus", window_id], + check=True, + stdout=subprocess.PIPE, + encoding="utf-8") + log.info(f"Two 'sddm-greeter' windows detected one of which had focus, changed focus to the other one.") + return True + except subprocess.CalledProcessError as x_focus_window_error: + log.warning(f"Failed to focus window ID {window_id}, we'll not try again") + log.warning(x_focus_window_error) + return False + + +def sddm_greeter_window_ids() -> list: + log.debug(f"Checking if an 'sddm-greeter' window exists ...") + try: + dm_windows = subprocess.run( + ["xdotool", "search", "--onlyvisible", "--classname", "sddm-greeter"], + check=True, + stdout=subprocess.PIPE, + encoding="utf-8") + return dm_windows.stdout.splitlines() + except subprocess.CalledProcessError as xdotool_error: + log.debug(f"No 'sddm-greeter' window exists, doing nothing") + log.debug(xdotool_error) + return [] + + +def has_sddm_focus() -> bool: + dm_windows = sddm_greeter_window_ids() + if not dm_windows: + return False + else: + log.debug(f"Checking if 'sddm-greeter' window has focus ...") + try: + x_window_focus = subprocess.run( + ["xdotool", "getwindowfocus"], + check=True, + stdout=subprocess.PIPE, + encoding="utf-8") + x_window_id_has_focus = x_window_focus.stdout.splitlines()[0] + if x_window_id_has_focus in dm_windows: + log.debug(f"An 'sddm-greeter' window has focus") + if len(dm_windows) > 1: + if len(dm_windows) == 2: + log.debug(f"We're assuming we have to focus the other one") + log.debug(f"From list of 'sddm-greeter' window IDs ({dm_windows}) remove the one that has " + f"focus ({x_window_id_has_focus})") + dm_windows.remove(x_window_id_has_focus) + x_focus_window(dm_windows[0]) + else: + log.warning(f"Number of 'sddm-greeter' windows ({len(dm_windows)}) is unexpected, not doing " + f"anything") + else: + log.debug(f"No other 'sddm-greeter' windows exist, no need to focus anything") + except subprocess.CalledProcessError as xdotool_error: + log.warning(f"Unable to get ID of focused window") + log.warning(xdotool_error) + return False + + def propagate_keypress(cec_key_id: int) -> None: log.info(f"""Key press '{xdo_map[cec_key_id]["human_readable"]}' detected (CEC key ID '{cec_key_id}')""") subprocess.run(["xdotool", "key", xdo_map[cec_key_id]["xdo"]], check=True) @@ -135,6 +200,7 @@ cec.add_callback(keypress_handler, cec.EVENT_KEYPRESS) cec.add_callback(log_handler, cec.EVENT_LOG) log.debug(f"Event handlers active") log.info(f"Open for business on adapter '{use_adapter}'!") +has_sddm_focus() while True: