+GetOptions(
+ '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"; },
+ '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 },
+
+ '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 : "");
+}
+$topmenu //= $menu;
+
+# Connect to MPD.
+$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);
+
+MPD::exec("binarylimit", 64);
+while (<$sock>) {
+ $mpd_have_binarylimit = 1 if /^OK/;
+ last if /^OK/ or /^ACK/;
+}
+
+if ($mode eq "top") {
+ my %current;
+ my %state;
+
+ $menu //= "MenuMPD";
+
+ MPD::exec("status");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ $state{$1} = $2;
+ }
+ }
+
+ MPD::exec("currentsong");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ add_track_metadata(\%current, $1, $2);
+ }
+ }
+
+ my $playstate = $state{state} eq "play" ? "Playing"
+ : $state{state} eq "stop" ? "Stopped"
+ : $state{state} eq "pause" ? "Paused"
+ : "Unknown";
+ fvwm_cmd("AddToMenu", $menu, $playstate, "Title");
+
+ if (exists($current{file})) {
+ top_track_cover(\%current);
+ top_track_title(\%current);
+ top_track_artist(\%current);
+ top_track_album(\%current);
+ top_track_musicbrainz(\%current);
+ } else {
+ fvwm_cmd("AddToMenu", $menu, "[current track unavailable]");
+ }
+
+ if ($state{state} =~ /^p/) {
+ my $pp = $state{state} eq "pause" ? "lay" : "ause";
+
+ fvwm_cmd("AddToMenu", $menu, "", "Nop");
+ fvwm_cmd("AddToMenu", $menu, "Next%next.svg:16x16%",
+ "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "next");
+ fvwm_cmd("AddToMenu", $menu, "P$pp%p$pp.svg:16x16%",
+ "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "p$pp");
+ fvwm_cmd("AddToMenu", $menu, "Stop%stop.svg:16x16%",
+ "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "stop");
+ fvwm_cmd("AddToMenu", $menu, "Prev%prev.svg:16x16%",
+ "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "previous");
+ } elsif ($state{state} eq "stop") {
+ fvwm_cmd("AddToMenu", $menu, "", "Nop");
+ fvwm_cmd("AddToMenu", $menu, "Play%play.svg:16x16%",
+ "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "play");
+ }
+} elsif ($mode eq "album") {
+ my $matches = get_tracks_by_release_mbid($albumid);
+ my @notqueued = ();
+
+ $menu //= "MenuMPDAlbum";
+
+ my $track_max = max(map { $_->{Track} } values %$matches);
+ my $disc_max = max(map { $_->{Disc} } values %$matches);
+
+ # CDs have a max of 99 tracks and I hope 100+-disc-releases
+ # don't exist so this is fine.
+ my $track_digits = $track_max >= 10 ? 2 : 1;
+ my $disc_digits = $disc_max > 1 ? $disc_max >= 10 ? 2 : 1 : 0;
+ my $currentdisc;
+
+ fvwm_cmd("AddToMenu", $menu);
+ fvwm_cmd("+", "Release not found", "Title") unless keys %$matches;
+ foreach my $file (sort { albumsort($matches, $a, $b) } keys %$matches) {
+ my $entry = $matches->{$file};
+
+ # Format disc/track numbers
+ $entry->{trackfmt} = sprintf("%*.*s%.*s%*d\t",
+ $disc_digits, $disc_digits, $entry->{Disc},
+ $disc_digits, "-",
+ $track_digits, $entry->{Track});
+ $entry->{trackfmt} =~ s/ /\N{U+2007}/g;
+
+ unless (exists $entry->{Id}) {
+ my ($id) = get_ids_by_filename($file);
+ if (defined $id) {
+ $entry->{Id} = $id;
+ } else {
+ push @notqueued, $entry;
+ next;
+ }
+ }
+
+ if (defined $currentdisc && $currentdisc != $entry->{Disc}) {
+ fvwm_cmd("+", "", "Nop");
+ }
+ $currentdisc = $entry->{Disc};
+
+ 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 "artist") {
+ # Create an artist menu.
+ 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);
+
+ unless (@mbids) {
+ fvwm_cmd("AddToMenu", $menu, "No releases found", "Title")
+ }
+
+ foreach my $mbid (@mbids) {
+ my $entry = $matches->{$mbid};
+ my $thumb = shift @thumbs;
+
+ my @submenu = make_submenu("$topmenu-$mbid",
+ "--album-id=$mbid");
+ fvwm_cmd("AddToMenu", $menu,
+ $thumb . fvwm_label_escape($entry->{Album}),
+ @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 "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;
+
+ # Show thumbnails for standalone recordings
+ my @thumbs = get_item_thumbnails({ small => 1 },
+ map { $_->{file} } @tracks);
+ foreach my $entry (@tracks) {
+ $entry->{thumb} = shift @thumbs;
+ }
+
+ 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);
+
+ fvwm_cmd("AddToMenu", $menu);
+ fvwm_cmd("+", "No tracks found", "Title") unless @files;
+ foreach my $file (@files) {
+ my $entry = $matches->{$file};
+ $entry->{thumb} = shift @thumbs;
+
+ unless (exists $entry->{Id}) {
+ my ($id) = get_ids_by_filename($file);
+ if (defined $id) {
+ $entry->{Id} = $id;
+ } else {
+ push @notqueued, $entry;
+ next;
+ }
+ }
+
+ 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));