diff options
author | Adrian C. (anrxc) <anrxc@sysphere.org> | 2009-11-11 03:50:25 +0100 |
---|---|---|
committer | Adrian C. (anrxc) <anrxc@sysphere.org> | 2009-11-11 03:50:25 +0100 |
commit | 7be560b70c17380245646a20a3bc7e96ce668b3a (patch) | |
tree | c9c52a4d30601689ff8dcb0ea671ae95059fae9b /bat.lua | |
parent | a99c1cf118b4423925473e2f29b75fdbbc2dce87 (diff) | |
download | vicious-legacy-7be560b70c17380245646a20a3bc7e96ce668b3a.tar.xz |
bat: widget rewritten for sysfs
This also means that it replaces batsys, and we are left with only
one, universal, battery widget.
Diffstat (limited to 'bat.lua')
-rw-r--r-- | bat.lua | 67 |
1 files changed, 32 insertions, 35 deletions
@@ -5,67 +5,64 @@ -- {{{ Grab environment local tonumber = tonumber -local io = { open = io.open } local setmetatable = setmetatable +local string = { format = string.format } +local helpers = require("vicious.helpers") local math = { min = math.min, 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 using procfs +-- Batsys: provides state, charge, and remaining time for a requested battery module("vicious.bat") -- {{{ Battery widget type local function worker(format, batid) + local battery = setmetatable( + { _path = "/sys/class/power_supply/" .. batid }, + helpers.pathtotable + ) + local battery_state = { - ["full"] = "↯", - ["unknown"] = "⌁", - ["charged"] = "↯", - ["charging"] = "+", - ["discharging"] = "-" + ["Full\n"] = "↯", + ["Unknown\n"] = "⌁", + ["Charged\n"] = "↯", + ["Charging\n"] = "+", + ["Discharging\n"] = "-" } - -- Get /proc/acpi/battery info - local f = io.open("/proc/acpi/battery/"..batid.."/info") - -- Handler for incompetent users - if not f then return {battery_state["unknown"], 0, "N/A"} end - local infofile = f:read("*all") - f:close() - -- Check if the battery is present - if infofile == nil or string.find(infofile, "present:[%s]+no") then - return {battery_state["unknown"], 0, "N/A"} + if not battery.present == "1\n" then + return {battery_state["Unknown\n"], 0, "N/A"} end - -- Get capacity information - local capacity = string.match(infofile, "last full capacity:[%s]+([%d]+).*") - - - -- Get /proc/acpi/battery state - local f = io.open("/proc/acpi/battery/"..batid.."/state") - local statefile = f:read("*all") - f: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]+).*") + local state = battery_state[battery.status] or battery_state["Unknown\n"] + -- 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 -- Calculate percentage (but work around broken BAT/ACPI implementations) local percent = math.min(math.floor(remaining / capacity * 100), 100) + + -- Get charge information + if battery.current_now then + rate = battery.current_now + else -- Todo: other rate sources, as with capacity? + return {state, percent, "N/A"} + end + -- Calculate remaining (charging or discharging) time if state == "+" then timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate) |