2020-02-17 18:39:17 +00:00
|
|
|
/*
|
2020-08-02 22:22:19 +00:00
|
|
|
SPDX-FileCopyrightText: 2015 Martin Flöser <mgraesslin@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
|
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2020-02-17 18:39:17 +00:00
|
|
|
|
|
|
|
#include "waylandclient.h"
|
|
|
|
#include "screens.h"
|
|
|
|
#include "wayland_server.h"
|
|
|
|
#include "workspace.h"
|
|
|
|
|
|
|
|
#include <KWaylandServer/display.h>
|
2021-07-20 19:37:03 +00:00
|
|
|
#include <KWaylandServer/clientbuffer.h>
|
2020-02-17 18:39:17 +00:00
|
|
|
#include <KWaylandServer/clientconnection.h>
|
|
|
|
#include <KWaylandServer/surface_interface.h>
|
|
|
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
|
|
|
#include <csignal>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
using namespace KWaylandServer;
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
2020-08-18 12:51:20 +00:00
|
|
|
enum WaylandGeometryType {
|
|
|
|
WaylandGeometryClient = 0x1,
|
|
|
|
WaylandGeometryFrame = 0x2,
|
|
|
|
WaylandGeometryBuffer = 0x4,
|
|
|
|
};
|
|
|
|
Q_DECLARE_FLAGS(WaylandGeometryTypes, WaylandGeometryType)
|
|
|
|
|
2020-02-17 18:39:17 +00:00
|
|
|
WaylandClient::WaylandClient(SurfaceInterface *surface)
|
|
|
|
{
|
|
|
|
setSurface(surface);
|
2020-09-25 06:57:28 +00:00
|
|
|
setupCompositing();
|
2020-02-17 18:39:17 +00:00
|
|
|
|
2020-09-25 06:57:28 +00:00
|
|
|
connect(surface, &SurfaceInterface::shadowChanged,
|
|
|
|
this, &WaylandClient::updateShadow);
|
2020-02-17 18:39:17 +00:00
|
|
|
connect(this, &WaylandClient::frameGeometryChanged,
|
|
|
|
this, &WaylandClient::updateClientOutputs);
|
|
|
|
connect(this, &WaylandClient::desktopFileNameChanged,
|
|
|
|
this, &WaylandClient::updateIcon);
|
|
|
|
connect(screens(), &Screens::changed, this,
|
|
|
|
&WaylandClient::updateClientOutputs);
|
2021-05-12 18:56:16 +00:00
|
|
|
connect(surface->client(), &ClientConnection::aboutToBeDestroyed,
|
|
|
|
this, &WaylandClient::destroyClient);
|
2020-02-17 18:39:17 +00:00
|
|
|
|
|
|
|
updateResourceName();
|
|
|
|
updateIcon();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WaylandClient::captionNormal() const
|
|
|
|
{
|
|
|
|
return m_captionNormal;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WaylandClient::captionSuffix() const
|
|
|
|
{
|
|
|
|
return m_captionSuffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
QRect WaylandClient::transparentRect() const
|
|
|
|
{
|
|
|
|
return QRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t WaylandClient::pid() const
|
|
|
|
{
|
|
|
|
return surface()->client()->processId();
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:06:17 +00:00
|
|
|
bool WaylandClient::isClient() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:39:17 +00:00
|
|
|
bool WaylandClient::isLockScreen() const
|
|
|
|
{
|
|
|
|
return surface()->client() == waylandServer()->screenLockerClientConnection();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandClient::isLocalhost() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AbstractClient *WaylandClient::findModal(bool allow_itself)
|
|
|
|
{
|
|
|
|
Q_UNUSED(allow_itself)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-04-25 08:52:30 +00:00
|
|
|
void WaylandClient::resizeWithChecks(const QSize &size)
|
2020-02-17 18:39:17 +00:00
|
|
|
{
|
|
|
|
const QRect area = workspace()->clientArea(WorkArea, this);
|
|
|
|
|
|
|
|
int width = size.width();
|
|
|
|
int height = size.height();
|
|
|
|
|
|
|
|
// don't allow growing larger than workarea
|
|
|
|
if (width > area.width()) {
|
|
|
|
width = area.width();
|
|
|
|
}
|
|
|
|
if (height > area.height()) {
|
|
|
|
height = area.height();
|
|
|
|
}
|
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
|
|
|
resize(QSize(width, height));
|
2020-02-17 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::killWindow()
|
|
|
|
{
|
|
|
|
if (!surface()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto c = surface()->client();
|
|
|
|
if (c->processId() == getpid() || c->processId() == 0) {
|
|
|
|
c->destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
::kill(c->processId(), SIGTERM);
|
|
|
|
// give it time to terminate and only if terminate fails, try destroy Wayland connection
|
|
|
|
QTimer::singleShot(5000, c, &ClientConnection::destroy);
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray WaylandClient::windowRole() const
|
|
|
|
{
|
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandClient::belongsToSameApplication(const AbstractClient *other, SameApplicationChecks checks) const
|
|
|
|
{
|
|
|
|
if (checks.testFlag(SameApplicationCheck::AllowCrossProcesses)) {
|
|
|
|
if (other->desktopFileName() == desktopFileName()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (auto s = other->surface()) {
|
|
|
|
return s->client() == surface()->client();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandClient::belongsToDesktop() const
|
|
|
|
{
|
|
|
|
const auto clients = waylandServer()->clients();
|
|
|
|
|
|
|
|
return std::any_of(clients.constBegin(), clients.constEnd(),
|
|
|
|
[this](const AbstractClient *client) {
|
|
|
|
if (belongsToSameApplication(client, SameApplicationChecks())) {
|
|
|
|
return client->isDesktop();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::updateClientOutputs()
|
|
|
|
{
|
2021-06-25 08:58:28 +00:00
|
|
|
surface()->setOutputs(waylandServer()->display()->outputsIntersecting(frameGeometry()));
|
2020-02-17 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::updateIcon()
|
|
|
|
{
|
|
|
|
const QString waylandIconName = QStringLiteral("wayland");
|
|
|
|
const QString dfIconName = iconFromDesktopFile();
|
|
|
|
const QString iconName = dfIconName.isEmpty() ? waylandIconName : dfIconName;
|
|
|
|
if (iconName == icon().name()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setIcon(QIcon::fromTheme(iconName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::updateResourceName()
|
|
|
|
{
|
|
|
|
const QFileInfo fileInfo(surface()->client()->executablePath());
|
|
|
|
if (fileInfo.exists()) {
|
2021-04-19 07:50:41 +00:00
|
|
|
const QByteArray executableFileName = fileInfo.fileName().toUtf8();
|
|
|
|
setResourceClass(executableFileName, executableFileName);
|
2020-02-17 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::updateCaption()
|
|
|
|
{
|
|
|
|
const QString oldSuffix = m_captionSuffix;
|
|
|
|
const auto shortcut = shortcutCaptionSuffix();
|
|
|
|
m_captionSuffix = shortcut;
|
|
|
|
if ((!isSpecialWindow() || isToolbar()) && findClientWithSameCaption()) {
|
|
|
|
int i = 2;
|
|
|
|
do {
|
|
|
|
m_captionSuffix = shortcut + QLatin1String(" <") + QString::number(i) + QLatin1Char('>');
|
|
|
|
i++;
|
|
|
|
} while (findClientWithSameCaption());
|
|
|
|
}
|
|
|
|
if (m_captionSuffix != oldSuffix) {
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT captionChanged();
|
2020-02-17 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::setCaption(const QString &caption)
|
|
|
|
{
|
|
|
|
const QString oldSuffix = m_captionSuffix;
|
|
|
|
m_captionNormal = caption.simplified();
|
|
|
|
updateCaption();
|
|
|
|
if (m_captionSuffix == oldSuffix) {
|
|
|
|
// Don't emit caption change twice it already got emitted by the changing suffix.
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT captionChanged();
|
2020-02-17 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::doSetActive()
|
|
|
|
{
|
|
|
|
if (isActive()) { // TODO: Xwayland clients must be unfocused somewhere else.
|
|
|
|
StackingUpdatesBlocker blocker(workspace());
|
|
|
|
workspace()->focusToNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:39:47 +00:00
|
|
|
void WaylandClient::updateDepth()
|
|
|
|
{
|
2020-10-02 15:35:33 +00:00
|
|
|
if (surface()->buffer()->hasAlphaChannel()) {
|
2020-07-29 16:39:47 +00:00
|
|
|
setDepth(32);
|
|
|
|
} else {
|
|
|
|
setDepth(24);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 08:01:33 +00:00
|
|
|
void WaylandClient::cleanGrouping()
|
|
|
|
{
|
|
|
|
if (transientFor()) {
|
|
|
|
transientFor()->removeTransient(this);
|
|
|
|
}
|
|
|
|
for (auto it = transients().constBegin(); it != transients().constEnd();) {
|
|
|
|
if ((*it)->transientFor() == this) {
|
|
|
|
removeTransient(*it);
|
|
|
|
it = transients().constBegin(); // restart, just in case something more has changed with the list
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 08:24:18 +00:00
|
|
|
bool WaylandClient::isShown(bool shaded_is_shown) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(shaded_is_shown)
|
|
|
|
return !isZombie() && !isHidden() && !isMinimized();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandClient::isHiddenInternal() const
|
|
|
|
{
|
|
|
|
return isHidden();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::hideClient(bool hide)
|
|
|
|
{
|
|
|
|
if (hide) {
|
|
|
|
internalHide();
|
|
|
|
} else {
|
|
|
|
internalShow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WaylandClient::isHidden() const
|
|
|
|
{
|
|
|
|
return m_isHidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::internalShow()
|
|
|
|
{
|
|
|
|
if (!isHidden()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_isHidden = false;
|
|
|
|
addRepaintFull();
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT windowShown(this);
|
2020-08-17 08:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::internalHide()
|
|
|
|
{
|
|
|
|
if (isHidden()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-04-30 18:06:58 +00:00
|
|
|
if (isInteractiveMoveResize()) {
|
|
|
|
leaveInteractiveMoveResize();
|
2020-08-17 08:24:18 +00:00
|
|
|
}
|
|
|
|
m_isHidden = true;
|
2021-02-05 12:46:22 +00:00
|
|
|
addWorkspaceRepaint(visibleGeometry());
|
2020-08-17 08:24:18 +00:00
|
|
|
workspace()->clientHidden(this);
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT windowHidden(this);
|
2020-08-17 08:24:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 12:51:20 +00:00
|
|
|
QRect WaylandClient::frameRectToBufferRect(const QRect &rect) const
|
|
|
|
{
|
|
|
|
return QRect(rect.topLeft(), surface()->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaylandClient::updateGeometry(const QRect &rect)
|
|
|
|
{
|
|
|
|
const QRect oldClientGeometry = m_clientGeometry;
|
|
|
|
const QRect oldFrameGeometry = m_frameGeometry;
|
|
|
|
const QRect oldBufferGeometry = m_bufferGeometry;
|
|
|
|
|
|
|
|
m_clientGeometry = frameRectToClientRect(rect);
|
|
|
|
m_frameGeometry = rect;
|
|
|
|
m_bufferGeometry = frameRectToBufferRect(rect);
|
|
|
|
|
|
|
|
WaylandGeometryTypes changedGeometries;
|
|
|
|
|
|
|
|
if (m_clientGeometry != oldClientGeometry) {
|
|
|
|
changedGeometries |= WaylandGeometryClient;
|
|
|
|
}
|
|
|
|
if (m_frameGeometry != oldFrameGeometry) {
|
|
|
|
changedGeometries |= WaylandGeometryFrame;
|
|
|
|
}
|
|
|
|
if (m_bufferGeometry != oldBufferGeometry) {
|
|
|
|
changedGeometries |= WaylandGeometryBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changedGeometries) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateWindowRules(Rules::Position | Rules::Size);
|
|
|
|
|
|
|
|
if (changedGeometries & WaylandGeometryBuffer) {
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT bufferGeometryChanged(this, oldBufferGeometry);
|
2020-08-18 12:51:20 +00:00
|
|
|
}
|
|
|
|
if (changedGeometries & WaylandGeometryClient) {
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT clientGeometryChanged(this, oldClientGeometry);
|
2020-08-18 12:51:20 +00:00
|
|
|
}
|
|
|
|
if (changedGeometries & WaylandGeometryFrame) {
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT frameGeometryChanged(this, oldFrameGeometry);
|
2020-08-18 12:51:20 +00:00
|
|
|
}
|
2021-06-08 07:02:14 +00:00
|
|
|
Q_EMIT geometryShapeChanged(this, oldFrameGeometry);
|
2020-08-18 12:51:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 18:39:17 +00:00
|
|
|
} // namespace KWin
|