ksldapp.cpp 22.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/********************************************************************
 KSld - the KDE Screenlocker Daemon
 This file is part of the KDE project.

 Copyright 1999 Martin R. Jones <mjones@kde.org>
 Copyright 2003 Oswald Buddenhagen <ossi@kde.org>
 Copyright 2008 Chani Armitage <chanika@gmail.com>
 Copyright (C) 2011 Martin Gräßlin <mgraesslin@kde.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "ksldapp.h"
#include "interface.h"
25
#include "globalaccel.h"
26
#include "x11locker.h"
27
#include "waylandlocker.h"
28
#include "logind.h"
29
#include "kscreensaversettings.h"
30
#include "powermanagement_inhibition.h"
31
#include <config-kscreenlocker.h>
32
#include <config-X11.h>
33
#include "waylandserver.h"
34
35
36
37
// KDE
#include <KAuthorized>
#include <KIdleTime>
#include <KLocalizedString>
38
#include <KNotification>
39
#include <KGlobalAccel>
40
41
42
43
44

//kwayland
#include <KWayland/Server/display.h>
#include <KWayland/Server/clientconnection.h>

45
46
// Qt
#include <QAction>
47
#include <QKeyEvent>
48
49
50
51
52
#include <QTimer>
#include <QProcess>
#include <QX11Info>
// X11
#include <X11/Xlib.h>
53
#include <xcb/xcb.h>
54
55
56
#ifdef X11_Xinput_FOUND
#include <X11/extensions/XInput2.h>
#endif
57
58
// other
#include <unistd.h>
59
#include <signal.h>
60

61
62
63
#include <sys/types.h>
#include <sys/socket.h>

64
65
66
namespace ScreenLocker
{

67
68
static const QString s_qtQuickBackend = QStringLiteral("QT_QUICK_BACKEND");

Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
69
static KSldApp * s_instance = nullptr;
70
71
72
73
74
75
76
77
78
79
80
81
82

KSldApp* KSldApp::self()
{
    if (!s_instance) {
        s_instance = new KSldApp();
    }

    return s_instance;
}

KSldApp::KSldApp(QObject * parent)
    : QObject(parent)
    , m_lockState(Unlocked)
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
83
84
    , m_lockProcess(nullptr)
    , m_lockWindow(nullptr)
85
    , m_waylandServer(new WaylandServer(this))
Martin Flöser's avatar
Martin Flöser committed
86
    , m_waylandDisplay(nullptr)
87
88
89
90
91
92
    , m_lockedTimer(QElapsedTimer())
    , m_idleId(0)
    , m_lockGrace(0)
    , m_inGraceTime(false)
    , m_graceTimer(new QTimer(this))
    , m_inhibitCounter(0)
93
    , m_logind(nullptr)
94
    , m_greeterEnv(QProcessEnvironment::systemEnvironment())
95
    , m_powerManagementInhibition(new PowerManagementInhibition(this))
96
{
97
    m_isX11 = QX11Info::isPlatformX11();
98
    m_isWayland = QCoreApplication::instance()->property("platformName").toString().startsWith( QLatin1String("wayland"), Qt::CaseInsensitive);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}

KSldApp::~KSldApp()
{
}

static int s_XTimeout;
static int s_XInterval;
static int s_XBlanking;
static int s_XExposures;

void KSldApp::cleanUp()
{
    if (m_lockProcess && m_lockProcess->state() != QProcess::NotRunning) {
        m_lockProcess->terminate();
    }
    delete m_lockProcess;
    delete m_lockWindow;

    // Restore X screensaver parameters
    XSetScreenSaver(QX11Info::display(), s_XTimeout, s_XInterval, s_XBlanking, s_XExposures);
}

122
static bool s_graceTimeKill = false;
123
124
static bool s_logindExit = false;

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
static bool hasXInput()
{
#ifdef X11_Xinput_FOUND
    Display *dpy = QX11Info::display();
    int xi_opcode, event, error;
    // init XInput extension
    if (!XQueryExtension(dpy, "XInputExtension", &xi_opcode, &event, &error)) {
        return false;
    }

    // verify that the XInput extension is at at least version 2.0
    int major = 2, minor = 0;
    int result = XIQueryVersion(dpy, &major, &minor);
    if (result == BadImplementation) {
        // Xinput 2.2 returns BadImplementation if checked against 2.0
        major = 2;
        minor = 2;
        return XIQueryVersion(dpy, &major, &minor) == Success;
    }
    return result == Success;
#else
    return false;
#endif
}

150
void KSldApp::initializeX11()
151
{
152
    m_hasXInput2 = hasXInput();
153
154
155
156
157
158
159
160
    // Save X screensaver parameters
    XGetScreenSaver(QX11Info::display(), &s_XTimeout, &s_XInterval, &s_XBlanking, &s_XExposures);
    // And disable it. The internal X screensaver is not used at all, but we use its
    // internal idle timer (and it is also used by DPMS support in X). This timer must not
    // be altered by this code, since e.g. resetting the counter after activating our
    // screensaver would prevent DPMS from activating. We use the timer merely to detect
    // user activity.
    XSetScreenSaver(QX11Info::display(), 0, s_XInterval, s_XBlanking, s_XExposures);
161
162
163
164
165
166
167
}

void KSldApp::initialize()
{
    if (m_isX11) {
        initializeX11();
    }
168
169

    // Global keys
170
    if (KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) {
171
        qDebug() << "Configuring Lock Action";
172
173
174
        QAction *a = new QAction(this);
        a->setObjectName(QStringLiteral("Lock Session"));
        a->setProperty("componentName", QStringLiteral("ksmserver"));
175
        a->setText(i18n("Lock Session"));
176
        KGlobalAccel::self()->setGlobalShortcut(a, QList<QKeySequence>() << Qt::META+Qt::Key_L << Qt::ALT+Qt::CTRL+Qt::Key_L << Qt::Key_ScreenSaver );
177
178
        connect(a, &QAction::triggered, this,
            [this]() {
179
                lock(EstablishLock::Immediate);
180
181
            }
        );
182
183
184
    }

    // idle support
185
186
187
188
189
190
191
192
193
194
    auto idleTimeSignal = static_cast<void (KIdleTime:: *)(int)>(&KIdleTime::timeoutReached);
    connect(KIdleTime::instance(), idleTimeSignal, this,
        [this](int identifier) {
            if (identifier != m_idleId) {
                // not our identifier
                return;
            }
            if (lockState() != Unlocked) {
                return;
            }
195
196
            if (m_inhibitCounter // either we got a direct inhibit request thru our outdated o.f.Screensaver iface ...
                    || isFdoPowerInhibited()) { // ... or the newer one at o.f.PowerManagement.Inhibit
197
198
199
200
201
202
203
204
205
                // there is at least one process blocking the auto lock of screen locker
                return;
            }
            if (m_lockGrace) {  // short-circuit if grace time is zero
                m_inGraceTime = true;
            } else if (m_lockGrace == -1) {
                m_inGraceTime = true;  // if no timeout configured, grace time lasts forever
            }

206
            lock(EstablishLock::Delayed);
207
208
        }
    );
209
210

    m_lockProcess = new QProcess();
211
    m_lockProcess->setProcessChannelMode(QProcess::ForwardedErrorChannel);
212
    m_lockProcess->setReadChannel(QProcess::StandardOutput);
213
214
215
    auto finishedSignal = static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished);
    connect(m_lockProcess, finishedSignal, this,
        [this](int exitCode, QProcess::ExitStatus exitStatus) {
216
217
218
            if (m_isWayland && m_waylandDisplay && m_greeterClientConnection) {
                m_greeterClientConnection->destroy();
            }
219
220
221
222
223
224
225
226
            if ((!exitCode && exitStatus == QProcess::NormalExit) || s_graceTimeKill || s_logindExit) {
                // unlock process finished successfully - we can remove the lock grab
                s_graceTimeKill = false;
                s_logindExit = false;
                doUnlock();
                return;
            }
            // failure, restart lock process
227
228
            m_greeterCrashedCounter++;
            if (m_greeterCrashedCounter < 4) {
229
230
                // Perhaps it crashed due to a graphics driver issue, force software rendering now
                setForceSoftwareRendering(true);
231
232
233
234
                startLockProcess(EstablishLock::Immediate);
            } else if (m_lockWindow) {
                m_lockWindow->emergencyShow();
            }
235
236
        }
    );
Laurent Montel's avatar
Laurent Montel committed
237
    connect(m_lockProcess, &QProcess::errorOccurred, this,
238
239
240
241
242
243
244
245
        [this](QProcess::ProcessError error) {
            if (error == QProcess::FailedToStart) {
                doUnlock();
                m_waylandServer->stop();
                qCritical() << "Greeter Process not available";
            }
        }
    );
246
247
    m_lockedTimer.invalidate();
    m_graceTimer->setSingleShot(true);
248
    connect(m_graceTimer, &QTimer::timeout, this, &KSldApp::endGraceTime);
249
250
251
    // create our D-Bus interface
    new Interface(this);

252
    // connect to logind
253
254
    m_logind = new LogindIntegration(this);
    connect(m_logind, &LogindIntegration::requestLock, this,
255
        [this]() {
256
            lock(EstablishLock::Immediate);
257
258
        }
    );
259
    connect(m_logind, &LogindIntegration::requestUnlock, this,
260
        [this]() {
261
            if (lockState() == Locked || lockState() == AcquiringLock) {
262
263
                if (m_lockProcess->state() != QProcess::NotRunning) {
                    s_logindExit = true;
264
                    m_lockProcess->terminate();
265
266
267
                } else {
                    doUnlock();
                }
268
269
270
            }
        }
    );
271
    connect(m_logind, &LogindIntegration::prepareForSleep, this,
272
273
274
275
276
        [this](bool goingToSleep) {
            if (!goingToSleep) {
                // not interested in doing anything on wakeup
                return;
            }
277
278
279
            if (KScreenSaverSettings::lockOnResume()) {
                lock(EstablishLock::Immediate);
            }
280
281
        }
    );
282
283
    connect(m_logind, &LogindIntegration::inhibited, this,
        [this]() {
284
285
            // if we are already locked, we immediatelly remove the inhibition lock
            if (m_lockState == KSldApp::Locked) {
286
                m_logind->uninhibit();
287
288
289
            }
        }
    );
290
291
292
293
    connect(m_logind, &LogindIntegration::connectedChanged, this,
        [this]() {
            if (m_logind->isConnected() && m_lockState == ScreenLocker::KSldApp::Unlocked && KScreenSaverSettings::lockOnResume()) {
                m_logind->inhibit();
294
295
296
297
            }
        }
    );
    connect(this, &KSldApp::locked, this,
298
299
        [this]() {
            m_logind->uninhibit();
300
301
302
            if (m_lockGrace > 0 && m_inGraceTime) {
                m_graceTimer->start(m_lockGrace);
            }
303
304
305
        }
    );
    connect(this, &KSldApp::unlocked, this,
306
307
308
309
        [this]() {
            if (KScreenSaverSettings::lockOnResume()) {
                m_logind->inhibit();
            }
310
311
        }
    );
312

313
314
315
316
    m_globalAccel = new GlobalAccel(this);
    connect(this, &KSldApp::locked, m_globalAccel, &GlobalAccel::prepare);
    connect(this, &KSldApp::unlocked, m_globalAccel, &GlobalAccel::release);

317
318
319
320
321
322
323
324
325
    // fallback for non-logind systems:
    // connect to signal emitted by Solid. This is emitted unconditionally also on logind enabled systems
    // ksld ignores it in case logind is used
    QDBusConnection::sessionBus().connect(QStringLiteral("org.kde.Solid.PowerManagement"),
                                          QStringLiteral("/org/kde/Solid/PowerManagement/Actions/SuspendSession"),
                                          QStringLiteral("org.kde.Solid.PowerManagement.Actions.SuspendSession"),
                                          QStringLiteral("aboutToSuspend"),
                                          this, SLOT(solidSuspend()));

326
327
328
329
330
    configure();
}

void KSldApp::configure()
{
Laurent Montel's avatar
Laurent Montel committed
331
    KScreenSaverSettings::self()->load();
332
333
334
335
336
337
338
    // idle support
    if (m_idleId) {
        KIdleTime::instance()->removeIdleTimeout(m_idleId);
        m_idleId = 0;
    }
    const int timeout = KScreenSaverSettings::timeout();
    // screen saver enabled means there is an auto lock timer
339
340
    // timeout > 0 is for backwards compatibility with old configs
    if (KScreenSaverSettings::autolock() && timeout > 0) {
341
342
        // timeout stored in minutes
        m_idleId = KIdleTime::instance()->addIdleTimeout(timeout*1000*60);
343
344
    }
    if (KScreenSaverSettings::lock()) {
345
346
        // lockGrace is stored in seconds
        m_lockGrace = KScreenSaverSettings::lockGrace() * 1000;
347
348
349
    } else {
        m_lockGrace = -1;
    }
350
351
352
353
354
355
356
    if (m_logind && m_logind->isConnected()) {
        if (KScreenSaverSettings::lockOnResume() && !m_logind->isInhibited()) {
            m_logind->inhibit();
        } else if (!KScreenSaverSettings::lockOnResume() && m_logind->isInhibited()) {
            m_logind->uninhibit();
        }
    }
357
358
}

359
void KSldApp::lock(EstablishLock establishLock, int attemptCount)
360
361
362
363
364
{
    if (lockState() != Unlocked) {
        // already locked or acquiring lock, no need to lock again
        // but make sure it's really locked
        endGraceTime();
365
        if (establishLock == EstablishLock::Immediate) {
366
367
368
            // signal the greeter to switch to immediateLock mode
            kill(m_lockProcess->pid(), SIGUSR1);
        }
369
370
371
        return;
    }

372
373
374
375
    if (attemptCount == 0) {
        emit aboutToLock();
    }

376
    qDebug() << "lock called";
377
    if (!establishGrab()) {
378
379
380
381
382
383
384
385
        if (attemptCount < 3) {
            qWarning() << "Could not establish screen lock. Trying again in 10ms";
            QTimer::singleShot(10, this, [=]() {
                lock(establishLock, attemptCount+1);
            });
        } else {
            qCritical() << "Could not establish screen lock";
        }
386
387
388
        return;
    }

389
390
391
392
393
394
    KNotification::event(QStringLiteral("locked"),
                         i18n("Screen locked"),
                         QPixmap(),
                         nullptr,
                         KNotification::CloseOnTimeout,
                         QStringLiteral("ksmserver"));
395
396
397
398
399
400

    // blank the screen
    showLockWindow();

    m_lockState = AcquiringLock;

401
    setForceSoftwareRendering(false);
402
    // start unlock screen process
403
    startLockProcess(establishLock);
404
    emit lockStateChanged();
405
406
}

407
408
409
410
411
412
413
/*
 * Forward declarations:
 * Only called from KSldApp::establishGrab(). Using from somewhere else is incorrect usage!
 **/
static bool grabKeyboard();
static bool grabMouse();

414
415
416
417
418
419
420
421
422
423
424
425
class XServerGrabber
{
public:
    XServerGrabber() {
        xcb_grab_server(QX11Info::connection());
    }
    ~XServerGrabber() {
        xcb_ungrab_server(QX11Info::connection());
        xcb_flush(QX11Info::connection());
    }
};

426
427
bool KSldApp::establishGrab()
{
428
429
430
    if (!m_isX11) {
        return true;
    }
431
    XSync(QX11Info::display(), False);
432
    XServerGrabber serverGrabber;
433
    if (!grabKeyboard()) {
434
        return false;
435
436
437
    }

    if (!grabMouse()) {
438
        XUngrabKeyboard(QX11Info::display(), CurrentTime);
439
        XFlush(QX11Info::display());
440
        return false;
441
442
    }

443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#ifdef X11_Xinput_FOUND
    if (m_hasXInput2) {
        // get all devices
        Display *dpy = QX11Info::display();
        int numMasters;
        XIDeviceInfo *masters = XIQueryDevice(dpy, XIAllMasterDevices, &numMasters);
        bool success = true;
        for (int i = 0; i < numMasters; ++i) {
            // ignoring core pointer and core keyboard as we already grabbed them
            if (qstrcmp(masters[i].name, "Virtual core pointer") == 0) {
                continue;
            }
            if (qstrcmp(masters[i].name, "Virtual core keyboard") == 0) {
                continue;
            }
            XIEventMask mask;
            uchar bitmask[] = { 0, 0 };
            mask.deviceid = masters[i].deviceid;
            mask.mask = bitmask;
            mask.mask_len = sizeof(bitmask);
            XISetMask(bitmask, XI_ButtonPress);
            XISetMask(bitmask, XI_ButtonRelease);
            XISetMask(bitmask, XI_Motion);
            XISetMask(bitmask, XI_Enter);
            XISetMask(bitmask, XI_Leave);
            const int result = XIGrabDevice(dpy, masters[i].deviceid, QX11Info::appRootWindow(),
                                            XCB_TIME_CURRENT_TIME, XCB_CURSOR_NONE, XIGrabModeAsync,
                                            XIGrabModeAsync, True, &mask);
            if (result != XIGrabSuccess) {
                success = false;
                break;
            }
        }
        if (!success) {
            // ungrab all devices again
            for (int i = 0; i < numMasters; ++i) {
                XIUngrabDevice(dpy, masters[i].deviceid, XCB_TIME_CURRENT_TIME);
            }
            xcb_connection_t *c = QX11Info::connection();
            xcb_ungrab_keyboard(c, XCB_CURRENT_TIME);
            xcb_ungrab_pointer(c, XCB_CURRENT_TIME);
        }
        XIFreeDeviceInfo(masters);
486
        XFlush(dpy);
487
488
489
490
        return success;
    }
#endif

491
492
493
    return true;
}

494
static bool grabKeyboard()
495
{
496
    int rv = XGrabKeyboard( QX11Info::display(), QX11Info::appRootWindow(),
497
498
499
500
501
        True, GrabModeAsync, GrabModeAsync, CurrentTime );

    return (rv == GrabSuccess);
}

502
static bool grabMouse()
503
504
505
{
#define GRABEVENTS ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
                   EnterWindowMask | LeaveWindowMask
506
    int rv = XGrabPointer( QX11Info::display(), QX11Info::appRootWindow(),
507
508
509
510
511
512
513
514
515
            True, GRABEVENTS, GrabModeAsync, GrabModeAsync, None,
            None, CurrentTime );
#undef GRABEVENTS

    return (rv == GrabSuccess);
}

void KSldApp::doUnlock()
{
516
    qDebug() << "Grab Released";
517
518
519
520
521
    if (m_isX11) {
        xcb_connection_t *c = QX11Info::connection();
        xcb_ungrab_keyboard(c, XCB_CURRENT_TIME);
        xcb_ungrab_pointer(c, XCB_CURRENT_TIME);
        xcb_flush(c);
522
#ifdef X11_Xinput_FOUND
523
524
525
526
527
528
529
530
531
532
533
        if (m_hasXInput2) {
            // get all devices
            Display *dpy = QX11Info::display();
            int numMasters;
            XIDeviceInfo *masters = XIQueryDevice(dpy, XIAllMasterDevices, &numMasters);
            // ungrab all devices again
            for (int i = 0; i < numMasters; ++i) {
                XIUngrabDevice(dpy, masters[i].deviceid, XCB_TIME_CURRENT_TIME);
            }
            XIFreeDeviceInfo(masters);
            XFlush(dpy);
534
535
        }
#endif
536
    }
537
538
539
    hideLockWindow();
    // delete the window again, to get rid of event filter
    delete m_lockWindow;
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
540
    m_lockWindow = nullptr;
541
542
    m_lockState = Unlocked;
    m_lockedTimer.invalidate();
543
    m_greeterCrashedCounter = 0;
544
    endGraceTime();
545
    m_waylandServer->stop();
546
547
548
549
550
551
    KNotification::event(QStringLiteral("unlocked"),
                         i18n("Screen unlocked"),
                         QPixmap(),
                         nullptr,
                         KNotification::CloseOnTimeout,
                         QStringLiteral("ksmserver"));
552
    emit unlocked();
553
    emit lockStateChanged();
554
555
556
557
}

bool KSldApp::isFdoPowerInhibited() const
{
558
    return m_powerManagementInhibition->isInhibited();
559
560
}

561
void KSldApp::startLockProcess(EstablishLock establishLock)
562
{
563
564
    QProcessEnvironment env = m_greeterEnv;

565
566
567
568
    if (m_isWayland && m_waylandDisplay) {
        int sx[2];
        if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sx) < 0) {
            qWarning() << "Can not create socket";
Laurent Montel's avatar
Laurent Montel committed
569
            emit m_lockProcess->errorOccurred(QProcess::FailedToStart);
570
571
572
            return;
        }
        m_greeterClientConnection = m_waylandDisplay->createClient(sx[0]);
573
        connect(m_greeterClientConnection, &QObject::destroyed, this,
574
575
576
577
            [this] (QObject *destroyedObject) {
                if (destroyedObject != m_greeterClientConnection) {
                    return;
                }
578
579
580
581
                m_greeterClientConnection = nullptr;
                emit greeterClientConnectionChanged();
            }
        );
582
        emit greeterClientConnectionChanged();
583
584
        int socket = dup(sx[1]);
        if (socket >= 0) {
585
            env.insert(QStringLiteral("WAYLAND_SOCKET"), QString::number(socket));
586
587
        }
    }
588
    QStringList args;
589
    if (establishLock == EstablishLock::Immediate) {
590
        args << QStringLiteral("--immediateLock");
591
    }
592
593
594
595
596
    if (establishLock == EstablishLock::DefaultToSwitchUser) {
        args << QStringLiteral("--immediateLock");
        args << QStringLiteral("--switchuser");
    }

597
    if (m_lockGrace > 0) {
598
        args << QStringLiteral("--graceTime");
599
        args << QString::number(m_lockGrace);
600
601
    }
    if (m_lockGrace == -1) {
602
        args << QStringLiteral("--nolock");
603
    }
604
605
606
    if (m_forceSoftwareRendering) {
        env.insert(s_qtQuickBackend, QStringLiteral("software"));
    }
607
608
609
610

    // start the Wayland server
    int fd = m_waylandServer->start();
    if (fd == -1) {
Laurent Montel's avatar
Laurent Montel committed
611
        emit m_lockProcess->errorOccurred(QProcess::FailedToStart);
612
        return;
613
614
    }

615
    args << QStringLiteral("--ksldfd");
616
617
    args << QString::number(fd);

618
    m_lockProcess->setProcessEnvironment(env);
619
    m_lockProcess->start(QStringLiteral(KSCREENLOCKER_GREET_BIN), args);
620
    close(fd);
621
622
623
624
625
}

void KSldApp::showLockWindow()
{
    if (!m_lockWindow) {
626
        if (m_isX11) {
627
            m_lockWindow = new X11Locker(this);
628
629
        }
        if (m_isWayland) {
630
            m_lockWindow = new WaylandLocker(m_waylandDisplay, this);
631
632
633
634
        }
        if (!m_lockWindow) {
            return;
        }
635
        m_lockWindow->setGlobalAccel(m_globalAccel);
636
        connect(m_lockWindow, &AbstractLocker::userActivity, m_lockWindow,
637
638
639
640
641
642
643
            [this]() {
                if (isGraceTime()) {
                    unlock();
                }
            },
            Qt::QueuedConnection
        );
644
645
646
647
648
        connect(m_lockWindow, &AbstractLocker::lockWindowShown, m_lockWindow,
            [this] {
                lockScreenShown();
            }
        , Qt::QueuedConnection);
649
        connect(m_waylandServer, &WaylandServer::x11WindowAdded, m_lockWindow, &AbstractLocker::addAllowedWindow);
650
651
    }
    m_lockWindow->showLockWindow();
652
653
654
    if (m_isX11) {
        XSync(QX11Info::display(), False);
    }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
}

void KSldApp::hideLockWindow()
{
    if (!m_lockWindow) {
        return;
    }
    m_lockWindow->hideLockWindow();
}

uint KSldApp::activeTime() const
{
    if (m_lockedTimer.isValid()) {
        return m_lockedTimer.elapsed();
    }
    return 0;
}

bool KSldApp::isGraceTime() const
{
    return m_inGraceTime;
}

void KSldApp::endGraceTime()
{
    m_graceTimer->stop();
    m_inGraceTime = false;
}

void KSldApp::unlock()
{
    if (!isGraceTime()) {
        return;
    }
    s_graceTimeKill = true;
690
    m_lockProcess->terminate();
691
692
693
694
695
696
697
698
699
700
701
702
}

void KSldApp::inhibit()
{
    ++m_inhibitCounter;
}

void KSldApp::uninhibit()
{
    --m_inhibitCounter;
}

703
704
705
706
707
708
709
710
711
712
713
void KSldApp::solidSuspend()
{
    // ignore in case that we use logind
    if (m_logind && m_logind->isConnected()) {
        return;
    }
    if (KScreenSaverSettings::lockOnResume()) {
        lock(EstablishLock::Immediate);
    }
}

714
715
716
717
718
719
720
void KSldApp::setWaylandDisplay(KWayland::Server::Display *display)
{
    if (m_waylandDisplay != display) {
        m_waylandDisplay = display;
    }
}

721
722
void KSldApp::lockScreenShown()
{
723
724
725
    if (m_lockState == Locked) {
        return;
    }
726
727
728
    m_lockState = Locked;
    m_lockedTimer.restart();
    emit locked();
729
    emit lockStateChanged();
730
731
}

732
733
734
void KSldApp::setGreeterEnvironment(const QProcessEnvironment &env)
{
    m_greeterEnv = env;
735
736
737
    if (m_isWayland) {
        m_greeterEnv.insert(QStringLiteral("QT_QPA_PLATFORM"), QStringLiteral("wayland"));
    }
738
739
}

740
741
742
743
744
745
746
747
748
749
bool KSldApp::event(QEvent *event)
{
    if (m_globalAccel && event->type() == QEvent::KeyPress) {
        if (m_globalAccel->keyEvent(static_cast<QKeyEvent*>(event))) {
            event->setAccepted(true);
        }
    }
    return false;
}

750
} // namespace