Reset to workspace 1 after monitor wake
Browse files at this commit0ce4537ae8ad684025c22c49f58f7a71bc1ede2dParent: 8d1fbfc- fix
- reset to workspace 1 after monitor wake
2 changed files +26 -48
.config/scripts/niri/show_desktop.shmodified+6 -48
| @@ -1,52 +1,10 @@ | ||
| 1 | 1 | #!/bin/bash |
| 2 | 2 | set -euo pipefail |
| 3 | 3 | |
| 4 | -STATE_FILE="/tmp/niri_show_desktop" | |
| 4 | +source "$(dirname "$0")/monitors.sh" | |
| 5 | 5 | |
| 6 | -ws_json=$(niri msg -j workspaces) | |
| 7 | - | |
| 8 | -ref_of() { | |
| 9 | - jq -r 'if .name != null then .name else (.idx | tostring) end' <<<"$1" | |
| 10 | -} | |
| 11 | - | |
| 12 | -if [ -f "$STATE_FILE" ]; then | |
| 13 | - while IFS=$'\t' read -r out ref; do | |
| 14 | - [ -n "$out" ] || continue | |
| 15 | - niri msg action focus-monitor "$out" >/dev/null | |
| 16 | - niri msg action focus-workspace "$ref" >/dev/null | |
| 17 | - done < <(grep -v '^FOCUSED' "$STATE_FILE") | |
| 18 | - | |
| 19 | - focused_out=$(grep '^FOCUSED' "$STATE_FILE" | cut -f2) | |
| 20 | - [ -n "$focused_out" ] && niri msg action focus-monitor "$focused_out" >/dev/null | |
| 21 | - | |
| 22 | - rm -f "$STATE_FILE" | |
| 23 | -else | |
| 24 | - win_json=$(niri msg -j windows) | |
| 25 | - focused_out=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") | |
| 26 | - | |
| 27 | - : > "$STATE_FILE" | |
| 28 | - printf 'FOCUSED\t%s\n' "$focused_out" >> "$STATE_FILE" | |
| 29 | - | |
| 30 | - while read -r out; do | |
| 31 | - active=$(jq -c --arg o "$out" '.[] | select(.output == $o and .is_active == true)' <<<"$ws_json") | |
| 32 | - printf '%s\t%s\n' "$out" "$(ref_of "$active")" >> "$STATE_FILE" | |
| 33 | - done < <(jq -r '[.[].output] | unique | .[]' <<<"$ws_json") | |
| 34 | - | |
| 35 | - while read -r out; do | |
| 36 | - empty_idx=$(jq -r --arg o "$out" --argjson w "$win_json" ' | |
| 37 | - [ .[] | |
| 38 | - | select(.output == $o and .name == null) | |
| 39 | - | . as $ws | |
| 40 | - | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) | |
| 41 | - ] | last | .idx // empty' <<<"$ws_json") | |
| 42 | - | |
| 43 | - if [ -z "$empty_idx" ]; then | |
| 44 | - empty_idx=$(jq -r --arg o "$out" '[.[] | select(.output == $o) | .idx] | max' <<<"$ws_json") | |
| 45 | - fi | |
| 46 | - | |
| 47 | - niri msg action focus-monitor "$out" >/dev/null | |
| 48 | - niri msg action focus-workspace "$empty_idx" >/dev/null | |
| 49 | - done < <(jq -r '[.[].output] | unique | .[]' <<<"$ws_json") | |
| 50 | - | |
| 51 | - niri msg action focus-monitor "$focused_out" >/dev/null | |
| 52 | -fi | |
| 6 | +# workspace 1 is the empty-workspace-above-first slot, so it is always free | |
| 7 | +niri msg action focus-monitor "$RIGHT_MONITOR" >/dev/null | |
| 8 | +niri msg action focus-workspace 1 >/dev/null | |
| 9 | +niri msg action focus-monitor "$LEFT_MONITOR" >/dev/null | |
| 10 | +niri msg action focus-workspace 1 >/dev/null |
.config/scripts/niri/toggle_monitors.shmodified+20 -0
| @@ -2,12 +2,32 @@ | ||
| 2 | 2 | |
| 3 | 3 | source "$(dirname "$0")/monitors.sh" |
| 4 | 4 | |
| 5 | +# DPMS off makes the display drop its link, so niri sees a disconnect and | |
| 6 | +# destroys that output's empty workspaces. On reconnect the active workspace | |
| 7 | +# falls back to the first surviving one. Just put every output back on its | |
| 8 | +# first workspace once the outputs return. | |
| 9 | +reset_workspaces() { | |
| 10 | + for _ in $(seq 1 600); do | |
| 11 | + niri msg -j workspaces 2>/dev/null | | |
| 12 | + jq -e --arg l "$LEFT_MONITOR" --arg r "$RIGHT_MONITOR" \ | |
| 13 | + 'any(.[]; .output == $l) and any(.[]; .output == $r)' >/dev/null && break | |
| 14 | + sleep 1 | |
| 15 | + done | |
| 16 | + sleep 1 | |
| 17 | + "$(dirname "$0")/show_desktop.sh" >/dev/null 2>&1 | |
| 18 | +} | |
| 19 | + | |
| 5 | 20 | case "${1:-menu}" in |
| 21 | + --reset) | |
| 22 | + reset_workspaces | |
| 23 | + exit 0 | |
| 24 | + ;; | |
| 6 | 25 | menu) |
| 7 | 26 | menu=$(echo -e "🖥️ All Monitors Off\n🖥️ $LEFT_MONITOR Off\n🖥️ $RIGHT_MONITOR Off\n💡 All Monitors On\n💡 $LEFT_MONITOR On\n💡 $RIGHT_MONITOR On\n❌ Cancel" | rofi -dmenu -p "Monitors" -i) |
| 8 | 27 | |
| 9 | 28 | case "$menu" in |
| 10 | 29 | "🖥️ All Monitors Off") |
| 30 | + setsid nohup "$0" --reset >/dev/null 2>&1 & | |
| 11 | 31 | niri msg action power-off-monitors |
| 12 | 32 | ;; |
| 13 | 33 | "🖥️ $LEFT_MONITOR Off") |