Use on_properties_changed on device to update the widget

Instead of using Awesome's DBus api, use directly the on_properties_changed
method on the device object. This change removes a lot of boilerplate code and
makes the module much simpler!
This commit is contained in:
Stefano Mazzucco 2017-11-05 15:58:37 +00:00
parent 584fb4b3f6
commit 14f0f5a962
2 changed files with 35 additions and 56 deletions

View File

@ -25,9 +25,6 @@ local power = require("upower_dbus")
local WarningLevel = power.enums.BatteryWarningLevel local WarningLevel = power.enums.BatteryWarningLevel
local DeviceType = power.enums.DeviceType local DeviceType = power.enums.DeviceType
-- Awesome DBus C API
local cdbus = dbus -- luacheck: ignore
local spawn_with_shell = awful.spawn.with_shell or awful.util.spawn_with_shell local spawn_with_shell = awful.spawn.with_shell or awful.util.spawn_with_shell
local icon_theme_dirs = { -- The trailing slash is mandatory! local icon_theme_dirs = { -- The trailing slash is mandatory!
"/usr/share/icons/Adwaita/scalable/status/", "/usr/share/icons/Adwaita/scalable/status/",
@ -67,54 +64,6 @@ function widget:update()
end end
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
)
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
then
for k, v in pairs(changed) do
wdg.device[k] = v
end
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
function widget:init() function widget:init()
local manager = power.Manager local manager = power.Manager
self.manager = manager self.manager = manager
@ -123,15 +72,18 @@ function widget:init()
for _, d in ipairs(self.manager.devices) do for _, d in ipairs(self.manager.devices) do
devices[d.type] = d devices[d.type] = d
end end
self.device = get_data(
devices[DeviceType.Battery] self.device = devices[DeviceType.Battery] or devices[DeviceType["Line Power"]]
or devices[DeviceType["Line Power"]])
self.device:on_properties_changed(
function ()
self:update()
end
)
self.tooltip = awful.tooltip({ objects = { widget },}) self.tooltip = awful.tooltip({ objects = { widget },})
self.gui_client = "" self.gui_client = ""
setup_signals(self)
self:update() self:update()
self:buttons(awful.util.table.join( self:buttons(awful.util.table.join(

View File

@ -0,0 +1,27 @@
package = "power_widget"
version = "0.3.2-1"
source = {
url = "git://github.com/stefano-m/awesome-power_widget",
tag = "v0.3.2"
}
description = {
summary = "A Power widget for the Awesome Window Manager",
detailed = [[
Monitor your power devices in Awesome with UPower and DBus.
]],
homepage = "https://github.com/stefano-m/awesome-power_widget",
license = "GPL v3"
}
supported_platforms = {
"linux"
}
dependencies = {
"lua >= 5.1",
"upower_dbus >= 0.3.0, < 0.4"
}
build = {
type = "builtin",
modules = {
power_widget = "power_widget.lua"
}
}