+# * Note: doing it just before a window is created also leaves that split behind if
+# the window is closed, and I don't want single window splits hanging around,
+# so I close them out in
+#
+# I have a keybind which disables both, super+shift+u, it runs
+# /b/ds/i3-auto-layout-toggle
+#
+
+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)
+
+
+ workspace = win.workspace()
+ #pprint(vars(workspace.rect))
+
+ screen_w = workspace.rect.width
+ screen_h = workspace.rect.height
+ half_w = screen_w / 2 + 1
+ half_h = screen_h / 2 + 1