kwin/keyboard_input.cpp

262 lines
8.4 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2013, 2016 Martin Gräßlin <mgraesslin@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 "keyboard_input.h"
#include "input_event.h"
#include "input_event_spy.h"
#include "keyboard_layout.h"
#include "keyboard_repeat.h"
#include "abstract_client.h"
#include "modifier_only_shortcuts.h"
#include "utils.h"
#include "screenlockerwatcher.h"
#include "toplevel.h"
#include "wayland_server.h"
#include "workspace.h"
// KWayland
Support for syncing the clipboard from X11 to Wayland and vice versa Summary: The clipboard sync is done by a dedicated helper binary launched by KWin. This helper binary is forced to xcb platform to piggy-back on Qt's implementation of the X11 clipboard. In addition it implements the Wayland clipboard - which is much simpler. Reading the Wayland clipboard is based on the implementation in QtWayland. KWin internally knows the DataDeviceInterface belonging to the helper application. Whenever an xwayland client is focussed, this DataDevice is allowed to set the selection and KWin manually updates the current selection in the SeatInterface. By that the sync from X11 to Wayland is implemented. When afterwards a Wayland client is selected, it's sent the current selection which references the X clipboard and a data transfer can be initiated in the normal Wayland way. For the other direction KWin sends the current selection to the helper's DataDevice whenever an xwayland window is focused. The helper application reads the Wayland clipboard and sets it on the X11 clipboard. Thus the Wayland clipboard is synced to X11. The approach used here will also be useful for implementing a clipboard manager (aka klipper). Currently the implementation is not yet fully completed. We need to make sure that the helper application gets restarted in case of a crash. Test Plan: See added test case Reviewers: #plasma_on_wayland, #kwin Subscribers: plasma-devel, kwin Tags: #plasma_on_wayland, #kwin Differential Revision: https://phabricator.kde.org/D1973
2016-06-20 09:21:16 +00:00
#include <KWayland/Server/datadevice_interface.h>
#include <KWayland/Server/seat_interface.h>
//screenlocker
#include <KScreenLocker/KsldApp>
// Frameworks
#include <KGlobalAccel>
// Qt
#include <QKeyEvent>
namespace KWin
{
KeyboardInputRedirection::KeyboardInputRedirection(InputRedirection *parent)
: QObject(parent)
, m_input(parent)
, m_xkb(new Xkb(parent))
{
}
KeyboardInputRedirection::~KeyboardInputRedirection() = default;
class KeyStateChangedSpy : public InputEventSpy
{
public:
KeyStateChangedSpy(InputRedirection *input)
: m_input(input)
{
}
void keyEvent(KeyEvent *event) override
{
if (event->isAutoRepeat()) {
return;
}
emit m_input->keyStateChanged(event->nativeScanCode(), event->type() == QEvent::KeyPress ? InputRedirection::KeyboardKeyPressed : InputRedirection::KeyboardKeyReleased);
}
private:
InputRedirection *m_input;
};
class ModifiersChangedSpy : public InputEventSpy
{
public:
ModifiersChangedSpy(InputRedirection *input)
: m_input(input)
, m_modifiers()
{
}
void keyEvent(KeyEvent *event) override
{
if (event->isAutoRepeat()) {
return;
}
updateModifiers(event->modifiers());
}
void updateModifiers(Qt::KeyboardModifiers mods)
{
if (mods == m_modifiers) {
return;
}
emit m_input->keyboardModifiersChanged(mods, m_modifiers);
m_modifiers = mods;
}
private:
InputRedirection *m_input;
Qt::KeyboardModifiers m_modifiers;
};
void KeyboardInputRedirection::init()
{
Q_ASSERT(!m_inited);
m_inited = true;
const auto config = kwinApp()->kxkbConfig();
m_xkb->setConfig(config);
m_input->installInputEventSpy(new KeyStateChangedSpy(m_input));
m_modifiersChangedSpy = new ModifiersChangedSpy(m_input);
m_input->installInputEventSpy(m_modifiersChangedSpy);
m_keyboardLayout = new KeyboardLayout(m_xkb.data());
m_keyboardLayout->setConfig(config);
m_keyboardLayout->init();
m_input->installInputEventSpy(m_keyboardLayout);
m_input->installInputEventSpy(new ModifierOnlyShortcuts);
KeyboardRepeat *keyRepeatSpy = new KeyboardRepeat(m_xkb.data());
connect(keyRepeatSpy, &KeyboardRepeat::keyRepeat, this,
std::bind(&KeyboardInputRedirection::processKey, this, std::placeholders::_1, InputRedirection::KeyboardKeyAutoRepeat, std::placeholders::_2, nullptr));
m_input->installInputEventSpy(keyRepeatSpy);
connect(workspace(), &QObject::destroyed, this, [this] { m_inited = false; });
connect(waylandServer(), &QObject::destroyed, this, [this] { m_inited = false; });
connect(workspace(), &Workspace::clientActivated, this,
[this] {
disconnect(m_activeClientSurfaceChangedConnection);
if (auto c = workspace()->activeClient()) {
m_activeClientSurfaceChangedConnection = connect(c, &Toplevel::surfaceChanged, this, &KeyboardInputRedirection::update);
} else {
m_activeClientSurfaceChangedConnection = QMetaObject::Connection();
}
update();
}
);
if (waylandServer()->hasScreenLockerIntegration()) {
connect(ScreenLocker::KSldApp::self(), &ScreenLocker::KSldApp::lockStateChanged, this, &KeyboardInputRedirection::update);
}
}
void KeyboardInputRedirection::update()
{
if (!m_inited) {
return;
}
auto seat = waylandServer()->seat();
// TODO: this needs better integration
Toplevel *found = nullptr;
if (waylandServer()->isScreenLocked()) {
const ToplevelList &stacking = Workspace::self()->stackingOrder();
if (!stacking.isEmpty()) {
auto it = stacking.end();
do {
--it;
Toplevel *t = (*it);
if (t->isDeleted()) {
// a deleted window doesn't get mouse events
continue;
}
if (!t->isLockScreen()) {
continue;
}
if (!t->readyForPainting()) {
continue;
}
found = t;
break;
} while (it != stacking.begin());
}
Implement support for pointer constraints Summary: There are two types of constraints supported: 1. Pointer confinement 2. Pointer locking In the case of confinement the pointer is confined to a given region of the surface. This is comparable to general operation where the pointer is confined to the screen region. In the second case the pointer gets locked. That means it cannot move at all. No further position updates are provided, only relative motion events can go to the application. There is a hint about cursor position update on unlock which is not yet implemented in KWayland::Server, thus also not in this change. The implementation in KWin grants the requests for pointer constraints when the pointer enters the constrained region, either by pointer movement or by e.g. stacking order changes. There is no confirmation from user required to enter that mode. But we want to show an OSD when the pointer gets constrained, this is not yet implemented, though. Breaking an active constraint is relatively easy. E.g. changing the stacking order will break the constraint if another surface is under the cursor. Also (in case of confinement) moving the pointer to an overlapping window breaks the confinement. But as soon as one moves the pointer back to the window a constraint might get honoured again. To properly break there is a dedicated event filter. It listens for a long press of the Escape key. If hold for 3sec the pointer constraint is broken and not activated again till the pointer got moved out of the window. Afterward when moving in the pointer might activate again. The escape filter ensures that the key press is forwarded to the application if it's a short press or if another key gets pressed during the three seconds. If the three seconds way fires, the later escape release is not sent to the application. This basic interaction is also ensured through an added auto test. This change implements T4605. Test Plan: Added auto test and nested KWin Wayland with D3488 Reviewers: #kwin, #plasma_on_wayland Subscribers: plasma-devel, kwin Tags: #plasma_on_wayland, #kwin Differential Revision: https://phabricator.kde.org/D3506
2016-11-25 06:17:43 +00:00
} else if (!input()->isSelectingWindow() && !input()->isBreakingPointerConstraints()) {
found = workspace()->activeClient();
}
if (found && found->surface()) {
if (found->surface() != seat->focusedKeyboardSurface()) {
seat->setFocusedKeyboardSurface(found->surface());
Support for syncing the clipboard from X11 to Wayland and vice versa Summary: The clipboard sync is done by a dedicated helper binary launched by KWin. This helper binary is forced to xcb platform to piggy-back on Qt's implementation of the X11 clipboard. In addition it implements the Wayland clipboard - which is much simpler. Reading the Wayland clipboard is based on the implementation in QtWayland. KWin internally knows the DataDeviceInterface belonging to the helper application. Whenever an xwayland client is focussed, this DataDevice is allowed to set the selection and KWin manually updates the current selection in the SeatInterface. By that the sync from X11 to Wayland is implemented. When afterwards a Wayland client is selected, it's sent the current selection which references the X clipboard and a data transfer can be initiated in the normal Wayland way. For the other direction KWin sends the current selection to the helper's DataDevice whenever an xwayland window is focused. The helper application reads the Wayland clipboard and sets it on the X11 clipboard. Thus the Wayland clipboard is synced to X11. The approach used here will also be useful for implementing a clipboard manager (aka klipper). Currently the implementation is not yet fully completed. We need to make sure that the helper application gets restarted in case of a crash. Test Plan: See added test case Reviewers: #plasma_on_wayland, #kwin Subscribers: plasma-devel, kwin Tags: #plasma_on_wayland, #kwin Differential Revision: https://phabricator.kde.org/D1973
2016-06-20 09:21:16 +00:00
auto newKeyboard = seat->focusedKeyboard();
if (newKeyboard && newKeyboard->client() == waylandServer()->xWaylandConnection()) {
// focus passed to an XWayland surface
const auto selection = seat->selection();
auto xclipboard = waylandServer()->xclipboardSyncDataDevice();
if (xclipboard && selection != xclipboard.data()) {
if (selection) {
xclipboard->sendSelection(selection);
} else {
xclipboard->sendClearSelection();
}
}
}
}
} else {
seat->setFocusedKeyboardSurface(nullptr);
}
}
void KeyboardInputRedirection::processKey(uint32_t key, InputRedirection::KeyboardKeyState state, uint32_t time, LibInput::Device *device)
{
QEvent::Type type;
bool autoRepeat = false;
switch (state) {
case InputRedirection::KeyboardKeyAutoRepeat:
autoRepeat = true;
// fall through
case InputRedirection::KeyboardKeyPressed:
type = QEvent::KeyPress;
break;
case InputRedirection::KeyboardKeyReleased:
type = QEvent::KeyRelease;
break;
default:
Q_UNREACHABLE();
}
if (!autoRepeat) {
m_xkb->updateKey(key, state);
}
const xkb_keysym_t keySym = m_xkb->currentKeysym();
KeyEvent event(type,
m_xkb->toQtKey(keySym),
m_xkb->modifiers(),
key,
keySym,
m_xkb->toString(keySym),
autoRepeat,
time,
device);
event.setModifiersRelevantForGlobalShortcuts(m_xkb->modifiersRelevantForGlobalShortcuts());
m_input->processSpies(std::bind(&InputEventSpy::keyEvent, std::placeholders::_1, &event));
if (!m_inited) {
return;
}
m_input->processFilters(std::bind(&InputEventFilter::keyEvent, std::placeholders::_1, &event));
}
void KeyboardInputRedirection::processModifiers(uint32_t modsDepressed, uint32_t modsLatched, uint32_t modsLocked, uint32_t group)
{
if (!m_inited) {
return;
}
// TODO: send to proper Client and also send when active Client changes
m_xkb->updateModifiers(modsDepressed, modsLatched, modsLocked, group);
m_modifiersChangedSpy->updateModifiers(modifiers());
m_keyboardLayout->checkLayoutChange();
}
void KeyboardInputRedirection::processKeymapChange(int fd, uint32_t size)
{
if (!m_inited) {
return;
}
// TODO: should we pass the keymap to our Clients? Or only to the currently active one and update
m_xkb->installKeymap(fd, size);
m_keyboardLayout->resetLayout();
}
}