awesome-power_widget/power_widget.lua

204 lines
5.0 KiB
Lua
Raw Normal View History

2017-03-10 17:01:01 +01:00
--[[
Copyright 2017-2019 Stefano Mazzucco <stefano AT curso DOT re>
2017-03-10 17:01:01 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
local wibox = require("wibox")
local awful = require("awful")
local naughty = require("naughty")
local lgi = require('lgi')
local icon_theme = lgi.Gtk.IconTheme.get_default()
local IconLookupFlags = lgi.Gtk.IconLookupFlags
2017-03-10 17:01:01 +01:00
local power = require("upower_dbus")
2017-11-05 16:18:12 +01:00
local WarningLevel = power.enums.BatteryWarningLevel
2017-03-10 17:01:01 +01:00
local spawn_with_shell = awful.spawn.with_shell or awful.util.spawn_with_shell
2017-03-10 17:01:01 +01:00
2019-11-09 23:14:12 +01:00
local math = math
local string = string
local function to_hour_min_str(seconds)
local hours = math.floor(seconds/3600)
local minutes = math.ceil( (seconds % 3600) / 60)
return string.format("%02dh:%02dm", hours, minutes)
end
local icon_size = 64
local icon_flags = {IconLookupFlags.GENERIC_FALLBACK}
local notification = nil
local device = nil
local power_widget = wibox.widget {
resize = true,
widget = wibox.widget.imagebox
}
2019-12-13 17:35:41 +01:00
local function get_percentage()
local percentage = device.Percentage
2019-12-13 11:49:04 +01:00
if percentage then
return math.floor(percentage)
end
return 0
end
local function update_icon(widget)
local icon = icon_theme:lookup_icon(
device.IconName,
icon_size,
icon_flags
)
if icon then
widget.image = icon:load_surface()
end
2019-12-13 11:09:00 +01:00
end
local function maybe_warn(widget, warning_condition, notification_preset)
local warning_level = device.warninglevel or "None"
2019-12-13 17:35:41 +01:00
local percentage = get_percentage()
2019-12-13 11:09:00 +01:00
2019-12-13 11:49:04 +01:00
if warning_condition then
2019-12-13 11:09:00 +01:00
local msg = (warning_level.name == "None" and "Low" or warning_level.name) .. " battery!"
if notification then
naughty.destroy(
notification,
naughty.notificationClosedReason.dismissedByCommand
)
end
2019-12-13 11:09:00 +01:00
notification = naughty.notify({
2019-12-13 11:49:04 +01:00
preset = notification_preset,
2019-12-13 11:09:00 +01:00
title = msg,
text = percentage .. "% remaining"})
end
end
2017-03-10 17:01:01 +01:00
local function update_tooltip(widget)
if device.IsPresent then
2019-12-13 17:35:41 +01:00
local percentage = get_percentage()
2019-11-09 23:14:12 +01:00
local charge_status_msg = ""
local what
local when
if device.type == power.enums.DeviceType.Battery then
if device.TimeToEmpty > 0 then
2019-11-09 23:14:12 +01:00
what = "Emtpy"
when = device.TimeToEmpty
elseif device.TimeToFull > 0 then
2019-11-09 23:14:12 +01:00
what = "Full"
when = device.TimeToFull
2019-11-09 23:14:12 +01:00
end
if when then
charge_status_msg = string.format("\n%s in %s", what, to_hour_min_str(when))
end
2019-11-09 23:14:12 +01:00
end
widget.tooltip:set_text(
2019-11-09 23:14:12 +01:00
string.format(
"%d%% - %s%s",
percentage,
device.state.name,
2019-11-09 23:14:12 +01:00
charge_status_msg
)
)
2017-03-10 17:01:01 +01:00
else
-- We don't know how we're powered, but we must be somehow!
widget.tooltip:set_text("Plugged In")
2017-03-10 17:01:01 +01:00
end
2019-12-13 11:09:00 +01:00
end
local function should_warn_critical(widget)
if not device.IsPresent then
2019-12-13 11:49:04 +01:00
return false
end
2019-12-13 17:35:41 +01:00
local percentage = get_percentage()
2019-12-13 11:49:04 +01:00
return (
device.state == power.enums.BatteryState.Discharging and
2019-12-13 11:49:04 +01:00
(
percentage <= widget.critical_percentage
or device.warninglevel == WarningLevel.Low
or device.warninglevel == WarningLevel.Critical
2019-12-13 11:49:04 +01:00
)
)
end
2019-12-13 11:09:00 +01:00
local function update(widget)
device:update_mappings()
update_icon(widget)
update_tooltip(widget)
2019-12-13 11:49:04 +01:00
maybe_warn(
widget,
should_warn_critical(widget),
2019-12-13 11:49:04 +01:00
naughty.config.presets.critical
)
maybe_warn(
widget,
2019-12-13 17:35:41 +01:00
get_percentage() <= widget.warning_config.percentage,
widget.warning_config.preset
)
if device.state ~= power.enums.BatteryState.Discharging and notification then
naughty.destroy(
notification,
naughty.notificationClosedReason.dismissedByCommand
)
end
2017-03-10 17:01:01 +01:00
end
local function init(widget)
-- https://upower.freedesktop.org/docs/UPower.html#UPower.GetDisplayDevice
device = power.create_device("/org/freedesktop/UPower/devices/DisplayDevice")
device:on_properties_changed(
function ()
update(widget)
end
)
2017-03-10 17:01:01 +01:00
widget.tooltip = awful.tooltip({ objects = { widget },})
widget.gui_client = nil
widget.critical_percentage = 5
2017-03-10 17:01:01 +01:00
widget.warning_config = {
percentage = -1, -- disabled by default
-- https://awesomewm.org/doc/api/libraries/naughty.html#config.presets
preset = naughty.config.presets.normal,
}
update(widget)
2017-03-10 17:01:01 +01:00
widget:buttons(awful.util.table.join(
2019-12-16 22:25:01 +01:00
awful.button({ }, 3,
function ()
if widget.gui_client then
spawn_with_shell(widget.gui_client)
end
end
2017-03-10 17:01:01 +01:00
)))
return widget
2017-03-10 17:01:01 +01:00
end
return init(power_widget)