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
|
|
|
|
*/
|
2016-04-06 09:35:31 +00:00
|
|
|
#include "keyboard_interface_p.h"
|
2014-11-25 12:39:24 +00:00
|
|
|
#include "display.h"
|
2020-11-06 04:33:47 +00:00
|
|
|
#include "logging.h"
|
2014-11-25 12:39:24 +00:00
|
|
|
#include "seat_interface.h"
|
2020-10-02 14:22:59 +00:00
|
|
|
#include "seat_interface_p.h"
|
2014-11-25 12:39:24 +00:00
|
|
|
#include "surface_interface.h"
|
2020-10-02 14:22:59 +00:00
|
|
|
#include "compositor_interface.h"
|
2014-11-25 12:39:24 +00:00
|
|
|
// Qt
|
2020-03-19 13:58:52 +00:00
|
|
|
#include <QTemporaryFile>
|
2014-11-26 14:00:44 +00:00
|
|
|
#include <QVector>
|
2014-11-25 12:39:24 +00:00
|
|
|
|
2020-03-19 13:58:52 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-04-29 14:56:38 +00:00
|
|
|
namespace KWaylandServer
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterfacePrivate::keyboard_bind_resource(Resource *resource)
|
|
|
|
{
|
|
|
|
if (resource->version() >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
|
|
|
|
send_repeat_info(resource->handle, keyRepeat.charactersPerSecond, keyRepeat.delay);
|
|
|
|
}
|
|
|
|
if (!keymap.isNull()) {
|
|
|
|
send_keymap(resource->handle, keymap_format::keymap_format_xkb_v1, keymap->handle(), keymap->size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<KeyboardInterfacePrivate::Resource *> KeyboardInterfacePrivate::keyboardsForClient(ClientConnection *client) const
|
|
|
|
{
|
|
|
|
return resourceMap().values(client->client());
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardInterfacePrivate::focusChildSurface(SurfaceInterface *childSurface, quint32 serial)
|
2016-04-06 11:50:19 +00:00
|
|
|
{
|
|
|
|
if (focusedChildSurface == childSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sendLeave(focusedChildSurface.data(), serial);
|
2020-10-02 14:22:59 +00:00
|
|
|
focusedChildSurface = QPointer<SurfaceInterface>(childSurface);
|
2016-04-06 11:50:19 +00:00
|
|
|
sendEnter(focusedChildSurface.data(), serial);
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterfacePrivate::sendLeave(SurfaceInterface *surface, quint32 serial)
|
2016-04-06 11:50:19 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
if (!surface) {
|
|
|
|
return;
|
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
|
|
|
if (!surface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto states = pressedKeys();
|
|
|
|
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
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
sendModifiers();
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-03-19 13:58:52 +00:00
|
|
|
void KeyboardInterface::setKeymap(const QByteArray &content)
|
|
|
|
{
|
2020-11-06 04:33:47 +00:00
|
|
|
if (content.isNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-19 13:58:52 +00:00
|
|
|
QScopedPointer<QTemporaryFile> tmp{new QTemporaryFile(this)};
|
|
|
|
if (!tmp->open()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
unlink(tmp->fileName().toUtf8().constData());
|
|
|
|
if (!tmp->resize(content.size())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uchar *address = tmp->map(0, content.size());
|
|
|
|
if (!address) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (qstrncpy(reinterpret_cast<char*>(address), content.constData(), content.size() + 1) == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
tmp->unmap(address);
|
2020-10-02 14:22:59 +00:00
|
|
|
|
2020-03-19 13:58:52 +00:00
|
|
|
d->sendKeymap(tmp->handle(), content.size());
|
|
|
|
d->keymap.swap(tmp);
|
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterfacePrivate::sendKeymap(int fd, quint32 size)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
const QList<Resource *> keyboards = resourceMap().values();
|
|
|
|
for (Resource *keyboardResource : keyboards) {
|
|
|
|
send_keymap(keyboardResource->handle, keymap_format::keymap_format_xkb_v1, fd, size);
|
2016-06-28 15:02:22 +00:00
|
|
|
}
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterfacePrivate::sendModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group, quint32 serial)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
if (!focusedSurface) {
|
2016-06-28 15:02:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
const QList<Resource *> keyboards = keyboardsForClient(focusedSurface->client());
|
|
|
|
for (Resource *keyboardResource : keyboards) {
|
|
|
|
send_modifiers(keyboardResource->handle, serial, depressed, latched, locked, group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyboardInterfacePrivate::updateKey(quint32 key, State state)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
void KeyboardInterfacePrivate::sendModifiers()
|
|
|
|
{
|
|
|
|
sendModifiers(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)
|
|
|
|
{
|
2016-04-06 11:50:19 +00:00
|
|
|
d->sendLeave(d->focusedChildSurface, serial);
|
|
|
|
disconnect(d->destroyConnection);
|
|
|
|
d->focusedChildSurface.clear();
|
2014-11-26 14:00:44 +00:00
|
|
|
d->focusedSurface = surface;
|
|
|
|
if (!d->focusedSurface) {
|
2014-11-25 12:39:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-07-16 13:18:56 +00:00
|
|
|
d->destroyConnection = connect(d->focusedSurface, &SurfaceInterface::aboutToBeDestroyed, this,
|
2014-11-26 14:00:44 +00:00
|
|
|
[this] {
|
2020-10-02 14:22:59 +00:00
|
|
|
CompositorInterface *compositor = d->focusedChildSurface->compositor();
|
|
|
|
d->sendLeave(d->focusedChildSurface.data(), compositor->display()->nextSerial());
|
2014-11-26 14:00:44 +00:00
|
|
|
d->focusedSurface = nullptr;
|
2016-04-06 11:50:19 +00:00
|
|
|
d->focusedChildSurface.clear();
|
2014-11-26 14:00:44 +00:00
|
|
|
}
|
|
|
|
);
|
2016-04-06 11:50:19 +00:00
|
|
|
d->focusedChildSurface = QPointer<SurfaceInterface>(surface);
|
2014-11-25 12:39:24 +00:00
|
|
|
|
2016-04-06 11:50:19 +00:00
|
|
|
d->sendEnter(d->focusedSurface, serial);
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
QVector<quint32> KeyboardInterfacePrivate::pressedKeys() const
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
QVector<quint32> keys;
|
|
|
|
for (auto it = states.constBegin(); it != states.constEnd(); ++it) {
|
|
|
|
if (it.value() == State::Pressed) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
|
|
|
|
void KeyboardInterface::keyPressed(quint32 key)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
if (!d->focusedSurface) {
|
2016-06-28 15:02:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
|
|
|
|
if (!d->updateKey(key, KeyboardInterfacePrivate::State::Pressed)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<KeyboardInterfacePrivate::Resource *> keyboards = d->keyboardsForClient(d->focusedSurface->client());
|
|
|
|
const quint32 serial = d->seat->d_func()->nextSerial();
|
|
|
|
for (KeyboardInterfacePrivate::Resource *keyboardResource : keyboards) {
|
|
|
|
d->send_key(keyboardResource->handle, serial, d->seat->timestamp(), key, KeyboardInterfacePrivate::key_state::key_state_pressed);
|
|
|
|
}
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterface::keyReleased(quint32 key)
|
2014-11-25 12:39:24 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
if (!d->focusedSurface) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->updateKey(key, KeyboardInterfacePrivate::State::Released)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QList<KeyboardInterfacePrivate::Resource *> keyboards = d->keyboardsForClient(d->focusedSurface->client());
|
|
|
|
const quint32 serial = d->seat->d_func()->nextSerial();
|
|
|
|
for (KeyboardInterfacePrivate::Resource *keyboardResource : keyboards) {
|
|
|
|
d->send_key(keyboardResource->handle, serial, d->seat->timestamp(), key, KeyboardInterfacePrivate::key_state::key_state_released);
|
|
|
|
}
|
2014-11-25 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 14:22:59 +00:00
|
|
|
void KeyboardInterface::updateModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group)
|
2015-09-02 14:00:11 +00:00
|
|
|
{
|
2020-10-02 14:22:59 +00:00
|
|
|
if (!d->focusedSurface) {
|
2016-06-28 15:02:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
bool changed = false;
|
|
|
|
#define UPDATE( value ) \
|
|
|
|
if (d->modifiers.value != value) { \
|
|
|
|
d->modifiers.value = value; \
|
|
|
|
changed = true; \
|
|
|
|
}
|
|
|
|
UPDATE(depressed)
|
|
|
|
UPDATE(latched)
|
|
|
|
UPDATE(locked)
|
|
|
|
UPDATE(group)
|
|
|
|
if (!changed) {
|
2015-09-02 14:00:11 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-02 14:22:59 +00:00
|
|
|
d->modifiers.serial = d->seat->d_func()->nextSerial();
|
|
|
|
d->sendModifiers(depressed, latched, locked, group, d->modifiers.serial);
|
|
|
|
}
|
|
|
|
|
2020-11-06 04:33:47 +00:00
|
|
|
void KeyboardInterface::setRepeatInfo(qint32 charactersPerSecond, qint32 delay)
|
2020-10-02 14:22:59 +00:00
|
|
|
{
|
|
|
|
d->keyRepeat.charactersPerSecond = qMax(charactersPerSecond, 0);
|
|
|
|
d->keyRepeat.delay = qMax(delay, 0);
|
|
|
|
const QList<KeyboardInterfacePrivate::Resource *> keyboards = d->resourceMap().values();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|