From af723025b3e5efce694d38511fe6bc70b3bdcc47 Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sat, 24 Feb 2018 13:39:19 +1100 Subject: [PATCH] xfdesktop-focus-fix --- xfdesktop-focus-fix | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 xfdesktop-focus-fix diff --git a/xfdesktop-focus-fix b/xfdesktop-focus-fix new file mode 100755 index 0000000..757b562 --- /dev/null +++ b/xfdesktop-focus-fix @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import subprocess +import time +import Xlib +import Xlib.display + +display = Xlib.display.Display() +root = display.screen().root +NET_ACTIVE_WINDOW = display.intern_atom('_NET_ACTIVE_WINDOW') + +def main(): + root.change_attributes(event_mask=Xlib.X.PropertyChangeMask) + + handle_active_window_change() + + while True: + event = display.next_event() + if is_active_window_change(event): + handle_active_window_change() + +def is_active_window_change(event): + return ( + event.type == Xlib.X.PropertyNotify and + event.atom == NET_ACTIVE_WINDOW + ) + +def handle_active_window_change(): + window_id = get_active_window_id() + if not window_id: + focus_xfdesktop() + +def get_active_window_id(): + prop = root.get_full_property(NET_ACTIVE_WINDOW, Xlib.X.AnyPropertyType) + if prop and prop.value: + return prop.value[0] + +def try_with_sleep(fn): + def wrapper(*args, **kwargs): + count = 0 + while True: + try: + return fn(*args, **kwargs) + except: + if count > 5: + print("Failed finally") + raise + print("Failed, will retry") + time.sleep(1) + count += 1 + return wrapper + +@try_with_sleep +def focus_xfdesktop(): + print("Focusing xfdesktop") + subprocess.check_output([ + 'xdotool', + 'search', + '--onlyvisible', + '--class', 'xfdesktop', + 'windowfocus', + ]) + +if __name__ == '__main__': + main() -- 2.39.2