2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-11-25 12:39:24 +00:00
|
|
|
|
2020-03-15 15:19:28 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2014-11-25 12:39:24 +00:00
|
|
|
#include "display.h"
|
2023-09-13 05:52:59 +00:00
|
|
|
#include "keyboard_p.h"
|
|
|
|
#include "seat.h"
|
|
|
|
#include "seat_p.h"
|
|
|
|
#include "surface.h"
|
2022-04-22 09:27:33 +00:00
|
|
|
#include "utils/common.h"
|
2014-11-25 12:39:24 +00:00
|
|
|
// Qt
|
2023-10-19 06:50:15 +00:00
|
|
|
#include <QList>
|
2014-11-25 12:39:24 +00:00
|
|
|
|
2020-03-19 13:58:52 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
namespace KWin
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
KeyboardInterfacePrivate::KeyboardInterfacePrivate(SeatInterface *s)
|
|
|
|
: seat(s)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:34:35 +00:00
|
|
|
void KeyboardInterfacePrivate::keyboard_release(Resource *resource)
|
|
|
|
{
|
|
|
|
wl_resource_destroy(resource->handle);
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterfacePrivate::keyboard_bind_resource(Resource *resource)
|
|
|
|
{
|
2021-02-13 15:00:09 +00:00
|
|
|
const ClientConnection *focusedClient = focusedSurface ? focusedSurface->client() : nullptr;
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
if (resource->version() >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
|
|
|
|
send_repeat_info(resource->handle, keyRepeat.charactersPerSecond, keyRepeat.delay);
|
|
|
|
}
|
|
|
|
if (!keymap.isNull()) {
|
2021-03-26 11:14:04 +00:00
|
|
|
sendKeymap(resource);
|
2020-10-02 14:22:59 +00:00
|
|
|
}
|
2021-02-13 15:00:09 +00:00
|
|
|
|
|
|
|
if (focusedClient && focusedClient->client() == resource->client()) {
|
2023-10-19 06:50:15 +00:00
|
|
|
const QList<quint32> keys = pressedKeys();
|
2021-08-29 05:11:06 +00:00
|
|
|
const QByteArray keysData = QByteArray::fromRawData(reinterpret_cast<const char *>(keys.data()), sizeof(quint32) * keys.count());
|
2021-02-13 15:00:09 +00:00
|
|
|
const quint32 serial = seat->display()->nextSerial();
|
|
|
|
|
|
|
|
send_enter(resource->handle, serial, focusedSurface->resource(), keysData);
|
2021-08-29 05:11:06 +00:00
|
|
|
send_modifiers(resource->handle, serial, modifiers.depressed, modifiers.latched, modifiers.locked, modifiers.group);
|
2021-02-13 15:00:09 +00:00
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<KeyboardInterfacePrivate::Resource *> KeyboardInterfacePrivate::keyboardsForClient(ClientConnection *client) const
|
|
|
|
{
|
|
|
|
return resourceMap().values(client->client());
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardInterfacePrivate::sendLeave(SurfaceInterface *surface, quint32 serial)
|
2016-04-06 11:50:19 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
const QList<Resource *> keyboards = keyboardsForClient(surface->client());
|
|
|
|
for (Resource *keyboardResource : keyboards) {
|
|
|
|
send_leave(keyboardResource->handle, serial, surface->resource());
|
2016-04-06 11:50:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterfacePrivate::sendEnter(SurfaceInterface *surface, quint32 serial)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
const auto states = pressedKeys();
|
2021-08-29 05:11:06 +00:00
|
|
|
QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char *>(states.constData()), sizeof(quint32) * states.size());
|
2014-11-25 12:39:24 +00:00
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
const QList<Resource *> keyboards = keyboardsForClient(surface->client());
|
|
|
|
for (Resource *keyboardResource : keyboards) {
|
|
|
|
send_enter(keyboardResource->handle, serial, surface->resource(), data);
|
|
|
|
}
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 11:14:04 +00:00
|
|
|
void KeyboardInterfacePrivate::sendKeymap(Resource *resource)
|
2020-03-19 13:58:52 +00:00
|
|
|
{
|
2022-08-16 14:07:50 +00:00
|
|
|
// From version 7 on, keymaps must be mapped privately, so that
|
|
|
|
// we can seal the fd and reuse it between clients.
|
2023-09-13 17:59:29 +00:00
|
|
|
if (resource->version() >= 7 && sharedKeymapFile.effectiveFlags().testFlag(RamFile::Flag::SealWrite)) {
|
2022-08-16 14:07:50 +00:00
|
|
|
send_keymap(resource->handle, keymap_format::keymap_format_xkb_v1, sharedKeymapFile.fd(), sharedKeymapFile.size());
|
|
|
|
// otherwise give each client its own unsealed copy.
|
|
|
|
} else {
|
2023-09-13 17:59:29 +00:00
|
|
|
RamFile keymapFile("kwin-xkb-keymap", keymap.constData(), keymap.size() + 1); // Include QByteArray null-terminator.
|
2022-08-16 14:07:50 +00:00
|
|
|
send_keymap(resource->handle, keymap_format::keymap_format_xkb_v1, keymapFile.fd(), keymapFile.size());
|
2020-03-19 13:58:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:14:04 +00:00
|
|
|
void KeyboardInterface::setKeymap(const QByteArray &content)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2021-03-26 11:14:04 +00:00
|
|
|
if (content.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->keymap = content;
|
2022-08-16 14:07:50 +00:00
|
|
|
// +1 to include QByteArray null terminator.
|
2023-09-13 17:59:29 +00:00
|
|
|
d->sharedKeymapFile = RamFile("kwin-xkb-keymap-shared", content.constData(), content.size() + 1, RamFile::Flag::SealWrite);
|
2021-03-26 11:14:04 +00:00
|
|
|
|
|
|
|
const auto keyboardResources = d->resourceMap();
|
|
|
|
for (KeyboardInterfacePrivate::Resource *resource : keyboardResources) {
|
|
|
|
d->sendKeymap(resource);
|
2016-06-28 15:02:22 +00:00
|
|
|
}
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2022-08-09 02:10:45 +00:00
|
|
|
void KeyboardInterfacePrivate::sendModifiers(SurfaceInterface *surface, quint32 depressed, quint32 latched, quint32 locked, quint32 group, quint32 serial)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2022-08-09 02:10:45 +00:00
|
|
|
const QList<Resource *> keyboards = keyboardsForClient(surface->client());
|
2020-10-02 14:22:59 +00:00
|
|
|
for (Resource *keyboardResource : keyboards) {
|
|
|
|
send_modifiers(keyboardResource->handle, serial, depressed, latched, locked, group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:50:52 +00:00
|
|
|
bool KeyboardInterfacePrivate::updateKey(quint32 key, KeyboardKeyState state)
|
2020-10-02 14:22:59 +00:00
|
|
|
{
|
|
|
|
auto it = states.find(key);
|
|
|
|
if (it == states.end()) {
|
|
|
|
states.insert(key, state);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (it.value() == state) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
it.value() = state;
|
|
|
|
return true;
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
KeyboardInterface::KeyboardInterface(SeatInterface *seat)
|
|
|
|
: d(new KeyboardInterfacePrivate(seat))
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardInterface::~KeyboardInterface() = default;
|
|
|
|
|
2022-08-09 02:10:45 +00:00
|
|
|
void KeyboardInterfacePrivate::sendModifiers(SurfaceInterface *surface)
|
2020-10-02 14:22:59 +00:00
|
|
|
{
|
2022-08-09 02:10:45 +00:00
|
|
|
sendModifiers(surface, modifiers.depressed, modifiers.latched, modifiers.locked, modifiers.group, modifiers.serial);
|
2014-11-26 14:00:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardInterface::setFocusedSurface(SurfaceInterface *surface, quint32 serial)
|
|
|
|
{
|
2021-02-13 14:19:05 +00:00
|
|
|
if (d->focusedSurface == surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-15 06:58:39 +00:00
|
|
|
if (d->focusedSurface) {
|
|
|
|
d->sendLeave(d->focusedSurface, serial);
|
|
|
|
disconnect(d->destroyConnection);
|
|
|
|
}
|
|
|
|
|
2014-11-26 14:00:44 +00:00
|
|
|
d->focusedSurface = surface;
|
|
|
|
if (!d->focusedSurface) {
|
2014-11-25 12:39:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-15 06:58:39 +00:00
|
|
|
d->destroyConnection = connect(d->focusedSurface, &SurfaceInterface::aboutToBeDestroyed, this, [this] {
|
|
|
|
d->sendLeave(d->focusedSurface, d->seat->display()->nextSerial());
|
|
|
|
d->focusedSurface = nullptr;
|
|
|
|
});
|
2014-11-25 12:39:24 +00:00
|
|
|
|
2016-04-06 11:50:19 +00:00
|
|
|
d->sendEnter(d->focusedSurface, serial);
|
2022-08-09 02:10:45 +00:00
|
|
|
d->sendModifiers(d->focusedSurface);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardInterface::setModifierFocusSurface(SurfaceInterface *surface)
|
|
|
|
{
|
|
|
|
if (d->modifierFocusSurface == surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
d->modifierFocusSurface = surface;
|
|
|
|
if (d->modifierFocusSurface && d->focusedSurface != d->modifierFocusSurface) {
|
|
|
|
d->modifiers.serial = d->seat->display()->nextSerial();
|
|
|
|
d->sendModifiers(d->modifierFocusSurface);
|
|
|
|
}
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 06:50:15 +00:00
|
|
|
QList<quint32> KeyboardInterfacePrivate::pressedKeys() const
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2023-10-19 06:50:15 +00:00
|
|
|
QList<quint32> keys;
|
2020-10-02 14:22:59 +00:00
|
|
|
for (auto it = states.constBegin(); it != states.constEnd(); ++it) {
|
2021-03-24 09:50:52 +00:00
|
|
|
if (it.value() == KeyboardKeyState::Pressed) {
|
2020-10-02 14:22:59 +00:00
|
|
|
keys << it.key();
|
|
|
|
}
|
2016-06-28 15:02:22 +00:00
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
return keys;
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 19:14:53 +00:00
|
|
|
void KeyboardInterface::sendKey(quint32 key, KeyboardKeyState state, ClientConnection *client)
|
|
|
|
{
|
|
|
|
const QList<KeyboardInterfacePrivate::Resource *> keyboards = d->keyboardsForClient(client);
|
|
|
|
const quint32 serial = d->seat->display()->nextSerial();
|
|
|
|
for (KeyboardInterfacePrivate::Resource *keyboardResource : keyboards) {
|
2022-12-21 00:55:20 +00:00
|
|
|
d->send_key(keyboardResource->handle, serial, d->seat->timestamp().count(), key, quint32(state));
|
2022-01-27 19:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:50:52 +00:00
|
|
|
void KeyboardInterface::sendKey(quint32 key, KeyboardKeyState state)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2021-03-24 09:50:52 +00:00
|
|
|
if (!d->updateKey(key, state)) {
|
2016-06-28 15:02:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
|
2021-03-15 07:37:29 +00:00
|
|
|
if (!d->focusedSurface) {
|
2020-10-02 14:22:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-27 19:14:53 +00:00
|
|
|
sendKey(key, state, d->focusedSurface->client());
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-24 09:50:52 +00:00
|
|
|
void KeyboardInterface::sendModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group)
|
2015-09-02 14:00:11 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
bool changed = false;
|
2023-01-11 14:18:25 +00:00
|
|
|
if (d->modifiers.depressed != depressed) {
|
|
|
|
d->modifiers.depressed = depressed;
|
|
|
|
changed = true;
|
2015-09-02 14:00:11 +00:00
|
|
|
}
|
2023-01-11 14:18:25 +00:00
|
|
|
if (d->modifiers.latched != latched) {
|
|
|
|
d->modifiers.latched = latched;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (d->modifiers.locked != locked) {
|
|
|
|
d->modifiers.locked = locked;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (d->modifiers.group != group) {
|
|
|
|
d->modifiers.group = group;
|
|
|
|
changed = true;
|
|
|
|
}
|
2022-08-09 02:10:45 +00:00
|
|
|
if (!changed) {
|
2020-12-07 16:40:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-09 02:10:45 +00:00
|
|
|
if (d->focusedSurface) {
|
|
|
|
d->modifiers.serial = d->seat->display()->nextSerial();
|
|
|
|
d->sendModifiers(d->focusedSurface, depressed, latched, locked, group, d->modifiers.serial);
|
|
|
|
}
|
|
|
|
if (d->modifierFocusSurface && d->focusedSurface != d->modifierFocusSurface) {
|
|
|
|
d->modifiers.serial = d->seat->display()->nextSerial();
|
|
|
|
d->sendModifiers(d->modifierFocusSurface, depressed, latched, locked, group, d->modifiers.serial);
|
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-06 04:33:47 +00:00
|
|
|
void KeyboardInterface::setRepeatInfo(qint32 charactersPerSecond, qint32 delay)
|
2020-10-02 14:22:59 +00:00
|
|
|
{
|
2022-11-12 14:25:23 +00:00
|
|
|
d->keyRepeat.charactersPerSecond = std::max(charactersPerSecond, 0);
|
|
|
|
d->keyRepeat.delay = std::max(delay, 0);
|
2022-12-13 17:11:23 +00:00
|
|
|
const auto keyboards = d->resourceMap();
|
2020-10-02 14:22:59 +00:00
|
|
|
for (KeyboardInterfacePrivate::Resource *keyboardResource : keyboards) {
|
|
|
|
if (keyboardResource->version() >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
|
|
|
|
d->send_repeat_info(keyboardResource->handle, d->keyRepeat.charactersPerSecond, d->keyRepeat.delay);
|
|
|
|
}
|
|
|
|
}
|
2015-09-02 14:00:11 +00:00
|
|
|
}
|
|
|
|
|
2014-11-26 14:00:44 +00:00
|
|
|
SurfaceInterface *KeyboardInterface::focusedSurface() const
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2014-11-26 14:00:44 +00:00
|
|
|
return d->focusedSurface;
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
qint32 KeyboardInterface::keyRepeatDelay() const
|
|
|
|
{
|
|
|
|
return d->keyRepeat.delay;
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 KeyboardInterface::keyRepeatRate() const
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
return d->keyRepeat.charactersPerSecond;
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-07-05 06:30:14 +00:00
|
|
|
|
2023-09-13 05:52:59 +00:00
|
|
|
#include "moc_keyboard.cpp"
|