i3 improvements wip
[distro-setup] / i3-event-hook
diff --git a/i3-event-hook b/i3-event-hook
new file mode 100755 (executable)
index 0000000..01514a6
--- /dev/null
@@ -0,0 +1,61 @@
+#!/usr/bin/env python3
+
+import sys
+import os
+from i3ipc import Connection, Event
+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 focus_hook(i3, e):
+    """
+        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
+
+    # debugging
+    #pprint(vars(e))
+
+    parent, gp = find_parent(i3, e.container.id)
+
+    # This gets rid of tabbed container with single windows.
+    #if (parent and gp and parent.layout == 'tabbed' and len(parent.nodes) == 1):
+    # This gets rid of all single window containers.
+    if (parent and gp and len(parent.nodes) == 1):
+        i3.command('mark i3ha')
+        i3.command('focus parent')
+        i3.command('focus parent')
+        i3.command('mark i3hb')
+        i3.command('[con_mark="i3ha"] focus')
+        i3.command('move window to mark i3hb')
+        i3.command('unmark i3ha')
+        i3.command('unmark i3hb')
+
+def main():
+    i3 = Connection()
+    i3.on(Event.WINDOW_FOCUS, focus_hook)
+    i3.main()
+
+
+if __name__ == "__main__":
+    main()