# OpenVerse WebCam Plugin
# 
# this file initalizes the program and does any
# platform specific things/setup. It will then source 
# supporting modules.
#
# Module Name		- OpenVerse WebCam Plugin
# Current Maintainter 	- Cruise <cruise@openverse.org>
# Sourced By		- Command Line
#
# Copyright (C) 1999 David Gale <cruise@openverse.org>
# For more information visit http://OpenVerse.Org/
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
# USA.

#
# REQUIRES!!
#
# You need some sort of camera. :)
# You need some sort of command line program which can generate photos
#    from your camera. I have an Olympus D-400 Zoom and I'm using this
#    plugin under linux with the photopc software. You can download this
#    software from http://www.average.org/digicam/
# If your camera software does not support the creation of png or gif
#     images then you must have another command line program to convert
#     it. I use ImageMagick's convert program.
# Your image must be a different size than the last! This is because the
#     server will not change your avatar image unless it's a different
#     size. I accomplish this by alternting lengths on the comment which
#     I'm adding with convert.
#
# This script needs to be MODIFIED to suit your setup.
# Scroll down to where it says "MODIFY THIS SECTION" to 
# find the parts you need to change.
#

global WC


#
# MODIFY THIS SECTION
#
# The comment string is used to force the image's size
# to change each time we take a photo.
set WC(str) "c"
set WC(timer) 60
set WC(height) 120
set WC(width) 160
set WC(imagename) "$MV(images)/$MV(nick).gif"
proc WC_move_cmd {} {global WC MV;file rename -force "$MV(homedir)/plugins/WebCam/temp/$MV(nick).gif" $WC(imagename)}
proc WC_convert_cmd {} {
	global WC MV

	set options {}
	if $WC(blur) {lappend options "-blur";lappend options "$WC(blur)"}
	if $WC(charcoal) {lappend options "-charcoal";lappend options "$WC(charcoal)"}
	if $WC(edge_det) {lappend options "-edge";lappend options [expr $WC(edge_det) * 250]}
	if $WC(gamma) {lappend options "-gamma";lappend options $WC(gamma)}
	if $WC(implode) {lappend options "-implode";lappend options $WC(implode)}
	if $WC(sharpen) {lappend options "-sharpen";lappend options $WC(sharpen)}
	if $WC(solarize) {lappend options "-solarize";lappend options $WC(solarize)}
	if $WC(spread) {lappend options "-spread";lappend options $WC(spread)}
	if $WC(swirl) {lappend options "-swirl";lappend options $WC(swirl)}
	if $WC(embos) {lappend options "-emboss"}
	if $WC(flip) {lappend options "-flip"}
	if $WC(flop) {lappend options "-flop"}
	if $WC(neg) {lappend options "-negate"}

	lappend options "-comment"
	lappend options "$WC(str)"
	lappend options [file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"]
	catch {
	exec convert \
		[lindex $options 0] \
		[lindex $options 1] \
		[lindex $options 2] \
		[lindex $options 3] \
		[lindex $options 4] \
		[lindex $options 5] \
		[lindex $options 6] \
		[lindex $options 7] \
		[lindex $options 8] \
		[lindex $options 9] \
		[lindex $options 10] \
		[lindex $options 11] \
		[lindex $options 12] \
		[lindex $options 13] \
		[lindex $options 14] \
		[lindex $options 15] \
		[lindex $options 16] \
		[lindex $options 17] \
		[lindex $options 18] \
		[lindex $options 19] \
		[lindex $options 20] \
		[lindex $options 21] \
		[lindex $options 22] \
		[file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).gif"]} err
		puts $err
		catch { exec gifsicle -b [file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).gif"]} err 
		puts $err
}

# The color scheme.
set WC(colors.bg) "light blue"
set WC(colors.abg) "light blue"
set WC(colors.fg) "black"
set WC(colors.afg) "red"
#
# END MODIFY SECTION
#
set WC(blur) 0
set WC(charcoal) 0
set WC(edge_det) 0
set WC(embos) 0
set WC(flip) 0
set WC(flop) 0
set WC(gamma) 0
set WC(implode) 0
set WC(neg) 0
set WC(sharpen) 0
set WC(solarize) 0
set WC(spread) 0
set WC(swirl) 0
set WC(setup) 1
set WC(state) 0
set WC(camera) 0
set WC(homedir) "$MV(homedir)/plugins/WebCam"
if [file exists "$WC(homedir)/Camera.cfg"] {
	set infile [open "$WC(homedir)/Camera.cfg" "r"]
	gets $infile WC(camera_type)
	close $infile
} else {
	set WC(camera_type) 1
}
#------------------------------
# Make our presence known!
#------------------------------
image create photo WC1 -file "$MV(homedir)/plugins/WebCam/WebCam_on.gif"
image create photo WC0 -file "$MV(homedir)/plugins/WebCam/WebCam_off.gif"
button $MV(plugin_frame).webcam -image WC0 -command {ToggleCam} \
	-bg $WC(colors.bg) -fg $WC(colors.fg) \
	-activebackground $WC(colors.abg) \
	-activeforeground $WC(colors.afg)

bind $MV(plugin_frame).webcam <Button-2> CameraSetup
bind $MV(plugin_frame).webcam <Button-3> CameraSetup
pack $MV(plugin_frame).webcam -side left
proc ToggleCam {} {
	global WC MV
	if $WC(state) {
		uplevel #0 set WC(state) 0
		$MV(plugin_frame).webcam configure -image WC0
	} else {
		$MV(plugin_frame).webcam configure -image WC1
		uplevel #0 set WC(state) 1
		if !$WC(camera) {TakePhoto 1}
	}
}
proc TakePhoto {schedule} {
	global WC MV
	if {!$WC(state) && $schedule} {
		set WC(camera) 0
		return
	}
	if $schedule {set WC(camera) 1}
	catch {WC_capture_cmd} err
	puts $err
	catch {WC_convert_cmd} err
	catch {WC_move_cmd} err
	puts $err
	append WC(str) "c"
	if {[string length $WC(str)] > 5} {set WC(str) "c"}
	ChangeAvatar [file tail $WC(imagename)]
	if [winfo exists .webCam] {
		catch {image create photo WebCam -file $WC(imagename)}
	}
	if $schedule {after [expr $WC(timer) * 1000] "TakePhoto 1"}
}

proc CameraSetup {} {
	global WC MV

	if [winfo exists .webCam] {destroy .webCam}
	toplevel .webCam
	canvas .webCam.img -height 240 -width 320

	frame .webCam.top -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	frame .webCam.top.tt -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	frame .webCam.opta -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	frame .webCam.optb -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	frame .webCam.optc -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	frame .webCam.optd -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	frame .webCam.optd.o -borderwidth 2 -bg $WC(colors.bg)

	frame .webCam.tiptop -relief sunken -borderwidth 2 \
		-bg $WC(colors.bg)

	button .webCam.top.tt.take -text "Take Photo" \
		-command "TakePhoto 0" \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg)

	menubutton .webCam.top.tt.type -text "Camera Type" \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) -relief raised \
		-borderwidth 2 -menu .webCam.top.tt.type.m
	menu .webCam.top.tt.type.m


	frame .webCam.top.quit 
	
	button .webCam.top.quit.help -text " Help " \
		-command "source \"$MV(homedir)/plugins/WebCam/WebCam.ohlp\"" \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg)
	
	button .webCam.top.quit.quit -text " Close " \
		-command "destroy .webCam" \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg)

	scale .webCam.top.scale -orient horizontal -from 10 -to 300 -label \
		"Delay" -relief raised -borderwidth 2 -variable WC(timer) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.tiptop.scalel -orient horizontal -from 0 -to 240 -label \
		"blur" -relief raised -borderwidth 2 -variable WC(blur) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.tiptop.scaler -orient horizontal -from 0 -to 100 -label \
		"Charcoal" -relief raised -borderwidth 2 -variable WC(charcoal) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.tiptop.scalec -orient horizontal -from 0 -to 100 -label \
		"Edges" -relief raised -borderwidth 2 -variable WC(edge_det) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optb.gamma -orient horizontal -from 0 -to 10 -label \
		"Gamma" -relief raised -borderwidth 2 -variable WC(gamma) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optb.implode -orient horizontal -from 0 -to 100 -label \
		"Implode" -relief raised -borderwidth 2 -variable WC(implode) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optb.sharp -orient horizontal -from 0 -to 100 -label \
		"sharpen" -relief raised -borderwidth 2 -variable WC(sharpen) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optc.solar -orient horizontal -from 0 -to 100 -label \
		"Solarize" -relief raised -borderwidth 2 -variable WC(solarize) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optc.spread -orient horizontal -from 0 -to 100 -label \
		"Spread" -relief raised -borderwidth 2 -variable WC(spread) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optc.swirl -orient horizontal -from 0 -to 500 -label \
		"Swirl" -relief raised -borderwidth 2 -variable WC(swirl) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	checkbutton .webCam.opta.embos -text "Emboss" -variable WC(embos) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) -relief raised

	checkbutton .webCam.opta.flip -text "Flip" -variable WC(flip) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) -relief raised

	checkbutton .webCam.opta.flop -text "Flop" -variable WC(flop) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) -relief raised

	checkbutton .webCam.opta.neg -text "Negate" -variable WC(neg) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) -relief raised



	catch {image create photo WebCam -file $WC(imagename)}

	pack .webCam.tiptop -fill both -expand y
	pack .webCam.optb -fill both -expand y
	pack .webCam.optc -fill both -expand y
	pack .webCam.opta -fill both -expand y
	pack .webCam.optd -fill both -expand y
	pack .webCam.optd.o -fill both -expand y
	pack .webCam.top -fill both -expand y
	pack .webCam.top.tt -side left -fill both -expand y
	pack .webCam.img -fill both -expand y
	pack .webCam.tiptop.scalel .webCam.tiptop.scaler \
		.webCam.tiptop.scalec \
		-fill both -expand y -side left

	pack .webCam.optb.gamma .webCam.optb.implode .webCam.optb.sharp \
		-side left -fill both -expand y

	pack .webCam.optc.solar .webCam.optc.spread .webCam.optc.swirl \
		-side left -fill both -expand y

	pack .webCam.opta.embos .webCam.opta.flip .webCam.opta.flop \
		.webCam.opta.neg \
		-side left -fill both -expand y

	pack .webCam.top.tt.take -side top -fill both -expand y
	pack .webCam.top.tt.type -side bottom -fill both -expand y

	pack .webCam.top.quit .webCam.top.scale -side left \
 		-fill both -expand y
	pack .webCam.top.quit.quit -fill both -expand y
	pack .webCam.top.quit.help -fill both -expand y
	catch {.webCam.img create image 160 120 -image WebCam}
	wm resizable .webCam 0 0

	.webCam.top.tt.type.m add radiobutton -label "Olympus Cameras (photopc)" \
		-background $WC(colors.bg) -foreground $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) \
		-command UpdateType \
		-variable WC(camera_type) -value 0

	.webCam.top.tt.type.m add radiobutton -label "Screen Grab (import)" \
		-background $WC(colors.bg) -foreground $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) \
		-command UpdateType \
		-variable WC(camera_type) -value 1

	.webCam.top.tt.type.m add radiobutton -label "Color QuickCam (cqcam)" \
		-background $WC(colors.bg) -foreground $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) \
		-command UpdateType \
		-variable WC(camera_type) -value 2

	.webCam.top.tt.type.m add radiobutton -label "BTTV capture card 16:9 (bttvgrab)" \
		-background $WC(colors.bg) -foreground $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) \
		-command UpdateType \
		-variable WC(camera_type) -value 3

	.webCam.top.tt.type.m add radiobutton -label "BTTV capture card 4:3 (bttvgrab)" \
		-background $WC(colors.bg) -foreground $WC(colors.fg) \
		-activebackground $WC(colors.abg) \
		-activeforeground $WC(colors.afg) \
		-command UpdateType \
		-variable WC(camera_type) -value 4

	UpdateType
}

proc UpdateType {} {
	global WC MV
	
	switch $WC(camera_type) {
		0 {
			#
			# The Olympus camera software
			# by....
			#
			# Eugene  G.  Crosser <crosser@average.org>
			# http://www.average.org/digicam/
			#
			proc WC_capture_cmd {} {
				global WC MV
				catch {exec photopc preview [file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"]} err
				puts $err
			}
			WC_GENERIC_options
		}
		1 {
			#
			# Screen shot using the import program from
			# the ImageMagick package.
			#
			proc WC_capture_cmd {} {
				global WC MV
				catch {exec import -geometry 160x120 -window root -delay 0 [file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"] } err
			}
			WC_GENERIC_options
		}
		2 {
			#
			# Color Quick Cam
			# 
			#
			proc WC_capture_cmd {} {
				global WC MV
				catch {exec cqcam -b $WC(camera.cqcam.brightness) -B $WC(camera.cqcam.blacklvl) -c $WC(camera.cqcam.contrast) -H $WC(camera.cqcam.hue) -S $WC(camera.cqcam.saturation) -w $WC(camera.cqcam.whitelvl) -s 2 > "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"} err
			}
			WC_CQCAM_Options
		}
		3 {
			#
			# BTTV capture cards. 16:9
			#
			proc WC_capture_cmd {} {
				global WC MV
				catch {exec bttvgrab -w 240 -W 135 -o jpg -p quality=100 -G /dev/video -l 1 -N NTSC -Q -d d -S 1 -f [file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"] } err 
			}
			WC_GENERIC_options
		}
		4 {
			#
			# BTTV capture cards. 4:3
			#
			proc WC_capture_cmd {} {
				global WC MV
				catch {exec bttvgrab -w 180  -o jpg -p quality=100 -G /dev/video -l 1 -N NTSC -Q -d d -S 1 -f [file nativename "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"] } err 
			}
			WC_GENERIC_options
		}
		5 {
			#
			# Generic Camera.
			#
			proc WC_capture_cmd {} {
				global WC MV
				set i "$MV(homedir)/plugins/WebCam/temp/$MV(nick).jpg"
				set outfile [open "$MV(homedir)/plugins/WebCam/generic.bat" w]
				puts $outfile "[subst $WC(camera_cmd)]"
				close $outfile
				catch {exec $MV(homedir)/plugins/WebCam/generic.bat}
			}
			WC_SHELL_Options
		}
	}
	set outfile [open "$WC(homedir)/Camera.cfg" "w"]
	puts $outfile "$WC(camera_type)"
	close $outfile
}

proc WC_GENERIC_options {} {
	global WC

	if !$WC(setup) {
		destroy .webCam.optd.o
		frame .webCam.optd.o -borderwidth 2 -bg $WC(colors.bg)
		pack .webCam.optd.o -fill both -expand y
	}	
}

proc WC_CQCAM_Options {} {
	global WC


	#QuickCam Defaults.
	set WC(camera.cqcam.brightness) 135
	set WC(camera.cqcam.contrast) 200
	set WC(camera.cqcam.blacklvl) 200
	set WC(camera.cqcam.whitelvl) 200
	set WC(camera.cqcam.saturation) 120
	set WC(camera.cqcam.hue) 100
	
	if !$WC(setup) {
		destroy .webCam.optd.o
		frame .webCam.optd.o -borderwidth 2 -bg $WC(colors.bg)
	}
	frame .webCam.optd.o.a -relief sunken -borderwidth 2 -bg $WC(colors.bg)
	frame .webCam.optd.o.b -relief sunken -borderwidth 2 -bg $WC(colors.bg)

	scale .webCam.optd.o.a.bright -orient horizontal -from 0 -to 255 -label \
		"Brightness" -relief raised -borderwidth 2 -variable WC(camera.cqcam.brightness) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optd.o.a.white -orient horizontal -from 0 -to 255 -label \
		"White Level" -relief raised -borderwidth 2 -variable WC(camera.cqcam.whitelvl) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optd.o.a.contr -orient horizontal -from 0 -to 255 -label \
		"Contrast" -relief raised -borderwidth 2 -variable WC(camera.cqcam.contrast) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optd.o.b.saturation -orient horizontal -from 0 -to 255 -label \
		"Saturation" -relief raised -borderwidth 2 -variable WC(camera.cqcam.saturation) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optd.o.b.black -orient horizontal -from 0 -to 255 -label \
		"Black Level" -relief raised -borderwidth 2 -variable WC(camera.cqcam.blacklvl) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 

	scale .webCam.optd.o.b.hue -orient horizontal -from 0 -to 255 -label \
		"Hue" -relief raised -borderwidth 2 -variable WC(camera.cqcam.hue) \
		-bg $WC(colors.bg) -fg $WC(colors.fg) \
		-activebackground $WC(colors.abg) 
	
	if !$WC(setup) {
		pack .webCam.optd.o -fill both -expand y
	}
	pack .webCam.optd.o.a -fill both -expand y
	pack .webCam.optd.o.b -fill both -expand y
	pack .webCam.optd.o.a.bright .webCam.optd.o.a.white \
		.webCam.optd.o.a.contr -side left -fill both -expand y
	pack .webCam.optd.o.b.saturation .webCam.optd.o.b.black \
		.webCam.optd.o.b.hue -side left -fill both -expand y
}

#
# Tool Tips!
#
if $MV(tooltips) {
	Bubble::initBubble $MV(plugin_frame).webcam "Right Click this \
		button to load the WebCam Setup. Left Click this button \
		to start automated photo taking."
}

set WC(setup) 0
if $MV(debug) {puts "(WebCam) - Initialization complete!"}
#-------------------------------
# And ttttthats all folks!
#-------------------------------
