Juni 18, 2013

Battery Warning Notification for Awesome Window Manager

Just a quick hack for my awesome window manager configuration. There are some widget libraries for awesome that can be used, but this configuration doesn't need them. If you have awesome, then you are set. What I did was reading battery status ("Discharging" or "Charging") and also battery capacity and then put them in awesome timer, call it every 2 minutes. In each call, it will detect the status and capacity. If below 10% and Discharging, then awesome will notify user using Naughty (comes preinstalled in awesome 3.5). Here's the relevant part of the rc.lua:

-- battery warning
-- created by bpdp

local function trim(s)
  return s:find'^%s*$' and '' or s:match'^%s*(.*%S)'

local function bat_notification()
  local f_capacity = assert(io.open("/sys/class/power_supply/BAT0/capacity", "r"))
  local f_status = assert(io.open("/sys/class/power_supply/BAT0/status", "r"))

  local bat_capacity = tonumber(f_capacity:read("*all"))
  local bat_status = trim(f_status:read("*all"))

  if (bat_capacity <= 10 and bat_status == "Discharging") then
    naughty.notify({ title      = "Battery Warning"
      , text       = "Battery low! " .. bat_capacity .."%" .. " left!"
      , fg="#ff0000"
      , bg="#deb887"
      , timeout    = 15
      , position   = "bottom_left"

battimer = timer({timeout = 120})
battimer:connect_signal("timeout", bat_notification)

-- end here for battery warning

In my laptop, I put rc.lua in $XDG_CONFIG_HOME/.awesome/ (usually it will reside in $HOME/.config/awesome/rc.lua). Just put the script above at the end of rc.lua and you are all set. When awesome detects that battery capacity fall below or the same with 10% and the status is "Discharging" (means the laptop has not been charged), then it will show this notification in the bottom of the monitor, left side:

You may change some properties of naughty.notify if you want.


Posting Komentar