/var/www/www.irssi.org-old/scripts/html/apm.pl


   1 use strict;
   2 use vars qw($VERSION %IRSSI);
   3 
   4 use Irssi::TextUI;
   5 
   6 $VERSION = "0.4";
   7 %IRSSI = (
   8     authors     => "Alexander Wirt",
   9     contact     => "formorer\@formorer.de",
  10     name        => "apm",
  11     description => "Shows your battery status in your Statusbar",
  12     license     => "GNU Public License",
  13     url         => "http://www.formorer.de/code",
  14 );
  15 
  16 
  17 #
  18 #apm.pl 
  19 #	apm.pl is a small script for displaying your Battery Level in irssi.
  20 #	Just load the script and do a /statusbar window add apm
  21 #	and a small box [BAT: +/-XX%] should be displayed this is only possible 
  22 #	on Computers where /proc/apm or /proc/acpi is existing. 
  23 #	The + or - indicates if battery is charging or discharging.
  24 #
  25 #	/set power_refresh <sec>    changes the refreshing time of the display
  26 #
  27 #
  28 #	Changelog:
  29 #
  30 #	0.3 - Added support for ACPI and enhanced APM support
  31 #	0.2 - Added apm_refresh and some documentation
  32 #	0.1 - Initial Release
  33 
  34 
  35 
  36 
  37 
  38 my ($refresh, $last_refresh, $refresh_tag) = (10);
  39 
  40 my ($acpi,$apm) = 0;
  41 
  42 
  43 if (-r "/proc/acpi") { $acpi = "yes" }
  44 if (-r "/proc/apm") { $apm = "yes" }
  45 
  46 exit unless ($apm or $acpi);
  47 
  48 
  49 sub get_apm {
  50 	open(RC, "/proc/apm");
  51 		my $line = <RC>;
  52 	close RC;
  53 	my ($ver1, $ver2, $sysstatus, $acstat, $chargstat, $batstatus, $prozent, $remain) = split(/\s/,$line);
  54 
  55 	if ($acstat eq "0x01") { return "+$prozent" } else { return "-$prozent" }
  56 }
  57 
  58 sub get_acpi {
  59 	open(RC, "/proc/acpi/ac_adapter/ACAD/state");
  60 		my $line = <RC>;
  61 	close RC;
  62 	my ($text,$state) = split (/:/,$line);
  63 	$state =~ s/\s//g;
  64 
  65 	open (RC, "/proc/acpi/battery/BAT0/info");
  66 	my ($text,$capa,$ein);
  67 	while (my $line = <RC>) {
  68 		if ($line =~ /last full capacity/) {
  69 			($text, $capa,$ein) = split (/:/,$line);
  70 			$capa =~ s/\s//g;
  71 		}
  72 	}
  73 	open (RC, "/proc/acpi/battery/BAT0/state"); 
  74 	my ($text,$remain,$ein);
  75 	while (my $line = <RC>) {
  76 		if ($line =~ /remaining capacity/) {
  77 			($text, $remain,$ein) = split (/:/,$line);
  78 			$remain =~ s/\s//g;
  79 		}
  80 	}
  81 	my $pstate = $remain / $capa * 100;
  82 	$pstate = sprintf("%2i", $pstate);
  83 
  84 	if ($state eq "off-line") { $pstate = "-$pstate%"; } else { $pstate = "+$pstate%"; }
  85 	return $pstate;
  86 }
  87 
  88 
  89 sub power {
  90 	my ($item, $get_size_only) = @_;
  91 	my $pstate;
  92 	if ($apm) {
  93 		$pstate = get_apm();
  94 	} else {
  95 		$pstate = get_acpi();
  96 	}
  97 	$item->default_handler($get_size_only, undef, "BAT:$pstate", 1 );
  98 	}
  99 
 100 
 101 sub set_power {
 102 	$refresh = Irssi::settings_get_int('power_refresh');
 103 	$refresh = 1 if $refresh < 1;
 104 	return if $refresh == $last_refresh;
 105 	$last_refresh = $refresh;
 106 	Irssi::timeout_remove($refresh_tag) if $refresh_tag;
 107 	$refresh_tag = Irssi::timeout_add($refresh*1000, 'refresh_power', undef);
 108 
 109 }
 110 
 111 
 112 sub refresh_power {
 113 	Irssi::statusbar_items_redraw('power');
 114 }
 115 
 116 Irssi::statusbar_item_register('power', '{sb $0-}', 'power');
 117 Irssi::statusbars_recreate_items();
 118 
 119 Irssi::settings_add_int('misc', 'power_refresh', $refresh);
 120 set_power();
 121 Irssi::signal_add('setup changed', 'set_power');