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 | ;; Music widget - click to play/pause, right-click to show cover | 97 | ;; Music widget - click to play/pause, right-click to show cover |
| 98 | (defwidget music-widget [] | 98 | (defwidget music-widget [] |
| 99 | - (eventbox :onclick "playerctl play-pause" | 99 | + (eventbox :onclick "bash ~/.config/scripts/shared/playerctl-active.sh play-pause" |
| 100 | :onrightclick "bash ~/.config/scripts/shared/show_cover.sh" | 100 | :onrightclick "bash ~/.config/scripts/shared/show_cover.sh" |
| 101 | :class "music-box" | 101 | :class "music-box" |
| 102 | (label :text music))) | 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 | bindel = ,XF86MonBrightnessUp, exec, brightnessctl s 10%+ | 168 | bindel = ,XF86MonBrightnessUp, exec, brightnessctl s 10%+ |
| 169 | bindel = ,XF86MonBrightnessDown, exec, brightnessctl s 10%- | 169 | bindel = ,XF86MonBrightnessDown, exec, brightnessctl s 10%- |
| 170 | 170 | ||
| 171 | -bindl = , XF86AudioNext, exec, playerctl next | 171 | +bindl = , XF86AudioNext, exec, ~/.config/scripts/shared/playerctl-active.sh next |
| 172 | -bindl = , XF86AudioPause, exec, playerctl play-pause | 172 | +bindl = , XF86AudioPause, exec, ~/.config/scripts/shared/playerctl-active.sh play-pause |
| 173 | -bindl = , XF86AudioPlay, exec, playerctl play-pause | 173 | +bindl = , XF86AudioPlay, exec, ~/.config/scripts/shared/playerctl-active.sh play-pause |
| 174 | -bindl = , XF86AudioPrev, exec, playerctl previous | 174 | +bindl = , XF86AudioPrev, exec, ~/.config/scripts/shared/playerctl-active.sh previous |
| 175 | 175 | ||
| 176 | windowrule = match:class .*, suppress_event maximize | 176 | windowrule = match:class .*, suppress_event maximize |
| 177 | windowrule = match:class ^$, match:title ^$, match:xwayland 1, match:float 1, match:fullscreen 0, match:pin 0, no_focus on | 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 | XF86AudioLowerVolume allow-when-locked=true { spawn-sh "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.1-"; } | 208 | XF86AudioLowerVolume allow-when-locked=true { spawn-sh "wpctl set-volume @DEFAULT_AUDIO_SINK@ 0.1-"; } |
| 209 | XF86AudioMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"; } | 209 | XF86AudioMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle"; } |
| 210 | XF86AudioMicMute allow-when-locked=true { spawn-sh "wpctl set-mute @DEFAULT_AUDIO_SOURCE@ toggle"; } | 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"; } | 211 | + XF86AudioPlay allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh play-pause"; } |
| 212 | - XF86AudioStop allow-when-locked=true { spawn-sh "playerctl stop"; } | 212 | + XF86AudioStop allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh stop"; } |
| 213 | - XF86AudioPrev allow-when-locked=true { spawn-sh "playerctl previous"; } | 213 | + XF86AudioPrev allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh previous"; } |
| 214 | - XF86AudioNext allow-when-locked=true { spawn-sh "playerctl next"; } | 214 | + XF86AudioNext allow-when-locked=true { spawn-sh "~/.config/scripts/shared/playerctl-active.sh next"; } |
| 215 | XF86MonBrightnessUp allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "+10%"; } | 215 | XF86MonBrightnessUp allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "+10%"; } |
| 216 | XF86MonBrightnessDown allow-when-locked=true { spawn "brightnessctl" "--class=backlight" "set" "10%-"; } | 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 | #!/bin/bash | 1 | #!/bin/bash |
| 2 | 2 | ||
| 3 | # Get cover art URL from playerctl | 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 | if [[ -z "$cover_url" ]]; then | 6 | if [[ -z "$cover_url" ]]; then |
| 7 | notify-send "No cover art available" | 7 | notify-send "No cover art available" |
.config/waybar/configmodified+1 -1
| @@ -80,7 +80,7 @@ | |||
| 80 | "custom/music": { | 80 | "custom/music": { |
| 81 | "format": "{}", | 81 | "format": "{}", |
| 82 | "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", | 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 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", | 84 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", |
| 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", | 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", |
| 86 | "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" | 86 | "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" |
.config/waybar/config-nirimodified+1 -1
| @@ -80,7 +80,7 @@ | |||
| 80 | "custom/music": { | 80 | "custom/music": { |
| 81 | "format": "{}", | 81 | "format": "{}", |
| 82 | "exec": "tail -n 1 -F /tmp/song_detail_last 2>/dev/null", | 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 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", | 84 | "on-click-right": "bash ~/.config/scripts/shared/show_cover.sh", |
| 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", | 85 | "on-scroll-up": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%+", |
| 86 | "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" | 86 | "on-scroll-down": "wpctl set-volume @DEFAULT_AUDIO_SINK@ 5%-" |