01514a648d6017aa0b2f5bf58f9c6b38433946d7
[distro-setup] / i3-event-hook
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 from i3ipc import Connection, Event
6 from pprint import pprint
7
8
9 def find_parent(i3, window_id):
10 """
11 Find the parent of a given window id
12 """
13
14 def finder(con, parent, gp):
15 if con.id == window_id:
16 return (parent, gp)
17 for node in con.nodes:
18 res = finder(node, con, parent)
19 if res:
20 return res
21 return None
22
23 return finder(i3.get_tree(), None, None)
24
25
26 def focus_hook(i3, e):
27 """
28 Set the layout/split for the currently
29 focused window to either vertical or
30 horizontal, depending on its width/height
31 """
32
33 if os.path.isfile("/tmp/iank-i3-no-auto"):
34 return
35
36 # debugging
37 #pprint(vars(e))
38
39 parent, gp = find_parent(i3, e.container.id)
40
41 # This gets rid of tabbed container with single windows.
42 #if (parent and gp and parent.layout == 'tabbed' and len(parent.nodes) == 1):
43 # This gets rid of all single window containers.
44 if (parent and gp and len(parent.nodes) == 1):
45 i3.command('mark i3ha')
46 i3.command('focus parent')
47 i3.command('focus parent')
48 i3.command('mark i3hb')
49 i3.command('[con_mark="i3ha"] focus')
50 i3.command('move window to mark i3hb')
51 i3.command('unmark i3ha')
52 i3.command('unmark i3hb')
53
54 def main():
55 i3 = Connection()
56 i3.on(Event.WINDOW_FOCUS, focus_hook)
57 i3.main()
58
59
60 if __name__ == "__main__":
61 main()