forked from ashpie/simple-awesome
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:
parent
e36d42dfeb
commit
00533c13f6
@ -1,3 +1,4 @@
|
||||
local awful = require("awful")
|
||||
local beautiful = require("beautiful")
|
||||
local gears = require("gears")
|
||||
local naughty = require("naughty")
|
||||
@ -131,20 +132,9 @@ gears.timer {
|
||||
|
||||
|
||||
|
||||
|
||||
local battery = wibox.widget {
|
||||
{
|
||||
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]
|
||||
|
||||
---
|
||||
--- Batteries
|
||||
---
|
||||
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
|
||||
|
||||
function getBatteries()
|
||||
@ -155,48 +145,75 @@ function hasBattery()
|
||||
return rawBatteries:len() > 0
|
||||
end
|
||||
|
||||
function getBatteryCapacity()
|
||||
function getBatteryLevel(battery)
|
||||
local charge = 0
|
||||
local capacity = 0
|
||||
for b in getBatteries() do
|
||||
if b:sub(1,3) ~= 'hid' and readCommand("cat /sys/class/power_supply/" .. b .. "/type"):match("Battery") then
|
||||
if fileExists("/sys/class/power_supply/" .. b .. "/energy_now") then
|
||||
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_now")
|
||||
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/energy_full")
|
||||
else
|
||||
charge = charge + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_now")
|
||||
capacity = capacity + readCommand("cat /sys/class/power_supply/" .. b .. "/charge_full")
|
||||
end
|
||||
end
|
||||
if fileExists(battery .. "/energy_now") then
|
||||
charge = charge + readCommand("cat " .. battery .. "/energy_now")
|
||||
capacity = capacity + readCommand("cat " .. battery .. "/energy_full")
|
||||
elseif fileExists(battery .. "/charge_now") then
|
||||
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
|
||||
return "Unsupported"
|
||||
end
|
||||
return charge / capacity
|
||||
return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
|
||||
end
|
||||
|
||||
function isCharging()
|
||||
for b in getBatteries() do
|
||||
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
|
||||
function isCharging(battery)
|
||||
return not readCommand("cat " .. battery .. "/status") == "Discharging"
|
||||
end
|
||||
|
||||
|
||||
local batteryContainer = wibox.widget {
|
||||
layout = wibox.layout.flex.horizontal
|
||||
}
|
||||
if hasBattery() then
|
||||
gears.timer {
|
||||
timeout = config.battery.refresh_interval,
|
||||
call_now = true,
|
||||
autostart = true,
|
||||
callback = function()
|
||||
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%"
|
||||
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 {
|
||||
timeout = config.battery.refresh_interval,
|
||||
call_now = true,
|
||||
autostart = true,
|
||||
callback = function()
|
||||
batteryText.markup = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
|
||||
end
|
||||
}
|
||||
|
||||
batteryContainer:add(widget)
|
||||
end
|
||||
}
|
||||
end
|
||||
else
|
||||
battery = nil
|
||||
batteryContainer = nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
return {
|
||||
bars = bars,
|
||||
battery = battery,
|
||||
battery = batteryContainer,
|
||||
}
|
Loading…
Reference in New Issue
Block a user