2017-03-10 17:01:01 +01:00
|
|
|
--[[
|
2019-05-08 17:41:39 +02: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")
|
|
|
|
|
2019-05-08 17:41:39 +02:00
|
|
|
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
|
|
|
|
2017-04-09 00:19:31 +02: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
|
|
|
|
|
2019-05-08 17:41:39 +02:00
|
|
|
local icon_size = 64
|
|
|
|
local icon_flags = {IconLookupFlags.GENERIC_FALLBACK}
|
2019-12-13 11:16:10 +01:00
|
|
|
local notification = nil
|
2019-12-13 12:05:41 +01:00
|
|
|
local device = nil
|
2019-05-08 17:41:39 +02:00
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
local power_widget = wibox.widget {
|
2019-05-08 17:41:39 +02:00
|
|
|
resize = true,
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
}
|
|
|
|
|
2019-12-13 17:35:41 +01:00
|
|
|
local function get_percentage()
|
2019-12-13 12:05:41 +01:00
|
|
|
local percentage = device.Percentage
|
2019-12-13 11:49:04 +01:00
|
|
|
|
|
|
|
if percentage then
|
|
|
|
return math.floor(percentage)
|
|
|
|
end
|
|
|
|
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
local function update_icon(widget)
|
2019-05-08 17:41:39 +02:00
|
|
|
local icon = icon_theme:lookup_icon(
|
2019-12-13 12:05:41 +01:00
|
|
|
device.IconName,
|
2019-05-08 17:41:39 +02:00
|
|
|
icon_size,
|
|
|
|
icon_flags
|
|
|
|
)
|
|
|
|
|
|
|
|
if icon then
|
2019-12-13 12:05:41 +01:00
|
|
|
widget.image = icon:load_surface()
|
2019-05-08 17:41:39 +02:00
|
|
|
end
|
2019-12-13 11:09:00 +01:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
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!"
|
|
|
|
|
2019-12-13 11:16:10 +01:00
|
|
|
if notification then
|
|
|
|
naughty.destroy(
|
|
|
|
notification,
|
|
|
|
naughty.notificationClosedReason.dismissedByCommand
|
|
|
|
)
|
|
|
|
end
|
2019-12-13 11:09:00 +01:00
|
|
|
|
2019-12-13 11:16:10 +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
|
|
|
|
2019-12-13 12:05:41 +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
|
2019-12-13 12:05:41 +01:00
|
|
|
if device.type == power.enums.DeviceType.Battery then
|
|
|
|
if device.TimeToEmpty > 0 then
|
2019-11-09 23:14:12 +01:00
|
|
|
what = "Emtpy"
|
2019-12-13 12:05:41 +01:00
|
|
|
when = device.TimeToEmpty
|
|
|
|
elseif device.TimeToFull > 0 then
|
2019-11-09 23:14:12 +01:00
|
|
|
what = "Full"
|
2019-12-13 12:05:41 +01:00
|
|
|
when = device.TimeToFull
|
2019-11-09 23:14:12 +01:00
|
|
|
end
|
2019-11-11 01:35:18 +01:00
|
|
|
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
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
widget.tooltip:set_text(
|
2019-11-09 23:14:12 +01:00
|
|
|
string.format(
|
|
|
|
"%d%% - %s%s",
|
|
|
|
percentage,
|
2019-12-13 12:05:41 +01:00
|
|
|
device.state.name,
|
2019-11-09 23:14:12 +01:00
|
|
|
charge_status_msg
|
|
|
|
)
|
|
|
|
)
|
2017-03-10 17:01:01 +01:00
|
|
|
else
|
2017-11-05 17:28:39 +01:00
|
|
|
-- We don't know how we're powered, but we must be somehow!
|
2019-12-13 12:05:41 +01:00
|
|
|
widget.tooltip:set_text("Plugged In")
|
2017-03-10 17:01:01 +01:00
|
|
|
end
|
2019-12-13 11:09:00 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
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 (
|
2019-12-13 12:05:41 +01:00
|
|
|
device.state == power.enums.BatteryState.Discharging and
|
2019-12-13 11:49:04 +01:00
|
|
|
(
|
|
|
|
percentage <= widget.critical_percentage
|
2019-12-13 12:05:41 +01:00
|
|
|
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
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
local function update(widget)
|
|
|
|
device:update_mappings()
|
|
|
|
update_icon(widget)
|
|
|
|
update_tooltip(widget)
|
2019-12-13 11:49:04 +01:00
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
maybe_warn(
|
|
|
|
widget,
|
|
|
|
should_warn_critical(widget),
|
2019-12-13 11:49:04 +01:00
|
|
|
naughty.config.presets.critical
|
|
|
|
)
|
2019-12-13 12:28:17 +01:00
|
|
|
|
|
|
|
maybe_warn(
|
|
|
|
widget,
|
2019-12-13 17:35:41 +01:00
|
|
|
get_percentage() <= widget.warning_config.percentage,
|
2019-12-13 12:28:17 +01:00
|
|
|
widget.warning_config.preset
|
|
|
|
)
|
|
|
|
|
2017-03-10 17:01:01 +01:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
local function init(widget)
|
2017-11-05 17:28:39 +01:00
|
|
|
-- https://upower.freedesktop.org/docs/UPower.html#UPower.GetDisplayDevice
|
2019-12-13 12:05:41 +01:00
|
|
|
device = power.create_device("/org/freedesktop/UPower/devices/DisplayDevice")
|
2017-11-05 16:58:37 +01:00
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
device:on_properties_changed(
|
2017-11-05 16:58:37 +01:00
|
|
|
function ()
|
2019-12-13 12:05:41 +01:00
|
|
|
update(widget)
|
2017-11-05 16:58:37 +01:00
|
|
|
end
|
|
|
|
)
|
2017-03-10 17:01:01 +01:00
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
widget.tooltip = awful.tooltip({ objects = { widget },})
|
2019-12-13 12:50:40 +01:00
|
|
|
widget.gui_client = nil
|
2019-12-13 12:05:41 +01:00
|
|
|
widget.critical_percentage = 5
|
2017-03-10 17:01:01 +01:00
|
|
|
|
2019-12-13 12:28:17 +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,
|
|
|
|
}
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
update(widget)
|
2017-03-10 17:01:01 +01:00
|
|
|
|
2019-12-13 12:05:41 +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
|
2019-11-09 23:24:46 +01:00
|
|
|
end
|
2017-03-10 17:01:01 +01:00
|
|
|
)))
|
2019-12-13 12:05:41 +01:00
|
|
|
return widget
|
2017-03-10 17:01:01 +01:00
|
|
|
end
|
|
|
|
|
2019-12-13 12:05:41 +01:00
|
|
|
return init(power_widget)
|