From efb4b8656ca5f2388df17286a7452736e822487d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 30 Jul 2026 04:36:15 +0300 Subject: [PATCH] refactor(song-detail): one writer daemon, consumers read a file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.yuck | 4 +- .config/hypr/hyprlock.conf | 4 +- .config/scripts/shared/song_detail.sh | 130 ----------------- .config/scripts/shared/song_detail_writer.sh | 143 +++++++++++++++++++ .config/waybar/config | 3 +- 5 files changed, 148 insertions(+), 136 deletions(-) delete mode 100755 .config/scripts/shared/song_detail.sh create mode 100755 .config/scripts/shared/song_detail_writer.sh diff --git a/.config/eww/eww.yuck b/.config/eww/eww.yuck index e37b077..6b06057 100644 --- a/.config/eww/eww.yuck +++ b/.config/eww/eww.yuck @@ -15,9 +15,9 @@ (defvar byte_transform 1073741824) -;; Music/Player info - using song_detail.sh script +;; Music/Player info - single-source file maintained by song_detail_writer.sh (defpoll music :interval "1s" - `bash ~/.config/scripts/shared/song_detail.sh`) + `cat /tmp/song_detail_last 2>/dev/null`) (defpoll music-status :interval "1s" `playerctl status 2>/dev/null || echo "Stopped"`) diff --git a/.config/hypr/hyprlock.conf b/.config/hypr/hyprlock.conf index fe901e9..98bd713 100644 --- a/.config/hypr/hyprlock.conf +++ b/.config/hypr/hyprlock.conf @@ -53,10 +53,10 @@ input-field { fail_text = Authentication Failed } -# Music info label - displays current playing song with duration from song_detail.sh script +# Music info label - reads single-source file maintained by song_detail_writer.sh label { monitor = - text = cmd[update:1000] echo "$(~/.config/scripts/shared/song_detail.sh)" + text = cmd[update:1000] cat /tmp/song_detail_last 2>/dev/null color = rgba(180, 200, 215, 0.75) font_size = 18 font_family = JetBrains Mono Nerd, SF Pro Display Bold diff --git a/.config/scripts/shared/song_detail.sh b/.config/scripts/shared/song_detail.sh deleted file mode 100755 index 461975f..0000000 --- a/.config/scripts/shared/song_detail.sh +++ /dev/null @@ -1,130 +0,0 @@ -#!/bin/bash -# Get current playing song from playerctl with sticky player preference - -if ! command -v playerctl &> /dev/null; then - echo "" - exit 0 -fi - -player_list=$(playerctl -l 2>/dev/null) -if [[ -z "$player_list" ]]; then - echo "" - exit 0 -fi - -PREFERRED_FILE="/tmp/playerctl_preferred_player" - -# Find first Playing and first Paused player -best_playing="" -best_paused="" -while IFS= read -r player; do - player_status=$(playerctl -p "$player" status 2>/dev/null) - if [[ "$player_status" == "Playing" && -z "$best_playing" ]]; then - best_playing="$player" - elif [[ "$player_status" == "Paused" && -z "$best_paused" ]]; then - best_paused="$player" - fi -done <<< "$player_list" - -# Sticky player logic: prefer current player to avoid flickering -preferred="" -if [[ -f "$PREFERRED_FILE" ]]; then - preferred=$(cat "$PREFERRED_FILE") - pref_status=$(playerctl -p "$preferred" status 2>/dev/null) - if [[ "$pref_status" != "Playing" && "$pref_status" != "Paused" ]]; then - preferred="" - pref_status="" - fi -fi - -# Choose player: -# - If preferred is still Playing, keep it -# - If preferred is Paused but something else is Playing, switch -# - Otherwise fall back to best available -chosen="" -if [[ -n "$preferred" ]]; then - if [[ "$pref_status" == "Playing" ]]; then - chosen="$preferred" - elif [[ -n "$best_playing" ]]; then - chosen="$best_playing" - else - chosen="$preferred" - fi -else - chosen="${best_playing:-$best_paused}" -fi - -if [[ -z "$chosen" ]]; then - echo "" - exit 0 -fi - -echo "$chosen" > "$PREFERRED_FILE" - -# Fetch ALL metadata in a single playerctl call -info=$(playerctl -p "$chosen" metadata --format '{{status}} {{artist}} {{title}} {{mpris:length}} {{position}}' 2>/dev/null) - -if [[ -z "$info" ]]; then - echo "" - exit 0 -fi - -IFS=$'\t' read -r status artist title duration_us position_us <<< "$info" - -if [[ "$status" == "Stopped" ]]; then - echo "" - exit 0 -fi - -# Set icon based on playback status -if [[ "$status" == "Playing" ]]; then - icon="▶" -else - icon="⏸" -fi - -# Convert duration from microseconds to MM:SS -duration="" -if [[ -n "$duration_us" && "$duration_us" -gt 0 ]] 2>/dev/null; then - duration_sec=$((duration_us / 1000000)) - minutes=$((duration_sec / 60)) - seconds=$((duration_sec % 60)) - duration=$(printf "%02d:%02d" $minutes $seconds) -fi - -# Get position with caching for paused state -POSITION_CACHE="/tmp/playerctl_position_cache_${chosen}" -position="" -if [[ "$status" == "Playing" ]]; then - if [[ -n "$position_us" && "$position_us" -gt 0 ]] 2>/dev/null; then - position_sec=$((position_us / 1000000)) - pos_minutes=$((position_sec / 60)) - pos_seconds=$((position_sec % 60)) - position=$(printf "%02d:%02d" $pos_minutes $pos_seconds) - echo "$position" > "$POSITION_CACHE" - fi -else - if [[ -f "$POSITION_CACHE" ]]; then - position=$(cat "$POSITION_CACHE") - fi -fi - -# Escape ampersands for waybar markup -artist="${artist//&/&}" -title="${title//&/&}" - -if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then - echo "$icon $artist - $title ($position / $duration)" -elif [[ -n "$artist" && -n "$title" && -n "$duration" ]]; then - echo "$icon $artist - $title ($duration)" -elif [[ -n "$title" && -n "$duration" && -n "$position" ]]; then - echo "$icon $title ($position / $duration)" -elif [[ -n "$title" && -n "$duration" ]]; then - echo "$icon $title ($duration)" -elif [[ -n "$artist" && -n "$title" ]]; then - echo "$icon $artist - $title" -elif [[ -n "$title" ]]; then - echo "$icon $title" -else - echo "$icon Something's Playing" -fi diff --git a/.config/scripts/shared/song_detail_writer.sh b/.config/scripts/shared/song_detail_writer.sh new file mode 100755 index 0000000..c2ab121 --- /dev/null +++ b/.config/scripts/shared/song_detail_writer.sh @@ -0,0 +1,143 @@ +#!/bin/bash +# Single-source writer: poll playerctl, write the formatted song-detail line +# to /tmp/song_detail_last so every waybar bar can stream it via `tail -F` +# without two scripts racing on shared /tmp state. + +OUTPUT_FILE="/tmp/song_detail_last" +POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_" +ACTIVITY_PREFIX="/tmp/playerctl_activity_" +INTERVAL=1 + +atomic_write() { + local target="$1" content="$2" tmp + tmp=$(mktemp --tmpdir=/tmp song_detail.XXXXXX) || return 1 + printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target" +} + +last_emit="__init__" +emit() { + local out="$1" + [[ "$out" == "$last_emit" ]] && return + atomic_write "$OUTPUT_FILE" "$out" && last_emit="$out" +} + +# Ensure file exists immediately so `tail -F` doesn't sit empty on startup. +atomic_write "$OUTPUT_FILE" "" + +while true; do + if ! command -v playerctl &>/dev/null; then + emit "" + sleep "$INTERVAL" + continue + fi + + player_list=$(playerctl -l 2>/dev/null) + if [[ -z "$player_list" ]]; then + emit "" + sleep "$INTERVAL" + continue + fi + + now=$(date +%s) + sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; } + + best_playing="" + best_playing_ts=0 + best_paused="" + best_paused_ts=0 + while IFS= read -r player; do + [[ -z "$player" ]] && continue + player_status=$(playerctl -p "$player" status 2>/dev/null) + act_file="${ACTIVITY_PREFIX}$(sanitize "$player")" + if [[ "$player_status" == "Playing" ]]; then + atomic_write "$act_file" "$now" + ts="$now" + if (( ts > best_playing_ts )); then + best_playing_ts="$ts" + best_playing="$player" + fi + elif [[ "$player_status" == "Paused" ]]; then + ts=0 + [[ -f "$act_file" ]] && ts=$(cat "$act_file" 2>/dev/null) && [[ -z "$ts" ]] && ts=0 + if (( ts > best_paused_ts )); then + best_paused_ts="$ts" + best_paused="$player" + fi + fi + done <<< "$player_list" + + chosen="${best_playing:-$best_paused}" + + if [[ -z "$chosen" ]]; then + emit "" + sleep "$INTERVAL" + continue + fi + + info=$(playerctl -p "$chosen" metadata --format '{{status}} {{artist}} {{title}} {{mpris:length}} {{position}}' 2>/dev/null) + if [[ -z "$info" ]]; then + emit "" + sleep "$INTERVAL" + continue + fi + + IFS=$'\t' read -r status artist title duration_us position_us <<< "$info" + + if [[ "$status" == "Stopped" ]]; then + emit "" + sleep "$INTERVAL" + continue + fi + + if [[ "$status" == "Playing" ]]; then + icon="▶" + else + icon="⏸" + fi + + duration="" + if [[ -n "$duration_us" && "$duration_us" -gt 0 ]] 2>/dev/null; then + duration_sec=$((duration_us / 1000000)) + minutes=$((duration_sec / 60)) + seconds=$((duration_sec % 60)) + duration=$(printf "%02d:%02d" $minutes $seconds) + fi + + POSITION_CACHE="${POSITION_CACHE_PREFIX}$(sanitize "$chosen")" + position="" + if [[ "$status" == "Playing" ]]; then + if [[ -n "$position_us" && "$position_us" -gt 0 ]] 2>/dev/null; then + position_sec=$((position_us / 1000000)) + pos_minutes=$((position_sec / 60)) + pos_seconds=$((position_sec % 60)) + position=$(printf "%02d:%02d" $pos_minutes $pos_seconds) + atomic_write "$POSITION_CACHE" "$position" + fi + else + if [[ -f "$POSITION_CACHE" ]]; then + position=$(cat "$POSITION_CACHE") + fi + fi + + artist="${artist//&/&}" + title="${title//&/&}" + + if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then + out="$icon $artist - $title ($position / $duration)" + elif [[ -n "$artist" && -n "$title" && -n "$duration" ]]; then + out="$icon $artist - $title ($duration)" + elif [[ -n "$title" && -n "$duration" && -n "$position" ]]; then + out="$icon $title ($position / $duration)" + elif [[ -n "$title" && -n "$duration" ]]; then + out="$icon $title ($duration)" + elif [[ -n "$artist" && -n "$title" ]]; then + out="$icon $artist - $title" + elif [[ -n "$title" ]]; then + out="$icon $title" + else + out="$icon Something's Playing" + fi + + emit "$out" + sleep "$INTERVAL" +done diff --git a/.config/waybar/config b/.config/waybar/config index 32f537a..317b7ba 100644 --- a/.config/waybar/config +++ b/.config/waybar/config @@ -79,8 +79,7 @@ "custom/music": { "format": "{}", - "exec": "bash ~/.config/scripts/shared/song_detail.sh", - "interval": 1, + "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", "on-click": "playerctl play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+",