Files
dotfiles/config/sway/scripts/lib-outputs.sh
T
owen f1275e7870 Fix multi-monitor workspace routing regression, add laptop auto-detect
- Restore desktop DP-3=primary/DP-2=secondary workspace mapping that
  broke when workspace routing switched from name-matching to raw
  geometric ordering.
- Extract shared laptop/desktop detection + output ranking into
  lib-outputs.sh (battery presence selects the profile automatically),
  sourced by both select-workspace.sh and the new apply-outputs.sh, so
  keybind routing and monitor placement can never disagree.
- Add apply-outputs.sh: on the laptop, automatically positions whatever
  external display is plugged in (and re-runs on hotplug via swaymsg
  -t subscribe), making it primary. No-op on the desktop, which keeps
  its static fixed-arrangement config.
2026-08-21 23:08:01 -05:00

56 lines
1.9 KiB
Bash

# Shared helpers for arranging outputs and routing workspaces across both
# machines: the laptop (whatever external display happens to be plugged
# in, if any) and the desktop (a fixed DP-3/DP-2 pair). Sourced by
# apply-outputs.sh and select-workspace.sh so both always agree on which
# output is "primary" and in what order the rest follow.
is_laptop() {
local supply
for supply in /sys/class/power_supply/*/; do
[[ -r "${supply}type" ]] || continue
[[ "$(<"${supply}type")" == Battery ]] && return 0
done
return 1
}
is_internal_panel() {
case "$1" in
eDP-*|LVDS-*|DSI-*) return 0 ;;
*) return 1 ;;
esac
}
# Prints the names of currently active outputs, one per line, ordered
# primary-first:
# - laptop: any external display(s), in the order sway reports them,
# then the internal panel last (so a docked external is
# always primary; undocked, the panel is the only entry).
# - desktop: DP-3, then DP-2, then anything else alphabetically as a
# fallback for an unexpected output.
output_order() {
local outputs_json name
outputs_json=$(swaymsg -t get_outputs -r)
mapfile -t active < <(jq -r '.[] | select(.active) | .name' <<<"$outputs_json")
local ordered=()
if is_laptop; then
local externals=() internals=()
for name in "${active[@]}"; do
if is_internal_panel "$name"; then internals+=("$name"); else externals+=("$name"); fi
done
ordered=("${externals[@]}" "${internals[@]}")
else
local preferred=(DP-3 DP-2) p seen
for p in "${preferred[@]}"; do
for name in "${active[@]}"; do [[ "$name" == "$p" ]] && ordered+=("$name"); done
done
for name in "${active[@]}"; do
seen=false
local o
for o in "${ordered[@]}"; do [[ "$o" == "$name" ]] && seen=true; done
"$seen" || ordered+=("$name")
done
fi
printf '%s\n' "${ordered[@]}"
}