sway: waybar: fix media to better handle multiple players

This commit is contained in:
Colin 2023-09-10 00:01:39 +00:00
parent 755f844294
commit fad3972554
5 changed files with 69 additions and 22 deletions

View File

@ -45,6 +45,7 @@
./nix-index.nix
./obsidian.nix
./offlineimap.nix
./playerctl.nix
./rhythmbox.nix
./ripgrep.nix
./sfeed.nix

View File

@ -0,0 +1,15 @@
{ config, lib, pkgs, ... }:
{
# TODO: give `sane.programs` native support for defining services
systemd.user.services.playerctld = lib.mkIf config.sane.programs.playerctl.enabled {
description = "playerctl daemon to keep track of which MPRIS players were recently active";
documentation = [ "https://github.com/altdesktop/playerctl/issues/161" ];
wantedBy = [ "default.target" ];
serviceConfig.ExecStart = "${config.sane.programs.playerctl.package}/bin/playerctld";
# serviceConfig.Type = "dbus";
# serviceConfig.BusName = "org.mpris.MediaPlayer2.Player";
serviceConfig.Type = "simple"; # playerctl also supports a --daemon option, idk if that's better
serviceConfig.Restart = "on-failure";
serviceConfig.RestartSec = "10s";
};
}

View File

@ -171,6 +171,7 @@ in
"swayidle"
"wl-clipboard"
"blueberry" # GUI bluetooth manager
"playerctl" # for waybar & particularly to have playerctld running
# "mako" # notification daemon
"swaynotificationcenter" # notification daemon
# # "pavucontrol"

View File

@ -0,0 +1,17 @@
#!/usr/bin/env nix-shell
#!nix-shell -i bash -p jq -p playerctl
status=$(playerctl status 2> /dev/null | tr 'A-Z' 'a-z')
if [ -z "$status" ]; then
status="inactive"
fi
artist=$(playerctl metadata artist 2> /dev/null)
title=$(playerctl metadata title 2> /dev/null)
text=
if [ -n "$title" ]; then
text="$artist - $title"
fi
# waybar requires output to be on a single line.
# `alt` key determines the icon
jq --null-input --compact-output --arg status "$status" --arg text "$text" '{ "text": $text, "alt": $status }'

View File

@ -1,6 +1,14 @@
# docs: <https://github.com/Alexays/Waybar/wiki/Configuration>
# format specifiers: <https://fmt.dev/latest/syntax.html#syntax>
# - custom modules: <https://github.com/Alexays/Waybar/wiki/Module:-Custom>
# - format specifiers: <https://fmt.dev/latest/syntax.html#syntax>
{ lib, pkgs }:
let
waybar-media = pkgs.static-nix-shell.mkBash {
pname = "waybar-media";
src = ./.;
pkgs = [ "jq" "playerctl" ];
};
in
{
height = lib.mkDefault 40;
modules-left = lib.mkDefault [ "sway/workspaces" ];
@ -19,28 +27,36 @@
max-length = 50;
};
# include song artist/title.
# source: <https://www.reddit.com/r/swaywm/comments/ni0vso/waybar_spotify_tracktitle/>
"custom/media" = {
exec = pkgs.writeShellScript "waybar-mediaplayer" ''
player_status=$(${pkgs.playerctl}/bin/playerctl status 2> /dev/null)
if [ "$player_status" = "Playing" ]; then
echo " $(${pkgs.playerctl}/bin/playerctl metadata artist) - $(${pkgs.playerctl}/bin/playerctl metadata title)"
elif [ "$player_status" = "Paused" ]; then
echo " $(${pkgs.playerctl}/bin/playerctl metadata artist) - $(${pkgs.playerctl}/bin/playerctl metadata title)"
fi
'';
# this module shows the actively playing song
# - source: <https://www.reddit.com/r/swaywm/comments/ni0vso/waybar_spotify_tracktitle/>
# - alternative: <https://github.com/Alexays/Waybar/wiki/Module:-MPRIS>
# - alternative: <https://github.com/Alexays/Waybar/wiki/Module:-Custom#mpris-controller>
#
# N.B.: for this to behave well with multiple MPRIS clients,
# `playerctld` must be enabled. see: <https://github.com/altdesktop/playerctl/issues/161>
exec = "${waybar-media}/bin/waybar-media";
return-type = "json";
interval = 2;
format = "{}";
# return-type = "json";
on-click = "${pkgs.playerctl}/bin/playerctl play-pause";
on-scroll-up = "${pkgs.playerctl}/bin/playerctl next";
on-scroll-down = "${pkgs.playerctl}/bin/playerctl previous";
format = "{icon}{}";
max-length = 50;
format-icons = {
playing = " ";
paused = " ";
inactive = "";
};
tooltip = false;
on-click = "playerctl play-pause";
on-scroll-up = "playerctl next";
on-scroll-down = "playerctl previous";
};
"custom/swaync" = {
# source: <https://github.com/ErikReider/SwayNotificationCenter#waybar-example>
tooltip = false;
format = "{icon}"; # or "{icon} {}" to inclde notif count
exec-if = "which swaync-client";
exec = "swaync-client -swb";
return-type = "json";
escape = true;
format = "{icon}"; # or "{icon} {}" to include notif count
format-icons = {
notification = "<span foreground='red'><sup></sup></span>";
none = "";
@ -51,12 +67,9 @@
dnd-inhibited-notification = "<span foreground='red'><sup></sup></span>";
dnd-inhibited-none = "";
};
return-type = "json";
exec-if = "which swaync-client";
exec = "swaync-client -swb";
tooltip = false;
on-click = "swaync-client -t -sw";
on-click-right = "swaync-client -d -sw";
escape = true;
};
network = {
# docs: <https://github.com/Alexays/Waybar/blob/master/man/waybar-network.5.scd>