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