From b03390bf741e1362eec12d4e22f1bf85f7babd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 23 Sep 2026 15:42:06 +0300 Subject: [PATCH] media keys: target the playing player via playerctl-active wrapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bare playerctl picks the first MPRIS player (chromium/Deezer), while song_detail_writer displays the Playing one (mpv) — clicks hit the wrong player. New shared script selects the Playing player, else most recently active Paused, mirroring the writer's logic. Waybar (both bars), niri, hyprland, eww, and show_cover now route through it. --- .config/eww/eww.yuck | 2 +- .config/hypr/hyprland.conf | 8 ++--- .config/niri/config.kdl | 8 ++--- .config/scripts/shared/playerctl-active.sh | 35 ++++++++++++++++++++++ .config/scripts/shared/show_cover.sh | 2 +- .config/waybar/config | 2 +- .config/waybar/config-niri | 2 +- 7 files changed, 47 insertions(+), 12 deletions(-) create mode 100755 .config/scripts/shared/playerctl-active.sh diff --git a/.config/eww/eww.yuck b/.config/eww/eww.yuck index 6b06057..75c4acc 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 "playerctl 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 25ed246..e9374ea 100644 --- a/.config/hypr/hyprland.conf +++ b/.config/hypr/hyprland.conf @@ -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, playerctl next -bindl = , XF86AudioPause, exec, playerctl play-pause -bindl = , XF86AudioPlay, exec, playerctl play-pause -bindl = , XF86AudioPrev, exec, playerctl 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/niri/config.kdl b/.config/niri/config.kdl index 81aaa51..468c268 100644 --- a/.config/niri/config.kdl +++ b/.config/niri/config.kdl @@ -208,10 +208,10 @@ binds { 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 "playerctl play-pause"; } - XF86AudioStop allow-when-locked=true { spawn-sh "playerctl stop"; } - XF86AudioPrev allow-when-locked=true { spawn-sh "playerctl previous"; } - XF86AudioNext allow-when-locked=true { spawn-sh "playerctl 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 stop"; } + 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/playerctl-active.sh b/.config/scripts/shared/playerctl-active.sh new file mode 100755 index 0000000..0d0fd05 --- /dev/null +++ b/.config/scripts/shared/playerctl-active.sh @@ -0,0 +1,35 @@ +#!/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/show_cover.sh b/.config/scripts/shared/show_cover.sh index 2290b29..a3efa92 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=$(playerctl 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/waybar/config b/.config/waybar/config index 7cf6482..32a9164 100644 --- a/.config/waybar/config +++ b/.config/waybar/config @@ -80,7 +80,7 @@ "custom/music": { "format": "{}", "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", - "on-click": "playerctl play-pause", + "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.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 c7d90fa..c3f1279 100644 --- a/.config/waybar/config-niri +++ b/.config/waybar/config-niri @@ -80,7 +80,7 @@ "custom/music": { "format": "{}", "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", - "on-click": "playerctl play-pause", + "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-"