From c1d36d631d43b687bdff2df56fbf19cbad3a630c Mon Sep 17 00:00:00 2001 From: Richard Abrich Date: Sun, 29 Dec 2024 17:48:05 -0500 Subject: [PATCH] global X server connection --- openadapt/window/_linux.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/openadapt/window/_linux.py b/openadapt/window/_linux.py index 6f7be6e26..5ffb21307 100644 --- a/openadapt/window/_linux.py +++ b/openadapt/window/_linux.py @@ -5,16 +5,30 @@ from openadapt.custom_logger import logger +# Global X server connection +_conn = None + + +def get_x_server_connection() -> xcffib.Connection: + """Get or create a global connection to the X server. + + Returns: + xcffib.Connection: A global connection object. + """ + global _conn + if _conn is None: + _conn = xcffib.connect() + return _conn + def get_active_window_meta() -> dict | None: - """Retrieve metadata of the active window using X server directly. + """Retrieve metadata of the active window using a persistent X server connection. Returns: dict or None: A dictionary containing metadata of the active window. """ try: - # Connect to the X server - conn = xcffib.connect() + conn = get_x_server_connection() root = conn.get_setup().roots[0].root # Get the _NET_ACTIVE_WINDOW atom