I'm trying to develop an extension for gnome, that allows me to inhibit system sleep when the lid is closed directly from the status bar without having to open tweaks.
Till now, I have a basic js script that successfully connect to the gdbus and is able to execute commands, but I can't get the inhibitor working. I believe the problem is I'm not handling the file descriptor that the inhibit function returns in the right way, because the documentation says the inhibitor is kept in place only while the file descriptor is referenced and open. But it's also possible that I'm not calling the function correctly.
If anyone could give me some advise I'd be really thankful. I'm running Ubuntu 18.04.4 and gnome 3.28.2
/* -*- mode: js2 - indent-tabs-mode: nil - js2-basic-offset: 4 -*- */
/*jshint multistr:true */
/*jshint esnext:true */
/*global imports: true */
/*global global: true */
/*global log: true */
'use strict';
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Mainloop = imports.mainloop;
const Atk = imports.gi.Atk;
const DBusInterface = '<node>\
<interface name="org.freedesktop.login1.Manager">\
<method name="Inhibit">\
<arg type="s" direction="in" />\
<arg type="s" direction="in" />\
<arg type="s" direction="in" />\
<arg type="s" direction="in" />\
<arg type="h" direction="out" />\
</method>\
<method name="ListInhibitors">\
<arg type="a(ssssuu)" direction="out" />\
</method>\
</interface>\
</node>';
const DBusProxy = Gio.DBusProxy.makeProxyWrapper(DBusInterface);
let proxy = new DBusProxy(
Gio.DBus.system,
"org.freedesktop.login1",
"/org/freedesktop/login1"
);
let inhibitors = proxy.ListInhibitorsSync();
print(inhibitors);
let fd = proxy.InhibitSync('handle-lid-switch',
'gnome-extension-lid-inhibitor',
'user preference',
'block');
inhibitors = proxy.ListInhibitorsSync();
print(inhibitors);
let loop = new GLib.MainLoop(null, false);
loop.run();