#!/usr/local/bin/tclgus -f
# -*-Tcl-*- (for Emacs)
#
# This is the Tcl script for the xgusmix program
# shell written for the Linux UltraSound project
#
# Copyright (C) 1996 Andy Lo A Foe
# Released under the GNU General Public License
# See the COPYING file for more info.
#

set w .gusmix
set bf {-adobe-helvetica-medium-r-*-*-14-*-*-*-*-*-*-*}
set tf {-adobe-helvetica-medium-r-*-*-10-*-*-*-*-*-*-*}
set mf {-adobe-helvetica-medium-r-*-*-12-*-*-*-*-*-*-*}
set nmf {-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*}
set bg {#d2d2d2}
set sl {160}
set sw {25}
set ver {1.10b}
set card_nr {0}


wm minsize	. 452 266

frame	$w -bg $bg -relief raised -borderwidth 2 
pack	$w -expand yes -fill both

frame	$w.control -bg $bg 
button	$w.control.quit -bg $bg -font $mf -text {Exit} -command "Quit"
pack	$w.control -expand yes -side top -fill both -padx 8 -pady 8
pack	$w.control.quit -side left 



if { $argc == 0 } {
   gusmix open 1
} elseif { $argc == 1 } {
   set card_nr [lindex $argv 0]
   gusmix open $card_nr
} else {
   puts "Usage: xgusmix \[card_nr\]"
   exit
}

set mver [gusmix mixerchip]
set mdevs [gusmix mdevs]
set mdevs_mute [gusmix mdevs_mute]
set mdevs_rec [gusmix mdevs_rec]

foreach i $mdevs {
	frame $w.$i -bg $bg
	
	frame $w.$i.mute -bg $bg

	checkbutton $w.$i.mute.lmute -bg $bg -font $tf -text {mute} \
		-padx 0 -pady 0 -borderwidth 2 -indicatoron off \
		-variable lmute-$i -selectcolor $bg
	checkbutton $w.$i.mute.rmute -bg $bg -font $tf -text {mute} \
		-padx 0 -pady 0 -borderwidth 2 -indicatoron off \
		-variable rmute-$i -selectcolor $bg         

	frame $w.$i.volume -bg $bg 
	
	label $w.$i.label -bg $bg -width 6 -relief flat -borderwidth 2 \
		-text [string toupper $i] -foreground {black} -font $nmf

	scale $w.$i.volume.left -bg $bg -from 100 -to 0 -length $sl \
                -orient vertical -showvalue off -borderwidth 0 \
		-width $sw -sliderlength 20
        
	scale $w.$i.volume.right -bg $bg -from 100 -to 0 -length $sl \
                -orient vertical -showvalue off -borderwidth 0 \
		-width $sw -sliderlength 20
	
	checkbutton $w.$i.unlock -bg $bg -font $mf -text {unlock}  -padx 0 \
                 -pady 0 -borderwidth 2  -indicatoron off \
		 -variable lock-$i -selectcolor $bg

	pack $w.$i -padx 4 -pady 8 -side left -fill both
	
	pack $w.$i.label
	
	pack $w.$i.unlock -fill both

        pack $w.$i.volume -padx 0 -pady 0 -fill both 
	pack $w.$i.volume.left  -padx 0 -pady 0 -side left -fill both 
        pack $w.$i.volume.right -padx 0 -pady 0 -side right -fill both

	pack $w.$i.mute -fill both
	pack $w.$i.mute.lmute -fill both -side left -expand yes
	pack $w.$i.mute.rmute -fill both -side right -expand yes

}

foreach i $mdevs_rec {
	checkbutton $w.$i.rec -bg $bg -font $mf -pady 0 -text {rec} \
	       -borderwidth 2 -indicatoron off -variable rec-$i \
	       -selectcolor $bg
	pack $w.$i.rec -fill both
}

# Some procedures

proc Quit {} {
	global card_nr
        
	gusmix close $card_nr
	destroy .
	quit
}

proc UpdatePanel {} {
    global w
    global mdevs
    global mdevs_rec    

    foreach i $mdevs { 
	set vol [split [gusmix $i get]]
	$w.$i.volume.left set [lindex $vol 0]
	$w.$i.volume.right set [lindex $vol 1]
	set mute [split [gusmix $i mute]]
	if { [lindex $mute 0] == "on" } {
	   $w.$i.mute.lmute select
	}
	if { [lindex $mute 0] == "off" } {
	   $w.$i.mute.lmute deselect
	}
	if { [lindex $mute 1] == "on" } {
	   $w.$i.mute.rmute select
	}
	if { [lindex $mute 1] == "off" } {
	   $w.$i.mute.rmute deselect
	}
    }
    foreach i $mdevs_rec {
	set val [gusmix $i rec]
	if { $val == "on"} {
	   $w.$i.rec select
	   }
	if { $val == "off"} {
	   $w.$i.rec deselect
	   }
	}
}


proc MuteCB {source channel} {
    global w
    global lock-$source

    set val [split [gusmix $source mute]]

    set mleft [lindex $val 0]
    set mright [lindex $val 1]    
 
    if { ($channel == "left") || ([set lock-$source] == 0) } {
    if { [lindex $val 0] == "on"} {
	  set mleft {off}
       } else {
	  set mleft {on}
       }
    }
 
    if { ($channel == "right") || ([set lock-$source] == 0) } {
    if { [lindex $val 1] == "on"} {
          set mright {off}
       } else {
	  set mright {on}
       }
    }
    gusmix $source mute $mleft $mright  
    UpdatePanel
}

    
proc RecCB {source} {
    global w

    set val [gusmix $source rec]

    if { $val == "on"} {
	gusmix $source rec off
       }
    if { $val == "off" } {
	gusmix $source rec on
       }
}

proc ScaleCB {source channel dummy} {
    global w
    global lock-$source

    set val [$w.$source.volume.$channel get]
    
    set vol [gusmix $source get]

    set vleft [lindex $vol 0]
    set vright [lindex $vol 1]

    if {($channel == "left") || ([set lock-$source] == 0) } {
	   set vleft $val
	}    
    if {($channel == "right") || ([set lock-$source] == 0)} {
           set vright $val
	}
    $w.$source.volume.left set $vleft
    $w.$source.volume.right set $vright
    gusmix $source set $vleft $vright
    }

# The Update procedure updates the screen every 1/4th second
# change the 'after' line if you want more frequent updates

proc Update {} {

UpdatePanel

after 250 Update
}


# I would have liked to put the following statements in a foreach
# loop put it doesn't seem to work right, or maybe I'm doing something
# silly, if you have a fix, let me know

# volume scale callback assignment

if { [ lsearch -exact $mdevs main ] >= 0 } {
	$w.main.volume.left configure -command {ScaleCB main left}
	$w.main.volume.right configure -command {ScaleCB main right}
}
if { [ lsearch -exact $mdevs synth ] >= 0 } {
	$w.synth.volume.left configure -command {ScaleCB synth left}
	$w.synth.volume.right configure -command {ScaleCB synth right}
}
if { [ lsearch -exact $mdevs effect ] >= 0 } {
	$w.effect.volume.left configure -command {ScaleCB effect left}
	$w.effect.volume.right configure -command {ScaleCB effect right}
}
if { [ lsearch -exact $mdevs pcm ] >= 0 } {
	$w.pcm.volume.left configure -command {ScaleCB pcm left}
	$w.pcm.volume.right configure -command {ScaleCB pcm right}
}
if { [ lsearch -exact $mdevs line ] >= 0 } {
	$w.line.volume.left configure -command {ScaleCB line left}
	$w.line.volume.right configure -command {ScaleCB line  right}
}
if { [ lsearch -exact $mdevs mic ] >= 0 } {
	$w.mic.volume.left configure -command {ScaleCB mic left}
	$w.mic.volume.right configure -command {ScaleCB mic right}
}
if { [ lsearch -exact $mdevs cd ] >= 0 } {
	$w.cd.volume.left configure -command {ScaleCB cd left}
	$w.cd.volume.right configure -command {ScaleCB cd right}
}
if { [ lsearch -exact $mdevs soft ] >= 0 } {
	$w.soft.volume.left configure -command {ScaleCB soft left}
	$w.soft.volume.right configure -command {ScaleCB soft right}
}
if { [ lsearch -exact $mdevs gain ] >= 0 } {
	$w.gain.volume.left configure -command {ScaleCB gain left}
	$w.gain.volume.right configure -command {ScaleCB gain right}
}
if { [ lsearch -exact $mdevs loop ] >= 0 } {
	$w.loop.volume.left configure -command {ScaleCB loop left}
	$w.loop.volume.right configure -command {ScaleCB loop right}
}

# rec buttons callback assignments

if { [ lsearch -exact $mdevs_rec synth ] >= 0 } {
	$w.synth.rec configure -command {RecCB synth}
}
if { [ lsearch -exact $mdevs_rec line ] >= 0 } {
	$w.line.rec configure -command {RecCB line}
}
if { [ lsearch -exact $mdevs_rec mic ] >= 0 } {
	$w.mic.rec configure -command {RecCB mic}
}
if { [ lsearch -exact $mdevs_rec cd ] >= 0 } {
	$w.cd.rec configure -command {RecCB cd}
}

# mute buttons callback assignments

if { [ lsearch -exact $mdevs_mute main ] >= 0 } {
	$w.main.mute.lmute configure -command {MuteCB main left}
	$w.main.mute.rmute configure -command {MuteCB main right}
}
if { [ lsearch -exact $mdevs_mute synth ] >= 0 } {
	$w.synth.mute.lmute configure -command {MuteCB synth left}
	$w.synth.mute.rmute configure -command {MuteCB synth right}
}
if { [ lsearch -exact $mdevs_mute effect ] >= 0 } {
	$w.effect.mute.lmute configure -command {MuteCB effect left}
	$w.effect.mute.rmute configure -command {MuteCB effect right}
}
if { [ lsearch -exact $mdevs_mute pcm ] >= 0 } {
	$w.pcm.mute.lmute configure -command {MuteCB pcm left}
	$w.pcm.mute.rmute configure -command {MuteCB pcm right}
}
if { [ lsearch -exact $mdevs_mute line ] >= 0 } {
	$w.line.mute.lmute configure -command {MuteCB line left}
	$w.line.mute.rmute configure -command {MuteCB line right}
}
if { [ lsearch -exact $mdevs_mute mic ] >= 0 } {
	$w.mic.mute.lmute configure -command {MuteCB mic left}
	$w.mic.mute.rmute configure -command {MuteCB mic right}
}
if { [ lsearch -exact $mdevs_mute cd ] >= 0 } {
	$w.cd.mute.lmute configure -command {MuteCB cd left}
	$w.cd.mute.rmute configure -command {MuteCB cd right}
}
if { [ lsearch -exact $mdevs_mute soft ] >= 0 } {
	$w.soft.mute.lmute configure -command {MuteCB soft left}
	$w.soft.mute.rmute configure -command {MuteCB soft right}
}
if { [ lsearch -exact $mdevs_mute gain ] >= 0 } {
	$w.gain.mute.lmute configure -command {MuteCB gain left}
	$w.gain.mute.rmute configure -command {MuteCB gain right}
}
if { [ lsearch -exact $mdevs_mute loop ] >= 0 } {
	$w.loop.mute.lmute configure -command {MuteCB loop left}
	$w.loop.mute.rmute configure -command {MuteCB loop right}
}

# Open mixer control and do some initialization stuff
# After that, start the main loop (Update)

UpdatePanel

wm title . [concat {XGusMix: Gravis Ultrasound with } $mver { mixer chip}]
Update
