forge
Explore
Log in

Don't you dare go Hollow

Stop media flicker from stacked song writers

Browse files at this commit

9dbb31688bb48e4d7c11b8cc18a7b09b7acf6931Ahmet Kaan GümüşParent: 43a16a9
  • fix
    • stop media flicker from stacked song writers
Show full message
fix(waybar): stop media flicker from stacked song writers

niri restart left orphan song_detail_writer copies (11 live). Each
mv-replaced /tmp/song_detail_last every second, so waybar's tail -F
reopened and blanked the module.

Newest writer takes over; the tailed file is rewritten in place.
Transient playerctl misses hold the last line for 3s instead of clearing.

Also drops named-output Mod+Ctrl+Shift+Q/E workspace moves; directional
binds remain.
.config/niri/config.kdlmodified+0 -2
@@ -224,8 +224,6 @@ binds {
224 Mod+E { focus-monitor "HDMI-A-1"; }224 Mod+E { focus-monitor "HDMI-A-1"; }
225 Mod+Shift+Q { move-column-to-monitor "DP-1"; }225 Mod+Shift+Q { move-column-to-monitor "DP-1"; }
226 Mod+Shift+E { move-column-to-monitor "HDMI-A-1"; }226 Mod+Shift+E { move-column-to-monitor "HDMI-A-1"; }
227- Mod+Ctrl+Shift+Q hotkey-overlay-title="Move workspace: DP-1" { move-workspace-to-monitor "DP-1"; }
228- Mod+Ctrl+Shift+E hotkey-overlay-title="Move workspace: HDMI-A-1" { move-workspace-to-monitor "HDMI-A-1"; }
229 Mod+Ctrl+Shift+A hotkey-overlay-title="Move workspace left" { move-workspace-to-monitor-left; }227 Mod+Ctrl+Shift+A hotkey-overlay-title="Move workspace left" { move-workspace-to-monitor-left; }
230 Mod+Ctrl+Shift+D hotkey-overlay-title="Move workspace right" { move-workspace-to-monitor-right; }228 Mod+Ctrl+Shift+D hotkey-overlay-title="Move workspace right" { move-workspace-to-monitor-right; }
231 Mod+Ctrl+Shift+W hotkey-overlay-title="Move workspace up" { move-workspace-to-monitor-up; }229 Mod+Ctrl+Shift+W hotkey-overlay-title="Move workspace up" { move-workspace-to-monitor-up; }
.config/scripts/shared/song_detail_writer.shmodified+72 -7
@@ -2,11 +2,43 @@
2 # Single-source writer: poll playerctl, write the formatted song-detail line2 # Single-source writer: poll playerctl, write the formatted song-detail line
3 # to /tmp/song_detail_last so every waybar bar can stream it via `tail -F`3 # to /tmp/song_detail_last so every waybar bar can stream it via `tail -F`
4 # without two scripts racing on shared /tmp state.4 # without two scripts racing on shared /tmp state.
5+#
6+# niri spawn-at-startup re-runs on compositor restart and used to stack
7+# copies; orphans then `mv`-replaced the output inode every second, which
8+# makes waybar's `tail -F` reopen and flicker. Newest instance takes over;
9+# the tailed file is rewritten in place so its inode stays put.
5 10
6 OUTPUT_FILE="/tmp/song_detail_last"11 OUTPUT_FILE="/tmp/song_detail_last"
7 POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_"12 POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_"
8 ACTIVITY_PREFIX="/tmp/playerctl_activity_"13 ACTIVITY_PREFIX="/tmp/playerctl_activity_"
14+LOCK_FILE="/tmp/song_detail_writer.lock"
15+PID_FILE="/tmp/song_detail_writer.pid"
9 INTERVAL=116 INTERVAL=1
17+EMPTY_GRACE=3
18+
19+takeover_singleton() {
20+ local pid
21+ while read -r pid; do
22+ [[ -z "$pid" || "$pid" == "$$" ]] && continue
23+ kill "$pid" 2>/dev/null || true
24+ done < <(pgrep -f '/scripts/shared/song_detail_writer.sh' || true)
25+ local i
26+ for i in 1 2 3 4 5 6 7 8 9 10; do
27+ local leftover=0
28+ while read -r pid; do
29+ [[ -z "$pid" || "$pid" == "$$" ]] && continue
30+ leftover=1
31+ kill "$pid" 2>/dev/null || true
32+ done < <(pgrep -f '/scripts/shared/song_detail_writer.sh' || true)
33+ (( leftover == 0 )) && break
34+ sleep 0.05
35+ done
36+ exec 8>"$LOCK_FILE"
37+ flock -n 8 || exit 0
38+ printf '%s\n' "$$" > "$PID_FILE"
39+}
40+
41+takeover_singleton
10 42
11 atomic_write() {43 atomic_write() {
12 local target="$1" content="$2" tmp44 local target="$1" content="$2" tmp
@@ -14,15 +46,40 @@ atomic_write() {
14 printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target"46 printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target"
15 }47 }
16 48
49+# Same inode so `tail -F` does not observe a replace/unlink.
50+write_output() {
51+ printf '%s\n' "$1" > "$OUTPUT_FILE"
52+}
53+
17 last_emit="__init__"54 last_emit="__init__"
55+empty_streak=0
56+last_position=""
57+last_track=""
58+
18 emit() {59 emit() {
19 local out="$1"60 local out="$1"
61+ if [[ -z "$out" ]]; then
62+ empty_streak=$((empty_streak + 1))
63+ if (( empty_streak < EMPTY_GRACE )); then
64+ return
65+ fi
66+ else
67+ empty_streak=0
68+ fi
20 [[ "$out" == "$last_emit" ]] && return69 [[ "$out" == "$last_emit" ]] && return
21- atomic_write "$OUTPUT_FILE" "$out" && last_emit="$out"70+ write_output "$out" && last_emit="$out"
22 }71 }
23 72
24-# Ensure file exists immediately so `tail -F` doesn't sit empty on startup.73+pango_escape() {
25-atomic_write "$OUTPUT_FILE" ""74+ local s="$1"
75+ s="${s//&/&amp;}"
76+ s="${s//</&lt;}"
77+ s="${s//>/&gt;}"
78+ printf '%s' "$s"
79+}
80+
81+# File must exist so `tail -F` can attach; do not wipe a live line.
82+[[ -e "$OUTPUT_FILE" ]] || : > "$OUTPUT_FILE"
26 83
27 while true; do84 while true; do
28 if ! command -v playerctl &>/dev/null; then85 if ! command -v playerctl &>/dev/null; then
@@ -96,7 +153,7 @@ while true; do
96 fi153 fi
97 154
98 duration=""155 duration=""
99- if [[ -n "$duration_us" && "$duration_us" -gt 0 ]] 2>/dev/null; then156+ if [[ "$duration_us" =~ ^[0-9]+$ ]]; then
100 duration_sec=$((duration_us / 1000000))157 duration_sec=$((duration_us / 1000000))
101 minutes=$((duration_sec / 60))158 minutes=$((duration_sec / 60))
102 seconds=$((duration_sec % 60))159 seconds=$((duration_sec % 60))
@@ -105,13 +162,21 @@ while true; do
105 162
106 POSITION_CACHE="${POSITION_CACHE_PREFIX}$(sanitize "$chosen")"163 POSITION_CACHE="${POSITION_CACHE_PREFIX}$(sanitize "$chosen")"
107 position=""164 position=""
165+ track_id="$chosen|$artist|$title|$duration"
166+ if [[ "$track_id" != "$last_track" ]]; then
167+ last_position=""
168+ last_track="$track_id"
169+ fi
108 if [[ "$status" == "Playing" ]]; then170 if [[ "$status" == "Playing" ]]; then
109- if [[ -n "$position_us" && "$position_us" -gt 0 ]] 2>/dev/null; then171+ if [[ "$position_us" =~ ^[0-9]+$ ]]; then
110 position_sec=$((position_us / 1000000))172 position_sec=$((position_us / 1000000))
111 pos_minutes=$((position_sec / 60))173 pos_minutes=$((position_sec / 60))
112 pos_seconds=$((position_sec % 60))174 pos_seconds=$((position_sec % 60))
113 position=$(printf "%02d:%02d" $pos_minutes $pos_seconds)175 position=$(printf "%02d:%02d" $pos_minutes $pos_seconds)
114 atomic_write "$POSITION_CACHE" "$position"176 atomic_write "$POSITION_CACHE" "$position"
177+ last_position="$position"
178+ elif [[ -n "$last_position" ]]; then
179+ position="$last_position"
115 fi180 fi
116 else181 else
117 if [[ -f "$POSITION_CACHE" ]]; then182 if [[ -f "$POSITION_CACHE" ]]; then
@@ -119,8 +184,8 @@ while true; do
119 fi184 fi
120 fi185 fi
121 186
122- artist="${artist//&/&amp;}"187+ artist=$(pango_escape "$artist")
123- title="${title//&/&amp;}"188+ title=$(pango_escape "$title")
124 189
125 if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then190 if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then
126 out="$icon $artist - $title ($position / $duration)"191 out="$icon $artist - $title ($position / $duration)"