diff --git a/.config/btop/btop.conf b/.config/btop/btop.conf index 64ab4bc..bbd86b8 100644 --- a/.config/btop/btop.conf +++ b/.config/btop/btop.conf @@ -192,7 +192,7 @@ show_swap = true swap_disk = true #* If mem box should be split to also show disks info. -show_disks = true +show_disks = false #* Filter out non physical disks. Set this to False to include network disks, RAM disks and similar. only_physical = true diff --git a/.config/eww/eww.yuck b/.config/eww/eww.yuck index 75c4acc..d06900c 100644 --- a/.config/eww/eww.yuck +++ b/.config/eww/eww.yuck @@ -96,7 +96,7 @@ ;; Music widget - click to play/pause, right-click to show cover (defwidget music-widget [] - (eventbox :onclick "bash ~/.config/scripts/shared/playerctl-active.sh play-pause" + (eventbox :onclick "bash ~/.config/scripts/shared/playerctl_active.sh play-pause" :onrightclick "bash ~/.config/scripts/shared/show_cover.sh" :class "music-box" (label :text music))) diff --git a/.config/hypr/hyprland.conf b/.config/hypr/hyprland.conf index 893f135..98c2dde 100644 --- a/.config/hypr/hyprland.conf +++ b/.config/hypr/hyprland.conf @@ -32,8 +32,8 @@ $clipboard = cliphist list | rofi -dmenu -show-icons -i | cliphist decode | wl- $screenshot = grim -t png - | tee "$HOME/Pictures/Screenshots/$(date '+%y%m%d_%H-%M-%S').png" | wl-copy -t image/png $screenshot_with_area_selector = grim -t png -g "$(slurp)" - | wl-copy -t image/png $power_menu = bash ~/.config/scripts/system/power_menu.sh -$browser = flatpak run io.github.ungoogled_software.ungoogled_chromium --enable-features=MiddleClickAutoscroll -$browser_unsafe = mullvad-exclude flatpak run org.chromium.Chromium --incognito --enable-features=MiddleClickAutoscroll,AllowLegacyMV2Extensions --disable-features=ExtensionManifestV2Unsupported,ExtensionManifestV2Disabled,ExtensionsManifestV3Only +$browser = flatpak run io.github.ungoogled_software.ungoogled_chromium --enable-features=MiddleClickAutoscroll,HardwareMediaKeyHandling +$browser_unsafe = mullvad-exclude flatpak run org.chromium.Chromium --incognito --enable-features=MiddleClickAutoscroll,HardwareMediaKeyHandling,AllowLegacyMV2Extensions --disable-features=ExtensionManifestV2Unsupported,ExtensionManifestV2Disabled,ExtensionsManifestV3Only $signal = flatpak run org.signal.Signal $deezer = mullvad-exclude flatpak run io.github.ungoogled_software.ungoogled_chromium --no-first-run --app="https://deezer.com/" --class=chrome-deezer.com__-Default --name=chrome-deezer.com__-Default --user-data-dir=/home/tahinli/.var/app/io.github.ungoogled_software.ungoogled_chromium/data/web-app-hub/profiles/VjW8vTQ3 $mainMod = SUPER @@ -63,7 +63,7 @@ bindl = $mainMod SHIFT, M, exec, ~/.config/scripts/hyprland/toggle_monitors.sh o bind = $mainMod SHIFT, K, exec, bash ~/.config/scripts/system/usb_menu.sh $discord = flatpak run dev.vencord.Vesktop -$youtube = mullvad-exclude flatpak run io.freetubeapp.FreeTube +$youtube = mullvad-exclude flatpak run --command=bash io.freetubeapp.FreeTube -c 'true & wait; run.sh; exit $?' $code = zed $mail = flatpak run io.github.ungoogled_software.ungoogled_chromium --no-first-run --app="https://mail.proton.me/" --class=chrome-mail.proton.me__-Default --name=chrome-mail.proton.me__-Default --user-data-dir=/home/tahinli/.var/app/io.github.ungoogled_software.ungoogled_chromium/data/web-app-hub/profiles/eIpzVtL8 $whatsapp = mullvad-exclude flatpak run io.github.ungoogled_software.ungoogled_chromium --no-first-run --app="https://web.whatsapp.com/" --class=chrome-web.whatsapp.com__-Default --name=chrome-web.whatsapp.com__-Default --user-data-dir=/home/tahinli/.var/app/io.github.ungoogled_software.ungoogled_chromium/data/web-app-hub/profiles/aW2IMaZ8 @@ -168,10 +168,10 @@ bindel = ,XF86AudioMicMute, exec, wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle bindel = ,XF86MonBrightnessUp, exec, brightnessctl s 10%+ bindel = ,XF86MonBrightnessDown, exec, brightnessctl s 10%- -bindl = , XF86AudioNext, exec, ~/.config/scripts/shared/playerctl-active.sh next -bindl = , XF86AudioPause, exec, ~/.config/scripts/shared/playerctl-active.sh play-pause -bindl = , XF86AudioPlay, exec, ~/.config/scripts/shared/playerctl-active.sh play-pause -bindl = , XF86AudioPrev, exec, ~/.config/scripts/shared/playerctl-active.sh previous +bindl = , XF86AudioNext, exec, ~/.config/scripts/shared/playerctl_active.sh next +bindl = , XF86AudioPause, exec, ~/.config/scripts/shared/playerctl_active.sh play-pause +bindl = , XF86AudioPlay, exec, ~/.config/scripts/shared/playerctl_active.sh play-pause +bindl = , XF86AudioPrev, exec, ~/.config/scripts/shared/playerctl_active.sh previous windowrule = match:class .*, suppress_event maximize windowrule = match:class ^$, match:title ^$, match:xwayland 1, match:float 1, match:fullscreen 0, match:pin 0, no_focus on diff --git a/.config/mimeapps.list b/.config/mimeapps.list index f83b1bc..deb5534 100644 --- a/.config/mimeapps.list +++ b/.config/mimeapps.list @@ -61,6 +61,7 @@ x-scheme-handler/surrealist=surrealist.desktop x-scheme-handler/openhuman=OpenHuman-handler.desktop x-scheme-handler/zcode=zcode.desktop x-scheme-handler/logseq=logseq.desktop +x-scheme-handler/fluxer=app.fluxer.FluxerDesktopCanary.desktop [Added Associations] video/mp4=mpv.desktop; diff --git a/.config/niri/config.kdl b/.config/niri/config.kdl index 3b14f96..f655cff 100644 --- a/.config/niri/config.kdl +++ b/.config/niri/config.kdl @@ -78,7 +78,7 @@ layout { width 3 // WALLUST-GRADIENT - active-gradient from="#9B1A1A" to="#FCAE03" angle=90 in="oklch" + active-gradient from="#5C112D" to="#CD5B74" angle=90 in="oklch" inactive-color "#595959aa" urgent-color "#9b0000" @@ -160,8 +160,8 @@ binds { Alt+F3 hotkey-overlay-title="Clipboard history" { spawn-sh "cliphist list | rofi -dmenu -show-icons -i | cliphist decode | wl-copy"; } Alt+3 hotkey-overlay-title="Clipboard history" { spawn-sh "cliphist list | rofi -dmenu -show-icons -i | cliphist decode | wl-copy"; } - Alt+F hotkey-overlay-title="Browser" { spawn-sh "flatpak run io.github.ungoogled_software.ungoogled_chromium --enable-features=MiddleClickAutoscroll"; } - Shift+Alt+F hotkey-overlay-title="Browser (incognito, outside VPN)" { spawn-sh "mullvad-exclude flatpak run org.chromium.Chromium --incognito --enable-features=MiddleClickAutoscroll,AllowLegacyMV2Extensions --disable-features=ExtensionManifestV2Unsupported,ExtensionManifestV2Disabled,ExtensionsManifestV3Only"; } + Alt+F hotkey-overlay-title="Browser" { spawn-sh "flatpak run io.github.ungoogled_software.ungoogled_chromium --enable-features=MiddleClickAutoscroll,HardwareMediaKeyHandling"; } + Shift+Alt+F hotkey-overlay-title="Browser (incognito, outside VPN)" { spawn-sh "mullvad-exclude flatpak run org.chromium.Chromium --incognito --enable-features=MiddleClickAutoscroll,HardwareMediaKeyHandling,AllowLegacyMV2Extensions --disable-features=ExtensionManifestV2Unsupported,ExtensionManifestV2Disabled,ExtensionsManifestV3Only"; } Alt+C hotkey-overlay-title="Workspace: code" { focus-workspace "code"; } Alt+G hotkey-overlay-title="Workspace: github" { focus-workspace "github"; } @@ -175,7 +175,7 @@ binds { Shift+Alt+W hotkey-overlay-title="Launch: whatsapp" { spawn-sh "~/.config/scripts/shared/webapp.sh --mullvad https://web.whatsapp.com/ aW2IMaZ8"; } Shift+Alt+D hotkey-overlay-title="Launch: discord" { spawn-sh "flatpak run org.equicord.equibop"; } Shift+Alt+S hotkey-overlay-title="Launch: deezer" { spawn-sh "~/.config/scripts/shared/webapp.sh --mullvad https://deezer.com/ VjW8vTQ3"; } - Shift+Alt+Y hotkey-overlay-title="Launch: youtube" { spawn-sh "mullvad-exclude flatpak run io.freetubeapp.FreeTube"; } + Shift+Alt+Y hotkey-overlay-title="Launch: youtube" { spawn-sh "mullvad-exclude flatpak run --command=bash io.freetubeapp.FreeTube -c 'true & wait; run.sh; exit $?'"; } Shift+Alt+P hotkey-overlay-title="Launch: mail" { spawn-sh "~/.config/scripts/shared/webapp.sh https://mail.proton.me/ eIpzVtL8"; } Mod+Shift+C { move-column-to-workspace "code"; } @@ -202,16 +202,16 @@ binds { Mod+Shift+Escape allow-inhibiting=false { toggle-keyboard-shortcuts-inhibit; } Mod+Shift+Slash { show-hotkey-overlay; } Mod+O repeat=false { toggle-overview; } - Super+Alt+S allow-when-locked=true hotkey-overlay-title=null { spawn-sh "pkill orca || exec orca"; } + // Super+Alt+S allow-when-locked=true hotkey-overlay-title=null { spawn-sh "pkill orca || exec orca"; } XF86AudioRaiseVolume allow-when-locked=true { spawn-sh "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.1+ -l 1.0"; } XF86AudioLowerVolume allow-when-locked=true { spawn-sh "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.1-"; } XF86AudioMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"; } XF86AudioMicMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"; } - XF86AudioPlay allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh play-pause"; } - XF86AudioStop allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh pause"; } - XF86AudioPrev allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh previous"; } - XF86AudioNext allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh next"; } + XF86AudioPlay allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl_active.sh play-pause"; } + XF86AudioStop allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl_active.sh pause"; } + XF86AudioPrev allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl_active.sh previous"; } + XF86AudioNext allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl_active.sh next"; } XF86MonBrightnessUp allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "+10%"; } XF86MonBrightnessDown allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "10%-"; } diff --git a/.config/scripts/shared/media_menu.sh b/.config/scripts/shared/media_menu.sh new file mode 100755 index 0000000..d87cfc8 --- /dev/null +++ b/.config/scripts/shared/media_menu.sh @@ -0,0 +1,103 @@ +#!/bin/bash +# Media picker (rofi): choose which MPRIS player the waybar music module follows. +# Auto (no pin file) follows the latest player; picking a player writes +# /tmp/playerctl_pin (line 1 bus name, 2 url host, 3 base name, 4 timestamp). +# Bound to middle-click on custom/music. + +PIN=/tmp/playerctl_pin + +notify() { + notify-send "$@" 2>/dev/null || true +} + +# host of an http(s) url, empty otherwise +host_of() { + local url=$1 + [[ $url =~ ^https?:// ]] || return 0 + sed -E 's#^[a-z]+://([^/]+).*#\1#' <<<"$url" +} + +# --- collect players ---------------------------------------------------------- + +players=() +while IFS= read -r p; do + [[ -n $p ]] && players+=("$p") +done < <(playerctl -l 2>/dev/null) + +if (( ${#players[@]} == 0 )); then + notify "No media players" + exit 0 +fi + +pinned="" +if [[ -f $PIN ]]; then + pinned=$(sed -n 1p "$PIN" 2>/dev/null) +fi + +# parallel arrays, index-aligned with players[] +titles=() +labels=() +places=() +entries=() + +if [[ -z $pinned ]]; then + entries+=("πŸ“Œ Auto (follow latest)") +else + entries+=("Auto (follow latest)") +fi + +for p in "${players[@]}"; do + status=$(playerctl -p "$p" status 2>/dev/null) + artist=$(playerctl -p "$p" metadata artist 2>/dev/null) + title=$(playerctl -p "$p" metadata title 2>/dev/null) + url=$(playerctl -p "$p" metadata xesam:url 2>/dev/null) + + case "$status" in + Playing) icon="β–Ά" ;; + Paused) icon="⏸" ;; + *) icon="⏹" ;; + esac + + if [[ -n $artist && -n $title ]]; then + label="$artist - $title" + elif [[ -n $title ]]; then + label=$title + else + label="(no title)" + fi + + host=$(host_of "$url") + base=${p%%.*} + place=${host:-$base} + + titles+=("${title:-(no title)}") + labels+=("$label") + places+=("$place") + + prefix="" + [[ $p == "$pinned" ]] && prefix="πŸ“Œ " + entries+=("${prefix}${icon} ${label} β€” ${place} [${p}]") +done + +# --- menu ------------------------------------------------------------------- + +idx=$(printf '%s\n' "${entries[@]}" | rofi -dmenu -i -p "Media" -format i) +[[ -z $idx ]] && exit 0 +[[ $idx =~ ^[0-9]+$ ]] || exit 0 + +if (( idx == 0 )); then + rm -f "$PIN" + notify "Media: auto" + exit 0 +fi + +(( idx <= ${#players[@]} )) || exit 0 +i=$((idx - 1)) +player=${players[i]} + +tmp=$(mktemp /tmp/playerctl_pin.XXXXXX) || exit 1 +host=$(host_of "$(playerctl -p "$player" metadata xesam:url 2>/dev/null)") +printf '%s\n%s\n%s\n%s\n' "$player" "$host" "${player%%.*}" "$(date +%s)" > "$tmp" +mv -f "$tmp" "$PIN" + +notify "Media pinned" "${titles[i]} β€” ${places[i]}" diff --git a/.config/scripts/shared/playerctl-active.sh b/.config/scripts/shared/playerctl-active.sh deleted file mode 100755 index 0d0fd05..0000000 --- a/.config/scripts/shared/playerctl-active.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/bash -# Forward playerctl commands to the ACTIVE player: the one currently Playing, -# else the most recently Playing (now Paused) one β€” the same selection -# song_detail_writer.sh makes for the waybar display, so clicks hit the -# player the bar is showing instead of the first in `playerctl -l` order. -# -# Usage: playerctl-active.sh play-pause|stop|next|previous|status|metadata ... - -ACTIVITY_PREFIX="/tmp/playerctl_activity_" -sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; } - -chosen="" -fallback="" -fallback_ts=0 -while IFS= read -r player; do - [[ -z "$player" ]] && continue - status=$(playerctl -p "$player" status 2>/dev/null) || continue - if [[ "$status" == "Playing" ]]; then - chosen="$player" - break - elif [[ "$status" == "Paused" ]]; then - ts=0 - act_file="${ACTIVITY_PREFIX}$(sanitize "$player")" - [[ -f "$act_file" ]] && ts=$(cat "$act_file" 2>/dev/null) && [[ -z "$ts" ]] && ts=0 - if (( ts > fallback_ts )); then - fallback_ts="$ts" - fallback="$player" - fi - fi -done < <(playerctl -l 2>/dev/null) - -chosen="${chosen:-$fallback}" -[[ -z "$chosen" ]] && exit 1 - -exec playerctl -p "$chosen" "$@" diff --git a/.config/scripts/shared/playerctl_active.sh b/.config/scripts/shared/playerctl_active.sh new file mode 100755 index 0000000..615f0c4 --- /dev/null +++ b/.config/scripts/shared/playerctl_active.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# Forward playerctl commands to the player the bar is showing, so clicks hit +# what song_detail_writer.sh displays instead of the first in `playerctl -l`. +# Target order: +# 1. /tmp/song_detail_player (the writer's current player), if still listed +# 2. pin file line 1 (/tmp/playerctl_pin), if still listed +# 3. Playing player with the most recent start (/tmp/playerctl_started_*) +# 4. Paused player with the latest activity (/tmp/playerctl_activity_*) +# +# Usage: playerctl_active.sh play-pause|stop|next|previous|status|metadata ... + +ACTIVITY_PREFIX="/tmp/playerctl_activity_" +STARTED_PREFIX="/tmp/playerctl_started_" +CHOSEN_FILE="/tmp/song_detail_player" +PIN_FILE="/tmp/playerctl_pin" +sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; } + +list=$(playerctl -l 2>/dev/null) +in_list() { grep -qxF -- "$1" <<< "$list"; } + +chosen="" +if [[ -s "$CHOSEN_FILE" ]]; then + cand=$(head -n 1 "$CHOSEN_FILE") + [[ -n "$cand" ]] && in_list "$cand" && chosen="$cand" +fi +if [[ -z "$chosen" && -f "$PIN_FILE" ]]; then + cand=$(head -n 1 "$PIN_FILE") + [[ -n "$cand" ]] && in_list "$cand" && chosen="$cand" +fi + +if [[ -z "$chosen" ]]; then + best_playing="" + best_playing_ts=-1 + fallback="" + fallback_ts=0 + while IFS= read -r player; do + [[ -z "$player" ]] && continue + status=$(playerctl -p "$player" status 2>/dev/null) || continue + if [[ "$status" == "Playing" ]]; then + started=$(cat "${STARTED_PREFIX}$(sanitize "$player")" 2>/dev/null) + [[ "$started" =~ ^[0-9]+$ ]] || started=0 + if (( started > best_playing_ts )); then + best_playing_ts="$started" + best_playing="$player" + fi + elif [[ "$status" == "Paused" ]]; then + ts=0 + act_file="${ACTIVITY_PREFIX}$(sanitize "$player")" + [[ -f "$act_file" ]] && ts=$(cat "$act_file" 2>/dev/null) && [[ -z "$ts" ]] && ts=0 + if (( ts > fallback_ts )); then + fallback_ts="$ts" + fallback="$player" + fi + fi + done <<< "$list" + chosen="${best_playing:-$fallback}" +fi + +[[ -z "$chosen" ]] && exit 1 + +exec playerctl -p "$chosen" "$@" diff --git a/.config/scripts/shared/show_cover.sh b/.config/scripts/shared/show_cover.sh index a3efa92..c9a5c92 100755 --- a/.config/scripts/shared/show_cover.sh +++ b/.config/scripts/shared/show_cover.sh @@ -1,7 +1,7 @@ #!/bin/bash # Get cover art URL from playerctl -cover_url=$(bash ~/.config/scripts/shared/playerctl-active.sh metadata mpris:artUrl 2>/dev/null) +cover_url=$(bash ~/.config/scripts/shared/playerctl_active.sh metadata mpris:artUrl 2>/dev/null) if [[ -z "$cover_url" ]]; then notify-send "No cover art available" diff --git a/.config/scripts/shared/song_detail_writer.sh b/.config/scripts/shared/song_detail_writer.sh index 169c817..6c5ab01 100755 --- a/.config/scripts/shared/song_detail_writer.sh +++ b/.config/scripts/shared/song_detail_writer.sh @@ -8,16 +8,25 @@ # the file each interval always see the live line. Whatever was in the file # before β€” including a leftover in-place log from an older build β€” is dropped. # +# Pin mode: /tmp/playerctl_pin (line 1 = bus name, 2 = url host, 3 = base name, +# 4 = pinned epoch) keeps the display on one player. While pinned the line gets +# a πŸ“Œ prefix and holds the last good line through short gaps. +# /tmp/song_detail_player always holds the player currently shown. +# # niri spawn-at-startup re-runs on compositor restart and used to stack # copies; newest instance takes over (singleton below). OUTPUT_FILE="/tmp/song_detail_last" +CHOSEN_FILE="/tmp/song_detail_player" +PIN_FILE="/tmp/playerctl_pin" POSITION_CACHE_PREFIX="/tmp/playerctl_position_cache_" ACTIVITY_PREFIX="/tmp/playerctl_activity_" +STARTED_PREFIX="/tmp/playerctl_started_" LOCK_FILE="/tmp/song_detail_writer.lock" PID_FILE="/tmp/song_detail_writer.pid" INTERVAL=1 EMPTY_GRACE=3 +PIN_GRACE=300 takeover_singleton() { local pid @@ -49,6 +58,8 @@ atomic_write() { printf '%s\n' "$content" > "$tmp" && mv -f "$tmp" "$target" } +sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; } + # Current line only. Truncate in place (same inode, same path) so a reader # that re-reads the file every interval gets the new content and never sees # a pile of old lines. Empty state = empty file. @@ -60,10 +71,26 @@ write_output() { fi } +# Written only when the shown player changes. Empty file = nothing shown. +last_chosen="__init__" +write_chosen() { + [[ "$1" == "$last_chosen" ]] && return + if [[ -z "$1" ]]; then + : > "$CHOSEN_FILE" + else + atomic_write "$CHOSEN_FILE" "$1" + fi && last_chosen="$1" +} + last_emit="__init__" empty_streak=0 last_position="" last_track="" +declare -A prev_status=() +pin_missing_since=0 +pin_noinfo_since=0 +last_good_text="" +text_owner="" emit() { local out="$1" @@ -87,6 +114,20 @@ pango_escape() { printf '%s' "$s" } +# Url host of a player's current page; empty for non-http(s) or no url. +host_of() { + local url + url=$(playerctl -p "$1" metadata xesam:url 2>/dev/null) + case "$url" in + http://*|https://*) printf '%s' "$url" | sed -E 's#^[a-z]+://([^/]+).*#\1#' ;; + esac +} + +# Args: name host base epoch +write_pin() { + atomic_write "$PIN_FILE" "$(printf '%s\n%s\n%s\n%s' "$1" "$2" "$3" "$4")" +} + # File must exist for readers. Carry over at most the current line β€” any # accumulated log from an older append-mode build is dropped here. if [[ -s "$OUTPUT_FILE" ]]; then @@ -94,71 +135,160 @@ if [[ -s "$OUTPUT_FILE" ]]; then else : > "$OUTPUT_FILE" fi +[[ -f "$CHOSEN_FILE" ]] || : > "$CHOSEN_FILE" -while true; do - if ! command -v playerctl &>/dev/null; then - emit "" - sleep "$INTERVAL" - continue +# Sets globals: out (line to show), chosen (player shown). +# Seconds -> MM:SS, or H:MM:SS once past an hour. +fmt_time() { + local t=$1 + if (( t >= 3600 )); then + printf "%d:%02d:%02d" $((t / 3600)) $((t % 3600 / 60)) $((t % 60)) + else + printf "%02d:%02d" $((t / 60)) $((t % 60)) fi +} - player_list=$(playerctl -l 2>/dev/null) - if [[ -z "$player_list" ]]; then - emit "" - sleep "$INTERVAL" - continue +tick() { + out="" + chosen="" + local now player_list player player_status started ts act_file info + local status artist title duration_us position_us duration body + local best_playing="" best_playing_ts=-1 best_paused="" best_paused_ts=0 + local pin_name="" pin_host="" pin_base="" pin_time="" pin_mode=0 pin_lines=() + local cand cur_host icon position position_sec pos_minutes pos_seconds + local duration_sec minutes seconds track_id POSITION_CACHE + local -A status_of=() + + if ! command -v playerctl &>/dev/null; then + return fi now=$(date +%s) - sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; } + player_list=$(playerctl -l 2>/dev/null) - best_playing="" - best_playing_ts=0 - best_paused="" - best_paused_ts=0 + # Status pass: record status, mark Playing transitions, bump activity. while IFS= read -r player; do [[ -z "$player" ]] && continue player_status=$(playerctl -p "$player" status 2>/dev/null) - act_file="${ACTIVITY_PREFIX}$(sanitize "$player")" + status_of["$player"]="$player_status" 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" + if [[ "${prev_status[$player]:-}" != "Playing" ]]; then + atomic_write "${STARTED_PREFIX}$(sanitize "$player")" "$(date +%s%N)" fi + atomic_write "${ACTIVITY_PREFIX}$(sanitize "$player")" "$now" fi + prev_status["$player"]="$player_status" done <<< "$player_list" - chosen="${best_playing:-$best_paused}" + # Forget players that vanished, so a reappearing player counts as a new start. + for p in "${!prev_status[@]}"; do + [[ -n "${status_of[$p]+x}" ]] || unset "prev_status[$p]" + done - if [[ -z "$chosen" ]]; then - emit "" - sleep "$INTERVAL" - continue + # Pin file, if any with a non-empty line 1. + if [[ -f "$PIN_FILE" ]]; then + mapfile -t pin_lines < "$PIN_FILE" + pin_name="${pin_lines[0]:-}" + pin_host="${pin_lines[1]:-}" + pin_base="${pin_lines[2]:-}" + pin_time="${pin_lines[3]:-$now}" + [[ -z "$pin_base" ]] && pin_base="${pin_name%%.*}" + fi + + if [[ -n "$pin_name" ]]; then + pin_mode=1 + # New pin target: drop the held text of the previous one. + if [[ "$pin_name" != "$text_owner" ]]; then + last_good_text="" + text_owner="$pin_name" + fi + + if [[ -n "${status_of[$pin_name]+x}" ]]; then + # Pinned player is listed: it wins over everything else. + pin_missing_since=0 + chosen="$pin_name" + cur_host=$(host_of "$pin_name") + if [[ -n "$cur_host" && "$cur_host" != "$pin_host" ]]; then + pin_host="$cur_host" + write_pin "$pin_name" "$pin_host" "$pin_base" "$pin_time" + fi + else + # Re-attach: same base name, and same host when one is pinned. + cand="" + while IFS= read -r player; do + [[ -z "$player" || "${player%%.*}" != "$pin_base" ]] && continue + if [[ -n "$pin_host" && "$(host_of "$player")" != "$pin_host" ]]; then + continue + fi + if [[ "${status_of[$player]}" == "Playing" ]]; then + cand="$player" + break + fi + [[ -z "$cand" ]] && cand="$player" + done <<< "$player_list" + + if [[ -n "$cand" ]]; then + pin_name="$cand" + text_owner="$cand" + pin_missing_since=0 + write_pin "$pin_name" "$pin_host" "$pin_base" "$pin_time" + chosen="$pin_name" + else + [[ $pin_missing_since -eq 0 ]] && pin_missing_since="$now" + if (( now - pin_missing_since < PIN_GRACE )); then + out="πŸ“Œ ⏳ ${last_good_text:-Loading…}" + return + fi + rm -f "$PIN_FILE" + notify-send "Media pin released" "$pin_name disappeared" 2>/dev/null || true + pin_mode=0 + pin_missing_since=0 + fi + fi fi - info=$(playerctl -p "$chosen" metadata --format '{{status}} {{artist}} {{title}} {{mpris:length}} {{position}}' 2>/dev/null) - if [[ -z "$info" ]]; then - emit "" - sleep "$INTERVAL" - continue + if (( pin_mode == 0 )); then + # Auto: most recently started Playing player, else latest Paused. + while IFS= read -r player; do + [[ -z "$player" ]] && continue + player_status="${status_of[$player]}" + if [[ "$player_status" == "Playing" ]]; then + started=$(cat "${STARTED_PREFIX}$(sanitize "$player")" 2>/dev/null) + [[ "$started" =~ ^[0-9]+$ ]] || started=0 + if (( started > best_playing_ts )); then + best_playing_ts="$started" + best_playing="$player" + fi + elif [[ "$player_status" == "Paused" ]]; then + ts=0 + act_file="${ACTIVITY_PREFIX}$(sanitize "$player")" + [[ -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}" + [[ -z "$chosen" ]] && return fi - IFS=$'\t' read -r status artist title duration_us position_us <<< "$info" + # \x1f (unit separator), not tab: tab is IFS whitespace, so an empty + # artist would collapse and shift every later field left. + info=$(playerctl -p "$chosen" metadata --format $'{{status}}\x1f{{artist}}\x1f{{title}}\x1f{{mpris:length}}\x1f{{position}}' 2>/dev/null) + IFS=$'\x1f' read -r status artist title duration_us position_us <<< "$info" - if [[ "$status" == "Stopped" ]]; then - emit "" - sleep "$INTERVAL" - continue + if [[ -z "$info" || "$status" == "Stopped" ]]; then + # Pinned player with no info (slow track change): hold for a while. + if (( pin_mode )); then + [[ $pin_noinfo_since -eq 0 ]] && pin_noinfo_since="$now" + if (( now - pin_noinfo_since < PIN_GRACE )); then + out="πŸ“Œ ⏳ ${last_good_text:-Loading…}" + fi + fi + return fi + pin_noinfo_since=0 if [[ "$status" == "Playing" ]]; then icon="β–Ά" @@ -168,10 +298,7 @@ while true; do duration="" if [[ "$duration_us" =~ ^[0-9]+$ ]]; then - duration_sec=$((duration_us / 1000000)) - minutes=$((duration_sec / 60)) - seconds=$((duration_sec % 60)) - duration=$(printf "%02d:%02d" $minutes $seconds) + duration=$(fmt_time $((duration_us / 1000000))) fi POSITION_CACHE="${POSITION_CACHE_PREFIX}$(sanitize "$chosen")" @@ -183,10 +310,7 @@ while true; do fi if [[ "$status" == "Playing" ]]; then if [[ "$position_us" =~ ^[0-9]+$ ]]; then - position_sec=$((position_us / 1000000)) - pos_minutes=$((position_sec / 60)) - pos_seconds=$((position_sec % 60)) - position=$(printf "%02d:%02d" $pos_minutes $pos_seconds) + position=$(fmt_time $((position_us / 1000000))) atomic_write "$POSITION_CACHE" "$position" last_position="$position" elif [[ -n "$last_position" ]]; then @@ -202,21 +326,32 @@ while true; do title=$(pango_escape "$title") if [[ -n "$artist" && -n "$title" && -n "$duration" && -n "$position" ]]; then - out="$icon $artist - $title ($position / $duration)" + body="$artist - $title ($position / $duration)" elif [[ -n "$artist" && -n "$title" && -n "$duration" ]]; then - out="$icon $artist - $title ($duration)" + body="$artist - $title ($duration)" elif [[ -n "$title" && -n "$duration" && -n "$position" ]]; then - out="$icon $title ($position / $duration)" + body="$title ($position / $duration)" elif [[ -n "$title" && -n "$duration" ]]; then - out="$icon $title ($duration)" + body="$title ($duration)" elif [[ -n "$artist" && -n "$title" ]]; then - out="$icon $artist - $title" + body="$artist - $title" elif [[ -n "$title" ]]; then - out="$icon $title" + body="$title" else - out="$icon Something's Playing" + body="Something's Playing" fi + if (( pin_mode )); then + last_good_text="$body" + out="πŸ“Œ $icon $body" + else + out="$icon $body" + fi +} + +while true; do + tick + write_chosen "$chosen" emit "$out" sleep "$INTERVAL" done diff --git a/.config/scripts/shared/webapp.sh b/.config/scripts/shared/webapp.sh index b22fc4f..ee8df17 100755 --- a/.config/scripts/shared/webapp.sh +++ b/.config/scripts/shared/webapp.sh @@ -31,7 +31,14 @@ HOST="${URL#*://}" HOST="${HOST%%/*}" CLASS="chrome-${HOST}__-Default" -cmd=(flatpak run "$BROWSER_ID" +# Chromium names its MPRIS player chromium.instance, and inside a flatpak +# sandbox the browser is always pid 2 β€” so this app, the Chromium flatpak and +# FreeTube all fight over chromium.instance2 and only one shows in playerctl. +# Running chromium as a child of a sandbox shell moves it to pid 3, giving the +# web apps their own player name. The trailing `exit` stops bash from exec'ing +# a lone command (which would keep chromium at pid 2). +cmd=(flatpak run --command=bash "$BROWSER_ID" + -c 'chromium "$@"; exit $?' bash --no-first-run --app="$URL" --class="$CLASS" diff --git a/.config/waybar/config-hyprland b/.config/waybar/config-hyprland index 289c113..41d2448 100644 --- a/.config/waybar/config-hyprland +++ b/.config/waybar/config-hyprland @@ -81,8 +81,9 @@ "format": "{}", "exec": "cat /tmp/song_detail_last 2>/dev/null", "interval": 1, - "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", + "on-click": "bash ~/.config/scripts/shared/playerctl_active.sh play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", + "on-click-middle": "bash ~/.config/scripts/shared/media_menu.sh", "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" }, diff --git a/.config/waybar/config-niri b/.config/waybar/config-niri index e883c62..b5d98f6 100644 --- a/.config/waybar/config-niri +++ b/.config/waybar/config-niri @@ -81,8 +81,9 @@ "format": "{}", "exec": "cat /tmp/song_detail_last 2>/dev/null", "interval": 1, - "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", + "on-click": "bash ~/.config/scripts/shared/playerctl_active.sh play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", + "on-click-middle": "bash ~/.config/scripts/shared/media_menu.sh", "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" },