Add support for coarse representation of battery capacity, refactor battery widget for multiple battery display

https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power
This commit is contained in:
Alice Gaudon 2021-07-18 15:34:55 +02:00
parent e36d42dfeb
commit 00533c13f6

View File

@ -1,3 +1,4 @@
local awful = require("awful")
local beautiful = require("beautiful") local beautiful = require("beautiful")
local gears = require("gears") local gears = require("gears")
local naughty = require("naughty") local naughty = require("naughty")
@ -131,20 +132,9 @@ gears.timer {
---
local battery = wibox.widget { --- Batteries
{ ---
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
local batteryText = battery:get_children()[1]
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply") local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
function getBatteries() function getBatteries()
@ -155,48 +145,75 @@ function hasBattery()
return rawBatteries:len() > 0 return rawBatteries:len() > 0
end end
function getBatteryCapacity() function getBatteryLevel(battery)
local charge = 0 local charge = 0
local capacity = 0 local capacity = 0
for b in getBatteries() do if fileExists(battery .. "/energy_now") then
if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then charge = charge + readCommand("cat " .. battery .. "/energy_now")
if fileExists("/sys/class/power_supply/" .. b .. "/energy_now") then capacity = capacity + readCommand("cat " .. battery .. "/energy_full")
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now") elseif fileExists(battery .. "/charge_now") then
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full") charge = charge + readCommand("cat " .. battery .. "/charge_now")
capacity = capacity + readCommand("cat " .. battery .. "/charge_full")
elseif fileExists(battery .. "/capacity_level") then
return readCommand("cat " .. battery .. "/capacity_level")
else else
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now") return "Unsupported"
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full")
end end
end return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
end
return charge / capacity
end end
function isCharging() function isCharging(battery)
for b in getBatteries() do return not readCommand("cat " .. battery .. "/status") == "Discharging"
if readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
return readCommand("cat /sys/class/power_supply/" .. b .. "/status") ~= "Discharging"
end
end
return nil
end end
local batteryContainer = wibox.widget {
layout = wibox.layout.flex.horizontal
}
if hasBattery() then if hasBattery() then
for battery in getBatteries() do
local path = "/sys/class/power_supply/" .. battery
if readCommand("cat " .. path .. "/type"):match("Battery") then
-- widget
local widget = wibox.widget {
{
markup = "-- %",
align = "center",
valign = "center",
widget = wibox.widget.textbox,
},
margins = beautiful.system_resources_widget_battery_margin,
widget = wibox.container.margin,
}
-- tooltip
local name = readCommand("cat " .. path .. "/model_name")
local tooltip = awful.tooltip {
text = name
}
tooltip:add_to_object(widget)
-- update battery level
local batteryText = widget:get_children()[1]
gears.timer { gears.timer {
timeout = config.battery.refresh_interval, timeout = config.battery.refresh_interval,
call_now = true, call_now = true,
autostart = true, autostart = true,
callback = function() callback = function()
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%" batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
end end
} }
batteryContainer:add(widget)
end
end
else else
battery = nil batteryContainer = nil
end end
return { return {
bars = bars, bars = bars,
battery = battery, battery = batteryContainer,
} }