]> git.draconx.ca Git - mpdhacks.git/blobdiff - mpdmenu.pl
mpdthumb: Fix failure when readpicture/albumart both return data.
[mpdhacks.git] / mpdmenu.pl
index 2b5fcaddf3a18abcd5d0355277dc2b66666ba8d4..71613c5fb015d8a9e5489999f0934c88b9850454 100755 (executable)
@@ -35,7 +35,7 @@ use constant {
 my $SELF = "$FindBin::Bin/$FindBin::Script";
 
 my $MUSIC = $ENV{MUSIC}    // "/srv/music";
-my $sock;
+my ($sock, $mpd_have_binarylimit);
 
 my ($albumid, $albumname, $trackid, $recordingid);
 my ($topmenu, $menu);
@@ -134,6 +134,13 @@ sub get_item_thumbnails {
                $c = "%";
        }
 
+       if ($mpd_have_binarylimit) {
+               # --embedded implies and requires binarylimit support
+               push @opts, "--embedded";
+       } else {
+               push @opts, "--no-binarylimit";
+       }
+
        open THUMB, "-|", "$FindBin::Bin/mpdthumb.sh", @opts, "--", @_;
        foreach (@_) {
                my $thumb = <THUMB>;
@@ -282,6 +289,53 @@ sub top_track_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 {
@@ -566,6 +620,12 @@ $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;
@@ -603,6 +663,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]");
        }
@@ -715,6 +776,14 @@ if ($mode eq "top") {
        $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);