awesome-power_widget/power_widget.lua

142 lines
4.5 KiB
Lua
Raw Normal View History

2017-03-10 17:01:01 +01:00
--[[
Copyright 2017 Stefano Mazzucco <stefano AT curso DOT re>
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 beautiful = require("beautiful")
local wibox = require("wibox")
local awful = require("awful")
local naughty = require("naughty")
local power = require("upower_dbus")
-- Awesome DBus C API
local cdbus = dbus -- luacheck: ignore
local spawn_with_shell = awful.spawn.with_shell or awful.util.spawn_with_shell
2017-03-10 17:01:01 +01:00
local icon_theme_dirs = { -- The trailing slash is mandatory!
"/usr/share/icons/Adwaita/scalable/status/",
"/usr/share/icons/Adwaita/scalable/devices/"}
local icon_theme_extensions = {"svg"}
icon_theme_dirs = beautiful.upower_icon_theme_dirs or icon_theme_dirs
icon_theme_extensions = beautiful.upower_icon_theme_extension or icon_theme_extensions
local widget = wibox.widget.imagebox()
local function build_icon_path(device)
if device.IconName then
return awful.util.geticonpath(device.IconName, icon_theme_extensions, icon_theme_dirs)
end
return ""
end
function widget:update()
self.device:update_mappings()
self:set_image(build_icon_path(self.device))
if self.device.IsPresent then
local percentage = math.floor(self.device.Percentage)
local warning_level = self.device.warninglevel
2017-03-10 17:01:01 +01:00
self.tooltip:set_text(
percentage .. "%" .. " - " .. self.device.state)
2017-03-10 17:01:01 +01:00
if warning_level == "Low" or warning_level == "Critical" then
naughty.notify({
preset = naughty.config.presets.critical,
title = warning_level .. " battery!",
text = percentage .. "% remaining"})
end
else
self.tooltip:set_text("Plugged In")
end
end
local function setup_signals(wdg)
if wdg.device then
-- Recent versions of UPower do not implement signals any more
-- Use the PropertiesChanged signal instead.
cdbus.add_match(
"system",
"type=signal" ..
",interface=org.freedesktop.DBus.Properties" ..
",member=PropertiesChanged" ..
",path=" ..
wdg.device.object_path
2017-03-10 17:01:01 +01:00
)
cdbus.connect_signal("org.freedesktop.DBus.Properties",
-- PropertiesChanged (STRING interface_name,
-- DICT<STRING,VARIANT> changed_properties,
-- ARRAY<STRING> invalidated_properties);
function (info, interface, changed, _)
if info.member == "PropertiesChanged"
and interface == wdg.device.interface
and info.path == wdg.device.object_path
2017-03-10 17:01:01 +01:00
then
for k, v in pairs(changed) do
wdg.device[k] = v
end
2017-03-10 17:01:01 +01:00
wdg:update()
end
end)
end
end
-- Although it would be nice to use ctx = lgi.GLib.MainLoop:get_context() and
-- then ctx:iteration() to update the proxy object, this causes a Lua Stack
-- Dump in awesome. You will see a line saying "Something was left on the Lua
-- stack, this is a bug!" in the stderr. Instead, copy over the values in a
-- simple table and to be used when PropertiesChanged is emitted.
local function get_data(device)
device = device or {}
local out = {}
for k, v in pairs(device) do
out[k] = v
end
for k, _ in pairs(device.accessors) do
out[k] = device[k]
end
return out
end
2017-03-10 17:01:01 +01:00
function widget:init()
local manager = power.Manager
self.manager = manager
local devices = {}
for _, d in ipairs(self.manager.devices) do
devices[d.type] = d
2017-03-10 17:01:01 +01:00
end
self.device = get_data(devices["Battery"] or devices["Line Power"])
2017-03-10 17:01:01 +01:00
self.tooltip = awful.tooltip({ objects = { widget },})
self.gui_client = ""
setup_signals(self)
self:update()
self:buttons(awful.util.table.join(
awful.button({ }, 3,
function ()
spawn_with_shell(self.gui_client)
end
)))
end
return widget