# 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[@]}" }