Compare commits
No commits in common. "develop" and "develop" have entirely different histories.
@ -171,8 +171,7 @@ config.widgets = {
|
|||||||
battery = {
|
battery = {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
screens = { 1, 2 },
|
screens = { 1, 2 },
|
||||||
refresh_interval = 15, -- In seconds
|
refresh_interval = 30, -- In seconds
|
||||||
list_refresh_interval = 2, -- In seconds
|
|
||||||
precision = 0, -- How many decimals
|
precision = 0, -- How many decimals
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
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")
|
||||||
@ -51,13 +50,6 @@ function readCommand(command)
|
|||||||
return r
|
return r
|
||||||
end
|
end
|
||||||
|
|
||||||
function readFile(file)
|
|
||||||
local handle = io.open(file, "r")
|
|
||||||
local r = handle:read("*a")
|
|
||||||
handle:close()
|
|
||||||
return r
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1]
|
local cpuBar = bars:get_children()[1]:get_children()[1]:get_children()[1]
|
||||||
function getCpuData()
|
function getCpuData()
|
||||||
@ -139,62 +131,8 @@ gears.timer {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
---
|
|
||||||
--- Batteries
|
|
||||||
---
|
|
||||||
rawBatteries = {}
|
|
||||||
function refreshRawBatteries()
|
|
||||||
rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
|
|
||||||
end
|
|
||||||
|
|
||||||
function getBatteryLevel(battery)
|
local battery = wibox.widget {
|
||||||
local charge = 0
|
|
||||||
local capacity = 0
|
|
||||||
if fileExists(battery .. "/energy_now") then
|
|
||||||
charge = charge + readFile(battery .. "/energy_now")
|
|
||||||
capacity = capacity + readFile(battery .. "/energy_full")
|
|
||||||
elseif fileExists(battery .. "/charge_now") then
|
|
||||||
charge = charge + readFile(battery .. "/charge_now")
|
|
||||||
capacity = capacity + readFile(battery .. "/charge_full")
|
|
||||||
elseif fileExists(battery .. "/capacity_level") then
|
|
||||||
return readFile(battery .. "/capacity_level")
|
|
||||||
else
|
|
||||||
return "Unsupported"
|
|
||||||
end
|
|
||||||
return string.format("%." .. config.battery.precision .. "f", (charge / capacity) * 100) .. "%"
|
|
||||||
end
|
|
||||||
|
|
||||||
function isCharging(battery)
|
|
||||||
return readFile(battery .. "/status") == "Charging\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
local batteryContainer = wibox.widget {
|
|
||||||
layout = wibox.layout.fixed.horizontal
|
|
||||||
}
|
|
||||||
local timers = {}
|
|
||||||
function refreshBatteries()
|
|
||||||
-- refresh raw batteries and check for change
|
|
||||||
local oldBatteries = rawBatteries
|
|
||||||
refreshRawBatteries()
|
|
||||||
if rawBatteries == oldBatteries then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- clear timers
|
|
||||||
for i,v in ipairs(timers) do
|
|
||||||
v:stop()
|
|
||||||
end
|
|
||||||
timers = {};
|
|
||||||
|
|
||||||
-- clear layout
|
|
||||||
batteryContainer:set_children({})
|
|
||||||
|
|
||||||
for battery in rawBatteries:gmatch("([^\n]+)") do
|
|
||||||
local path = "/sys/class/power_supply/" .. battery
|
|
||||||
if readFile(path .. "/type"):match("Battery") then
|
|
||||||
-- widget
|
|
||||||
local widget = wibox.widget {
|
|
||||||
{
|
{
|
||||||
markup = "-- %",
|
markup = "-- %",
|
||||||
align = "center",
|
align = "center",
|
||||||
@ -203,42 +141,62 @@ function refreshBatteries()
|
|||||||
},
|
},
|
||||||
margins = beautiful.system_resources_widget_battery_margin,
|
margins = beautiful.system_resources_widget_battery_margin,
|
||||||
widget = wibox.container.margin,
|
widget = wibox.container.margin,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- tooltip
|
local batteryText = battery:get_children()[1]
|
||||||
local name = readFile(path .. "/model_name")
|
|
||||||
local tooltip = awful.tooltip {
|
|
||||||
text = name
|
|
||||||
}
|
|
||||||
tooltip:add_to_object(widget)
|
|
||||||
|
|
||||||
-- update battery level
|
local rawBatteries = readCommand("ls -A1 /sys/class/power_supply")
|
||||||
local batteryText = widget:get_children()[1]
|
|
||||||
local timer = gears.timer {
|
function getBatteries()
|
||||||
|
return rawBatteries:gmatch("([^\n]+)")
|
||||||
|
end
|
||||||
|
|
||||||
|
function hasBattery()
|
||||||
|
return rawBatteries:len() > 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function getBatteryCapacity()
|
||||||
|
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
|
||||||
|
end
|
||||||
|
return charge / capacity
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
if hasBattery() then
|
||||||
|
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 = (isCharging(path) and "+" or "") .. getBatteryLevel(path)
|
batteryText.markup = string.format("%." .. config.battery.precision .. "f", getBatteryCapacity() * 100) .. "%"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
table.insert(timers, timer)
|
else
|
||||||
|
battery = nil
|
||||||
batteryContainer:add(widget)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
gears.timer {
|
|
||||||
timeout = config.battery.list_refresh_interval,
|
|
||||||
call_now = true,
|
|
||||||
autostart = true,
|
|
||||||
callback = function()
|
|
||||||
refreshBatteries()
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
bars = bars,
|
bars = bars,
|
||||||
battery = batteryContainer,
|
battery = battery,
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user