#!/usr/bin/wish -f
#
# tkbattery V1.0
# tkbattery V1.0 by fastjack@fastjack.turf.org
# Last Modified 4-7-97 (International Date Format)
#
# xbattery was originally written by Roland Barmettler, tkbattery 1.0H is
# his original code with lots of revision, more features, and some bug fixes
#
# Enhancements include:
#	non-blocking program (interface will always update)
#	beeps below 10% (turn off by clicking on it once, or cmdline)
#	added overrideredirect (frameless window)
# 	smaller interface (removed bitmaps and AC power indicator)
#	bug fix. properly displays below 10% (that might have been a feature?)
#	Command line arguments for user control
#	Battery percentage or minutes display
#	2% warning dialog (with cmdline turnoff)
#	Mouseover info popup
#	Update set to 1/min
#	
# Battery and AC power status for X using the kernel's Advanced Power
# Management (APM) BIOS interface.
#
# Author		 : Roland Barmettler <roli@freestone.ch>
# Last modified	: 15.02.97
#------------------------------------------------------------------------------
#	 
#	 Copyright (C) 1997 by Chris Allen <fastjack@fastjack.turf.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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#	 
#	 Copyright (C) 1997 by Roland Barmettler <roli@freestone.ch>
#
#	 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#------------------------------------------------------------------------------
proc CenterGeo {win} {
	set sx [winfo screenwidth .]
	set sy [winfo screenheight .]
	set rx [winfo reqwidth $win]
	set ry [winfo reqheight $win]
	set sx [expr $sx / 2]
	set sy [expr $sy / 2]
	set sx [expr $sx - [expr $rx / 2]]
	set sy [expr $sy - [expr $ry / 2]]
	return "+$sx+$sy"
}
proc APMi {} {
	global gentle
	global capacity
	global ac
	global mins
	bind . <Enter> {}
	toplevel .info
	wm overrideredirect .info 1
	if {$gentle != 1} {wm geometry .info +4000+4000}
	if {$ac == 0} {set AC False} {set AC True}
	label .info.l -text "Capacity: \nMinutes:\nAC:" -anchor w -justify left
	pack .info.l -side left
	label .info.li -text "$capacity\n$mins\n$AC" -anchor w -justify left
	pack .info.li -side left
	update
	wm geometry .info [CenterGeo .info]
	bind . <Leave> {bind . <Leave> {}; destroy .info; bind . <Enter> {APMi}}
}
proc Update {} {
	global oldcap
	global sleep
	global backg
	global cap
	global capacity
	global backg_low
	global ding
	global mins
	global noisy
	global ac
	set apm_file [open "/proc/apm" r ]
	gets $apm_file apm_info
	regexp "(\[^ \]*\%)" $apm_info capacity
	set cap [string trim $capacity "%"]
	set ac [string index "$apm_info" 16]
	regexp "(\[^ \]*) min" $apm_info mins
	regsub " min" $mins {} mins
	if {$cap > 10} {.capacity config -background $backg}
	if {$cap < 10} {
		.capacity config -background $backg_low
		if {($ding == 1) && ($ac != 1) && ($oldcap > $cap)} {bell}
	}
	if {($cap <= 2) && ($noisy == 1) && ($ac!= 1) && ($oldcap > $cap)} {
		tk_dialog .error "Battery Low" "Capacity: $capacity\nMinutes: $mins" {} 0 Ok
	}
	close $apm_file
	set oldcap $cap
	after $sleep Update
}
if { [file exists "/proc/apm"] } {
	set sleep 60000
	set mins 128
	set ding 1
	set capacity "999%"
	set cap 999
	set ac 0
	set backg #4b7b82
	set backg_low #FF0000
	set noisy 1
	set ding 1
	set smins 0
	set usen 0
	set fg #dddddd
	set noinfo 0
	set oldcap 100
	set gentle 0
	foreach of "$argv" {
		if {$usen != 0} {
			set $usen $of
			set usen 0
			continue
		}
		if {[string compare $of -quiet] == 0} {set noisy 0}
		if {[string compare $of -nobeep] == 0} {set ding 0}
		if {[string compare $of -bg] == 0} {set usen backg}
		if {[string compare $of -lbg] == 0} {set usen backg_low}
		if {[string compare $of -min] == 0} {set smins 1}
		if {[string compare $of -fg] == 0} {set usen fg}
		if {[string compare $of -sleep] == 0} {set usen sleep} 
		if {[string compare $of -noinfo] == 0} {set noinfo 1}
		if {[string compare $of -gentle] == 0} {set gentle 1}
	}
	wm overrideredirect . 1
	if {$smins == 0} {set svar capacity} {set svar mins}
	label .capacity -textvariable $svar -bg $backg -relief flat -fg $fg
	pack .capacity -anchor s -side bottom -fill both -expand 1
	bind . <Button> "set ding 0"
	bind . <Button-3> {exit}
	if {$noinfo == 0} {bind . <Enter> {catch {APMi}}}
	unset usen
	unset smins
	unset fg
	unset svar
	unset argv
	unset noinfo
	Update
} {
	puts "You do not appear to have APM compiled into the kernel.\nAPM is a non-modular kernel feature."
	exit
}
