forge
Explore
Log in

Don't you dare go Hollow

One writer daemon, consumers read a file

Browse files at this commit

efb4b8656ca5f2388df17286a7452736e822487dAhmet Kaan GümüşParent: 192cda9
  • refactor
    • one writer daemon, consumers read a file
Show full message
refactor(song-detail): one writer daemon, consumers read a file

song_detail.sh ran once per tick per consumer (eww, waybar, hyprlock) —
three playerctl spawns a second. song_detail_writer.sh runs once and
writes /tmp/song_detail_last; consumers cat/tail it instead.
.config/eww/eww.yuckmodified+2 -2
@@ -15,9 +15,9 @@
15 15
16 (defvar byte_transform 1073741824)16 (defvar byte_transform 1073741824)
17 17
18-;; Music/Player info - using song_detail.sh script18+;; Music/Player info - single-source file maintained by song_detail_writer.sh
19 (defpoll music :interval "1s"19 (defpoll music :interval "1s"
20- `bash ~/.config/scripts/shared/song_detail.sh`)20+ `cat /tmp/song_detail_last 2>/dev/null`)
21 21
22 (defpoll music-status :interval "1s"22 (defpoll music-status :interval "1s"
23 `playerctl status 2>/dev/null || echo "Stopped"`)23 `playerctl status 2>/dev/null || echo "Stopped"`)
.config/hypr/hyprlock.confmodified+2 -2
@@ -53,10 +53,10 @@ input-field {
53 fail_text = <i>Authentication Failed</i>53 fail_text = <i>Authentication Failed</i>
54 }54 }
55 55
56-# Music info label - displays current playing song with duration from song_detail.sh script56+# Music info label - reads single-source file maintained by song_detail_writer.sh
57 label {57 label {
58 monitor =58 monitor =
59- text = cmd[update:1000] echo "$(~/.config/scripts/shared/song_detail.sh)"59+ text = cmd[update:1000] cat /tmp/song_detail_last 2>/dev/null
60 color = rgba(180, 200, 215, 0.75)60 color = rgba(180, 200, 215, 0.75)
61 font_size = 1861 font_size = 18
62 font_family = JetBrains Mono Nerd, SF Pro Display Bold62 font_family = JetBrains Mono Nerd, SF Pro Display Bold
.config/scripts/shared/song_detail.shdeleted+0 -130
@@ -1,130 +0,0 @@
1-#!/bin/bash
2-# Get current playing song from playerctl with sticky player preference
3-
4-if ! command -v playerctl &> /dev/null; then
5- echo ""
6- exit 0
7-fi
8-
9-player_list=$(playerctl -l 2>/dev/null)
10-if [[ -z "$player_list" ]]; then
11- echo ""
12- exit 0
13-fi
14-
15-PREFERRED_FILE="/tmp/playerctl_preferred_player"
16-
17-# Find first Playing and first Paused player
18-best_playing=""
19-best_paused=""
20-while IFS= read -r player; do
21- player_status=$(playerctl -p "$player" status 2>/dev/null)
22- if [[ "$player_status" == "Playing" && -z "$best_playing" ]]; then
23- best_playing="$player"
24- elif [[ "$player_status" == "Paused" && -z "$best_paused" ]]; then
25- best_paused="$player"
26- fi
27-done <<< "$player_list"
28-
29-# Sticky player logic: prefer current player to avoid flickering
30-preferred=""
31-if [[ -f "$PREFERRED_FILE" ]]; then
32- preferred=$(cat "$PREFERRED_FILE")
33- pref_status=$(playerctl -p "$preferred" status 2>/dev/null)
34- if [[ "$pref_status" != "Playing" && "$pref_status" != "Paused" ]]; then
35- preferred=""
36- pref_status=""
37- fi
38-fi
39-
40-# Choose player:
41-# - If preferred is still Playing, keep it
42-# - If preferred is Paused but something else is Playing, switch
43-# - Otherwise fall back to best available
44-chosen=""
45-if [[ -n "$preferred" ]]; then
46- if [[ "$pref_status" == "Playing" ]]; then
47- chosen="$preferred"
48- elif [[ -n "$best_playing" ]]; then
49- chosen="$best_playing"
50- else
51- chosen="$preferred"
52- fi
53-else
54- chosen="${best_playing:-$best_paused}"
55-fi
56-
57-if [[ -z "$chosen" ]]; then
58- echo ""
59- exit 0
60-fi
61-
62-echo "$chosen" > "$PREFERRED_FILE"
63-
64-# Fetch ALL metadata in a single playerctl call
65-info=$(playerctl -p "$chosen" metadata --format '{{status}} {{artist}} {{title}} {{mpris:length}} {{position}}' 2>/dev/null)
66-
67-if [[ -z "$info" ]]; then
68- echo ""
69- exit 0
70-fi
71-
72-IFS=$'\t' read -r status artist title duration_us position_us <<< "$info"
73-
74-if [[ "$status" == "Stopped" ]]; then
75- echo ""
76- exit 0
77-fi
78-
79-# Set icon based on playback status
80-if [[ "$status" == "Playing" ]]; then
81- icon="▶"
82-else
83- icon="⏸"
84-fi
85-
86-# Convert duration from microseconds to MM:SS
87-duration=""
88-if [[ -n "$duration_us" && "$duration_us" -gt 0 ]] 2>/dev/null; then
89- duration_sec=$((duration_us / 1000000))
90- minutes=$((duration_sec / 60))
91- seconds=$((duration_sec % 60))
92- duration=$(printf "%02d:%02d" $minutes $seconds)
93-fi
94-
95-# Get position with caching for paused state
96-POSITION_CACHE="/tmp/playerctl_position_cache_${chosen}"
97-position=""
98-if [[ "$status" == "Playing" ]]; then
99- if [[ -n "$position_us" && "$position_us" -gt 0 ]] 2>/dev/null; then
100- position_sec=$((position_us / 1000000))
101- pos_minutes=$((position_sec / 60))
102- pos_seconds=$((position_sec % 60))
103- position=$(printf "%02d:%02d" $pos_minutes $pos_seconds)
104- echo "$position" > "$POSITION_CACHE"
105- fi
106-else
107- if [[ -f "$POSITION_CACHE" ]]; then
108- position=$(cat "$POSITION_CACHE")
109- fi
110-fi
111-
112-# Escape ampersands for waybar markup
113-artist="${artist//&/&amp;}"
114-title="${title//&/&amp;}"
115-
116-if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then
117- echo "$icon $artist - $title ($position / $duration)"
118-elif [[ -n "$artist" && -n "$title" && -n "$duration" ]]; then
119- echo "$icon $artist - $title ($duration)"
120-elif [[ -n "$title" && -n "$duration" && -n "$position" ]]; then
121- echo "$icon $title ($position / $duration)"
122-elif [[ -n "$title" && -n "$duration" ]]; then
123- echo "$icon $title ($duration)"
124-elif [[ -n "$artist" && -n "$title" ]]; then
125- echo "$icon $artist - $title"
126-elif [[ -n "$title" ]]; then
127- echo "$icon $title"
128-else
129- echo "$icon Something's Playing"
130-fi
.config/scripts/shared/song_detail_writer.shadded+143 -0
@@ -0,0 +1,143 @@
1+#!/bin/bash
2+# 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`
4+# without two scripts racing on shared /tmp state.
5+
6+OUTPUT_FILE="/tmp/song_detail_last"
7+POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_"
8+ACTIVITY_PREFIX="/tmp/playerctl_activity_"
9+INTERVAL=1
10+
11+atomic_write() {
12+ local target="$1" content="$2" tmp
13+ tmp=$(mktemp --tmpdir=/tmp song_detail.XXXXXX) || return 1
14+ printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target"
15+}
16+
17+last_emit="__init__"
18+emit() {
19+ local out="$1"
20+ [[ "$out" == "$last_emit" ]] && return
21+ atomic_write "$OUTPUT_FILE" "$out" && last_emit="$out"
22+}
23+
24+# Ensure file exists immediately so `tail -F` doesn't sit empty on startup.
25+atomic_write "$OUTPUT_FILE" ""
26+
27+while true; do
28+ if ! command -v playerctl &>/dev/null; then
29+ emit ""
30+ sleep "$INTERVAL"
31+ continue
32+ fi
33+
34+ player_list=$(playerctl -l 2>/dev/null)
35+ if [[ -z "$player_list" ]]; then
36+ emit ""
37+ sleep "$INTERVAL"
38+ continue
39+ fi
40+
41+ now=$(date +%s)
42+ sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; }
43+
44+ best_playing=""
45+ best_playing_ts=0
46+ best_paused=""
47+ best_paused_ts=0
48+ while IFS= read -r player; do
49+ [[ -z "$player" ]] && continue
50+ player_status=$(playerctl -p "$player" status 2>/dev/null)
51+ act_file="${ACTIVITY_PREFIX}$(sanitize "$player")"
52+ if [[ "$player_status" == "Playing" ]]; then
53+ atomic_write "$act_file" "$now"
54+ ts="$now"
55+ if (( ts > best_playing_ts )); then
56+ best_playing_ts="$ts"
57+ best_playing="$player"
58+ fi
59+ elif [[ "$player_status" == "Paused" ]]; then
60+ ts=0
61+ [[ -f "$act_file" ]] && ts=$(cat "$act_file" 2>/dev/null) && [[ -z "$ts" ]] && ts=0
62+ if (( ts > best_paused_ts )); then
63+ best_paused_ts="$ts"
64+ best_paused="$player"
65+ fi
66+ fi
67+ done <<< "$player_list"
68+
69+ chosen="${best_playing:-$best_paused}"
70+
71+ if [[ -z "$chosen" ]]; then
72+ emit ""
73+ sleep "$INTERVAL"
74+ continue
75+ fi
76+
77+ info=$(playerctl -p "$chosen" metadata --format '{{status}} {{artist}} {{title}} {{mpris:length}} {{position}}' 2>/dev/null)
78+ if [[ -z "$info" ]]; then
79+ emit ""
80+ sleep "$INTERVAL"
81+ continue
82+ fi
83+
84+ IFS=$'\t' read -r status artist title duration_us position_us <<< "$info"
85+
86+ if [[ "$status" == "Stopped" ]]; then
87+ emit ""
88+ sleep "$INTERVAL"
89+ continue
90+ fi
91+
92+ if [[ "$status" == "Playing" ]]; then
93+ icon="▶"
94+ else
95+ icon="⏸"
96+ fi
97+
98+ duration=""
99+ if [[ -n "$duration_us" && "$duration_us" -gt 0 ]] 2>/dev/null; then
100+ duration_sec=$((duration_us / 1000000))
101+ minutes=$((duration_sec / 60))
102+ seconds=$((duration_sec % 60))
103+ duration=$(printf "%02d:%02d" $minutes $seconds)
104+ fi
105+
106+ POSITION_CACHE="${POSITION_CACHE_PREFIX}$(sanitize "$chosen")"
107+ position=""
108+ if [[ "$status" == "Playing" ]]; then
109+ if [[ -n "$position_us" && "$position_us" -gt 0 ]] 2>/dev/null; then
110+ position_sec=$((position_us / 1000000))
111+ pos_minutes=$((position_sec / 60))
112+ pos_seconds=$((position_sec % 60))
113+ position=$(printf "%02d:%02d" $pos_minutes $pos_seconds)
114+ atomic_write "$POSITION_CACHE" "$position"
115+ fi
116+ else
117+ if [[ -f "$POSITION_CACHE" ]]; then
118+ position=$(cat "$POSITION_CACHE")
119+ fi
120+ fi
121+
122+ artist="${artist//&/&amp;}"
123+ title="${title//&/&amp;}"
124+
125+ if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then
126+ out="$icon $artist - $title ($position / $duration)"
127+ elif [[ -n "$artist" && -n "$title" && -n "$duration" ]]; then
128+ out="$icon $artist - $title ($duration)"
129+ elif [[ -n "$title" && -n "$duration" && -n "$position" ]]; then
130+ out="$icon $title ($position / $duration)"
131+ elif [[ -n "$title" && -n "$duration" ]]; then
132+ out="$icon $title ($duration)"
133+ elif [[ -n "$artist" && -n "$title" ]]; then
134+ out="$icon $artist - $title"
135+ elif [[ -n "$title" ]]; then
136+ out="$icon $title"
137+ else
138+ out="$icon Something's Playing"
139+ fi
140+
141+ emit "$out"
142+ sleep "$INTERVAL"
143+done
.config/waybar/configmodified+1 -2
@@ -79,8 +79,7 @@
79 79
80 "custom/music": {80 "custom/music": {
81 "format": "{}",81 "format": "{}",
82- "exec": "bash ~/.config/scripts/shared/song_detail.sh",82+ "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null",
83- "interval": 1,
84 "on-click": "playerctl play-pause",83 "on-click": "playerctl play-pause",
85 "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh",84 "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh",
86 "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+",85 "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+",