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