2020-01-17 13:56:30 +08:00
|
|
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
|
|
|
|
|
* (c)LGPL2+
|
|
|
|
|
* Copyright: 2019 Tianjin KYLIN Information Technology Co., Ltd.
|
|
|
|
|
* 2012 Razor team
|
|
|
|
|
* Authors:
|
|
|
|
|
* Christian Surlykke <christian@surlykke.dk>
|
|
|
|
|
*
|
|
|
|
|
* This program or library is free software; you can redistribute it
|
|
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU Lesser General
|
|
|
|
|
* Public License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
|
|
|
|
* END_COMMON_COPYRIGHT_HEADER */
|
2019-07-25 15:56:20 +08:00
|
|
|
#include "powerprovider.h"
|
|
|
|
|
|
|
|
|
|
#include <QDBusInterface>
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
2019-08-31 16:06:13 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#define LIGHTDM_SERVICE "org.freedesktop.DisplayManager"
|
|
|
|
|
#define LIGTHDM_INTERFACE "org.freedesktop.DisplayManager.Seat"
|
|
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
#define SYSTEMD_SERVICE "org.freedesktop.login1"
|
|
|
|
|
#define SYSTEMD_PATH "/org/freedesktop/login1"
|
|
|
|
|
#define SYSTEMD_INTERFACE "org.freedesktop.login1.Manager"
|
2019-07-25 15:56:20 +08:00
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
#define UKUI_SERVICE "org.gnome.SessionManager"
|
|
|
|
|
#define UKUI_PATH "/org/gnome/SessionManager"
|
|
|
|
|
#define UKUI_INTERFACE "org.gnome.SessionManager"
|
2019-07-25 15:56:20 +08:00
|
|
|
|
|
|
|
|
#define PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
|
|
|
|
|
|
|
|
|
|
static bool dbusCall(const QString &service,
|
|
|
|
|
const QString &path,
|
|
|
|
|
const QString &interface,
|
|
|
|
|
const QDBusConnection &connection,
|
|
|
|
|
const QString &method)
|
|
|
|
|
{
|
|
|
|
|
QDBusInterface dbus(service, path, interface, connection);
|
|
|
|
|
if (!dbus.isValid()) {
|
|
|
|
|
qWarning() << "dbusCall: QDBusInterface is invalid" << service << path << interface << method;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDBusMessage msg = dbus.call(method);
|
|
|
|
|
|
|
|
|
|
if (!msg.errorName().isEmpty()) {
|
|
|
|
|
qWarning() << "Dbus error: " << msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return msg.arguments().isEmpty() ||
|
|
|
|
|
msg.arguments().constFirst().isNull() ||
|
|
|
|
|
msg.arguments().constFirst().toBool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool dbusCallSystemd(const QString &service,
|
|
|
|
|
const QString &path,
|
|
|
|
|
const QString &interface,
|
|
|
|
|
const QDBusConnection &connection,
|
|
|
|
|
const QString &method,
|
|
|
|
|
bool needBoolArg)
|
|
|
|
|
{
|
|
|
|
|
QDBusInterface dbus(service, path, interface, connection);
|
|
|
|
|
if (!dbus.isValid()) {
|
|
|
|
|
qWarning() << "dbusCall: QDBusInterface is invalid" << service << path << interface << method;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDBusMessage msg = dbus.call(method, needBoolArg ? QVariant(true) : QVariant());
|
|
|
|
|
|
|
|
|
|
if (!msg.errorName().isEmpty()) {
|
|
|
|
|
qWarning() << "Debus error: " << msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (msg.arguments().isEmpty() || msg.arguments().constFirst().isNull())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
QString response = msg.arguments().constFirst().toString();
|
|
|
|
|
qDebug() << "systemd:" << method << "=" << response;
|
|
|
|
|
return response == QLatin1String("yes") || response == QLatin1String("challenge");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool dbusGetProperty(const QString &service,
|
|
|
|
|
const QString &path,
|
|
|
|
|
const QString &interface,
|
|
|
|
|
const QDBusConnection &connection,
|
|
|
|
|
const QString &property)
|
|
|
|
|
{
|
|
|
|
|
QDBusInterface dbus(service, path, interface, connection);
|
|
|
|
|
if (!dbus.isValid()) {
|
|
|
|
|
qWarning() << "dbusGetProperty: QDBusinterface is invalid" << service << path << interface << property;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 14:03:14 +08:00
|
|
|
QDBusMessage msg = dbus.call("SwitchToGreeter");//QLatin1String("Get"), dbus.interface(),property
|
2019-07-25 15:56:20 +08:00
|
|
|
|
|
|
|
|
if (!msg.errorName().isEmpty()) {
|
|
|
|
|
qWarning() << "Dbus error: " << msg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return !msg.arguments().isEmpty() &&
|
|
|
|
|
msg.arguments().constFirst().value<QDBusVariant>().variant().toBool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PowerProvider::PowerProvider(QObject *parent) : QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PowerProvider::~PowerProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/************************************************
|
|
|
|
|
SystemdProvider
|
|
|
|
|
|
|
|
|
|
http://www.freedesktop.org/wiki/Software/systemd/logind
|
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
|
|
SystemdProvider::SystemdProvider(QObject *parent): PowerProvider(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SystemdProvider::~SystemdProvider()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-31 16:06:13 +08:00
|
|
|
bool SystemdProvider::canSwitchUser() const
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
char *seat_id = nullptr;
|
|
|
|
|
char *session_id = nullptr;
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
sd_pid_get_session(getpid(), &session_id);
|
|
|
|
|
sd_session_get_seat(session_id, &seat_id);
|
|
|
|
|
ret = sd_seat_can_multi_session(seat_id);
|
|
|
|
|
|
|
|
|
|
return ret>0;
|
|
|
|
|
*/
|
|
|
|
|
QString property = "CanSwitch";
|
|
|
|
|
QString xdg_seat_path = qgetenv("XDG_SEAT_PATH");
|
|
|
|
|
return dbusGetProperty(QLatin1String(LIGHTDM_SERVICE),
|
|
|
|
|
xdg_seat_path,
|
|
|
|
|
QLatin1String(LIGTHDM_INTERFACE),
|
|
|
|
|
QDBusConnection::systemBus(),
|
|
|
|
|
property);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:56:20 +08:00
|
|
|
bool SystemdProvider::canAction(UkuiPower::Action action) const
|
|
|
|
|
{
|
|
|
|
|
QString command;
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
2020-04-18 20:36:34 +08:00
|
|
|
case UkuiPower::PowerSwitchUser:
|
2019-08-31 16:06:13 +08:00
|
|
|
return canSwitchUser();
|
2020-04-18 20:36:34 +08:00
|
|
|
case UkuiPower::PowerReboot:
|
2019-07-25 15:56:20 +08:00
|
|
|
command = QLatin1String("CanReboot");
|
|
|
|
|
break;
|
|
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
case UkuiPower::PowerShutdown:
|
2019-07-25 15:56:20 +08:00
|
|
|
command = QLatin1String("CanPowerOff");
|
|
|
|
|
break;
|
|
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
case UkuiPower::PowerSuspend:
|
2019-07-25 15:56:20 +08:00
|
|
|
command = QLatin1String("CanSuspend");
|
|
|
|
|
break;
|
|
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
case UkuiPower::PowerHibernate:
|
2019-07-25 15:56:20 +08:00
|
|
|
command = QLatin1String("CanHibernate");
|
|
|
|
|
break;
|
|
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
default:
|
2019-07-25 15:56:20 +08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// canAction should be always silent because it can freeze
|
|
|
|
|
// g_main_context_iteration Qt event loop in QMessageBox
|
|
|
|
|
// on panel startup if there is no DBUS running.
|
|
|
|
|
return dbusCallSystemd(QLatin1String(SYSTEMD_SERVICE),
|
|
|
|
|
QLatin1String(SYSTEMD_PATH),
|
|
|
|
|
QLatin1String(SYSTEMD_INTERFACE),
|
|
|
|
|
QDBusConnection::systemBus(),
|
|
|
|
|
command,
|
|
|
|
|
false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-31 16:06:13 +08:00
|
|
|
bool SystemdProvider::doSwitchUser()
|
|
|
|
|
{
|
|
|
|
|
QString command = "SwitchToGreeter";
|
|
|
|
|
QString xdg_seat_path = qgetenv("XDG_SEAT_PATH");
|
|
|
|
|
return dbusCall(QLatin1String(LIGHTDM_SERVICE),
|
|
|
|
|
xdg_seat_path,
|
|
|
|
|
QLatin1String(LIGTHDM_INTERFACE),
|
|
|
|
|
QDBusConnection::systemBus(),
|
|
|
|
|
command);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-25 15:56:20 +08:00
|
|
|
bool SystemdProvider::doAction(UkuiPower::Action action)
|
|
|
|
|
{
|
|
|
|
|
QString command;
|
|
|
|
|
|
2020-04-18 20:36:34 +08:00
|
|
|
switch (action) {
|
2019-08-31 16:06:13 +08:00
|
|
|
case UkuiPower::PowerSwitchUser:
|
|
|
|
|
return doSwitchUser();
|
2019-07-25 15:56:20 +08:00
|
|
|
case UkuiPower::PowerReboot:
|
|
|
|
|
command = QLatin1String("Reboot");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerShutdown:
|
|
|
|
|
command = QLatin1String("PowerOff");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerSuspend:
|
|
|
|
|
command = QLatin1String("Suspend");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerHibernate:
|
|
|
|
|
command = QLatin1String("Hibernate");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dbusCallSystemd(QLatin1String(SYSTEMD_SERVICE),
|
|
|
|
|
QLatin1String(SYSTEMD_PATH),
|
|
|
|
|
QLatin1String(SYSTEMD_INTERFACE),
|
|
|
|
|
QDBusConnection::systemBus(),
|
|
|
|
|
command,
|
|
|
|
|
true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-03 17:12:22 +08:00
|
|
|
UKUIProvider::UKUIProvider(QObject *parent): PowerProvider (parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-07-25 15:56:20 +08:00
|
|
|
|
2019-08-03 17:12:22 +08:00
|
|
|
UKUIProvider::~UKUIProvider()
|
|
|
|
|
{}
|
2019-07-25 15:56:20 +08:00
|
|
|
|
2019-08-03 17:12:22 +08:00
|
|
|
bool UKUIProvider::canAction(UkuiPower::Action action) const
|
|
|
|
|
{
|
|
|
|
|
QString command;
|
|
|
|
|
switch (action) {
|
|
|
|
|
case UkuiPower::PowerLogout:
|
|
|
|
|
command = QLatin1String("canLogout");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerReboot:
|
|
|
|
|
command = QLatin1String("canReboot");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerShutdown:
|
|
|
|
|
command = QLatin1String("canPowerOff");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-07-25 15:56:20 +08:00
|
|
|
|
2019-08-22 15:55:55 +08:00
|
|
|
return dbusCall(QLatin1String(UKUI_SERVICE),
|
|
|
|
|
QLatin1String(UKUI_PATH),
|
|
|
|
|
QLatin1String(UKUI_INTERFACE),
|
|
|
|
|
QDBusConnection::sessionBus(),
|
|
|
|
|
command);
|
2019-08-03 17:12:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UKUIProvider::doAction(UkuiPower::Action action)
|
|
|
|
|
{
|
|
|
|
|
QString command;
|
|
|
|
|
switch (action) {
|
|
|
|
|
case UkuiPower::PowerLogout:
|
|
|
|
|
command = QLatin1String("logout");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerReboot:
|
|
|
|
|
command = QLatin1String("reboot");
|
|
|
|
|
break;
|
|
|
|
|
case UkuiPower::PowerShutdown:
|
|
|
|
|
command = QLatin1String("powerOff");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dbusCall(QLatin1String(UKUI_SERVICE),
|
|
|
|
|
QLatin1String(UKUI_PATH),
|
|
|
|
|
QLatin1String(UKUI_INTERFACE),
|
|
|
|
|
QDBusConnection::sessionBus(),
|
2019-08-22 15:55:55 +08:00
|
|
|
command);
|
2019-08-03 17:12:22 +08:00
|
|
|
}
|
2019-07-25 15:56:20 +08:00
|
|
|
|
|
|
|
|
|