Declare app workspaces, drop reaper
Browse files at this commit09f97244dff32482d4b49cc0a8ae0ba85e495a0eParent: 15197df- refactor
- declare app workspaces, drop reaper
4 changed files +65 -164
- .config/niri/config.kdl +28 -10
- .config/scripts/niri/app_workspaces.sh +0 -21
- .config/scripts/niri/workspace_app.sh +37 -39
- .config/scripts/niri/workspace_reaper.sh +0 -94
.config/niri/config.kdlmodified+28 -10
| @@ -126,7 +126,6 @@ environment { | |||
| 126 | // starts them. Hyprland ignored that target, which is why it needed exec-once. | 126 | // starts them. Hyprland ignored that target, which is why it needed exec-once. |
| 127 | // The sleeps preserve Hyprland's ordering (tray applets after waybar). | 127 | // The sleeps preserve Hyprland's ordering (tray applets after waybar). |
| 128 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/wallpaper_restore.sh" | 128 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/wallpaper_restore.sh" |
| 129 | -spawn-at-startup "sh" "-c" "bash ~/.config/scripts/niri/workspace_reaper.sh" | ||
| 130 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/shared/song_detail_writer.sh" | 129 | spawn-at-startup "sh" "-c" "bash ~/.config/scripts/shared/song_detail_writer.sh" |
| 131 | // waybar needs its own config here: the shared one uses hyprland/workspaces | 130 | // waybar needs its own config here: the shared one uses hyprland/workspaces |
| 132 | // and hyprland/window, which do nothing under niri. config-niri swaps those | 131 | // and hyprland/window, which do nothing under niri. config-niri swaps those |
| @@ -167,11 +166,28 @@ window-rule { | |||
| 167 | open-floating true | 166 | open-floating true |
| 168 | } | 167 | } |
| 169 | 168 | ||
| 170 | -// The app workspaces (code, github, ...) are deliberately NOT declared here. | 169 | +// The app workspaces. A declared `workspace "name"` node is immortal — which is |
| 171 | -// A declared `workspace "name"` node is immortal, which left eight empty | 170 | +// exactly the point: nothing has to create, name or clean them up, so there is |
| 172 | -// workspaces lying around. workspace_app.sh names a fresh workspace when it | 171 | +// no reaper daemon and no marker files. They were briefly named at runtime |
| 173 | -// launches the app, and workspace_reaper.sh un-names it once it empties so niri | 172 | +// instead, and the reaper that un-named them died mid-session (niri dropped its |
| 174 | -// reaps it. Names live in ~/.config/scripts/niri/app_workspaces.sh. | 173 | +// IPC event stream for "reading events too slowly") leaving empty named |
| 174 | +// workspaces behind — the failure this replaces. | ||
| 175 | +// | ||
| 176 | +// The empties are only visible in the overview: workspace_nav.sh filters on | ||
| 177 | +// `.name == null`, so number and directional navigation skip them, and waybar | ||
| 178 | +// runs niri/workspaces with current-only. | ||
| 179 | +// | ||
| 180 | +// No open-on-output on purpose — KDL has no variables and monitor names are kept | ||
| 181 | +// to the `output` blocks and the monitor binds. They start on the first output | ||
| 182 | +// and workspace_app.sh pulls an empty one to the monitor in use on launch. | ||
| 183 | +workspace "code" | ||
| 184 | +workspace "github" | ||
| 185 | +workspace "whatsapp" | ||
| 186 | +workspace "discord" | ||
| 187 | +workspace "deezer" | ||
| 188 | +workspace "youtube" | ||
| 189 | +workspace "mail" | ||
| 190 | +workspace "signal" | ||
| 175 | 191 | ||
| 176 | binds { | 192 | binds { |
| 177 | // ─── launchers (Alt trigger, Hyprland $my_trigger) ─────────────────────── | 193 | // ─── launchers (Alt trigger, Hyprland $my_trigger) ─────────────────────── |
| @@ -193,13 +209,15 @@ binds { | |||
| 193 | 209 | ||
| 194 | // ─── app workspaces ───────────────────────────────────────────────────── | 210 | // ─── app workspaces ───────────────────────────────────────────────────── |
| 195 | // workspace_app.sh emulates Hyprland's on-created-empty, which niri lacks. | 211 | // workspace_app.sh emulates Hyprland's on-created-empty, which niri lacks. |
| 196 | - // An empty workspace moves to the monitor you are on and the app launches | 212 | + // The workspaces are declared above, so "is the app running" is just "does |
| 197 | - // there; once it holds windows the key only moves focus to it. webapp.sh | 213 | + // its workspace hold a window". Empty -> the workspace moves to the monitor |
| 198 | - // builds the Chromium web-app command from a URL + profile id. | 214 | + // you are on and the app launches there; holding windows -> the key only |
| 215 | + // moves focus to it, leaving it on its own monitor. webapp.sh builds the | ||
| 216 | + // Chromium web-app command from a URL + profile id. | ||
| 199 | Alt+C hotkey-overlay-title="Workspace: code" { spawn-sh "~/.config/scripts/niri/workspace_app.sh code zed"; } | 217 | Alt+C hotkey-overlay-title="Workspace: code" { spawn-sh "~/.config/scripts/niri/workspace_app.sh code zed"; } |
| 200 | Alt+G hotkey-overlay-title="Workspace: github" { spawn-sh "~/.config/scripts/niri/workspace_app.sh github ~/.config/scripts/shared/webapp.sh https://github.com/ FCF18Wmt"; } | 218 | Alt+G hotkey-overlay-title="Workspace: github" { spawn-sh "~/.config/scripts/niri/workspace_app.sh github ~/.config/scripts/shared/webapp.sh https://github.com/ FCF18Wmt"; } |
| 201 | Alt+W hotkey-overlay-title="Workspace: whatsapp" { spawn-sh "~/.config/scripts/niri/workspace_app.sh whatsapp ~/.config/scripts/shared/webapp.sh --mullvad https://web.whatsapp.com/ aW2IMaZ8"; } | 219 | Alt+W hotkey-overlay-title="Workspace: whatsapp" { spawn-sh "~/.config/scripts/niri/workspace_app.sh whatsapp ~/.config/scripts/shared/webapp.sh --mullvad https://web.whatsapp.com/ aW2IMaZ8"; } |
| 202 | - Alt+D hotkey-overlay-title="Workspace: discord" { spawn-sh "~/.config/scripts/niri/workspace_app.sh discord flatpak run dev.vencord.Vesktop"; } | 220 | + Alt+D hotkey-overlay-title="Workspace: discord" { spawn-sh "~/.config/scripts/niri/workspace_app.sh discord flatpak run org.equicord.equibop"; } |
| 203 | Alt+S hotkey-overlay-title="Workspace: deezer" { spawn-sh "~/.config/scripts/niri/workspace_app.sh deezer ~/.config/scripts/shared/webapp.sh --mullvad https://deezer.com/ VjW8vTQ3"; } | 221 | Alt+S hotkey-overlay-title="Workspace: deezer" { spawn-sh "~/.config/scripts/niri/workspace_app.sh deezer ~/.config/scripts/shared/webapp.sh --mullvad https://deezer.com/ VjW8vTQ3"; } |
| 204 | Alt+Y hotkey-overlay-title="Workspace: youtube" { spawn-sh "~/.config/scripts/niri/workspace_app.sh youtube mullvad-exclude flatpak run io.freetubeapp.FreeTube"; } | 222 | Alt+Y hotkey-overlay-title="Workspace: youtube" { spawn-sh "~/.config/scripts/niri/workspace_app.sh youtube mullvad-exclude flatpak run io.freetubeapp.FreeTube"; } |
| 205 | Alt+P hotkey-overlay-title="Workspace: mail" { spawn-sh "~/.config/scripts/niri/workspace_app.sh mail ~/.config/scripts/shared/webapp.sh https://mail.proton.me/ eIpzVtL8"; } | 223 | Alt+P hotkey-overlay-title="Workspace: mail" { spawn-sh "~/.config/scripts/niri/workspace_app.sh mail ~/.config/scripts/shared/webapp.sh https://mail.proton.me/ eIpzVtL8"; } |
.config/scripts/niri/app_workspaces.shdeleted+0 -21
| @@ -1,21 +0,0 @@ | |||
| 1 | -# Names of the app workspaces, sourced by the niri workspace scripts. | ||
| 2 | -# Not executable on purpose. | ||
| 3 | -# | ||
| 4 | -# These are NOT declared in config.kdl: a declared `workspace "name"` node makes | ||
| 5 | -# niri keep that workspace alive forever, which left eight empty workspaces | ||
| 6 | -# sitting around. Instead workspace_app.sh names a fresh workspace on launch and | ||
| 7 | -# workspace_reaper.sh un-names it once it empties, letting niri reap it. | ||
| 8 | -# | ||
| 9 | -# Only names in this list are ever un-named, so a workspace you name by hand is | ||
| 10 | -# left alone. | ||
| 11 | - | ||
| 12 | -APP_WORKSPACES=(code github whatsapp discord deezer youtube mail signal) | ||
| 13 | - | ||
| 14 | -# True if $1 is one of the app workspaces. | ||
| 15 | -is_app_workspace() { | ||
| 16 | - local candidate="$1" name | ||
| 17 | - for name in "${APP_WORKSPACES[@]}"; do | ||
| 18 | - [ "$name" = "$candidate" ] && return 0 | ||
| 19 | - done | ||
| 20 | - return 1 | ||
| 21 | -} | ||
.config/scripts/niri/workspace_app.shmodified+37 -39
| @@ -1,24 +1,23 @@ | |||
| 1 | #!/usr/bin/env bash | 1 | #!/usr/bin/env bash |
| 2 | -# Emulates Hyprland's `workspace = N, on-created-empty: <cmd>` for niri. | 2 | +# Focus an app workspace, launching the app the first time. |
| 3 | # | 3 | # |
| 4 | # usage: workspace_app.sh <workspace-name> <command> [args...] | 4 | # usage: workspace_app.sh <workspace-name> <command> [args...] |
| 5 | # workspace_app.sh --move <workspace-name> | 5 | # workspace_app.sh --move <workspace-name> |
| 6 | # | 6 | # |
| 7 | # Behaviour: | 7 | # Behaviour: |
| 8 | -# * workspace already exists (app is running) -> just focus it. It stays on the | 8 | +# * workspace holds windows (app is running) -> just focus it. It stays on the |
| 9 | # monitor it was launched on; the key moves FOCUS there rather than dragging | 9 | # monitor it was launched on; the key moves FOCUS there rather than dragging |
| 10 | # the workspace to the monitor you happen to be on. | 10 | # the workspace to the monitor you happen to be on. |
| 11 | -# * workspace does not exist -> take the trailing empty workspace of the | 11 | +# * workspace is empty (app not running) -> pull it to the focused monitor, |
| 12 | -# focused monitor, name it, and launch the app there. | 12 | +# focus it, and launch the app there. |
| 13 | # | 13 | # |
| 14 | -# The workspaces are deliberately NOT declared in config.kdl: a declared | 14 | +# The workspaces are DECLARED in config.kdl, so they always exist and niri never |
| 15 | -# `workspace "name"` node is immortal, so eight empty workspaces would always be | 15 | +# reaps them. That is what removed the naming step, the grace marker files and |
| 16 | -# present. Naming happens here and workspace_reaper.sh un-names an app workspace | 16 | +# workspace_reaper.sh: with a permanent workspace, "is the app running" is just |
| 17 | -# once it empties, after which niri reaps it. | 17 | +# "does its workspace hold a window", which is one stateless query per keypress |
| 18 | +# instead of a daemon following the IPC event stream. | ||
| 18 | set -euo pipefail | 19 | set -euo pipefail |
| 19 | 20 | ||
| 20 | -. "$(dirname "$(realpath "$0")")/app_workspaces.sh" | ||
| 21 | - | ||
| 22 | mode="focus" | 21 | mode="focus" |
| 23 | if [ "${1:-}" = "--move" ]; then | 22 | if [ "${1:-}" = "--move" ]; then |
| 24 | mode="move" | 23 | mode="move" |
| @@ -29,47 +28,46 @@ ws="${1:?usage: workspace_app.sh [--move] <workspace-name> [command...]}" | |||
| 29 | shift || true | 28 | shift || true |
| 30 | 29 | ||
| 31 | ws_json=$(niri msg -j workspaces) | 30 | ws_json=$(niri msg -j workspaces) |
| 32 | -exists=$(jq -r --arg w "$ws" '[.[] | select(.name == $w)] | length' <<<"$ws_json") | ||
| 33 | - | ||
| 34 | -if [ "$exists" = "0" ]; then | ||
| 35 | - # Claim the last empty unnamed workspace on this monitor. niri always keeps | ||
| 36 | - # one at the end of every output, so there is something to claim. | ||
| 37 | - output=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") | ||
| 38 | - win_json=$(niri msg -j windows) | ||
| 39 | 31 | ||
| 40 | - idx=$(jq -r --arg o "$output" --argjson w "$win_json" ' | 32 | +# Resolved before the --move branch so both modes fail loudly rather than letting |
| 41 | - [ .[] | 33 | +# niri exit non-zero into a keybind, where nothing would be shown at all. |
| 42 | - | select(.output == $o and .name == null) | 34 | +# Two ids means two workspaces carry the name — a leftover runtime name next to |
| 43 | - | . as $ws | 35 | +# the declared one — which every jq below would then choke on. |
| 44 | - | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) | 36 | +mapfile -t ids < <(jq -r --arg w "$ws" '.[] | select(.name == $w) | .id' <<<"$ws_json") |
| 45 | - ] | last | .idx // empty' <<<"$ws_json") | 37 | +if [ "${#ids[@]}" -eq 0 ]; then |
| 46 | - | 38 | + notify-send "workspace_app" "No workspace named '$ws' — is it declared in config.kdl?" |
| 47 | - if [ -z "$idx" ]; then | 39 | + exit 1 |
| 48 | - notify-send "workspace_app" "No empty workspace free on $output for '$ws'" | 40 | +fi |
| 49 | - exit 1 | 41 | +if [ "${#ids[@]}" -gt 1 ]; then |
| 50 | - fi | 42 | + notify-send "workspace_app" "Several workspaces named '$ws' — un-name the leftover one" |
| 51 | - | 43 | + exit 1 |
| 52 | - # A freshly named workspace is still empty until the app maps its window, | ||
| 53 | - # and workspace_reaper.sh would un-name it in that gap. The marker tells the | ||
| 54 | - # reaper to leave this one alone for a while; see GRACE_SECONDS there. | ||
| 55 | - mkdir -p "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" | ||
| 56 | - : > "${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace/$ws" | ||
| 57 | - | ||
| 58 | - niri msg action set-workspace-name --workspace "$idx" "$ws" >/dev/null | ||
| 59 | fi | 44 | fi |
| 45 | +id="${ids[0]}" | ||
| 60 | 46 | ||
| 61 | if [ "$mode" = "move" ]; then | 47 | if [ "$mode" = "move" ]; then |
| 62 | niri msg action move-column-to-workspace "$ws" >/dev/null | 48 | niri msg action move-column-to-workspace "$ws" >/dev/null |
| 63 | exit 0 | 49 | exit 0 |
| 64 | fi | 50 | fi |
| 65 | 51 | ||
| 52 | +count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') | ||
| 53 | + | ||
| 54 | +if [ "$count" -eq 0 ]; then | ||
| 55 | + # Empty, so the app is not running: bring the workspace to the monitor in use | ||
| 56 | + # before focusing, so the app opens where you are. move-workspace-to-monitor | ||
| 57 | + # takes --reference; the positional argument is the OUTPUT. | ||
| 58 | + focused=$(jq -r '.[] | select(.is_focused == true) | .output' <<<"$ws_json") | ||
| 59 | + home=$(jq -r --arg w "$ws" '.[] | select(.name == $w) | .output' <<<"$ws_json") | ||
| 60 | + # || true: a failed move must not take the focus and the launch down with it | ||
| 61 | + # under `set -e`. Worst case the app opens on the workspace's own monitor. | ||
| 62 | + if [ -n "$focused" ] && [ "$focused" != "$home" ]; then | ||
| 63 | + niri msg action move-workspace-to-monitor --reference "$ws" "$focused" >/dev/null || true | ||
| 64 | + fi | ||
| 65 | +fi | ||
| 66 | + | ||
| 66 | niri msg action focus-workspace "$ws" >/dev/null | 67 | niri msg action focus-workspace "$ws" >/dev/null |
| 67 | 68 | ||
| 68 | # Launch only when the workspace holds nothing, so a second press is "go there" | 69 | # Launch only when the workspace holds nothing, so a second press is "go there" |
| 69 | # rather than a duplicate app. | 70 | # rather than a duplicate app. |
| 70 | -id=$(niri msg -j workspaces | jq -r --arg w "$ws" '.[] | select(.name == $w) | .id') | ||
| 71 | -count=$(niri msg -j windows | jq --argjson id "$id" '[.[] | select(.workspace_id == $id)] | length') | ||
| 72 | - | ||
| 73 | if [ "$count" -eq 0 ] && [ "$#" -gt 0 ]; then | 71 | if [ "$count" -eq 0 ] && [ "$#" -gt 0 ]; then |
| 74 | setsid "$@" >/dev/null 2>&1 & | 72 | setsid "$@" >/dev/null 2>&1 & |
| 75 | fi | 73 | fi |
.config/scripts/niri/workspace_reaper.shdeleted+0 -94
| @@ -1,94 +0,0 @@ | |||
| 1 | -#!/usr/bin/env bash | ||
| 2 | -# Un-names app workspaces once they are empty, so niri reaps them. | ||
| 3 | -# | ||
| 4 | -# Runs for the whole session (spawn-at-startup in config.kdl). Follows | ||
| 5 | -# `niri msg event-stream` rather than polling, so it is idle unless windows or | ||
| 6 | -# workspaces actually change. | ||
| 7 | -# | ||
| 8 | -# Why: a named workspace is never reaped, whether the name came from a config | ||
| 9 | -# `workspace "..."` node or from set-workspace-name at runtime. The app | ||
| 10 | -# workspaces are named on launch by workspace_app.sh; without this they would | ||
| 11 | -# pile up empty exactly as the declared ones did. | ||
| 12 | -# | ||
| 13 | -# Only names listed in app_workspaces.sh are touched, so hand-named workspaces | ||
| 14 | -# survive. | ||
| 15 | -set -uo pipefail | ||
| 16 | - | ||
| 17 | -. "$(dirname "$(realpath "$0")")/app_workspaces.sh" | ||
| 18 | - | ||
| 19 | -# A workspace named by workspace_app.sh sits empty until the app maps its window, | ||
| 20 | -# and reaping it in that gap would drop the name the app was launched under. The | ||
| 21 | -# marker file it leaves means "not populated yet, hands off". | ||
| 22 | -# | ||
| 23 | -# The marker is cleared as soon as the workspace holds a window, so the NEXT time | ||
| 24 | -# it goes empty it is reaped at once. A plain timer would not do: if the app is | ||
| 25 | -# closed while the timer is still running, that close event gets skipped and no | ||
| 26 | -# further event arrives to retry. | ||
| 27 | -# | ||
| 28 | -# GRACE_SECONDS is only a backstop for a launch that never produced a window at | ||
| 29 | -# all (crash, wrong command). Flatpak cold starts are slow, hence the size. | ||
| 30 | -GRACE_SECONDS=60 | ||
| 31 | -GRACE_DIR="${XDG_RUNTIME_DIR:-/tmp}/niri-ws-grace" | ||
| 32 | - | ||
| 33 | -in_grace() { | ||
| 34 | - local marker="$GRACE_DIR/$1" age | ||
| 35 | - [ -f "$marker" ] || return 1 | ||
| 36 | - age=$(( $(date +%s) - $(stat -c %Y "$marker") )) | ||
| 37 | - if [ "$age" -lt "$GRACE_SECONDS" ]; then | ||
| 38 | - return 0 | ||
| 39 | - fi | ||
| 40 | - rm -f "$marker" | ||
| 41 | - return 1 | ||
| 42 | -} | ||
| 43 | - | ||
| 44 | -reap() { | ||
| 45 | - local ws_json win_json name | ||
| 46 | - ws_json=$(niri msg -j workspaces 2>/dev/null) || return 0 | ||
| 47 | - win_json=$(niri msg -j windows 2>/dev/null) || return 0 | ||
| 48 | - | ||
| 49 | - # Any named workspace that now holds windows has served its purpose: drop the | ||
| 50 | - # marker so emptying it later reaps immediately. | ||
| 51 | - while read -r name; do | ||
| 52 | - [ -n "$name" ] && rm -f "$GRACE_DIR/$name" | ||
| 53 | - done < <(jq -r --argjson w "$win_json" ' | ||
| 54 | - .[] | ||
| 55 | - | select(.name != null) | ||
| 56 | - | . as $ws | ||
| 57 | - | select(([$w[] | select(.workspace_id == $ws.id)] | length) > 0) | ||
| 58 | - | .name | ||
| 59 | - ' <<<"$ws_json") | ||
| 60 | - | ||
| 61 | - # Named workspaces holding no windows. | ||
| 62 | - while read -r name; do | ||
| 63 | - [ -n "$name" ] || continue | ||
| 64 | - is_app_workspace "$name" || continue | ||
| 65 | - in_grace "$name" && continue | ||
| 66 | - # The reference is POSITIONAL here. set-workspace-name takes --workspace | ||
| 67 | - # and move-workspace-to-monitor takes --reference, but this one does not. | ||
| 68 | - niri msg action unset-workspace-name "$name" >/dev/null 2>&1 || true | ||
| 69 | - done < <(jq -r --argjson w "$win_json" ' | ||
| 70 | - .[] | ||
| 71 | - | select(.name != null) | ||
| 72 | - | . as $ws | ||
| 73 | - | select(([$w[] | select(.workspace_id == $ws.id)] | length) == 0) | ||
| 74 | - | .name | ||
| 75 | - ' <<<"$ws_json") | ||
| 76 | -} | ||
| 77 | - | ||
| 78 | -# Sweep once at startup: workspaces can be left named and empty by a previous | ||
| 79 | -# session, a config reload, or a crash of this script. | ||
| 80 | -reap | ||
| 81 | - | ||
| 82 | -# Any window or workspace event. Matching loosely on purpose: niri emits | ||
| 83 | -# WindowOpenedOrChanged (singular) when a window maps and WindowsChanged (plural) | ||
| 84 | -# only for the bulk snapshot, so a narrow list missed the moment a workspace | ||
| 85 | -# became populated — which is exactly when the grace marker must be dropped. | ||
| 86 | -# Ignored by omission: ConfigLoaded, KeyboardLayoutsChanged, OverviewOpenedOrClosed, | ||
| 87 | -# CastsChanged. | ||
| 88 | -niri msg -j event-stream 2>/dev/null | while read -r line; do | ||
| 89 | - case "$line" in | ||
| 90 | - *Window* | *Workspace*) | ||
| 91 | - reap | ||
| 92 | - ;; | ||
| 93 | - esac | ||
| 94 | -done | ||