#!/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.
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);
+use List::Util qw(any max);
use FindBin;
+use lib "$FindBin::Bin";
+use MPDHacks;
+
use constant {
MPD_MJR_MIN => 0,
MPD_MNR_MIN => 21,
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 $menu;
+my ($albumid, $albumname, $trackid, $recordingid);
+my ($topmenu, $menu);
my $mode = "top";
-
-# 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";
-}
+my %artistids;
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
sub make_submenu {
my $name = shift;
$name =~ s/-/_/g;
- unshift @_, ("exec", $SELF, "--menu=$name");
+ unshift @_, ("exec", $SELF, "--topmenu=$topmenu", "--menu=$name");
fvwm_cmd("DestroyFunc", "Make$name");
fvwm_cmd("AddToFunc", "Make$name");
fvwm_cmd("DestroyMenu", $name);
fvwm_cmd("AddToMenu", $name, "DynamicPopupAction", "Make$name");
- fvwm_cmd("AddToFunc", "KillMenuMPD", "I", "DestroyMenu", $name);
+ fvwm_cmd("AddToFunc", "Kill$topmenu", "I", "DestroyMenu", $name);
fvwm_cmd("DestroyFunc", "Make$name");
fvwm_cmd("AddToFunc", "Make$name");
fvwm_cmd("+", "I", "DestroyMenu", $name);
fvwm_cmd("+", "I", "-PipeRead",
join(' ', map { fvwm_shell_literal } @_));
- fvwm_cmd("AddToFunc", "KillMenuMPD", "I", "DestroyFunc", "Make$name");
+ fvwm_cmd("AddToFunc", "Kill$topmenu", "I", "DestroyFunc", "Make$name");
return ("Popup", $name);
}
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);
}
}
# Generate the "Title:" entry in the top menu.
sub top_track_title {
my ($entry) = @_;
+ my @submenu;
- my @submenu = make_submenu("$menu-TopTrack",
- "--title=$entry->{Title}");
+ my ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_RELEASETRACKID");
+ 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}"),
# Generate the "Artist:" entry in the top menu.
sub top_track_artist {
my ($entry) = @_;
+ my @submenu;
- my @submenu = make_submenu("$menu-TopArtist",
- "--artist=$entry->{Artist}");
+ # TODO: multi-artist tracks should get multiple artist menus; for now
+ # just combine the releases from all artists.
+ my @mbids = get_track_metadata($entry, "MUSICBRAINZ_ARTISTID");
+ if (@mbids) {
+ @submenu = make_submenu("$menu-TopArtist",
+ map { "--artist-id=$_" } @mbids);
+ }
fvwm_cmd("AddToMenu", $menu,
fvwm_label_escape("Artist:\t$entry->{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 @submenu = make_submenu("$menu-TopAlbum",
- "--artist=$entry->{Artist}",
- "--album=$entry->{Album}");
+ @submenu = make_submenu("$menu-$mbid", "--album-name=$album",
+ map { "--artist-id=$_" } @a);
+
+ }
fvwm_cmd("AddToMenu", $menu,
fvwm_label_escape("Album:\t$entry->{Album}"),
@submenu);
}
-# Global hash for tracking what is to be "accepted".
-my %accept = ();
+# 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 {
+ my %matches;
+ my $entry;
+
+ foreach my $mbid (@_) {
+ MPD::exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ if ($1 eq "file") {
+ if (exists($matches{$2})) {
+ $entry = $matches{$2};
+ } else {
+ $entry = {};
+ $matches{$2} = $entry;
+ }
+ }
+
+ add_track_metadata($entry, $1, $2);
+ }
+ }
+ }
+
+ return \%matches;
+}
+
+# Given a track MBID, return a hash reference containing all "related"
+# tracks in the MPD database. The hash keys are filenames.
+#
+# Currently tracks are considered "related" if their associated recordings
+# have at least one work in common.
+sub get_tracks_by_track_mbid {
+ my ($mbid, $tagname) = (@_, "MUSICBRAINZ_RELEASETRACKID");
+ my %source;
+ my %matches;
+ my $entry;
+
+ return \%matches unless ($mbid);
+ MPD::exec("search", "($tagname == \"$mbid\")");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ add_track_metadata(\%source, $1, $2);
+ }
+ }
+
+ # Always include the current track
+ $matches{$source{file}} = \%source;
+
+ # Find all tracks related by work
+ foreach my $mbid (get_track_metadata(\%source, "MUSICBRAINZ_WORKID")) {
+ my $related = get_tracks_by_work_mbid($mbid);
+ foreach (keys %$related) {
+ $matches{$_} //= $related->{$_};
+ }
+ }
+
+ 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 {
+ my ($mbid) = @_;
+ my %matches;
+ my $entry;
+
+ return \%matches unless ($mbid);
+ MPD::exec("search", "(MUSICBRAINZ_ALBUMID == \"$mbid\")");
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ if ($1 eq "file") {
+ if (exists($matches{$2})) {
+ $entry = $matches{$2};
+ } else {
+ $entry = {};
+ $matches{$2} = $entry;
+ }
+ }
+
+ add_track_metadata($entry, $1, $2);
+ }
+ }
+
+ return \%matches;
+}
+
+# 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, %standalones);
+ my $entry;
+
+ foreach my $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;
+ }
+
+ add_track_metadata($entry, $1, $2);
+ }
+ }
+ check_standalone(\%standalones, $entry);
+ }
+
+ return wantarray ? (\%releases, values %standalones) : \%releases;
+}
+
+# Given a filename, return the IDs (if any) for that file in the
+# current MPD play queue.
+sub get_ids_by_filename {
+ my ($file) = @_;
+ my @results = ();
+
+ MPD::exec("playlistfind", "file", $file);
+ while (<$sock>) {
+ last if (/^OK/);
+ die($_) if (/^ACK/);
+
+ if (/^(\w+): (.*)$/) {
+ push @results, $2 if ($1 eq "Id");
+ }
+ }
+
+ 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.
+sub albumsort {
+ my ($matches, $a, $b) = @_;
+
+ return $matches->{$a}->{Disc} <=> $matches->{$b}->{Disc}
+ || $matches->{$a}->{Track} <=> $matches->{$b}->{Track}
+ || $a cmp $b;
+}
-# Default values for stuff.
-my ($album, $artist, $title);
+# datesort(matches, a, b)
+#
+# Sort hash keys (a, b) by release date
+sub datesort {
+ my ($matches, $a, $b) = @_;
+
+ return $matches->{$a}->{Date} cmp $matches->{$b}->{Date}
+ || $a cmp $b;
+}
+
+# menu_trackname(entry)
+#
+# Format the track name for display in an FVWM menu, where entry
+# is a hash reference containing the track metadata.
+sub menu_trackname {
+ my ($entry) = @_;
+ my $fmt = "$entry->{trackfmt}$entry->{Artist} - $entry->{Title}";
+ return "$entry->{thumb}" . fvwm_label_escape($fmt);
+}
sub print_version {
print <<EOF
-mpdmenu.pl 0.8
-Copyright © 2019 Nick Bowler
+mpdmenu.pl 0.9
+Copyright © 2021 Nick Bowler
License GPLv3+: GNU General Public License version 3 or any later version.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
}
sub print_usage {
- my $fh = $_[1] // *STDERR;
+ my ($fh) = (@_, *STDERR);
print $fh "Usage: $0 [options]\n";
- print "Try $0 --help for more information.\n" unless (@_ > 0);
+ print $fh "Try $0 --help for more information.\n" unless (@_ > 0);
}
sub print_help {
-h, --host=HOST Connect to the MPD server on HOST, overriding defaults.
-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"; },
+ 'album-name=s' => sub { $albumname = $_[1]; $mode = "albumname"; },
+ 'track-id=s' => sub { $trackid = $_[1]; $mode = "track"; },
+ 'recording-id=s' => sub { $recordingid = $_[1]; $mode = "recording"; },
- 'album=s' => sub { $album = $_[1]; $mode = "album"; },
- 'artist=s' => sub { $artist = $_[1];
- $mode = "artist" unless $mode eq "album"; },
- 'title=s' => sub { $title = $_[1]; $mode = "track"; },
+ '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)
) 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 = 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;
$menu //= "MenuMPD";
- mpd_exec("status");
+ MPD::exec("status");
while (<$sock>) {
last if (/^OK/);
die($_) if (/^ACK/);
}
}
- mpd_exec("currentsong");
+ MPD::exec("currentsong");
while (<$sock>) {
last if (/^OK/);
die($_) if (/^ACK/);
"Exec", "exec", "$FindBin::Bin/mpdexec.pl", "play");
}
} elsif ($mode eq "album") {
- # Create an album menu.
- my @playlist = ();
- my $entry;
-
- $menu = "MenuMPDAlbum" unless defined $menu;
-
- $album =~ s/"/\\"/g;
- print $sock "playlistfind album \"$album\"\n";
- while (<$sock>) {
- last if (/^OK/);
- die($_) if (/^ACK/);
-
- if (/^(\w+): (.*)$/) {
- if ($1 eq "file") {
- if (keys(%$entry) > 0) {
- addalbumentry(\@playlist, $entry)
- }
-
- $entry = {};
+ 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;
}
-
- $entry->{$1} = $2;
}
- }
- addalbumentry(\@playlist, $entry) if (keys(%$entry) > 0);
-
- die("No tracks found.\n") if (!@playlist);
- foreach (sort albumsort @playlist) {
- my ($t_file, $t_trackno, $t_artist, $t_title, $t_id) = (
- $_->{file},
- $_->{Track},
- $_->{Artist},
- $_->{Title},
- $_->{Id},
- );
- next if (defined $artist && !$accept{albumdir($t_file)});
-
- $t_artist = sanitise($t_artist, 0);
- $t_title = sanitise($t_title, 0);
+ if (defined $currentdisc && $currentdisc != $entry->{Disc}) {
+ fvwm_cmd("+", "", "Nop");
+ }
+ $currentdisc = $entry->{Disc};
- my $cmd = sprintf "AddToMenu $menu \"%d\t%s - %s\""
- ." Exec exec $FindBin::Bin/mpdexec.pl"
- ." playid %d",
- $t_trackno, $t_artist, $t_title, $t_id;
+ fvwm_cmd("+", menu_trackname($entry), "Exec",
+ "exec", "$FindBin::Bin/mpdexec.pl",
+ "playid", $entry->{Id});
+ }
- cmd($cmd);
+ 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 %albums = ();
- my $file;
- my $quoteartist = $artist;
+ my ($matches, @recs) = get_releases_by_artist_mbid(keys %artistids);
- $menu = "MenuMPDArtist" unless defined $menu;
+ $menu //= "MenuMPDArtist";
- $quoteartist =~ s/"/\\"/g;
- print $sock "playlistfind artist \"$quoteartist\"\n";
- while (<$sock>) {
- last if (/^OK/);
- die($_) if (/^ACK/);
+ my @mbids = sort { datesort($matches, $a, $b) } keys %$matches;
+ my @files = map { $matches->{$_}->{file} } @mbids;
+ my @thumbs = get_item_thumbnails({ small => 1 }, @files);
- if (/^(\w+): (.*)$/) {
- $file = $2 if ($1 eq "file");
- $albums{$2} = $file if ($1 eq "Album");
- }
+ unless (@mbids) {
+ fvwm_cmd("AddToMenu", $menu, "No releases found", "Title")
}
- die("No albums found.\n") if (!keys(%albums));
-
-{ # work around 'use locale' breaking s///i
- my $i = 0;
- use locale;
-
- my @keys = sort keys %albums;
- my @thumbs = get_item_thumbnails({ small => 1 },
- map { $albums{$_} } @keys);
-
- foreach my $key (@keys) {
- my $a_album = sanitise($key, 1);
+ foreach my $mbid (@mbids) {
+ my $entry = $matches->{$mbid};
my $thumb = shift @thumbs;
- cmd("AddToMenu $menu \"$thumb$a_album\" Popup MenuMPDArt_$i");
-
- cmd("AddToMenu MenuMPDArt_$i DynamicPopUpAction MakeMenuMPDArt_$i");
-
- cmd("DestroyFunc MakeMenuMPDArt_$i");
- cmd("AddToFunc MakeMenuMPDArt_$i
- + I DestroyMenu MenuMPDArt_$i
- + I -PipeRead \"exec $SELF "
- ."--menu MenuMPDArt_$i "
- ."--album ".shellify($key, 1)." "
- ."--artist ".shellify($artist, 1)."\"");
-
- cmd("AddToFunc KillMenuMPD I DestroyMenu MenuMPDArt_$i");
- cmd("AddToFunc KillMenuMPD I DestroyFunc MakeMenuMPDArt_$i");
-
- $i++;
+ my @submenu = make_submenu("$topmenu-$mbid",
+ "--album-id=$mbid");
+ fvwm_cmd("AddToMenu", $menu,
+ $thumb . fvwm_label_escape($entry->{Album}),
+ @submenu);
}
-} # end use locale workaround
-} elsif ($mode eq "track") {
- # Create a title menu.
- my @titles;
- my $entry;
- $menu = "MenuMPDTitle" unless defined $menu;
-
- # Open and close brackets.
- my ($ob, $cb) = ("[\[~〜<〈(ー−-]", "[\]~〜>〉)ー−-]");
-
- $_ = $title;
-
- # Deal with specific cases.
- s/ちいさな(?=ヘミソフィア)//; # ヘミソフィア
- s/ "mix on air flavor" dear EIKO SHIMAMIYA//; # Spiral wind
- s/ "So,you need me" Style//; # I need you
- s/ ::Symphony Second movement:://; # Disintegration
- s/-\[instrumental\]//; # 青い果実
- s/ -Practice Track-//; # Fair Heaven
- s/〜世界で一番アナタが好き〜//; # Pure Heart
- s/〜彼方への哀歌//; # 十二幻夢
- s/ sora no uta ver.//; # 美しい星
-
- s/\s*-remix-$//; # Otherwise "D-THREAD -remix-" doesn't work right.
+ 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);
- # Deal with titles like "blah (ABC version)".
- s/\s*$ob.*(style|mix|edit|edition|ver\.?|version|melody|カラオケ)$cb?$//i;
+ $menu //= "MenuMPDRecordings";
+ my @tracks = sort { $a->{Title} cmp $b->{Title} }
+ grep { $_->{Album} eq $albumname } @recs;
+ my @notqueued = update_entry_ids(@tracks);
- # Deal with titles like "blah (without XYZ)".
- s/\s*$ob\s*((e\.)?piano|english|japanese|inst|tv|without|w\/o|off|back|short|karaoke|game).*//i;
+ fvwm_cmd("AddToMenu", $menu);
+ fvwm_cmd("+", "No tracks found", "Title") unless @tracks;
- # Deal with titles like "blah instrumental".
- s/\s+(instrumental|off vocal|short|tv)([\s-]+(mix|size|version))?$//i;
- s/\s+without\s+\w+$//i;
+ foreach my $entry (@tracks) {
+ next unless exists $entry->{Id};
- # Deal with separate movements in classical pieces.
- s/: [IVX]+\..*//;
+ fvwm_cmd("+", menu_trackname($entry), "Exec",
+ "exec", "$FindBin::Bin/mpdexec.pl",
+ "playid", $entry->{Id});
+ }
- my $basetitle = $_;
- my $_basetitle = $basetitle;
+ 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;
- $_basetitle =~ s/"/\\"/g;
- print $sock "playlistsearch title \"$_basetitle\"\n";
- while (<$sock>) {
- last if (/^OK/);
- die($_) if (/^ACK/);
+ if ($mode eq "track") {
+ $matches = get_tracks_by_track_mbid($trackid)
+ } else {
+ $matches = get_tracks_by_recording_mbid($recordingid)
+ }
- if (/^(\w+): (.*)$/) {
- if ($1 eq "file") {
- push @titles, $entry if (keys(%$entry) > 0);
- $entry = {};
+ $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;
}
-
- $entry->{$1} = $2;
}
- }
- push @titles, $entry if (keys(%$entry) > 0);
-
-{ # work around 'use locale' breaking s///i
- use locale;
- my @thumbs = get_item_thumbnails({ small => 1 },
- map { $_->{file} } @titles);
- for (my $i = 0; $i < @titles; $i++) {
- $titles[$i]->{thumb} = $thumbs[$i];
+ fvwm_cmd("+", menu_trackname($entry), "Exec",
+ "exec", "$FindBin::Bin/mpdexec.pl",
+ "playid", $entry->{Id});
}
- foreach (sort titlesort @titles) {
- my ($t_file, $t_artist, $t_title, $t_id, $thumb) = (
- $_->{file},
- $_->{Artist},
- $_->{Title},
- $_->{Id},
- $_->{thumb}
- );
-
- # MPD searches are case-insensitive.
- next if (!($t_title =~ m/(\P{Latin}|^)\Q$basetitle\E(\P{Latin}|$)/ || $t_title =~ m/\Q$basetitle\E/i));
-
- $t_artist = sanitise($t_artist, 1);
- $t_title = sanitise($t_title, 1);
-
- cmd("AddToMenu $menu \"$thumb$t_artist - $t_title\""
- ." Exec exec $FindBin::Bin/mpdexec.pl"
- ." playid $t_id");
+ fvwm_cmd("+", "Not in play queue", "Title") if @notqueued;
+ foreach my $entry (@notqueued) {
+ fvwm_cmd("+", menu_trackname($entry));
}
-} # end use locale workaround
}
# Finished.
print $sock "close\n";
-
-sub sanitise
-{
- $_ = $_[0];
- s/&/&&/g if ($_[1]);
- s/([\$@%^*])/\1\1/g;
- s/"/\\"/g;
- return $_;
-}
-
-sub addalbumentry
-{
- my ($playlist, $entry) = @_;
-
- push(@$playlist, $entry);
-
- if (defined $artist && $artist eq $entry->{Artist}) {
- my $albumdir = albumdir($entry->{file});
- $accept{$albumdir} = 1;
- }
-}
-
-sub albumdir
-{
- my $file = $_[0];
-
- $file =~ s:(/Disk [0-9]+[^/]*)?/[^/]*$::;
- return $file
-}
-
-sub albumsort
-{
- return ($a->{Disc} <=> $b->{Disc}) if ($a->{Disc} != $b->{Disc});
- return ($a->{Track} <=> $b->{Track});
-}
-
-sub titlesort
-{
- return ($a->{Album} cmp $b->{Album}) if($a->{Album} ne $b->{Album});
- return ($a->{Artist} cmp $b->{Artist}) if($a->{Artist} ne $b->{Artist});
- return ($a->{Title} cmp $b->{Title});
-}
-
-sub shellify
-{
- my ($str, $quoted) = @_;
- $str =~ s/'/'\\''/g;
- if ($quoted) {
- $str =~ s/\\/\\\\/g;
- $str =~ s/"/\\"/g;
- }
- return "'$str'";
-}
-
-sub cmd
-{
- print "$_[0]\n";
-}