#!avview_shell

add_parameter_pane "/Diagnostics" "Diagnostics" \
	"Version information" heading "" \
	"AVview version" ro_entry "avview_version" \
	"Tcl version"  ro_entry "tcl_patchLevel" \
	"Tk version" ro_entry "tk_patchLevel" \
	"Additional diagnostics" heading "" \
	"Show XVideo information" command {switch_to_pane "/Diagnostics/commands/xvinfo"
		Tree:open .setup.tree "/Diagnostics"
		Tree:open .setup.tree "/Diagnostics/commands"
		set xvinfo_pane [get_pane_frame "/Diagnostics/commands/xvinfo"]
		$xvinfo_pane.f.refresh invoke} \
	"Show kernel debug messages" command {switch_to_pane "/Diagnostics/commands/dmesg"
		Tree:open .setup.tree "/Diagnostics"
		Tree:open .setup.tree "/Diagnostics/commands"
		set dmesg_pane [get_pane_frame "/Diagnostics/commandsdmesg"]
		$dmesg_pane.f.refresh invoke} \
	"Show loaded kernel modules" command {switch_to_pane "/Diagnostics/proc/modules"
		Tree:open .setup.tree "/Diagnostics"
		Tree:open .setup.tree "/Diagnostics/proc"
		set dmesg_pane [get_pane_frame "/Diagnostics/proc/modules"]
		$dmesg_pane.f.refresh invoke} \
	"Show interrupt handlers" command {switch_to_pane "/Diagnostics/proc/interrupts"
		Tree:open .setup.tree "/Diagnostics"
		Tree:open .setup.tree "/Diagnostics/proc"
		set dmesg_pane [get_pane_frame "/Diagnostics/proc/interrupts"]
		$dmesg_pane.f.refresh invoke}
	

add_pane "/Diagnostics/proc" "Runtime Linux kernel data"
add_pane "/Diagnostics/commands" "Output of diagnostic commands"

add_parameter_pane "/Information" "Information" \
	"Show README file" command {switch_to_pane "/Information/README"
		Tree:open .setup.tree "/Information/README"
		set pane [get_pane_frame "/Information/README"]
		$pane.f.refresh invoke} \
	"Show INSTALL file" command {switch_to_pane "/Information/INSTALL"
		Tree:open .setup.tree "/Information/INSTALL"
		set pane [get_pane_frame "/Information/INSTALL"]
		$pane.f.refresh invoke} 

add_pane "/Information/README" "README file"	
add_pane "/Information/INSTALL" "INSTALL file"	
Tree:open .setup.tree "/Information"


proc make_text_holder { pane refresh_cmd} {
button $pane.f.refresh -text "Refresh" -command $refresh_cmd
grid $pane.f.refresh -pady 3 -padx 3
scrollbar $pane.f.vsb -command "$pane.f.text yview" -orient vert
scrollbar $pane.f.hsb -command "$pane.f.text xview" -orient horiz
text $pane.f.text -xscrollcommand "$pane.f.hsb set" -yscrollcommand "$pane.f.vsb set"
grid $pane.f.text - $pane.f.vsb -sticky news
grid $pane.f.hsb - - -sticky news
grid columnconfig $pane.f 1 -weight 1
grid rowconfig $pane.f 1 -weight 1
global empty.$pane
set empty.$pane true
#
# Delay obtaining diagnostic information until such
# time as the window becomes visible
#
# This is because some the commands (like glxinfo)
# can crash X when things go wrong (i.e. broken 3d)
#
bind $pane.f <Visibility> [subst -nocommands {
	global empty.$pane
	if { [set empty.$pane ] } {
		set empty.$pane false
		$pane.f.refresh invoke
		}
	} ]
}


foreach {command desc index} {
	xvinfo "XVideo extension information" 0.0
	glxinfo "GLx extension information" 0.0 
	dmesg  "Linux kernel debug messages" end
	xdpyinfo "X server information" 0.0 } {

	add_pane "/Diagnostics/commands/$command" $desc

	set pane [get_pane_frame "/Diagnostics/commands/$command"]

	make_text_holder $pane [subst -nocommands {
		set a "Failed to run $command"
		catch {
			set a [exec $command]
			} b
		$pane.f.text delete 0.0 end
		if { \$a != "Failed to run $command" } {
			$pane.f.text insert end \$a
			$pane.f.text insert end "\n-------------------------------------\n"
			}
		regsub {child process exited abnormally.?.?.?$} \$b "" c
		$pane.f.text insert end \$c
		$pane.f.text see $index
		} ]
	}

foreach {unit desc} {modules "Loaded modules"
	interrupts "Interrupt handlers"
	dma "DMA"
	ioports "IO ports"
	meminfo "Memory information"
	mtrr "MTRR"
	pci "PCI devices"} {

	add_pane "/Diagnostics/proc/$unit" $desc

	set pane [get_pane_frame "/Diagnostics/proc/$unit"]

	make_text_holder $pane [subst -nocommands {
		set a "Failed to read /proc/$unit"
		catch {
			set FILE [open "/proc/$unit" "r"]
			set a [read \$FILE]
			close \$FILE
			} b
		$pane.f.text delete 0.0 end
		if { \$a != "Failed to read /proc/$unit" } {
			$pane.f.text insert end \$a
			$pane.f.text insert end "\n-------------------------------------\n"
			}
		$pane.f.text insert end \$b
		$pane.f.text see 0.0
		} ]
	}

set readme_pane [get_pane_frame "/Information/README"]

make_text_holder $readme_pane {
	global avview_directory
	set a "Failed to read $avview_directory/README"
	catch {
		set FILE [open "$avview_directory/README" "r"]
		set a [read $FILE]
		close $FILE
		} b
	set readme_pane [get_pane_frame "/Information/README"]
	$readme_pane.f.text delete 0.0 end
	if { $a != "Failed to read $avview_directory/README" } {
		$readme_pane.f.text insert end $a
		$readme_pane.f.text insert end "-------------------------------------"
		}
	$readme_pane.f.text insert end $b
	$readme_pane.f.text see 0.0
	}

set install_pane [get_pane_frame "/Information/INSTALL"]

make_text_holder $install_pane {
	global avview_directory
	set a "Failed to read $avview_directory/INSTALL"
	catch {
		set FILE [open "$avview_directory/INSTALL" "r"]
		set a [read $FILE]
		close $FILE
		} b
	set install_pane [get_pane_frame "/Information/INSTALL"]
	$install_pane.f.text delete 0.0 end
	if { $a != "Failed to read $avview_directory/INSTALL" } {
		$install_pane.f.text insert end $a
		$install_pane.f.text insert end "-------------------------------------"
		}
	$install_pane.f.text insert end $b
	$install_pane.f.text see 0.0
	}

