File: apm.pl

package info (click to toggle)
irssi-scripts 20201016
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,068 kB
  • sloc: perl: 71,842; sh: 193; makefile: 6
file content (122 lines) | stat: -rw-r--r-- 2,953 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use strict;
use vars qw($VERSION %IRSSI);

use Irssi::TextUI;

$VERSION = "0.4";
%IRSSI = (
    authors     => "Alexander Wirt",
    contact     => "formorer\@formorer.de",
    name        => "apm",
    description => "Shows your battery status in your Statusbar",
    sbitems     => "power",
    license     => "GNU Public License",
    url         => "http://www.formorer.de/code",
);


#
#apm.pl 
#	apm.pl is a small script for displaying your Battery Level in irssi.
#	Just load the script and do a /statusbar window add apm
#	and a small box [BAT: +/-XX%] should be displayed this is only possible 
#	on Computers where /proc/apm or /proc/acpi is existing. 
#	The + or - indicates if battery is charging or discharging.
#
#	/set power_refresh <sec>    changes the refreshing time of the display
#
#
#	Changelog:
#
#	0.3 - Added support for ACPI and enhanced APM support
#	0.2 - Added apm_refresh and some documentation
#	0.1 - Initial Release





my ($refresh, $last_refresh, $refresh_tag) = (10);

my ($acpi,$apm) = 0;


if (-r "/proc/acpi") { $acpi = "yes" }
if (-r "/proc/apm") { $apm = "yes" }

exit unless ($apm or $acpi);


sub get_apm {
	open(RC, q{<}, "/proc/apm");
		my $line = <RC>;
	close RC;
	my ($ver1, $ver2, $sysstatus, $acstat, $chargstat, $batstatus, $prozent, $remain) = split(/\s/,$line);

	if ($acstat eq "0x01") { return "+$prozent" } else { return "-$prozent" }
}

sub get_acpi {
	open(RC, q{<}, "/proc/acpi/ac_adapter/ACAD/state");
		my $line = <RC>;
	close RC;
	my ($text,$state) = split (/:/,$line);
	$state =~ s/\s//g;

	open (RC, q{<}, "/proc/acpi/battery/BAT0/info");
	my ($text,$capa,$ein);
	while (my $line = <RC>) {
		if ($line =~ /last full capacity/) {
			($text, $capa,$ein) = split (/:/,$line);
			$capa =~ s/\s//g;
		}
	}
	open (RC, q{<}, "/proc/acpi/battery/BAT0/state"); 
	my ($text,$remain,$ein);
	while (my $line = <RC>) {
		if ($line =~ /remaining capacity/) {
			($text, $remain,$ein) = split (/:/,$line);
			$remain =~ s/\s//g;
		}
	}
	my $pstate = $remain / $capa * 100;
	$pstate = sprintf("%2i", $pstate);

	if ($state eq "off-line") { $pstate = "-$pstate%"; } else { $pstate = "+$pstate%"; }
	return $pstate;
}


sub power {
	my ($item, $get_size_only) = @_;
	my $pstate;
	if ($apm) {
		$pstate = get_apm();
	} else {
		$pstate = get_acpi();
	}
	$item->default_handler($get_size_only, undef, "BAT:$pstate", 1 );
}


sub set_power {
	$refresh = Irssi::settings_get_int('power_refresh');
	$refresh = 1 if $refresh < 1;
	return if $refresh == $last_refresh;
	$last_refresh = $refresh;
	Irssi::timeout_remove($refresh_tag) if $refresh_tag;
	$refresh_tag = Irssi::timeout_add($refresh*1000, 'refresh_power', undef);

}


sub refresh_power {
	Irssi::statusbar_items_redraw('power');
}

Irssi::statusbar_item_register('power', '{sb $0-}', 'power');
Irssi::statusbars_recreate_items();

Irssi::settings_add_int('misc', 'power_refresh', $refresh);
set_power();
Irssi::signal_add('setup changed', 'set_power');