+
+import sys
+import os
+from i3ipc import Connection, Event
+# for debugging
+#from pprint import pprint
+
+
+def find_parent(i3, window_id):
+ """
+ Find the parent of a given window id
+ """
+
+ def finder(con, parent, gp):
+ if con.id == window_id:
+ return (parent, gp)
+ for node in con.nodes:
+ res = finder(node, con, parent)
+ if res:
+ return res
+ return None
+
+ return finder(i3.get_tree(), None, None)
+
+
+def set_layout(i3):
+ """
+ Set the layout/split for the currently
+ focused window to either vertical or
+ horizontal, depending on its width/height
+ """
+
+ if os.path.isfile("/tmp/iank-i3-no-auto"):
+ return
+
+ win = i3.get_tree().find_focused()
+ parent, gp = find_parent(i3, win.id)