[kwin_wayland] Add SeatInterface to server module
So far the Seat interface is provided together with pointer and keyboard. As always touch is not yet implemented. The pointer interface is still lacking the set cursor callback. Keyboard on the other hand is complete. Both Keyboard and Pointer have the concept of a focused surface and only to the bound interface belonging to the same client as the focused surface events are sent. The change comes with a set of new auto tests also verifying the client side which wasn't possible before as we couldn't fake events.
This commit is contained in:
parent
71b125e967
commit
9d780deeb2
4 changed files with 877 additions and 0 deletions
|
@ -20,6 +20,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||
#include "display.h"
|
||||
#include "compositor_interface.h"
|
||||
#include "output_interface.h"
|
||||
#include "seat_interface.h"
|
||||
#include "shell_interface.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
|
@ -139,6 +140,13 @@ ShellInterface *Display::createShell(QObject *parent)
|
|||
return shell;
|
||||
}
|
||||
|
||||
SeatInterface *Display::createSeat(QObject *parent)
|
||||
{
|
||||
SeatInterface *seat = new SeatInterface(this, parent);
|
||||
connect(this, &Display::aboutToTerminate, seat, [this,seat] { delete seat; });
|
||||
return seat;
|
||||
}
|
||||
|
||||
void Display::createShm()
|
||||
{
|
||||
Q_ASSERT(m_running);
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace WaylandServer
|
|||
|
||||
class CompositorInterface;
|
||||
class OutputInterface;
|
||||
class SeatInterface;
|
||||
class ShellInterface;
|
||||
|
||||
class Display : public QObject
|
||||
|
@ -72,6 +73,7 @@ public:
|
|||
CompositorInterface *createCompositor(QObject *parent = nullptr);
|
||||
void createShm();
|
||||
ShellInterface *createShell(QObject *parent = nullptr);
|
||||
SeatInterface *createSeat(QObject *parent = nullptr);
|
||||
|
||||
Q_SIGNALS:
|
||||
void socketNameChanged(const QString&);
|
||||
|
|
601
src/wayland/seat_interface.cpp
Normal file
601
src/wayland/seat_interface.cpp
Normal file
|
@ -0,0 +1,601 @@
|
|||
/********************************************************************
|
||||
KWin - the KDE window manager
|
||||
This file is part of the KDE project.
|
||||
|
||||
Copyright (C) 2014 Martin Gräßlin <mgraesslin@kde.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************/
|
||||
#include "seat_interface.h"
|
||||
#include "display.h"
|
||||
#include "surface_interface.h"
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace KWin
|
||||
{
|
||||
|
||||
namespace WaylandServer
|
||||
{
|
||||
|
||||
static const quint32 s_version = 3;
|
||||
|
||||
const struct wl_seat_interface SeatInterface::s_interface = {
|
||||
SeatInterface::getPointerCallback,
|
||||
SeatInterface::getKeyboardCallback,
|
||||
SeatInterface::getTouchCallback
|
||||
};
|
||||
|
||||
SeatInterface::SeatInterface(Display *display, QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_display(display)
|
||||
, m_seat(nullptr)
|
||||
, m_name()
|
||||
, m_pointer(false)
|
||||
, m_keyboard(false)
|
||||
, m_touch(false)
|
||||
, m_pointerInterface(new PointerInterface(display, this))
|
||||
, m_keyboardInterface(new KeyboardInterface(display, this))
|
||||
{
|
||||
connect(this, &SeatInterface::nameChanged, this,
|
||||
[this] {
|
||||
for (auto it = m_resources.constBegin(); it != m_resources.constEnd(); ++it) {
|
||||
sendName(*it);
|
||||
}
|
||||
}
|
||||
);
|
||||
auto sendCapabilitiesAll = [this] {
|
||||
for (auto it = m_resources.constBegin(); it != m_resources.constEnd(); ++it) {
|
||||
sendCapabilities(*it);
|
||||
}
|
||||
};
|
||||
connect(this, &SeatInterface::hasPointerChanged, this, sendCapabilitiesAll);
|
||||
connect(this, &SeatInterface::hasKeyboardChanged, this, sendCapabilitiesAll);
|
||||
connect(this, &SeatInterface::hasTouchChanged, this, sendCapabilitiesAll);
|
||||
}
|
||||
|
||||
SeatInterface::~SeatInterface()
|
||||
{
|
||||
destroy();
|
||||
}
|
||||
|
||||
void SeatInterface::create()
|
||||
{
|
||||
Q_ASSERT(!m_seat);
|
||||
m_seat = wl_global_create(*m_display, &wl_seat_interface, s_version, this, &SeatInterface::bind);
|
||||
}
|
||||
|
||||
void SeatInterface::destroy()
|
||||
{
|
||||
while (!m_resources.isEmpty()) {
|
||||
wl_resource_destroy(m_resources.takeLast());
|
||||
}
|
||||
if (m_seat) {
|
||||
wl_global_destroy(m_seat);
|
||||
m_seat = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void SeatInterface::bind(wl_client *client, void *data, uint32_t version, uint32_t id)
|
||||
{
|
||||
reinterpret_cast<SeatInterface*>(data)->bind(client, version, id);
|
||||
}
|
||||
|
||||
void SeatInterface::bind(wl_client *client, uint32_t version, uint32_t id)
|
||||
{
|
||||
wl_resource *r = wl_resource_create(client, &wl_seat_interface, qMin(s_version, version), id);
|
||||
if (!r) {
|
||||
wl_client_post_no_memory(client);
|
||||
return;
|
||||
}
|
||||
m_resources << r;
|
||||
|
||||
wl_resource_set_implementation(r, &SeatInterface::s_interface, this, SeatInterface::unbind);
|
||||
|
||||
sendCapabilities(r);
|
||||
sendName(r);
|
||||
}
|
||||
|
||||
void SeatInterface::unbind(wl_resource *r)
|
||||
{
|
||||
SeatInterface::cast(r)->m_resources.removeAll(r);
|
||||
}
|
||||
|
||||
void SeatInterface::sendName(wl_resource *r)
|
||||
{
|
||||
if (wl_resource_get_version(r) < WL_SEAT_NAME_SINCE_VERSION) {
|
||||
return;
|
||||
}
|
||||
wl_seat_send_name(r, m_name.toUtf8().constData());
|
||||
}
|
||||
|
||||
void SeatInterface::sendCapabilities(wl_resource *r)
|
||||
{
|
||||
uint32_t capabilities = 0;
|
||||
if (m_pointer) {
|
||||
capabilities |= WL_SEAT_CAPABILITY_POINTER;
|
||||
}
|
||||
if (m_keyboard) {
|
||||
capabilities |= WL_SEAT_CAPABILITY_KEYBOARD;
|
||||
}
|
||||
if (m_touch) {
|
||||
capabilities |= WL_SEAT_CAPABILITY_TOUCH;
|
||||
}
|
||||
wl_seat_send_capabilities(r, capabilities);
|
||||
}
|
||||
|
||||
SeatInterface *SeatInterface::cast(wl_resource *r)
|
||||
{
|
||||
return reinterpret_cast<SeatInterface*>(wl_resource_get_user_data(r));
|
||||
}
|
||||
|
||||
void SeatInterface::setHasKeyboard(bool has)
|
||||
{
|
||||
if (m_keyboard == has) {
|
||||
return;
|
||||
}
|
||||
m_keyboard = has;
|
||||
emit hasKeyboardChanged(m_keyboard);
|
||||
}
|
||||
|
||||
void SeatInterface::setHasPointer(bool has)
|
||||
{
|
||||
if (m_pointer == has) {
|
||||
return;
|
||||
}
|
||||
m_pointer = has;
|
||||
emit hasPointerChanged(m_pointer);
|
||||
}
|
||||
|
||||
void SeatInterface::setHasTouch(bool has)
|
||||
{
|
||||
if (m_touch == has) {
|
||||
return;
|
||||
}
|
||||
m_touch = has;
|
||||
emit hasTouchChanged(m_touch);
|
||||
}
|
||||
|
||||
void SeatInterface::setName(const QString &name)
|
||||
{
|
||||
if (m_name == name) {
|
||||
return;
|
||||
}
|
||||
m_name = name;
|
||||
emit nameChanged(m_name);
|
||||
}
|
||||
|
||||
void SeatInterface::getPointerCallback(wl_client *client, wl_resource *resource, uint32_t id)
|
||||
{
|
||||
SeatInterface::cast(resource)->m_pointerInterface->createInterface(client, resource, id);
|
||||
}
|
||||
|
||||
void SeatInterface::getKeyboardCallback(wl_client *client, wl_resource *resource, uint32_t id)
|
||||
{
|
||||
SeatInterface::cast(resource)->m_keyboardInterface->createInterfae(client, resource, id);
|
||||
}
|
||||
|
||||
void SeatInterface::getTouchCallback(wl_client *client, wl_resource *resource, uint32_t id)
|
||||
{
|
||||
Q_UNUSED(client)
|
||||
Q_UNUSED(resource)
|
||||
Q_UNUSED(id)
|
||||
}
|
||||
|
||||
/****************************************
|
||||
* PointerInterface
|
||||
***************************************/
|
||||
|
||||
const struct wl_pointer_interface PointerInterface::s_interface = {
|
||||
PointerInterface::setCursorCallback,
|
||||
PointerInterface::releaseCallback
|
||||
};
|
||||
|
||||
PointerInterface::PointerInterface(Display *display, SeatInterface *parent)
|
||||
: QObject(parent)
|
||||
, m_display(display)
|
||||
, m_seat(parent)
|
||||
, m_eventTime(0)
|
||||
{
|
||||
}
|
||||
|
||||
PointerInterface::~PointerInterface()
|
||||
{
|
||||
while (!m_resources.isEmpty()) {
|
||||
ResourceData data = m_resources.takeLast();
|
||||
wl_resource_destroy(data.pointer);
|
||||
}
|
||||
}
|
||||
|
||||
void PointerInterface::createInterface(wl_client *client, wl_resource *parentResource, uint32_t id)
|
||||
{
|
||||
wl_resource *p = wl_resource_create(client, &wl_pointer_interface, wl_resource_get_version(parentResource), id);
|
||||
if (!p) {
|
||||
wl_resource_post_no_memory(parentResource);
|
||||
return;
|
||||
}
|
||||
ResourceData data;
|
||||
data.client = client;
|
||||
data.pointer = p;
|
||||
m_resources << data;
|
||||
|
||||
wl_resource_set_implementation(p, &PointerInterface::s_interface, this, PointerInterface::unbind);
|
||||
}
|
||||
|
||||
wl_resource *PointerInterface::pointerForSurface(SurfaceInterface *surface) const
|
||||
{
|
||||
if (!surface) {
|
||||
return nullptr;
|
||||
}
|
||||
for (auto it = m_resources.constBegin(); it != m_resources.constEnd(); ++it) {
|
||||
if ((*it).client == surface->client()) {
|
||||
return (*it).pointer;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void PointerInterface::setFocusedSurface(SurfaceInterface *surface, const QPoint &surfacePosition)
|
||||
{
|
||||
const quint32 serial = m_display->nextSerial();
|
||||
if (m_focusedSurface.surface && m_focusedSurface.pointer) {
|
||||
wl_pointer_send_leave(m_focusedSurface.pointer, serial, m_focusedSurface.surface->surface());
|
||||
disconnect(m_focusedSurface.surface, &QObject::destroyed, this, &PointerInterface::surfaceDeleted);
|
||||
}
|
||||
m_focusedSurface.pointer = pointerForSurface(surface);
|
||||
if (!m_focusedSurface.pointer) {
|
||||
m_focusedSurface = FocusedSurface();
|
||||
return;
|
||||
}
|
||||
m_focusedSurface.surface = surface;
|
||||
m_focusedSurface.offset = surfacePosition;
|
||||
m_focusedSurface.serial = serial;
|
||||
connect(m_focusedSurface.surface, &QObject::destroyed, this, &PointerInterface::surfaceDeleted);
|
||||
|
||||
const QPoint pos = m_globalPos - surfacePosition;
|
||||
wl_pointer_send_enter(m_focusedSurface.pointer, m_focusedSurface.serial,
|
||||
m_focusedSurface.surface->surface(),
|
||||
wl_fixed_from_int(pos.x()), wl_fixed_from_int(pos.y()));
|
||||
}
|
||||
|
||||
void PointerInterface::surfaceDeleted()
|
||||
{
|
||||
m_focusedSurface = FocusedSurface();
|
||||
}
|
||||
|
||||
void PointerInterface::setFocusedSurfacePosition(const QPoint &surfacePosition)
|
||||
{
|
||||
if (!m_focusedSurface.surface) {
|
||||
return;
|
||||
}
|
||||
m_focusedSurface.offset = surfacePosition;
|
||||
}
|
||||
|
||||
void PointerInterface::setGlobalPos(const QPoint &pos)
|
||||
{
|
||||
if (m_globalPos == pos) {
|
||||
return;
|
||||
}
|
||||
m_globalPos = pos;
|
||||
if (m_focusedSurface.surface && m_focusedSurface.pointer) {
|
||||
const QPoint pos = m_globalPos - m_focusedSurface.offset;
|
||||
wl_pointer_send_motion(m_focusedSurface.pointer, m_eventTime,
|
||||
wl_fixed_from_int(pos.x()), wl_fixed_from_int(pos.y()));
|
||||
}
|
||||
emit globalPosChanged(m_globalPos);
|
||||
}
|
||||
|
||||
void PointerInterface::updateTimestamp(quint32 time)
|
||||
{
|
||||
m_eventTime = time;
|
||||
}
|
||||
|
||||
void PointerInterface::buttonPressed(quint32 button)
|
||||
{
|
||||
const quint32 serial = m_display->nextSerial();
|
||||
updateButtonSerial(button, serial);
|
||||
updateButtonState(button, ButtonState::Pressed);
|
||||
if (!m_focusedSurface.surface || !m_focusedSurface.pointer) {
|
||||
return;
|
||||
}
|
||||
wl_pointer_send_button(m_focusedSurface.pointer, serial, m_eventTime, button, WL_POINTER_BUTTON_STATE_PRESSED);
|
||||
}
|
||||
|
||||
void PointerInterface::buttonReleased(quint32 button)
|
||||
{
|
||||
const quint32 serial = m_display->nextSerial();
|
||||
updateButtonSerial(button, serial);
|
||||
updateButtonState(button, ButtonState::Released);
|
||||
if (!m_focusedSurface.surface || !m_focusedSurface.pointer) {
|
||||
return;
|
||||
}
|
||||
wl_pointer_send_button(m_focusedSurface.pointer, serial, m_eventTime, button, WL_POINTER_BUTTON_STATE_RELEASED);
|
||||
}
|
||||
|
||||
void PointerInterface::updateButtonSerial(quint32 button, quint32 serial)
|
||||
{
|
||||
auto it = m_buttonSerials.find(button);
|
||||
if (it == m_buttonSerials.end()) {
|
||||
m_buttonSerials.insert(button, serial);
|
||||
return;
|
||||
}
|
||||
it.value() = serial;
|
||||
}
|
||||
|
||||
quint32 PointerInterface::buttonSerial(quint32 button) const
|
||||
{
|
||||
auto it = m_buttonSerials.constFind(button);
|
||||
if (it == m_buttonSerials.constEnd()) {
|
||||
return 0;
|
||||
}
|
||||
return it.value();
|
||||
}
|
||||
|
||||
void PointerInterface::updateButtonState(quint32 button, PointerInterface::ButtonState state)
|
||||
{
|
||||
auto it = m_buttonStates.find(button);
|
||||
if (it == m_buttonStates.end()) {
|
||||
m_buttonStates.insert(button, state);
|
||||
return;
|
||||
}
|
||||
it.value() = state;
|
||||
}
|
||||
|
||||
bool PointerInterface::isButtonPressed(quint32 button) const
|
||||
{
|
||||
auto it = m_buttonStates.constFind(button);
|
||||
if (it == m_buttonStates.constEnd()) {
|
||||
return false;
|
||||
}
|
||||
return it.value() == ButtonState::Pressed ? true : false;
|
||||
}
|
||||
|
||||
void PointerInterface::axis(Qt::Orientation orientation, quint32 delta)
|
||||
{
|
||||
if (!m_focusedSurface.surface || !m_focusedSurface.pointer) {
|
||||
return;
|
||||
}
|
||||
wl_pointer_send_axis(m_focusedSurface.pointer, m_eventTime,
|
||||
(orientation == Qt::Vertical) ? WL_POINTER_AXIS_VERTICAL_SCROLL : WL_POINTER_AXIS_HORIZONTAL_SCROLL,
|
||||
wl_fixed_from_int(delta));
|
||||
}
|
||||
|
||||
void PointerInterface::unbind(wl_resource *resource)
|
||||
{
|
||||
PointerInterface *p = PointerInterface::cast(resource);
|
||||
auto it = std::find_if(p->m_resources.begin(), p->m_resources.end(),
|
||||
[resource](const ResourceData &data) {
|
||||
return data.pointer == resource;
|
||||
}
|
||||
);
|
||||
if (it == p->m_resources.end()) {
|
||||
return;
|
||||
}
|
||||
if ((*it).pointer == p->m_focusedSurface.pointer) {
|
||||
disconnect(p->m_focusedSurface.surface, &QObject::destroyed, p, &PointerInterface::surfaceDeleted);
|
||||
p->m_focusedSurface = FocusedSurface();
|
||||
}
|
||||
p->m_resources.erase(it);
|
||||
}
|
||||
|
||||
void PointerInterface::setCursorCallback(wl_client *client, wl_resource *resource, uint32_t serial,
|
||||
wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y)
|
||||
{
|
||||
Q_UNUSED(client)
|
||||
Q_UNUSED(resource)
|
||||
Q_UNUSED(serial)
|
||||
Q_UNUSED(surface)
|
||||
Q_UNUSED(hotspot_x)
|
||||
Q_UNUSED(hotspot_y)
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
void PointerInterface::releaseCallback(wl_client *client, wl_resource *resource)
|
||||
{
|
||||
Q_UNUSED(client)
|
||||
unbind(resource);
|
||||
}
|
||||
|
||||
/****************************************
|
||||
* KeyboardInterface
|
||||
***************************************/
|
||||
|
||||
const struct wl_keyboard_interface KeyboardInterface::s_interface {
|
||||
KeyboardInterface::releaseCallback
|
||||
};
|
||||
|
||||
KeyboardInterface::KeyboardInterface(Display *display, SeatInterface *parent)
|
||||
: QObject(parent)
|
||||
, m_display(display)
|
||||
, m_seat(parent)
|
||||
, m_eventTime(0)
|
||||
{
|
||||
}
|
||||
|
||||
KeyboardInterface::~KeyboardInterface()
|
||||
{
|
||||
while (!m_resources.isEmpty()) {
|
||||
ResourceData data = m_resources.takeLast();
|
||||
wl_resource_destroy(data.keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardInterface::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;
|
||||
m_resources << data;
|
||||
|
||||
wl_resource_set_implementation(k, &KeyboardInterface::s_interface, this, KeyboardInterface::unbind);
|
||||
|
||||
sendKeymap(k);
|
||||
}
|
||||
|
||||
void KeyboardInterface::unbind(wl_resource *resource)
|
||||
{
|
||||
KeyboardInterface *k = KeyboardInterface::cast(resource);
|
||||
auto it = std::find_if(k->m_resources.begin(), k->m_resources.end(),
|
||||
[resource](const ResourceData &data) {
|
||||
return data.keyboard == resource;
|
||||
}
|
||||
);
|
||||
if (it == k->m_resources.end()) {
|
||||
return;
|
||||
}
|
||||
if ((*it).keyboard == k->m_focusedSurface.keyboard) {
|
||||
disconnect(k->m_focusedSurface.surface, &QObject::destroyed, k, &KeyboardInterface::surfaceDeleted);
|
||||
k->m_focusedSurface = FocusedSurface();
|
||||
}
|
||||
k->m_resources.erase(it);
|
||||
}
|
||||
|
||||
void KeyboardInterface::surfaceDeleted()
|
||||
{
|
||||
m_focusedSurface = FocusedSurface();
|
||||
}
|
||||
|
||||
void KeyboardInterface::releaseCallback(wl_client *client, wl_resource *resource)
|
||||
{
|
||||
Q_UNUSED(client)
|
||||
unbind(resource);
|
||||
}
|
||||
|
||||
void KeyboardInterface::setKeymap(int fd, quint32 size)
|
||||
{
|
||||
m_keymap.xkbcommonCompatible = true;
|
||||
m_keymap.fd = fd;
|
||||
m_keymap.size = size;
|
||||
sendKeymapToAll();
|
||||
}
|
||||
|
||||
void KeyboardInterface::sendKeymap(wl_resource *r)
|
||||
{
|
||||
if (m_keymap.xkbcommonCompatible) {
|
||||
wl_keyboard_send_keymap(r,
|
||||
WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
|
||||
m_keymap.fd,
|
||||
m_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::sendKeymapToAll()
|
||||
{
|
||||
for (auto it = m_resources.constBegin(); it != m_resources.constEnd(); ++it) {
|
||||
sendKeymap((*it).keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardInterface::sendModifiers(wl_resource* r)
|
||||
{
|
||||
wl_keyboard_send_modifiers(r, m_display->nextSerial(), m_modifiers.depressed, m_modifiers.latched, m_modifiers.locked, m_modifiers.group);
|
||||
}
|
||||
|
||||
void KeyboardInterface::setFocusedSurface(SurfaceInterface *surface)
|
||||
{
|
||||
const quint32 serial = m_display->nextSerial();
|
||||
if (m_focusedSurface.surface && m_focusedSurface.keyboard) {
|
||||
wl_keyboard_send_leave(m_focusedSurface.keyboard, serial, m_focusedSurface.surface->surface());
|
||||
disconnect(m_focusedSurface.surface, &QObject::destroyed, this, &KeyboardInterface::surfaceDeleted);
|
||||
}
|
||||
m_focusedSurface.keyboard = keyboardForSurface(surface);
|
||||
if (!m_focusedSurface.keyboard) {
|
||||
m_focusedSurface = FocusedSurface();
|
||||
return;
|
||||
}
|
||||
m_focusedSurface.surface = surface;
|
||||
connect(m_focusedSurface.surface, &QObject::destroyed, this, &KeyboardInterface::surfaceDeleted);
|
||||
|
||||
wl_array keys;
|
||||
wl_array_init(&keys);
|
||||
for (auto it = m_keyStates.constBegin(); it != m_keyStates.constEnd(); ++it) {
|
||||
if (it.value() == KeyState::Pressed) {
|
||||
continue;
|
||||
}
|
||||
uint32_t *k = reinterpret_cast<uint32_t*>(wl_array_add(&keys, sizeof(uint32_t)));
|
||||
*k = it.key();
|
||||
}
|
||||
wl_keyboard_send_enter(m_focusedSurface.keyboard, serial, m_focusedSurface.surface->surface(), &keys);
|
||||
wl_array_release(&keys);
|
||||
|
||||
sendModifiers(m_focusedSurface.keyboard);
|
||||
}
|
||||
|
||||
wl_resource *KeyboardInterface::keyboardForSurface(SurfaceInterface *surface) const
|
||||
{
|
||||
if (!surface) {
|
||||
return nullptr;
|
||||
}
|
||||
for (auto it = m_resources.constBegin(); it != m_resources.constEnd(); ++it) {
|
||||
if ((*it).client == surface->client()) {
|
||||
return (*it).keyboard;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void KeyboardInterface::keyPressed(quint32 key)
|
||||
{
|
||||
updateKey(key, KeyState::Pressed);
|
||||
if (m_focusedSurface.surface && m_focusedSurface.keyboard) {
|
||||
wl_keyboard_send_key(m_focusedSurface.keyboard, m_display->nextSerial(), m_eventTime, key, WL_KEYBOARD_KEY_STATE_PRESSED);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardInterface::keyReleased(quint32 key)
|
||||
{
|
||||
updateKey(key, KeyState::Released);
|
||||
if (m_focusedSurface.surface && m_focusedSurface.keyboard) {
|
||||
wl_keyboard_send_key(m_focusedSurface.keyboard, m_display->nextSerial(), m_eventTime, key, WL_KEYBOARD_KEY_STATE_RELEASED);
|
||||
}
|
||||
}
|
||||
|
||||
void KeyboardInterface::updateKey(quint32 key, KeyboardInterface::KeyState state)
|
||||
{
|
||||
auto it = m_keyStates.find(key);
|
||||
if (it == m_keyStates.end()) {
|
||||
m_keyStates.insert(key, state);
|
||||
return;
|
||||
}
|
||||
it.value() = state;
|
||||
}
|
||||
|
||||
void KeyboardInterface::updateTimestamp(quint32 time)
|
||||
{
|
||||
m_eventTime = time;
|
||||
}
|
||||
|
||||
void KeyboardInterface::updateModifiers(quint32 depressed, quint32 latched, quint32 locked, quint32 group)
|
||||
{
|
||||
m_modifiers.depressed = depressed;
|
||||
m_modifiers.latched = latched;
|
||||
m_modifiers.locked = locked;
|
||||
m_modifiers.group = group;
|
||||
if (m_focusedSurface.surface && m_focusedSurface.keyboard) {
|
||||
sendModifiers(m_focusedSurface.keyboard);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
266
src/wayland/seat_interface.h
Normal file
266
src/wayland/seat_interface.h
Normal file
|
@ -0,0 +1,266 @@
|
|||
/********************************************************************
|
||||
KWin - the KDE window manager
|
||||
This file is part of the KDE project.
|
||||
|
||||
Copyright (C) 2014 Martin Gräßlin <mgraesslin@kde.org>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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 General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*********************************************************************/
|
||||
#ifndef KWIN_WAYLAND_SERVER_SEAT_INTERFACE_H
|
||||
#define KWIN_WAYLAND_SERVER_SEAT_INTERFACE_H
|
||||
|
||||
#include <QHash>
|
||||
#include <QObject>
|
||||
#include <QPoint>
|
||||
|
||||
#include <wayland-server.h>
|
||||
|
||||
namespace KWin
|
||||
{
|
||||
namespace WaylandServer
|
||||
{
|
||||
|
||||
class Display;
|
||||
class KeyboardInterface;
|
||||
class PointerInterface;
|
||||
class SurfaceInterface;
|
||||
|
||||
class SeatInterface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
|
||||
Q_PROPERTY(bool pointer READ hasPointer WRITE setHasPointer NOTIFY hasPointerChanged)
|
||||
Q_PROPERTY(bool keyboard READ hasKeyboard WRITE setHasKeyboard NOTIFY hasKeyboardChanged)
|
||||
Q_PROPERTY(bool tourch READ hasTouch WRITE setHasTouch NOTIFY hasTouchChanged)
|
||||
public:
|
||||
virtual ~SeatInterface();
|
||||
|
||||
void create();
|
||||
void destroy();
|
||||
bool isValid() const {
|
||||
return m_seat != nullptr;
|
||||
}
|
||||
|
||||
const QString &name() const {
|
||||
return m_name;
|
||||
}
|
||||
bool hasPointer() const {
|
||||
return m_pointer;
|
||||
}
|
||||
bool hasKeyboard() const {
|
||||
return m_keyboard;
|
||||
}
|
||||
bool hasTouch() const {
|
||||
return m_touch;
|
||||
}
|
||||
PointerInterface *pointer() {
|
||||
return m_pointerInterface;
|
||||
}
|
||||
KeyboardInterface *keyboard() {
|
||||
return m_keyboardInterface;
|
||||
}
|
||||
|
||||
void setName(const QString &name);
|
||||
void setHasPointer(bool has);
|
||||
void setHasKeyboard(bool has);
|
||||
void setHasTouch(bool has);
|
||||
|
||||
Q_SIGNALS:
|
||||
void nameChanged(const QString&);
|
||||
void hasPointerChanged(bool);
|
||||
void hasKeyboardChanged(bool);
|
||||
void hasTouchChanged(bool);
|
||||
|
||||
private:
|
||||
friend class Display;
|
||||
explicit SeatInterface(Display *display, QObject *parent);
|
||||
|
||||
static void bind(wl_client *client, void *data, uint32_t version, uint32_t id);
|
||||
static void unbind(wl_resource *r);
|
||||
|
||||
// interface
|
||||
static void getPointerCallback(wl_client *client, wl_resource *resource, uint32_t id);
|
||||
static void getKeyboardCallback(wl_client *client, wl_resource *resource, uint32_t id);
|
||||
static void getTouchCallback(wl_client *client, wl_resource *resource, uint32_t id);
|
||||
|
||||
static SeatInterface *cast(wl_resource *r);
|
||||
void bind(wl_client *client, uint32_t version, uint32_t id);
|
||||
void sendCapabilities(wl_resource *r);
|
||||
void sendName(wl_resource *r);
|
||||
|
||||
Display *m_display;
|
||||
wl_global *m_seat;
|
||||
QString m_name;
|
||||
bool m_pointer;
|
||||
bool m_keyboard;
|
||||
bool m_touch;
|
||||
QList<wl_resource*> m_resources;
|
||||
PointerInterface *m_pointerInterface;
|
||||
KeyboardInterface *m_keyboardInterface;
|
||||
static const struct wl_seat_interface s_interface;
|
||||
};
|
||||
|
||||
class PointerInterface : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QPoint globalPos READ globalPos WRITE setGlobalPos NOTIFY globalPosChanged)
|
||||
public:
|
||||
virtual ~PointerInterface();
|
||||
|
||||
void createInterface(wl_client *client, wl_resource *parentResource, uint32_t id);
|
||||
|
||||
void updateTimestamp(quint32 time);
|
||||
void setGlobalPos(const QPoint &pos);
|
||||
const QPoint &globalPos() const {
|
||||
return m_globalPos;
|
||||
}
|
||||
void buttonPressed(quint32 button);
|
||||
void buttonReleased(quint32 button);
|
||||
bool isButtonPressed(quint32 button) const;
|
||||
quint32 buttonSerial(quint32 button) const;
|
||||
void axis(Qt::Orientation orientation, quint32 delta);
|
||||
|
||||
void setFocusedSurface(SurfaceInterface *surface, const QPoint &surfacePosition = QPoint());
|
||||
void setFocusedSurfacePosition(const QPoint &surfacePosition);
|
||||
SurfaceInterface *focusedSurface() const {
|
||||
return m_focusedSurface.surface;
|
||||
}
|
||||
const QPoint &focusedSurfacePosition() const {
|
||||
return m_focusedSurface.offset;
|
||||
}
|
||||
|
||||
Q_SIGNALS:
|
||||
void globalPosChanged(const QPoint &pos);
|
||||
|
||||
private:
|
||||
friend class SeatInterface;
|
||||
explicit PointerInterface(Display *display, SeatInterface *parent);
|
||||
wl_resource *pointerForSurface(SurfaceInterface *surface) const;
|
||||
void surfaceDeleted();
|
||||
void updateButtonSerial(quint32 button, quint32 serial);
|
||||
enum class ButtonState {
|
||||
Released,
|
||||
Pressed
|
||||
};
|
||||
void updateButtonState(quint32 button, ButtonState state);
|
||||
|
||||
static PointerInterface *cast(wl_resource *resource) {
|
||||
return reinterpret_cast<PointerInterface*>(wl_resource_get_user_data(resource));
|
||||
}
|
||||
|
||||
static void unbind(wl_resource *resource);
|
||||
// interface
|
||||
static void setCursorCallback(wl_client *client, wl_resource *resource, uint32_t serial,
|
||||
wl_resource *surface, int32_t hotspot_x, int32_t hotspot_y);
|
||||
// since version 3
|
||||
static void releaseCallback(wl_client *client, wl_resource *resource);
|
||||
|
||||
Display *m_display;
|
||||
SeatInterface *m_seat;
|
||||
struct ResourceData {
|
||||
wl_client *client = nullptr;
|
||||
wl_resource *pointer = nullptr;
|
||||
};
|
||||
QList<ResourceData> m_resources;
|
||||
quint32 m_eventTime;
|
||||
QPoint m_globalPos;
|
||||
struct FocusedSurface {
|
||||
SurfaceInterface *surface = nullptr;
|
||||
QPoint offset = QPoint();
|
||||
wl_resource *pointer = nullptr;
|
||||
quint32 serial = 0;
|
||||
};
|
||||
FocusedSurface m_focusedSurface;
|
||||
QHash<quint32, quint32> m_buttonSerials;
|
||||
QHash<quint32, ButtonState> m_buttonStates;
|
||||
|
||||
static const struct wl_pointer_interface s_interface;
|
||||
};
|
||||
|
||||
class 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 {
|
||||
return m_focusedSurface.surface;
|
||||
}
|
||||
|
||||
private:
|
||||
friend class SeatInterface;
|
||||
explicit KeyboardInterface(Display *display, SeatInterface *parent);
|
||||
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);
|
||||
|
||||
static KeyboardInterface *cast(wl_resource *resource) {
|
||||
return reinterpret_cast<KeyboardInterface*>(wl_resource_get_user_data(resource));
|
||||
}
|
||||
|
||||
static void unbind(wl_resource *resource);
|
||||
// since version 3
|
||||
static void releaseCallback(wl_client *client, wl_resource *resource);
|
||||
|
||||
Display *m_display;
|
||||
SeatInterface *m_seat;
|
||||
struct ResourceData {
|
||||
wl_client *client = nullptr;
|
||||
wl_resource *keyboard = nullptr;
|
||||
};
|
||||
QList<ResourceData> m_resources;
|
||||
struct Keymap {
|
||||
int fd = -1;
|
||||
quint32 size = 0;
|
||||
bool xkbcommonCompatible = false;
|
||||
};
|
||||
Keymap m_keymap;
|
||||
struct Modifiers {
|
||||
quint32 depressed = 0;
|
||||
quint32 latched = 0;
|
||||
quint32 locked = 0;
|
||||
quint32 group = 0;
|
||||
};
|
||||
Modifiers m_modifiers;
|
||||
struct FocusedSurface {
|
||||
SurfaceInterface *surface = nullptr;
|
||||
wl_resource *keyboard = nullptr;
|
||||
};
|
||||
FocusedSurface m_focusedSurface;
|
||||
QHash<quint32, KeyState> m_keyStates;
|
||||
quint32 m_eventTime;
|
||||
|
||||
static const struct wl_keyboard_interface s_interface;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue