bat: bat widget using procfs renamed to batproc
This commit is contained in:
parent
636abecd60
commit
5ca6bf2748
|
@ -1,72 +1,73 @@
|
||||||
---------------------------------------------------
|
---------------------------------------------------
|
||||||
-- Licensed under the GNU General Public License v2
|
-- Licensed under the GNU General Public License v2
|
||||||
-- * (c) 2009, Adrian C. <anrxc@sysphere.org>
|
-- * (c) 2009, Adrian C. <anrxc@sysphere.org>
|
||||||
-- * (c) 2009, Benedikt Sauer <filmor@gmail.com>
|
|
||||||
---------------------------------------------------
|
---------------------------------------------------
|
||||||
|
|
||||||
-- {{{ Grab environment
|
-- {{{ Grab environment
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
local io = { open = io.open }
|
local io = { open = io.open }
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
local string = { format = string.format }
|
|
||||||
local math = {
|
local math = {
|
||||||
min = math.min,
|
min = math.min,
|
||||||
floor = math.floor
|
floor = math.floor
|
||||||
}
|
}
|
||||||
|
local string = {
|
||||||
|
find = string.find,
|
||||||
|
match = string.match,
|
||||||
|
format = string.format
|
||||||
|
}
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
||||||
|
|
||||||
-- Batsys: provides state, charge, and remaining time for a requested battery using sysfs
|
-- Batproc: provides state, charge, and remaining time for a requested battery using procfs
|
||||||
module("vicious.batsys")
|
module("vicious.widgets.batproc")
|
||||||
|
|
||||||
|
|
||||||
-- {{{ Battery widget type
|
-- {{{ Battery widget type
|
||||||
local function worker(format, batid)
|
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 = {
|
local battery_state = {
|
||||||
["Full\n"] = "↯",
|
["full"] = "↯",
|
||||||
["Unknown\n"] = "⌁",
|
["unknown"] = "⌁",
|
||||||
["Charged\n"] = "↯",
|
["charged"] = "↯",
|
||||||
["Charging\n"] = "+",
|
["charging"] = "+",
|
||||||
["Discharging\n"] = "-"
|
["discharging"] = "-"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-- 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
|
-- Check if the battery is present
|
||||||
if not battery.present == "1\n" then
|
if infofile == nil or string.find(infofile, "present:[%s]+no") then
|
||||||
return {battery_state["Unknown\n"], 0, "N/A"}
|
return {battery_state["unknown"], 0, "N/A"}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Get state information
|
|
||||||
local state = battery_state[battery.status] or battery_state["Unknown\n"]
|
|
||||||
|
|
||||||
-- Get capacity information
|
-- Get capacity information
|
||||||
if battery.charge_now then
|
local capacity = string.match(infofile, "last full capacity:[%s]+([%d]+).*")
|
||||||
remaining, capacity = battery.charge_now, battery.charge_full
|
|
||||||
elseif battery.energy_now then
|
|
||||||
remaining, capacity = battery.energy_now, battery.energy_full
|
-- Get /proc/acpi/battery state
|
||||||
else
|
local f = io.open("/proc/acpi/battery/"..batid.."/state")
|
||||||
return {battery_state["Unknown\n"], 0, "N/A"}
|
local statefile = f:read("*all")
|
||||||
end
|
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
|
-- Get charge information
|
||||||
if battery.current_now then rate = battery.current_now
|
local rate = string.match(statefile, "present rate:[%s]+([%d]+).*")
|
||||||
else return {battery_state["Unknown\n"], 0, "N/A"} end
|
local remaining = string.match(statefile, "remaining capacity:[%s]+([%d]+).*")
|
||||||
|
|
||||||
|
|
||||||
-- Calculate percentage (but work around broken BAT/ACPI implementations)
|
-- Calculate percentage (but work around broken BAT/ACPI implementations)
|
||||||
local percent = math.min(math.floor(remaining / capacity * 100), 100)
|
local percent = math.min(math.floor(remaining / capacity * 100), 100)
|
||||||
|
|
||||||
-- Calculate remaining (charging or discharging) time
|
-- Calculate remaining (charging or discharging) time
|
||||||
if state == "+"then
|
if state == "+" then
|
||||||
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
|
timeleft = (tonumber(capacity) - tonumber(remaining)) / tonumber(rate)
|
||||||
elseif state == "-" then
|
elseif state == "-" then
|
||||||
timeleft = tonumber(remaining) / tonumber(rate)
|
timeleft = tonumber(remaining) / tonumber(rate)
|
Loading…
Reference in New Issue