unmanaged.cpp 6.57 KB
Newer Older
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
1
2
3
/*
    KWin - the KDE window manager
    This file is part of the KDE project.
4

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
5
    SPDX-FileCopyrightText: 2006 Lubos Lunak <l.lunak@kde.org>
6

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
7
8
    SPDX-License-Identifier: GPL-2.0-or-later
*/
9
10
11
12
13
14

#include "unmanaged.h"

#include "workspace.h"
#include "effects.h"
#include "deleted.h"
15
#include "surfaceitem_x11.h"
Martin Flöser's avatar
Martin Flöser committed
16
#include "utils.h"
17
#include "xcbutils.h"
18

19
#include <QTimer>
20
#include <QDebug>
21
#include <QWindow>
22

23
#include <xcb/shape.h>
24

25
26
27
28
#include <KWaylandServer/surface_interface.h>

using namespace KWaylandServer;

29
30
31
namespace KWin
{

32
33
34
35
36
37
38
// window types that are supported as unmanaged (mainly for compositing)
const NET::WindowTypes SUPPORTED_UNMANAGED_WINDOW_TYPES_MASK = NET::NormalMask | NET::DesktopMask | NET::DockMask
        | NET::ToolbarMask | NET::MenuMask | NET::DialogMask /*| NET::OverrideMask*/ | NET::TopMenuMask
        | NET::UtilityMask | NET::SplashMask | NET::DropdownMenuMask | NET::PopupMenuMask
        | NET::TooltipMask | NET::NotificationMask | NET::ComboBoxMask | NET::DNDIconMask | NET::OnScreenDisplayMask
        | NET::CriticalNotificationMask;

39
40
Unmanaged::Unmanaged()
    : Toplevel()
41
{
42
43
    switch (kwinApp()->operationMode()) {
    case Application::OperationModeXwayland:
44
45
        // The wayland surface is associated with the override-redirect window asynchronously.
        connect(this, &Toplevel::surfaceChanged, this, &Unmanaged::associate);
46
47
        break;
    case Application::OperationModeX11:
48
49
50
        // We have no way knowing whether the override-redirect window can be painted. Mark it
        // as ready for painting after synthetic 50ms delay.
        QTimer::singleShot(50, this, &Unmanaged::initialize);
51
52
53
54
        break;
    case Application::OperationModeWaylandOnly:
        Q_UNREACHABLE();
    }
55
56
}

57
Unmanaged::~Unmanaged()
58
59
{
}
60

61
62
63
void Unmanaged::associate()
{
    if (surface()->isMapped()) {
64
        initialize();
65
    } else {
66
67
68
        // Queued connection because we want to mark the window ready for painting after
        // the associated surface item has processed the new surface state.
        connect(surface(), &SurfaceInterface::mapped, this, &Unmanaged::initialize, Qt::QueuedConnection);
69
70
71
    }
}

72
73
74
75
76
77
78
79
void Unmanaged::initialize()
{
    setReadyForPainting();

    // With unmanaged windows there is a race condition between the client painting the window
    // and us setting up damage tracking.  If the client wins we won't get a damage event even
    // though the window has been painted.  To avoid this we mark the whole window as damaged
    // and schedule a repaint immediately after creating the damage object.
80
81
82
    if (auto item = surfaceItem()) {
        item->addDamage(item->rect());
    }
83
84
}

Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
85
bool Unmanaged::track(xcb_window_t w)
86
{
87
    XServerGrabber xserverGrabber;
Martin Flöser's avatar
Martin Flöser committed
88
89
90
    Xcb::WindowAttributes attr(w);
    Xcb::WindowGeometry geo(w);
    if (attr.isNull() || attr->map_state != XCB_MAP_STATE_VIEWABLE) {
91
        return false;
92
    }
Martin Flöser's avatar
Martin Flöser committed
93
94
95
96
    if (attr->_class == XCB_WINDOW_CLASS_INPUT_ONLY) {
        return false;
    }
    if (geo.isNull()) {
97
        return false;
98
    }
99
    setWindowHandles(w);   // the window is also the frame
Martin Flöser's avatar
Martin Flöser committed
100
    Xcb::selectInput(w, attr->your_event_mask | XCB_EVENT_MASK_STRUCTURE_NOTIFY | XCB_EVENT_MASK_PROPERTY_CHANGE);
101
    m_frameGeometry = geo.rect();
102
    m_clientGeometry = geo.rect();
103
    checkScreen();
Martin Flöser's avatar
Martin Flöser committed
104
105
    m_visual = attr->visual;
    bit_depth = geo->depth;
106
    info = new NETWinInfo(connection(), w, rootWindow(),
107
108
109
                          NET::WMWindowType | NET::WMPid,
                          NET::WM2Opacity |
                          NET::WM2WindowRole |
Martin Flöser's avatar
Martin Flöser committed
110
111
                          NET::WM2WindowClass |
                          NET::WM2OpaqueRegion);
112
    setOpacity(info->opacityF());
113
114
115
    getResourceClass();
    getWmClientLeader();
    getWmClientMachine();
116
    if (Xcb::Extensions::self()->isShapeAvailable())
117
        xcb_shape_select_input(connection(), w, true);
118
    detectShape(w);
119
    getWmOpaqueRegion();
120
    getSkipCloseAnimation();
121
    setupCompositing();
122
123
124
    if (QWindow *internalWindow = findInternalWindow()) {
        m_outline = internalWindow->property("__kwin_outline").toBool();
    }
125
    if (effects)
126
127
        static_cast<EffectsHandlerImpl*>(effects)->checkInputWindowStacking();
    return true;
128
}
129

130
void Unmanaged::release(ReleaseReason releaseReason)
131
{
132
    addWorkspaceRepaint(visibleGeometry());
133
    emit markedAsZombie();
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
134
    Deleted* del = nullptr;
135
    if (releaseReason != ReleaseReason::KWinShutsDown) {
136
137
        del = Deleted::create(this);
    }
138
    emit windowClosed(this, del);
139
140
    finishCompositing(releaseReason);
    if (!QWidget::find(window()) && releaseReason != ReleaseReason::Destroyed) { // don't affect our own windows
141
        if (Xcb::Extensions::self()->isShapeAvailable())
142
            xcb_shape_select_input(connection(), window(), false);
143
        Xcb::selectInput(window(), XCB_EVENT_MASK_NO_EVENT);
144
    }
Vlad Zahorodnii's avatar
Vlad Zahorodnii committed
145
    workspace()->removeUnmanaged(this);
146
    if (releaseReason != ReleaseReason::KWinShutsDown) {
147
148
149
        disownDataPassedToDeleted();
        del->unrefWindow();
    }
150
    deleteUnmanaged(this);
151
}
152

153
void Unmanaged::deleteUnmanaged(Unmanaged* c)
154
{
155
    delete c;
156
}
157

158
159
160
161
162
bool Unmanaged::hasScheduledRelease() const
{
    return m_scheduledRelease;
}

163
164
QRect Unmanaged::bufferGeometry() const
{
165
    return m_frameGeometry;
166
167
}

168
int Unmanaged::desktop() const
169
{
170
    return NET::OnAllDesktops; // TODO for some window types should be the current desktop?
171
}
172

173
QStringList Unmanaged::activities() const
174
{
175
    return QStringList();
176
}
177

178
QVector<VirtualDesktop *> Unmanaged::desktops() const
179
{
180
    return QVector<VirtualDesktop *>();
181
182
}

183
QPoint Unmanaged::clientPos() const
184
185
186
{
    return QPoint(0, 0);   // unmanaged windows don't have decorations
}
187

188
QRect Unmanaged::transparentRect() const
189
{
190
    return QRect(clientPos(), clientSize());
191
}
192

193
194
195
196
197
198
199
200
NET::WindowType Unmanaged::windowType(bool direct, int supportedTypes) const
{
    // for unmanaged windows the direct does not make any difference
    // as there are no rules to check and no hacks to apply
    Q_UNUSED(direct)
    if (supportedTypes == 0) {
        supportedTypes = SUPPORTED_UNMANAGED_WINDOW_TYPES_MASK;
    }
201
    return info->windowType(NET::WindowTypes(supportedTypes));
202
203
}

204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
bool Unmanaged::isOutline() const
{
    return m_outline;
}

QWindow *Unmanaged::findInternalWindow() const
{
    const QWindowList windows = kwinApp()->topLevelWindows();
    for (QWindow *w : windows) {
        if (w->winId() == window()) {
            return w;
        }
    }
    return nullptr;
}

220
221
222
223
224
225
226
void Unmanaged::damageNotifyEvent()
{
    Q_ASSERT(kwinApp()->operationMode() == Application::OperationModeX11);
    SurfaceItemX11 *item = static_cast<SurfaceItemX11 *>(surfaceItem());
    if (item) {
        item->processDamage();
    }
227
228
}

229
230
} // namespace