]> code.delx.au - mediapc-tools/blob - xfdesktop-focus-fix
xfdesktop-focus-fix
[mediapc-tools] / xfdesktop-focus-fix
1 #!/usr/bin/env python3
2
3 import subprocess
4 import time
5 import Xlib
6 import Xlib.display
7
8 display = Xlib.display.Display()
9 root = display.screen().root
10 NET_ACTIVE_WINDOW = display.intern_atom('_NET_ACTIVE_WINDOW')
11
12 def main():
13 root.change_attributes(event_mask=Xlib.X.PropertyChangeMask)
14
15 handle_active_window_change()
16
17 while True:
18 event = display.next_event()
19 if is_active_window_change(event):
20 handle_active_window_change()
21
22 def is_active_window_change(event):
23 return (
24 event.type == Xlib.X.PropertyNotify and
25 event.atom == NET_ACTIVE_WINDOW
26 )
27
28 def handle_active_window_change():
29 window_id = get_active_window_id()
30 if not window_id:
31 focus_xfdesktop()
32
33 def get_active_window_id():
34 prop = root.get_full_property(NET_ACTIVE_WINDOW, Xlib.X.AnyPropertyType)
35 if prop and prop.value:
36 return prop.value[0]
37
38 def try_with_sleep(fn):
39 def wrapper(*args, **kwargs):
40 count = 0
41 while True:
42 try:
43 return fn(*args, **kwargs)
44 except:
45 if count > 5:
46 print("Failed finally")
47 raise
48 print("Failed, will retry")
49 time.sleep(1)
50 count += 1
51 return wrapper
52
53 @try_with_sleep
54 def focus_xfdesktop():
55 print("Focusing xfdesktop")
56 subprocess.check_output([
57 'xdotool',
58 'search',
59 '--onlyvisible',
60 '--class', 'xfdesktop',
61 'windowfocus',
62 ])
63
64 if __name__ == '__main__':
65 main()