2012-08-18 08:05:52 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Lang = imports.lang;
|
2012-10-19 11:29:53 -04:00
|
|
|
const Mainloop = imports.mainloop;
|
2012-08-18 08:05:52 -04:00
|
|
|
const Shell = imports.gi.Shell;
|
2012-10-23 08:35:49 -04:00
|
|
|
const Signals = imports.signals;
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2013-10-24 17:51:58 -04:00
|
|
|
const SystemdLoginManagerIface = '<node> \
|
|
|
|
<interface name="org.freedesktop.login1.Manager"> \
|
|
|
|
<method name="Suspend"> \
|
|
|
|
<arg type="b" direction="in"/> \
|
|
|
|
</method> \
|
|
|
|
<method name="CanSuspend"> \
|
|
|
|
<arg type="s" direction="out"/> \
|
|
|
|
</method> \
|
|
|
|
<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="GetSession"> \
|
|
|
|
<arg type="s" direction="in"/> \
|
|
|
|
<arg type="o" direction="out"/> \
|
|
|
|
</method> \
|
|
|
|
<method name="ListSessions"> \
|
|
|
|
<arg name="sessions" type="a(susso)" direction="out"/> \
|
|
|
|
</method> \
|
|
|
|
<signal name="PrepareForSleep"> \
|
|
|
|
<arg type="b" direction="out"/> \
|
|
|
|
</signal> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2013-10-24 17:51:58 -04:00
|
|
|
const SystemdLoginSessionIface = '<node> \
|
|
|
|
<interface name="org.freedesktop.login1.Session"> \
|
|
|
|
<signal name="Lock" /> \
|
|
|
|
<signal name="Unlock" /> \
|
2014-03-07 19:35:02 -05:00
|
|
|
<property name="Active" type="b" access="read" /> \
|
2013-10-24 17:51:58 -04:00
|
|
|
</interface> \
|
|
|
|
</node>';
|
2012-08-18 08:05:52 -04:00
|
|
|
|
|
|
|
const SystemdLoginManager = Gio.DBusProxy.makeProxyWrapper(SystemdLoginManagerIface);
|
|
|
|
const SystemdLoginSession = Gio.DBusProxy.makeProxyWrapper(SystemdLoginSessionIface);
|
|
|
|
|
|
|
|
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);
|
2013-03-04 13:20:12 -05:00
|
|
|
} catch(e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-18 08:05:52 -04:00
|
|
|
let _loginManager = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* LoginManager:
|
|
|
|
* An abstraction over systemd/logind and ConsoleKit.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LoginManagerSystemd = new Lang.Class({
|
|
|
|
Name: 'LoginManagerSystemd',
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._proxy = new SystemdLoginManager(Gio.DBus.system,
|
|
|
|
'org.freedesktop.login1',
|
|
|
|
'/org/freedesktop/login1');
|
2012-10-23 08:35:49 -04:00
|
|
|
this._proxy.connectSignal('PrepareForSleep',
|
|
|
|
Lang.bind(this, this._prepareForSleep));
|
2012-08-18 08:05:52 -04:00
|
|
|
},
|
|
|
|
|
2013-03-25 13:25:30 -04:00
|
|
|
getCurrentSessionProxy: function(callback) {
|
|
|
|
if (this._currentSession) {
|
|
|
|
callback (this._currentSession);
|
|
|
|
return;
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
|
|
|
|
2013-03-25 13:25:30 -04:00
|
|
|
this._proxy.GetSessionRemote(GLib.getenv('XDG_SESSION_ID'), Lang.bind(this,
|
|
|
|
function(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);
|
|
|
|
}
|
|
|
|
}));
|
2012-08-18 08:05:52 -04:00
|
|
|
},
|
|
|
|
|
2012-10-19 11:29:53 -04:00
|
|
|
canSuspend: function(asyncCallback) {
|
|
|
|
this._proxy.CanSuspendRemote(function(result, error) {
|
|
|
|
if (error)
|
|
|
|
asyncCallback(false);
|
|
|
|
else
|
2015-04-22 18:00:16 -04:00
|
|
|
asyncCallback(result[0] != 'no' && result[0] != 'na');
|
2012-10-19 11:29:53 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-02-11 11:39:29 -05:00
|
|
|
listSessions: function(asyncCallback) {
|
|
|
|
this._proxy.ListSessionsRemote(function(result, error) {
|
|
|
|
if (error)
|
|
|
|
asyncCallback([]);
|
|
|
|
else
|
|
|
|
asyncCallback(result[0]);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2012-10-19 11:29:53 -04:00
|
|
|
suspend: function() {
|
|
|
|
this._proxy.SuspendRemote(true);
|
2012-10-23 08:35:49 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
inhibit: function(reason, callback) {
|
|
|
|
let inVariant = GLib.Variant.new('(ssss)',
|
|
|
|
['sleep',
|
|
|
|
'GNOME Shell',
|
|
|
|
reason,
|
|
|
|
'delay']);
|
|
|
|
this._proxy.call_with_unix_fd_list('Inhibit', inVariant, 0, -1, null, null,
|
|
|
|
Lang.bind(this, function(proxy, result) {
|
|
|
|
let fd = -1;
|
|
|
|
try {
|
|
|
|
let [outVariant, fdList] = proxy.call_with_unix_fd_list_finish(result);
|
2014-04-15 17:52:50 -04:00
|
|
|
fd = fdList.steal_fds()[0];
|
2012-10-23 08:35:49 -04:00
|
|
|
callback(new Gio.UnixInputStream({ fd: fd }));
|
|
|
|
} catch(e) {
|
|
|
|
logError(e, "Error getting systemd inhibitor");
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
},
|
|
|
|
|
|
|
|
_prepareForSleep: function(proxy, sender, [aboutToSuspend]) {
|
|
|
|
this.emit('prepare-for-sleep', aboutToSuspend);
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
|
|
|
});
|
2012-10-23 08:35:49 -04:00
|
|
|
Signals.addSignalMethods(LoginManagerSystemd.prototype);
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2014-04-24 11:55:56 -04:00
|
|
|
const LoginManagerDummy = new Lang.Class({
|
|
|
|
Name: 'LoginManagerDummy',
|
2012-08-18 08:05:52 -04:00
|
|
|
|
2013-03-25 13:25:30 -04:00
|
|
|
getCurrentSessionProxy: function(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
|
2012-08-18 08:05:52 -04:00
|
|
|
},
|
|
|
|
|
2012-10-19 11:29:53 -04:00
|
|
|
canSuspend: function(asyncCallback) {
|
2013-02-08 02:38:36 -05:00
|
|
|
asyncCallback(false);
|
2012-10-19 11:29:53 -04:00
|
|
|
},
|
|
|
|
|
2013-02-11 11:39:29 -05:00
|
|
|
listSessions: function(asyncCallback) {
|
|
|
|
asyncCallback([]);
|
|
|
|
},
|
|
|
|
|
2012-10-19 11:29:53 -04:00
|
|
|
suspend: function() {
|
2012-10-23 08:35:49 -04:00
|
|
|
this.emit('prepare-for-sleep', true);
|
|
|
|
this.emit('prepare-for-sleep', false);
|
|
|
|
},
|
|
|
|
|
|
|
|
inhibit: function(reason, callback) {
|
|
|
|
callback(null);
|
2012-08-18 08:05:52 -04:00
|
|
|
}
|
|
|
|
});
|
2014-04-24 11:55:56 -04:00
|
|
|
Signals.addSignalMethods(LoginManagerDummy.prototype);
|