Commit 59ae02f0 authored by Martin Flöser's avatar Martin Flöser
Browse files

[server] Move KeyboardInterface into dedicated .h and .cpp

parent 76935b97
......@@ -32,6 +32,7 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#include "../../src/server/buffer_interface.h"
#include "../../src/server/compositor_interface.h"
#include "../../src/server/display.h"
#include "../../src/server/keyboard_interface.h"
#include "../../src/server/seat_interface.h"
#include "../../src/server/surface_interface.h"
// Wayland
......
......@@ -8,6 +8,7 @@ set(SERVER_LIB_SRCS
datasource_interface.cpp
display.cpp
global.cpp
keyboard_interface.cpp
output_interface.cpp
region_interface.cpp
resource.cpp
......@@ -59,6 +60,7 @@ set_target_properties(KF5WaylandServer PROPERTIES VERSION ${KWAYLAND_VERSION_S
# datasource_interface.h
# display.h
# global.h
# keyboard_interface.h
# output_interface.h
# region_interface.h
# resource.h
......
/********************************************************************
Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "keyboard_interface.h"
#include "display.h"
#include "seat_interface.h"
#include "surface_interface.h"
// Qt
#include <QHash>
// System
#include <fcntl.h>
#include <unistd.h>
// Wayland
#include <wayland-server.h>
namespace KWayland
{
namespace Server
{
class KeyboardInterface::Private
{
public:
Private(Display *d, SeatInterface *s);
void createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id);
void surfaceDeleted();
wl_resource *keyboardForSurface(SurfaceInterface *surface) const;
void sendKeymap(wl_resource *r);
void sendKeymapToAll();
void sendModifiers(wl_resource *r);
enum class KeyState {
Released,
Pressed
};
void updateKey(quint32 key, KeyState state);
Display *display;
SeatInterface *seat;
struct ResourceData {
wl_client *client = nullptr;
wl_resource *keyboard = nullptr;
};
QList<ResourceData> resources;
struct Keymap {
int fd = -1;
quint32 size = 0;
bool xkbcommonCompatible = false;
};
Keymap keymap;
struct Modifiers {
quint32 depressed = 0;
quint32 latched = 0;
quint32 locked = 0;
quint32 group = 0;
};
Modifiers modifiers;
struct FocusedSurface {
SurfaceInterface *surface = nullptr;
wl_resource *keyboard = nullptr;
};
FocusedSurface focusedSurface;
QHash<quint32, KeyState> keyStates;
quint32 eventTime = 0;
QMetaObject::Connection destroyConnection;
private:
static Private *cast(wl_resource *resource) {
return reinterpret_cast<KeyboardInterface::Private*>(wl_resource_get_user_data(resource));
}
static void unbind(wl_resource *resource);
// since version 3
static void releaseCallback(wl_client *client, wl_resource *resource);
static const struct wl_keyboard_interface s_interface;
};
KeyboardInterface::Private::Private(Display *d, SeatInterface *s)
: display(d)
, seat(s)
{
}
const struct wl_keyboard_interface KeyboardInterface::Private::s_interface {
releaseCallback
};
KeyboardInterface::KeyboardInterface(Display *display, SeatInterface *parent)
: QObject(parent)
, d(new Private(display, parent))
{
}
KeyboardInterface::~KeyboardInterface()
{
while (!d->resources.isEmpty()) {
auto data = d->resources.takeLast();
wl_resource_destroy(data.keyboard);
}
}
void KeyboardInterface::createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id)
{
d->createInterfae(client, parentResource, id);
}
void KeyboardInterface::Private::createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id)
{
wl_resource *k = wl_resource_create(client, &wl_keyboard_interface, wl_resource_get_version(parentResource), id);
if (!k) {
wl_resource_post_no_memory(parentResource);
return;
}
ResourceData data;
data.client = client;
data.keyboard = k;
resources << data;
wl_resource_set_implementation(k, &s_interface, this, unbind);
sendKeymap(k);
}
void KeyboardInterface::Private::unbind(wl_resource *resource)
{
auto k = cast(resource);
auto it = std::find_if(k->resources.begin(), k->resources.end(),
[resource](const ResourceData &data) {
return data.keyboard == resource;
}
);
if (it == k->resources.end()) {
return;
}
if ((*it).keyboard == k->focusedSurface.keyboard) {
QObject::disconnect(k->destroyConnection);
k->focusedSurface = FocusedSurface();
}
k->resources.erase(it);
}
void KeyboardInterface::Private::surfaceDeleted()
{
focusedSurface = FocusedSurface();
}
void KeyboardInterface::Private::releaseCallback(wl_client *client, wl_resource *resource)
{
Q_UNUSED(client)
unbind(resource);
}
void KeyboardInterface::setKeymap(int fd, quint32 size)
{
d->keymap.xkbcommonCompatible = true;
d->keymap.fd = fd;
d->keymap.size = size;
d->sendKeymapToAll();
}
void KeyboardInterface::Private::sendKeymap(wl_resource *r)
{
if (keymap.xkbcommonCompatible) {
wl_keyboard_send_keymap(r,
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
keymap.fd,
keymap.size);
} else {
int nullFd = open("/dev/null", O_RDONLY);
wl_keyboard_send_keymap(r, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP, nullFd, 0);
close(nullFd);
}
}
void KeyboardInterface::Private::sendKeymapToAll()
{
for (auto it = resources.constBegin(); it != resources.constEnd(); ++it) {
sendKeymap((*it).keyboard);
}
}
void KeyboardInterface::Private::sendModifiers(wl_resource* r)
{
wl_keyboard_send_modifiers(r, display->nextSerial(), modifiers.depressed, modifiers.latched, modifiers.locked, modifiers.group);
}
void KeyboardInterface::setFocusedSurface(SurfaceInterface *surface)
{
const quint32 serial = d->display->nextSerial();
if (d->focusedSurface.surface && d->focusedSurface.keyboard) {
wl_keyboard_send_leave(d->focusedSurface.keyboard, serial, d->focusedSurface.surface->resource());
disconnect(d->destroyConnection);
}
d->focusedSurface.keyboard = d->keyboardForSurface(surface);
if (!d->focusedSurface.keyboard) {
d->focusedSurface = Private::FocusedSurface();
return;
}
d->focusedSurface.surface = surface;
d->destroyConnection = connect(d->focusedSurface.surface, &QObject::destroyed, this, [this] { d->surfaceDeleted(); });
wl_array keys;
wl_array_init(&keys);
for (auto it = d->keyStates.constBegin(); it != d->keyStates.constEnd(); ++it) {
if (it.value() == Private::KeyState::Pressed) {
continue;
}
uint32_t *k = reinterpret_cast<uint32_t*>(wl_array_add(&keys, sizeof(uint32_t)));
*k = it.key();
}
wl_keyboard_send_enter(d->focusedSurface.keyboard, serial, d->focusedSurface.surface->resource(), &keys);
wl_array_release(&keys);
d->sendModifiers(d->focusedSurface.keyboard);
}
wl_resource *KeyboardInterface::Private::keyboardForSurface(SurfaceInterface *surface) const
{
if (!surface) {
return nullptr;
}
for (auto it = resources.constBegin(); it != resources.constEnd(); ++it) {
if ((*it).client == *surface->client()) {
return (*it).keyboard;
}
}
return nullptr;
}
void KeyboardInterface::keyPressed(quint32 key)
{
d->updateKey(key, Private::KeyState::Pressed);
if (d->focusedSurface.surface && d->focusedSurface.keyboard) {
wl_keyboard_send_key(d->focusedSurface.keyboard, d->display->nextSerial(), d->eventTime, key, WL_KEYBOARD_KEY_STATE_PRESSED);
}
}
void KeyboardInterface::keyReleased(quint32 key)
{
d->updateKey(key, Private::KeyState::Released);
if (d->focusedSurface.surface && d->focusedSurface.keyboard) {
wl_keyboard_send_key(d->focusedSurface.keyboard, d->display->nextSerial(), d->eventTime, key, WL_KEYBOARD_KEY_STATE_RELEASED);
}
}
void KeyboardInterface::Private::updateKey(quint32 key, KeyState state)
{
auto it = keyStates.find(key);
if (it == keyStates.end()) {
keyStates.insert(key, state);
return;
}
it.value() = state;
}
void KeyboardInterface::updateTimestamp(quint32 time)
{
d->eventTime = time;
}
void KeyboardInterface::updateModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group)
{
d->modifiers.depressed = depressed;
d->modifiers.latched = latched;
d->modifiers.locked = locked;
d->modifiers.group = group;
if (d->focusedSurface.surface && d->focusedSurface.keyboard) {
d->sendModifiers(d->focusedSurface.keyboard);
}
}
SurfaceInterface *KeyboardInterface::focusedSurface() const
{
return d->focusedSurface.surface;
}
}
}
/********************************************************************
Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#ifndef WAYLAND_SERVER_KEYBOARD_INTERFACE_H
#define WAYLAND_SERVER_KEYBOARD_INTERFACE_H
#include <QObject>
#include <QPoint>
#include <KWayland/Server/kwaylandserver_export.h>
struct wl_client;
struct wl_resource;
namespace KWayland
{
namespace Server
{
class Display;
class SeatInterface;
class SurfaceInterface;
class KWAYLANDSERVER_EXPORT KeyboardInterface : public QObject
{
Q_OBJECT
public:
virtual ~KeyboardInterface();
void createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id);
void updateTimestamp(quint32 time);
void setKeymap(int fd, quint32 size);
void keyPressed(quint32 key);
void keyReleased(quint32 key);
void updateModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group);
void setFocusedSurface(SurfaceInterface *surface);
SurfaceInterface *focusedSurface() const;
private:
friend class SeatInterface;
explicit KeyboardInterface(Display *display, SeatInterface *parent);
class Private;
QScopedPointer<Private> d;
};
}
}
#endif
......@@ -20,12 +20,10 @@ License along with this library. If not, see <http://www.gnu.org/licenses/>.
#include "seat_interface.h"
#include "global_p.h"
#include "display.h"
#include "keyboard_interface.h"
#include "surface_interface.h"
// Qt
#include <QHash>
// System
#include <fcntl.h>
#include <unistd.h>
// Wayland
#include <wayland-server.h>
#ifndef WL_SEAT_NAME_SINCE_VERSION
......@@ -614,265 +612,5 @@ QPoint PointerInterface::focusedSurfacePosition() const
return d->focusedSurface.offset;
}
/****************************************
* KeyboardInterface
***************************************/
class KeyboardInterface::Private
{
public:
Private(Display *d, SeatInterface *s);
void createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id);
void surfaceDeleted();
wl_resource *keyboardForSurface(SurfaceInterface *surface) const;
void sendKeymap(wl_resource *r);
void sendKeymapToAll();
void sendModifiers(wl_resource *r);
enum class KeyState {
Released,
Pressed
};
void updateKey(quint32 key, KeyState state);
Display *display;
SeatInterface *seat;
struct ResourceData {
wl_client *client = nullptr;
wl_resource *keyboard = nullptr;
};
QList<ResourceData> resources;
struct Keymap {
int fd = -1;
quint32 size = 0;
bool xkbcommonCompatible = false;
};
Keymap keymap;
struct Modifiers {
quint32 depressed = 0;
quint32 latched = 0;
quint32 locked = 0;
quint32 group = 0;
};
Modifiers modifiers;
struct FocusedSurface {
SurfaceInterface *surface = nullptr;
wl_resource *keyboard = nullptr;
};
FocusedSurface focusedSurface;
QHash<quint32, KeyState> keyStates;
quint32 eventTime = 0;
QMetaObject::Connection destroyConnection;
private:
static Private *cast(wl_resource *resource) {
return reinterpret_cast<KeyboardInterface::Private*>(wl_resource_get_user_data(resource));
}
static void unbind(wl_resource *resource);
// since version 3
static void releaseCallback(wl_client *client, wl_resource *resource);
static const struct wl_keyboard_interface s_interface;
};
KeyboardInterface::Private::Private(Display *d, SeatInterface *s)
: display(d)
, seat(s)
{
}
const struct wl_keyboard_interface KeyboardInterface::Private::s_interface {
releaseCallback
};
KeyboardInterface::KeyboardInterface(Display *display, SeatInterface *parent)
: QObject(parent)
, d(new Private(display, parent))
{
}
KeyboardInterface::~KeyboardInterface()
{
while (!d->resources.isEmpty()) {
auto data = d->resources.takeLast();
wl_resource_destroy(data.keyboard);
}
}
void KeyboardInterface::createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id)
{
d->createInterfae(client, parentResource, id);
}
void KeyboardInterface::Private::createInterfae(wl_client *client, wl_resource *parentResource, uint32_t id)
{
wl_resource *k = wl_resource_create(client, &wl_keyboard_interface, wl_resource_get_version(parentResource), id);
if (!k) {
wl_resource_post_no_memory(parentResource);
return;
}
ResourceData data;
data.client = client;
data.keyboard = k;
resources << data;
wl_resource_set_implementation(k, &s_interface, this, unbind);
sendKeymap(k);
}
void KeyboardInterface::Private::unbind(wl_resource *resource)
{
auto k = cast(resource);
auto it = std::find_if(k->resources.begin(), k->resources.end(),
[resource](const ResourceData &data) {
return data.keyboard == resource;
}
);
if (it == k->resources.end()) {
return;
}
if ((*it).keyboard == k->focusedSurface.keyboard) {
QObject::disconnect(k->destroyConnection);
k->focusedSurface = FocusedSurface();
}
k->resources.erase(it);
}
void KeyboardInterface::Private::surfaceDeleted()
{
focusedSurface = FocusedSurface();
}
void KeyboardInterface::Private::releaseCallback(wl_client *client, wl_resource *resource)
{
Q_UNUSED(client)
unbind(resource);
}
void KeyboardInterface::setKeymap(int fd, quint32 size)
{
d->keymap.xkbcommonCompatible = true;
d->keymap.fd = fd;
d->keymap.size = size;
d->sendKeymapToAll();
}
void KeyboardInterface::Private::sendKeymap(wl_resource *r)
{
if (keymap.xkbcommonCompatible) {
wl_keyboard_send_keymap(r,
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
keymap.fd,
keymap.size);
} else {
int nullFd = open("/dev/null", O_RDONLY);
wl_keyboard_send_keymap(r, WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP, nullFd, 0);
close(nullFd);
}
}
void KeyboardInterface::Private::sendKeymapToAll()
{
for (auto it = resources.constBegin(); it != resources.constEnd(); ++it) {
sendKeymap((*it).keyboard);
}
}
void KeyboardInterface::Private::sendModifiers(wl_resource* r)
{
wl_keyboard_send_modifiers(r, display->nextSerial(), modifiers.depressed, modifiers.latched, modifiers.locked, modifiers.group);
}
void KeyboardInterface::setFocusedSurface(SurfaceInterface *surface)
{
const quint32 serial = d->display->nextSerial();
if (d->focusedSurface.surface && d->focusedSurface.keyboard) {
wl_keyboard_send_leave(d->focusedSurface.keyboard, serial, d->focusedSurface.surface->resource());
disconnect(d->destroyConnection);
}
d->focusedSurface.keyboard = d->keyboardForSurface(surface);
if (!d->focusedSurface.keyboard) {
d->focusedSurface = Private::FocusedSurface();
return;
}
d->focusedSurface.surface = surface;
d->destroyConnection = connect(d->focusedSurface.surface, &QObject::destroyed, this, [this] { d->surfaceDeleted(); });
wl_array keys;
wl_array_init(&keys);
for (auto it = d->keyStates.constBegin(); it != d->keyStates.constEnd(); ++it) {
if (it.value() == Private::KeyState::Pressed) {
continue;
}
uint32_t *k = reinterpret_cast<uint32_t*>(wl_array_add(&keys, sizeof(uint32_t)));
*k = it.key();
}
wl_keyboard_send_enter(d->focusedSurface.keyboard, serial, d->focusedSurface.surface->resource(), &keys);
wl_array_release(&keys);
d->sendModifiers(d->focusedSurface.keyboard);
}
wl_resource *KeyboardInterface::Private::keyboardForSurface(SurfaceInterface *surface) const
{
if (!surface) {
return nullptr;