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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#!/bin/sh
# fibonacci — dwm's dwindle layout, for sway.
#
# Geometry = dwm's dwindle (dwm-fibonacci-6.2.diff): window 1 = left half,
# 2 = top-right, 3 = bottom of 2 (right column keeps going down), 4 = top of
# the following remainder, 5 = bottom of it... each new window shrinks toward
# the bottom-right corner; "look to the bottom right" holds at every birth.
#
# Sway only guarantees a new window lands *next to the focused* window.
# On each new tiled window this listener:
# 1. finds the corner leaf (deepest last-child leaf = the previous window),
# 2. re-homes the newborn beside it (`move container to mark`) when sway
# gave birth beside an older pane instead — the corner already sits in
# its open birth-cell, whose layout was set by the last toggle, so the
# plain move inserts as the second child with the right direction,
# 3. wraps the newborn in a singleton cell with the direction opposite its
# housing (`split toggle`) so the NEXT window slides into that cell —
# that is what makes the dwindle pattern self-perpetuating.
#
# Floating windows and tabbed/stacked containers are left alone entirely.
# Cost: one jq spawn per window event, zero while idle. `--monitor` keeps
# the subscription streaming (without it swaymsg exits after one event, and
# births in the reconnect gap would be lost); the flock keeps sway reloads
# from stacking duplicate listeners (singleton per sway socket).
[ -n "$SWAYSOCK" ] || exit 1
LOCK="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}/fibonacci-$(basename "$SWAYSOCK").lock"
exec 9>"$LOCK"
flock -n 9 || exit 0
JQ='
def has($i): any(.. | objects; .id == $i);
def leaf:
if . == null then null
elif (.nodes | length) == 0 then .id
elif (.nodes[-1].layout == "tabbed" or .nodes[-1].layout == "stacked") then null
else .nodes[-1] | leaf end;
def housing($i): # immediate parent container node of node $i
([(.. | objects | select(any(.nodes[]?; .id == $i)))] | last);
def scan($w; $m): # $w inside a tabbed/stacked container?
. as $node
| if .id == $w then $m
elif (.nodes | length) == 0 then empty
else
($m or .layout == "tabbed" or .layout == "stacked") as $m2
| .nodes[] | scan($w; $m2)
end;
. as $r
| ([$r | scan($w; false)] | any(. == true)) as $manual
| (first(.. | objects | select(.type == "workspace" and has($w)))) as $ws
| ([$ws.nodes[] | select(.id != $w)] | last | leaf) as $corner
| if $ws == null or $manual then "skip"
else
(housing($w)) as $hnW
| if $corner == null then "none"
elif $corner == $w then "self"
else
(housing($corner)) as $hnC
| if $hnW == $hnC then "home"
else "away|\($corner)" end
end
end
'
on_new() {
# $1 = con id of the newborn window
tree=$(swaymsg --raw --type get_tree) || return
info=$(printf %s "$tree" | jq -r --argjson w "$1" "$JQ") || return
mode=${info%%|*}
corner=$(printf %s "$info" | cut -d'|' -f2)
case $mode in
skip|self|none) ;; # nothing to re-home
home) ;; # newborn already beside the
# corner; nothing to move
away) # alien housing: the corner
# already sits in its open
# birth-cell, so a plain
# mark-move inserts beside it
swaymsg --quiet "[con_id=$corner] mark fibonacci-corner"
swaymsg --quiet "[con_id=$1] move container to mark fibonacci-corner"
swaymsg --quiet "[con_id=$corner] unmark" ;;
esac
# wrap the newborn so the NEXT birth lands in this corner cell
[ "$mode" != skip ] && swaymsg --quiet "[con_id=$1] split toggle"
}
# Wrap the subscription in a self-healing loop: a missed sway reload or IPC
# hiccup drops the stream; retry rather than dying.
while :; do
swaymsg --monitor --raw --type subscribe '["window"]' | while IFS= read -r event; do
# only leaf con births; wrapper containers also emit "new" events
kind=$(printf %s "$event" | jq -r '.change + "/" + (if (.container.nodes // []) | length == 0 then "leaf" else "wrap" end) + "/" + (.container.type // "?")')
case $kind in
new/leaf/con) on_new "$(printf %s "$event" | jq '.container.id')" ;;
esac
done
sleep 0.5
done
|