6 #include "keyboard_interface.h"
8 #include "keyboard_interface_p.h"
9 #include "seat_interface.h"
10 #include "surface_interface.h"
12 #include <QTemporaryFile>
15 #include <wayland-server.h>
23 KeyboardInterface::Private::Private(SeatInterface *s, wl_resource *parentResource, KeyboardInterface *q)
24 :
Resource::Private(q, s, parentResource, &wl_keyboard_interface, &s_interface)
31 if (focusedChildSurface == childSurface) {
34 sendLeave(focusedChildSurface.data(), serial);
35 focusedChildSurface = childSurface;
36 sendEnter(focusedChildSurface.data(), serial);
39 void KeyboardInterface::Private::sendLeave(SurfaceInterface *surface, quint32 serial)
41 if (surface && resource && surface->resource()) {
42 wl_keyboard_send_leave(resource, serial, surface->resource());
46 void KeyboardInterface::Private::sendEnter(SurfaceInterface *surface, quint32 serial)
50 const auto states = seat->pressedKeys();
51 for (
auto it = states.constBegin(); it != states.constEnd(); ++it) {
52 uint32_t *k =
reinterpret_cast<uint32_t *
>(wl_array_add(&keys,
sizeof(uint32_t)));
55 wl_keyboard_send_enter(resource, serial, surface->resource(), &keys);
56 wl_array_release(&keys);
62 const struct wl_keyboard_interface KeyboardInterface::Private::s_interface {
63 resourceDestroyedCallback
67 KeyboardInterface::KeyboardInterface(SeatInterface *parent, wl_resource *parentResource)
68 :
Resource(new Private(parent, parentResource, this))
72 KeyboardInterface::~KeyboardInterface() =
default;
74 void KeyboardInterface::setKeymap(
int fd, quint32 size)
77 d->sendKeymap(fd, size);
80 void KeyboardInterface::setKeymap(
const QByteArray &content)
86 unlink(tmp->fileName().toUtf8().constData());
87 if (!tmp->resize(content.
size())) {
94 if (qstrncpy(
reinterpret_cast<char *
>(address), content.
constData(), content.
size() + 1) ==
nullptr) {
99 d->sendKeymap(tmp->handle(), content.
size());
103 void KeyboardInterface::Private::sendKeymap(
int fd, quint32 size)
108 wl_keyboard_send_keymap(resource, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, fd, size);
111 void KeyboardInterface::Private::sendModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group, quint32 serial)
116 wl_keyboard_send_modifiers(resource, serial, depressed, latched, locked, group);
119 void KeyboardInterface::Private::sendModifiers()
121 sendModifiers(seat->depressedModifiers(), seat->latchedModifiers(), seat->lockedModifiers(), seat->groupModifiers(), seat->lastModifiersSerial());
124 void KeyboardInterface::setFocusedSurface(SurfaceInterface *surface, quint32 serial)
127 d->sendLeave(d->focusedChildSurface, serial);
128 disconnect(d->destroyConnection);
129 d->focusedChildSurface.clear();
130 d->focusedSurface = surface;
131 if (!d->focusedSurface) {
134 d->destroyConnection = connect(d->focusedSurface, &Resource::aboutToBeUnbound,
this, [
this] {
137 wl_keyboard_send_leave(d->resource, d->global->display()->nextSerial(), d->focusedSurface->resource());
139 d->focusedSurface =
nullptr;
140 d->focusedChildSurface.clear();
144 d->sendEnter(d->focusedSurface, serial);
148 void KeyboardInterface::keyPressed(quint32 key, quint32 serial)
154 Q_ASSERT(d->focusedSurface);
155 wl_keyboard_send_key(d->resource, serial, d->seat->timestamp(), key, WL_KEYBOARD_KEY_STATE_PRESSED);
158 void KeyboardInterface::keyReleased(quint32 key, quint32 serial)
164 Q_ASSERT(d->focusedSurface);
165 wl_keyboard_send_key(d->resource, serial, d->seat->timestamp(), key, WL_KEYBOARD_KEY_STATE_RELEASED);
168 void KeyboardInterface::updateModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group, quint32 serial)
171 Q_ASSERT(d->focusedSurface);
172 d->sendModifiers(depressed, latched, locked, group, serial);
175 void KeyboardInterface::repeatInfo(qint32 charactersPerSecond, qint32 delay)
181 if (wl_resource_get_version(d->resource) < WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) {
185 wl_keyboard_send_repeat_info(d->resource, charactersPerSecond, delay);
191 return d->focusedSurface;
194 KeyboardInterface::Private *KeyboardInterface::d_func()
const
196 return reinterpret_cast<Private *
>(d.data());