1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2010-2011 Razor team
* Authors:
* Petr Vanek <petr@scribus.info>
* Copyright (c) 2016 Luís Pereira <luis.artur.pereira@gmail.com>
* Copyright (c) 2012 The Chromium Authors. All rights reserved.
*
* 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 */
#include <QProcess>
#include <QSettings>
#include "lxqtscreensaver.h"
#include "lxqtsettings.h"
#include "lxqttranslator.h"
#include <memory>
#include <XdgIcon>
#include <QMessageBox>
#include <QAction>
#include <QPointer>
#include <QProcess>
#include <QGuiApplication> // for Q_DECLARE_TR_FUNCTIONS and platform detect
#include <QDebug>
#include <X11/extensions/scrnsaver.h>
// Avoid polluting everything with X11/Xlib.h:
typedef struct _XDisplay Display;
typedef unsigned long XAtom;
typedef unsigned long XID;
extern "C" {
int XFree(void*);
}
template <class T, class R, R (*F)(T*)>
struct XObjectDeleter {
inline void operator()(void* ptr) const { F(static_cast<T*>(ptr)); }
};
template <class T, class D = XObjectDeleter<void, int, XFree>>
using XScopedPtr = std::unique_ptr<T, D>;
namespace LXQt {
static bool GetProperty(XID window, const std::string& property_name, long max_length,
Atom* type, int* format, unsigned long* num_items,
unsigned char** property);
static bool GetIntArrayProperty(XID window,
const std::string& property_name,
std::vector<int>* value);
static bool GetProperty(XID window, const std::string& property_name, long max_length,
Atom* type, int* format, unsigned long* num_items,
unsigned char** property)
{
if (auto *x11Application = qGuiApp->nativeInterface<QNativeInterface::QX11Application>())
{
Atom property_atom = XInternAtom(x11Application->display(), property_name.c_str(), false);
unsigned long remaining_bytes = 0;
return XGetWindowProperty(x11Application->display(),
window,
property_atom,
0, // offset into property data to read
max_length, // max length to get
False, // deleted
AnyPropertyType,
type,
format,
num_items,
&remaining_bytes,
property);
}
else
{
qWarning() << "GetProperty() not implemented on Wayland";
return false;
}
}
static bool GetIntArrayProperty(XID window,
const std::string& property_name,
std::vector<int>* value)
{
Atom type = None;
int format = 0; // size in bits of each item in 'property'
unsigned long num_items = 0;
unsigned char* properties = nullptr;
int result = GetProperty(window, property_name,
(~0L), // (all of them)
&type, &format, &num_items, &properties);
XScopedPtr<unsigned char> scoped_properties(properties);
if (result != Success)
return false;
if (format != 32)
return false;
long* int_properties = reinterpret_cast<long*>(properties);
value->clear();
for (unsigned long i = 0; i < num_items; ++i)
{
value->push_back(static_cast<int>(int_properties[i]));
}
return true;
}
class ScreenSaverPrivate
{
Q_DECLARE_TR_FUNCTIONS(LXQt::ScreenSaver)
Q_DECLARE_PUBLIC(ScreenSaver)
ScreenSaver* const q_ptr;
public:
ScreenSaverPrivate(ScreenSaver *q) : q_ptr(q) {
if (QGuiApplication::platformName() == QStringLiteral("xcb")) {
Settings settings(QStringLiteral("lxqt"));
settings.beginGroup(QL1SV("Screensaver"));
QString lockCommand(settings.value(QL1SV("lock_command"), QL1SV("xdg-screensaver lock")).toString());
settings.endGroup();
QString sessionConfig(QString::fromLocal8Bit(qgetenv("LXQT_SESSION_CONFIG")));
Settings sessionSettings(sessionConfig.isEmpty() ? QStringLiteral("session") : sessionConfig);
lock_command = sessionSettings.value(QL1SV("lock_command"), lockCommand).toString();
} else if (QGuiApplication::platformName() == QStringLiteral("wayland")) {
Settings settings(QStringLiteral("lxqt"));
settings.beginGroup(QL1SV("Screensaver"));
QString lockCommand(settings.value(QL1SV("lock_command_wayland")).toString());
settings.endGroup();
QString sessionConfig(QString::fromLocal8Bit(qgetenv("LXQT_SESSION_CONFIG")));
Settings sessionSettings(sessionConfig.isEmpty() ? QStringLiteral("session") : sessionConfig);
lock_command = sessionSettings.value(QL1SV("lock_command_wayland"), lockCommand).toString();
}
}
void reportLockProcessError();
void _l_lockProcess_finished(int, QProcess::ExitStatus);
void _l_lockProcess_errorOccurred(QProcess::ProcessError);
bool isScreenSaverLocked();
QPointer<QProcess> m_lockProcess;
QString lock_command;
};
void ScreenSaverPrivate::reportLockProcessError()
{
QMessageBox box;
box.setIcon(QMessageBox::Warning);
box.setWindowTitle(tr("Screen Saver Error"));
QString message;
// contains() instead of startsWith() as the command might be "env FOO=bar xdg-screensaver lock"
// (e.g., overwrite $XDG_CURRENT_DESKTOP for some different behaviors)
if (lock_command.contains(QL1SV("xdg-screensaver"))) {
message = tr("Failed to run \"%1\". "
"Ensure you have a locker/screensaver compatible with xdg-screensaver installed and running."
);
} else {
message = tr("Failed to run \"%1\". "
"Ensure the specified locker/screensaver is installed and running."
);
}
box.setText(message.arg(lock_command));
box.exec();
}
void ScreenSaverPrivate::_l_lockProcess_finished(int err, QProcess::ExitStatus status)
{
Q_UNUSED(status)
Q_Q(ScreenSaver);
if (err == 0)
Q_EMIT q->activated();
else
{
reportLockProcessError();
}
Q_EMIT q->done();
}
void ScreenSaverPrivate::_l_lockProcess_errorOccurred(QProcess::ProcessError)
{
reportLockProcessError();
}
bool ScreenSaverPrivate::isScreenSaverLocked()
{
if (auto *x11Application = qGuiApp->nativeInterface<QNativeInterface::QX11Application>())
{
XScreenSaverInfo *info = nullptr;
Display *display = x11Application->display();
XID window = DefaultRootWindow(display);
info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(display, window, info);
const int state = info->state;
XFree(info);
if (state == ScreenSaverOn)
return true;
// Ironically, xscreensaver does not conform to the XScreenSaver protocol, so
// info.state == ScreenSaverOff or info.state == ScreenSaverDisabled does not
// necessarily mean that a screensaver is not active, so add a special check
// for xscreensaver.
XAtom lock_atom = XInternAtom(display, "LOCK", false);
std::vector<int> atom_properties;
if (GetIntArrayProperty(window, "_SCREENSAVER_STATUS", &atom_properties) &&
atom_properties.size() > 0)
{
if (atom_properties[0] == static_cast<int>(lock_atom))
return true;
}
return false;
}
else
{
qWarning() << "isScreenSaverLocked() not implemented on Wayland";
return false;
}
}
ScreenSaver::ScreenSaver(QObject * parent)
: QObject(parent),
d_ptr(new ScreenSaverPrivate(this))
{
Q_D(ScreenSaver);
d->m_lockProcess = new QProcess(this);
connect(d->m_lockProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[=](int exitCode, QProcess::ExitStatus exitStatus){ d->_l_lockProcess_finished(exitCode, exitStatus); });
connect(d->m_lockProcess, &QProcess::errorOccurred,
[=](QProcess::ProcessError error) { d->_l_lockProcess_errorOccurred(error); });
}
ScreenSaver::~ScreenSaver()
{
delete d_ptr;
}
QList<QAction*> ScreenSaver::availableActions()
{
QList<QAction*> ret;
QAction * act;
act = new QAction(XdgIcon::fromTheme(QL1SV("system-lock-screen"), QL1SV("lock")), tr("Lock Screen"), this);
connect(act, &QAction::triggered, this, &ScreenSaver::lockScreen);
ret.append(act);
return ret;
}
void ScreenSaver::lockScreen()
{
Q_D(ScreenSaver);
if (!d->isScreenSaverLocked()) {
QStringList args = QProcess::splitCommand(d->lock_command);
if (args.isEmpty()) {
qWarning() << Q_FUNC_INFO << "Empty screen lock_command";
return;
}
const QString program = args.takeFirst();
d->m_lockProcess->start(program, args, QIODevice::ReadWrite);
}
}
} // namespace LXQt
#include "moc_lxqtscreensaver.cpp"
|