kwin/src/inputpanelv1window.cpp

210 lines
6.9 KiB
C++
Raw Normal View History

2020-07-11 16:40:28 +00:00
/*
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 "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"
2020-07-11 16:40:28 +00:00
#include "wayland_server.h"
#include "workspace.h"
namespace KWin
{
InputPanelV1Window::InputPanelV1Window(InputPanelSurfaceV1Interface *panelSurface)
: WaylandWindow(panelSurface->surface())
2020-07-11 16:40:28 +00:00
, m_panelSurface(panelSurface)
{
setSkipSwitcher(true);
2020-07-11 16:40:28 +00:00
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);
2020-07-11 16:40:28 +00:00
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);
kwinApp()->inputMethod()->setPanel(this);
2020-07-11 16:40:28 +00:00
}
void InputPanelV1Window::showOverlayPanel()
2020-07-11 16:40:28 +00:00
{
m_mode = Mode::Overlay;
maybeShow();
2020-07-11 16:40:28 +00:00
}
void InputPanelV1Window::showTopLevel(OutputInterface *output, InputPanelSurfaceV1Interface::Position position)
2020-07-11 16:40:28 +00:00
{
m_mode = Mode::VirtualKeyboard;
maybeShow();
}
void InputPanelV1Window::allow()
{
m_allowed = true;
maybeShow();
}
void InputPanelV1Window::show()
{
m_virtualKeyboardShouldBeShown = true;
maybeShow();
}
void InputPanelV1Window::hide()
{
m_virtualKeyboardShouldBeShown = false;
if (readyForPainting() && m_mode != Mode::Overlay) {
setHidden(true);
}
2020-07-11 16:40:28 +00:00
}
void InputPanelV1Window::reposition()
2020-07-11 16:40:28 +00:00
{
if (!readyForPainting()) {
return;
}
2020-07-11 16:40:28 +00:00
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;
geo.moveLeft(availableArea.left() + (availableArea.width() - geo.width()) / 2);
geo.moveBottom(availableArea.bottom());
moveResize(geo);
} break;
case Mode::Overlay: {
auto textInputSurface = waylandServer()->seat()->focusedTextInputSurface();
auto textWindow = waylandServer()->findWindow(textInputSurface);
QRect cursorRectangle;
auto textInputV1 = waylandServer()->seat()->textInputV1();
if (textInputV1 && textInputV1->isEnabled() && textInputV1->surface() == textInputSurface) {
cursorRectangle = textInputV1->cursorRectangle();
}
auto textInputV2 = waylandServer()->seat()->textInputV2();
if (textInputV2 && textInputV2->isEnabled() && textInputV2->surface() == textInputSurface) {
cursorRectangle = textInputV2->cursorRectangle();
}
auto textInputV3 = waylandServer()->seat()->textInputV3();
if (textInputV3 && textInputV3->isEnabled() && textInputV3->surface() == textInputSurface) {
cursorRectangle = textInputV3->cursorRectangle();
}
if (textWindow) {
cursorRectangle.translate(textWindow->bufferGeometry().topLeft().toPoint());
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());
}
2020-07-11 16:40:28 +00:00
}
if (popupRect.top() < screen.top()) {
popupRect.moveTop(screen.top());
}
if (popupRect.bottom() > screen.bottom()) {
popupRect.moveBottom(screen.bottom());
}
moveResize(popupRect);
}
} break;
2020-07-11 16:40:28 +00:00
}
}
void InputPanelV1Window::destroyWindow()
2020-07-11 16:40:28 +00:00
{
m_panelSurface->disconnect(this);
m_panelSurface->surface()->disconnect(this);
2020-07-11 16:40:28 +00:00
markAsDeleted();
Q_EMIT closed();
2020-07-11 16:40:28 +00:00
StackingUpdatesBlocker blocker(workspace());
waylandServer()->removeWindow(this);
2020-07-11 16:40:28 +00:00
2023-03-02 20:51:46 +00:00
unref();
2020-07-11 16:40:28 +00:00
}
WindowType InputPanelV1Window::windowType() const
2020-07-11 16:40:28 +00:00
{
return WindowType::Utility;
2020-07-11 16:40:28 +00:00
}
QRectF InputPanelV1Window::frameRectToBufferRect(const QRectF &rect) const
{
return QRectF(rect.topLeft() - m_windowGeometry.topLeft(), surface()->size());
}
void InputPanelV1Window::moveResizeInternal(const QRectF &rect, MoveResizeMode mode)
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
{
updateGeometry(rect);
}
void InputPanelV1Window::handleMapped()
{
maybeShow();
}
void InputPanelV1Window::maybeShow()
{
const bool shouldShow = m_mode == Mode::Overlay || (m_mode == Mode::VirtualKeyboard && m_allowed && m_virtualKeyboardShouldBeShown);
if (shouldShow && !isDeleted() && surface()->isMapped()) {
markAsMapped();
reposition();
setHidden(false);
}
}
} // namespace KWin
#include "moc_inputpanelv1window.cpp"