-
Notifications
You must be signed in to change notification settings - Fork 40
/
alternating_layouts.py
executable file
·79 lines (62 loc) · 1.8 KB
/
alternating_layouts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import getopt
import sys
import os
from i3ipc import Connection, Event
def find_parent(i3, window_id):
"""
Find the parent of a given window id
"""
def finder(con, parent):
if con.id == window_id:
return parent
for node in con.nodes:
res = finder(node, con)
if res:
return res
return None
return finder(i3.get_tree(), None)
def set_layout(i3, e):
"""
Set the layout/split for the currently
focused window to either vertical or
horizontal, depending on its width/height
"""
win = i3.get_tree().find_focused()
parent = find_parent(i3, win.id)
if (parent and parent.layout != 'tabbed'
and parent.layout != 'stacked'):
if win.rect.height > win.rect.width:
if parent.orientation == 'horizontal':
i3.command('split v')
else:
if parent.orientation == 'vertical':
i3.command('split h')
def print_help():
print("Usage: " + sys.argv[0] + " [-p path/to/pid.file]")
print("")
print("Options:")
print(" -p path/to/pid.file Saves the PID for this program in the filename specified")
print("")
def main():
"""
Main function - listen for window focus
changes and call set_layout when focus
changes
"""
opt_list, _ = getopt.getopt(sys.argv[1:], 'hp:')
pid_file = None
for opt in opt_list:
if opt[0] == "-h":
print_help()
sys.exit()
if opt[0] == "-p":
pid_file = opt[1]
if pid_file:
with open(pid_file, 'w') as f:
f.write(str(os.getpid()))
i3 = Connection()
i3.on(Event.WINDOW_FOCUS, set_layout)
i3.main()
if __name__ == "__main__":
main()