]> code.delx.au - mediapc-tools/commitdiff
xfdesktop-focus-fix: simplified
authorJames Bunton <jamesbunton@delx.net.au>
Sat, 3 Mar 2018 10:24:33 +0000 (21:24 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Sat, 3 Mar 2018 10:24:33 +0000 (21:24 +1100)
xfdesktop-focus-fix

index b620932c19aad46b8375b6a3d573453fcb91cda3..3517158dfdb5e873201869a71d0327188e835f3f 100755 (executable)
@@ -1,72 +1,12 @@
-#!/usr/bin/env python3
-
-import os
-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():
-    if is_running_media_wrap():
-        return
-
-    window_id = get_active_window_id()
-    if not window_id:
-        focus_xfdesktop()
-
-def is_running_media_wrap():
-    return os.path.isfile(os.path.expanduser("~/.mediawrap.lock"))
-
-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()
+#!/bin/bash
+
+function has_active_window {
+    xprop -root -f _NET_ACTIVE_WINDOW 32c ' $0\n' _NET_ACTIVE_WINDOW | awk '{ exit $2 == 0 }'
+}
+
+while true; do
+    if ! has_active_window; then
+        xdotool search --onlyvisible --class xfdesktop windowfocus
+    fi
+    sleep 5
+done &> /dev/null