2020-08-07 19:01:42 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "layershellv1integration.h"
|
|
|
|
#include "abstract_wayland_output.h"
|
|
|
|
#include "layershellv1client.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "screens.h"
|
|
|
|
#include "wayland_server.h"
|
|
|
|
#include "workspace.h"
|
|
|
|
|
|
|
|
#include <KWaylandServer/display.h>
|
|
|
|
#include <KWaylandServer/layershell_v1_interface.h>
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
using namespace KWaylandServer;
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
static const Qt::Edges AnchorHorizontal = Qt::LeftEdge | Qt::RightEdge;
|
|
|
|
static const Qt::Edges AnchorVertical = Qt::TopEdge | Qt::BottomEdge;
|
|
|
|
|
|
|
|
LayerShellV1Integration::LayerShellV1Integration(QObject *parent)
|
|
|
|
: WaylandShellIntegration(parent)
|
|
|
|
{
|
2020-12-09 21:24:41 +00:00
|
|
|
LayerShellV1Interface *shell = new LayerShellV1Interface(waylandServer()->display(), this);
|
2020-08-07 19:01:42 +00:00
|
|
|
connect(shell, &KWaylandServer::LayerShellV1Interface::surfaceCreated,
|
|
|
|
this, &LayerShellV1Integration::createClient);
|
|
|
|
|
|
|
|
m_rearrangeTimer = new QTimer(this);
|
|
|
|
m_rearrangeTimer->setSingleShot(true);
|
|
|
|
connect(m_rearrangeTimer, &QTimer::timeout, this, &LayerShellV1Integration::rearrange);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerShellV1Integration::createClient(LayerSurfaceV1Interface *shellSurface)
|
|
|
|
{
|
|
|
|
AbstractOutput *output = waylandServer()->findOutput(shellSurface->output());
|
|
|
|
if (!output) {
|
2021-08-28 18:58:29 +00:00
|
|
|
output = workspace()->activeOutput();
|
2020-08-07 19:01:42 +00:00
|
|
|
}
|
|
|
|
if (!output) {
|
|
|
|
qCWarning(KWIN_CORE) << "Could not find any suitable output for a layer surface";
|
|
|
|
shellSurface->sendClosed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT clientCreated(new LayerShellV1Client(shellSurface, output, this));
|
2020-08-07 19:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerShellV1Integration::recreateClient(LayerSurfaceV1Interface *shellSurface)
|
|
|
|
{
|
|
|
|
destroyClient(shellSurface);
|
|
|
|
createClient(shellSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerShellV1Integration::destroyClient(LayerSurfaceV1Interface *shellSurface)
|
|
|
|
{
|
|
|
|
const QList<AbstractClient *> clients = waylandServer()->clients();
|
|
|
|
for (AbstractClient *client : clients) {
|
|
|
|
LayerShellV1Client *layerShellClient = qobject_cast<LayerShellV1Client *>(client);
|
|
|
|
if (layerShellClient && layerShellClient->shellSurface() == shellSurface) {
|
|
|
|
layerShellClient->destroyClient();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void adjustWorkArea(const LayerSurfaceV1Interface *shellSurface, QRect *workArea)
|
|
|
|
{
|
2020-09-09 08:19:59 +00:00
|
|
|
switch (shellSurface->exclusiveEdge()) {
|
|
|
|
case Qt::LeftEdge:
|
2020-08-07 19:01:42 +00:00
|
|
|
workArea->adjust(shellSurface->leftMargin() + shellSurface->exclusiveZone(), 0, 0, 0);
|
2020-09-09 08:19:59 +00:00
|
|
|
break;
|
|
|
|
case Qt::RightEdge:
|
2020-08-07 19:01:42 +00:00
|
|
|
workArea->adjust(0, 0, -shellSurface->rightMargin() - shellSurface->exclusiveZone(), 0);
|
2020-09-09 08:19:59 +00:00
|
|
|
break;
|
|
|
|
case Qt::TopEdge:
|
2020-08-07 19:01:42 +00:00
|
|
|
workArea->adjust(0, shellSurface->topMargin() + shellSurface->exclusiveZone(), 0, 0);
|
2020-09-09 08:19:59 +00:00
|
|
|
break;
|
|
|
|
case Qt::BottomEdge:
|
2020-08-07 19:01:42 +00:00
|
|
|
workArea->adjust(0, 0, 0, -shellSurface->bottomMargin() - shellSurface->exclusiveZone());
|
2020-09-09 08:19:59 +00:00
|
|
|
break;
|
2020-08-07 19:01:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rearrangeLayer(const QList<LayerShellV1Client *> &clients, QRect *workArea,
|
|
|
|
LayerSurfaceV1Interface::Layer layer, bool exclusive)
|
|
|
|
{
|
|
|
|
for (LayerShellV1Client *client : clients) {
|
|
|
|
LayerSurfaceV1Interface *shellSurface = client->shellSurface();
|
|
|
|
|
|
|
|
if (shellSurface->layer() != layer) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-09-09 14:05:34 +00:00
|
|
|
if (exclusive != (shellSurface->exclusiveZone() > 0)) {
|
2020-08-07 19:01:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect bounds;
|
|
|
|
if (shellSurface->exclusiveZone() == -1) {
|
2021-08-25 10:47:44 +00:00
|
|
|
bounds = client->desiredOutput()->geometry();
|
2020-08-07 19:01:42 +00:00
|
|
|
} else {
|
|
|
|
bounds = *workArea;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect geometry(QPoint(0, 0), shellSurface->desiredSize());
|
|
|
|
|
|
|
|
if ((shellSurface->anchor() & AnchorHorizontal) && geometry.width() == 0) {
|
|
|
|
geometry.setLeft(bounds.left());
|
|
|
|
geometry.setWidth(bounds.width());
|
|
|
|
} else if (shellSurface->anchor() & Qt::LeftEdge) {
|
|
|
|
geometry.moveLeft(bounds.left());
|
|
|
|
} else if (shellSurface->anchor() & Qt::RightEdge) {
|
|
|
|
geometry.moveRight(bounds.right());
|
|
|
|
} else {
|
|
|
|
geometry.moveLeft(bounds.left() + (bounds.width() - geometry.width()) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((shellSurface->anchor() & AnchorVertical) && geometry.height() == 0) {
|
|
|
|
geometry.setTop(bounds.top());
|
|
|
|
geometry.setHeight(bounds.height());
|
|
|
|
} else if (shellSurface->anchor() & Qt::TopEdge) {
|
|
|
|
geometry.moveTop(bounds.top());
|
|
|
|
} else if (shellSurface->anchor() & Qt::BottomEdge) {
|
|
|
|
geometry.moveBottom(bounds.bottom());
|
|
|
|
} else {
|
|
|
|
geometry.moveTop(bounds.top() + (bounds.height() - geometry.height()) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((shellSurface->anchor() & AnchorHorizontal) == AnchorHorizontal) {
|
|
|
|
geometry.adjust(shellSurface->leftMargin(), 0, -shellSurface->rightMargin(), 0);
|
|
|
|
} else if (shellSurface->anchor() & Qt::LeftEdge) {
|
|
|
|
geometry.translate(shellSurface->leftMargin(), 0);
|
|
|
|
} else if (shellSurface->anchor() & Qt::RightEdge) {
|
|
|
|
geometry.translate(-shellSurface->rightMargin(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((shellSurface->anchor() & AnchorVertical) == AnchorVertical) {
|
|
|
|
geometry.adjust(0, shellSurface->topMargin(), 0, -shellSurface->bottomMargin());
|
|
|
|
} else if (shellSurface->anchor() & Qt::TopEdge) {
|
|
|
|
geometry.translate(0, shellSurface->topMargin());
|
|
|
|
} else if (shellSurface->anchor() & Qt::BottomEdge) {
|
|
|
|
geometry.translate(0, -shellSurface->bottomMargin());
|
|
|
|
}
|
|
|
|
|
2021-04-09 14:44:44 +00:00
|
|
|
// Move the window's bottom if its virtual keyboard is overlapping it
|
|
|
|
if (shellSurface->exclusiveZone() >= 0 && !client->virtualKeyboardGeometry().isEmpty() && geometry.bottom() > client->virtualKeyboardGeometry().top()) {
|
|
|
|
geometry.setBottom(client->virtualKeyboardGeometry().top());
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:01:42 +00:00
|
|
|
if (geometry.isValid()) {
|
Rework async geometry updates
Window management features were written with synchronous geometry
updates in mind. Currently, this poses a big problem on Wayland because
geometry updates are done in asynchronous fashion there.
At the moment, geometry is updated in a so called pseudo-asynchronous
fashion, meaning that the frame geometry will be reset to the old value
once geometry updates are unblocked. The main drawback of this approach
is that it is too error prone, the data flow is hard to comprehend, etc.
It is worth noting that there is already a machinery to perform async
geometry which is used during interactive move/resize operations.
This change extends the move/resize geometry usage beyond interactive
move/resize to make asynchronous geometry updates less error prone and
easier to comprehend.
With the proposed solution, all geometry updates must be done on the
move/resize geometry first. After that, the new geometry is passed on to
the Client-specific implementation of moveResizeInternal().
To be more specific, the frameGeometry() returns the current frame
geometry, it is primarily useful only to the scene. If you want to move
or resize a window, you need to use moveResizeGeometry() because it
corresponds to the last requested frame geometry.
It is worth noting that the moveResizeGeometry() returns the desired
bounding geometry. The client may commit the xdg_toplevel surface with a
slightly smaller window geometry, for example to enforce a specific
aspect ratio. The client is not allowed to resize beyond the size as
indicated in moveResizeGeometry().
The data flow is very simple: moveResize() updates the move/resize
geometry and calls the client-specific implementation of the
moveResizeInternal() method. Based on whether a configure event is
needed, moveResizeInternal() will update the frameGeometry() either
immediately or after the client commits a new buffer.
Unfortunately, both the compositor and xdg-shell clients try to update
the window geometry. It means that it's possible to have conflicts
between the two. With this change, the compositor's move resize geometry
will be synced only if there are no pending configure events, meaning
that the user doesn't try to resize the window.
2021-04-30 18:26:09 +00:00
|
|
|
client->moveResize(geometry);
|
2020-08-07 19:01:42 +00:00
|
|
|
} else {
|
|
|
|
qCWarning(KWIN_CORE) << "Closing a layer shell client due to invalid geometry";
|
|
|
|
client->closeWindow();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exclusive && shellSurface->exclusiveZone() > 0) {
|
|
|
|
adjustWorkArea(shellSurface, workArea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static QList<LayerShellV1Client *> clientsForOutput(AbstractOutput *output)
|
|
|
|
{
|
|
|
|
QList<LayerShellV1Client *> result;
|
|
|
|
const QList<AbstractClient *> clients = waylandServer()->clients();
|
|
|
|
for (AbstractClient *client : clients) {
|
|
|
|
LayerShellV1Client *layerShellClient = qobject_cast<LayerShellV1Client *>(client);
|
2021-08-25 10:47:44 +00:00
|
|
|
if (!layerShellClient || layerShellClient->desiredOutput() != output) {
|
2020-08-07 19:01:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (layerShellClient->shellSurface()->isCommitted()) {
|
|
|
|
result.append(layerShellClient);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rearrangeOutput(AbstractOutput *output)
|
|
|
|
{
|
|
|
|
const QList<LayerShellV1Client *> clients = clientsForOutput(output);
|
|
|
|
if (!clients.isEmpty()) {
|
|
|
|
QRect workArea = output->geometry();
|
|
|
|
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::OverlayLayer, true);
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::TopLayer, true);
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::BottomLayer, true);
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::BackgroundLayer, true);
|
|
|
|
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::OverlayLayer, false);
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::TopLayer, false);
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::BottomLayer, false);
|
|
|
|
rearrangeLayer(clients, &workArea, LayerSurfaceV1Interface::BackgroundLayer, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LayerShellV1Integration::rearrange()
|
|
|
|
{
|
|
|
|
m_rearrangeTimer->stop();
|
|
|
|
|
2021-01-22 08:13:35 +00:00
|
|
|
const QVector<AbstractOutput *> outputs = kwinApp()->platform()->enabledOutputs();
|
2020-08-07 19:01:42 +00:00
|
|
|
for (AbstractOutput *output : outputs) {
|
|
|
|
rearrangeOutput(output);
|
|
|
|
}
|
|
|
|
|
Prevent layershell from crashing when rearranging when we shouldn't
Here's the backtrace that prompted the MR:
```
0 QVector<KWin::VirtualDesktop*>::count() const (this=0x10) at
/home/apol/devel/kde5/include/QtCore/qvector.h:241
1 KWin::VirtualDesktopManager::count() const (this=0x0) at
/home/apol/devel/frameworks/kwin/src/virtualdesktops.h:687
2 KWin::Workspace::updateClientArea(bool) (this=0x0, force=false) at
/home/apol/devel/frameworks/kwin/src/workspace.cpp:2089
3 0x00007fef12a180b3 in KWin::LayerShellV1Integration::rearrange()
(this=<optimized out>) at
/home/apol/devel/frameworks/kwin/src/layershellv1integration.cpp:208
4 0x00007fef13094806 in QtPrivate::QSlotObjectBase::call(QObject*,
void**) (a=0x7ffcf9674f70, r=0x5569d2981a40, this=0x5569d2981e50) at
../../include/QtCore/../../../../../devel/frameworks/qt5/qtbase/src/corelib/kernel/qobjectdefs_impl.h:398
5 doActivate<false>(QObject*, int, void**) (sender=0x5569d2981dc0,
signal_index=3, argv=argv@entry=0x7ffcf9674f70) at
/home/apol/devel/frameworks/qt5/qtbase/src/corelib/kernel/qobject.cpp:3886
6 0x00007fef1308db60 in QMetaObject::activate(QObject*, QMetaObject
const*, int, void**) (sender=<optimized out>, m=m@entry=0x7fef1332d280
<QTimer::staticMetaObject>,
local_signal_index=local_signal_index@entry=0,
argv=argv@entry=0x7ffcf9674f70) at
/home/apol/devel/frameworks/qt5/qtbase/src/corelib/kernel/qobject.cpp:3946
7 0x00007fef1309871a in QTimer::timeout(QTimer::QPrivateSignal)
(this=<optimized out>, _t1=...) at .moc/moc_qtimer.cpp:205
```
2021-02-17 17:02:17 +00:00
|
|
|
if (workspace()) {
|
|
|
|
workspace()->updateClientArea();
|
|
|
|
}
|
2020-08-07 19:01:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LayerShellV1Integration::scheduleRearrange()
|
|
|
|
{
|
|
|
|
m_rearrangeTimer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KWin
|