
# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1999 ICEM CFD Engineering
# Author:  Wayne A. Christopher, wayne@icemcfd.com

proc jobs_view {} {
    global jobs_scroller jobs_widgets
    
    set d [form_init .jobs_list "Current processes" "" close]
    if {$d != ""} {
	set sc [form_scroller $d.fr 100 {0 0} 300]
	form_label $sc.job "Job          " {0 0}
	form_label $sc.pid "Pid          " {0 2}
	form_label $sc.time "Time (M:S)" {0 4}
	form_label $sc.size "Size (MB)" {0 6}
	form_label $sc.term "  Terminate  " {0 8}
	form_label $sc.cmd "Command                                  " {0 10}
	foreach i {0 2 4 6 8 10} { form_underline $sc.ul$i "1 $i" }
	foreach i {1 3 5 7 9} { form_colwidth $sc $i 4 }
	set jobs_scroller $sc
	form_finish bot
    }
    jobs_view_update
}

proc jobs_view_update {} {
    global jobs_list jobs_widgets jobs_scroller
    
    if {![winfo exists .jobs_list] || ![winfo ismapped .jobs_list]} return
    
    foreach p [array names jobs_list] {
	set stat [jobs_status $p]
	if {$stat == ""} {
	    set time($p) XXX
	    set size($p) XXX
	} else {
	    set size($p) [lindex $stat 0]
	    set time($p) [lindex $stat 1]
	}
    }
    
    foreach sl [grid slaves $jobs_scroller] {
	set rowused([lindex [grid info $sl] 5]) 1
    }
    foreach p [lsort [array names jobs_list]] {
	if ![winfo exists $jobs_scroller.job$p] {
	    for {set i 2} {[info exists rowused($i)]} {incr i} { }
	    set rowused($i) 1
	    
	    form_label $jobs_scroller.job$p [lindex $jobs_list($p) 0] "$i 0"
	    form_label $jobs_scroller.pid$p $p "$i 2"
	    form_label $jobs_scroller.time$p $time($p) "$i 4"
	    form_label $jobs_scroller.size$p $size($p) "$i 6"
	    form_button $jobs_scroller.term$p Terminate "jobs_terminate $p" "$i 8"
	    form_entry $jobs_scroller.cmd$p "" string "$i 10"
	    $jobs_scroller.cmd$p insert end [lindex $jobs_list($p) 1]
	    $jobs_scroller.cmd$p configure -width 16 -state disabled
	    
	    foreach w {job pid time size term cmd} {
		set jobs_widgets($jobs_scroller.$w$p) 1
	    }
	} else {
	    $jobs_scroller.time$p configure -text $time($p)
	    $jobs_scroller.size$p configure -text $size($p)
	}
	if {$time($p) == "XXX"} {
	    unset jobs_list($p)
	    $jobs_scroller.time$p configure -fg red
	    $jobs_scroller.size$p configure -fg red
	}
	foreach w {job pid time size term cmd} {
	    set okay($jobs_scroller.$w$p) 1
	}
    }
    
    # Clear out all the old widgets
    foreach w [array names jobs_widgets] {
	if ![info exists okay($w)] {
	    top_destroy $w
	    unset jobs_widgets($w)
	}
    }
    
    update idle
    after 1000 jobs_view_update
}

proc jobs_add {pid name cmd} {
    global jobs_list
    if {$pid <= 0} return
    set jobs_list($pid) [list $name $cmd]
    catch {jobs_view_update}
}

proc jobs_remove {pid} {
    global jobs_list
    
    if {$pid <= 0} return
    catch {unset jobs_list($pid)}
    catch {jobs_view_update}
}

proc jobs_terminate {pid} {
    if {$pid <= 0} return
    if [catch {cmd_kill $pid 9} res] { mess "$res\n" red }
    mess "Process $pid killed\n" red
}

proc jobs_status {pid} {
    global is_win
    
    if {$pid <= 0} { return "" }
    
    if !$is_win {
        if [catch {exec ps -lp$pid} psout] {
            return ""
        }

        set line ""
        foreach wd [split [lindex [split $psout \n] 1] " "] {
            if [string compare $wd ""] { lappend line $wd }
        }

        if {$line == ""} {
            return ""
        }
        switch [llength $line] {
            14 {
                set tsize [lindex $line 9]
                set ttime [lindex $line 12]
            }
            13 {
                set tsize [lindex $line 9]
                set ttime [lindex $line 11]
            }
            15 {
                set tsize [lindex $line 10]
                set ttime [lindex $line 13]
            }
            default {
                mess "Can't parse output of ps: \"$line\"\n" red
            }
        }

        set psize [cmd_pagesize]
        set xxx $psize
	
        set sz [lindex [split $tsize :] 0]
        if [string match *K $sz] {
            set psize 1024
        } elseif [string match *M $sz] {
            set psize 1048576
        }
	if {[scan $sz %d sz] != 1} { return "" }
	
        set meg [format %.1f [expr {$sz * $psize / 1048576.0}]]
	
	return [list $meg $ttime]
	
    } else {
        set psout [exec tlist $pid]

        set size ""
        foreach line [split $psout \n] {
            if [regexp {^ *VirtualSize: *([0-9]*) } $line dummy s] {
                set size $s
            }
        }
        if {$size == ""} {
	    return ""
        }
        set meg [format %.1f [expr $size / 1024]]
	return [list $meg ?:?]
    }
}
