#!/usr/bin/wish -f
#
# xbattery V1.0
# 
# 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 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.
#
#------------------------------------------------------------------------------

# Check for the existance of /proc/apm

if { [file exists "/proc/apm"] } \
{
  # init variables
  set capacity "999%"
  set cap 999
  set ac 0
  set batt_xbm_100 "/usr/X11/include/X11/bitmaps/battery_100.xbm"
  set batt_xbm_75 "/usr/X11/include/X11/bitmaps/battery_75.xbm"
  set batt_xbm_50 "/usr/X11/include/X11/bitmaps/battery_50.xbm"
  set batt_xbm_25 "/usr/X11/include/X11/bitmaps/battery_25.xbm"
  set batt_xbm_0 "/usr/X11/include/X11/bitmaps/battery_0.xbm"
  set ac_power_xbm "/usr/X11/include/X11/bitmaps/ac_power.xbm"
  set backg #CFCFCF
  set backg_low #FF5000

  # Init X interface
  label .bitmap -bitmap @$batt_xbm_100 -bg $backg -relief ridge
  label .capacity -textvariable capacity -bg $backg -relief ridge
  pack .bitmap -anchor n -side top -fill both -expand 1
  pack .capacity -anchor s -side bottom -fill both -expand 1

  while { 1 } \
  {
    # get info from /proc/apm
    set apm_file [open "/proc/apm" r ]
    gets $apm_file apm_info
    set capacity [string trim [string range "$apm_info" 28 31] " -"]
    set cap [string trim $capacity "%"]
    set ac [string index "$apm_info" 16]

    # are we connected to AC power ?
    if { $ac == 1 } \
    {
      # yes, we are
      destroy .bitmap
      label .bitmap -bitmap @$ac_power_xbm -bg $backg -relief ridge
      pack .bitmap -anchor n -side top -fill both -expand 1
    } else \
    {
      # no, we are running on battery power
      destroy .bitmap
      if { $cap > 75 } {
        label .bitmap -bitmap @$batt_xbm_100 -bg $backg -relief ridge
      } elseif { $cap > 50 } {
        label .bitmap -bitmap @$batt_xbm_75 -bg $backg -relief ridge
      } elseif { $cap > 25 } {
        label .bitmap -bitmap @$batt_xbm_50 -bg $backg -relief ridge
      } elseif { $cap > 6  } {
        label .bitmap -bitmap @$batt_xbm_25 -bg $backg -relief ridge
      } else {
        label .bitmap -bitmap @$batt_xbm_0 -bg $backg -relief ridge
      }
      # if the battery is really low, use red background colour
      if { $cap < 5 } {
        destroy .bitmap
        label .bitmap -bitmap @$batt_xbm_0 -bg $backg_low -relief ridge
      }
      pack .bitmap -anchor n -side top -fill both -expand 1
    }
    update idletasks
    close $apm_file

    # Sleep for 10 seconds
    after 10000
  }
} else \
{
  label .info -text "No APM BIOS support found !" -relief ridge
  button .ok -text "That's too bad" -command exit
  pack .info .ok -padx 2m -pady 2m
}
