1 /*
2     KWin - the KDE window manager
3     This file is part of the KDE project.
4 
5     SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
6 
7     SPDX-License-Identifier: GPL-2.0-or-later
8 */
9 #include "x11windowed_backend.h"
10 #include "x11windowed_output.h"
11 #include <config-kwin.h>
12 #include "scene_qpainter_x11_backend.h"
13 #include "logging.h"
14 #include "wayland_server.h"
15 #include "xcbutils.h"
16 #include "egl_x11_backend.h"
17 #include "screens.h"
18 #include "session.h"
19 #include <kwinxrenderutils.h>
20 #include <cursor.h>
21 #include <pointer_input.h>
22 // KDE
23 #include <KLocalizedString>
24 #include <QAbstractEventDispatcher>
25 #include <QCoreApplication>
26 #include <QSocketNotifier>
27 // kwayland
28 #include <KWaylandServer/display.h>
29 #include <KWaylandServer/seat_interface.h>
30 // xcb
31 #include <xcb/xcb_keysyms.h>
32 // X11
33 #if HAVE_X11_XINPUT
34 #include "ge_event_mem_mover.h"
35 #include <X11/extensions/XInput2.h>
36 #include <X11/extensions/XI2proto.h>
37 #endif
38 // system
39 #include <linux/input.h>
40 #include <X11/Xlib-xcb.h>
41 #include <X11/keysym.h>
42 
43 namespace KWin
44 {
45 
X11WindowedBackend(QObject * parent)46 X11WindowedBackend::X11WindowedBackend(QObject *parent)
47     : Platform(parent)
48     , m_session(Session::create(Session::Type::Noop, this))
49 {
50     setSupportsPointerWarping(true);
51     setPerScreenRenderingEnabled(true);
52 }
53 
~X11WindowedBackend()54 X11WindowedBackend::~X11WindowedBackend()
55 {
56     if (sceneEglDisplay() != EGL_NO_DISPLAY) {
57         eglTerminate(sceneEglDisplay());
58     }
59     if (m_connection) {
60         if (m_keySymbols) {
61             xcb_key_symbols_free(m_keySymbols);
62         }
63         if (m_cursor) {
64             xcb_free_cursor(m_connection, m_cursor);
65         }
66         xcb_disconnect(m_connection);
67     }
68 }
69 
initialize()70 bool X11WindowedBackend::initialize()
71 {
72     int screen = 0;
73     xcb_connection_t *c = nullptr;
74     Display *xDisplay = XOpenDisplay(deviceIdentifier().constData());
75     if (xDisplay) {
76         c = XGetXCBConnection(xDisplay);
77         XSetEventQueueOwner(xDisplay, XCBOwnsEventQueue);
78         screen = XDefaultScreen(xDisplay);
79     }
80     if (c && !xcb_connection_has_error(c)) {
81         m_connection = c;
82         m_screenNumber = screen;
83         m_display = xDisplay;
84         for (xcb_screen_iterator_t it = xcb_setup_roots_iterator(xcb_get_setup(m_connection));
85             it.rem;
86             --screen, xcb_screen_next(&it)) {
87             if (screen == m_screenNumber) {
88                 m_screen = it.data;
89             }
90         }
91         initXInput();
92         XRenderUtils::init(m_connection, m_screen->root);
93         createOutputs();
94         connect(kwinApp(), &Application::workspaceCreated, this, &X11WindowedBackend::startEventReading);
95         connect(Cursors::self(), &Cursors::currentCursorChanged, this,
96             [this] {
97                 KWin::Cursor* c = KWin::Cursors::self()->currentCursor();
98                 createCursor(c->image(), c->hotspot());
99             }
100         );
101         setReady(true);
102         waylandServer()->seat()->setHasPointer(true);
103         waylandServer()->seat()->setHasKeyboard(true);
104         if (m_hasXInput) {
105             waylandServer()->seat()->setHasTouch(true);
106         }
107         Q_EMIT screensQueried();
108         return true;
109     } else {
110         return false;
111     }
112 }
113 
session() const114 Session *X11WindowedBackend::session() const
115 {
116     return m_session;
117 }
118 
initXInput()119 void X11WindowedBackend::initXInput()
120 {
121 #if HAVE_X11_XINPUT
122     int xi_opcode, event, error;
123     // init XInput extension
124     if (!XQueryExtension(m_display, "XInputExtension", &xi_opcode, &event, &error)) {
125         qCDebug(KWIN_X11WINDOWED) << "XInputExtension not present";
126         return;
127     }
128 
129     // verify that the XInput extension is at at least version 2.0
130     int major = 2, minor = 2;
131     int result = XIQueryVersion(m_display, &major, &minor);
132     if (result != Success) {
133         qCDebug(KWIN_X11WINDOWED) << "Failed to init XInput 2.2, trying 2.0";
134         minor = 0;
135         if (XIQueryVersion(m_display, &major, &minor) != Success) {
136             qCDebug(KWIN_X11WINDOWED) << "Failed to init XInput";
137             return;
138         }
139     }
140     m_xiOpcode = xi_opcode;
141     m_majorVersion = major;
142     m_minorVersion = minor;
143     m_hasXInput = m_majorVersion >=2 && m_minorVersion >= 2;
144 #endif
145 }
146 
findOutput(xcb_window_t window) const147 X11WindowedOutput *X11WindowedBackend::findOutput(xcb_window_t window) const
148 {
149     auto it = std::find_if(m_outputs.constBegin(), m_outputs.constEnd(),
150         [window] (X11WindowedOutput *output) {
151             return output->window() == window;
152         }
153     );
154     if (it != m_outputs.constEnd()) {
155         return *it;
156     }
157     return nullptr;
158 }
159 
createOutputs()160 void X11WindowedBackend::createOutputs()
161 {
162     Xcb::Atom protocolsAtom(QByteArrayLiteral("WM_PROTOCOLS"), false, m_connection);
163     Xcb::Atom deleteWindowAtom(QByteArrayLiteral("WM_DELETE_WINDOW"), false, m_connection);
164 
165     // we need to multiply the initial window size with the scale in order to
166     // create an output window of this size in the end
167     const int pixelWidth = initialWindowSize().width() * initialOutputScale() + 0.5;
168     const int pixelHeight = initialWindowSize().height() * initialOutputScale() + 0.5;
169     const int logicalWidth = initialWindowSize().width();
170 
171     int logicalWidthSum = 0;
172     for (int i = 0; i < initialOutputCount(); ++i) {
173         auto *output = new X11WindowedOutput(this);
174         output->init(QPoint(logicalWidthSum, 0), QSize(pixelWidth, pixelHeight));
175 
176         m_protocols = protocolsAtom;
177         m_deleteWindowProtocol = deleteWindowAtom;
178 
179         xcb_change_property(m_connection,
180                             XCB_PROP_MODE_REPLACE,
181                             output->window(),
182                             m_protocols,
183                             XCB_ATOM_ATOM,
184                             32, 1,
185                             &m_deleteWindowProtocol);
186 
187         logicalWidthSum += logicalWidth;
188         m_outputs << output;
189         Q_EMIT outputAdded(output);
190         Q_EMIT outputEnabled(output);
191     }
192 
193     updateWindowTitle();
194 
195     xcb_flush(m_connection);
196 }
197 
startEventReading()198 void X11WindowedBackend::startEventReading()
199 {
200     QSocketNotifier *notifier = new QSocketNotifier(xcb_get_file_descriptor(m_connection), QSocketNotifier::Read, this);
201     auto processXcbEvents = [this] {
202         while (auto event = xcb_poll_for_event(m_connection)) {
203             handleEvent(event);
204             free(event);
205         }
206         xcb_flush(m_connection);
207     };
208     connect(notifier, &QSocketNotifier::activated, this, processXcbEvents);
209     connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::aboutToBlock, this, processXcbEvents);
210     connect(QCoreApplication::eventDispatcher(), &QAbstractEventDispatcher::awake, this, processXcbEvents);
211 }
212 
213 #if HAVE_X11_XINPUT
214 
fixed1616ToReal(FP1616 val)215 static inline qreal fixed1616ToReal(FP1616 val)
216 {
217     return (val) * 1.0 / (1 << 16);
218 }
219 #endif
220 
handleEvent(xcb_generic_event_t * e)221 void X11WindowedBackend::handleEvent(xcb_generic_event_t *e)
222 {
223     const uint8_t eventType = e->response_type & ~0x80;
224     switch (eventType) {
225     case XCB_BUTTON_PRESS:
226     case XCB_BUTTON_RELEASE:
227         handleButtonPress(reinterpret_cast<xcb_button_press_event_t*>(e));
228         break;
229     case XCB_MOTION_NOTIFY: {
230             auto event = reinterpret_cast<xcb_motion_notify_event_t*>(e);
231             const X11WindowedOutput *output = findOutput(event->event);
232             if (!output) {
233                 break;
234             }
235             const QPointF position = output->mapFromGlobal(QPointF(event->root_x, event->root_y));
236             pointerMotion(position, event->time);
237         }
238         break;
239     case XCB_KEY_PRESS:
240     case XCB_KEY_RELEASE: {
241             auto event = reinterpret_cast<xcb_key_press_event_t*>(e);
242             if (eventType == XCB_KEY_PRESS) {
243                 if (!m_keySymbols) {
244                     m_keySymbols = xcb_key_symbols_alloc(m_connection);
245                 }
246                 const xcb_keysym_t kc = xcb_key_symbols_get_keysym(m_keySymbols, event->detail, 0);
247                 if (kc == XK_Control_R) {
248                     grabKeyboard(event->time);
249                 }
250                 keyboardKeyPressed(event->detail - 8, event->time);
251             } else {
252                 keyboardKeyReleased(event->detail - 8, event->time);
253             }
254         }
255         break;
256     case XCB_CONFIGURE_NOTIFY:
257         updateSize(reinterpret_cast<xcb_configure_notify_event_t*>(e));
258         break;
259     case XCB_ENTER_NOTIFY: {
260             auto event = reinterpret_cast<xcb_enter_notify_event_t*>(e);
261             const X11WindowedOutput *output = findOutput(event->event);
262             if (!output) {
263                 break;
264             }
265             const QPointF position = output->mapFromGlobal(QPointF(event->root_x, event->root_y));
266             pointerMotion(position, event->time);
267         }
268         break;
269     case XCB_CLIENT_MESSAGE:
270         handleClientMessage(reinterpret_cast<xcb_client_message_event_t*>(e));
271         break;
272     case XCB_EXPOSE:
273         handleExpose(reinterpret_cast<xcb_expose_event_t*>(e));
274         break;
275     case XCB_MAPPING_NOTIFY:
276         if (m_keySymbols) {
277             xcb_refresh_keyboard_mapping(m_keySymbols, reinterpret_cast<xcb_mapping_notify_event_t*>(e));
278         }
279         break;
280 #if HAVE_X11_XINPUT
281     case XCB_GE_GENERIC: {
282         GeEventMemMover ge(e);
283         auto te = reinterpret_cast<xXIDeviceEvent*>(e);
284         const X11WindowedOutput *output = findOutput(te->event);
285         if (!output) {
286             break;
287         }
288 
289         const QPointF position = output->mapFromGlobal(QPointF(fixed1616ToReal(te->root_x), fixed1616ToReal(te->root_y)));
290 
291         switch (ge->event_type) {
292 
293         case XI_TouchBegin: {
294             touchDown(te->detail, position, te->time);
295             touchFrame();
296             break;
297         }
298         case XI_TouchUpdate: {
299             touchMotion(te->detail, position, te->time);
300             touchFrame();
301             break;
302         }
303         case XI_TouchEnd: {
304             touchUp(te->detail, te->time);
305             touchFrame();
306             break;
307         }
308         case XI_TouchOwnership: {
309             auto te = reinterpret_cast<xXITouchOwnershipEvent*>(e);
310             XIAllowTouchEvents(m_display, te->deviceid, te->sourceid, te->touchid, XIAcceptTouch);
311             break;
312         }
313         }
314         break;
315     }
316 #endif
317     default:
318         break;
319     }
320 }
321 
grabKeyboard(xcb_timestamp_t time)322 void X11WindowedBackend::grabKeyboard(xcb_timestamp_t time)
323 {
324     const bool oldState = m_keyboardGrabbed;
325     if (m_keyboardGrabbed) {
326         xcb_ungrab_keyboard(m_connection, time);
327         xcb_ungrab_pointer(m_connection, time);
328         m_keyboardGrabbed = false;
329     } else {
330         const auto c = xcb_grab_keyboard_unchecked(m_connection, false, window(), time,
331                                                    XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
332         ScopedCPointer<xcb_grab_keyboard_reply_t> grab(xcb_grab_keyboard_reply(m_connection, c, nullptr));
333         if (grab.isNull()) {
334             return;
335         }
336         if (grab->status == XCB_GRAB_STATUS_SUCCESS) {
337             const auto c = xcb_grab_pointer_unchecked(m_connection, false, window(),
338                                                       XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
339                                                       XCB_EVENT_MASK_POINTER_MOTION |
340                                                       XCB_EVENT_MASK_ENTER_WINDOW | XCB_EVENT_MASK_LEAVE_WINDOW,
341                                                       XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
342                                                       window(), XCB_CURSOR_NONE, time);
343             ScopedCPointer<xcb_grab_pointer_reply_t> grab(xcb_grab_pointer_reply(m_connection, c, nullptr));
344             if (grab.isNull() || grab->status != XCB_GRAB_STATUS_SUCCESS) {
345                 xcb_ungrab_keyboard(m_connection, time);
346                 return;
347             }
348             m_keyboardGrabbed = true;
349         }
350     }
351     if (oldState != m_keyboardGrabbed) {
352         updateWindowTitle();
353         xcb_flush(m_connection);
354     }
355 }
356 
updateWindowTitle()357 void X11WindowedBackend::updateWindowTitle()
358 {
359     const QString grab = m_keyboardGrabbed ? i18n("Press right control to ungrab input") : i18n("Press right control key to grab input");
360     const QString title = QStringLiteral("%1 (%2) - %3").arg(i18n("KDE Wayland Compositor"),
361                                                              waylandServer()->socketName(),
362                                                              grab);
363     for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
364         (*it)->setWindowTitle(title);
365     }
366 }
367 
handleClientMessage(xcb_client_message_event_t * event)368 void X11WindowedBackend::handleClientMessage(xcb_client_message_event_t *event)
369 {
370     auto it = std::find_if(m_outputs.begin(), m_outputs.end(),
371                            [event] (X11WindowedOutput *output) { return output->window() == event->window; }
372               );
373     if (it == m_outputs.end()) {
374         return;
375     }
376     if (event->type == m_protocols && m_protocols != XCB_ATOM_NONE) {
377         if (event->data.data32[0] == m_deleteWindowProtocol && m_deleteWindowProtocol != XCB_ATOM_NONE) {
378             if (m_outputs.count() == 1) {
379                 qCDebug(KWIN_X11WINDOWED) << "Backend window is going to be closed, shutting down.";
380                 QCoreApplication::quit();
381             } else {
382                 // remove the window
383                 qCDebug(KWIN_X11WINDOWED) << "Removing one output window.";
384 
385                 auto removedOutput = *it;
386                 it = m_outputs.erase(it);
387 
388                 // update the sizes
389                 int x = removedOutput->internalPosition().x();
390                 for (; it != m_outputs.end(); ++it) {
391                     (*it)->setGeometry(QPoint(x, 0), (*it)->pixelSize());
392                     x += (*it)->geometry().width();
393                 }
394 
395                 Q_EMIT outputDisabled(removedOutput);
396                 Q_EMIT outputRemoved(removedOutput);
397                 delete removedOutput;
398                 QMetaObject::invokeMethod(screens(), "updateCount");
399             }
400         }
401     }
402 }
403 
handleButtonPress(xcb_button_press_event_t * event)404 void X11WindowedBackend::handleButtonPress(xcb_button_press_event_t *event)
405 {
406     const X11WindowedOutput *output = findOutput(event->event);
407     if (!output) {
408         return;
409     }
410     bool const pressed = (event->response_type & ~0x80) == XCB_BUTTON_PRESS;
411     if (event->detail >= XCB_BUTTON_INDEX_4 && event->detail <= 7) {
412         // wheel
413         if (!pressed) {
414             return;
415         }
416         const int delta = (event->detail == XCB_BUTTON_INDEX_4 || event->detail == 6) ? -1 : 1;
417         static const qreal s_defaultAxisStepDistance = 10.0;
418         if (event->detail > 5) {
419             pointerAxisHorizontal(delta * s_defaultAxisStepDistance, event->time, delta);
420         } else {
421             pointerAxisVertical(delta * s_defaultAxisStepDistance, event->time, delta);
422         }
423         return;
424     }
425     uint32_t button = 0;
426     switch (event->detail) {
427     case XCB_BUTTON_INDEX_1:
428         button = BTN_LEFT;
429         break;
430     case XCB_BUTTON_INDEX_2:
431         button = BTN_MIDDLE;
432         break;
433     case XCB_BUTTON_INDEX_3:
434         button = BTN_RIGHT;
435         break;
436     default:
437         button = event->detail + BTN_LEFT - 1;
438         return;
439     }
440 
441     const QPointF position = output->mapFromGlobal(QPointF(event->root_x, event->root_y));
442     pointerMotion(position, event->time);
443 
444     if (pressed) {
445         pointerButtonPressed(button, event->time);
446     } else {
447         pointerButtonReleased(button, event->time);
448     }
449 }
450 
handleExpose(xcb_expose_event_t * event)451 void X11WindowedBackend::handleExpose(xcb_expose_event_t *event)
452 {
453     repaint(QRect(event->x, event->y, event->width, event->height));
454 }
455 
updateSize(xcb_configure_notify_event_t * event)456 void X11WindowedBackend::updateSize(xcb_configure_notify_event_t *event)
457 {
458     X11WindowedOutput *output = findOutput(event->window);
459     if (!output) {
460         return;
461     }
462 
463     output->setHostPosition(QPoint(event->x, event->y));
464 
465     const QSize s = QSize(event->width, event->height);
466     if (s != output->pixelSize()) {
467         output->setGeometry(output->internalPosition(), s);
468     }
469     Q_EMIT sizeChanged();
470 }
471 
createCursor(const QImage & srcImage,const QPoint & hotspot)472 void X11WindowedBackend::createCursor(const QImage &srcImage, const QPoint &hotspot)
473 {
474     const xcb_pixmap_t pix = xcb_generate_id(m_connection);
475     const xcb_gcontext_t gc = xcb_generate_id(m_connection);
476     const xcb_cursor_t cid = xcb_generate_id(m_connection);
477 
478     //right now on X we only have one scale between all screens, and we know we will have at least one screen
479     const qreal outputScale = 1;
480     const QSize targetSize = srcImage.size() * outputScale / srcImage.devicePixelRatio();
481     const QImage img = srcImage.scaled(targetSize, Qt::KeepAspectRatio);
482 
483     xcb_create_pixmap(m_connection, 32, pix, m_screen->root, img.width(), img.height());
484     xcb_create_gc(m_connection, gc, pix, 0, nullptr);
485 
486     xcb_put_image(m_connection, XCB_IMAGE_FORMAT_Z_PIXMAP, pix, gc, img.width(), img.height(), 0, 0, 0, 32, img.sizeInBytes(), img.constBits());
487 
488     XRenderPicture pic(pix, 32);
489     xcb_render_create_cursor(m_connection, cid, pic, qRound(hotspot.x() * outputScale), qRound(hotspot.y() * outputScale));
490     for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
491         xcb_change_window_attributes(m_connection, (*it)->window(), XCB_CW_CURSOR, &cid);
492     }
493 
494     xcb_free_pixmap(m_connection, pix);
495     xcb_free_gc(m_connection, gc);
496     if (m_cursor) {
497         xcb_free_cursor(m_connection, m_cursor);
498     }
499     m_cursor = cid;
500     xcb_flush(m_connection);
501 }
502 
rootWindow() const503 xcb_window_t X11WindowedBackend::rootWindow() const
504 {
505     if (!m_screen) {
506         return XCB_WINDOW_NONE;
507     }
508     return m_screen->root;
509 }
510 
createOpenGLBackend()511 OpenGLBackend *X11WindowedBackend::createOpenGLBackend()
512 {
513     return  new EglX11Backend(this);
514 }
515 
createQPainterBackend()516 QPainterBackend *X11WindowedBackend::createQPainterBackend()
517 {
518     return new X11WindowedQPainterBackend(this);
519 }
520 
warpPointer(const QPointF & globalPos)521 void X11WindowedBackend::warpPointer(const QPointF &globalPos)
522 {
523     const xcb_window_t w = m_outputs.at(0)->window();
524     xcb_warp_pointer(m_connection, w, w, 0, 0, 0, 0, globalPos.x(), globalPos.y());
525     xcb_flush(m_connection);
526 }
527 
windowForScreen(AbstractOutput * output) const528 xcb_window_t X11WindowedBackend::windowForScreen(AbstractOutput *output) const
529 {
530     if (!output) {
531         return XCB_WINDOW_NONE;
532     }
533     return static_cast<X11WindowedOutput*>(output)->window();
534 }
535 
window() const536 xcb_window_t X11WindowedBackend::window() const
537 {
538     return m_outputs.first()->window();
539 }
540 
outputs() const541 Outputs X11WindowedBackend::outputs() const
542 {
543     return m_outputs;
544 }
545 
enabledOutputs() const546 Outputs X11WindowedBackend::enabledOutputs() const
547 {
548     return m_outputs;
549 }
550 
551 }
552