Commit c7057625 authored by Marco Martin's avatar Marco Martin
Browse files

share the wayland connection in the plugin

use a singleton to create the wayland connection and the interfaces.
also create the interfaces only on demand
parent 20107c68
set(wayland_plugin_SRCS
logging.cpp
plugin.cpp
waylandintegration.cpp
windoweffects.cpp
windowsystem.cpp
)
......
/*
* Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
* Copyright 2015 Marco Martin <mart@kde.org>
*
* This 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) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include "waylandintegration.h"
#include "logging.h"
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/compositor.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/surface.h>
#include <KWayland/Client/blur.h>
#include <KWayland/Client/contrast.h>
#include <KWayland/Client/region.h>
#include <KWindowSystem/KWindowSystem>
class WaylandIntegrationSingleton
{
public:
WaylandIntegration self;
};
Q_GLOBAL_STATIC(WaylandIntegrationSingleton, privateWaylandIntegrationSelf)
WaylandIntegration::WaylandIntegration()
: QObject(),
m_waylandConnection(nullptr),
m_waylandBlurManager(nullptr),
m_waylandCompositor(nullptr)
{
setupKWaylandIntegration();
}
WaylandIntegration::~WaylandIntegration()
{}
void WaylandIntegration::setupKWaylandIntegration()
{
using namespace KWayland::Client;
m_waylandConnection = ConnectionThread::fromApplication(this);
if (!m_waylandConnection) {
qCWarning(KWAYLAND_KWS) << "Failed getting Wayland connection from QPA";
return;
}
m_registry = new Registry(this);
m_registry->create(m_waylandConnection);
m_waylandCompositor = Compositor::fromApplication(this);
m_registry->setup();
m_waylandConnection->roundtrip();
}
WaylandIntegration *WaylandIntegration::self()
{
return &privateWaylandIntegrationSelf()->self;
}
KWayland::Client::ConnectionThread *WaylandIntegration::waylandConnection() const
{
return m_waylandConnection;
}
KWayland::Client::BlurManager *WaylandIntegration::waylandBlurManager()
{
if (!m_waylandBlurManager) {
const KWayland::Client::Registry::AnnouncedInterface wmInterface = m_registry->interface(KWayland::Client::Registry::Interface::Blur);
m_waylandBlurManager = m_registry->createBlurManager(wmInterface.name, wmInterface.version, this);
connect(m_waylandBlurManager, &KWayland::Client::BlurManager::removed, this,
[this] () {
m_waylandBlurManager->deleteLater();
m_waylandBlurManager = nullptr;
}
);
}
return m_waylandBlurManager;
}
KWayland::Client::ContrastManager *WaylandIntegration::waylandContrastManager()
{
if (!m_waylandContrastManager) {
const KWayland::Client::Registry::AnnouncedInterface wmInterface = m_registry->interface(KWayland::Client::Registry::Interface::Contrast);
m_waylandContrastManager = m_registry->createContrastManager(wmInterface.name, wmInterface.version, this);
connect(m_waylandContrastManager, &KWayland::Client::ContrastManager::removed, this,
[this] () {
m_waylandContrastManager->deleteLater();
m_waylandContrastManager = nullptr;
}
);
}
return m_waylandContrastManager;
}
KWayland::Client::Compositor *WaylandIntegration::waylandCompositor() const
{
return m_waylandCompositor;
}
KWayland::Client::PlasmaWindowManagement *WaylandIntegration::plasmaWindowManagement()
{
using namespace KWayland::Client;
if (!m_wm) {
const Registry::AnnouncedInterface wmInterface = m_registry->interface(Registry::Interface::PlasmaWindowManagement);
m_wm = m_registry->createPlasmaWindowManagement(wmInterface.name, wmInterface.version, this);
connect(m_wm, &PlasmaWindowManagement::windowCreated, this,
[this] (PlasmaWindow *w) {
emit KWindowSystem::self()->windowAdded(w->internalId());
emit KWindowSystem::self()->stackingOrderChanged();
connect(w, &PlasmaWindow::unmapped, this,
[w] {
emit KWindowSystem::self()->windowRemoved(w->internalId());
emit KWindowSystem::self()->stackingOrderChanged();
}
);
}
);
connect(m_wm, &PlasmaWindowManagement::activeWindowChanged, this,
[this] {
if (PlasmaWindow *w = m_wm->activeWindow()) {
emit KWindowSystem::self()->activeWindowChanged(w->internalId());
} else {
emit KWindowSystem::self()->activeWindowChanged(0);
}
}
);
connect(m_wm, &PlasmaWindowManagement::showingDesktopChanged, KWindowSystem::self(), &KWindowSystem::showingDesktopChanged);
qCDebug(KWAYLAND_KWS) << "Plasma Window Management interface bound";
}
return m_wm;
}
KWayland::Client::PlasmaShell *WaylandIntegration::waylandPlasmaShell()
{
if (!m_waylandPlasmaShell) {
const KWayland::Client::Registry::AnnouncedInterface wmInterface = m_registry->interface(KWayland::Client::Registry::Interface::PlasmaShell);
m_waylandPlasmaShell = m_registry->createPlasmaShell(wmInterface.name, wmInterface.version, this);
}
return m_waylandPlasmaShell;
}
/*
* Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
* Copyright 2015 Marco Martin <mart@kde.org>
*
* This 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) version 3, or any
* later version accepted by the membership of KDE e.V. (or its
* successor approved by the membership of KDE e.V.), which shall
* act as a proxy defined in Section 6 of version 3 of the license.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef WAYLANDINTEGRATION_H
#define WAYLANDINTEGRATION_H
#include <KWindowSystem/private/kwindoweffects_p.h>
namespace KWayland
{
namespace Client
{
class BlurManager;
class ContrastManager;
class Compositor;
class ConnectionThread;
class PlasmaWindowManagement;
class PlasmaShell;
class Registry;
}
}
class WaylandIntegration : public QObject
{
public:
explicit WaylandIntegration();
~WaylandIntegration();
void setupKWaylandIntegration();
static WaylandIntegration *self();
KWayland::Client::ConnectionThread *waylandConnection() const;
KWayland::Client::BlurManager *waylandBlurManager();
KWayland::Client::ContrastManager *waylandContrastManager();
KWayland::Client::Compositor *waylandCompositor() const;
KWayland::Client::PlasmaWindowManagement *plasmaWindowManagement();
KWayland::Client::PlasmaShell *waylandPlasmaShell();
private:
KWayland::Client::ConnectionThread *m_waylandConnection;
KWayland::Client::BlurManager *m_waylandBlurManager;
KWayland::Client::ContrastManager *m_waylandContrastManager;
KWayland::Client::Compositor *m_waylandCompositor;
KWayland::Client::PlasmaWindowManagement *m_wm = nullptr;
KWayland::Client::PlasmaShell *m_waylandPlasmaShell = nullptr;
KWayland::Client::Registry *m_registry = nullptr;
};
#endif
......@@ -20,9 +20,14 @@
*/
#include "windoweffects.h"
#include "waylandintegration.h"
#include <QDebug>
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/plasmashell.h>
#include <KWayland/Client/compositor.h>
#include <KWayland/Client/surface.h>
#include <KWayland/Client/blur.h>
......@@ -31,63 +36,20 @@
WindowEffects::WindowEffects()
: QObject(),
KWindowEffectsPrivate(),
m_waylandConnection(nullptr),
m_waylandBlurManager(nullptr),
m_waylandCompositor(nullptr)
KWindowEffectsPrivate()
{
setupKWaylandIntegration();
}
WindowEffects::~WindowEffects()
{}
void WindowEffects::setupKWaylandIntegration()
{
using namespace KWayland::Client;
m_waylandConnection = ConnectionThread::fromApplication(this);
if (!m_waylandConnection) {
return;
}
Registry *registry = new Registry(this);
registry->create(m_waylandConnection);
m_waylandCompositor = Compositor::fromApplication(this);
connect(registry, &Registry::blurAnnounced, this,
[this, registry] (quint32 name, quint32 version) {
m_waylandBlurManager = registry->createBlurManager(name, version, this);
connect(m_waylandBlurManager, &BlurManager::removed, this,
[this] () {
m_waylandBlurManager->deleteLater();
m_waylandBlurManager = nullptr;
}
);
}
);
connect(registry, &Registry::contrastAnnounced, this,
[this, registry] (quint32 name, quint32 version) {
m_waylandContrastManager = registry->createContrastManager(name, version, this);
connect(m_waylandContrastManager, &ContrastManager::removed, this,
[this] () {
m_waylandContrastManager->deleteLater();
m_waylandContrastManager = nullptr;
}
);
}
);
registry->setup();
m_waylandConnection->roundtrip();
}
bool WindowEffects::isEffectAvailable(KWindowEffects::Effect effect)
{
switch (effect) {
case KWindowEffects::BackgroundContrast:
return m_waylandContrastManager != nullptr;
return WaylandIntegration::self()->waylandContrastManager() != nullptr;
case KWindowEffects::BlurBehind:
return m_waylandBlurManager != nullptr;
return WaylandIntegration::self()->waylandBlurManager() != nullptr;
default:
return false;
}
......@@ -133,44 +95,44 @@ void WindowEffects::highlightWindows(WId controller, const QList<WId> &ids)
void WindowEffects::enableBlurBehind(WId window, bool enable, const QRegion &region)
{
if (!m_waylandBlurManager) {
if (!WaylandIntegration::self()->waylandBlurManager()) {
return;
}
KWayland::Client::Surface *surface = KWayland::Client::Surface::fromQtWinId(window);
if (surface) {
if (enable) {
auto blur = m_waylandBlurManager->createBlur(surface, surface);
blur->setRegion(m_waylandCompositor->createRegion(region, nullptr));
auto blur = WaylandIntegration::self()->waylandBlurManager()->createBlur(surface, surface);
blur->setRegion(WaylandIntegration::self()->waylandCompositor()->createRegion(region, nullptr));
blur->commit();
} else {
m_waylandBlurManager->removeBlur(surface);
WaylandIntegration::self()->waylandBlurManager()->removeBlur(surface);
}
surface->commit(KWayland::Client::Surface::CommitFlag::None);
m_waylandConnection->flush();
WaylandIntegration::self()->waylandConnection()->flush();
}
}
void WindowEffects::enableBackgroundContrast(WId window, bool enable, qreal contrast, qreal intensity, qreal saturation, const QRegion &region)
{
if (!m_waylandContrastManager) {
if (!WaylandIntegration::self()->waylandContrastManager()) {
return;
}
KWayland::Client::Surface *surface = KWayland::Client::Surface::fromQtWinId(window);
if (surface) {
if (enable) {
auto backgroundContrast = m_waylandContrastManager->createContrast(surface, surface);
backgroundContrast->setRegion(m_waylandCompositor->createRegion(region, nullptr));
auto backgroundContrast = WaylandIntegration::self()->waylandContrastManager()->createContrast(surface, surface);
backgroundContrast->setRegion(WaylandIntegration::self()->waylandCompositor()->createRegion(region, nullptr));
backgroundContrast->setContrast(contrast);
backgroundContrast->setIntensity(intensity);
backgroundContrast->setSaturation(saturation);
backgroundContrast->commit();
} else {
m_waylandContrastManager->removeContrast(surface);
WaylandIntegration::self()->waylandContrastManager()->removeContrast(surface);
}
surface->commit(KWayland::Client::Surface::CommitFlag::None);
m_waylandConnection->flush();
WaylandIntegration::self()->waylandConnection()->flush();
}
}
......
......@@ -39,7 +39,7 @@ class WindowEffects : public QObject, public KWindowEffectsPrivate
public:
explicit WindowEffects();
~WindowEffects();
void setupKWaylandIntegration();
bool isEffectAvailable(KWindowEffects::Effect effect) override;
void slideWindow(WId id, KWindowEffects::SlideFromLocation location, int offset) override;
void slideWindow(QWidget *widget, KWindowEffects::SlideFromLocation location) override;
......@@ -50,12 +50,6 @@ public:
void enableBlurBehind(WId window, bool enable = true, const QRegion &region = QRegion()) override;
void enableBackgroundContrast(WId window, bool enable = true, qreal contrast = 1, qreal intensity = 1, qreal saturation = 1, const QRegion &region = QRegion()) override;
void markAsDashboard(WId window) override;
private:
KWayland::Client::ConnectionThread *m_waylandConnection;
KWayland::Client::BlurManager *m_waylandBlurManager;
KWayland::Client::ContrastManager *m_waylandContrastManager;
KWayland::Client::Compositor *m_waylandCompositor;
};
#endif
......@@ -18,6 +18,7 @@
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "windowsystem.h"
#include "waylandintegration.h"
#include "logging.h"
#include <KWindowSystem/KWindowSystem>
......@@ -25,6 +26,8 @@
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/plasmashell.h>
#include <KWayland/Client/surface.h>
#include <QPixmap>
#include <QPoint>
......@@ -38,66 +41,14 @@ WindowSystem::WindowSystem()
{
}
void WindowSystem::setupKWaylandIntegration()
{
ConnectionThread *connection = ConnectionThread::fromApplication(this);
if (!connection) {
qCWarning(KWAYLAND_KWS) << "Failed getting Wayland connection from QPA";
return;
}
Registry *registry = new Registry(this);
registry->create(connection);
connect(registry, &Registry::interfacesAnnounced, this,
[this] {
if (!m_wm) {
qCWarning(KWAYLAND_KWS) << "This compositor does not support the Plasma Window Management interface";
}
}
);
connect(registry, &Registry::plasmaWindowManagementAnnounced, this,
[this, registry] (quint32 name, quint32 version) {
m_wm = registry->createPlasmaWindowManagement(name, version, this);
connect(m_wm, &PlasmaWindowManagement::windowCreated, this,
[this] (PlasmaWindow *w) {
emit KWindowSystem::self()->windowAdded(w->internalId());
emit KWindowSystem::self()->stackingOrderChanged();
connect(w, &PlasmaWindow::unmapped, this,
[w] {
emit KWindowSystem::self()->windowRemoved(w->internalId());
emit KWindowSystem::self()->stackingOrderChanged();
}
);
}
);
connect(m_wm, &PlasmaWindowManagement::activeWindowChanged, this,
[this] {
if (PlasmaWindow *w = m_wm->activeWindow()) {
emit KWindowSystem::self()->activeWindowChanged(w->internalId());
} else {
emit KWindowSystem::self()->activeWindowChanged(0);
}
}
);
connect(m_wm, &PlasmaWindowManagement::showingDesktopChanged, KWindowSystem::self(), &KWindowSystem::showingDesktopChanged);
emit KWindowSystem::self()->compositingChanged(true);
emit KWindowSystem::self()->showingDesktopChanged(m_wm->isShowingDesktop());
emit KWindowSystem::self()->stackingOrderChanged();
if (PlasmaWindow *w = m_wm->activeWindow()) {
emit KWindowSystem::self()->activeWindowChanged(w->internalId());
}
qCDebug(KWAYLAND_KWS) << "Plasma Window Management interface bound";
}
);
registry->setup();
}
KWayland::Client::PlasmaWindow *WindowSystem::window(WId wid) const
{
if (!m_wm) {
if (!WaylandIntegration::self()->plasmaWindowManagement()) {
qCWarning(KWAYLAND_KWS) << "This compositor does not support the Plasma Window Management interface";
return nullptr;
}
const auto &windows = m_wm->windows();
const auto &windows = WaylandIntegration::self()->plasmaWindowManagement()->windows();
auto it = std::find_if(windows.begin(), windows.end(), [wid] (PlasmaWindow *w) { return w->internalId() == wid; } );
if (it != windows.end()) {
return *it;
......@@ -123,8 +74,9 @@ void WindowSystem::forceActiveWindow(WId win, long int time)
WId WindowSystem::activeWindow()
{
if (m_wm && m_wm->activeWindow()) {
return m_wm->activeWindow()->internalId();
KWayland::Client::PlasmaWindowManagement *wm = WaylandIntegration::self()->plasmaWindowManagement();
if (wm && wm->activeWindow()) {
return wm->activeWindow()->internalId();
}
return 0;
}
......@@ -330,9 +282,11 @@ void WindowSystem::setOnDesktop(WId win, int desktop)
void WindowSystem::setShowingDesktop(bool showing)
{
if (m_wm) {
m_wm->setShowingDesktop(showing);
if (!WaylandIntegration::self()->plasmaWindowManagement()) {
qCWarning(KWAYLAND_KWS) << "This compositor does not support the Plasma Window Management interface";
return;
}
WaylandIntegration::self()->plasmaWindowManagement()->setShowingDesktop(showing);
}
void WindowSystem::clearState(WId win, NET::States state)
......@@ -351,9 +305,34 @@ void WindowSystem::setState(WId win, NET::States state)
void WindowSystem::setType(WId win, NET::WindowType windowType)
{
Q_UNUSED(win)
Q_UNUSED(windowType)
qCDebug(KWAYLAND_KWS) << "This plugin does not support changing window types";
if (!WaylandIntegration::self()->waylandPlasmaShell()) {
return;
}
KWayland::Client::PlasmaShellSurface::Role role;
switch (windowType) {
case NET::Normal:
role = KWayland::Client::PlasmaShellSurface::Role::Normal;
break;
case NET::Desktop:
role = KWayland::Client::PlasmaShellSurface::Role::Desktop;
break;
case NET::Dock:
role = KWayland::Client::PlasmaShellSurface::Role::Panel;
break;
case NET::OnScreenDisplay:
role = KWayland::Client::PlasmaShellSurface::Role::OnScreenDisplay;
break;
default:
return;
}
Surface *s = Surface::fromQtWinId(win);
if (!s) {
return;
}
KWayland::Client::PlasmaShellSurface *shellSurface = WaylandIntegration::self()->waylandPlasmaShell()->createSurface(s, this);
shellSurface->setRole(role);
}
void WindowSystem::setUserTime(WId win, long int time)
......@@ -365,23 +344,26 @@ void WindowSystem::setUserTime(WId win, long int time)
bool WindowSystem::showingDesktop()
{
if (m_wm) {
return m_wm->isShowingDesktop();
if (!WaylandIntegration::self()->plasmaWindowManagement()) {
qCWarning(KWAYLAND_KWS) << "This compositor does not support the Plasma Window Management interface";
return false;
}
return false;
return WaylandIntegration::self()->plasmaWindowManagement()->isShowingDesktop();
}
QList< WId > WindowSystem::stackingOrder()
{
if (m_wm) {
const auto &windows = m_wm->windows();
QList<WId> ret;
for (auto w : windows) {
ret << w->internalId();
}
return ret;
if (!WaylandIntegration::self()->plasmaWindowManagement()) {
qCWarning(KWAYLAND_KWS) << "This compositor does not support the Plasma Window Management interface";
return QList<WId>();
}
const auto &windows = WaylandIntegration::self()->plasmaWindowManagement()->windows();
QList<WId> ret;
for (auto w : windows) {
ret << w->internalId();
}
return QList<WId>();
return ret;
}
WId WindowSystem::transientFor(WId window)
......
......@@ -30,6 +30,7 @@ namespace Client
{
class PlasmaWindow;
class PlasmaWindowManagement;
class PlasmaShell;
}
}
......@@ -92,7 +93,7 @@ private:
void setupKWaylandIntegration();
KWayland::Client::PlasmaWindow *window(WId window) const;
KWayland::Client::PlasmaWindowManagement *m_wm = nullptr;
KWayland::Client::PlasmaShell *m_waylandPlasmaShell = nullptr;
};
#endif