From 422ae44cf64849d043240e9b4e0aec60fc628dad Mon Sep 17 00:00:00 2001 From: Mike Crute Date: Sat, 8 Oct 2022 10:37:24 -0700 Subject: More notes on dbus --- .config/awesome/dbus_test.lua | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/.config/awesome/dbus_test.lua b/.config/awesome/dbus_test.lua index 4394029..50b9ff8 100644 --- a/.config/awesome/dbus_test.lua +++ b/.config/awesome/dbus_test.lua @@ -42,3 +42,68 @@ Gio.Async.call(function() "GetAll" ) end)() + + + +-- https://docs.gtk.org/gio/index.html + +-- From https://stackoverflow.com/questions/43588324/how-to-call-dbus-methods-in-awesome-wm + +local lgi = require("lgi") +local Gio = lgi.require("Gio") +local GLib = lgi.require("GLib") + +-- Workaround for https://github.com/pavouk/lgi/issues/142 +local function bus_get_async(type) + Gio.bus_get(type, nil, coroutine.running()) + local a, b = coroutine.yield() + return Gio.bus_get_finish(b) +end + +local function inhibit(bus, what, who, why, mode) + local name = "org.freedesktop.login1" + local object = "/org/freedesktop/login1" + local interface = "org.freedesktop.login1.Manager" + local message = Gio.DBusMessage.new_method_call(name, object, interface, "Inhibit") + message:set_body(GLib.Variant("(ssss)", + { what, who, why, mode })) + + local timeout = -1 -- Just use the default + local result, err = bus:async_send_message_with_reply(message, Gio.DBusSendMessageFlags.NONE, + timeout, nil) + + if err then + print("error: " .. tostring(err)) + return + end + + if result:get_message_type() == "ERROR" then + local _, err = result:to_gerror() + print("error: " .. tostring(err)) + return + end + + local fd_list = result:get_unix_fd_list() + local fd, err = fd_list:get(0) + if err then + print("error: " .. tostring(err)) + return + end + + -- Now... somehow turn this fd into something we can close + return Gio.UnixInputStream.new(fd, true) +end + +Gio.Async.call(function() + local bus = bus_get_async(Gio.BusType.SYSTEM) + local a = inhibit(bus, "shutdown:sleep", "hi, it's me!", "Just because", "delay") + print("got lock") + io.popen("sleep 10"):read("*a") + a:async_close() + -- Speed up deletion of the GDBusMessage that still references the FD + collectgarbage("collect") + collectgarbage("collect") + + print("released lock") + io.popen("sleep 10"):read("*a") +end)() -- cgit v1.2.3