kwin/toplevel.cpp

476 lines
12 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2006 Lubos Lunak <l.lunak@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 "toplevel.h"
#ifdef KWIN_BUILD_ACTIVITIES
#include "activities.h"
#endif
#include "atoms.h"
#include "client.h"
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
#include "client_machine.h"
#include "effects.h"
#include "screens.h"
#include "shadow.h"
#include "xcbutils.h"
#if HAVE_WAYLAND
#include <KWayland/Server/surface_interface.h>
#endif
#include <QDebug>
namespace KWin
{
Toplevel::Toplevel()
: m_visual(XCB_NONE)
2011-01-30 14:34:42 +00:00
, info(NULL)
, ready_for_painting(true)
2012-11-15 06:48:08 +00:00
, m_isDamaged(false)
, m_client()
2011-01-30 14:34:42 +00:00
, damage_handle(None)
, is_shape(false)
, effect_window(NULL)
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
, m_clientMachine(new ClientMachine(this))
2011-01-30 14:34:42 +00:00
, wmClientLeaderWin(0)
, unredirect(false)
, unredirectSuspend(false)
, m_damageReplyPending(false)
, m_screen(0)
, m_skipCloseAnimation(false)
2011-01-30 14:34:42 +00:00
{
connect(this, SIGNAL(damaged(KWin::Toplevel*,QRect)), SIGNAL(needsRepaint()));
connect(screens(), SIGNAL(changed()), SLOT(checkScreen()));
connect(screens(), SIGNAL(countChanged(int,int)), SLOT(checkScreen()));
setupCheckScreenConnection();
2011-01-30 14:34:42 +00:00
}
Toplevel::~Toplevel()
2011-01-30 14:34:42 +00:00
{
assert(damage_handle == None);
delete info;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QDebug& operator<<(QDebug& stream, const Toplevel* cl)
{
if (cl == NULL)
return stream << "\'NULL\'";
2011-01-30 14:34:42 +00:00
cl->debug(stream);
return stream;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
QDebug& operator<<(QDebug& stream, const ToplevelList& list)
{
stream << "LIST:(";
bool first = true;
2011-01-30 14:34:42 +00:00
for (ToplevelList::ConstIterator it = list.begin();
it != list.end();
++it) {
if (!first)
stream << ":";
first = false;
stream << *it;
2011-01-30 14:34:42 +00:00
}
stream << ")";
return stream;
2011-01-30 14:34:42 +00:00
}
QRect Toplevel::decorationRect() const
{
return rect();
}
2011-01-30 14:34:42 +00:00
void Toplevel::detectShape(Window id)
{
const bool wasShape = is_shape;
is_shape = Xcb::Extensions::self()->hasShape(id);
if (wasShape != is_shape) {
emit shapedChanged();
}
2011-01-30 14:34:42 +00:00
}
// used only by Deleted::copy()
2011-01-30 14:34:42 +00:00
void Toplevel::copyToDeleted(Toplevel* c)
{
geom = c->geom;
m_visual = c->m_visual;
bit_depth = c->bit_depth;
info = c->info;
m_client.reset(c->m_client, false);
ready_for_painting = c->ready_for_painting;
damage_handle = None;
damage_region = c->damage_region;
repaints_region = c->repaints_region;
is_shape = c->is_shape;
effect_window = c->effect_window;
2011-01-30 14:34:42 +00:00
if (effect_window != NULL)
effect_window->setWindow(this);
resource_name = c->resourceName();
resource_class = c->resourceClass();
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
m_clientMachine = c->m_clientMachine;
m_clientMachine->setParent(this);
wmClientLeaderWin = c->wmClientLeader();
opaque_region = c->opaqueRegion();
m_screen = c->m_screen;
m_skipCloseAnimation = c->m_skipCloseAnimation;
2011-01-30 14:34:42 +00:00
}
// before being deleted, remove references to everything that's now
// owner by Deleted
void Toplevel::disownDataPassedToDeleted()
2011-01-30 14:34:42 +00:00
{
info = NULL;
2011-01-30 14:34:42 +00:00
}
QRect Toplevel::visibleRect() const
2011-01-30 14:34:42 +00:00
{
QRect r = decorationRect();
if (hasShadow() && !shadow()->shadowRegion().isEmpty()) {
r |= shadow()->shadowRegion().boundingRect();
}
return r.translated(geometry().topLeft());
2011-01-30 14:34:42 +00:00
}
Xcb::Property Toplevel::fetchWmClientLeader() const
{
return Xcb::Property(false, window(), atoms->wm_client_leader, XCB_ATOM_WINDOW, 0, 10000);
}
void Toplevel::readWmClientLeader(Xcb::Property &prop)
2011-01-30 14:34:42 +00:00
{
wmClientLeaderWin = prop.value<xcb_window_t>(window());
2011-01-30 14:34:42 +00:00
}
void Toplevel::getWmClientLeader()
{
auto prop = fetchWmClientLeader();
readWmClientLeader(prop);
}
/*!
Returns sessionId for this client,
taken either from its window or from the leader window.
*/
QByteArray Toplevel::sessionId() const
2011-01-30 14:34:42 +00:00
{
QByteArray result = Xcb::StringProperty(window(), atoms->sm_client_id);
2011-01-30 14:34:42 +00:00
if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin != window())
result = Xcb::StringProperty(wmClientLeaderWin, atoms->sm_client_id);
return result;
2011-01-30 14:34:42 +00:00
}
/*!
Returns command property for this client,
taken either from its window or from the leader window.
*/
QByteArray Toplevel::wmCommand()
2011-01-30 14:34:42 +00:00
{
QByteArray result = Xcb::StringProperty(window(), XCB_ATOM_WM_COMMAND);
2011-01-30 14:34:42 +00:00
if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin != window())
result = Xcb::StringProperty(wmClientLeaderWin, XCB_ATOM_WM_COMMAND);
result.replace(0, ' ');
return result;
2011-01-30 14:34:42 +00:00
}
void Toplevel::getWmClientMachine()
2011-01-30 14:34:42 +00:00
{
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
m_clientMachine->resolve(window(), wmClientLeader());
2011-01-30 14:34:42 +00:00
}
/*!
Returns client machine for this client,
taken either from its window or from the leader window.
*/
2011-01-30 14:34:42 +00:00
QByteArray Toplevel::wmClientMachine(bool use_localhost) const
{
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
if (!m_clientMachine) {
// this should never happen
return QByteArray();
}
if (use_localhost && m_clientMachine->isLocal()) {
2011-01-30 14:34:42 +00:00
// special name for the local machine (localhost)
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
return ClientMachine::localhost();
}
Improved resolving whether a window is on local machine Most windows use the hostname in WM_CLIENT_MACHINE, but there are windows using the FQDN (for example libreoffice). So instead of "foo" it is "foo.local.net" or similar. The logic so far has been unable to properly determine whether windows with FQDN are on the local system. In order to solve this problem the handling is split out into an own class which stores the information of hostname and whether it is a local machine. This is to not query multiple times. To determine whether the Client is on the local system getaddrinfo is used for the own hostname and the FQDN provided in WM_CLIENT_MACHINE. If one of the queried names matches, we know that it is on the local machine. The old logic to compare the hostname is still used and getaddrinfo is only a fallback in case hostname does not match. The problem with getaddrinfo is, that it accesses the network and by that could block. To circumvent this problem the calls are moved into threads by using QtConcurrent::run. Obviously this brings disadvantages. When trying to resolve whether a Client is on the local machine and a FQDN is used, the information is initially wrong. The new ClientMachine class emits a signal when the information that the system is local becomes available, but for some things this is just too late: * window rules are already gathered * Session Management has already taken place In both cases this is an acceptable loss. For window rules it just needs a proper matching of the machine in case of localhost (remote hosts are not affected). And the case of session management is very academic as it is unlikely that a restoring session contains remote windows. BUG: 308391 FIXED-IN: 4.11 REVIEW: 108235
2013-01-07 07:07:27 +00:00
return m_clientMachine->hostName();
2011-01-30 14:34:42 +00:00
}
/*!
Returns client leader window for this client.
Returns the client window itself if no leader window is defined.
*/
Window Toplevel::wmClientLeader() const
2011-01-30 14:34:42 +00:00
{
if (wmClientLeaderWin)
return wmClientLeaderWin;
return window();
2011-01-30 14:34:42 +00:00
}
void Toplevel::getResourceClass()
2011-01-30 14:34:42 +00:00
{
resource_name = QByteArray(info->windowClassName()).toLower();
resource_class = QByteArray(info->windowClassClass()).toLower();
emit windowClassChanged();
2011-01-30 14:34:42 +00:00
}
double Toplevel::opacity() const
2011-01-30 14:34:42 +00:00
{
if (info->opacity() == 0xffffffff)
return 1.0;
return info->opacity() * 1.0 / 0xffffffff;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void Toplevel::setOpacity(double new_opacity)
{
double old_opacity = opacity();
2011-01-30 14:34:42 +00:00
new_opacity = qBound(0.0, new_opacity, 1.0);
if (old_opacity == new_opacity)
return;
2011-01-30 14:34:42 +00:00
info->setOpacity(static_cast< unsigned long >(new_opacity * 0xffffffff));
if (compositing()) {
addRepaintFull();
2011-03-06 09:30:23 +00:00
emit opacityChanged(this, old_opacity);
}
2011-01-30 14:34:42 +00:00
}
void Toplevel::setReadyForPainting()
{
if (!ready_for_painting) {
ready_for_painting = true;
if (compositing()) {
addRepaintFull();
emit windowShown(this);
if (Client *cl = dynamic_cast<Client*>(this)) {
if (cl->tabGroup() && cl->tabGroup()->current() == cl)
cl->tabGroup()->setCurrent(cl, true);
}
}
}
}
void Toplevel::deleteEffectWindow()
2011-01-30 14:34:42 +00:00
{
delete effect_window;
effect_window = NULL;
2011-01-30 14:34:42 +00:00
}
void Toplevel::checkScreen()
2011-01-30 14:34:42 +00:00
{
if (screens()->count() == 1) {
if (m_screen != 0) {
m_screen = 0;
emit screenChanged();
}
return;
}
const int s = screens()->number(geometry().center());
if (s != m_screen) {
m_screen = s;
emit screenChanged();
}
}
void Toplevel::setupCheckScreenConnection()
{
connect(this, SIGNAL(geometryShapeChanged(KWin::Toplevel*,QRect)), SLOT(checkScreen()));
connect(this, SIGNAL(geometryChanged()), SLOT(checkScreen()));
checkScreen();
}
void Toplevel::removeCheckScreenConnection()
{
disconnect(this, SIGNAL(geometryShapeChanged(KWin::Toplevel*,QRect)), this, SLOT(checkScreen()));
disconnect(this, SIGNAL(geometryChanged()), this, SLOT(checkScreen()));
}
int Toplevel::screen() const
{
return m_screen;
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
bool Toplevel::isOnScreen(int screen) const
{
return screens()->geometry(screen).intersects(geometry());
}
bool Toplevel::isOnActiveScreen() const
{
return isOnScreen(screens()->current());
2011-01-30 14:34:42 +00:00
}
void Toplevel::getShadow()
{
QRect dirtyRect; // old & new shadow region
const QRect oldVisibleRect = visibleRect();
if (hasShadow()) {
dirtyRect = shadow()->shadowRegion().boundingRect();
effectWindow()->sceneWindow()->shadow()->updateShadow();
} else {
Shadow::createShadow(this);
}
if (hasShadow())
dirtyRect |= shadow()->shadowRegion().boundingRect();
if (oldVisibleRect != visibleRect())
emit paddingChanged(this, oldVisibleRect);
if (dirtyRect.isValid()) {
dirtyRect.translate(pos());
addLayerRepaint(dirtyRect);
}
}
bool Toplevel::hasShadow() const
{
if (effectWindow() && effectWindow()->sceneWindow()) {
return effectWindow()->sceneWindow()->shadow() != NULL;
}
return false;
}
Shadow *Toplevel::shadow()
{
if (effectWindow() && effectWindow()->sceneWindow()) {
return effectWindow()->sceneWindow()->shadow();
} else {
return NULL;
}
}
const Shadow *Toplevel::shadow() const
{
if (effectWindow() && effectWindow()->sceneWindow()) {
return effectWindow()->sceneWindow()->shadow();
} else {
return NULL;
}
}
bool Toplevel::wantsShadowToBeRendered() const
{
return true;
}
void Toplevel::getWmOpaqueRegion()
{
const auto rects = info->opaqueRegion();
QRegion new_opaque_region;
for (const auto &r : rects) {
new_opaque_region += QRect(r.pos.x, r.pos.y, r.size.width, r.size.height);
}
opaque_region = new_opaque_region;
}
bool Toplevel::isClient() const
{
return false;
}
bool Toplevel::isDeleted() const
{
return false;
}
bool Toplevel::isOnCurrentActivity() const
{
#ifdef KWIN_BUILD_ACTIVITIES
return isOnActivity(Activities::self()->current());
#else
return true;
#endif
}
void Toplevel::elevate(bool elevate)
{
if (!effectWindow()) {
return;
}
effectWindow()->elevate(elevate);
addWorkspaceRepaint(visibleRect());
}
pid_t Toplevel::pid() const
{
return info->pid();
}
xcb_window_t Toplevel::frameId() const
{
return m_client;
}
Xcb::Property Toplevel::fetchSkipCloseAnimation() const
{
return Xcb::Property(false, window(), atoms->kde_skip_close_animation, XCB_ATOM_CARDINAL, 0, 1);
}
void Toplevel::readSkipCloseAnimation(Xcb::Property &property)
{
setSkipCloseAnimation(property.toBool());
}
void Toplevel::getSkipCloseAnimation()
{
Xcb::Property property = fetchSkipCloseAnimation();
readSkipCloseAnimation(property);
}
bool Toplevel::skipsCloseAnimation() const
{
return m_skipCloseAnimation;
}
void Toplevel::setSkipCloseAnimation(bool set)
{
if (set == m_skipCloseAnimation) {
return;
}
m_skipCloseAnimation = set;
emit skipCloseAnimationChanged();
}
#if HAVE_WAYLAND
void Toplevel::setSurface(KWayland::Server::SurfaceInterface *surface)
{
if (m_surface == surface) {
return;
}
using namespace KWayland::Server;
if (m_surface) {
disconnect(m_surface, &SurfaceInterface::damaged, this, &Toplevel::addDamage);
}
m_surface = surface;
connect(m_surface, &SurfaceInterface::damaged, this, &Toplevel::addDamage);
}
#endif
void Toplevel::addDamage(const QRegion &damage)
{
m_isDamaged = true;
damage_region += damage;
repaints_region += damage;
for (const QRect &r : damage.rects()) {
emit damaged(this, r);
}
}
} // namespace
#include "toplevel.moc"