#!/usr/bin/python3 import sys 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): """ 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) # We never want to set the layout of a single window container, # there are already keys for that. I don't know why i3 even does # this, it is stupid. So, eliminate single window container if we # are focused on one. # # Alternatively, it could first focus the parent, but I think when # layout changes, we expect new windows to be created within that # layout. if (parent and len(parent.nodes) == 1): gp = find_parent(i3, parent.id) if (gp.nodes[0].id == parent.id): if (gp.layout == 'splitv'): i3.command('move down') else: # splith or tabbed i3.command('move right') else: if (gp.layout == 'splitv'): i3.command('move up') else: i3.command('move left') i3.command('layout ' + sys.argv[1]) def main(): i3 = Connection() set_layout(i3) if __name__ == "__main__": main()