From 11e2f004df02be7aa854feab61af74dc546a9fe6 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sun, 9 Jun 2024 15:15:13 +0200 Subject: [PATCH] Got it working. --- nwnfaf.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 nwnfaf.py diff --git a/nwnfaf.py b/nwnfaf.py new file mode 100644 index 0000000..01abca5 --- /dev/null +++ b/nwnfaf.py @@ -0,0 +1,59 @@ +#!/usr/bin/python3 + +from Xlib import X, display, Xatom +import subprocess +import time +import Xlib.error + +# Establish a connection to the X server +disp = display.Display() +root = disp.screen().root + +# Set the mask to receive events +root.change_attributes(event_mask=X.SubstructureNotifyMask | X.StructureNotifyMask) + +# Retrieve the Atom for _NET_WM_WINDOW_TYPE +NET_WM_WINDOW_TYPE = disp.intern_atom('_NET_WM_WINDOW_TYPE') +NET_WM_WINDOW_TYPE_NORMAL = disp.intern_atom('_NET_WM_WINDOW_TYPE_NORMAL') + + +def is_main_window(window): + try: + # Query the _NET_WM_WINDOW_TYPE attribute + window_type = window.get_full_property(NET_WM_WINDOW_TYPE, Xatom.ATOM) + if window_type and NET_WM_WINDOW_TYPE_NORMAL in window_type.value: + return True + + except Xlib.error.XError: + pass + + return False + + +def focus_window(window): + try: + # Use xdotool to focus the window + subprocess.run(['xdotool', 'windowactivate', str(window.id)], check=True) + + except subprocess.CalledProcessError as e: + print(f"Error focusing window {window.id}: {e}") + + +while True: + event = root.display.next_event() + + if event.type == X.CreateNotify: + window = event.window + # Wait a short time to ensure the window is initialized + time.sleep(0.05) + + if is_main_window(window): + focus_window(window) + + elif event.type == X.MapNotify: + window = event.window + # Wait a short time to ensure the window is initialized + time.sleep(0.05) + + if is_main_window(window): + focus_window(window) \ No newline at end of file