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_wayland_backend.h"
27
#include "screens_wayland.h"
28
#include "wayland_server.h"
29
#if HAVE_WAYLAND_CURSOR
30
#include "wayland_cursor_theme.h"
33
#include "egl_wayland_backend.h"
35
#include <KWayland/Client/buffer.h>
36
#include <KWayland/Client/compositor.h>
37
#include <KWayland/Client/connection_thread.h>
38
#include <KWayland/Client/event_queue.h>
39
#include <KWayland/Client/fullscreen_shell.h>
40
#include <KWayland/Client/keyboard.h>
41
#include <KWayland/Client/output.h>
42
#include <KWayland/Client/pointer.h>
43
#include <KWayland/Client/region.h>
44
#include <KWayland/Client/registry.h>
45
#include <KWayland/Client/seat.h>
46
#include <KWayland/Client/shell.h>
47
#include <KWayland/Client/shm_pool.h>
48
#include <KWayland/Client/subcompositor.h>
49
#include <KWayland/Client/subsurface.h>
50
#include <KWayland/Client/surface.h>
51
#include <KWayland/Client/touch.h>
52
#include <KWayland/Server/buffer_interface.h>
53
#include <KWayland/Server/seat_interface.h>
54
#include <KWayland/Server/surface_interface.h>
56
#include <QMetaMethod>
59
#if HAVE_WAYLAND_CURSOR
60
#include <wayland-cursor.h>
68
using namespace KWayland::Client;
70
WaylandSeat::WaylandSeat(wl_seat *seat, WaylandBackend *backend)
72
, m_seat(new Seat(this))
77
#if HAVE_WAYLAND_CURSOR
78
, m_theme(new WaylandCursorTheme(backend->shmPool(), this))
82
, m_installCursor(false)
85
connect(m_seat, &Seat::hasKeyboardChanged, this,
86
[this](bool hasKeyboard) {
88
m_keyboard = m_seat->createKeyboard(this);
89
connect(m_keyboard, &Keyboard::keyChanged, this,
90
[this](quint32 key, Keyboard::KeyState state, quint32 time) {
92
case Keyboard::KeyState::Pressed:
93
m_backend->keyboardKeyPressed(key, time);
95
case Keyboard::KeyState::Released:
96
m_backend->keyboardKeyReleased(key, time);
103
connect(m_keyboard, &Keyboard::modifiersChanged, this,
104
[this](quint32 depressed, quint32 latched, quint32 locked, quint32 group) {
105
m_backend->keyboardModifiers(depressed, latched, locked, group);
108
connect(m_keyboard, &Keyboard::keymapChanged, this,
109
[this](int fd, quint32 size) {
110
m_backend->keymapChange(fd, size);
118
connect(m_seat, &Seat::hasPointerChanged, this,
119
[this](bool hasPointer) {
120
if (hasPointer && !m_pointer) {
121
m_pointer = m_seat->createPointer(this);
122
connect(m_pointer, &Pointer::entered, this,
123
[this](quint32 serial) {
124
m_enteredSerial = serial;
125
if (!m_installCursor) {
126
// explicitly hide cursor
127
m_pointer->hideCursor();
131
connect(m_pointer, &Pointer::motion, this,
132
[this](const QPointF &relativeToSurface, quint32 time) {
133
m_backend->pointerMotion(relativeToSurface, time);
136
connect(m_pointer, &Pointer::buttonStateChanged, this,
137
[this](quint32 serial, quint32 time, quint32 button, Pointer::ButtonState state) {
140
case Pointer::ButtonState::Pressed:
141
m_backend->pointerButtonPressed(button, time);
143
case Pointer::ButtonState::Released:
144
m_backend->pointerButtonReleased(button, time);
151
connect(m_pointer, &Pointer::axisChanged, this,
152
[this](quint32 time, Pointer::Axis axis, qreal delta) {
154
case Pointer::Axis::Horizontal:
155
m_backend->pointerAxisHorizontal(delta, time);
157
case Pointer::Axis::Vertical:
158
m_backend->pointerAxisVertical(delta, time);
170
connect(m_seat, &Seat::hasTouchChanged,
171
[this] (bool hasTouch) {
172
if (hasTouch && !m_touch) {
173
m_touch = m_seat->createTouch(this);
174
connect(m_touch, &Touch::sequenceCanceled, m_backend, &AbstractBackend::touchCancel);
175
connect(m_touch, &Touch::frameEnded, m_backend, &AbstractBackend::touchFrame);
176
connect(m_touch, &Touch::sequenceStarted, this,
177
[this] (TouchPoint *tp) {
178
m_backend->touchDown(tp->id(), tp->position(), tp->time());
181
connect(m_touch, &Touch::pointAdded, this,
182
[this] (TouchPoint *tp) {
183
m_backend->touchDown(tp->id(), tp->position(), tp->time());
186
connect(m_touch, &Touch::pointRemoved, this,
187
[this] (TouchPoint *tp) {
188
m_backend->touchUp(tp->id(), tp->time());
191
connect(m_touch, &Touch::pointMoved, this,
192
[this] (TouchPoint *tp) {
193
m_backend->touchMotion(tp->id(), tp->position(), tp->time());
201
WaylandServer *server = waylandServer();
203
using namespace KWayland::Server;
204
SeatInterface *si = server->seat();
205
connect(m_seat, &Seat::hasKeyboardChanged, si, &SeatInterface::setHasKeyboard);
206
connect(m_seat, &Seat::hasPointerChanged, si, &SeatInterface::setHasPointer);
207
connect(m_seat, &Seat::hasTouchChanged, si, &SeatInterface::setHasTouch);
208
connect(m_seat, &Seat::nameChanged, si, &SeatInterface::setName);
212
WaylandSeat::~WaylandSeat()
219
void WaylandSeat::destroyPointer()
225
void WaylandSeat::destroyKeyboard()
228
m_keyboard = nullptr;
231
void WaylandSeat::destroyTouch()
237
void WaylandSeat::installCursorImage(wl_buffer *image, const QSize &size, const QPoint &hotSpot)
239
if (!m_installCursor) {
242
if (!m_pointer || !m_pointer->isValid()) {
246
m_cursor = m_backend->compositor()->createSurface(this);
248
if (!m_cursor || !m_cursor->isValid()) {
251
m_pointer->setCursor(m_cursor, hotSpot);
252
m_cursor->attachBuffer(image);
253
m_cursor->damage(QRect(QPoint(0,0), size));
254
m_cursor->commit(Surface::CommitFlag::None);
257
void WaylandSeat::installCursorImage(Qt::CursorShape shape)
259
#if HAVE_WAYLAND_CURSOR
260
wl_cursor_image *image = m_theme->get(shape);
264
installCursorImage(wl_cursor_image_get_buffer(image),
265
QSize(image->width, image->height),
266
QPoint(image->hotspot_x, image->hotspot_y));
270
void WaylandSeat::installCursorImage(const QImage &image, const QPoint &hotSpot)
272
installCursorImage(*(m_backend->shmPool()->createBuffer(image).data()), image.size(), hotSpot);
275
void WaylandSeat::setInstallCursor(bool install)
277
// TODO: remove, add?
278
m_installCursor = install;
281
WaylandCursor::WaylandCursor(Surface *parentSurface, WaylandBackend *backend)
284
#if HAVE_WAYLAND_CURSOR
285
, m_theme(new WaylandCursorTheme(backend->shmPool(), this))
288
auto surface = backend->compositor()->createSurface(this);
289
m_subSurface = backend->subCompositor()->createSubSurface(QPointer<Surface>(surface), QPointer<Surface>(parentSurface), this);
291
connect(Cursor::self(), &Cursor::posChanged, this,
292
[this](const QPoint &pos) {
293
m_subSurface->setPosition(pos - m_hotSpot);
294
QPointer<Surface> parent = m_subSurface->parentSurface();
295
if (parent.isNull()) {
298
parent->commit(Surface::CommitFlag::None);
302
// install a default cursor image:
303
setCursorImage(Qt::ArrowCursor);
306
void WaylandCursor::setHotSpot(const QPoint &pos)
308
if (m_hotSpot == pos) {
312
emit hotSpotChanged(m_hotSpot);
315
void WaylandCursor::setCursorImage(wl_buffer *image, const QSize &size, const QPoint &hotspot)
317
QPointer<Surface> cursor = m_subSurface->surface();
318
if (cursor.isNull()) {
321
cursor->attachBuffer(image);
322
cursor->damage(QRect(QPoint(0,0), size));
323
cursor->setInputRegion(m_backend->compositor()->createRegion(QRegion()).get());
324
cursor->commit(Surface::CommitFlag::None);
326
m_subSurface->setPosition(Cursor::pos() - m_hotSpot);
327
QPointer<Surface> parent = m_subSurface->parentSurface();
328
if (parent.isNull()) {
331
parent->commit(Surface::CommitFlag::None);
334
void WaylandCursor::setCursorImage(const QImage &image, const QPoint &hotspot)
336
setCursorImage(*(m_backend->shmPool()->createBuffer(image).data()), image.size(), hotspot);
339
void WaylandCursor::setCursorImage(Qt::CursorShape shape)
341
#if HAVE_WAYLAND_CURSOR
342
wl_cursor_image *image = m_theme->get(shape);
346
setCursorImage(wl_cursor_image_get_buffer(image),
347
QSize(image->width, image->height),
348
QPoint(image->hotspot_x, image->hotspot_y));
352
WaylandBackend::WaylandBackend(QObject *parent)
353
: AbstractBackend(parent)
355
, m_eventQueue(new EventQueue(this))
356
, m_registry(new Registry(this))
357
, m_compositor(new Compositor(this))
358
, m_shell(new Shell(this))
360
, m_shellSurface(NULL)
362
, m_shm(new ShmPool(this))
363
, m_connectionThreadObject(new ConnectionThread(nullptr))
364
, m_connectionThread(nullptr)
365
, m_fullscreenShell(new FullscreenShell(this))
366
, m_subCompositor(new SubCompositor(this))
369
connect(this, &WaylandBackend::outputsChanged, this, &WaylandBackend::screensQueried);
370
connect(this, &WaylandBackend::connectionFailed, this, &WaylandBackend::initFailed);
373
WaylandBackend::~WaylandBackend()
376
if (m_shellSurface) {
377
m_shellSurface->release();
379
m_fullscreenShell->release();
381
m_surface->release();
384
m_compositor->release();
385
m_registry->release();
388
m_eventQueue->release();
390
m_connectionThreadObject->deleteLater();
391
m_connectionThread->quit();
392
m_connectionThread->wait();
394
qCDebug(KWIN_WAYLAND_BACKEND) << "Destroyed Wayland display";
397
void WaylandBackend::init()
399
connect(this, &WaylandBackend::shellSurfaceSizeChanged, this, &WaylandBackend::checkBackendReady);
400
connect(m_registry, &Registry::compositorAnnounced, this,
401
[this](quint32 name) {
402
m_compositor->setup(m_registry->bindCompositor(name, 1));
405
connect(m_registry, &Registry::shellAnnounced, this,
406
[this](quint32 name) {
407
m_shell->setup(m_registry->bindShell(name, 1));
410
connect(m_registry, &Registry::outputAnnounced, this,
411
[this](quint32 name) {
412
Output *output = new Output(this);
413
output->setup(m_registry->bindOutput(name, 2));
414
m_outputs.append(output);
415
connect(output, &Output::changed, this, &WaylandBackend::outputsChanged);
418
connect(m_registry, &Registry::seatAnnounced, this,
419
[this](quint32 name) {
420
if (Application::usesLibinput()) {
423
m_seat.reset(new WaylandSeat(m_registry->bindSeat(name, 2), this));
426
connect(m_registry, &Registry::shmAnnounced, this,
427
[this](quint32 name) {
428
m_shm->setup(m_registry->bindShm(name, 1));
431
connect(m_registry, &Registry::fullscreenShellAnnounced, this,
432
[this](quint32 name, quint32 version) {
433
m_fullscreenShell->setup(m_registry->bindFullscreenShell(name, version));
436
connect(m_registry, &Registry::subCompositorAnnounced, this,
437
[this](quint32 name, quint32 version) {
438
m_subCompositor->setup(m_registry->bindSubCompositor(name, version));
441
connect(m_registry, &Registry::interfacesAnnounced, this, &WaylandBackend::createSurface);
442
m_connectionThreadObject->setSocketName(deviceIdentifier());
446
void WaylandBackend::destroyOutputs()
448
qDeleteAll(m_outputs);
452
void WaylandBackend::initConnection()
454
connect(m_connectionThreadObject, &ConnectionThread::connected, this,
456
// create the event queue for the main gui thread
457
m_display = m_connectionThreadObject->display();
458
m_eventQueue->setup(m_connectionThreadObject);
459
m_registry->setEventQueue(m_eventQueue);
461
m_registry->create(m_display);
464
Qt::QueuedConnection);
465
connect(m_connectionThreadObject, &ConnectionThread::connectionDied, this,
468
emit systemCompositorDied();
472
if (m_shellSurface) {
473
m_shellSurface->destroy();
474
delete m_shellSurface;
475
m_shellSurface = nullptr;
477
m_fullscreenShell->destroy();
479
m_surface->destroy();
486
m_compositor->destroy();
487
m_registry->destroy();
488
m_eventQueue->destroy();
493
Qt::QueuedConnection);
494
connect(m_connectionThreadObject, &ConnectionThread::failed, this, &WaylandBackend::connectionFailed, Qt::QueuedConnection);
496
m_connectionThread = new QThread(this);
497
m_connectionThreadObject->moveToThread(m_connectionThread);
498
m_connectionThread->start();
500
m_connectionThreadObject->initConnection();
503
void WaylandBackend::installCursorImage(Qt::CursorShape shape)
505
if (!m_seat.isNull() && m_seat->isInstallCursor()) {
506
m_seat->installCursorImage(shape);
507
} else if (m_cursor) {
508
m_cursor->setCursorImage(shape);
512
void WaylandBackend::installCursorFromServer()
514
if (!waylandServer() || !waylandServer()->seat()->focusedPointer()) {
517
auto c = waylandServer()->seat()->focusedPointer()->cursor();
519
auto cursorSurface = c->surface();
520
if (!cursorSurface.isNull()) {
521
auto buffer = cursorSurface.data()->buffer();
524
if (!m_seat.isNull() && m_seat->isInstallCursor()) {
525
m_seat->installCursorImage(buffer->data(), c->hotspot());
526
} else if (m_cursor) {
527
m_cursor->setCursorImage(buffer->data(), c->hotspot());
533
// TODO: unset cursor
536
void WaylandBackend::createSurface()
538
m_surface = m_compositor->createSurface(this);
539
if (!m_surface || !m_surface->isValid()) {
540
qCCritical(KWIN_WAYLAND_BACKEND) << "Creating Wayland Surface failed";
543
if (m_subCompositor->isValid()) {
544
// we have a sub compositor - let's use it for mouse cursor
545
m_cursor = new WaylandCursor(m_surface, this);
547
// no sub-compositor - use the seat for setting the cursor image
549
m_seat->setInstallCursor(true);
552
if (m_fullscreenShell->isValid()) {
553
Output *o = m_outputs.first();
554
m_fullscreenShell->present(m_surface, o);
555
if (o->pixelSize().isValid()) {
556
emit shellSurfaceSizeChanged(o->pixelSize());
558
connect(o, &Output::changed, this,
560
if (o->pixelSize().isValid()) {
561
emit shellSurfaceSizeChanged(o->pixelSize());
565
} else if (m_shell->isValid()) {
566
// map the surface as fullscreen
567
m_shellSurface = m_shell->createSurface(m_surface, this);
568
m_shellSurface->setFullscreen();
569
connect(m_shellSurface, &ShellSurface::sizeChanged, this, &WaylandBackend::shellSurfaceSizeChanged);
573
QSize WaylandBackend::shellSurfaceSize() const
575
if (m_shellSurface) {
576
return m_shellSurface->size();
578
if (m_fullscreenShell->isValid()) {
579
return m_outputs.first()->pixelSize();
584
void WaylandBackend::checkBackendReady()
586
if (!shellSurfaceSize().isValid()) {
589
disconnect(this, &WaylandBackend::shellSurfaceSizeChanged, this, &WaylandBackend::checkBackendReady);
593
Screens *WaylandBackend::createScreens(QObject *parent)
595
return new WaylandScreens(this, parent);
598
OpenGLBackend *WaylandBackend::createOpenGLBackend()
601
return new EglWaylandBackend(this);
607
QPainterBackend *WaylandBackend::createQPainterBackend()
609
return new WaylandQPainterBackend(this);