#!/usr/bin/wish

# Detect Nokia N800 by CPU
if {[string match "arm*" $tcl_platform(machine)]} {
    . configure -background white
    option add *background white
    option add *highlightBackground white
    option add *activeBackground #c0ffff
    option add *cursor right_ptr
    option add *font {{Sans Serif} -15 normal}
    option add *Scale.font {{Sans Serif} -11 normal}
    option add *Checkbutton.font {{Sans Serif} -11 normal}
}

catch {package require v4lng}
if {[info command v4lng] eq ""} {
    load ./tkv4lng.so V4lng
}

proc toggle_cap {} {
    set cap [.v4l capture]
    set cap [expr !$cap]
    .v4l capture $cap
}

proc mkphoto {} {
    global PHOTO
    if {[info exists PHOTO]} {
	incr PHOTO
    } else {
	set PHOTO 1
    }
    set p photo_$PHOTO
    image create photo $p
    set w .$p
    toplevel $w
    wm title $w $p 
    label $w.p -image $p
    pack $w.p -side top -fill both -expand 1
    .v4l photo $p
}

frame .buttons
grid .buttons -row 0 -column 0 -columnspan 2
button .buttons.cap -text Capture -command toggle_cap
button .buttons.photo -text Photo -command mkphoto
pack .buttons.cap .buttons.photo -side left -expand 1
scrollbar .y -command {.v4l yview} -orient vertical -takefocus 0
grid .y -row 1 -column 1 -sticky ns
scrollbar .x -command {.v4l xview} -orient horizontal -takefocus 0
grid .x -row 2 -column 0 -sticky ew
v4lng .v4l -width 100 -height 100 -yscrollcommand {.y set} \
    -xscrollcommand {.x set}
grid .v4l -row 1 -column 0 -sticky news
grid rowconfigure . 1 -weight 1
grid columnconfigure . 0 -weight 1

if {[string match "arm*" $tcl_platform(machine)]} {
    .v4l geometry 400 300
}

# puts stderr [.v4l devices]

.v4l open [lindex [lindex [.v4l devices] 0] 0]

frame .attr
grid .attr -row 0 -column 2 -rowspan 2

proc attr_chg {w attr args} {
    global ATTR
    set nv [.v4l setattr $attr $ATTR($w)]
}

proc setfps val {
    global ATTR
    .v4l fps $val
}

set ATTR(reqfps) [lindex [.v4l fps] 0]

set col 0
set row 0
scale .attr.reqfps -from 1 -to 50 -variable ATTR(reqfps) -command setfps \
    -label FPS -orient horizontal
grid .attr.reqfps -row $row -column $col
incr row
label .attr.fps -textvariable ATTR(fps)
grid .attr.fps -row $row -column $col
incr row

foreach attr [.v4l getattr] {
    set aname [string tolower $attr]
    regsub -all -- { } $aname {_} aname
    set w .attr.$aname
    set info [.v4l getattr $attr]
    set ATTR($w) [lindex $info 0]
    switch -glob -- [lindex $info 1] {
	int* {
	    scale $w -from [lindex $info 2] -to [lindex $info 3] \
		-variable ATTR($w) -command [list attr_chg $w $attr] \
		-label $attr -orient horizontal
	}
	bool* {
	    checkbutton $w -command [list attr_chg $w $attr] \
		-text $attr -onvalue 1 -offvalue 0 \
		-variable ATTR($w)
	}
	choice* {
	    set opts [lrange $info 2 end]
	    if {[llength $opts] > 1} {
		eval tk_optionMenu $w ATTR($w) $opts
		trace add variable ATTR($w) write [list attr_chg $w $attr]
	    }
	}
    }
    if {[winfo exists $w]} {
	grid $w -row $row -column $col
	incr row
	if {$row > 6} {
	    set row 0
	    incr col
	}
    }
}

# parray ATTR

bind all <q> exit
bind all <Escape> exit
bind all <p> mkphoto

proc pfps {} {
    global ATTR
    after 1000 pfps
    set ATTR(fps) [format "FPS=%5.2f" [lindex [.v4l fps] 1]]
}

after 1000 pfps

.v4l fps 2
