File: inputpanelv1window.cpp

package info (click to toggle)
kwin 4%3A6.5.4-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,420 kB
  • sloc: cpp: 240,899; javascript: 2,225; xml: 2,096; ansic: 775; sh: 37; python: 15; makefile: 8
file content (259 lines) | stat: -rw-r--r-- 8,078 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
    KWin - the KDE window manager
    This file is part of the KDE project.

    SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "inputpanelv1window.h"
#include "core/output.h"
#include "core/pixelgrid.h"
#include "inputmethod.h"
#include "wayland/output.h"
#include "wayland/seat.h"
#include "wayland/surface.h"
#include "wayland/textinput_v1.h"
#include "wayland/textinput_v2.h"
#include "wayland/textinput_v3.h"
#include "wayland_server.h"
#include "workspace.h"

namespace KWin
{

InputPanelV1Window::InputPanelV1Window(InputPanelSurfaceV1Interface *panelSurface)
    : WaylandWindow(panelSurface->surface())
    , m_panelSurface(panelSurface)
{
    setOutput(workspace()->activeOutput());
    setMoveResizeOutput(workspace()->activeOutput());
    setSkipSwitcher(true);
    setSkipPager(true);
    setSkipTaskbar(true);

    connect(surface(), &SurfaceInterface::aboutToBeDestroyed, this, &InputPanelV1Window::destroyWindow);
    connect(surface(), &SurfaceInterface::sizeChanged, this, &InputPanelV1Window::reposition);
    connect(surface(), &SurfaceInterface::inputChanged, this, &InputPanelV1Window::reposition);
    connect(surface(), &SurfaceInterface::mapped, this, &InputPanelV1Window::handleMapped);
    connect(surface(), &SurfaceInterface::unmapped, this, &InputPanelV1Window::handleUnmapped);

    connect(panelSurface, &InputPanelSurfaceV1Interface::topLevel, this, &InputPanelV1Window::showTopLevel);
    connect(panelSurface, &InputPanelSurfaceV1Interface::overlayPanel, this, &InputPanelV1Window::showOverlayPanel);
    connect(panelSurface, &InputPanelSurfaceV1Interface::aboutToBeDestroyed, this, &InputPanelV1Window::destroyWindow);

    connect(workspace(), &Workspace::outputsChanged, this, &InputPanelV1Window::reposition);
    connect(kwinApp()->inputMethod(), &InputMethod::cursorRectangleChanged, this, &InputPanelV1Window::reposition);

    m_rescalingTimer.setSingleShot(true);
    m_rescalingTimer.setInterval(0);
    connect(&m_rescalingTimer, &QTimer::timeout, this, [this]() {
        setTargetScale(nextTargetScale());
    });
    connect(&m_rescalingTimer, &QTimer::timeout, this, &InputPanelV1Window::reposition);

    kwinApp()->inputMethod()->setPanel(this);
}

void InputPanelV1Window::showOverlayPanel()
{
    m_mode = Mode::Overlay;
    maybeShow();
}

void InputPanelV1Window::showTopLevel(OutputInterface *output, InputPanelSurfaceV1Interface::Position position)
{
    m_mode = Mode::VirtualKeyboard;
    maybeShow();
}

void InputPanelV1Window::allow()
{
    m_allowed = true;
    maybeShow();
}

void InputPanelV1Window::show()
{
    m_requestedToBeShown = true;
    maybeShow();
}

void InputPanelV1Window::hide()
{
    m_requestedToBeShown = false;
    if (readyForPainting() && m_mode != Mode::Overlay) {
        setHidden(true);
    }
}

void InputPanelV1Window::resetPosition()
{
    Q_ASSERT(!isDeleted());
    switch (m_mode) {
    case Mode::None: {
        // should never happen
    }; break;
    case Mode::VirtualKeyboard: {
        // maliit creates a fullscreen overlay so use the input shape as the window geometry.
        m_windowGeometry = surface()->input().boundingRect();

        const auto activeOutput = workspace()->activeOutput();
        QRectF availableArea;
        if (waylandServer()->isScreenLocked()) {
            availableArea = workspace()->clientArea(FullScreenArea, this, activeOutput);
        } else {
            availableArea = workspace()->clientArea(MaximizeArea, this, activeOutput);
        }

        QRectF geo = m_windowGeometry;

        // if it fits, align within available area
        if (geo.width() < availableArea.width()) {
            geo.moveLeft(availableArea.left() + (availableArea.width() - geo.width()) / 2);
        } else { // otherwise align to be centred within the screen
            const QRectF outputArea = activeOutput->geometry();
            geo.moveLeft(outputArea.left() + (outputArea.width() - geo.width()) / 2);
        }

        geo.moveBottom(availableArea.bottom());

        moveResize(snapToPixels(geo, targetScale()));
    } break;
    case Mode::Overlay: {
        QRect cursorRectangle = kwinApp()->inputMethod()->cursorRectangle();
        const QRectF screen = Workspace::self()->clientArea(PlacementArea, this, cursorRectangle.bottomLeft());

        m_windowGeometry = QRectF(QPointF(0, 0), surface()->size());

        QRectF popupRect(cursorRectangle.left(),
                         cursorRectangle.top() + cursorRectangle.height(),
                         m_windowGeometry.width(),
                         m_windowGeometry.height());
        if (popupRect.left() < screen.left()) {
            popupRect.moveLeft(screen.left());
        }
        if (popupRect.right() > screen.right()) {
            popupRect.moveRight(screen.right());
        }
        if (popupRect.top() < screen.top() || popupRect.bottom() > screen.bottom()) {
            const QRectF flippedPopupRect(cursorRectangle.left(),
                                          cursorRectangle.top() - m_windowGeometry.height(),
                                          m_windowGeometry.width(),
                                          m_windowGeometry.height());

            // if it still doesn't fit we should continue with the unflipped version
            if (flippedPopupRect.top() >= screen.top() && flippedPopupRect.bottom() <= screen.bottom()) {
                popupRect.moveTop(flippedPopupRect.top());
            }
        }
        if (popupRect.top() < screen.top()) {
            popupRect.moveTop(screen.top());
        }
        if (popupRect.bottom() > screen.bottom()) {
            popupRect.moveBottom(screen.bottom());
        }

        moveResize(snapToPixels(popupRect, targetScale()));

    } break;
    }
}

void InputPanelV1Window::reposition()
{
    if (!readyForPainting()) {
        return;
    }

    resetPosition();
}

void InputPanelV1Window::destroyWindow()
{
    m_panelSurface->disconnect(this);
    m_panelSurface->surface()->disconnect(this);
    disconnect(workspace(), &Workspace::outputsChanged, this, &InputPanelV1Window::reposition);
    disconnect(kwinApp()->inputMethod(), &InputMethod::cursorRectangleChanged, this, &InputPanelV1Window::reposition);

    markAsDeleted();
    Q_EMIT closed();

    m_rescalingTimer.stop();
    StackingUpdatesBlocker blocker(workspace());
    waylandServer()->removeWindow(this);

    unref();
}

WindowType InputPanelV1Window::windowType() const
{
    return WindowType::Utility;
}

QRectF InputPanelV1Window::frameRectToBufferRect(const QRectF &rect) const
{
    return QRectF(rect.topLeft() - m_windowGeometry.topLeft(), surface()->size());
}

void InputPanelV1Window::moveResizeInternal(const QRectF &rect, MoveResizeMode mode)
{
    updateGeometry(rect);
}

void InputPanelV1Window::doSetNextTargetScale()
{
    if (isDeleted()) {
        return;
    }
    surface()->setPreferredBufferScale(nextTargetScale());
    // re-align the surface with the new target scale
    m_rescalingTimer.start();
}

void InputPanelV1Window::doSetPreferredBufferTransform()
{
    if (isDeleted()) {
        return;
    }
    surface()->setPreferredBufferTransform(preferredBufferTransform());
}

void InputPanelV1Window::doSetPreferredColorDescription()
{
    if (isDeleted()) {
        return;
    }
    surface()->setPreferredColorDescription(preferredColorDescription());
}

void InputPanelV1Window::handleMapped()
{
    m_wasEverMapped = true;
    maybeShow();
}

void InputPanelV1Window::handleUnmapped()
{
    setHidden(true);
}

bool InputPanelV1Window::wasUnmapped() const
{
    return m_wasEverMapped && !surface()->isMapped();
}

void InputPanelV1Window::maybeShow()
{
    const bool shouldShow = m_mode == Mode::Overlay || (m_mode == Mode::VirtualKeyboard && m_allowed && m_requestedToBeShown);
    if (shouldShow && !isDeleted() && surface()->isMapped()) {
        resetPosition();
        markAsMapped();
        setHidden(false);
    }
}

} // namespace KWin

#include "moc_inputpanelv1window.cpp"