#!/usr/local/bin/wish8.4 -f
# -*- tcl -*-

#  Copyright (C) 1998 by Jean-Marc Zucconi (jmz@FreeBSD.ORG)
#  Everyone is granted permission to copy, modify and redistribute.
#  This notice must be preserved on all copies or derivates.

set cdplayer  "/usr/local/lib/xcd/cdplayer";
set bitmapdir "/usr/local/lib/xcd";
set size ""
set linear ""
set device ""
foreach i $argv {
    if {$i == "-small"} {set size $i}\
    elseif {$i == "-linear"} {set linear "-l"}\
    else {set device $i}
}

#set cdplayer "./cdplayer"
set player [open "|$cdplayer $linear $device" r+];

#color/geometry variables:
if {$size == "-small"} {
  set bd 2; 		#border width for buttons
} else {
  set bd 3; 		#border width for buttons
}
set bg gray;		#background color
set fg black;		#foreground color
set active firebrick;	#active color

#font for track#/time display:
if {$size == "-small"} {
  set digitfont "-Adobe-Helvetica-Bold-R-Normal--*-120-*";
  set smallfont "-Adobe-Helvetica-Bold-R-Normal--*-100-*";
} else {
  set digitfont "-Adobe-Helvetica-Medium-R-Normal--*-180-*";
}

frame .top;
frame .middle;
frame .bottom;
frame .topright
frame .right
frame .left

set btns [list play stop pause eject prev next rewind ff];   

foreach i $btns {
  if {$size == "-small"} {
    button .$i -relief groove  -bitmap @$bitmapdir/${i}_s.xbm -bg $bg -bd $bd;
  } else {
    button .$i -relief groove  -bitmap @$bitmapdir/$i.xbm -bg $bg -bd $bd;
  }
  bind .$i <1> "cmd $i 1";
}

bind .rewind  <ButtonRelease-1> "cmd rewind 0";
bind .ff      <ButtonRelease-1> "cmd ff 0";

button .off -text quit -bd $bd -pady 0 -command "cmd quit 1" -relief groove;

if {$size == "-small" } {
  button .track -text track -relief groove -font $smallfont;
} else {
  button .track -text track -relief groove;
}
bind .track <1> "cmd track 1";
bind .track <ButtonRelease-1> "cmd track 0";
bind .track <2> {cmd track "2.%X.%Y"};

if {$size == "-small" } {
  button .time -text time -relief groove -font $smallfont;
} else {
  button .time -text time -relief groove;
}
bind .time <1> "cmd time 1";
bind .time <ButtonRelease-1> "cmd time 0";

label .trackn -textvariable track -relief sunken -width 3 -font $digitfont;
label .timec -textvariable time -relief sunken -width 5 -font $digitfont;

if {$size == "-small" } {
  scale .volume -from 0 -to 100 -orient horizontal -length 150 \
      -command "cmd volume" -font $smallfont -variable volume;
} else {
  scale .volume -label volume -from 0 -to 100 -orient horizontal -length 170 \
      -command "cmd volume" -variable volume;
}

pack .track .trackn .time .timec -in .topright -side left -fill none;
pack .play .pause .stop .eject  -in .top	-side left;
pack .prev .next .rewind .ff -in .middle -side left;
pack  .off -in .bottom  -fill x;
pack .top .middle .bottom -side top -pady 0 -fill both -in .left;
pack .topright .volume -side top -in .right
pack .left .right -side left

wm resizable . 0 0

set track 00
set time 00:00

proc do_update {} {
    global player
    global fg active volume track time
    puts $player "update"
    flush $player
    gets $player str
    eval $str
}
proc cmd {a b} {
    global player
    global fg active volume track time
    after cancel do_update
    puts $player "$a $b"
    flush $player
    gets $player str
    eval $str
}

tkwait visibility .
do_update
