2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
5 ############################################################################
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License, version 2, as
8 # published by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # A copy of the GNU General Public License is available on the World Wide Web
16 # at `http://www.gnu.org/copyleft/gpl.html'. You can also obtain it by
17 # writing to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 ############################################################################
27 # find program directory
30 my $l = readlink or die "readlink $_: $!\n";
31 if($l =~ m
|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
34 my $progdir = abs_path
($1);
36 require "$progdir/soepkiptng.lib";
40 read_configfile
(\
%conf, $opt_c);
42 eval "use Term::ReadKey;";
44 warn "Term::ReadKey not found; assuming 80-column terminal.\n";
48 ($screen_width) = GetTerminalSize
();
55 my ($album, $track) = @_;
59 $album = substr($album, 0, $w_al - length($nr));
60 if(length($album) != $w_al - length($nr)) { $album .= " "; }
63 $album = substr($album, 0, $w_al);
72 $val =~ s/\b\s+\b/,/g;
74 foreach(split /,/, $val) {
79 for($i = $b; $i <= $e; $i++) {
91 ($song_id) = kill_song
('', $killid);
92 if(!defined($song_id)) {
93 print "not killing (song $killid not playing anymore)\n";
96 ($t, $a, $al, $tr) = $dbh->selectrow_array(
97 "SELECT song.title,artist.name,album.name,song.track" .
98 " FROM song,artist,album" .
99 " WHERE song.id=$song_id" .
100 " AND song.artist_id=artist.id AND song.album_id=album.id"
102 printf "next: %s - %02d. %s [%s]\n", $a, $tr, $t, $al;
107 $dbh = DBI
->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
108 $conf{db_user
}, $conf{db_pass
}) or die "can't connect to database";
111 open F
, $conf{statusfile
} or exit;
115 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
117 my $song = add_song_next
($dbh, "queue", $nowplaying, $user)
118 or die "No next song.\n";
120 print "Adding next song ($song->{track}, $song->{title}).\n";
124 $screen_width = 80 if $screen_width == 0; # just in case
125 $w_a = $screen_width * 25 / 100;
126 $w_t = $screen_width * 45 / 100;
127 $w_al = $screen_width - $w_a - $w_t - 7;
128 $w_a > 0 && $w_t > 0 && $w_al > 0 or die "screen size too small.\n";
130 if($0 =~ /qr$/ || @ARGV) {
134 if(s/^-//) { $q = " NOT"; }
137 $a =~ s/^\^// or $a = "%$a";
138 $a =~ s/\$$// or $a .= "%";
140 if($0 =~ /qa$/ || s/^ar=//) {
141 $q .= " (artist.name LIKE ?)";
144 if($0 =~ /qa$/ || s/^f=//) {
145 $q .= " (song.filename LIKE ?)";
148 elsif($0 =~ /ql$/ || s/^al=//) {
149 $q .= " (album.name LIKE ?)";
152 elsif($0 =~ /qt$/ || s/^t=//) {
153 $q .= " (song.title LIKE ?)";
157 $q .= " (song.title LIKE ? OR artist.name LIKE ? OR album.name LIKE ?)";
163 push @q, "track > 0";
166 push @q, "track <= $opt_t";
169 push @q, " last_played=0 AND (unix_timestamp(now()) - unix_timestamp(time_added)) < 7*86400";
171 $q = "SELECT title,artist.name as artist,album.name as album,song.id as id,track,filename,length,encoding" .
172 " FROM song,artist,album" .
173 " WHERE song.artist_id=artist.id AND song.album_id=album.id" .
174 " AND present AND filename LIKE '/%' AND " . join(" AND ", @q) .
175 " ORDER BY artist.name,album.name,song.track,song.title";
177 $sth = $dbh->prepare($q);
180 $head = sprintf("\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
181 'Artist', 'Song', 'Album', '-'x
$w_a, '-'x
$w_t, '-'x
$w_al);
184 while($_ = $sth->fetchrow_hashref) {
186 my $f = $_->{filename
};
188 $f =~ s
|^(.*)/|| or die;
190 $d =~ s
|^.*/|| or die;
192 $f =~ /([^-\w])/ and die "\nERROR: invalid character ($1) in filename ($f)!\n";
193 if(defined($dirname)) {
194 $dirname eq $d or die "\nERROR: inconsistent dirname ($dirname, $d)\n";
197 $g_output .= ">$dirname\n";
199 $_->{title
} =~ s/^(\W+)/<$1>/;
200 $g_artist = $_->{artist
} unless $g_artist;
201 $g_album = $_->{album
} unless $g_album;
202 $g_output .= "-$_->{track}\n" if $_->{track
};
203 $g_output .= "/$f\n";
204 $g_output .= ":$_->{artist}\n" unless $_->{artist
} eq $g_artist;
205 $g_output .= "$_->{title}\n";
210 # $sep = ($i % 5) == 4? "_":" ";
211 # printf STDERR "%-3s$sep%-${w_a}.${w_a}s$sep%-${w_t}.${w_t}s$sep%-${w_al}.${w_al}s\n",
212 # "$i.", $_->{artist}, $_->{title}, albumtrack($_->{album}, $_->{track});
213 my $s = sprintf "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
214 "$i.", $_->{artist
}, $_->{title
}, albumtrack
($_->{album
}, $_->{track
});
215 $s =~ s/ /_/g if $i % 5 == 0;
217 $opt_v and printf STDERR
"%d:%02d %s %s\n", $_->{length} / 60, $_->{length} % 60,
218 $_->{encoding
}, $_->{filename
};
223 print "$g_artist-$g_album\n";
229 $i == 2 or die "More than 1 hit\n";
232 print STDERR
"\nAdd (a=all): ";
237 my $user = (getpwuid $<)[6] || getpwuid $< || "uid $<";
240 for($n = 1; $n < $i; $n++) {
241 add_song
($dbh, "queue", $user, $id[$n]) or warn "can't add song.\n";
244 foreach(splitrange
($_, $i)) {
245 add_song
($dbh, "queue", $user, $id[$_]) or warn "can't add song.\n"
251 printf "\n %-${w_a}s %-${w_t}s %-${w_al}s\n %s %s %s\n",
252 'Artist', 'Song', 'Album', '-'x
$w_a, '-'x
$w_t, '-'x
$w_al;
255 if($_ = get_nowplaying
($dbh)) {
256 printf "1.* %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
257 $_->{artist
}, $_->{title
}, albumtrack
($_->{album
}, $_->{track
});
258 if($_->{id
}) { $delid[$i++] = $_->{id
}; }
262 $query = "SELECT song.title,artist.name,album.name,song.id,song.track" .
263 " FROM song,artist,album,queue" .
264 " WHERE song.id=queue.song_id" .
265 " AND song.artist_id=artist.id AND song.album_id=album.id" .
266 " ORDER BY queue.song_order";
267 $sth = $dbh->prepare($query);
269 while(@q = $sth->fetchrow_array) {
270 printf "%-3s %-${w_a}.${w_a}s %-${w_t}.${w_t}s %-${w_al}.${w_al}s\n",
271 "$i.", $q[1], $q[0], albumtrack
($q[2], $q[4]);
277 print STDERR
"\nDelete (a=all): ";
281 shuffle_table
($dbh, "queue");
283 $sth = $dbh->prepare("DELETE FROM queue");
287 foreach(splitrange
($_, $i)) {
288 if($_ == 1) { killit
($nowid); }
289 else { del_song
($dbh, "queue", $delid[$_]) if $delid[$_]; }