#!/usr/bin/perl
use strict;
use warnings;
use Gtk2::TrayIcon;

sub read_file {
    my ($filename) = @_;
    die "read_file: can not open $filename: $!\n"
	unless open(FD, $filename);
    my $ret = <FD>;
    chomp($ret);
    close(FD);
    return $ret;
}

Gtk2->init();
my $icon= Gtk2::TrayIcon->new("");
my $label = Gtk2::Label->new("???-????");
$icon->add($label);
$icon->show_all();

Glib::Timeout->add(30*1000, sub {
    my $capacity = read_file("/sys/devices/platform/bq27000-battery.0/power_supply/bat/capacity");
    my $current = read_file("/sys/devices/platform/bq27000-battery.0/power_supply/bat/current_now");
    my $current2 = int(int($current) / 1000.0);
    my $str = "$capacity";
    if ($current > 0) {
	$str .= "+";
    }
    $str .= $current2;
    print($str . "\n");
    $label->set_text($str);
    return 1;
		   });

Gtk2->main();

