continuously show lyrics (on terminal, using less)
[soepkiptng.git] / soepkiptng_show_lyrics
blobff51e9ce4c484b08fd53aba27bfd6a48e2af2fd0
1 #!/usr/bin/perl
2 ############################################################################
3 # soepkiptng (c) copyright 2000 Eric Lammerts <eric@lammerts.org>.
4 # $Id$
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 ############################################################################
21 use integer;
22 use Cwd 'abs_path';
23 use DBI;
24 use Socket;
25 use Getopt::Std;
26 use POSIX ":sys_wait_h";
28 # find program directory
29 $_ = $0;
30 while(-l) {
31 my $l = readlink or die "readlink $_: $!\n";
32 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
34 m|(.*)/|;
35 my $progdir = abs_path($1);
37 require "$progdir/soepkiptng.lib";
39 getopts('avnc:g');
41 read_configfile(\%conf, $opt_c);
43 $dbh = DBI->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}",
44 $conf{db_user}, $conf{db_pass}) or die "can't connect to database";
46 $pid = 0;
47 open STDOUT, ">/dev/null";
48 $SIG{CHLD} = sub {
49 while(waitpid(-1,WNOHANG) > 0) {
50 exit 0 if $@ == 0;
53 for(;;) {
54 while((stat $conf{statusfile})[9] == $mtime) { sleep 1; }
55 $mtime = (stat _)[9];
56 if($pid) { kill 9, $pid; wait; }
57 print STDERR "\n"x100;
58 open F, $conf{statusfile} or next;
59 chop($id = <F>);
60 close F;
62 ($lyrics, $ar, $ti, $al, $tr) = $dbh->selectrow_array(
63 "SELECT lyrics.lyrics, artist.name, song.title, album.name, song.track " .
64 "FROM song " .
65 "LEFT JOIN album ON album.id=song.album_id " .
66 "LEFT JOIN artist ON artist.id=song.artist_id " .
67 "LEFT JOIN lyrics ON lyrics.id=song.id " .
68 "WHERE song.id=$id");
69 $lyrics or next;
71 pipe STDIN, STDOUT;
72 if(($pid = fork) == 0) {
73 open STDOUT, ">/dev/tty";
74 exec "less", "-c";
75 die "less -c: $!\n";
77 close STDIN;
78 $t = "$ar - $ti ($al [$tr])";
79 print "$t\n";
80 print "-" x length($t);
81 print "\n\n$lyrics\n";
82 close STDOUT;