use SNDCTL_DSP_GETODELAY for more accurate time display
[soepkiptng.git] / gtk_soepkiptng_status
blobd1ee475cd5cb2eff8842fff2e50f9d04bcfd0c03
1 #!/usr/bin/perl
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 ############################################################################
20 use Cwd 'abs_path';
21 use DBI;
22 use Getopt::Std;
23 use Gnome;
25 # find program directory
26 $_ = $0;
27 while(-l) {
28 my $l = readlink or die "readlink $_: $!\n";
29 if($l =~ m|^/|) { $_ = $l; } else { s|[^/]*$|/$l|; }
31 m|(.*)/|;
32 my $progdir = abs_path($1);
34 require "$progdir/soepkiptng.lib";
36 getopts('c:');
38 read_configfile(\%conf, $opt_c);
40 init Gnome "soepkiptng.pl";
42 #init Gnome::Panel::AppletWidget 'soepkiptng.pl';
44 $SIG{'HUP'} = sub {
45 exec $0;
46 die;
49 my $pause_xpm = (
50 "16 16 4 1",
51 " c None s None",
52 ". c black",
53 "X c #808080",
54 "o c white",
55 " ",
56 " ",
57 " ",
58 " ",
59 " ... ... ",
60 " ... ... ",
61 " ... ... ",
62 " ... ... ",
63 " ... ... ",
64 " ... ... ",
65 " ... ... ",
66 " ",
67 " ",
68 " ",
69 " ",
70 " ");
72 #my $window = new Gtk::Window;
73 my $window = new Gtk::Widget "GtkWindow",
74 type => -toplevel,
75 title => "SoepkipTNG status",
76 allow_grow => 1,
77 allow_shrink => 1,
80 #$window = new Gnome::Panel::AppletWidget 'soepkiptng.pl';
81 #realize $window;
83 #my $Label = Gtk::Widget->new("Gnome::CanvasText",
84 # text => "A string\nToinen rivi",
85 # font_gdk => load Gtk::Gdk::Font('-*-helvetica-*'),
86 # fill_color => 'steelblue',
87 # anchor => 'center',
88 #);
90 my $label = new Gtk::Label("Artist\nTitle\nAlbum");
91 $label->set_justify('left');
92 #$label->set_usize(300,40);
94 my $label2 = new Gtk::Label("");
96 $button_pause = Gtk::Widget->new("GtkButton",
97 -label => "Pause",
98 -border_width => 1,
99 -clicked => sub { player_cmd("pausetoggle"); },
100 -visible=>1,
103 $button_kill = Gtk::Widget->new("GtkButton",
104 -label => "Kill",
105 -border_width => 1,
106 -clicked => \&kill_song,
107 -visible=>1,
111 $hbox = new Gtk::HBox 0, 1;
112 show $hbox;
113 $window->add($hbox);
115 $vbox = new Gtk::VBox 0, 1;
117 $hbox->pack_start($label, 0, 1, 3);
118 $hbox->pack_start($label2, 1, 1, 3);
119 $hbox->pack_start($vbox, 0, 0, 0);
121 $vbox->pack_start($button_pause, 0, 0, 1);
122 #$vbox->pack_start($button_resume, 0, 0, 1);
123 $vbox->pack_start($button_kill, 0, 0, 1);
126 #$splashimage = new Gtk::Pixmap($p,$pause_xpm)
128 $mtime = -1;
129 $paused = -1;
130 update_song();
131 my $id = Gtk->timeout_add(250, \&update_song);
133 $window->signal_connect('destroy', sub { Gtk->exit(0); });
135 $window->show_all;
137 Gtk->main;
138 # Gnome::Panel::AppletWidget;
141 sub update_song() {
142 my $newmtime = (stat $conf{statusfile})[9];
144 if($newmtime != $mtime) {
145 $mtime = $newmtime;
146 if(open F, $conf{statusfile}) {
147 my ($nowplaying, $filename);
148 chop($nowplaying = <F>);
149 chop($filename = <F>);
150 <F>;
151 $playerpid = 0 + <F>;
152 chop ((undef, undef, undef, undef,
153 $now_playing->{artist},
154 $now_playing->{title},
155 $now_playing->{album},
156 $now_playing->{track}) = <F>);
157 close F;
159 if($nowplaying > 0) {
160 my $query = "SELECT song.title as title,artist.name as artist,album.name as album,song.id,song.track" .
161 " FROM song,artist,album" .
162 " WHERE song.id=?" .
163 " AND song.artist_id=artist.id AND song.album_id=album.id";
165 if($dbh = DBI->connect("DBI:$conf{db_type}:$conf{db_name}:$conf{db_host}", $conf{db_user}, $conf{db_pass})) {
166 my $sth = $dbh->prepare($query);
167 my $rv = $sth->execute($nowplaying);
168 if(my $n = $sth->fetchrow_hashref) {
169 $now_playing = $n;
171 $sth->finish;
172 $dbh->disconnect;
174 $label->set_text(sprintf("%s\n%s\n[%02d] %s",
175 $now_playing->{artist},
176 $now_playing->{title},
177 $now_playing->{track},
178 $now_playing->{album}));
179 } else {
180 $filename =~ /^(.*)\/(.*?)$/;
181 $label->set_text("$1\n$2\n");
183 } else {
184 $label->set_text("** soepkiptngd not running!\n\n");
188 # if($playerpid) {
189 # my $p = is_paused($playerpid);
190 # if($p != $paused) {
191 # if($p) {
192 # $button_pause->set_title("Resume");
193 # } else {
194 # $button_pause->set_title("Pause");
196 # $paused = $p;
199 return 1;