From 91c033869c60792752e85499e916aade03a7dd57 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Tue, 8 Mar 2022 02:20:10 +0100 Subject: [PATCH] Focus correct sddm-greeter window ID if we have more than one Also add inflect for cleaner English texts. Fixes #3. --- main.py | 108 ++++++++++++++++++++++++++++++----------------- requirements.in | 3 +- requirements.txt | 4 ++ 3 files changed, 76 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index 8916629..0e15a62 100644 --- a/main.py +++ b/main.py @@ -6,12 +6,14 @@ import sys import time import cec import logging +import inflect from rich import traceback from rich.console import Console from rich.logging import RichHandler from inotify_simple import INotify, flags traceback.install() console = Console() +p = inflect.engine() FORMAT = "%(message)s" @@ -71,18 +73,29 @@ def x_focus_window(window_id: str) -> bool: 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 [] + sddm_check_attempt = 1 + sddm_check_attempts_max = 2 + sddm_check_sleep = 2 + while sddm_check_attempt <= sddm_check_attempts_max: + log.debug(f"Checking if 'sddm-greeter' window exists " + f"({sddm_check_attempt} of {sddm_check_attempts_max}) ...") + try: + dm_windows = subprocess.run( + ["xdotool", "search", "--onlyvisible", "--classname", "sddm-greeter"], + check=True, + capture_output=True, + encoding="utf-8") + return dm_windows.stdout.splitlines() + except subprocess.CalledProcessError as xdotool_error: + if sddm_check_attempt < sddm_check_attempts_max: + log.debug(f"No 'sddm-greeter' window, checking again in " + f"""{sddm_check_sleep} {p.plural("second", sddm_check_sleep)} ...""") + sddm_check_attempt += 1 + time.sleep(sddm_check_sleep) + else: + log.debug(f"No 'sddm-greeter' window exists, doing nothing") + log.debug(xdotool_error) + return [] def sddm_change_focus_if_two_windows() -> bool: @@ -90,32 +103,51 @@ def sddm_change_focus_if_two_windows() -> bool: 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]) + sddm_focus_attempt = 1 + sddm_focus_attempt_max = 2 + sddm_focus_sleep = 2 + sddm_window_count_attempt = 1 + sddm_window_count_attempt_max = 2 + while sddm_focus_attempt <= sddm_focus_attempt_max: + log.debug(f"Checking if 'sddm-greeter' window has focus " + f"({sddm_focus_attempt} of {sddm_focus_attempt_max}) ...") + try: + x_window_focus = subprocess.run( + ["xdotool", "getwindowfocus"], + check=True, + capture_output=True, + 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]) + return True + else: + log.warning(f"Number of 'sddm-greeter' windows ({len(dm_windows)}) unexpected, " + f"not doing anything") + return False else: - log.warning(f"Number of 'sddm-greeter' windows ({len(dm_windows)}) is unexpected, not doing " - f"anything") + log.debug(f"No other 'sddm-greeter' windows exist, checking again ...") + sddm_window_count_attempt += 1 + if sddm_window_count_attempt == sddm_window_count_attempt_max: + log.debug(f"No other 'sddm-greeter' windows exist, no need to focus anything") + return False + except subprocess.CalledProcessError as xdotool_error: + if sddm_focus_attempt < sddm_focus_attempt_max: + log.debug(f"Unable to get ID of focused window, checking again in " + f"""{sddm_focus_sleep} {p.plural("second", sddm_focus_sleep)} ...""") + sddm_focus_attempt += 1 + time.sleep(sddm_focus_sleep) 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 + log.warning(f"Unable to get ID of focused window") + log.warning(xdotool_error) + return False def propagate_keypress(cec_key_id: int) -> None: @@ -166,6 +198,7 @@ def signal_handler(rx_signal: int, frame) -> None: def set_xauth_env(file_abs_path: str) -> None: os.environ["XAUTHORITY"] = file_abs_path log.debug(f"""Env variable 'XAUTHORITY' set to '{os.environ["XAUTHORITY"]}'""") + sddm_change_focus_if_two_windows() signal.signal(signal.SIGINT, signal_handler) @@ -190,9 +223,9 @@ xdo_map = { } -set_xauth_env(xauth_abs_path) os.environ["DISPLAY"] = ":0" log.debug(f"""Env variable 'DISPLAY' set to '{os.environ["DISPLAY"]}'""") +set_xauth_env(xauth_abs_path) log.debug(f"Activating event handlers ...") @@ -200,7 +233,6 @@ 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}'!") -sddm_change_focus_if_two_windows() while True: diff --git a/requirements.in b/requirements.in index 34c7d03..8df23a9 100644 --- a/requirements.in +++ b/requirements.in @@ -1,3 +1,4 @@ cec inotify_simple -rich \ No newline at end of file +rich +inflect \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 6cb91b9..e168969 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,10 @@ colorama==0.4.4 # via rich commonmark==0.9.1 # via rich +inflect==5.4.0 + # via -r requirements.in +inotify-simple==1.3.5 + # via -r requirements.in pygments==2.11.2 # via rich rich==11.2.0