-
-
Save mlen/61b474ad7fa32a274187bf52f0bcb6b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lgi = require 'lgi' | |
local Gio = lgi.require 'Gio' | |
local GLib = lgi.require 'GLib' | |
local ldbus = {} | |
ldbus.Bus = {} | |
ldbus.Object = {} | |
function ldbus.Object:__index(method) | |
local info = self.proxy:get_interface_info() | |
local minfo = info:lookup_method(method) | |
if minfo then | |
return function(...) | |
local args = {...} | |
local serialized = {} | |
if #args == #minfo.in_args and #args > 0 then | |
for i, t in ipairs(minfo.in_args) do | |
serialized[i] = GLib.Variant(t, args[i]) | |
end | |
end | |
local params = GLib.Variant.new_tuple(serialized, #serialized) | |
local result = self.proxy:call_sync(method, params, Gio.DBusCallFlags.NONE, -1, nil) | |
if not result then | |
return nil | |
end | |
return unpack(result.value) | |
end | |
else | |
local pinfo = info:lookup_property(method) | |
if pinfo then | |
return self.bus:GetObject(self.object_name, self.object_path, "org.freedesktop.DBus.Properties").Get(self.object_iface, method) | |
else | |
return nil | |
end | |
end | |
end | |
function ldbus.Bus:Open(name) | |
local bus = Gio.bus_get_sync(Gio.BusType[name]) | |
if not bus then | |
return nil | |
end | |
local table = {} | |
table.bus = bus | |
table.meta = {} | |
table.meta.__index = self | |
setmetatable(table, table.meta) | |
return table | |
end | |
function ldbus.Bus:Connect(address) | |
local bus = Gio.DBusConnection.new_for_address_sync(address, Gio.DBusConnectionFlags.NONE, nil, nil) | |
if not bus then | |
return nil | |
end | |
local table = {} | |
table.bus = bus | |
table.meta = {} | |
table.meta.__index = self | |
setmetatable(table, table.meta) | |
return table | |
end | |
function ldbus.Bus:GetObject(name, path, iface) | |
local table = {} | |
table.object_name = name | |
table.object_path = path | |
table.object_iface = iface | |
table.bus = self | |
local result = self.bus:call_sync(name, path, "org.freedesktop.DBus.Introspectable", "Introspect", nil, nil, Gio.DBusSendMessageFlags.NONE, -1, nil) | |
if not result then | |
return nil | |
end | |
local info = Gio.DBusNodeInfo.new_for_xml(result.value[1]):lookup_interface(iface) | |
table.proxy = Gio.DBusProxy.new_sync(self.bus, Gio.DBusProxyFlags.NONE, info, name, path, iface, nil) | |
if not table.proxy then | |
return nil | |
end | |
setmetatable(table, ldbus.Object) | |
return table | |
end | |
local bus = ldbus.Bus:Open('SYSTEM') | |
local ck = bus:GetObject('org.freedesktop.ConsoleKit', '/org/freedesktop/ConsoleKit/Manager', 'org.freedesktop.ConsoleKit.Manager') | |
print(ck.CanStop()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment