2009-07-30 01:48:07 +02:00
|
|
|
----------------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License version 2
|
|
|
|
-- * Copyright (C) 2009 Adrian C. <anrxc_sysphere_org>
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
|
|
|
local io = { open = io.open }
|
2009-08-01 23:11:41 +02:00
|
|
|
local setmetatable = setmetatable
|
2009-07-30 01:48:07 +02:00
|
|
|
local math = { floor = math.floor }
|
|
|
|
local string = {
|
|
|
|
find = string.find,
|
|
|
|
match = string.match,
|
|
|
|
format = string.format
|
|
|
|
}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Bat: provides state, charge, and remaining time for a requested battery
|
|
|
|
module("vicious.bat")
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Battery widget type
|
|
|
|
function worker(format, batid)
|
|
|
|
-- Initialise tables
|
|
|
|
local battery_state = {
|
2009-07-31 20:40:36 +02:00
|
|
|
["full"] = "↯",
|
|
|
|
["unknown"] = "⌁",
|
|
|
|
["charged"] = "↯",
|
|
|
|
["charging"] = "+",
|
2009-07-30 01:48:07 +02:00
|
|
|
["discharging"] = "-"
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Get /proc/acpi/battery info
|
|
|
|
local finfo = io.open("/proc/acpi/battery/"..batid.."/info")
|
|
|
|
local infofile = finfo:read("*all")
|
|
|
|
finfo:close()
|
|
|
|
|
|
|
|
-- Check if the file wasn't found or the battery isn't present
|
|
|
|
if infofile == nil or string.find(infofile, "present:[%s]+no") then
|
2009-07-30 20:47:02 +02:00
|
|
|
return {"/", "/", "/"}
|
2009-07-30 01:48:07 +02:00
|
|
|
else
|
|
|
|
-- Get capacity information
|
|
|
|
local capacity = string.match(infofile, "last full capacity:[%s]+([%d]+).*")
|
|
|
|
|
|
|
|
|
|
|
|
-- Get /proc/acpi/battery state
|
|
|
|
local fstate = io.open("/proc/acpi/battery/"..batid.."/state")
|
|
|
|
local statefile = fstate:read("*all")
|
|
|
|
fstate:close()
|
|
|
|
|
|
|
|
-- Get state information
|
|
|
|
local state = string.match(statefile, "charging state:[%s]+([%a]+).*")
|
|
|
|
local state = battery_state[state] or battery_state["unknown"]
|
|
|
|
|
|
|
|
-- Get charge information
|
|
|
|
local rate = string.match(statefile, "present rate:[%s]+([%d]+).*")
|
|
|
|
local remaining = string.match(statefile, "remaining capacity:[%s]+([%d]+).*")
|
|
|
|
|
|
|
|
|
|
|
|
-- Calculate percentage
|
|
|
|
local percent = math.floor(remaining / capacity * 100)
|
|
|
|
local percent = string.format("%02d", percent)
|
|
|
|
|
|
|
|
-- Calculate remaining (charging or discharging) time
|
|
|
|
if state == "+" then
|
|
|
|
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
|
|
|
|
elseif state == "-" then
|
|
|
|
timeleft = tonumber(remaining) / tonumber(rate)
|
|
|
|
else
|
|
|
|
return { state, percent, "/" }
|
|
|
|
end
|
|
|
|
local hoursleft = math.floor(timeleft)
|
|
|
|
local minutesleft = math.floor((timeleft - hoursleft) * 60 )
|
|
|
|
local time = string.format("%02d:%02d", hoursleft, minutesleft)
|
|
|
|
|
2009-07-30 20:47:02 +02:00
|
|
|
return {state, percent, time}
|
2009-07-30 01:48:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
|
|
|
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
|