2012-08-18 08:05:52 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported canLock, getLoginManager, registerSessionWithGDM */
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2019-02-08 22:21:36 -05:00
|
|
|
const { GLib, Gio } = imports.gi;
|
2012-10-23 08:35:49 -04:00
|
|
|
const Signals = imports.signals;
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const SystemdLoginManagerIface = loadInterfaceXML('org.freedesktop.login1.Manager');
|
|
|
|
const SystemdLoginSessionIface = loadInterfaceXML('org.freedesktop.login1.Session');
|
|
|
|
const SystemdLoginUserIface = loadInterfaceXML('org.freedesktop.login1.User');
|
2018-01-08 09:23:52 -05:00
|
|
|
|
2012-08-18 08:05:52 -04:00
|
|
|
const SystemdLoginManager = Gio.DBusProxy.makeProxyWrapper(SystemdLoginManagerIface);
|
|
|
|
const SystemdLoginSession = Gio.DBusProxy.makeProxyWrapper(SystemdLoginSessionIface);
|
2018-01-08 09:23:52 -05:00
|
|
|
const SystemdLoginUser = Gio.DBusProxy.makeProxyWrapper(SystemdLoginUserIface);
|
2012-08-18 08:05:52 -04:00
|
|
|
|
|
|
|
function haveSystemd() {
|
2013-03-21 04:07:23 -04:00
|
|
|
return GLib.access("/run/systemd/seats", 0) >= 0;
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
|
|
|
|
2013-03-04 13:20:12 -05:00
|
|
|
function versionCompare(required, reference) {
|
|
|
|
required = required.split('.');
|
|
|
|
reference = reference.split('.');
|
|
|
|
|
|
|
|
for (let i = 0; i < required.length; i++) {
|
2013-09-24 12:43:40 -04:00
|
|
|
let requiredInt = parseInt(required[i]);
|
|
|
|
let referenceInt = parseInt(reference[i]);
|
|
|
|
if (requiredInt != referenceInt)
|
|
|
|
return requiredInt < referenceInt;
|
2013-03-04 13:20:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function canLock() {
|
|
|
|
try {
|
|
|
|
let params = GLib.Variant.new('(ss)', ['org.gnome.DisplayManager.Manager', 'Version']);
|
|
|
|
let result = Gio.DBus.system.call_sync('org.gnome.DisplayManager',
|
|
|
|
'/org/gnome/DisplayManager/Manager',
|
|
|
|
'org.freedesktop.DBus.Properties',
|
|
|
|
'Get', params, null,
|
|
|
|
Gio.DBusCallFlags.NONE,
|
|
|
|
-1, null);
|
|
|
|
|
|
|
|
let version = result.deep_unpack()[0].deep_unpack();
|
2014-04-24 11:55:56 -04:00
|
|
|
return haveSystemd() && versionCompare('3.5.91', version);
|
2019-01-28 20:26:39 -05:00
|
|
|
} catch (e) {
|
2013-03-04 13:20:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 05:49:12 -04:00
|
|
|
|
2019-12-19 14:50:37 -05:00
|
|
|
async function registerSessionWithGDM() {
|
2019-05-02 05:49:12 -04:00
|
|
|
log("Registering session with GDM");
|
2019-12-19 14:50:37 -05:00
|
|
|
try {
|
|
|
|
await Gio.DBus.system.call(
|
|
|
|
'org.gnome.DisplayManager',
|
|
|
|
'/org/gnome/DisplayManager/Manager',
|
|
|
|
'org.gnome.DisplayManager.Manager',
|
|
|
|
'RegisterSession',
|
|
|
|
GLib.Variant.new('(a{sv})', [{}]), null,
|
|
|
|
Gio.DBusCallFlags.NONE, -1, null);
|
|
|
|
} catch (e) {
|
|
|
|
if (!e.matches(Gio.DBusError, Gio.DBusError.UNKNOWN_METHOD))
|
|
|
|
log(`Error registering session with GDM: ${e.message}`);
|
|
|
|
else
|
|
|
|
log('Not calling RegisterSession(): method not exported, GDM too old?');
|
|
|
|
}
|
2019-05-02 05:49:12 -04:00
|
|
|
}
|
|
|
|
|
2012-08-18 08:05:52 -04:00
|
|
|
let _loginManager = null;
|
|
|
|
|
|
|
|
/**
|
2019-10-17 12:41:52 -04:00
|
|
|
* getLoginManager:
|
2012-08-18 08:05:52 -04:00
|
|
|
* An abstraction over systemd/logind and ConsoleKit.
|
2019-10-17 12:41:52 -04:00
|
|
|
* @returns {object} - the LoginManager singleton
|
2012-08-18 08:05:52 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
function getLoginManager() {
|
|
|
|
if (_loginManager == null) {
|
|
|
|
if (haveSystemd())
|
|
|
|
_loginManager = new LoginManagerSystemd();
|
|
|
|
else
|
2014-04-24 11:55:56 -04:00
|
|
|
_loginManager = new LoginManagerDummy();
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return _loginManager;
|
|
|
|
}
|
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var LoginManagerSystemd = class {
|
|
|
|
constructor() {
|
2012-08-18 08:05:52 -04:00
|
|
|
this._proxy = new SystemdLoginManager(Gio.DBus.system,
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
'/org/freedesktop/login1');
|
2018-01-08 09:23:52 -05:00
|
|
|
this._userProxy = new SystemdLoginUser(Gio.DBus.system,
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
'/org/freedesktop/login1/user/self');
|
2012-10-23 08:35:49 -04:00
|
|
|
this._proxy.connectSignal('PrepareForSleep',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._prepareForSleep.bind(this));
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
getCurrentSessionProxy(callback) {
|
2013-03-25 13:25:30 -04:00
|
|
|
if (this._currentSession) {
|
2019-08-19 13:55:49 -04:00
|
|
|
callback(this._currentSession);
|
2013-03-25 13:25:30 -04:00
|
|
|
return;
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
|
|
|
|
2015-11-05 08:49:23 -05:00
|
|
|
let sessionId = GLib.getenv('XDG_SESSION_ID');
|
|
|
|
if (!sessionId) {
|
2018-01-08 09:23:52 -05:00
|
|
|
log('Unset XDG_SESSION_ID, getCurrentSessionProxy() called outside a user session. Asking logind directly.');
|
2019-08-19 20:20:08 -04:00
|
|
|
let [session, objectPath] = this._userProxy.Display;
|
2018-01-08 09:23:52 -05:00
|
|
|
if (session) {
|
|
|
|
log(`Will monitor session ${session}`);
|
|
|
|
sessionId = session;
|
|
|
|
} else {
|
|
|
|
log('Failed to find "Display" session; are we the greeter?');
|
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
for ([session, objectPath] of this._userProxy.Sessions) {
|
2018-01-08 09:23:52 -05:00
|
|
|
let sessionProxy = new SystemdLoginSession(Gio.DBus.system,
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
objectPath);
|
|
|
|
log(`Considering ${session}, class=${sessionProxy.Class}`);
|
|
|
|
if (sessionProxy.Class == 'greeter') {
|
|
|
|
log(`Yes, will monitor session ${session}`);
|
|
|
|
sessionId = session;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sessionId) {
|
|
|
|
log('No, failed to get session from logind.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 08:49:23 -05:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:38:18 -04:00
|
|
|
this._proxy.GetSessionRemote(sessionId, (result, error) => {
|
|
|
|
if (error) {
|
|
|
|
logError(error, 'Could not get a proxy for the current session');
|
|
|
|
} else {
|
|
|
|
this._currentSession = new SystemdLoginSession(Gio.DBus.system,
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
result[0]);
|
|
|
|
callback(this._currentSession);
|
|
|
|
}
|
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
canSuspend(asyncCallback) {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._proxy.CanSuspendRemote((result, error) => {
|
2016-05-26 16:20:50 -04:00
|
|
|
if (error) {
|
|
|
|
asyncCallback(false, false);
|
|
|
|
} else {
|
|
|
|
let needsAuth = result[0] == 'challenge';
|
|
|
|
let canSuspend = needsAuth || result[0] == 'yes';
|
|
|
|
asyncCallback(canSuspend, needsAuth);
|
|
|
|
}
|
2012-10-19 11:29:53 -04:00
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-10-19 11:29:53 -04:00
|
|
|
|
2020-07-29 08:22:58 -04:00
|
|
|
canRebootToBootLoaderMenu(asyncCallback) {
|
|
|
|
this._proxy.CanRebootToBootLoaderMenuRemote((result, error) => {
|
|
|
|
if (error) {
|
|
|
|
asyncCallback(false, false);
|
|
|
|
} else {
|
|
|
|
const needsAuth = result[0] === 'challenge';
|
|
|
|
const canRebootToBootLoaderMenu = needsAuth || result[0] === 'yes';
|
|
|
|
asyncCallback(canRebootToBootLoaderMenu, needsAuth);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setRebootToBootLoaderMenu() {
|
|
|
|
/* Parameter is timeout in usec, show to menu for 60 seconds */
|
|
|
|
this._proxy.SetRebootToBootLoaderMenuRemote(60000000);
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
listSessions(asyncCallback) {
|
2017-10-30 20:38:18 -04:00
|
|
|
this._proxy.ListSessionsRemote((result, error) => {
|
2013-02-11 11:39:29 -05:00
|
|
|
if (error)
|
|
|
|
asyncCallback([]);
|
|
|
|
else
|
|
|
|
asyncCallback(result[0]);
|
|
|
|
});
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-02-11 11:39:29 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
suspend() {
|
2012-10-19 11:29:53 -04:00
|
|
|
this._proxy.SuspendRemote(true);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-10-23 08:35:49 -04:00
|
|
|
|
2021-08-14 20:18:12 -04:00
|
|
|
async inhibit(reason, cancellable) {
|
|
|
|
const inVariant = new GLib.Variant('(ssss)',
|
|
|
|
['sleep', 'GNOME Shell', reason, 'delay']);
|
|
|
|
const [outVariant_, fdList] =
|
|
|
|
await this._proxy.call_with_unix_fd_list('Inhibit',
|
|
|
|
inVariant, 0, -1, null, cancellable);
|
|
|
|
const [fd] = fdList.steal_fds();
|
|
|
|
return new Gio.UnixInputStream({ fd });
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-10-23 08:35:49 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_prepareForSleep(proxy, sender, [aboutToSuspend]) {
|
2012-10-23 08:35:49 -04:00
|
|
|
this.emit('prepare-for-sleep', aboutToSuspend);
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2012-10-23 08:35:49 -04:00
|
|
|
Signals.addSignalMethods(LoginManagerSystemd.prototype);
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var LoginManagerDummy = class {
|
2019-01-31 09:08:10 -05:00
|
|
|
getCurrentSessionProxy(_callback) {
|
2014-04-24 11:55:56 -04:00
|
|
|
// we could return a DummySession object that fakes whatever callers
|
|
|
|
// expect (at the time of writing: connect() and connectSignal()
|
|
|
|
// methods), but just never calling the callback should be safer
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
canSuspend(asyncCallback) {
|
2016-05-26 16:20:50 -04:00
|
|
|
asyncCallback(false, false);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-10-19 11:29:53 -04:00
|
|
|
|
2020-07-29 08:22:58 -04:00
|
|
|
canRebootToBootLoaderMenu(asyncCallback) {
|
|
|
|
asyncCallback(false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
setRebootToBootLoaderMenu() {
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
listSessions(asyncCallback) {
|
2013-02-11 11:39:29 -05:00
|
|
|
asyncCallback([]);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-02-11 11:39:29 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
suspend() {
|
2012-10-23 08:35:49 -04:00
|
|
|
this.emit('prepare-for-sleep', true);
|
|
|
|
this.emit('prepare-for-sleep', false);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2012-10-23 08:35:49 -04:00
|
|
|
|
2021-08-14 20:18:12 -04:00
|
|
|
/* eslint-disable-next-line require-await */
|
|
|
|
async inhibit() {
|
|
|
|
return null;
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2014-04-24 11:55:56 -04:00
|
|
|
Signals.addSignalMethods(LoginManagerDummy.prototype);
|