summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/xcb/qxcbconnection_screens.cpp
blob: 0a45b1b0c9edd0c6b7942ddd32697dae60afc2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qxcbconnection.h"
#include "qxcbscreen.h"
#include "qxcbintegration.h"

#include <QtGui/private/qhighdpiscaling_p.h>
#include <QtCore/QString>
#include <QtCore/QList>

#include <qpa/qwindowsysteminterface.h>

void QXcbConnection::xrandrSelectEvents()
{
    xcb_screen_iterator_t rootIter = xcb_setup_roots_iterator(setup());
    for (; rootIter.rem; xcb_screen_next(&rootIter)) {
        xcb_randr_select_input(xcb_connection(),
            rootIter.data->root,
            XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE |
            XCB_RANDR_NOTIFY_MASK_OUTPUT_CHANGE |
            XCB_RANDR_NOTIFY_MASK_CRTC_CHANGE |
            XCB_RANDR_NOTIFY_MASK_OUTPUT_PROPERTY
        );
    }
}

QXcbScreen* QXcbConnection::findScreenForCrtc(xcb_window_t rootWindow, xcb_randr_crtc_t crtc) const
{
    for (QXcbScreen *screen : m_screens) {
        if (screen->root() == rootWindow) {
            if (screen->m_monitor) {
                if (screen->crtcs().contains(crtc))
                    return screen;
            } else {
                if (screen->crtc() == crtc)
                    return screen;
            }
        }
    }

    return nullptr;
}

QXcbScreen* QXcbConnection::findScreenForOutput(xcb_window_t rootWindow, xcb_randr_output_t output) const
{
    for (QXcbScreen *screen : m_screens) {
        if (screen->root() == rootWindow) {
            if (screen->m_monitor) {
                if (screen->outputs().contains(output))
                    return screen;
            } else {
                if (screen->output() == output)
                    return screen;
            }
        }
    }

    return nullptr;
}

QXcbVirtualDesktop* QXcbConnection::virtualDesktopForRootWindow(xcb_window_t rootWindow) const
{
    for (QXcbVirtualDesktop *virtualDesktop : m_virtualDesktops) {
        if (virtualDesktop->screen()->root == rootWindow)
            return virtualDesktop;
    }

    return nullptr;
}

/*!
    \brief Synchronizes the screen list, adds new screens, removes deleted ones
*/
void QXcbConnection::updateScreens(const xcb_randr_notify_event_t *event)
{
    if (event->subCode == XCB_RANDR_NOTIFY_CRTC_CHANGE) {
        xcb_randr_crtc_change_t crtc = event->u.cc;
        QXcbVirtualDesktop *virtualDesktop = virtualDesktopForRootWindow(crtc.window);
        if (!virtualDesktop)
            // Not for us
            return;

        QXcbScreen *screen = findScreenForCrtc(crtc.window, crtc.crtc);
        qCDebug(lcQpaScreen) << "QXcbConnection: XCB_RANDR_NOTIFY_CRTC_CHANGE:" << crtc.crtc
                             << "mode" << crtc.mode << "relevant screen" << screen;
        // Only update geometry when there's a valid mode on the CRTC
        // CRTC with node mode could mean that output has been disabled, and we'll
        // get RRNotifyOutputChange notification for that.
        if (screen && crtc.mode) {
            if (crtc.rotation == XCB_RANDR_ROTATION_ROTATE_90 ||
                crtc.rotation == XCB_RANDR_ROTATION_ROTATE_270)
                std::swap(crtc.width, crtc.height);
            screen->updateGeometry(QRect(crtc.x, crtc.y, crtc.width, crtc.height), crtc.rotation);
            if (screen->mode() != crtc.mode)
                screen->updateRefreshRate(crtc.mode);
        }

    } else if (event->subCode == XCB_RANDR_NOTIFY_OUTPUT_CHANGE) {
        xcb_randr_output_change_t output = event->u.oc;
        QXcbVirtualDesktop *virtualDesktop = virtualDesktopForRootWindow(output.window);
        if (!virtualDesktop)
            // Not for us
            return;

        QXcbScreen *screen = findScreenForOutput(output.window, output.output);
        qCDebug(lcQpaScreen) << "QXcbConnection: XCB_RANDR_NOTIFY_OUTPUT_CHANGE:" << output.output;

        if (screen && output.connection == XCB_RANDR_CONNECTION_DISCONNECTED) {
            qCDebug(lcQpaScreen) << "screen" << screen->name() << "has been disconnected";
            destroyScreen(screen);
        } else if (!screen && output.connection == XCB_RANDR_CONNECTION_CONNECTED) {
            // New XRandR output is available and it's enabled
            if (output.crtc != XCB_NONE && output.mode != XCB_NONE) {
                auto outputInfo = Q_XCB_REPLY(xcb_randr_get_output_info, xcb_connection(),
                                              output.output, output.config_timestamp);
                // Find a fake screen
                const auto scrs = virtualDesktop->screens();
                for (QPlatformScreen *scr : scrs) {
                    QXcbScreen *xcbScreen = static_cast<QXcbScreen *>(scr);
                    if (xcbScreen->output() == XCB_NONE) {
                        screen = xcbScreen;
                        break;
                    }
                }

                if (screen) {
                    QString nameWas = screen->name();
                    // Transform the fake screen into a physical screen
                    screen->setOutput(output.output, outputInfo.get());
                    updateScreen(screen, output);
                    qCDebug(lcQpaScreen) << "output" << screen->name()
                                         << "is connected and enabled; was fake:" << nameWas;
                } else {
                    screen = createScreen(virtualDesktop, output, outputInfo.get());
                    qCDebug(lcQpaScreen) << "output" << screen->name() << "is connected and enabled";
                }
            }
        } else if (screen) {
            if (output.crtc == XCB_NONE && output.mode == XCB_NONE) {
                // Screen has been disabled
                auto outputInfo = Q_XCB_REPLY(xcb_randr_get_output_info, xcb_connection(),
                                              output.output, output.config_timestamp);
                if (outputInfo->crtc == XCB_NONE) {
                    qCDebug(lcQpaScreen) << "output" << screen->name() << "has been disabled";
                    destroyScreen(screen);
                } else {
                    qCDebug(lcQpaScreen) << "output" << screen->name() << "has been temporarily disabled for the mode switch";
                    // Reset crtc to skip RRCrtcChangeNotify events,
                    // because they may be invalid in the middle of the mode switch
                    screen->setCrtc(XCB_NONE);
                }
            } else {
                updateScreen(screen, output);
                qCDebug(lcQpaScreen) << "output has changed" << screen;
            }
        }

        qCDebug(lcQpaScreen) << "updateScreens: primary output is" << qAsConst(m_screens).first()->name();
    }
}

bool QXcbConnection::checkOutputIsPrimary(xcb_window_t rootWindow, xcb_randr_output_t output)
{
    auto primary = Q_XCB_REPLY(xcb_randr_get_output_primary, xcb_connection(), rootWindow);
    if (!primary)
        qWarning("failed to get the primary output of the screen");

    const bool isPrimary = primary ? (primary->output == output) : false;

    return isPrimary;
}

void QXcbConnection::updateScreen(QXcbScreen *screen, const xcb_randr_output_change_t &outputChange)
{
    screen->setCrtc(outputChange.crtc); // Set the new crtc, because it can be invalid
    screen->updateGeometry(outputChange.config_timestamp);
    if (screen->mode() != outputChange.mode)
        screen->updateRefreshRate(outputChange.mode);
    // Only screen which belongs to the primary virtual desktop can be a primary screen
    if (screen->screenNumber() == primaryScreenNumber()) {
        if (!screen->isPrimary() && checkOutputIsPrimary(outputChange.window, outputChange.output)) {
            screen->setPrimary(true);

            // If the screen became primary, reshuffle the order in QGuiApplicationPrivate
            const int idx = m_screens.indexOf(screen);
            if (idx > 0) {
                qAsConst(m_screens).first()->setPrimary(false);
                m_screens.swapItemsAt(0, idx);
            }
            screen->virtualDesktop()->setPrimaryScreen(screen);
            QWindowSystemInterface::handlePrimaryScreenChanged(screen);
        }
    }
}

QXcbScreen *QXcbConnection::createScreen(QXcbVirtualDesktop *virtualDesktop,
                                         const xcb_randr_output_change_t &outputChange,
                                         xcb_randr_get_output_info_reply_t *outputInfo)
{
    QXcbScreen *screen = new QXcbScreen(this, virtualDesktop, outputChange.output, outputInfo);
    // Only screen which belongs to the primary virtual desktop can be a primary screen
    if (screen->screenNumber() == primaryScreenNumber())
        screen->setPrimary(checkOutputIsPrimary(outputChange.window, outputChange.output));

    if (screen->isPrimary()) {
        if (!m_screens.isEmpty())
            qAsConst(m_screens).first()->setPrimary(false);

        m_screens.prepend(screen);
    } else {
        m_screens.append(screen);
    }
    virtualDesktop->addScreen(screen);
    QWindowSystemInterface::handleScreenAdded(screen, screen->isPrimary());

    return screen;
}

void QXcbConnection::destroyScreen(QXcbScreen *screen)
{
    QXcbVirtualDesktop *virtualDesktop = screen->virtualDesktop();
    if (virtualDesktop->screens().count() == 1) {
        // If there are no other screens on the same virtual desktop,
        // then transform the physical screen into a fake screen.
        const QString nameWas = screen->name();
        screen->setOutput(XCB_NONE, nullptr);
        qCDebug(lcQpaScreen) << "transformed" << nameWas << "to fake" << screen;
    } else {
        // There is more than one screen on the same virtual desktop, remove the screen
        m_screens.removeOne(screen);
        virtualDesktop->removeScreen(screen);

        // When primary screen is removed, set the new primary screen
        // which belongs to the primary virtual desktop.
        if (screen->isPrimary()) {
            QXcbScreen *newPrimary = static_cast<QXcbScreen *>(virtualDesktop->screens().at(0));
            newPrimary->setPrimary(true);
            const int idx = m_screens.indexOf(newPrimary);
            if (idx > 0)
                m_screens.swapItemsAt(0, idx);
            QWindowSystemInterface::handlePrimaryScreenChanged(newPrimary);
        }

        qCDebug(lcQpaScreen) << "destroyScreen: destroy" << screen;
        QWindowSystemInterface::handleScreenRemoved(screen);
    }
}

void QXcbConnection::updateScreen_monitor(QXcbScreen *screen, xcb_randr_monitor_info_t *monitorInfo, xcb_timestamp_t timestamp)
{
    screen->setMonitor(monitorInfo, timestamp);

    if (screen->isPrimary()) {
        const int idx = m_screens.indexOf(screen);
        if (idx > 0) {
            qAsConst(m_screens).first()->setPrimary(false);
            m_screens.swapItemsAt(0, idx);
        }
        screen->virtualDesktop()->setPrimaryScreen(screen);
        QWindowSystemInterface::handlePrimaryScreenChanged(screen);
    }
    qCDebug(lcQpaScreen) << "updateScreen_monitor: update" << screen << "(Primary:" << screen->isPrimary() << ")";
}

QXcbScreen *QXcbConnection::createScreen_monitor(QXcbVirtualDesktop *virtualDesktop, xcb_randr_monitor_info_t *monitorInfo, xcb_timestamp_t timestamp)
{
    QXcbScreen *screen = new QXcbScreen(this, virtualDesktop, monitorInfo, timestamp);

    if (screen->isPrimary()) {
        if (!m_screens.isEmpty())
            qAsConst(m_screens).first()->setPrimary(false);

        m_screens.prepend(screen);
    } else {
        m_screens.append(screen);
    }
    qCDebug(lcQpaScreen) << "createScreen_monitor: adding" << screen << "(Primary:" << screen->isPrimary() << ")";
    virtualDesktop->addScreen(screen);
    QWindowSystemInterface::handleScreenAdded(screen, screen->isPrimary());
    return screen;
}

QXcbVirtualDesktop *QXcbConnection::virtualDesktopForNumber(int n) const
{
    for (QXcbVirtualDesktop *virtualDesktop : m_virtualDesktops) {
        if (virtualDesktop->number() == n)
            return virtualDesktop;
    }

    return nullptr;
}

QXcbScreen *QXcbConnection::findScreenForMonitorInfo(const QList<QPlatformScreen *> &screens, xcb_randr_monitor_info_t *monitorInfo)
{
    for (int i = 0; i < screens.size(); ++i) {
        auto s = static_cast<QXcbScreen*>(screens[i]);
        if (s->m_monitor && monitorInfo) {
            QByteArray ba1 = atomName(s->m_monitor->name);
            QByteArray ba2 = atomName(monitorInfo->name);
            if (ba1 == ba2)
                return s;
        }
    }

    return nullptr;
}

void QXcbConnection::initializeScreens(bool initialized)
{
    xcb_screen_iterator_t it = xcb_setup_roots_iterator(setup());
    int xcbScreenNumber = 0;    // screen number in the xcb sense
    QXcbScreen *primaryScreen = nullptr;
    while (it.rem) {
        if (isAtLeastXRandR15())
            initializeScreensFromMonitor(&it, xcbScreenNumber, primaryScreen, initialized);
        else if (isAtLeastXRandR12())
            initializeScreensFromOutput(&it, xcbScreenNumber, primaryScreen);

        xcb_screen_next(&it);
        ++xcbScreenNumber;
    }

    for (QXcbVirtualDesktop *virtualDesktop : qAsConst(m_virtualDesktops))
        virtualDesktop->subscribeToXFixesSelectionNotify();

    if (m_virtualDesktops.isEmpty()) {
        qFatal("QXcbConnection: no screens available");
    } else {
        // Ensure the primary screen is first on the list
        if (primaryScreen) {
            if (qAsConst(m_screens).first() != primaryScreen) {
                m_screens.removeOne(primaryScreen);
                m_screens.prepend(primaryScreen);
            }
        }

        // Push the screens to QGuiApplication
        if (!initialized) {
            for (QXcbScreen *screen : qAsConst(m_screens)) {
                qCDebug(lcQpaScreen) << "adding" << screen << "(Primary:" << screen->isPrimary() << ")";
                QWindowSystemInterface::handleScreenAdded(screen, screen->isPrimary());
            }
        }

        if (!m_screens.isEmpty())
            qCDebug(lcQpaScreen) << "initializeScreens: primary output is" << qAsConst(m_screens).first()->name();
    }
}

void QXcbConnection::initializeScreensFromOutput(xcb_screen_iterator_t *it, int xcbScreenNumber, QXcbScreen *primaryScreen)
{
    // Each "screen" in xcb terminology is a virtual desktop,
    // potentially a collection of separate juxtaposed monitors.
    // But we want a separate QScreen for each output (e.g. DVI-I-1, VGA-1, etc.)
    // which will become virtual siblings.
    xcb_screen_t *xcbScreen = it->data;
    QXcbVirtualDesktop *virtualDesktop = new QXcbVirtualDesktop(this, xcbScreen, xcbScreenNumber);
    m_virtualDesktops.append(virtualDesktop);
    QList<QPlatformScreen *> siblings;
    if (isAtLeastXRandR12()) {
        // RRGetScreenResourcesCurrent is fast but it may return nothing if the
        // configuration is not initialized wrt to the hardware. We should call
        // RRGetScreenResources in this case.
        auto resources_current = Q_XCB_REPLY(xcb_randr_get_screen_resources_current,
                                                xcb_connection(), xcbScreen->root);
        decltype(Q_XCB_REPLY(xcb_randr_get_screen_resources,
                                xcb_connection(), xcbScreen->root)) resources;
        if (!resources_current) {
            qWarning("failed to get the current screen resources");
        } else {
            xcb_timestamp_t timestamp = 0;
            xcb_randr_output_t *outputs = nullptr;
            int outputCount = xcb_randr_get_screen_resources_current_outputs_length(resources_current.get());
            if (outputCount) {
                timestamp = resources_current->config_timestamp;
                outputs = xcb_randr_get_screen_resources_current_outputs(resources_current.get());
            } else {
                resources = Q_XCB_REPLY(xcb_randr_get_screen_resources,
                                        xcb_connection(), xcbScreen->root);
                if (!resources) {
                    qWarning("failed to get the screen resources");
                } else {
                    timestamp = resources->config_timestamp;
                    outputCount = xcb_randr_get_screen_resources_outputs_length(resources.get());
                    outputs = xcb_randr_get_screen_resources_outputs(resources.get());
                }
            }

            if (outputCount) {
                auto primary = Q_XCB_REPLY(xcb_randr_get_output_primary, xcb_connection(), xcbScreen->root);
                if (!primary) {
                    qWarning("failed to get the primary output of the screen");
                } else {
                    for (int i = 0; i < outputCount; i++) {
                        auto output = Q_XCB_REPLY_UNCHECKED(xcb_randr_get_output_info,
                                                            xcb_connection(), outputs[i], timestamp);
                        // Invalid, disconnected or disabled output
                        if (!output)
                            continue;

                        if (output->connection != XCB_RANDR_CONNECTION_CONNECTED) {
                            qCDebug(lcQpaScreen, "Output %s is not connected", qPrintable(
                                        QString::fromUtf8((const char*)xcb_randr_get_output_info_name(output.get()),
                                                            xcb_randr_get_output_info_name_length(output.get()))));
                            continue;
                        }

                        if (output->crtc == XCB_NONE) {
                            qCDebug(lcQpaScreen, "Output %s is not enabled", qPrintable(
                                        QString::fromUtf8((const char*)xcb_randr_get_output_info_name(output.get()),
                                                            xcb_randr_get_output_info_name_length(output.get()))));
                            continue;
                        }

                        QXcbScreen *screen = new QXcbScreen(this, virtualDesktop, outputs[i], output.get());
                        siblings << screen;
                        m_screens << screen;

                        // There can be multiple outputs per screen, use either
                        // the first or an exact match.  An exact match isn't
                        // always available if primary->output is XCB_NONE
                        // or currently disconnected output.
                        if (primaryScreenNumber() == xcbScreenNumber) {
                            if (!primaryScreen || (primary && outputs[i] == primary->output)) {
                                if (primaryScreen)
                                    primaryScreen->setPrimary(false);
                                primaryScreen = screen;
                                primaryScreen->setPrimary(true);
                                siblings.prepend(siblings.takeLast());
                            }
                        }
                    }
                }
            }
        }
    }
    if (siblings.isEmpty()) {
        // If there are no XRandR outputs or XRandR extension is missing,
        // then create a fake/legacy screen.
        QXcbScreen *screen = new QXcbScreen(this, virtualDesktop, XCB_NONE, nullptr);
        qCDebug(lcQpaScreen) << "created fake screen" << screen;
        m_screens << screen;
        if (primaryScreenNumber() == xcbScreenNumber) {
            primaryScreen = screen;
            primaryScreen->setPrimary(true);
        }
        siblings << screen;
    }
    virtualDesktop->setScreens(std::move(siblings));
}

void QXcbConnection::initializeScreensFromMonitor(xcb_screen_iterator_t *it, int xcbScreenNumber, QXcbScreen *primaryScreen, bool initialized)
{
    // Each "screen" in xcb terminology is a virtual desktop,
    // potentially a collection of separate juxtaposed monitors.
    // But we want a separate QScreen for each output (e.g. DVI-I-1, VGA-1, etc.)
    // which will become virtual siblings.
    xcb_screen_t *xcbScreen = it->data;
    QXcbVirtualDesktop *virtualDesktop = nullptr;
    if (initialized)
        virtualDesktop = virtualDesktopForNumber(xcbScreenNumber);
    if (!virtualDesktop) {
        virtualDesktop = new QXcbVirtualDesktop(this, xcbScreen, xcbScreenNumber);
        m_virtualDesktops.append(virtualDesktop);
    }
    QList<QPlatformScreen*> old = virtualDesktop->m_screens;
    if (initialized)
        m_screens.clear();

    QList<QPlatformScreen *> siblings;

    xcb_randr_get_monitors_cookie_t monitors_c = xcb_randr_get_monitors(xcb_connection(), xcbScreen->root, 1);
    xcb_randr_get_monitors_reply_t *monitors_r = xcb_randr_get_monitors_reply(xcb_connection(), monitors_c, nullptr);

    if (!monitors_r) {
        qWarning("RANDR GetMonitors failed; this should not be possible");
        return;
    }

    xcb_randr_monitor_info_iterator_t monitor_iter = xcb_randr_get_monitors_monitors_iterator(monitors_r);
    while (monitor_iter.rem) {
        xcb_randr_monitor_info_t *monitor_info = monitor_iter.data;
        QXcbScreen *screen = nullptr;
        if (!initialized) {
            screen = new QXcbScreen(this, virtualDesktop, monitor_info, monitors_r->timestamp);
            m_screens << screen;
        } else {
            screen = findScreenForMonitorInfo(virtualDesktop->m_screens, monitor_info);
            if (!screen) {
                screen = createScreen_monitor(virtualDesktop, monitor_info, monitors_r->timestamp);
                QHighDpiScaling::updateHighDpiScaling();
            } else {
                m_screens << screen;
                updateScreen_monitor(screen, monitor_info, monitors_r->timestamp);
                old.removeAll(screen);
            }
        }

        siblings << screen;

        xcb_randr_monitor_info_next(&monitor_iter);
    }

    if (siblings.isEmpty()) {
        QXcbScreen *screen = nullptr;
        if (initialized && !old.isEmpty()) {
            // If there are no other screens on the same virtual desktop,
            // then transform the physical screen into a fake screen.
            screen = static_cast<QXcbScreen *>(old.takeFirst());
            const QString nameWas = screen->name();
            screen->setMonitor(nullptr, XCB_NONE);
            qCDebug(lcQpaScreen) << "transformed" << nameWas << "to fake" << screen;
        } else {
            // If there are no XRandR outputs or XRandR extension is missing,
            // then create a fake/legacy screen.
            screen = new QXcbScreen(this, virtualDesktop, nullptr);
            qCDebug(lcQpaScreen) << "create a fake screen: " << screen;
        }

        if (primaryScreenNumber() == xcbScreenNumber) {
            primaryScreen = screen;
            primaryScreen->setPrimary(true);
        }
        siblings << screen;
        m_screens << screen;
    }

    if (initialized) {
        for (QPlatformScreen *ps : old) {
            virtualDesktop->removeScreen(ps);
            qCDebug(lcQpaScreen) << "destroy screen: " << ps;
            QWindowSystemInterface::handleScreenRemoved(ps);
        }
    }

    virtualDesktop->setScreens(std::move(siblings));
}