| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Audio::MPD q{0.19.0}; |
| 5 |
use MpdToys; |
| 6 |
use Getopt::Long; |
| 7 |
use Term::ReadKey; |
| 8 |
use Encode; |
| 9 |
|
| 10 |
=head1 NAME |
| 11 |
|
| 12 |
mprompt - simple prompt-based control for mpd |
| 13 |
|
| 14 |
=head1 SYNOPSIS |
| 15 |
|
| 16 |
mpompt [-s] [-m key=key] [-t n] [-f] [tty] [-T] [host] |
| 17 |
|
| 18 |
=cut |
| 19 |
|
| 20 |
sub usage { |
| 21 |
die "Usage: mprompt [-s] [-m key=key] [-t n] [-f] [-t] [tty] [host]\n"; |
| 22 |
} |
| 23 |
|
| 24 |
=head1 DESCRIPTION |
| 25 |
|
| 26 |
B<mprompt> is a mpd client with a prompt-based interface. It is |
| 27 |
designed to be usable on a headless machine. |
| 28 |
|
| 29 |
At the prompt, enter the name of a playlist, or part of the name of an |
| 30 |
album, artist, or song. Matching items will start playing. You can also |
| 31 |
paste in urls to stream. |
| 32 |
|
| 33 |
(If the perl String::Approx module is available, it will be used to handle |
| 34 |
typos, etc in the names you enter.) |
| 35 |
|
| 36 |
Use the left and right arrow keys to adjust volume, and the up and down |
| 37 |
arrow keys to move through the playlist. |
| 38 |
|
| 39 |
The Tab and Enter keys can both be used to pause and unpause playback. |
| 40 |
(Enter toggles pause only if nothing has been entered at the prompt.) |
| 41 |
|
| 42 |
Example of how to run mprompt in /etc/inittab: |
| 43 |
|
| 44 |
1:2345:respawn:/usr/bin/mprompt /dev/tty1 |
| 45 |
|
| 46 |
=head1 OPTIONS |
| 47 |
|
| 48 |
=over 4 |
| 49 |
|
| 50 |
=item -s |
| 51 |
|
| 52 |
This option allows shell commands to be typed in to mprompt, to be |
| 53 |
run by whatever user it is running as. (Typically root if it is run from |
| 54 |
/etc/inittab). |
| 55 |
|
| 56 |
To enter a shell command, type a "!", followed by the command to run, |
| 57 |
followed by Enter. |
| 58 |
|
| 59 |
=item -m key=key |
| 60 |
|
| 61 |
This option allows remapping keys. Any key can be remapped to any other |
| 62 |
key, which is useful to support keyboard with unusual key layouts, or |
| 63 |
missing keys. |
| 64 |
|
| 65 |
For alphanumeric and punctuation keys, individual symbols can be remapped. |
| 66 |
For example, "-m a=b" will turn each entered "a" into "b". |
| 67 |
|
| 68 |
For other keys, use the following names: |
| 69 |
|
| 70 |
=over 4 |
| 71 |
|
| 72 |
=item <return> |
| 73 |
|
| 74 |
=item <tab> |
| 75 |
|
| 76 |
=item <space> |
| 77 |
|
| 78 |
=item <up> |
| 79 |
|
| 80 |
=item <down> |
| 81 |
|
| 82 |
=item <left> |
| 83 |
|
| 84 |
=item <right> |
| 85 |
|
| 86 |
=item <backspace> |
| 87 |
|
| 88 |
=back |
| 89 |
|
| 90 |
For example, -m "n=<down>" will map the "n" key to the down arrow, causing |
| 91 |
that key to change to the next track; -m "<space>=<return>" will make the space |
| 92 |
bar act as a pause. |
| 93 |
|
| 94 |
It's possible to swap keys too. For example, -m "<down>=<up>" -m "<up>=<down>" |
| 95 |
|
| 96 |
A single key can also be bound to a series of keystrokes. For example, |
| 97 |
-m "1=Mule Variations<return>" will cause the "1" key to play the "Mule |
| 98 |
Variations" album, a nice choice. |
| 99 |
|
| 100 |
=item -t n |
| 101 |
|
| 102 |
Adds a timeout, a specified number of seconds after which the entry |
| 103 |
on the command line will be cleared. Useful for headless systems, to avoid |
| 104 |
cat-on-keyboard confusing your later commands. |
| 105 |
|
| 106 |
=item -T |
| 107 |
|
| 108 |
Enables terse output mode. This mode tries to avoid displaying excessive |
| 109 |
or complex things, with the intent that mprompt's output can be piped into |
| 110 |
a speech synthesiser, such as espeak. |
| 111 |
|
| 112 |
=back |
| 113 |
|
| 114 |
=head1 SEE ALSO |
| 115 |
|
| 116 |
vipl(1) mptoggle(1) mpd(1) |
| 117 |
|
| 118 |
=head1 AUTHOR |
| 119 |
|
| 120 |
Copyright 2009 Joey Hess <[email protected]> |
| 121 |
|
| 122 |
Licensed under the GNU GPL version 2 or higher. |
| 123 |
|
| 124 |
http://kitenet.net/~joey/code/mpdtoys |
| 125 |
|
| 126 |
=cut |
| 127 |
|
| 128 |
my $tty; |
| 129 |
my $shell=0; |
| 130 |
my $timeout=0; |
| 131 |
my $terse=0; |
| 132 |
my %controlchars = GetControlChars; |
| 133 |
my %keysyms = ( |
| 134 |
"\n" => '<return>', |
| 135 |
"\t" => '<tab>', |
| 136 |
" " => '<space>', |
| 137 |
"\e[A" => '<up>', |
| 138 |
"\e[3~" => '<up>', # delete on some terminals, raw on others |
| 139 |
"\e[B" => '<down>', |
| 140 |
"\e[D" => '<left>', |
| 141 |
"\e[C" => '<right>', |
| 142 |
$controlchars{ERASE} => "<backspace>", |
| 143 |
); |
| 144 |
my %keymap; |
| 145 |
|
| 146 |
Getopt::Long::Configure("no_ignore_case"); |
| 147 |
GetOptions( |
| 148 |
"s" => \$shell, |
| 149 |
"m=s" => sub { |
| 150 |
my ($old, $new)=split(/=/, $_[1], 2); |
| 151 |
$keymap{$old}=$new; |
| 152 |
}, |
| 153 |
"t=i" => \$timeout, |
| 154 |
"f" => sub { print STDERR "the -f option is now enabled by default\n" }, |
| 155 |
"T" => \$terse, |
| 156 |
) || usage(); |
| 157 |
|
| 158 |
if (@ARGV) { |
| 159 |
$tty=shift; |
| 160 |
close STDIN; |
| 161 |
close STDOUT; |
| 162 |
open(STDIN, "<", $tty) || die "open $tty: $!"; |
| 163 |
open(STDOUT, ">", $tty) || die "open $tty: $!"; |
| 164 |
} |
| 165 |
if (@ARGV) { |
| 166 |
$ENV{MPD_HOST}=shift; |
| 167 |
} |
| 168 |
my $mpd=Audio::MPD->new(conntype => "reuse"); |
| 169 |
|
| 170 |
sub quit { |
| 171 |
ReadMode("restore"); |
| 172 |
exit(0); |
| 173 |
}; |
| 174 |
$SIG{INT}=$SIG{TERM}=\&quit; |
| 175 |
ReadMode("raw"); |
| 176 |
|
| 177 |
$|=1; |
| 178 |
|
| 179 |
my $line=""; |
| 180 |
my $sequence; |
| 181 |
my $laststroke=time; |
| 182 |
|
| 183 |
showprompt(); |
| 184 |
|
| 185 |
KEY: while (my $key = ReadKey(0)) { |
| 186 |
if ($timeout) { |
| 187 |
if (length $line && time - $laststroke > $timeout) { |
| 188 |
$line=""; |
| 189 |
print " <timeout>\n" unless $terse; |
| 190 |
showprompt(); |
| 191 |
} |
| 192 |
$laststroke=time; |
| 193 |
} |
| 194 |
|
| 195 |
if ($key eq $controlchars{INTERRUPT} || |
| 196 |
$key eq $controlchars{EOF}) { |
| 197 |
quit(); |
| 198 |
} |
| 199 |
|
| 200 |
# Sequences are started with escape, and accumulated |
| 201 |
# until a recognised sequence is seen, or until it becomes clear |
| 202 |
# that it is not part of a recognised sequence. |
| 203 |
if (defined $sequence) { |
| 204 |
$sequence.=$key; |
| 205 |
if (exists $keysyms{$sequence}) { |
| 206 |
$key=$sequence; |
| 207 |
$sequence=undef; |
| 208 |
} |
| 209 |
else { |
| 210 |
foreach my $sym (keys %keysyms) { |
| 211 |
if ($sym=~/^\Q$sequence\E/) { |
| 212 |
next KEY; # unfinished sequence |
| 213 |
} |
| 214 |
} |
| 215 |
$key=$sequence; |
| 216 |
$sequence=undef; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
$key = $keysyms{$key} if exists $keysyms{$key}; |
| 221 |
$key = $keymap{$key} if exists $keymap{$key}; |
| 222 |
|
| 223 |
# The key may be mapped to a multiple letter sequence. |
| 224 |
while (length $key) { |
| 225 |
if ($key=~s/^(<[^>]+>)//) { |
| 226 |
handle($1); |
| 227 |
} |
| 228 |
elsif ($key=~s/(.)//) { |
| 229 |
handle($1); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
sub handle { |
| 235 |
my $key=shift; |
| 236 |
|
| 237 |
if ($key eq "\e") { |
| 238 |
$sequence=$key; |
| 239 |
} |
| 240 |
elsif ($key eq '<return>') { |
| 241 |
if ($shell && $line =~ /^\!(.*)/) { |
| 242 |
print "\nrunning $1\n"; |
| 243 |
system($1); |
| 244 |
} |
| 245 |
elsif (length $line && $line !~ /^\s*$/) { |
| 246 |
queue($line); |
| 247 |
} |
| 248 |
else { |
| 249 |
toggle(); |
| 250 |
} |
| 251 |
$line=""; |
| 252 |
showprompt(); |
| 253 |
} |
| 254 |
elsif ($key eq '<backspace>') { |
| 255 |
if (length $line) { |
| 256 |
chop $line; |
| 257 |
print "\b \b"; |
| 258 |
} |
| 259 |
} |
| 260 |
elsif ($key eq '<space>') { |
| 261 |
print " "; |
| 262 |
$line.=" "; |
| 263 |
} |
| 264 |
elsif ($key eq '<tab>') { |
| 265 |
toggle(); |
| 266 |
showprompt(); |
| 267 |
} |
| 268 |
elsif ($key eq '<left>') { |
| 269 |
adjustvolume(-5); |
| 270 |
} |
| 271 |
elsif ($key eq '<right>') { |
| 272 |
adjustvolume(+5); |
| 273 |
} |
| 274 |
elsif ($key eq '<up>') { |
| 275 |
$mpd->prev; |
| 276 |
$mpd->play; |
| 277 |
showplaying(); |
| 278 |
showprompt(); |
| 279 |
} |
| 280 |
elsif ($key eq '<down>') { |
| 281 |
$mpd->next; |
| 282 |
$mpd->play; |
| 283 |
showplaying(); |
| 284 |
showprompt(); |
| 285 |
} |
| 286 |
else { |
| 287 |
print "$key"; |
| 288 |
$line.=$key; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
sub adjustvolume { |
| 293 |
my $amount=shift; |
| 294 |
my $vol=$mpd->status->volume; |
| 295 |
|
| 296 |
$vol+=$amount; |
| 297 |
if ($vol > 100) { |
| 298 |
$vol=100; |
| 299 |
} |
| 300 |
elsif ($vol < 0) { |
| 301 |
$vol=0; |
| 302 |
} |
| 303 |
|
| 304 |
if (! $terse) { |
| 305 |
print "\nvolume: $vol%\n"; |
| 306 |
} |
| 307 |
$mpd->volume($vol); |
| 308 |
showprompt(); |
| 309 |
} |
| 310 |
|
| 311 |
sub showprompt { |
| 312 |
print "> $line"; |
| 313 |
} |
| 314 |
|
| 315 |
sub showplaying { |
| 316 |
print "\n"; |
| 317 |
my $song=$mpd->current; |
| 318 |
if (! defined $song) { |
| 319 |
print "nothing queued\n"; |
| 320 |
} |
| 321 |
else { |
| 322 |
if (! $terse) { |
| 323 |
print encode_utf8($song->as_string)."\n"; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
sub toggle { |
| 329 |
$mpd->pause; |
| 330 |
my $state=$mpd->status->state; |
| 331 |
print "\n"; |
| 332 |
print "$state\n" if ! $terse || $state ne "play"; |
| 333 |
} |
| 334 |
|
| 335 |
sub queue { |
| 336 |
my $line=shift; |
| 337 |
|
| 338 |
print "\n"; |
| 339 |
|
| 340 |
my $pl=$mpd->playlist; |
| 341 |
|
| 342 |
eval q{$pl->load($line)}; |
| 343 |
if (! $@) { |
| 344 |
$pl->clear; |
| 345 |
$pl->load($line); |
| 346 |
$mpd->play; |
| 347 |
|
| 348 |
print "added $line playlist"; |
| 349 |
showplaying(); |
| 350 |
return; |
| 351 |
} |
| 352 |
|
| 353 |
my @matches=MpdToys::findmatchingsongs($line, $mpd); |
| 354 |
if (! @matches && MpdToys::canmatch_fuzzy()) { |
| 355 |
print "trying fuzzy match..\n"; |
| 356 |
@matches=MpdToys::findmatchingsongs_fuzzy($line, $mpd); |
| 357 |
} |
| 358 |
if (@matches) { |
| 359 |
$pl->clear; |
| 360 |
foreach (@matches) { |
| 361 |
$pl->add($_->file); |
| 362 |
} |
| 363 |
$mpd->play; |
| 364 |
print "added ".int(@matches)." songs"; |
| 365 |
showplaying(); |
| 366 |
} |
| 367 |
else { |
| 368 |
print "no matches found for \"$line\"\n"; |
| 369 |
} |
| 370 |
} |