]> git.draconx.ca Git - mpdhacks.git/blob - mpdmenu.pl
mpdmenu: Use MBIDs for track matching instead of name matching.
[mpdhacks.git] / mpdmenu.pl
1 #!/usr/bin/perl
2 #
3 # Copyright © 2008,2010,2012,2019 Nick Bowler
4 #
5 # Silly little script to generate an FVWM menu with various bits of MPD
6 # status information and controls.
7 #
8 # License GPLv3+: GNU General Public License version 3 or any later version.
9 # This is free software: you are free to change and redistribute it.
10 # There is NO WARRANTY, to the extent permitted by law.
11
12 use strict;
13
14 use utf8;
15
16 use Encode qw(decode encode);
17 use Encode::Locale qw(decode_argv);
18 decode_argv(Encode::FB_CROAK);
19 binmode(STDOUT, ":utf8");
20
21 use IO::Socket::INET6;
22 use Getopt::Long qw(:config gnu_getopt);
23 use Scalar::Util qw(reftype);
24 use List::Util qw(any max);
25 use FindBin;
26
27 use constant {
28         MPD_MJR_MIN => 0,
29         MPD_MNR_MIN => 21,
30         MPD_REV_MIN => 0,
31 };
32
33 my $SELF = "$FindBin::Bin/$FindBin::Script";
34
35 my $MUSIC = $ENV{MUSIC}    // "/srv/music";
36 my $host  = $ENV{MPD_HOST} // "localhost";
37 my $port  = $ENV{MPD_PORT} // "6600";
38 my $sock;
39
40 my ($albumid, $trackid);
41 my %artistids;
42 my $menu;
43 my $mode = "top";
44
45 # Quotes the argument so that it is presented as a single argument to MPD
46 # at the protocol level.  This also works OK for most FVWM arguments.
47 sub escape {
48         my $s = @_[0] // $_;
49
50         # No way to encode literal newlines in the protocol, so we
51         # convert any newlines in the arguments into a space, which
52         # can help with quoting.
53         $s =~ s/\n/ /g;
54
55         if (/[ \t\\"]/) {
56                 $s =~ s/[\\"]/\\$&/g;
57                 return "\"$s\"";
58         }
59
60         $s =~ s/^\s*$/"$&"/;
61         return $s;
62 }
63
64 # Submit a command to the MPD server; each argument to this function
65 # is quoted and sent as a single argument to MPD.
66 sub mpd_exec {
67         my $cmd = join(' ', map { escape } @_);
68
69         print $sock "$cmd\n";
70 }
71
72 sub fvwm_cmd_unquoted {
73         print join(' ', @_), "\n";
74 }
75
76 sub fvwm_cmd {
77         fvwm_cmd_unquoted(map { escape } @_);
78 }
79
80 # Quotes the argument in such a way that it is passed unadulterated by
81 # both FVWM and the shell to a command as a single argument (for use as
82 # an # argument for e.g., the Exec or PipeRead FVWM commands).
83 #
84 # The result must be used with fvwm_cmd_unquoted;
85 sub fvwm_shell_literal {
86         my $s = @_[0] // $_;
87
88         $s =~ s/\$/\$\$/g;
89         if ($s =~ /[' \t]/) {
90                 $s =~ s/'/'\\''/g;
91                 return "'$s'";
92         }
93         $s =~ s/^\s*$/'$&'/;
94         return "$s";
95 }
96
97 # Escapes metacharacters in the argument used in FVWM menu labels.  The
98 # string must still be quoted (e.g., by using fvwm_cmd).
99 sub fvwm_label_escape {
100         my @tokens = split /\t/, $_[0];
101         @tokens[0] =~ s/&/&&/g;
102         my $ret = join "\t", @tokens;
103         $ret =~ s/[\$@%^*]/$&$&/g;
104         return $ret;
105 }
106
107 # make_submenu(name, [args ...])
108 #
109 # Creates a submenu (with the specified name) constructed by invoking this
110 # script with the given arguments.  Returns a list that can be passed to
111 # fvwm_cmd to display the menu.
112 sub make_submenu {
113         my $name = shift;
114         $name =~ s/-/_/g;
115         unshift @_, ("exec", $SELF, "--menu=$name");
116
117         fvwm_cmd("DestroyFunc", "Make$name");
118         fvwm_cmd("AddToFunc", "Make$name");
119         fvwm_cmd("+", "I", "DestroyMenu", $name);
120
121         fvwm_cmd("DestroyMenu", $name);
122         fvwm_cmd("AddToMenu", $name, "DynamicPopupAction", "Make$name");
123         fvwm_cmd("AddToFunc", "KillMenuMPD", "I", "DestroyMenu", $name);
124
125         fvwm_cmd("DestroyFunc", "Make$name");
126         fvwm_cmd("AddToFunc", "Make$name");
127         fvwm_cmd("+", "I", "DestroyMenu", $name);
128         fvwm_cmd("+", "I", "-PipeRead",
129                  join(' ', map { fvwm_shell_literal } @_));
130         fvwm_cmd("AddToFunc", "KillMenuMPD", "I", "DestroyFunc", "Make$name");
131
132         return ("Popup", $name);
133 }
134
135 # get_item_thumbnails({ options }, file, ...)
136 # get_item_thumbnails(file, ...)
137 #
138 # For each music file listed, obtain a thumbnail (if any) for the cover art.
139 #
140 # The first argument is a hash reference to control the mode of operation;
141 # it may be omitted for default options.
142 #
143 #   get_item_thumbnails({ small => 1 }, ...) - smaller thumbnails
144 #
145 # The returned list consists of strings (in the same order as the filename
146 # arguments) suitable for use directly in FVWM menus; by default the filename
147 # is bracketed by asterisks (e.g., "*thumbnail.png*"); in small mode it is
148 # surrounded by % (e.g., "%thumbnail.png%").  If no cover art was found, the
149 # empty string is returned for that file.
150 sub get_item_thumbnails {
151         my @results = ();
152         my $flags = {};
153         my @opts = ();
154
155         $flags = shift if (reftype($_[0]) eq "HASH");
156         return @results unless @_;
157
158         my $c = "*";
159         if ($flags->{small}) {
160                 push @opts, "--small";
161                 $c = "%";
162         }
163
164         open THUMB, "-|", "$FindBin::Bin/mpdthumb.sh", @opts, "--", @_;
165         foreach (@_) {
166                 my $thumb = <THUMB>;
167                 chomp $thumb;
168
169                 $thumb = "$c$thumb$c" if (-f $thumb);
170                 push @results, $thumb;
171         }
172         close THUMB;
173         die("mpdthumb failed") if ($?);
174
175         return @results;
176 }
177
178 # add_track_metadata(hashref, key, value)
179 #
180 # Inserts the given key into the referenced hash; if the key already exists
181 # in the hash then the hash element is converted to an array reference (if
182 # it isn't already) and the value is appended to that array.
183 sub add_track_metadata {
184         my ($entry, $key, $value) = @_;
185
186         if (exists($entry->{$key})) {
187                 my $ref = $entry->{$key};
188
189                 if (reftype($ref) ne "ARRAY") {
190                         return if ($ref eq $value);
191
192                         $ref = [$ref];
193                         $entry->{$key} = $ref;
194                 }
195
196                 push(@$ref, $value) unless (any {$_ eq $value} @$ref);
197         } else {
198                 $entry->{$key} = $value;
199         }
200 }
201
202 # get_track_metadata(hashref, key)
203 #
204 # Return the values associated with the given metadata key as a list.
205 sub get_track_metadata {
206         my ($entry, $key) = @_;
207
208         return () unless (exists($entry->{$key}));
209
210         my $ref = $entry->{$key};
211         return @$ref if (reftype($ref) eq "ARRAY");
212         return $ref
213 }
214
215 # Given a music filename, search for the cover art in the same directory.
216 sub mpd_cover_filename {
217         my ($dir) = @_;
218         my $file;
219
220         $dir =~ s/\/[^\/]*$//;
221         foreach ("cover.png", "cover.jpg", "cover.tiff", "cover.bmp") {
222                 if (-f "$dir/$_") {
223                         $file = "$dir/$_";
224                         last;
225                 }
226         }
227         return unless defined $file;
228
229         # Follow one level of symbolic link to get to the scans directory.
230         $file = readlink($file) // $file;
231         $file = "$dir/$file" unless ($file =~ /^\//);
232         return $file;
233 }
234
235 # Generate the cover art entry in the top menu.
236 sub top_track_cover {
237         my ($entry) = @_;
238
239         ($entry->{thumb}) = get_item_thumbnails($entry->{file});
240         print "$entry->{thumb}\n";
241         if ($entry->{thumb}) {
242                 my $file = "$MUSIC/$entry->{file}";
243                 my $cover = mpd_cover_filename($file);
244
245                 $cover = fvwm_shell_literal($cover // $file);
246                 fvwm_cmd_unquoted("AddToMenu", escape($menu),
247                                   escape($entry->{thumb}),
248                                   "Exec", "exec", "geeqie", $cover);
249         }
250 }
251
252 # Generate the "Title:" entry in the top menu.
253 sub top_track_title {
254         my ($entry) = @_;
255         my @submenu;
256
257         my ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_RELEASETRACKID");
258         @submenu = make_submenu("$menu-$mbid", "--track-id=$mbid") if $mbid;
259
260         fvwm_cmd("AddToMenu", $menu,
261                  fvwm_label_escape("Title:\t$entry->{Title}"),
262                  @submenu);
263 }
264
265 # Generate the "Artist:" entry in the top menu.
266 sub top_track_artist {
267         my ($entry) = @_;
268         my @submenu;
269
270         # TODO: multi-artist tracks should get multiple artist menus; for now
271         # just combine the releases from all artists.
272         my @mbids = get_track_metadata($entry, "MUSICBRAINZ_ARTISTID");
273         if (@mbids) {
274                 @submenu = make_submenu("$menu-TopArtist",
275                                         map { "--artist-id=$_" } @mbids);
276         }
277
278         fvwm_cmd("AddToMenu", $menu,
279                  fvwm_label_escape("Artist:\t$entry->{Artist}"),
280                  @submenu);
281 }
282
283 # Generate the "Album:" entry in the top menu.
284 sub top_track_album {
285         my ($entry) = @_;
286         my @submenu;
287
288         my ($mbid) = get_track_metadata($entry, "MUSICBRAINZ_ALBUMID");
289         @submenu = make_submenu("$menu-$mbid", "--album-id=$mbid") if $mbid;
290
291         fvwm_cmd("AddToMenu", $menu,
292                  fvwm_label_escape("Album:\t$entry->{Album}"),
293                  @submenu);
294 }
295
296 # Given a work MBID, return a hash reference containing all tracks
297 # linked to that work.  The hash keys are filenames.
298 sub get_tracks_by_work_mbid {
299         my %matches;
300         my $entry;
301
302         foreach my $mbid (@_) {
303                 mpd_exec("search", "(MUSICBRAINZ_WORKID == \"$mbid\")");
304                 while (<$sock>) {
305                         last if (/^OK/);
306                         die($_) if (/^ACK/);
307
308                         if (/^(\w+): (.*)$/) {
309                                 if ($1 eq "file") {
310                                         if (exists($matches{$2})) {
311                                                 $entry = $matches{$2};
312                                         } else {
313                                                 $entry = {};
314                                                 $matches{$2} = $entry;
315                                         }
316                                 }
317
318                                 add_track_metadata($entry, $1, $2);
319                         }
320                 }
321         }
322
323         return \%matches;
324 }
325
326 # Given a track MBID, return a hash reference containing all "related"
327 # tracks in the MPD database.  The hash keys are filenames.
328 #
329 # Currently tracks are considered "related" if their associated recordings
330 # have at least one work in common.
331 sub get_tracks_by_track_mbid {
332         my ($mbid) = @_;
333         my %source;
334         my %matches;
335         my $entry;
336
337         return \%matches unless ($mbid);
338         mpd_exec("search", "(MUSICBRAINZ_RELEASETRACKID == \"$mbid\")");
339         while (<$sock>) {
340                 last if (/^OK/);
341                 die($_) if (/^ACK/);
342
343                 if (/^(\w+): (.*)$/) {
344                         add_track_metadata(\%source, $1, $2);
345                 }
346         }
347
348         # Always include the current track
349         $matches{$source{file}} = \%source;
350
351         # Find all tracks related by work
352         foreach my $mbid (get_track_metadata(\%source, "MUSICBRAINZ_WORKID")) {
353                 my $related = get_tracks_by_work_mbid($mbid);
354                 foreach (keys %$related) {
355                         $matches{$_} //= $related->{$_};
356                 }
357         }
358
359         return \%matches;
360 }
361
362 # Given a release MBID, return a hash reference containing all its
363 # associated tracks in the MPD database.  The hash keys are filenames.
364 sub get_tracks_by_release_mbid {
365         my ($mbid) = @_;
366         my %matches;
367         my $entry;
368
369         return \%matches unless ($mbid);
370         mpd_exec("search", "(MUSICBRAINZ_ALBUMID == \"$mbid\")");
371         while (<$sock>) {
372                 last if (/^OK/);
373                 die($_) if (/^ACK/);
374
375                 if (/^(\w+): (.*)$/) {
376                         if ($1 eq "file") {
377                                 if (exists($matches{$2})) {
378                                         $entry = $matches{$2};
379                                 } else {
380                                         $entry = {};
381                                         $matches{$2} = $entry;
382                                 }
383                         }
384
385                         add_track_metadata($entry, $1, $2);
386                 }
387         }
388
389         return \%matches;
390 }
391
392 # Given an artist MBID, return a hash reference containing associated
393 # releases in the MPD database.  The hash keys are release MBIDs.
394 #
395 # Since MPD returns results on a per-track basis, each entry in the
396 # hash has the metadata for one unspecified track from that release.
397 sub get_releases_by_artist_mbid {
398         my %releases;
399         my $entry;
400
401         foreach my $mbid (@_) {
402                 mpd_exec("search", "(MUSICBRAINZ_ARTISTID == \"$mbid\")");
403                 while (<$sock>) {
404                         last if (/^OK/);
405                         die($_) if (/^ACK/);
406
407                         if (/^(\w+): (.*)$/) {
408                                 if ($1 eq "file") {
409                                         $entry = {};
410                                 } elsif ($1 eq "MUSICBRAINZ_ALBUMID") {
411                                         $releases{$2} //= $entry;
412                                 }
413
414                                 add_track_metadata($entry, $1, $2);
415                         }
416                 }
417         }
418
419         return \%releases;
420 }
421
422 # Given a filename, return the IDs (if any) for that file in the
423 # current MPD play queue.
424 sub get_ids_by_filename {
425         my ($file) = @_;
426         my @results = ();
427
428         mpd_exec("playlistfind", "file", $file);
429         while (<$sock>) {
430                 last if (/^OK/);
431                 die($_) if (/^ACK/);
432
433                 if (/^(\w+): (.*)$/) {
434                         push @results, $2 if ($1 eq "Id");
435                 }
436         }
437
438         return @results;
439 }
440
441 # albumsort(matches, a, b)
442 #
443 # Sort hash keys (a, b) by disc/track number for album menus.
444 sub albumsort {
445         my ($matches, $a, $b) = @_;
446
447         return $matches->{$a}->{Disc} <=> $matches->{$b}->{Disc}
448             || $matches->{$a}->{Track} <=> $matches->{$b}->{Track}
449             || $a cmp $b;
450 }
451
452 # datesort(matches, a, b)
453 #
454 # Sort hash keys (a, b) by release date
455 sub datesort {
456         my ($matches, $a, $b) = @_;
457
458         return $matches->{$a}->{Date} cmp $matches->{$b}->{Date}
459             || $a cmp $b;
460 }
461
462 # menu_trackname(entry)
463 #
464 # Format the track name for display in an FVWM menu, where entry
465 # is a hash reference containing the track metadata.
466 sub menu_trackname {
467         my ($entry) = @_;
468         my $fmt = "$entry->{trackfmt}$entry->{Artist} - $entry->{Title}";
469         return "$entry->{thumb}" . fvwm_label_escape($fmt);
470 }
471
472 sub print_version {
473         print <<EOF
474 mpdmenu.pl 0.8
475 Copyright © 2019 Nick Bowler
476 License GPLv3+: GNU General Public License version 3 or any later version.
477 This is free software: you are free to change and redistribute it.
478 There is NO WARRANTY, to the extent permitted by law.
479 EOF
480 }
481
482 sub print_usage {
483         my $fh = $_[1] // *STDERR;
484
485         print $fh "Usage: $0 [options]\n";
486         print "Try $0 --help for more information.\n" unless (@_ > 0);
487 }
488
489 sub print_help {
490         print_usage(*STDOUT);
491         print <<EOF
492 This is "mpdmenu": a menu-based MPD client for FVWM.
493
494 Options:
495   -h, --host=HOST   Connect to the MPD server on HOST, overriding defaults.
496   -p, --port=PORT   Connect to the MPD server on PORT, overriding defaults.
497   -m, --menu=NAME   Set the name of the generated menu.
498   --album-id=MBID   Generate a menu for the given release MBID.
499   --artist-id=MBID  Generate a menu for the given artist MBID.
500   --track-id=MBID   Generate a menu for the given track MBID.
501   -V, --version     Print a version message and then exit.
502   -H, --help        Print this message and then exit.
503 EOF
504 }
505
506 GetOptions(
507         'host|h=s'    => \$host,
508         'port|p=s'    => \$port,
509         'menu|m=s'    => \$menu,
510
511         'artist-id=s' => sub { $artistids{$_[1]} = 1; $mode = "artist"; },
512         'album-id=s'  => sub { $albumid = $_[1]; $mode = "album"; },
513         'track-id=s'  => sub { $trackid = $_[1]; $mode = "track"; },
514
515         'V|version'   => sub { print_version(); exit },
516         'H|help'      => sub { print_help(); exit },
517 ) or do { print_usage; exit 1 };
518
519 # Connect to MPD.
520 $sock = new IO::Socket::INET6(
521         PeerAddr => $host,
522         PeerPort => $port,
523         Proto => 'tcp',
524         Timeout => 2
525 ) or die("could not open socket: $!.\n");
526 binmode($sock, ":utf8");
527
528 die("could not connect to MPD: $!.\n")
529         if (!(<$sock> =~ /^OK MPD ([0-9]+)\.([0-9]+)\.([0-9]+)$/));
530
531 die("MPD version $1.$2.$3 insufficient.\n")
532         if (  ($1 <  MPD_MJR_MIN)
533            || ($1 == MPD_MJR_MIN && $2 <  MPD_MNR_MIN)
534            || ($1 == MPD_MJR_MIN && $2 == MPD_MNR_MIN && $3 < MPD_REV_MIN));
535
536 if ($mode eq "top") {
537         my %current;
538         my %state;
539
540         $menu //= "MenuMPD";
541
542         mpd_exec("status");
543         while (<$sock>) {
544                 last if (/^OK/);
545                 die($_) if (/^ACK/);
546
547                 if (/^(\w+): (.*)$/) {
548                         $state{$1} = $2;
549                 }
550         }
551
552         mpd_exec("currentsong");
553         while (<$sock>) {
554                 last if (/^OK/);
555                 die($_) if (/^ACK/);
556
557                 if (/^(\w+): (.*)$/) {
558                         add_track_metadata(\%current, $1, $2);
559                 }
560         }
561
562         my $playstate = $state{state} eq "play"  ? "Playing"
563                       : $state{state} eq "stop"  ? "Stopped"
564                       : $state{state} eq "pause" ? "Paused"
565                       : "Unknown";
566         fvwm_cmd("AddToMenu", $menu, $playstate, "Title");
567
568         if (exists($current{file})) {
569                 top_track_cover(\%current);
570                 top_track_title(\%current);
571                 top_track_artist(\%current);
572                 top_track_album(\%current);
573         } else {
574                 fvwm_cmd("AddToMenu", $menu, "[current track unavailable]");
575         }
576
577         if ($state{state} =~ /^p/) {
578                 my $pp = $state{state} eq "pause" ? "lay" : "ause";
579
580                 fvwm_cmd("AddToMenu", $menu, "", "Nop");
581                 fvwm_cmd("AddToMenu", $menu, "Next%next.svg:16x16%",
582                        "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "next");
583                 fvwm_cmd("AddToMenu", $menu, "P$pp%p$pp.svg:16x16%",
584                        "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "p$pp");
585                 fvwm_cmd("AddToMenu", $menu, "Stop%stop.svg:16x16%",
586                        "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "stop");
587                 fvwm_cmd("AddToMenu", $menu, "Prev%prev.svg:16x16%",
588                        "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "previous");
589         } elsif ($state{state} eq "stop") {
590                 fvwm_cmd("AddToMenu", $menu, "", "Nop");
591                 fvwm_cmd("AddToMenu", $menu, "Play%play.svg:16x16%",
592                        "Exec", "exec", "$FindBin::Bin/mpdexec.pl", "play");
593         }
594 } elsif ($mode eq "album") {
595         my $matches = get_tracks_by_release_mbid($albumid);
596         my @notqueued = ();
597
598         $menu //= "MenuMPDAlbum";
599
600         my $track_max = max(map { $_->{Track} } values %$matches);
601         my $disc_max = max(map { $_->{Disc} } values %$matches);
602
603         # CDs have a max of 99 tracks and I hope 100+-disc-releases
604         # don't exist so this is fine.
605         my $track_digits = $track_max >= 10 ? 2 : 1;
606         my $disc_digits = $disc_max > 1 ? $disc_max >= 10 ? 2 : 1 : 0;
607         my $currentdisc;
608
609         fvwm_cmd("AddToMenu", $menu);
610         fvwm_cmd("+", "Release not found", "Title") unless keys %$matches;
611         foreach my $file (sort { albumsort($matches, $a, $b) } keys %$matches) {
612                 my $entry = $matches->{$file};
613
614                 # Format disc/track numbers
615                 $entry->{trackfmt} = sprintf("%*.*s%.*s%*d\t",
616                                   $disc_digits, $disc_digits, $entry->{Disc},
617                                   $disc_digits, "-",
618                                   $track_digits, $entry->{Track});
619                 $entry->{trackfmt} =~ s/ /\N{U+2007}/g;
620
621                 unless (exists $entry->{Id}) {
622                         my ($id) = get_ids_by_filename($file);
623                         if (defined $id) {
624                                 $entry->{Id} = $id;
625                         } else {
626                                 push @notqueued, $entry;
627                                 next;
628                         }
629                 }
630
631                 if (defined $currentdisc && $currentdisc != $entry->{Disc}) {
632                         fvwm_cmd("+", "", "Nop");
633                 }
634                 $currentdisc = $entry->{Disc};
635
636                 fvwm_cmd("+", menu_trackname($entry), "Exec",
637                          "exec", "$FindBin::Bin/mpdexec.pl",
638                          "playid", $entry->{Id});
639         }
640
641         fvwm_cmd("+", "Not in play queue", "Title") if @notqueued;
642         foreach my $entry (@notqueued) {
643                 fvwm_cmd("+", menu_trackname($entry));
644         }
645 } elsif ($mode eq "artist") {
646         # Create an artist menu.
647         my $matches = get_releases_by_artist_mbid(keys %artistids);
648         my $entry;
649
650         $menu //= "MenuMPDArtist";
651
652         my @mbids = sort { datesort($matches, $a, $b) } keys %$matches;
653         my @files = map { $matches->{$_}->{file} } @mbids;
654         my @thumbs = get_item_thumbnails({ small => 1 }, @files);
655         fvwm_cmd("AddToMenu", $menu, "No releases found", "Title") unless @mbids;
656
657         foreach my $mbid (@mbids) {
658                 my $entry = $matches->{$mbid};
659                 my $thumb = shift @thumbs;
660
661                 my @submenu = make_submenu("$menu-$mbid", "--album-id=$mbid");
662                 fvwm_cmd("AddToMenu", $menu,
663                          $thumb . fvwm_label_escape($entry->{Album}),
664                          @submenu);
665         }
666 } elsif ($mode eq "track") {
667         my $matches = get_tracks_by_track_mbid($trackid);
668         my @notqueued;
669
670         $menu //= "MenuMPDTrack";
671
672         my @files = sort { datesort($matches, $a, $b) } keys %$matches;
673         my @thumbs = get_item_thumbnails({ small => 1 }, @files);
674
675         fvwm_cmd("AddToMenu", $menu);
676         fvwm_cmd("+", "No tracks found", "Title") unless @files;
677         foreach my $file (@files) {
678                 my $entry = $matches->{$file};
679                 $entry->{thumb} = shift @thumbs;
680
681                 unless (exists $entry->{Id}) {
682                         my ($id) = get_ids_by_filename($file);
683                         if (defined $id) {
684                                 $entry->{Id} = $id;
685                         } else {
686                                 push @notqueued, $entry;
687                                 next;
688                         }
689                 }
690
691                 fvwm_cmd("+", menu_trackname($entry), "Exec",
692                          "exec", "$FindBin::Bin/mpdexec.pl",
693                          "playid", $entry->{Id});
694         }
695
696         fvwm_cmd("+", "Not in play queue", "Title") if @notqueued;
697         foreach my $entry (@notqueued) {
698                 fvwm_cmd("+", menu_trackname($entry));
699         }
700 }
701
702 # Finished.
703 print $sock "close\n";