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}
|
|
|
|
|
|
|
|
local widget = wibox.widget {
|
|
|
|
resize = true,
|
|
|
|
widget = wibox.widget.imagebox
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.critical_percentage = 5
|
2017-03-10 17:01:01 +01:00
|
|
|
|
|
|
|
function widget:update()
|
|
|
|
self.device:update_mappings()
|
2017-11-05 17:28:39 +01:00
|
|
|
|
2019-05-08 17:41:39 +02:00
|
|
|
local icon = icon_theme:lookup_icon(
|
|
|
|
self.device.IconName,
|
|
|
|
icon_size,
|
|
|
|
icon_flags
|
|
|
|
)
|
|
|
|
|
|
|
|
if icon then
|
|
|
|
self.image = icon:load_surface()
|
|
|
|
end
|
2017-03-10 17:01:01 +01:00
|
|
|
|
|
|
|
if self.device.IsPresent then
|
2017-11-05 17:28:39 +01:00
|
|
|
|
2017-03-10 17:01:01 +01:00
|
|
|
local percentage = math.floor(self.device.Percentage)
|
2017-04-09 00:19:31 +02:00
|
|
|
local warning_level = self.device.warninglevel
|
2017-03-10 17:01:01 +01:00
|
|
|
|
2019-11-09 23:14:12 +01:00
|
|
|
local charge_status_msg = ""
|
|
|
|
local what
|
|
|
|
local when
|
|
|
|
if self.device.type == power.enums.DeviceType.Battery then
|
|
|
|
if self.device.TimeToEmpty > 0 then
|
|
|
|
what = "Emtpy"
|
|
|
|
when = self.device.TimeToEmpty
|
|
|
|
elseif self.device.TimeToFull > 0 then
|
|
|
|
what = "Full"
|
|
|
|
when = self.device.TimeToFull
|
|
|
|
end
|
|
|
|
charge_status_msg = string.format("\n%s in %s", what, to_hour_min_str(when))
|
|
|
|
end
|
|
|
|
|
2017-03-10 17:01:01 +01:00
|
|
|
self.tooltip:set_text(
|
2019-11-09 23:14:12 +01:00
|
|
|
string.format(
|
|
|
|
"%d%% - %s%s",
|
|
|
|
percentage,
|
|
|
|
self.device.state.name,
|
|
|
|
charge_status_msg
|
|
|
|
)
|
|
|
|
)
|
2017-03-10 17:01:01 +01:00
|
|
|
|
2018-07-12 22:18:32 +02:00
|
|
|
local should_warn = (
|
|
|
|
self.device.state == power.enums.BatteryState.Discharging and
|
|
|
|
(
|
|
|
|
percentage <= self.critical_percentage
|
|
|
|
or warning_level == WarningLevel.Low
|
|
|
|
or warning_level == WarningLevel.Critical
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if should_warn then
|
2018-07-03 08:10:50 +02:00
|
|
|
local msg = (warning_level.name == "None" and "Low" or warning_level.name) .. " battery!"
|
2017-03-10 17:01:01 +01:00
|
|
|
naughty.notify({
|
|
|
|
preset = naughty.config.presets.critical,
|
2018-07-03 08:10:50 +02:00
|
|
|
title = msg,
|
2017-03-10 17:01:01 +01:00
|
|
|
text = percentage .. "% remaining"})
|
|
|
|
end
|
|
|
|
else
|
2017-11-05 17:28:39 +01:00
|
|
|
-- We don't know how we're powered, but we must be somehow!
|
2017-03-10 17:01:01 +01:00
|
|
|
self.tooltip:set_text("Plugged In")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function widget:init()
|
|
|
|
local manager = power.Manager
|
|
|
|
self.manager = manager
|
|
|
|
|
2017-11-05 17:28:39 +01:00
|
|
|
-- https://upower.freedesktop.org/docs/UPower.html#UPower.GetDisplayDevice
|
|
|
|
self.device = power.create_device("/org/freedesktop/UPower/devices/DisplayDevice")
|
2017-11-05 16:58:37 +01:00
|
|
|
|
|
|
|
self.device:on_properties_changed(
|
|
|
|
function ()
|
|
|
|
self:update()
|
|
|
|
end
|
|
|
|
)
|
2017-03-10 17:01:01 +01:00
|
|
|
|
|
|
|
self.tooltip = awful.tooltip({ objects = { widget },})
|
|
|
|
self.gui_client = ""
|
|
|
|
|
|
|
|
self:update()
|
|
|
|
|
|
|
|
self:buttons(awful.util.table.join(
|
|
|
|
awful.button({ }, 3,
|
|
|
|
function ()
|
|
|
|
spawn_with_shell(self.gui_client)
|
|
|
|
end
|
|
|
|
)))
|
|
|
|
end
|
|
|
|
|
|
|
|
return widget
|