Compare commits

..

No commits in common. "29e9f39dafe7729cf8729c2625df6d042ef892b4" and "fabb975dd90b5afa8c73f2e7d54fe36ef8a026b2" have entirely different histories.

3 changed files with 39 additions and 76 deletions

108
main.py
View File

@ -6,14 +6,12 @@ import sys
import time import time
import cec import cec
import logging import logging
import inflect
from rich import traceback from rich import traceback
from rich.console import Console from rich.console import Console
from rich.logging import RichHandler from rich.logging import RichHandler
from inotify_simple import INotify, flags from inotify_simple import INotify, flags
traceback.install() traceback.install()
console = Console() console = Console()
p = inflect.engine()
FORMAT = "%(message)s" FORMAT = "%(message)s"
@ -73,29 +71,18 @@ def x_focus_window(window_id: str) -> bool:
def sddm_greeter_window_ids() -> list: def sddm_greeter_window_ids() -> list:
sddm_check_attempt = 1 log.debug(f"Checking if an 'sddm-greeter' window exists ...")
sddm_check_attempts_max = 2 try:
sddm_check_sleep = 2 dm_windows = subprocess.run(
while sddm_check_attempt <= sddm_check_attempts_max: ["xdotool", "search", "--onlyvisible", "--classname", "sddm-greeter"],
log.debug(f"Checking if 'sddm-greeter' window exists " check=True,
f"({sddm_check_attempt} of {sddm_check_attempts_max}) ...") stdout=subprocess.PIPE,
try: encoding="utf-8")
dm_windows = subprocess.run( return dm_windows.stdout.splitlines()
["xdotool", "search", "--onlyvisible", "--classname", "sddm-greeter"], except subprocess.CalledProcessError as xdotool_error:
check=True, log.debug(f"No 'sddm-greeter' window exists, doing nothing")
capture_output=True, log.debug(xdotool_error)
encoding="utf-8") return []
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: def sddm_change_focus_if_two_windows() -> bool:
@ -103,51 +90,32 @@ def sddm_change_focus_if_two_windows() -> bool:
if not dm_windows: if not dm_windows:
return False return False
else: else:
sddm_focus_attempt = 1 log.debug(f"Checking if 'sddm-greeter' window has focus ...")
sddm_focus_attempt_max = 2 try:
sddm_focus_sleep = 2 x_window_focus = subprocess.run(
sddm_window_count_attempt = 1 ["xdotool", "getwindowfocus"],
sddm_window_count_attempt_max = 2 check=True,
while sddm_focus_attempt <= sddm_focus_attempt_max: stdout=subprocess.PIPE,
log.debug(f"Checking if 'sddm-greeter' window has focus " encoding="utf-8")
f"({sddm_focus_attempt} of {sddm_focus_attempt_max}) ...") x_window_id_has_focus = x_window_focus.stdout.splitlines()[0]
try: if x_window_id_has_focus in dm_windows:
x_window_focus = subprocess.run( log.debug(f"An 'sddm-greeter' window has focus")
["xdotool", "getwindowfocus"], if len(dm_windows) > 1:
check=True, if len(dm_windows) == 2:
capture_output=True, log.debug(f"We're assuming we have to focus the other one")
encoding="utf-8") log.debug(f"From list of 'sddm-greeter' window IDs ({dm_windows}) remove the one that has "
x_window_id_has_focus = x_window_focus.stdout.splitlines()[0] f"focus ({x_window_id_has_focus})")
if x_window_id_has_focus in dm_windows: dm_windows.remove(x_window_id_has_focus)
log.debug(f"An 'sddm-greeter' window has focus") x_focus_window(dm_windows[0])
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: else:
log.debug(f"No other 'sddm-greeter' windows exist, checking again ...") log.warning(f"Number of 'sddm-greeter' windows ({len(dm_windows)}) is unexpected, not doing "
sddm_window_count_attempt += 1 f"anything")
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: else:
log.warning(f"Unable to get ID of focused window") log.debug(f"No other 'sddm-greeter' windows exist, no need to focus anything")
log.warning(xdotool_error) except subprocess.CalledProcessError as 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: def propagate_keypress(cec_key_id: int) -> None:
@ -198,7 +166,6 @@ def signal_handler(rx_signal: int, frame) -> None:
def set_xauth_env(file_abs_path: str) -> None: def set_xauth_env(file_abs_path: str) -> None:
os.environ["XAUTHORITY"] = file_abs_path os.environ["XAUTHORITY"] = file_abs_path
log.debug(f"""Env variable 'XAUTHORITY' set to '{os.environ["XAUTHORITY"]}'""") log.debug(f"""Env variable 'XAUTHORITY' set to '{os.environ["XAUTHORITY"]}'""")
sddm_change_focus_if_two_windows()
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
@ -223,9 +190,9 @@ xdo_map = {
} }
set_xauth_env(xauth_abs_path)
os.environ["DISPLAY"] = ":0" os.environ["DISPLAY"] = ":0"
log.debug(f"""Env variable 'DISPLAY' set to '{os.environ["DISPLAY"]}'""") log.debug(f"""Env variable 'DISPLAY' set to '{os.environ["DISPLAY"]}'""")
set_xauth_env(xauth_abs_path)
log.debug(f"Activating event handlers ...") log.debug(f"Activating event handlers ...")
@ -233,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:

View File

@ -1,4 +1,3 @@
cec cec
inotify_simple inotify_simple
rich rich
inflect

View File

@ -10,10 +10,6 @@ colorama==0.4.4
# via rich # via rich
commonmark==0.9.1 commonmark==0.9.1
# via rich # via rich
inflect==5.4.0
# via -r requirements.in
inotify-simple==1.3.5
# via -r requirements.in
pygments==2.11.2 pygments==2.11.2
# via rich # via rich
rich==11.2.0 rich==11.2.0