keyboard_interface.cpp 5.51 KB
Newer Older
1
2
/*
    SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
3

4
5
    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
6
#include "keyboard_interface.h"
7
#include "keyboard_interface_p.h"
8
9
10
11
#include "display.h"
#include "seat_interface.h"
#include "surface_interface.h"
// Qt
12
#include <QTemporaryFile>
Martin Flöser's avatar
Martin Flöser committed
13
#include <QVector>
14
15
16
// Wayland
#include <wayland-server.h>

17
18
#include <unistd.h>

19
namespace KWaylandServer
20
21
{

22
23
24
KeyboardInterface::Private::Private(SeatInterface *s, wl_resource *parentResource, KeyboardInterface *q)
    : Resource::Private(q, s, parentResource, &wl_keyboard_interface, &s_interface)
    , seat(s)
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
void KeyboardInterface::Private::focusChildSurface(const QPointer<SurfaceInterface> &childSurface, quint32 serial)
{
    if (focusedChildSurface == childSurface) {
        return;
    }
    sendLeave(focusedChildSurface.data(), serial);
    focusedChildSurface = childSurface;
    sendEnter(focusedChildSurface.data(), serial);
}

void KeyboardInterface::Private::sendLeave(SurfaceInterface *surface, quint32 serial)
{
    if (surface && resource && surface->resource()) {
        wl_keyboard_send_leave(resource, serial, surface->resource());
    }
}

void KeyboardInterface::Private::sendEnter(SurfaceInterface *surface, quint32 serial)
{
    wl_array keys;
    wl_array_init(&keys);
    const auto states = seat->pressedKeys();
Frederik Gladhorn's avatar
Frederik Gladhorn committed
50
    for (auto it = states.constBegin(); it != states.constEnd(); ++it) {
51
52
53
54
55
56
57
58
59
        uint32_t *k = reinterpret_cast<uint32_t*>(wl_array_add(&keys, sizeof(uint32_t)));
        *k = *it;
    }
    wl_keyboard_send_enter(resource, serial, surface->resource(), &keys);
    wl_array_release(&keys);

    sendModifiers();
}

60
#ifndef K_DOXYGEN
61
const struct wl_keyboard_interface KeyboardInterface::Private::s_interface {
62
    resourceDestroyedCallback
63
};
Martin Flöser's avatar
Martin Flöser committed
64
#endif
65

66
KeyboardInterface::KeyboardInterface(SeatInterface *parent, wl_resource *parentResource)
67
    : Resource(new Private(parent, parentResource, this))
68
69
70
{
}

71
KeyboardInterface::~KeyboardInterface() = default;
72
73
74

void KeyboardInterface::setKeymap(int fd, quint32 size)
{
75
    Q_D();
Martin Flöser's avatar
Martin Flöser committed
76
    d->sendKeymap(fd, size);
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
void KeyboardInterface::setKeymap(const QByteArray &content)
{
    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);
    Q_D();
    d->sendKeymap(tmp->handle(), content.size());
    d->keymap.swap(tmp);
}

Martin Flöser's avatar
Martin Flöser committed
102
void KeyboardInterface::Private::sendKeymap(int fd, quint32 size)
103
{
104
105
106
    if (!resource) {
        return;
    }
Martin Flöser's avatar
Martin Flöser committed
107
    wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, fd, size);
108
109
}

Martin Flöser's avatar
Martin Flöser committed
110
void KeyboardInterface::Private::sendModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group, quint32 serial)
111
{
112
113
114
    if (!resource) {
        return;
    }
Martin Flöser's avatar
Martin Flöser committed
115
    wl_keyboard_send_modifiers(resource, serial, depressed, latched, locked, group);
116
117
}

Martin Flöser's avatar
Martin Flöser committed
118
void KeyboardInterface::Private::sendModifiers()
119
{
Martin Flöser's avatar
Martin Flöser committed
120
121
122
123
124
    sendModifiers(seat->depressedModifiers(), seat->latchedModifiers(), seat->lockedModifiers(), seat->groupModifiers(), seat->lastModifiersSerial());
}

void KeyboardInterface::setFocusedSurface(SurfaceInterface *surface, quint32 serial)
{
125
    Q_D();
126
127
128
    d->sendLeave(d->focusedChildSurface, serial);
    disconnect(d->destroyConnection);
    d->focusedChildSurface.clear();
Martin Flöser's avatar
Martin Flöser committed
129
130
    d->focusedSurface = surface;
    if (!d->focusedSurface) {
131
132
        return;
    }
133
    d->destroyConnection = connect(d->focusedSurface, &SurfaceInterface::aboutToBeDestroyed, this,
Martin Flöser's avatar
Martin Flöser committed
134
        [this] {
135
            Q_D();
136
137
138
            if (d->resource) {
                wl_keyboard_send_leave(d->resource, d->global->display()->nextSerial(), d->focusedSurface->resource());
            }
Martin Flöser's avatar
Martin Flöser committed
139
            d->focusedSurface = nullptr;
140
            d->focusedChildSurface.clear();
Martin Flöser's avatar
Martin Flöser committed
141
142
        }
    );
143
    d->focusedChildSurface = QPointer<SurfaceInterface>(surface);
144

145
    d->sendEnter(d->focusedSurface, serial);
146
    d->client->flush();
147
148
}

Martin Flöser's avatar
Martin Flöser committed
149
void KeyboardInterface::keyPressed(quint32 key, quint32 serial)
150
{
151
    Q_D();
152
153
154
    if (!d->resource) {
        return;
    }
Martin Flöser's avatar
Martin Flöser committed
155
156
    Q_ASSERT(d->focusedSurface);
    wl_keyboard_send_key(d->resource, serial, d->seat->timestamp(), key, WL_KEYBOARD_KEY_STATE_PRESSED);
157
158
}

Martin Flöser's avatar
Martin Flöser committed
159
void KeyboardInterface::keyReleased(quint32 key, quint32 serial)
160
{
161
    Q_D();
162
163
164
    if (!d->resource) {
        return;
    }
Martin Flöser's avatar
Martin Flöser committed
165
166
    Q_ASSERT(d->focusedSurface);
    wl_keyboard_send_key(d->resource, serial, d->seat->timestamp(), key, WL_KEYBOARD_KEY_STATE_RELEASED);
167
168
}

Martin Flöser's avatar
Martin Flöser committed
169
void KeyboardInterface::updateModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group, quint32 serial)
170
{
171
    Q_D();
Martin Flöser's avatar
Martin Flöser committed
172
173
    Q_ASSERT(d->focusedSurface);
    d->sendModifiers(depressed, latched, locked, group, serial);
174
175
}

176
177
178
void KeyboardInterface::repeatInfo(qint32 charactersPerSecond, qint32 delay)
{
    Q_D();
179
180
181
    if (!d->resource) {
        return;
    }
182
    if (wl_resource_get_version(d->resource) < WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
183
184
185
186
187
188
        // only supported since version 4
        return;
    }
    wl_keyboard_send_repeat_info(d->resource, charactersPerSecond, delay);
}

Martin Flöser's avatar
Martin Flöser committed
189
SurfaceInterface *KeyboardInterface::focusedSurface() const
190
{
191
    Q_D();
Martin Flöser's avatar
Martin Flöser committed
192
    return d->focusedSurface;
193
194
}

195
KeyboardInterface::Private *KeyboardInterface::d_func() const
196
{
197
    return reinterpret_cast<Private*>(d.data());
198
199
200
}

}