summaryrefslogtreecommitdiffstats
path: root/.i3/conky-wrapper.pl
blob: 2e69d299bba4731b1f3a5de148fe473c9f177c0b (plain)
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
#!/usr/bin/perl

use v5.14;
use strict;
use warnings;

use JSON;
use Clone 'clone';
use IO::Handle;

# Send the header so that i3bar knows we want to use JSON:
printf "%s", '{"version":1}';

# Begin the endless array.
printf "%s", '[';

# We send an empty first array of blocks to make the loop simpler:
printf "%s",'[],';

open my $conky, "-|:encoding(utf8)", "conky -c $ENV{HOME}/.i3/conkyrc-perl";
STDOUT->autoflush();

my @data;
my %in_progress;
my $json = JSON->new;
$json->ascii([1]);

my %simple_mappings = (
	"SHORTTEXT" => "shorttext",
);

my %commands = (
	battery => \&get_bat_stats,
);

LINE: while (<$conky>) {
	chomp($_);

	next if m/^\s*$/;

	if (m/^END_BLOCK$/) {
		STDOUT->printf("%s,\n", $json->encode(\@data));
		undef @data;
		next;
	}

	if (m/^COLOR: (#[a-zA-Z0-9]+)$/) {
		$in_progress{color} = $1;
		next;
	}

	for my $key (keys %simple_mappings) {
		if (m/^$key: (.*)$/) {
			$in_progress{$simple_mappings{$key}} = $1;
			next LINE;
		}
	}

	if (m/^FULLTEXT: (.*)$/) {
		my $fulltext = $1;
		$in_progress{full_text} = $fulltext;
		push @data, clone \%in_progress;
		undef %in_progress;
		next;
	}


	if (m/^COMMAND: ([^\s]*)(?: (.*)?)$/) {
		my $command = $1;
		my @args = split / /, $2;
		die "Unknown command requested: $command" unless defined $commands{$command};
		my $command_ret = $commands{$command}->(@args);
		for my $text (@$command_ret) {
			$in_progress{full_text} = $text;
			push @data, clone \%in_progress;
		}
		undef %in_progress;
		next;
	}

	printf STDERR "Got unexpected data: %s\n", $_;
}


sub get_bat_stats {
	my $bats = [@_] // ["BAT0"];

	my $ret = [];

	for my $bat (@$bats) {
		next unless -e "/sys/class/power_supply/$bat";

		open my $fh, "<", "/sys/class/power_supply/$bat/uevent";
		my %values;
		while (my $line = <$fh>) {
			my ($key, $value) = split /=/, $line;
			$key =~ s/^POWER_SUPPLY_//;
			chomp $value;
			$values{$key} = $value;
		}
		close $fh;

		my $time_remaining_hours = 0;
		$time_remaining_hours = $values{ENERGY_NOW} / $values{POWER_NOW} unless $values{POWER_NOW} == 0;

		push @$ret, sprintf("%s %s%%, %.2fW, %.2fh", $bat, $values{CAPACITY}, $values{POWER_NOW} / 1e6, $time_remaining_hours);
	}
	return $ret;
}