From d2a3a10802a5be6e7816c36123fea3bcb2306de8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Mon, 15 Aug 2016 08:16:33 +0200 Subject: [PATCH] Split ScreenLockerWatcher into dedicated source files Being able to monitor whether the screen is locked is useful not only to the effects system but overall in KWin. Thus to make it possible to use it from more locations as a first step it's moved into dedicated source files. --- CMakeLists.txt | 1 + effects.cpp | 101 +-------------------------------- effects.h | 24 -------- screenlockerwatcher.cpp | 121 ++++++++++++++++++++++++++++++++++++++++ screenlockerwatcher.h | 56 +++++++++++++++++++ 5 files changed, 179 insertions(+), 124 deletions(-) create mode 100644 screenlockerwatcher.cpp create mode 100644 screenlockerwatcher.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 72523181ef..e89dd86049 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -383,6 +383,7 @@ set(kwin_KDEINIT_SRCS scene_xrender.cpp scene_opengl.cpp scene_qpainter.cpp + screenlockerwatcher.cpp thumbnailitem.cpp lanczosfilter.cpp deleted.cpp diff --git a/effects.cpp b/effects.cpp index 2fe0efd4e6..4d0ecc5e22 100644 --- a/effects.cpp +++ b/effects.cpp @@ -40,16 +40,13 @@ along with this program. If not, see . #include "screenedge.h" #include "scripting/scriptedeffect.h" #include "screens.h" +#include "screenlockerwatcher.h" #include "thumbnailitem.h" #include "virtualdesktops.h" #include "workspace.h" #include "kwinglutils.h" #include -#include -#include -#include -#include #include @@ -63,104 +60,8 @@ along with this program. If not, see . #include "decorations/decorationbridge.h" #include -// dbus generated -#include "screenlocker_interface.h" - - namespace KWin { - -static const QString SCREEN_LOCKER_SERVICE_NAME = QStringLiteral("org.freedesktop.ScreenSaver"); - -ScreenLockerWatcher::ScreenLockerWatcher(QObject *parent) - : QObject(parent) - , m_interface(NULL) - , m_serviceWatcher(new QDBusServiceWatcher(this)) - , m_locked(false) -{ - connect(m_serviceWatcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), SLOT(serviceOwnerChanged(QString,QString,QString))); - m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange); - m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME); - // check whether service is registered - QFutureWatcher > *watcher = new QFutureWatcher >(this); - connect(watcher, SIGNAL(finished()), SLOT(serviceRegisteredQueried())); - connect(watcher, SIGNAL(canceled()), watcher, SLOT(deleteLater())); - watcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), - &QDBusConnectionInterface::isServiceRegistered, - SCREEN_LOCKER_SERVICE_NAME)); -} - -ScreenLockerWatcher::~ScreenLockerWatcher() -{ -} - -void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner) -{ - Q_UNUSED(oldOwner) - if (serviceName != SCREEN_LOCKER_SERVICE_NAME) { - return; - } - delete m_interface; - m_interface = NULL; - m_locked = false; - if (!newOwner.isEmpty()) { - m_interface = new OrgFreedesktopScreenSaverInterface(newOwner, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); - connect(m_interface, SIGNAL(ActiveChanged(bool)), SLOT(setLocked(bool))); - QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this); - connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(activeQueried(QDBusPendingCallWatcher*))); - } -} - -void ScreenLockerWatcher::serviceRegisteredQueried() -{ - QFutureWatcher > *watcher = dynamic_cast > *>(sender()); - if (!watcher) { - return; - } - const QDBusReply &reply = watcher->result(); - if (reply.isValid() && reply.value()) { - QFutureWatcher > *ownerWatcher = new QFutureWatcher >(this); - connect(ownerWatcher, SIGNAL(finished()), SLOT(serviceOwnerQueried())); - connect(ownerWatcher, SIGNAL(canceled()), ownerWatcher, SLOT(deleteLater())); - ownerWatcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), - &QDBusConnectionInterface::serviceOwner, - SCREEN_LOCKER_SERVICE_NAME)); - } - watcher->deleteLater(); -} - -void ScreenLockerWatcher::serviceOwnerQueried() -{ - QFutureWatcher > *watcher = dynamic_cast > *>(sender()); - if (!watcher) { - return; - } - const QDBusReply reply = watcher->result(); - if (reply.isValid()) { - serviceOwnerChanged(SCREEN_LOCKER_SERVICE_NAME, QString(), reply.value()); - } - - watcher->deleteLater(); -} - -void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher) -{ - QDBusPendingReply reply = *watcher; - if (!reply.isError()) { - setLocked(reply.value()); - } - watcher->deleteLater(); -} - -void ScreenLockerWatcher::setLocked(bool activated) -{ - if (m_locked == activated) { - return; - } - m_locked = activated; - emit locked(m_locked); -} - //--------------------- // Static diff --git a/effects.h b/effects.h index f7bd4ba1a0..4e8a5a9d82 100644 --- a/effects.h +++ b/effects.h @@ -45,7 +45,6 @@ class Display; class QDBusPendingCallWatcher; class QDBusServiceWatcher; -class OrgFreedesktopScreenSaverInterface; namespace KWin @@ -454,29 +453,6 @@ private: Plasma::Theme *m_theme; }; -class ScreenLockerWatcher : public QObject -{ - Q_OBJECT -public: - explicit ScreenLockerWatcher(QObject *parent = 0); - virtual ~ScreenLockerWatcher(); - bool isLocked() const { - return m_locked; - } -Q_SIGNALS: - void locked(bool locked); -private Q_SLOTS: - void setLocked(bool activated); - void activeQueried(QDBusPendingCallWatcher *watcher); - void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner); - void serviceRegisteredQueried(); - void serviceOwnerQueried(); -private: - OrgFreedesktopScreenSaverInterface *m_interface; - QDBusServiceWatcher *m_serviceWatcher; - bool m_locked; -}; - inline QList EffectsHandlerImpl::elevatedWindows() const { diff --git a/screenlockerwatcher.cpp b/screenlockerwatcher.cpp new file mode 100644 index 0000000000..5534c5ae9d --- /dev/null +++ b/screenlockerwatcher.cpp @@ -0,0 +1,121 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2013 Martin Gräßlin + +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 . +*********************************************************************/ +#include "screenlockerwatcher.h" + +#include +#include +// dbus generated +#include "screenlocker_interface.h" + +namespace KWin +{ + +static const QString SCREEN_LOCKER_SERVICE_NAME = QStringLiteral("org.freedesktop.ScreenSaver"); + +ScreenLockerWatcher::ScreenLockerWatcher(QObject *parent) + : QObject(parent) + , m_interface(NULL) + , m_serviceWatcher(new QDBusServiceWatcher(this)) + , m_locked(false) +{ + connect(m_serviceWatcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)), SLOT(serviceOwnerChanged(QString,QString,QString))); + m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange); + m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME); + // check whether service is registered + QFutureWatcher > *watcher = new QFutureWatcher >(this); + connect(watcher, SIGNAL(finished()), SLOT(serviceRegisteredQueried())); + connect(watcher, SIGNAL(canceled()), watcher, SLOT(deleteLater())); + watcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), + &QDBusConnectionInterface::isServiceRegistered, + SCREEN_LOCKER_SERVICE_NAME)); +} + +ScreenLockerWatcher::~ScreenLockerWatcher() +{ +} + +void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner) +{ + Q_UNUSED(oldOwner) + if (serviceName != SCREEN_LOCKER_SERVICE_NAME) { + return; + } + delete m_interface; + m_interface = NULL; + m_locked = false; + if (!newOwner.isEmpty()) { + m_interface = new OrgFreedesktopScreenSaverInterface(newOwner, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this); + connect(m_interface, SIGNAL(ActiveChanged(bool)), SLOT(setLocked(bool))); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this); + connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), SLOT(activeQueried(QDBusPendingCallWatcher*))); + } +} + +void ScreenLockerWatcher::serviceRegisteredQueried() +{ + QFutureWatcher > *watcher = dynamic_cast > *>(sender()); + if (!watcher) { + return; + } + const QDBusReply &reply = watcher->result(); + if (reply.isValid() && reply.value()) { + QFutureWatcher > *ownerWatcher = new QFutureWatcher >(this); + connect(ownerWatcher, SIGNAL(finished()), SLOT(serviceOwnerQueried())); + connect(ownerWatcher, SIGNAL(canceled()), ownerWatcher, SLOT(deleteLater())); + ownerWatcher->setFuture(QtConcurrent::run(QDBusConnection::sessionBus().interface(), + &QDBusConnectionInterface::serviceOwner, + SCREEN_LOCKER_SERVICE_NAME)); + } + watcher->deleteLater(); +} + +void ScreenLockerWatcher::serviceOwnerQueried() +{ + QFutureWatcher > *watcher = dynamic_cast > *>(sender()); + if (!watcher) { + return; + } + const QDBusReply reply = watcher->result(); + if (reply.isValid()) { + serviceOwnerChanged(SCREEN_LOCKER_SERVICE_NAME, QString(), reply.value()); + } + + watcher->deleteLater(); +} + +void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher) +{ + QDBusPendingReply reply = *watcher; + if (!reply.isError()) { + setLocked(reply.value()); + } + watcher->deleteLater(); +} + +void ScreenLockerWatcher::setLocked(bool activated) +{ + if (m_locked == activated) { + return; + } + m_locked = activated; + emit locked(m_locked); +} + +} diff --git a/screenlockerwatcher.h b/screenlockerwatcher.h new file mode 100644 index 0000000000..ed6b79635e --- /dev/null +++ b/screenlockerwatcher.h @@ -0,0 +1,56 @@ +/******************************************************************** + KWin - the KDE window manager + This file is part of the KDE project. + +Copyright (C) 2013 Martin Gräßlin + +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 . +*********************************************************************/ +#ifndef KWIN_SCREENLOCKERWATCHER_H +#define KWIN_SCREENLOCKERWATCHER_H + +#include + +class OrgFreedesktopScreenSaverInterface; +class QDBusServiceWatcher; +class QDBusPendingCallWatcher; + +namespace KWin +{ + +class ScreenLockerWatcher : public QObject +{ + Q_OBJECT +public: + explicit ScreenLockerWatcher(QObject *parent = 0); + virtual ~ScreenLockerWatcher(); + bool isLocked() const { + return m_locked; + } +Q_SIGNALS: + void locked(bool locked); +private Q_SLOTS: + void setLocked(bool activated); + void activeQueried(QDBusPendingCallWatcher *watcher); + void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner); + void serviceRegisteredQueried(); + void serviceOwnerQueried(); +private: + OrgFreedesktopScreenSaverInterface *m_interface; + QDBusServiceWatcher *m_serviceWatcher; + bool m_locked; +}; +} + +#endif