2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 ############################################################################
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License, version 2, as
7 # published by the Free Software Foundation.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # A copy of the GNU General Public License is available on the World Wide Web
15 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
16 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 # Boston, MA 02111-1307, USA.
18 ############################################################################
26 # find program directory
29 my $l = readlink or die "readlink $_: $!\n";
30 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
33 my $progdir = abs_path
($1);
35 require "$progdir/soepkiptng.lib";
39 read_configfile
(\
%conf, $opt_c);
41 eval "use Term::ReadKey;";
43 warn "Term::ReadKey not found; assuming 80-column terminal.\n";
47 ($screen_width) = GetTerminalSize
();
54 my ($album, $track) = @_;
58 $album = substr($album, 0, $w_al - length($nr));
59 if(length($album) != $w_al - length($nr)) { $album .= " "; }
62 $album = substr($album, 0, $w_al);
71 $val =~ s/\b\s+\b/,/g;
73 foreach(split /,/, $val) {
78 for($i = $b; $i <= $e; $i++) {
90 ($song_id) = kill_song
('', $killid);
91 if(!defined($song_id)) {
92 print "not killing (song $killid not playing anymore)\n";
95 ($t, $a, $al, $tr) = $dbh->selectrow_array(
96 "SELECT song.title,artist.name,album.name,song.track" .
97 " FROM song,artist,album" .
98 " WHERE song.id=$song_id" .
99 " AND song.artist_id=artist.id AND song.album_id=album.id"
101 printf "next: %s - %02d. %s [%s]\n", $a, $tr, $t, $al;
106 $dbh = connect_to_db
(\
%conf);
109 open F
, $conf{statusfile
} or exit;
113 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
115 my $song = add_song_next
($dbh, "queue", $nowplaying, $user)
116 or die "No next song.\n";
118 print "Adding next song ($song->{track}, $song->{title}).\n";
122 $screen_width = 80 if $screen_width == 0; # just in case
123 $w_a = $screen_width * 25 / 100;
124 $w_t = $screen_width * 45 / 100;
125 $w_al = $screen_width - $w_a - $w_t - 7;
126 $w_a > 0 && $w_t > 0 && $w_al > 0 or die "screen size too small.\n";
128 if($0 =~ /qr$/ || @ARGV) {
132 if(s/^-//) { $q = " NOT"; }
135 $a =~ s/^\^// or $a = "%$a";
136 $a =~ s/\$$// or $a .= "%";
138 if($0 =~ /qa$/ || s/^ar=//) {
139 $q .= " (artist.name LIKE ?)";
142 if($0 =~ /qa$/ || s/^f=//) {
143 $q .= " (song.filename LIKE ?)";
146 elsif($0 =~ /ql$/ || s/^al=//) {
147 $q .= " (album.name LIKE ?)";
150 elsif($0 =~ /qt$/ || s/^t=//) {
151 $q .= " (song.title LIKE ?)";
155 $q .= " (song.title LIKE ? OR artist.name LIKE ? OR album.name LIKE ?)";
161 push @q, "track > 0";
164 push @q, "track <= $opt_t";
167 push @q, " last_played=0 AND (unix_timestamp(now()) - unix_timestamp(time_added)) < 7*86400";
169 $q = "SELECT title,artist.name as artist,album.name as album,song.id as id,track,filename,length,encoding" .
170 " FROM song,artist,album" .
171 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
172 " AND present AND filename LIKE '/%' AND " . join(" AND ", @q) .
173 " ORDER BY artist.name,album.name,song.track,song.title";
175 $sth = $dbh->prepare($q);
178 $head = sprintf("\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
179 'Artist', 'Song', 'Album', '-'x
$w_a, '-'x
$w_t, '-'x
$w_al);
182 while($_ = $sth->fetchrow_hashref) {
184 my $f = $_->{filename
};
186 $f =~ s
|^(.*)/|| or die;
188 $d =~ s
|^.*/|| or die;
190 $f =~ /([^-\w])/ and die "\nERROR: invalid character ($1) in filename ($f)!\n";
191 if(defined($dirname)) {
192 $dirname eq $d or die "\nERROR: inconsistent dirname ($dirname, $d)\n";
195 $g_output .= ">$dirname\n";
197 $_->{title
} =~ s/^(\W+)/<$1>/;
198 $g_artist = $_->{artist
} unless $g_artist;
199 $g_album = $_->{album
} unless $g_album;
200 $g_output .= "-$_->{track}\n" if $_->{track
};
201 $g_output .= "/$f\n";
202 $g_output .= ":$_->{artist}\n" unless $_->{artist
} eq $g_artist;
203 $g_output .= "$_->{title}\n";
208 # $sep = ($i % 5) == 4? "_":" ";
209 # printf STDERR "%-3s$sep%-${w_a}.${w_a}s$sep%-${w_t}.${w_t}s$sep%-${w_al}.${w_al}s\n",
210 # "$i.", $_->{artist}, $_->{title}, albumtrack($_->{album}, $_->{track});
211 my $s = sprintf "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
212 "$i.", $_->{artist
}, $_->{title
}, albumtrack
($_->{album
}, $_->{track
});
213 $s =~ s/ /_/g if $i % 5 == 0;
215 $opt_v and printf STDERR
"%d:%02d %s %s\n", $_->{length} / 60, $_->{length} % 60,
216 $_->{encoding
}, $_->{filename
};
221 print "$g_artist-$g_album\n";
227 $i == 2 or die "More than 1 hit\n";
230 print STDERR
"\nAdd (a=all): ";
235 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
238 for($n = 1; $n < $i; $n++) {
239 add_song
($dbh, "queue", $user, $id[$n]) or warn "can't add song.\n";
242 foreach(splitrange
($_, $i)) {
243 add_song
($dbh, "queue", $user, $id[$_]) or warn "can't add song.\n"
249 printf "\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
250 'Artist', 'Song', 'Album', '-'x
$w_a, '-'x
$w_t, '-'x
$w_al;
253 if($_ = get_nowplaying
($dbh)) {
254 printf "1.* %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
255 $_->{artist
}, $_->{title
}, albumtrack
($_->{album
}, $_->{track
});
256 if($_->{id
}) { $delid[$i++] = $_->{id
}; }
260 $query = "SELECT song.title,artist.name,album.name,song.id,song.track" .
261 " FROM song,artist,album,queue" .
262 " WHERE song.id=queue.song_id" .
263 " AND song.artist_id=artist.id AND song.album_id=album.id" .
264 " ORDER BY queue.song_order";
265 $sth = $dbh->prepare($query);
267 while(@q = $sth->fetchrow_array) {
268 printf "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
269 "$i.", $q[1], $q[0], albumtrack
($q[2], $q[4]);
275 print STDERR
"\nDelete (a=all): ";
279 shuffle_table
($dbh, "queue");
281 $sth = $dbh->prepare("DELETE FROM queue");
285 foreach(splitrange
($_, $i)) {
286 if($_ == 1) { killit
($nowid); }
287 else { del_song
($dbh, "queue", $delid[$_]) if $delid[$_]; }