2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
2010-01-02 21:21:54 +01:00
|
|
|
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
|
2009-09-29 22:33:19 +02:00
|
|
|
---------------------------------------------------
|
2009-07-30 01:48:07 +02:00
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
2009-08-01 23:11:41 +02:00
|
|
|
local setmetatable = setmetatable
|
2009-11-11 03:50:25 +01:00
|
|
|
local string = { format = string.format }
|
|
|
|
local helpers = require("vicious.helpers")
|
2009-10-15 23:11:36 +02:00
|
|
|
local math = {
|
|
|
|
min = math.min,
|
|
|
|
floor = math.floor
|
|
|
|
}
|
2009-07-30 01:48:07 +02:00
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
2010-03-16 20:26:49 +01:00
|
|
|
-- Bat: provides state, charge, and remaining time for a requested battery
|
2010-03-14 01:55:33 +01:00
|
|
|
module("vicious.widgets.bat")
|
2009-07-30 01:48:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Battery widget type
|
2010-03-14 03:37:40 +01:00
|
|
|
local function worker(format, warg)
|
|
|
|
if not warg then return end
|
|
|
|
|
|
|
|
local battery = helpers.pathtotable("/sys/class/power_supply/"..warg)
|
2009-07-30 01:48:07 +02:00
|
|
|
local battery_state = {
|
2010-03-06 22:12:47 +01:00
|
|
|
["Full\n"] = "↯",
|
|
|
|
["Unknown\n"] = "⌁",
|
|
|
|
["Charged\n"] = "↯",
|
|
|
|
["Charging\n"] = "+",
|
2009-11-11 03:50:25 +01:00
|
|
|
["Discharging\n"] = "-"
|
2009-07-30 01:48:07 +02:00
|
|
|
}
|
|
|
|
|
2009-10-04 16:26:34 +02:00
|
|
|
-- Check if the battery is present
|
2010-03-15 02:42:36 +01:00
|
|
|
if battery.present ~= "1\n" then
|
2009-11-11 03:50:25 +01:00
|
|
|
return {battery_state["Unknown\n"], 0, "N/A"}
|
2009-10-04 00:54:27 +02:00
|
|
|
end
|
2009-07-30 01:48:07 +02:00
|
|
|
|
|
|
|
|
2009-10-04 00:54:27 +02:00
|
|
|
-- Get state information
|
2009-11-11 03:50:25 +01:00
|
|
|
local state = battery_state[battery.status] or battery_state["Unknown\n"]
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2009-11-11 03:50:25 +01:00
|
|
|
-- Get capacity information
|
|
|
|
if battery.charge_now then
|
|
|
|
remaining, capacity = battery.charge_now, battery.charge_full
|
|
|
|
elseif battery.energy_now then
|
|
|
|
remaining, capacity = battery.energy_now, battery.energy_full
|
|
|
|
else
|
|
|
|
return {battery_state["Unknown\n"], 0, "N/A"}
|
|
|
|
end
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2009-10-15 23:11:36 +02:00
|
|
|
-- Calculate percentage (but work around broken BAT/ACPI implementations)
|
|
|
|
local percent = math.min(math.floor(remaining / capacity * 100), 100)
|
2009-07-30 01:48:07 +02:00
|
|
|
|
2009-11-11 03:50:25 +01:00
|
|
|
|
|
|
|
-- Get charge information
|
|
|
|
if battery.current_now then
|
|
|
|
rate = battery.current_now
|
2011-02-15 03:49:43 +01:00
|
|
|
elseif battery.power_now then
|
|
|
|
rate = battery.power_now
|
|
|
|
else
|
2009-11-11 03:50:25 +01:00
|
|
|
return {state, percent, "N/A"}
|
|
|
|
end
|
|
|
|
|
2009-10-04 00:54:27 +02:00
|
|
|
-- Calculate remaining (charging or discharging) time
|
2011-02-21 06:46:49 +01:00
|
|
|
local time = "N/A"
|
2011-07-12 23:54:13 +02:00
|
|
|
if tonumber(rate) then
|
2011-02-21 06:46:49 +01:00
|
|
|
if state == "+" then
|
|
|
|
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
|
|
|
|
elseif state == "-" then
|
|
|
|
timeleft = tonumber(remaining) / tonumber(rate)
|
|
|
|
else
|
|
|
|
return {state, percent, time}
|
|
|
|
end
|
2011-11-19 02:20:08 +01:00
|
|
|
-- Calculate time (but work around broken BAT/ACPI implementations)
|
|
|
|
local hoursleft = math.min(math.floor(timeleft), 0)
|
|
|
|
local minutesleft = math.min(math.floor((timeleft - hoursleft) * 60 ), 0)
|
2011-02-21 06:46:49 +01:00
|
|
|
time = string.format("%02d:%02d", hoursleft, minutesleft)
|
2009-07-30 01:48:07 +02:00
|
|
|
end
|
2009-10-04 00:54:27 +02:00
|
|
|
|
|
|
|
return {state, percent, time}
|
2009-07-30 01:48:07 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
2009-08-01 23:11:41 +02:00
|
|
|
|
|
|
|
setmetatable(_M, { __call = function(_, ...) return worker(...) end })
|