1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2013 Martin Gräßlin <mgraesslin@kde.org>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
*********************************************************************/
21
#include "wayland_backend.h"
26
#include "scene_qpainter.h"
27
#include "screens_wayland.h"
29
#include "wayland_server.h"
31
#include "egl_wayland_backend.h"
33
#include <KWayland/Client/buffer.h>
34
#include <KWayland/Client/compositor.h>
35
#include <KWayland/Client/connection_thread.h>
36
#include <KWayland/Client/event_queue.h>
37
#include <KWayland/Client/fullscreen_shell.h>
38
#include <KWayland/Client/keyboard.h>
39
#include <KWayland/Client/output.h>
40
#include <KWayland/Client/pointer.h>
41
#include <KWayland/Client/region.h>
42
#include <KWayland/Client/registry.h>
43
#include <KWayland/Client/seat.h>
44
#include <KWayland/Client/shell.h>
45
#include <KWayland/Client/shm_pool.h>
46
#include <KWayland/Client/subcompositor.h>
47
#include <KWayland/Client/subsurface.h>
48
#include <KWayland/Client/surface.h>
49
#include <KWayland/Client/touch.h>
50
#include <KWayland/Server/buffer_interface.h>
51
#include <KWayland/Server/seat_interface.h>
52
#include <KWayland/Server/surface_interface.h>
55
#include <QMetaMethod>
58
#if HAVE_WAYLAND_CURSOR
59
#include <wayland-cursor.h>
67
using namespace KWayland::Client;
69
WaylandSeat::WaylandSeat(wl_seat *seat, WaylandBackend *backend)
71
, m_seat(new Seat(this))
76
#if HAVE_WAYLAND_CURSOR
77
, m_theme(new WaylandCursorTheme(backend->shmPool(), this))
81
, m_installCursor(false)
84
connect(m_seat, &Seat::hasKeyboardChanged, this,
85
[this](bool hasKeyboard) {
87
m_keyboard = m_seat->createKeyboard(this);
88
connect(m_keyboard, &Keyboard::keyChanged, this,
89
[this](quint32 key, Keyboard::KeyState state, quint32 time) {
90
auto toState = [state] {
92
case Keyboard::KeyState::Pressed:
93
return InputRedirection::KeyboardKeyPressed;
94
case Keyboard::KeyState::Released:
95
return InputRedirection::KeyboardKeyReleased;
99
input()->processKeyboardKey(key, toState(), time);
102
connect(m_keyboard, &Keyboard::modifiersChanged, this,
103
[this](quint32 depressed, quint32 latched, quint32 locked, quint32 group) {
104
input()->processKeyboardModifiers(depressed, latched, locked, group);
107
connect(m_keyboard, &Keyboard::keymapChanged, this,
108
[this](int fd, quint32 size) {
109
input()->processKeymapChange(fd, size);
117
connect(m_seat, &Seat::hasPointerChanged, this,
118
[this](bool hasPointer) {
119
if (hasPointer && !m_pointer) {
120
m_pointer = m_seat->createPointer(this);
121
connect(m_pointer, &Pointer::entered, this,
122
[this](quint32 serial) {
123
m_enteredSerial = serial;
124
if (!m_installCursor) {
125
// explicitly hide cursor
126
m_pointer->hideCursor();
130
connect(m_pointer, &Pointer::motion, this,
131
[this](const QPointF &relativeToSurface, quint32 time) {
132
input()->processPointerMotion(relativeToSurface.toPoint(), time);
135
connect(m_pointer, &Pointer::buttonStateChanged, this,
136
[this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState state) {
138
auto toState = [state] {
140
case Pointer::ButtonState::Pressed:
141
return InputRedirection::PointerButtonPressed;
142
case Pointer::ButtonState::Released:
143
return InputRedirection::PointerButtonReleased;
147
input()->processPointerButton(button, toState(), time);
150
connect(m_pointer, &Pointer::axisChanged, this,
151
[this](quint32 time, Pointer::Axis axis, qreal delta) {
152
auto toAxis = [axis] {
154
case Pointer::Axis::Horizontal:
155
return InputRedirection::PointerAxisHorizontal;
156
case Pointer::Axis::Vertical:
157
return InputRedirection::PointerAxisVertical;
161
input()->processPointerAxis(toAxis(), delta, time);
169
connect(m_seat, &Seat::hasTouchChanged,
170
[this] (bool hasTouch) {
171
if (hasTouch && !m_touch) {
172
m_touch = m_seat->createTouch(this);
173
connect(m_touch, &Touch::sequenceCanceled, input(), &InputRedirection::cancelTouch);
174
connect(m_touch, &Touch::frameEnded, input(), &InputRedirection::touchFrame);
175
connect(m_touch, &Touch::sequenceStarted, this,
176
[] (TouchPoint *tp) {
177
input()->processTouchDown(tp->id(), tp->position(), tp->time());
180
connect(m_touch, &Touch::pointAdded, this,
181
[] (TouchPoint *tp) {
182
input()->processTouchDown(tp->id(), tp->position(), tp->time());
185
connect(m_touch, &Touch::pointRemoved, this,
186
[] (TouchPoint *tp) {
187
input()->processTouchUp(tp->id(), tp->time());
190
connect(m_touch, &Touch::pointMoved, this,
191
[] (TouchPoint *tp) {
192
input()->processTouchMotion(tp->id(), tp->position(), tp->time());
200
WaylandServer *server = waylandServer();
202
using namespace KWayland::Server;
203
SeatInterface *si = server->seat();
204
connect(m_seat, &Seat::hasKeyboardChanged, si, &SeatInterface::setHasKeyboard);
205
connect(m_seat, &Seat::hasPointerChanged, si, &SeatInterface::setHasPointer);
206
connect(m_seat, &Seat::hasTouchChanged, si, &SeatInterface::setHasTouch);
207
connect(m_seat, &Seat::nameChanged, si, &SeatInterface::setName);
211
WaylandSeat::~WaylandSeat()
218
void WaylandSeat::destroyPointer()
224
void WaylandSeat::destroyKeyboard()
227
m_keyboard = nullptr;
230
void WaylandSeat::destroyTouch()
236
void WaylandSeat::installCursorImage(wl_buffer *image, const QSize &size, const QPoint &hotSpot)
238
if (!m_installCursor) {
241
if (!m_pointer || !m_pointer->isValid()) {
245
m_cursor = m_backend->compositor()->createSurface(this);
247
if (!m_cursor || !m_cursor->isValid()) {
250
m_pointer->setCursor(m_cursor, hotSpot);
251
m_cursor->attachBuffer(image);
252
m_cursor->damage(QRect(QPoint(0,0), size));
253
m_cursor->commit(Surface::CommitFlag::None);
256
void WaylandSeat::installCursorImage(Qt::CursorShape shape)
258
#if HAVE_WAYLAND_CURSOR
259
wl_cursor_image *image = m_theme->get(shape);
263
installCursorImage(wl_cursor_image_get_buffer(image),
264
QSize(image->width, image->height),
265
QPoint(image->hotspot_x, image->hotspot_y));
269
void WaylandSeat::installCursorImage(const QImage &image, const QPoint &hotSpot)
271
installCursorImage(*(m_backend->shmPool()->createBuffer(image).data()), image.size(), hotSpot);
274
void WaylandSeat::setInstallCursor(bool install)
276
// TODO: remove, add?
277
m_installCursor = install;
280
#if HAVE_WAYLAND_CURSOR
281
WaylandCursorTheme::WaylandCursorTheme(KWayland::Client::ShmPool *shm, QObject *parent)
288
WaylandCursorTheme::~WaylandCursorTheme()
293
void WaylandCursorTheme::loadTheme()
295
if (!m_shm->isValid()) {
298
Cursor *c = Cursor::self();
300
// so far the theme had not been created, this means we need to start tracking theme changes
301
connect(c, &Cursor::themeChanged, this, &WaylandCursorTheme::loadTheme);
305
m_theme = wl_cursor_theme_load(c->themeName().toUtf8().constData(),
306
c->themeSize() ? c->themeSize() : -1, m_shm->shm());
309
void WaylandCursorTheme::destroyTheme()
314
wl_cursor_theme_destroy(m_theme);
318
wl_cursor_image *WaylandCursorTheme::get(Qt::CursorShape shape)
324
// loading cursor failed
327
wl_cursor *c = wl_cursor_theme_get_cursor(m_theme, Cursor::self()->cursorName(shape).constData());
328
if (!c || c->image_count <= 0) {
335
WaylandCursor::WaylandCursor(Surface *parentSurface, WaylandBackend *backend)
338
#if HAVE_WAYLAND_CURSOR
339
, m_theme(new WaylandCursorTheme(backend->shmPool(), this))
342
auto surface = backend->compositor()->createSurface(this);
343
m_subSurface = backend->subCompositor()->createSubSurface(QPointer<Surface>(surface), QPointer<Surface>(parentSurface), this);
345
connect(Cursor::self(), &Cursor::posChanged, this,
346
[this](const QPoint &pos) {
347
m_subSurface->setPosition(pos - m_hotSpot);
348
QPointer<Surface> parent = m_subSurface->parentSurface();
349
if (parent.isNull()) {
352
parent->commit(Surface::CommitFlag::None);
356
// install a default cursor image:
357
setCursorImage(Qt::ArrowCursor);
360
void WaylandCursor::setHotSpot(const QPoint &pos)
362
if (m_hotSpot == pos) {
366
emit hotSpotChanged(m_hotSpot);
369
void WaylandCursor::setCursorImage(wl_buffer *image, const QSize &size, const QPoint &hotspot)
371
QPointer<Surface> cursor = m_subSurface->surface();
372
if (cursor.isNull()) {
375
cursor->attachBuffer(image);
376
cursor->damage(QRect(QPoint(0,0), size));
377
cursor->setInputRegion(m_backend->compositor()->createRegion(QRegion()).get());
378
cursor->commit(Surface::CommitFlag::None);
380
m_subSurface->setPosition(Cursor::pos() - m_hotSpot);
381
QPointer<Surface> parent = m_subSurface->parentSurface();
382
if (parent.isNull()) {
385
parent->commit(Surface::CommitFlag::None);
388
void WaylandCursor::setCursorImage(const QImage &image, const QPoint &hotspot)
390
setCursorImage(*(m_backend->shmPool()->createBuffer(image).data()), image.size(), hotspot);
393
void WaylandCursor::setCursorImage(Qt::CursorShape shape)
395
#if HAVE_WAYLAND_CURSOR
396
wl_cursor_image *image = m_theme->get(shape);
400
setCursorImage(wl_cursor_image_get_buffer(image),
401
QSize(image->width, image->height),
402
QPoint(image->hotspot_x, image->hotspot_y));
406
WaylandBackend::WaylandBackend(const QByteArray &display, QObject *parent)
407
: AbstractBackend(parent)
409
, m_eventQueue(new EventQueue(this))
410
, m_registry(new Registry(this))
411
, m_compositor(new Compositor(this))
412
, m_shell(new Shell(this))
414
, m_shellSurface(NULL)
416
, m_shm(new ShmPool(this))
417
, m_connectionThreadObject(new ConnectionThread(nullptr))
418
, m_connectionThread(nullptr)
419
, m_fullscreenShell(new FullscreenShell(this))
420
, m_subCompositor(new SubCompositor(this))
423
connect(this, &WaylandBackend::shellSurfaceSizeChanged, this, &WaylandBackend::checkBackendReady);
424
connect(m_registry, &Registry::compositorAnnounced, this,
425
[this](quint32 name) {
426
m_compositor->setup(m_registry->bindCompositor(name, 1));
429
connect(m_registry, &Registry::shellAnnounced, this,
430
[this](quint32 name) {
431
m_shell->setup(m_registry->bindShell(name, 1));
434
connect(m_registry, &Registry::outputAnnounced, this,
435
[this](quint32 name) {
436
Output *output = new Output(this);
437
output->setup(m_registry->bindOutput(name, 2));
438
m_outputs.append(output);
439
connect(output, &Output::changed, this, &WaylandBackend::outputsChanged);
442
connect(m_registry, &Registry::seatAnnounced, this,
443
[this](quint32 name) {
444
if (Application::usesLibinput()) {
447
m_seat.reset(new WaylandSeat(m_registry->bindSeat(name, 2), this));
450
connect(m_registry, &Registry::shmAnnounced, this,
451
[this](quint32 name) {
452
m_shm->setup(m_registry->bindShm(name, 1));
455
connect(m_registry, &Registry::fullscreenShellAnnounced, this,
456
[this](quint32 name, quint32 version) {
457
m_fullscreenShell->setup(m_registry->bindFullscreenShell(name, version));
460
connect(m_registry, &Registry::subCompositorAnnounced, this,
461
[this](quint32 name, quint32 version) {
462
m_subCompositor->setup(m_registry->bindSubCompositor(name, version));
465
connect(m_registry, &Registry::interfacesAnnounced, this, &WaylandBackend::createSurface);
466
m_connectionThreadObject->setSocketName(display);
470
WaylandBackend::~WaylandBackend()
473
if (m_shellSurface) {
474
m_shellSurface->release();
476
m_fullscreenShell->release();
478
m_surface->release();
481
m_compositor->release();
482
m_registry->release();
485
m_eventQueue->release();
487
m_connectionThreadObject->deleteLater();
488
m_connectionThread->quit();
489
m_connectionThread->wait();
491
qCDebug(KWIN_CORE) << "Destroyed Wayland display";
494
void WaylandBackend::destroyOutputs()
496
qDeleteAll(m_outputs);
500
void WaylandBackend::initConnection()
502
connect(m_connectionThreadObject, &ConnectionThread::connected, this,
504
// create the event queue for the main gui thread
505
m_display = m_connectionThreadObject->display();
506
m_eventQueue->setup(m_connectionThreadObject);
507
m_registry->setEventQueue(m_eventQueue);
509
m_registry->create(m_display);
512
Qt::QueuedConnection);
513
connect(m_connectionThreadObject, &ConnectionThread::connectionDied, this,
516
emit systemCompositorDied();
520
if (m_shellSurface) {
521
m_shellSurface->destroy();
522
delete m_shellSurface;
523
m_shellSurface = nullptr;
525
m_fullscreenShell->destroy();
527
m_surface->destroy();
534
m_compositor->destroy();
535
m_registry->destroy();
536
m_eventQueue->destroy();
541
Qt::QueuedConnection);
542
connect(m_connectionThreadObject, &ConnectionThread::failed, this, &WaylandBackend::connectionFailed, Qt::QueuedConnection);
544
m_connectionThread = new QThread(this);
545
m_connectionThreadObject->moveToThread(m_connectionThread);
546
m_connectionThread->start();
548
m_connectionThreadObject->initConnection();
551
void WaylandBackend::installCursorImage(Qt::CursorShape shape)
553
if (!m_seat.isNull() && m_seat->isInstallCursor()) {
554
m_seat->installCursorImage(shape);
555
} else if (m_cursor) {
556
m_cursor->setCursorImage(shape);
560
void WaylandBackend::installCursorFromServer()
562
if (!waylandServer() || !waylandServer()->seat()->focusedPointer()) {
565
auto c = waylandServer()->seat()->focusedPointer()->cursor();
567
auto cursorSurface = c->surface();
568
if (!cursorSurface.isNull()) {
569
auto buffer = cursorSurface.data()->buffer();
572
if (!m_seat.isNull() && m_seat->isInstallCursor()) {
573
m_seat->installCursorImage(buffer->data(), c->hotspot());
574
} else if (m_cursor) {
575
m_cursor->setCursorImage(buffer->data(), c->hotspot());
581
// TODO: unset cursor
584
void WaylandBackend::createSurface()
586
m_surface = m_compositor->createSurface(this);
587
if (!m_surface || !m_surface->isValid()) {
588
qCritical() << "Creating Wayland Surface failed";
591
if (m_subCompositor->isValid()) {
592
// we have a sub compositor - let's use it for mouse cursor
593
m_cursor = new WaylandCursor(m_surface, this);
595
// no sub-compositor - use the seat for setting the cursor image
597
m_seat->setInstallCursor(true);
600
if (m_fullscreenShell->isValid()) {
601
Output *o = m_outputs.first();
602
m_fullscreenShell->present(m_surface, o);
603
if (o->pixelSize().isValid()) {
604
emit shellSurfaceSizeChanged(o->pixelSize());
606
connect(o, &Output::changed, this,
608
if (o->pixelSize().isValid()) {
609
emit shellSurfaceSizeChanged(o->pixelSize());
613
} else if (m_shell->isValid()) {
614
// map the surface as fullscreen
615
m_shellSurface = m_shell->createSurface(m_surface, this);
616
m_shellSurface->setFullscreen();
617
connect(m_shellSurface, &ShellSurface::sizeChanged, this, &WaylandBackend::shellSurfaceSizeChanged);
621
QSize WaylandBackend::shellSurfaceSize() const
623
if (m_shellSurface) {
624
return m_shellSurface->size();
626
if (m_fullscreenShell->isValid()) {
627
return m_outputs.first()->pixelSize();
632
void WaylandBackend::checkBackendReady()
634
if (!shellSurfaceSize().isValid()) {
637
disconnect(this, &WaylandBackend::shellSurfaceSizeChanged, this, &WaylandBackend::checkBackendReady);
642
void WaylandBackend::connectNotify(const QMetaMethod &signal)
644
if (m_ready && signal == QMetaMethod::fromSignal(&WaylandBackend::backendReady)) {
645
// backend is already ready, let's emit the signal
646
signal.invoke(this, Qt::QueuedConnection);
650
Screens *WaylandBackend::createScreens(QObject *parent)
652
return new WaylandScreens(this, parent);
655
OpenGLBackend *WaylandBackend::createOpenGLBackend()
658
return new EglWaylandBackend(this);
664
QPainterBackend *WaylandBackend::createQPainterBackend()
666
return new WaylandQPainterBackend(this);