X-Git-Url: https://git.draconx.ca/gitweb/mpdhacks.git/blobdiff_plain/82b8d686ad89f91d69f8bdcca3b469c4065e6b1c..9b02bdda42565b687ecefe63b106118088f1c505:/mpdmenu.pl diff --git a/mpdmenu.pl b/mpdmenu.pl index a12eb4c..15a49b2 100755 --- a/mpdmenu.pl +++ b/mpdmenu.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl # -# Copyright © 2008,2010,2012,2019 Nick Bowler +# Copyright © 2008,2010,2012,2020-2021 Nick Bowler # # Silly little script to generate an FVWM menu with various bits of MPD # status information and controls. @@ -18,12 +18,14 @@ use Encode::Locale qw(decode_argv); decode_argv(Encode::FB_CROAK); binmode(STDOUT, ":utf8"); -use IO::Socket::INET6; use Getopt::Long qw(:config gnu_getopt); use Scalar::Util qw(reftype); use List::Util qw(any max); use FindBin; +use lib "$FindBin::Bin"; +use MPDHacks; + use constant { MPD_MJR_MIN => 0, MPD_MNR_MIN => 21, @@ -33,48 +35,19 @@ use constant { my $SELF = "$FindBin::Bin/$FindBin::Script"; my $MUSIC = $ENV{MUSIC} // "/srv/music"; -my $host = $ENV{MPD_HOST} // "localhost"; -my $port = $ENV{MPD_PORT} // "6600"; my $sock; -my ($albumid, $trackid); +my ($albumid, $albumname, $trackid, $recordingid); my ($topmenu, $menu); my $mode = "top"; my %artistids; -# Quotes the argument so that it is presented as a single argument to MPD -# at the protocol level. This also works OK for most FVWM arguments. -sub escape { - my $s = @_[0] // $_; - - # No way to encode literal newlines in the protocol, so we - # convert any newlines in the arguments into a space, which - # can help with quoting. - $s =~ s/\n/ /g; - - if (/[ \t\\"]/) { - $s =~ s/[\\"]/\\$&/g; - return "\"$s\""; - } - - $s =~ s/^\s*$/"$&"/; - return $s; -} - -# Submit a command to the MPD server; each argument to this function -# is quoted and sent as a single argument to MPD. -sub mpd_exec { - my $cmd = join(' ', map { escape } @_); - - print $sock "$cmd\n"; -} - sub fvwm_cmd_unquoted { print join(' ', @_), "\n"; } sub fvwm_cmd { - fvwm_cmd_unquoted(map { escape } @_); + fvwm_cmd_unquoted(map { MPD::escape } @_); } # Quotes the argument in such a way that it is passed unadulterated by @@ -243,8 +216,8 @@ sub top_track_cover { my $cover = mpd_cover_filename($file); $cover = fvwm_shell_literal($cover // $file); - fvwm_cmd_unquoted("AddToMenu", escape($menu), - escape($entry->{thumb}), + fvwm_cmd_unquoted("AddToMenu", MPD::escape($menu), + MPD::escape($entry->{thumb}), "Exec", "exec", "geeqie", $cover); } } @@ -255,7 +228,13 @@ sub top_track_title { my @submenu; my ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_RELEASETRACKID"); - @submenu = make_submenu("$menu-$mbid", "--track-id=$mbid") if $mbid; + if ($mbid) { + @submenu = make_submenu("$menu-$mbid", "--track-id=$mbid") + } else { + ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_TRACKID"); + @submenu = make_submenu("$menu-track-$mbid", "--recording-id=$mbid") + if ($mbid); + } fvwm_cmd("AddToMenu", $menu, fvwm_label_escape("Title:\t$entry->{Title}"), @@ -284,15 +263,72 @@ sub top_track_artist { sub top_track_album { my ($entry) = @_; my @submenu; + my $mbid; + + if (($mbid) = get_track_metadata($entry, "MUSICBRAINZ_ALBUMID")) { + @submenu = make_submenu("$menu-$mbid", "--album-id=$mbid"); + } elsif (($mbid) = get_track_metadata($entry, "MUSICBRAINZ_TRACKID")) { + # Standalone recording + my @a = get_track_metadata($entry, "MUSICBRAINZ_ARTISTID"); + my ($album) = get_track_metadata($entry, "Album"); - my ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_ALBUMID"); - @submenu = make_submenu("$menu-$mbid", "--album-id=$mbid") if $mbid; + @submenu = make_submenu("$menu-$mbid", "--album-name=$album", + map { "--artist-id=$_" } @a); + + } fvwm_cmd("AddToMenu", $menu, fvwm_label_escape("Album:\t$entry->{Album}"), @submenu); } +# Generate the "MusicBrainz:" entry in the top menu. +sub top_track_musicbrainz { + my ($entry) = @_; + my ($track_mbid, $recording_mbid, $release_mbid); + my @artist_mbids; + my $label = "MB:"; + my %idmap; + + ($track_mbid) = get_track_metadata($entry, "MUSICBRAINZ_RELEASETRACKID"); + ($recording_mbid) = get_track_metadata($entry, "MUSICBRAINZ_TRACKID"); + ($release_mbid) = get_track_metadata($entry, "MUSICBRAINZ_ALBUMID"); + @artist_mbids = get_track_metadata($entry, "MUSICBRAINZ_ARTISTID"); + return unless $track_mbid // $recording_mbid + // $release_mbid // @artist_mbids; + + foreach (get_track_metadata($entry, "Comment")) { + $idmap{$1} = $2 if /^([^=]*)=(.*) \(idmap\)$/; + } + + fvwm_cmd("AddToMenu", $menu, "", "Nop"); + if ($track_mbid) { + fvwm_cmd("AddToMenu", $menu, "$label\tShow track", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/track/$track_mbid"); + $label = ""; + } elsif ($recording_mbid) { + fvwm_cmd("AddToMenu", $menu, "$label\tShow recording", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/recording/$recording_mbid"); + $label = ""; + } elsif ($release_mbid) { + fvwm_cmd("AddToMenu", $menu, "$label\tShow", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/release/$release_mbid"); + $label = ""; + } + + foreach my $mbid (@artist_mbids) { + my $name = " $idmap{$mbid}" if $idmap{$mbid}; + + fvwm_cmd("AddToMenu", $menu, "$label\tShow artist$name", + "Exec", "exec", "xdg-open", + "https://musicbrainz.org/artist/$mbid"); + $label = ""; + } +} + # Given a work MBID, return a hash reference containing all tracks # linked to that work. The hash keys are filenames. sub get_tracks_by_work_mbid { @@ -300,7 +336,7 @@ sub get_tracks_by_work_mbid { my $entry; foreach my $mbid (@_) { - mpd_exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -329,13 +365,13 @@ sub get_tracks_by_work_mbid { # Currently tracks are considered "related" if their associated recordings # have at least one work in common. sub get_tracks_by_track_mbid { - my ($mbid) = @_; + my ($mbid, $tagname) = (@_, "MUSICBRAINZ_RELEASETRACKID"); my %source; my %matches; my $entry; return \%matches unless ($mbid); - mpd_exec("search", "(MUSICBRAINZ_RELEASETRACKID == \"$mbid\")"); + MPD::exec("search", "($tagname == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -359,6 +395,10 @@ sub get_tracks_by_track_mbid { return \%matches; } +sub get_tracks_by_recording_mbid { + return get_tracks_by_track_mbid($_[0], "MUSICBRAINZ_TRACKID"); +} + # Given a release MBID, return a hash reference containing all its # associated tracks in the MPD database. The hash keys are filenames. sub get_tracks_by_release_mbid { @@ -367,7 +407,7 @@ sub get_tracks_by_release_mbid { my $entry; return \%matches unless ($mbid); - mpd_exec("search", "(MUSICBRAINZ_ALBUMID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_ALBUMID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -389,23 +429,39 @@ sub get_tracks_by_release_mbid { return \%matches; } -# Given an artist MBID, return a hash reference containing associated -# releases in the MPD database. The hash keys are release MBIDs. +# Insert the given entry into the referenced hash if it represents a +# standalone recording (not associated with a release). The recording +# MBID is used as the hash key. +sub check_standalone { + my ($outhash, $entry) = @_; + my ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_TRACKID"); + + return if exists $entry->{MUSICBRAINZ_ALBUMID}; + $outhash->{$mbid} = $entry if ($mbid); +} + +# Given an artist MBID, return a list of two hash refererences. The +# first contains the associated releases in the MPD database and the +# hash keys are release MBIDs. The second contains the artist's +# standalone recordings and the hash keys are recording MBIDs. +# +# In scalar context only the release hash is returned. # # Since MPD returns results on a per-track basis, each entry in the # hash has the metadata for one unspecified track from that release. sub get_releases_by_artist_mbid { - my %releases; + my (%releases, %standalones); my $entry; foreach my $mbid (@_) { - mpd_exec("search", "(MUSICBRAINZ_ARTISTID == \"$mbid\")"); + MPD::exec("search", "(MUSICBRAINZ_ARTISTID == \"$mbid\")"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); if (/^(\w+): (.*)$/) { if ($1 eq "file") { + check_standalone(\%standalones, $entry); $entry = {}; } elsif ($1 eq "MUSICBRAINZ_ALBUMID") { $releases{$2} //= $entry; @@ -414,9 +470,10 @@ sub get_releases_by_artist_mbid { add_track_metadata($entry, $1, $2); } } + check_standalone(\%standalones, $entry); } - return \%releases; + return wantarray ? (\%releases, values %standalones) : \%releases; } # Given a filename, return the IDs (if any) for that file in the @@ -425,7 +482,7 @@ sub get_ids_by_filename { my ($file) = @_; my @results = (); - mpd_exec("playlistfind", "file", $file); + MPD::exec("playlistfind", "file", $file); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -438,6 +495,24 @@ sub get_ids_by_filename { return @results; } +sub update_entry_ids { + my @notqueued = (); + + foreach my $entry (@_) { + unless (exists $entry->{Id}) { + my ($id) = get_ids_by_filename($entry->{file}); + if (defined $id) { + $entry->{Id} = $id; + } else { + push @notqueued, $entry; + next; + } + } + } + + return @notqueued; +} + # albumsort(matches, a, b) # # Sort hash keys (a, b) by disc/track number for album menus. @@ -471,8 +546,8 @@ sub menu_trackname { sub print_version { print < 0); + print $fh "Try $0 --help for more information.\n" unless (@_ > 0); } sub print_help { @@ -496,28 +571,37 @@ Options: -p, --port=PORT Connect to the MPD server on PORT, overriding defaults. -m, --menu=NAME Set the name of the generated menu. --album-id=MBID Generate a menu for the given release MBID. + --album-name=NAME + Generate a menu for standalone tracks with the given + "album" NAME. An artist MBID must be supplied. --artist-id=MBID Generate a menu for the given artist MBID. --track-id=MBID Generate a menu for the given track MBID. + --recording-id=MBID + Generate a menu for the given recording MBID. -V, --version Print a version message and then exit. -H, --help Print this message and then exit. EOF } GetOptions( - 'host|h=s' => \$host, - 'port|p=s' => \$port, - 'menu|m=s' => \$menu, + 'host|h=s' => \$MPD::host, + 'port|p=s' => \$MPD::port, + 'menu|m=s' => \$menu, - 'artist-id=s' => sub { $artistids{$_[1]} = 1; $mode = "artist"; }, - 'album-id=s' => sub { $albumid = $_[1]; $mode = "album"; }, - 'track-id=s' => sub { $trackid = $_[1]; $mode = "track"; }, + 'artist-id=s' => sub { $artistids{$_[1]} = 1; $mode = "artist"; }, + 'album-id=s' => sub { $albumid = $_[1]; $mode = "album"; }, + 'album-name=s' => sub { $albumname = $_[1]; $mode = "albumname"; }, + 'track-id=s' => sub { $trackid = $_[1]; $mode = "track"; }, + 'recording-id=s' => sub { $recordingid = $_[1]; $mode = "recording"; }, - 'V|version' => sub { print_version(); exit }, - 'H|help' => sub { print_help(); exit }, + 'V|version' => sub { print_version(); exit }, + 'H|help' => sub { print_help(); exit }, - 'topmenu=s' => \$topmenu, # top menu name (for submenu generation) + 'topmenu=s' => \$topmenu, # top menu name (for submenu generation) ) or do { print_usage; exit 1 }; +$mode = "albumname" if ($albumname && $mode eq "artist"); + unless (defined $menu) { $topmenu //= "MenuMPD"; $menu = $topmenu . ($mode ne "top" ? $mode : ""); @@ -525,21 +609,9 @@ unless (defined $menu) { $topmenu //= $menu; # Connect to MPD. -$sock = new IO::Socket::INET6( - PeerAddr => $host, - PeerPort => $port, - Proto => 'tcp', - Timeout => 2 -) or die("could not open socket: $!.\n"); -binmode($sock, ":utf8"); - -die("could not connect to MPD: $!.\n") - if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/)); - -die("MPD version $1.$2.$3 insufficient.\n") - if ( ($1 < MPD_MJR_MIN) - || ($1 == MPD_MJR_MIN && $2 < MPD_MNR_MIN) - || ($1 == MPD_MJR_MIN && $2 == MPD_MNR_MIN && $3 < MPD_REV_MIN)); +$sock = MPD::connect(); +die("MPD version $MPD::major.$MPD::minor.$MPD::revision insufficient.") + unless MPD::min_version(MPD_MJR_MIN, MPD_MNR_MIN, MPD_REV_MIN); if ($mode eq "top") { my %current; @@ -547,7 +619,7 @@ if ($mode eq "top") { $menu //= "MenuMPD"; - mpd_exec("status"); + MPD::exec("status"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -557,7 +629,7 @@ if ($mode eq "top") { } } - mpd_exec("currentsong"); + MPD::exec("currentsong"); while (<$sock>) { last if (/^OK/); die($_) if (/^ACK/); @@ -578,6 +650,7 @@ if ($mode eq "top") { top_track_title(\%current); top_track_artist(\%current); top_track_album(\%current); + top_track_musicbrainz(\%current); } else { fvwm_cmd("AddToMenu", $menu, "[current track unavailable]"); } @@ -652,15 +725,17 @@ if ($mode eq "top") { } } elsif ($mode eq "artist") { # Create an artist menu. - my $matches = get_releases_by_artist_mbid(keys %artistids); - my $entry; + my ($matches, @recs) = get_releases_by_artist_mbid(keys %artistids); $menu //= "MenuMPDArtist"; my @mbids = sort { datesort($matches, $a, $b) } keys %$matches; my @files = map { $matches->{$_}->{file} } @mbids; my @thumbs = get_item_thumbnails({ small => 1 }, @files); - fvwm_cmd("AddToMenu", $menu, "No releases found", "Title") unless @mbids; + + unless (@mbids) { + fvwm_cmd("AddToMenu", $menu, "No releases found", "Title") + } foreach my $mbid (@mbids) { my $entry = $matches->{$mbid}; @@ -670,13 +745,53 @@ if ($mode eq "top") { "--album-id=$mbid"); fvwm_cmd("AddToMenu", $menu, $thumb . fvwm_label_escape($entry->{Album}), - @submenu); + @submenu); + } + + my @artists = map { "--artist-id=$_" } keys %artistids; + my %nonalbums = map { $_->{Album} => $_ } @recs; + foreach my $name (sort keys %nonalbums) { + my $mbid = $nonalbums{$name}->{MUSICBRAINZ_TRACKID}; + my @submenu = make_submenu("$topmenu-$mbid", @artists, + "--album-name=$name"); + fvwm_cmd("AddToMenu", $menu, fvwm_label_escape($name), @submenu); } -} elsif ($mode eq "track") { - my $matches = get_tracks_by_track_mbid($trackid); +} elsif ($mode eq "albumname") { + # Create a standalone recordings menu + my ($releases, @recs) = get_releases_by_artist_mbid(keys %artistids); + + $menu //= "MenuMPDRecordings"; + my @tracks = sort { $a->{Title} cmp $b->{Title} } + grep { $_->{Album} eq $albumname } @recs; + my @notqueued = update_entry_ids(@tracks); + + fvwm_cmd("AddToMenu", $menu); + fvwm_cmd("+", "No tracks found", "Title") unless @tracks; + + foreach my $entry (@tracks) { + next unless exists $entry->{Id}; + + fvwm_cmd("+", menu_trackname($entry), "Exec", + "exec", "$FindBin::Bin/mpdexec.pl", + "playid", $entry->{Id}); + } + + fvwm_cmd("+", "Not in play queue", "Title") if @notqueued; + foreach my $entry (@notqueued) { + fvwm_cmd("+", menu_trackname($entry)); + } +} elsif ($mode eq "track" || $mode eq "recording") { + my ($matches, $mbid); my @notqueued; + if ($mode eq "track") { + $matches = get_tracks_by_track_mbid($trackid) + } else { + $matches = get_tracks_by_recording_mbid($recordingid) + } + $menu //= "MenuMPDTrack"; + fvwm_cmd("DestroyMenu", $menu); my @files = sort { datesort($matches, $a, $b) } keys %$matches; my @thumbs = get_item_thumbnails({ small => 1 }, @files);