media keys: target the playing player via playerctl-active wrapper
Browse files at this commitb03390bf741e1362eec12d4e22f1bf85f7babd91Parent: 386f14d7 changed files +47 -12
- .config/eww/eww.yuck +1 -1
- .config/hypr/hyprland.conf +4 -4
- .config/niri/config.kdl +4 -4
- .config/scripts/shared/playerctl-active.sh +35 -0
- .config/scripts/shared/show_cover.sh +1 -1
- .config/waybar/config +1 -1
- .config/waybar/config-niri +1 -1
.config/eww/eww.yuckmodified+1 -1
| @@ -96,7 +96,7 @@ | ||
| 96 | 96 | |
| 97 | 97 | ;; Music widget - click to play/pause, right-click to show cover |
| 98 | 98 | (defwidget music-widget [] |
| 99 | - (eventbox :onclick "playerctl play-pause" | |
| 99 | + (eventbox :onclick "bash ~/.config/scripts/shared/playerctl-active.sh play-pause" | |
| 100 | 100 | :onrightclick "bash ~/.config/scripts/shared/show_cover.sh" |
| 101 | 101 | :class "music-box" |
| 102 | 102 | (label :text music))) |
.config/hypr/hyprland.confmodified+4 -4
| @@ -168,10 +168,10 @@ bindel = ,XF86AudioMicMute, exec, wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle | ||
| 168 | 168 | bindel = ,XF86MonBrightnessUp, exec, brightnessctl s 10%+ |
| 169 | 169 | bindel = ,XF86MonBrightnessDown, exec, brightnessctl s 10%- |
| 170 | 170 | |
| 171 | -bindl = , XF86AudioNext, exec, playerctl next | |
| 172 | -bindl = , XF86AudioPause, exec, playerctl play-pause | |
| 173 | -bindl = , XF86AudioPlay, exec, playerctl play-pause | |
| 174 | -bindl = , XF86AudioPrev, exec, playerctl previous | |
| 171 | +bindl = , XF86AudioNext, exec, ~/.config/scripts/shared/playerctl-active.sh next | |
| 172 | +bindl = , XF86AudioPause, exec, ~/.config/scripts/shared/playerctl-active.sh play-pause | |
| 173 | +bindl = , XF86AudioPlay, exec, ~/.config/scripts/shared/playerctl-active.sh play-pause | |
| 174 | +bindl = , XF86AudioPrev, exec, ~/.config/scripts/shared/playerctl-active.sh previous | |
| 175 | 175 | |
| 176 | 176 | windowrule = match:class .*, suppress_event maximize |
| 177 | 177 | windowrule = match:class ^$, match:title ^$, match:xwayland 1, match:float 1, match:fullscreen 0, match:pin 0, no_focus on |
.config/niri/config.kdlmodified+4 -4
| @@ -208,10 +208,10 @@ binds { | ||
| 208 | 208 | XF86AudioLowerVolume allow-when-locked=true { spawn-sh "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.1-"; } |
| 209 | 209 | XF86AudioMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"; } |
| 210 | 210 | XF86AudioMicMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"; } |
| 211 | - XF86AudioPlay allow-when-locked=true { spawn-sh "playerctl play-pause"; } | |
| 212 | - XF86AudioStop allow-when-locked=true { spawn-sh "playerctl stop"; } | |
| 213 | - XF86AudioPrev allow-when-locked=true { spawn-sh "playerctl previous"; } | |
| 214 | - XF86AudioNext allow-when-locked=true { spawn-sh "playerctl next"; } | |
| 211 | + XF86AudioPlay allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh play-pause"; } | |
| 212 | + XF86AudioStop allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh stop"; } | |
| 213 | + XF86AudioPrev allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh previous"; } | |
| 214 | + XF86AudioNext allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh next"; } | |
| 215 | 215 | XF86MonBrightnessUp allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "+10%"; } |
| 216 | 216 | XF86MonBrightnessDown allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "10%-"; } |
| 217 | 217 |
.config/scripts/shared/playerctl-active.shadded+35 -0
| @@ -0,0 +1,35 @@ | ||
| 1 | +#!/bin/bash | |
| 2 | +# Forward playerctl commands to the ACTIVE player: the one currently Playing, | |
| 3 | +# else the most recently Playing (now Paused) one — the same selection | |
| 4 | +# song_detail_writer.sh makes for the waybar display, so clicks hit the | |
| 5 | +# player the bar is showing instead of the first in `playerctl -l` order. | |
| 6 | +# | |
| 7 | +# Usage: playerctl-active.sh play-pause|stop|next|previous|status|metadata ... | |
| 8 | + | |
| 9 | +ACTIVITY_PREFIX="/tmp/playerctl_activity_" | |
| 10 | +sanitize() { printf '%s' "$1" | tr -c 'A-Za-z0-9._-' '_'; } | |
| 11 | + | |
| 12 | +chosen="" | |
| 13 | +fallback="" | |
| 14 | +fallback_ts=0 | |
| 15 | +while IFS= read -r player; do | |
| 16 | + [[ -z "$player" ]] && continue | |
| 17 | + status=$(playerctl -p "$player" status 2>/dev/null) || continue | |
| 18 | + if [[ "$status" == "Playing" ]]; then | |
| 19 | + chosen="$player" | |
| 20 | + break | |
| 21 | + elif [[ "$status" == "Paused" ]]; then | |
| 22 | + ts=0 | |
| 23 | + act_file="${ACTIVITY_PREFIX}$(sanitize "$player")" | |
| 24 | + [[ -f "$act_file" ]] && ts=$(cat "$act_file" 2>/dev/null) && [[ -z "$ts" ]] && ts=0 | |
| 25 | + if (( ts > fallback_ts )); then | |
| 26 | + fallback_ts="$ts" | |
| 27 | + fallback="$player" | |
| 28 | + fi | |
| 29 | + fi | |
| 30 | +done < <(playerctl -l 2>/dev/null) | |
| 31 | + | |
| 32 | +chosen="${chosen:-$fallback}" | |
| 33 | +[[ -z "$chosen" ]] && exit 1 | |
| 34 | + | |
| 35 | +exec playerctl -p "$chosen" "$@" |
.config/scripts/shared/show_cover.shmodified+1 -1
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | #!/bin/bash |
| 2 | 2 | |
| 3 | 3 | # Get cover art URL from playerctl |
| 4 | -cover_url=$(playerctl metadata mpris:artUrl 2>/dev/null) | |
| 4 | +cover_url=$(bash ~/.config/scripts/shared/playerctl-active.sh metadata mpris:artUrl 2>/dev/null) | |
| 5 | 5 | |
| 6 | 6 | if [[ -z "$cover_url" ]]; then |
| 7 | 7 | notify-send "No cover art available" |
.config/waybar/configmodified+1 -1
| @@ -80,7 +80,7 @@ | ||
| 80 | 80 | "custom/music": { |
| 81 | 81 | "format": "{}", |
| 82 | 82 | "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", |
| 83 | - "on-click": "playerctl play-pause", | |
| 83 | + "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", | |
| 84 | 84 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", |
| 85 | 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", |
| 86 | 86 | "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" |
.config/waybar/config-nirimodified+1 -1
| @@ -80,7 +80,7 @@ | ||
| 80 | 80 | "custom/music": { |
| 81 | 81 | "format": "{}", |
| 82 | 82 | "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", |
| 83 | - "on-click": "playerctl play-pause", | |
| 83 | + "on-click": "bash ~/.config/scripts/shared/playerctl-active.sh play-pause", | |
| 84 | 84 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", |
| 85 | 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", |
| 86 | 86 | "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" |