From 7be560b70c17380245646a20a3bc7e96ce668b3a Mon Sep 17 00:00:00 2001 From: "Adrian C. (anrxc)" Date: Wed, 11 Nov 2009 03:50:25 +0100 Subject: bat: widget rewritten for sysfs This also means that it replaces batsys, and we are left with only one, universal, battery widget. --- README | 9 ------- bat.lua | 67 +++++++++++++++++++++++------------------------ batsys.lua | 87 -------------------------------------------------------------- init.lua | 1 - 4 files changed, 32 insertions(+), 132 deletions(-) delete mode 100644 batsys.lua diff --git a/README b/README index f9febff..ec8d48f 100644 --- a/README +++ b/README @@ -165,15 +165,6 @@ vicious.widgets.uptime vicious.widgets.bat - provides state, charge, and remaining time for a requested battery - using procfs - - takes battery ID as an argument, i.e. "BAT0" - - returns 1st value as state of requested battery, 2nd as charge - level in percent and 3rd as remaining (charging or discharging) - time - -vicious.widgets.batsys - - provides state, charge, and remaining time for a requested battery - using sysfs - takes battery ID as an argument, i.e. "BAT0" - returns 1st value as state of requested battery, 2nd as charge level in percent and 3rd as remaining (charging or discharging) diff --git a/bat.lua b/bat.lua index 24936fd..6241072 100644 --- a/bat.lua +++ b/bat.lua @@ -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) diff --git a/batsys.lua b/batsys.lua deleted file mode 100644 index 95d26af..0000000 --- a/batsys.lua +++ /dev/null @@ -1,87 +0,0 @@ ---------------------------------------------------- --- Licensed under the GNU General Public License v2 --- * (c) 2009, Adrian C. --- * (c) 2009, Benedikt Sauer ---------------------------------------------------- - --- {{{ Grab environment -local tonumber = tonumber -local io = { open = io.open } -local setmetatable = setmetatable -local string = { format = string.format } -local math = { - min = math.min, - floor = math.floor -} --- }}} - - --- Batsys: provides state, charge, and remaining time for a requested battery using sysfs -module("vicious.batsys") - - --- {{{ Battery widget type -local function worker(format, batid) - local battery = setmetatable({}, {__index = function(table, name) - local f = io.open("/sys/class/power_supply/"..batid.."/"..name) - if f then - local s = f:read("*all") - f:close() - return s - end - end}) - - local battery_state = { - ["Full\n"] = "↯", - ["Unknown\n"] = "⌁", - ["Charged\n"] = "↯", - ["Charging\n"] = "+", - ["Discharging\n"] = "-" - } - - -- Check if the battery is present - if not battery.present == "1\n" then - return {battery_state["Unknown\n"], 0, "N/A"} - end - - - -- Get state information - 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) - elseif state == "-" then - timeleft = tonumber(remaining) / tonumber(rate) - else - return {state, percent, "N/A"} - end - local hoursleft = math.floor(timeleft) - local minutesleft = math.floor((timeleft - hoursleft) * 60 ) - local time = string.format("%02d:%02d", hoursleft, minutesleft) - - return {state, percent, time} -end --- }}} - -setmetatable(_M, { __call = function(_, ...) return worker(...) end }) diff --git a/init.lua b/init.lua index abf9c56..eefae1f 100644 --- a/init.lua +++ b/init.lua @@ -28,7 +28,6 @@ require("vicious.thermal") require("vicious.load") require("vicious.uptime") require("vicious.bat") -require("vicious.batsys") require("vicious.mem") require("vicious.fs") require("vicious.dio") -- cgit v1.2.3