1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2015 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
*********************************************************************/
20
#include "x11windowed_backend.h"
21
#include "composite.h"
23
#include "scene_qpainter.h"
24
#include "screens_x11windowed.h"
26
#include "wayland_server.h"
30
#include "eglonxbackend.h"
33
#include <kwinxrenderutils.h>
34
#include <QAbstractEventDispatcher>
35
#include <QCoreApplication>
37
#include <QSocketNotifier>
39
#include <KWayland/Server/buffer_interface.h>
40
#include <KWayland/Server/seat_interface.h>
41
#include <KWayland/Server/surface_interface.h>
43
#include <linux/input.h>
45
#include <X11/Xlib-xcb.h>
51
X11WindowedBackend::X11WindowedBackend(const QByteArray &display, const QSize &size, QObject *parent)
52
: AbstractBackend(parent)
56
xcb_connection_t *c = nullptr;
58
Display *xDisplay = XOpenDisplay(display.constData());
60
c = XGetXCBConnection(xDisplay);
61
XSetEventQueueOwner(xDisplay, XCBOwnsEventQueue);
62
screen = XDefaultScreen(xDisplay);
65
c = xcb_connect(display.constData(), &screen);
67
if (c && !xcb_connection_has_error(c)) {
69
m_screenNumber = screen;
73
for (xcb_screen_iterator_t it = xcb_setup_roots_iterator(xcb_get_setup(m_connection));
75
--screen, xcb_screen_next(&it)) {
76
if (screen == m_screenNumber) {
80
XRenderUtils::init(m_connection, m_screen->root);
86
X11WindowedBackend::~X11WindowedBackend()
90
xcb_unmap_window(m_connection, m_window);
91
xcb_destroy_window(m_connection, m_window);
94
xcb_free_cursor(m_connection, m_cursor);
96
xcb_disconnect(m_connection);
100
void X11WindowedBackend::createWindow()
102
Q_ASSERT(m_window == XCB_WINDOW_NONE);
103
Xcb::Atom protocolsAtom(QByteArrayLiteral("WM_PROTOCOLS"), false, m_connection);
104
Xcb::Atom deleteWindowAtom(QByteArrayLiteral("WM_DELETE_WINDOW"), false, m_connection);
105
m_window = xcb_generate_id(m_connection);
106
uint32_t mask = XCB_CW_EVENT_MASK;
107
const uint32_t values[] = {
108
XCB_EVENT_MASK_KEY_PRESS |
109
XCB_EVENT_MASK_KEY_RELEASE |
110
XCB_EVENT_MASK_BUTTON_PRESS |
111
XCB_EVENT_MASK_BUTTON_RELEASE |
112
XCB_EVENT_MASK_POINTER_MOTION |
113
XCB_EVENT_MASK_ENTER_WINDOW |
114
XCB_EVENT_MASK_LEAVE_WINDOW |
115
XCB_EVENT_MASK_STRUCTURE_NOTIFY |
116
XCB_EVENT_MASK_EXPOSURE
118
xcb_create_window(m_connection, XCB_COPY_FROM_PARENT, m_window, m_screen->root,
119
0, 0, m_size.width(), m_size.height(),
120
0, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT, mask, values);
121
xcb_map_window(m_connection, m_window);
123
m_protocols = protocolsAtom;
124
m_deleteWindowProtocol = deleteWindowAtom;
125
xcb_change_property(m_connection, XCB_PROP_MODE_REPLACE, m_window, m_protocols, XCB_ATOM_ATOM, 32, 1, &m_deleteWindowProtocol);
127
xcb_flush(m_connection);
130
void X11WindowedBackend::startEventReading()
132
QSocketNotifier *notifier = new QSocketNotifier(xcb_get_file_descriptor(m_connection), QSocketNotifier::Read, this);
133
auto processXcbEvents = [this] {
134
while (auto event = xcb_poll_for_event(m_connection)) {
138
xcb_flush(m_connection);
140
connect(notifier, &QSocketNotifier::activated, this, processXcbEvents);
141
connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, processXcbEvents);
142
connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::awake, this, processXcbEvents);
145
void X11WindowedBackend::handleEvent(xcb_generic_event_t *e)
147
const uint8_t eventType = e->response_type & ~0x80;
149
case XCB_BUTTON_PRESS:
150
case XCB_BUTTON_RELEASE:
151
handleButtonPress(reinterpret_cast<xcb_button_press_event_t*>(e));
153
case XCB_MOTION_NOTIFY:
155
auto event = reinterpret_cast<xcb_motion_notify_event_t*>(e);
156
input()->processPointerMotion(QPointF(event->event_x, event->event_y), event->time);
160
case XCB_KEY_RELEASE:
162
auto event = reinterpret_cast<xcb_key_press_event_t*>(e);
163
input()->processKeyboardKey(event->detail - 8, eventType == XCB_KEY_PRESS ? InputRedirection::KeyboardKeyPressed : InputRedirection::KeyboardKeyReleased, event->time);
166
case XCB_CONFIGURE_NOTIFY:
167
updateSize(reinterpret_cast<xcb_configure_notify_event_t*>(e));
169
case XCB_ENTER_NOTIFY:
171
auto event = reinterpret_cast<xcb_enter_notify_event_t*>(e);
172
input()->processPointerMotion(QPointF(event->event_x, event->event_y), event->time);
175
case XCB_CLIENT_MESSAGE:
176
handleClientMessage(reinterpret_cast<xcb_client_message_event_t*>(e));
179
handleExpose(reinterpret_cast<xcb_expose_event_t*>(e));
186
void X11WindowedBackend::handleClientMessage(xcb_client_message_event_t *event)
188
if (event->window != m_window) {
191
if (event->type == m_protocols && m_protocols != XCB_ATOM_NONE) {
192
if (event->data.data32[0] == m_deleteWindowProtocol && m_deleteWindowProtocol != XCB_ATOM_NONE) {
193
qCDebug(KWIN_CORE) << "Backend window is going to be closed, shutting down.";
194
QCoreApplication::quit();
199
void X11WindowedBackend::handleButtonPress(xcb_button_press_event_t *event)
204
bool const pressed = (event->response_type & ~0x80) == XCB_BUTTON_PRESS;
205
if (event->detail >= XCB_BUTTON_INDEX_4 && event->detail <= 7) {
210
const int delta = (event->detail == XCB_BUTTON_INDEX_4 || event->detail == 6) ? -1 : 1;
211
InputRedirection::PointerAxis axis = (event->detail > 5) ? InputRedirection::PointerAxisHorizontal : InputRedirection::PointerAxisVertical;
212
static const qreal s_defaultAxisStepDistance = 10.0;
213
input()->processPointerAxis(axis, delta * s_defaultAxisStepDistance, event->time);
217
switch (event->detail) {
218
case XCB_BUTTON_INDEX_1:
221
case XCB_BUTTON_INDEX_2:
224
case XCB_BUTTON_INDEX_3:
228
button = event->detail + BTN_LEFT - 1;
231
input()->processPointerMotion(QPointF(event->event_x, event->event_y), event->time);
232
input()->processPointerButton(button, pressed ? InputRedirection::PointerButtonPressed : InputRedirection::PointerButtonReleased, event->time);
235
void X11WindowedBackend::handleExpose(xcb_expose_event_t *event)
237
if (!Compositor::self()) {
240
Compositor::self()->addRepaint(QRect(event->x, event->y, event->width, event->height));
243
void X11WindowedBackend::updateSize(xcb_configure_notify_event_t *event)
245
if (event->window != m_window) {
248
QSize s = QSize(event->width, event->height);
255
void X11WindowedBackend::installCursorFromServer()
257
if (!waylandServer() || !waylandServer()->seat()->focusedPointer()) {
260
auto c = waylandServer()->seat()->focusedPointer()->cursor();
262
auto cursorSurface = c->surface();
263
if (!cursorSurface.isNull()) {
264
auto buffer = cursorSurface.data()->buffer();
266
// TODO: cache generated cursors?
267
const xcb_pixmap_t pix = xcb_generate_id(m_connection);
268
const xcb_gcontext_t gc = xcb_generate_id(m_connection);
269
const xcb_cursor_t cid = xcb_generate_id(m_connection);
271
xcb_create_pixmap(m_connection, 32, pix, m_screen->root, buffer->size().width(), buffer->size().height());
272
xcb_create_gc(m_connection, gc, pix, 0, nullptr);
274
const QImage img = buffer->data();
275
xcb_put_image(m_connection, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc, img.width(), img.height(), 0, 0, 0, 32, img.byteCount(), img.constBits());
277
XRenderPicture pic(pix, 32);
278
xcb_render_create_cursor(m_connection, cid, pic, c->hotspot().x(), c->hotspot().y());
279
xcb_change_window_attributes(m_connection, m_window, XCB_CW_CURSOR, &cid);
281
xcb_free_pixmap(m_connection, pix);
282
xcb_free_gc(m_connection, gc);
284
xcb_free_cursor(m_connection, m_cursor);
287
xcb_flush(m_connection);
292
// TODO: unset cursor
295
xcb_window_t X11WindowedBackend::rootWindow() const
298
return XCB_WINDOW_NONE;
300
return m_screen->root;
303
Screens *X11WindowedBackend::createScreens(QObject *parent)
305
return new X11WindowedScreens(this, parent);
308
OpenGLBackend *X11WindowedBackend::createOpenGLBackend()
312
return new EglOnXBackend(this);
318
QPainterBackend *X11WindowedBackend::createQPainterBackend()
320
return new X11WindowedQPainterBackend(this);