#!/usr/bin/env perl # Joe Gillotti - 12/19/2011 use strict; use warnings; use Gtk2 qw(-init); sub getPercentage { my $full_path = '/sys/class/power_supply/BAT0/charge_full_design'; my $now_path = '/sys/class/power_supply/BAT0/charge_now'; my $h; open($h, "<$full_path"); chomp(my $full = <$h>); close $h; open($h, "<$now_path"); chomp(my $now = <$h>); close $h; sprintf("%.2f", $now/$full*100) } sub getState { my $h; open ($h, "); close $h; if ($online eq '1') { return 1; } return 0; } sub getPic { my $state = getState; my $pic = ''; my $perc = getPercentage; if ($perc > 95) { $pic = '100'; } elsif ($perc > 80) { $pic = '080'; } elsif ($perc > 60) { $pic = '060'; } elsif ($perc > 40) { $pic = '040'; } elsif ($perc > 20) { $pic = '020'; } elsif ($perc < 20) { $pic = '000'; } $pic .= '.png'; $pic = 'battery_'.($state ? 'charging' : 'discharging').'_'.$pic; $pic = 'bat_icons/'.$pic; $pic; } my $icon = Gtk2::StatusIcon->new_from_file(getPic()); $icon->set_tooltip((getState() ? 'Charging' : 'Discharging').' @ '.getPercentage().'%'); Glib::Timeout->add (2000, sub{ my $pic = getPic(); $icon->set_from_file($pic); $icon->set_tooltip((getState() ? 'Charging' : 'Discharging').' @ '.getPercentage().'%'); 1; }); Gtk2->main;