Merge pull request 'feature/1-focus-greeter-when-taking-control' (#2) from feature/1-focus-greeter-when-taking-control into master

Reviewed-on: #2
This commit is contained in:
hygienic-books 2022-03-07 23:40:51 +00:00
commit fabb975dd9

66
main.py
View File

@ -21,6 +21,7 @@ logging.basicConfig(
)] )]
) )
log = logging.getLogger("rich") log = logging.getLogger("rich")
log.setLevel("DEBUG")
# Vars # Vars
@ -53,6 +54,70 @@ log.debug(f"Printing environment vars ...")
log.debug(os.environ) 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 sddm_change_focus_if_two_windows() -> 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: 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}')""") 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) 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) cec.add_callback(log_handler, cec.EVENT_LOG)
log.debug(f"Event handlers active") log.debug(f"Event handlers active")
log.info(f"Open for business on adapter '{use_adapter}'!") log.info(f"Open for business on adapter '{use_adapter}'!")
sddm_change_focus_if_two_windows()
while True: while True: