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
|
/*
SPDX-FileCopyrightText: 2017 Martin Graesslin <mgraesslin@kde.org>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "popup_input_filter.h"
#include "input_event.h"
#include "internalwindow.h"
#include "keyboard_input.h"
#include "wayland/seat.h"
#include "wayland_server.h"
#include "waylandwindow.h"
#include "window.h"
#include "workspace.h"
#include <qpa/qwindowsysteminterface.h>
namespace KWin
{
PopupInputFilter::PopupInputFilter()
: QObject()
, InputEventFilter(InputFilterOrder::Popup)
{
connect(workspace(), &Workspace::windowAdded, this, &PopupInputFilter::handleWindowAdded);
connect(workspace(), &Workspace::windowActivated, this, &PopupInputFilter::handleWindowFocusChanged);
}
void PopupInputFilter::handleWindowAdded(Window *window)
{
if (m_popupWindows.contains(window)) {
return;
}
if (window->hasPopupGrab()) {
// TODO: verify that the Window is allowed as a popup
m_popupWindows << window;
focus(window);
connect(window, &Window::closed, this, [this, window]() {
m_popupWindows.removeOne(window);
// Move focus to the parent popup. If that's the last popup, then move focus back to the parent
if (!m_popupWindows.isEmpty()) {
focus(m_popupWindows.constLast());
} else {
input()->keyboard()->update();
}
});
}
}
void PopupInputFilter::handleWindowFocusChanged()
{
// user focussed a window through another mechanism such as a shortcut
cancelPopups();
}
bool PopupInputFilter::pointerButton(PointerButtonEvent *event)
{
if (m_popupWindows.isEmpty()) {
return false;
}
if (event->state == PointerButtonState::Pressed) {
auto pointerFocus = input()->findToplevel(event->position);
if (!pointerFocus || !Window::belongToSameApplication(pointerFocus, m_popupWindows.constLast())) {
// a press on a window (or no window) not belonging to the popup window
cancelPopups();
// filter out this press
return true;
}
if (pointerFocus && pointerFocus->isDecorated()) {
// test whether it is on the decoration
if (!exclusiveContains(pointerFocus->clientGeometry(), event->position)) {
cancelPopups();
return true;
}
}
}
return false;
}
bool PopupInputFilter::keyboardKey(KeyboardKeyEvent *event)
{
if (m_popupWindows.isEmpty()) {
return false;
}
Window *last = m_popupWindows.last();
focus(last);
if (auto internalWindow = qobject_cast<InternalWindow *>(last)) {
QWindowSystemInterface::handleExtendedKeyEvent(internalWindow->handle(),
event->state != KeyboardKeyState::Released ? QEvent::KeyPress : QEvent::KeyRelease,
event->key,
event->modifiers,
event->nativeScanCode,
event->nativeVirtualKey,
0,
event->text,
event->state == KeyboardKeyState::Repeated);
} else if (qobject_cast<WaylandWindow *>(last)) {
if (!passToInputMethod(event)) {
waylandServer()->seat()->setTimestamp(event->timestamp);
waylandServer()->seat()->notifyKeyboardKey(event->nativeScanCode, event->state, event->serial);
}
}
return true;
}
bool PopupInputFilter::touchDown(TouchDownEvent *event)
{
if (m_popupWindows.isEmpty()) {
return false;
}
auto pointerFocus = input()->findToplevel(event->pos);
if (!pointerFocus || !Window::belongToSameApplication(pointerFocus, m_popupWindows.constLast())) {
// a touch on a window (or no window) not belonging to the popup window
cancelPopups();
// filter out this touch
return true;
}
if (pointerFocus && pointerFocus->isDecorated()) {
// test whether it is on the decoration
if (!exclusiveContains(pointerFocus->clientGeometry(), event->pos)) {
cancelPopups();
return true;
}
}
return false;
}
bool PopupInputFilter::tabletToolTipEvent(TabletToolTipEvent *event)
{
if (m_popupWindows.isEmpty()) {
return false;
}
if (event->type == TabletToolTipEvent::Type::Press) {
auto tabletFocus = input()->findToplevel(event->position);
if (!tabletFocus || !Window::belongToSameApplication(tabletFocus, m_popupWindows.constLast())) {
// a touch on a window (or no window) not belonging to the popup window
cancelPopups();
// filter out this touch
return true;
}
if (tabletFocus && tabletFocus->isDecorated()) {
// test whether it is on the decoration
if (!exclusiveContains(tabletFocus->clientGeometry(), event->position)) {
cancelPopups();
return true;
}
}
}
return false;
}
void PopupInputFilter::focus(Window *popup)
{
if (auto internalWindow = qobject_cast<InternalWindow *>(m_popupWindows.constLast())) {
waylandServer()->seat()->setFocusedKeyboardSurface(nullptr);
if (QGuiApplication::focusWindow() != internalWindow->handle()) {
QWindowSystemInterface::handleFocusWindowChanged(internalWindow->handle());
}
} else if (auto waylandWindow = qobject_cast<WaylandWindow *>(m_popupWindows.constLast())) {
if (QGuiApplication::focusWindow()) {
QWindowSystemInterface::handleFocusWindowChanged(nullptr);
}
waylandServer()->seat()->setFocusedKeyboardSurface(waylandWindow->surface(), input()->keyboard()->unfilteredKeys());
}
}
void PopupInputFilter::cancelPopups()
{
while (!m_popupWindows.isEmpty()) {
auto c = m_popupWindows.takeLast();
c->popupDone();
}
}
}
#include "moc_popup_input_filter.cpp"
|