1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qwindowsscreen.h"
41 #include "qwindowscontext.h"
42 #include "qwindowswindow.h"
43 #include "qwindowsintegration.h"
44 #include "qwindowscursor.h"
45 #include "qwindowstheme.h"
46 
47 #include <QtCore/qt_windows.h>
48 
49 #include <QtCore/qsettings.h>
50 #include <QtGui/qpixmap.h>
51 #include <QtGui/qguiapplication.h>
52 #include <qpa/qwindowsysteminterface.h>
53 #include <private/qhighdpiscaling_p.h>
54 #include <private/qwindowsfontdatabase_p.h>
55 #include <QtGui/qscreen.h>
56 
57 #include <QtCore/qdebug.h>
58 
59 QT_BEGIN_NAMESPACE
60 
deviceDPI(HDC hdc)61 static inline QDpi deviceDPI(HDC hdc)
62 {
63     return QDpi(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY));
64 }
65 
monitorDPI(HMONITOR hMonitor)66 static inline QDpi monitorDPI(HMONITOR hMonitor)
67 {
68     if (QWindowsContext::shcoredll.isValid()) {
69         UINT dpiX;
70         UINT dpiY;
71         if (SUCCEEDED(QWindowsContext::shcoredll.getDpiForMonitor(hMonitor, 0, &dpiX, &dpiY)))
72             return QDpi(dpiX, dpiY);
73     }
74     return {0, 0};
75 }
76 
77 using WindowsScreenDataList = QVector<QWindowsScreenData>;
78 
monitorData(HMONITOR hMonitor,QWindowsScreenData * data)79 static bool monitorData(HMONITOR hMonitor, QWindowsScreenData *data)
80 {
81     MONITORINFOEX info;
82     memset(&info, 0, sizeof(MONITORINFOEX));
83     info.cbSize = sizeof(MONITORINFOEX);
84     if (GetMonitorInfo(hMonitor, &info) == FALSE)
85         return false;
86 
87     data->hMonitor = hMonitor;
88     data->geometry = QRect(QPoint(info.rcMonitor.left, info.rcMonitor.top), QPoint(info.rcMonitor.right - 1, info.rcMonitor.bottom - 1));
89     data->availableGeometry = QRect(QPoint(info.rcWork.left, info.rcWork.top), QPoint(info.rcWork.right - 1, info.rcWork.bottom - 1));
90     data->name = QString::fromWCharArray(info.szDevice);
91     if (data->name == u"WinDisc") {
92         data->flags |= QWindowsScreenData::LockScreen;
93     } else {
94         if (const HDC hdc = CreateDC(info.szDevice, nullptr, nullptr, nullptr)) {
95             const QDpi dpi = monitorDPI(hMonitor);
96             data->dpi = dpi.first > 0 ? dpi : deviceDPI(hdc);
97             data->depth = GetDeviceCaps(hdc, BITSPIXEL);
98             data->format = data->depth == 16 ? QImage::Format_RGB16 : QImage::Format_RGB32;
99             data->physicalSizeMM = QSizeF(GetDeviceCaps(hdc, HORZSIZE), GetDeviceCaps(hdc, VERTSIZE));
100             const int refreshRate = GetDeviceCaps(hdc, VREFRESH);
101             if (refreshRate > 1) // 0,1 means hardware default.
102                 data->refreshRateHz = refreshRate;
103             DeleteDC(hdc);
104         } else {
105             qWarning("%s: Unable to obtain handle for monitor '%s', defaulting to %g DPI.",
106                      __FUNCTION__, qPrintable(QString::fromWCharArray(info.szDevice)),
107                      data->dpi.first);
108         } // CreateDC() failed
109     } // not lock screen
110     data->orientation = data->geometry.height() > data->geometry.width() ?
111                        Qt::PortraitOrientation : Qt::LandscapeOrientation;
112     // EnumDisplayMonitors (as opposed to EnumDisplayDevices) enumerates only
113     // virtual desktop screens.
114     data->flags |= QWindowsScreenData::VirtualDesktop;
115     if (info.dwFlags & MONITORINFOF_PRIMARY) {
116         data->flags |= QWindowsScreenData::PrimaryScreen;
117         if ((data->flags & QWindowsScreenData::LockScreen) == 0)
118             QWindowsFontDatabase::setDefaultVerticalDPI(data->dpi.second);
119     }
120     return true;
121 }
122 
123 // from QDesktopWidget, taking WindowsScreenDataList as LPARAM
monitorEnumCallback(HMONITOR hMonitor,HDC,LPRECT,LPARAM p)124 BOOL QT_WIN_CALLBACK monitorEnumCallback(HMONITOR hMonitor, HDC, LPRECT, LPARAM p)
125 {
126     QWindowsScreenData data;
127     if (monitorData(hMonitor, &data)) {
128         auto *result = reinterpret_cast<WindowsScreenDataList *>(p);
129         // QWindowSystemInterface::handleScreenAdded() documentation specifies that first
130         // added screen will be the primary screen, so order accordingly.
131         // Note that the side effect of this policy is that there is no way to change primary
132         // screen reported by Qt, unless we want to delete all existing screens and add them
133         // again whenever primary screen changes.
134         if (data.flags & QWindowsScreenData::PrimaryScreen)
135             result->prepend(data);
136         else
137             result->append(data);
138     }
139     return TRUE;
140 }
141 
monitorData()142 static inline WindowsScreenDataList monitorData()
143 {
144     WindowsScreenDataList result;
145     EnumDisplayMonitors(nullptr, nullptr, monitorEnumCallback, reinterpret_cast<LPARAM>(&result));
146     return result;
147 }
148 
149 #ifndef QT_NO_DEBUG_STREAM
operator <<(QDebug dbg,const QWindowsScreenData & d)150 static QDebug operator<<(QDebug dbg, const QWindowsScreenData &d)
151 {
152     QDebugStateSaver saver(dbg);
153     dbg.nospace();
154     dbg.noquote();
155     dbg << "Screen \"" << d.name << "\" "
156         << d.geometry.width() << 'x' << d.geometry.height() << '+' << d.geometry.x() << '+' << d.geometry.y()
157         << " avail: "
158         << d.availableGeometry.width() << 'x' << d.availableGeometry.height() << '+' << d.availableGeometry.x() << '+' << d.availableGeometry.y()
159         << " physical: " << d.physicalSizeMM.width() << 'x' << d.physicalSizeMM.height()
160         << " DPI: " << d.dpi.first << 'x' << d.dpi.second << " Depth: " << d.depth
161         << " Format: " << d.format
162         << " hMonitor: " << d.hMonitor;
163     if (d.flags & QWindowsScreenData::PrimaryScreen)
164         dbg << " primary";
165     if (d.flags & QWindowsScreenData::VirtualDesktop)
166         dbg << " virtual desktop";
167     if (d.flags & QWindowsScreenData::LockScreen)
168         dbg << " lock screen";
169     return dbg;
170 }
171 #endif // !QT_NO_DEBUG_STREAM
172 
173 /*!
174     \class QWindowsScreen
175     \brief Windows screen.
176     \sa QWindowsScreenManager
177     \internal
178 */
179 
QWindowsScreen(const QWindowsScreenData & data)180 QWindowsScreen::QWindowsScreen(const QWindowsScreenData &data) :
181     m_data(data)
182 #ifndef QT_NO_CURSOR
183     , m_cursor(new QWindowsCursor(this))
184 #endif
185 {
186 }
187 
188 Q_GUI_EXPORT QPixmap qt_pixmapFromWinHBITMAP(HBITMAP bitmap, int hbitmapFormat = 0);
189 
grabWindow(WId window,int xIn,int yIn,int width,int height) const190 QPixmap QWindowsScreen::grabWindow(WId window, int xIn, int yIn, int width, int height) const
191 {
192     QSize windowSize;
193     int x = xIn;
194     int y = yIn;
195     HWND hwnd = reinterpret_cast<HWND>(window);
196     if (hwnd) {
197         RECT r;
198         GetClientRect(hwnd, &r);
199         windowSize = QSize(r.right - r.left, r.bottom - r.top);
200     } else {
201         // Grab current screen. The client rectangle of GetDesktopWindow() is the
202         // primary screen, but it is possible to grab other screens from it.
203         hwnd = GetDesktopWindow();
204         const QRect screenGeometry = geometry();
205         windowSize = screenGeometry.size();
206         x += screenGeometry.x();
207         y += screenGeometry.y();
208     }
209 
210     if (width < 0)
211         width = windowSize.width() - xIn;
212     if (height < 0)
213         height = windowSize.height() - yIn;
214 
215     // Create and setup bitmap
216     HDC display_dc = GetDC(nullptr);
217     HDC bitmap_dc = CreateCompatibleDC(display_dc);
218     HBITMAP bitmap = CreateCompatibleBitmap(display_dc, width, height);
219     HGDIOBJ null_bitmap = SelectObject(bitmap_dc, bitmap);
220 
221     // copy data
222     HDC window_dc = GetDC(hwnd);
223     BitBlt(bitmap_dc, 0, 0, width, height, window_dc, x, y, SRCCOPY | CAPTUREBLT);
224 
225     // clean up all but bitmap
226     ReleaseDC(hwnd, window_dc);
227     SelectObject(bitmap_dc, null_bitmap);
228     DeleteDC(bitmap_dc);
229 
230     const QPixmap pixmap = qt_pixmapFromWinHBITMAP(bitmap);
231 
232     DeleteObject(bitmap);
233     ReleaseDC(nullptr, display_dc);
234 
235     return pixmap;
236 }
237 
238 /*!
239     \brief Find a top level window taking the flags of ChildWindowFromPointEx.
240 */
241 
topLevelAt(const QPoint & point) const242 QWindow *QWindowsScreen::topLevelAt(const QPoint &point) const
243 {
244     QWindow *result = nullptr;
245     if (QWindow *child = QWindowsScreen::windowAt(point, CWP_SKIPINVISIBLE))
246         result = QWindowsWindow::topLevelOf(child);
247     if (QWindowsContext::verbose > 1)
248         qCDebug(lcQpaWindows) <<__FUNCTION__ << point << result;
249     return result;
250 }
251 
windowAt(const QPoint & screenPoint,unsigned flags)252 QWindow *QWindowsScreen::windowAt(const QPoint &screenPoint, unsigned flags)
253 {
254     QWindow* result = nullptr;
255     if (QPlatformWindow *bw = QWindowsContext::instance()->
256             findPlatformWindowAt(GetDesktopWindow(), screenPoint, flags))
257         result = bw->window();
258     if (QWindowsContext::verbose > 1)
259         qCDebug(lcQpaWindows) <<__FUNCTION__ << screenPoint << " returns " << result;
260     return result;
261 }
262 
263 /*!
264     \brief Determine siblings in a virtual desktop system.
265 
266     Self is by definition a sibling, else collect all screens
267     within virtual desktop.
268 */
269 
virtualSiblings() const270 QList<QPlatformScreen *> QWindowsScreen::virtualSiblings() const
271 {
272     QList<QPlatformScreen *> result;
273     if (m_data.flags & QWindowsScreenData::VirtualDesktop) {
274         const QWindowsScreenManager::WindowsScreenList screens
275             = QWindowsContext::instance()->screenManager().screens();
276         for (QWindowsScreen *screen : screens) {
277             if (screen->data().flags & QWindowsScreenData::VirtualDesktop)
278                 result.push_back(screen);
279         }
280     } else {
281         result.push_back(const_cast<QWindowsScreen *>(this));
282     }
283     return result;
284 }
285 
286 /*!
287     \brief Notify QWindowSystemInterface about changes of a screen and synchronize data.
288 */
289 
handleChanges(const QWindowsScreenData & newData)290 void QWindowsScreen::handleChanges(const QWindowsScreenData &newData)
291 {
292     m_data.physicalSizeMM = newData.physicalSizeMM;
293 
294     if (m_data.hMonitor != newData.hMonitor) {
295         qCDebug(lcQpaWindows) << "Monitor" << m_data.name
296             << "has had its hMonitor handle changed from"
297             << m_data.hMonitor << "to" << newData.hMonitor;
298         m_data.hMonitor = newData.hMonitor;
299     }
300 
301     // QGuiApplicationPrivate::processScreenGeometryChange() checks and emits
302     // DPI and orientation as well, so, assign new values and emit DPI first.
303     const bool geometryChanged = m_data.geometry != newData.geometry
304         || m_data.availableGeometry != newData.availableGeometry;
305     const bool dpiChanged = !qFuzzyCompare(m_data.dpi.first, newData.dpi.first)
306         || !qFuzzyCompare(m_data.dpi.second, newData.dpi.second);
307     const bool orientationChanged = m_data.orientation != newData.orientation;
308     m_data.dpi = newData.dpi;
309     m_data.orientation = newData.orientation;
310     m_data.geometry = newData.geometry;
311     m_data.availableGeometry = newData.availableGeometry;
312 
313     if (dpiChanged) {
314         QWindowSystemInterface::handleScreenLogicalDotsPerInchChange(screen(),
315                                                                      newData.dpi.first,
316                                                                      newData.dpi.second);
317     }
318     if (orientationChanged)
319        QWindowSystemInterface::handleScreenOrientationChange(screen(), newData.orientation);
320     if (geometryChanged) {
321         QWindowSystemInterface::handleScreenGeometryChange(screen(),
322                                                            newData.geometry, newData.availableGeometry);
323     }
324 }
325 
handle() const326 HMONITOR QWindowsScreen::handle() const
327 {
328     return m_data.hMonitor;
329 }
330 
virtualGeometry(const QPlatformScreen * screen)331 QRect QWindowsScreen::virtualGeometry(const QPlatformScreen *screen) // cf QScreen::virtualGeometry()
332 {
333     QRect result;
334     const auto siblings = screen->virtualSiblings();
335     for (const QPlatformScreen *sibling : siblings)
336         result |= sibling->geometry();
337     return result;
338 }
339 
340 enum OrientationPreference : DWORD // matching Win32 API ORIENTATION_PREFERENCE
341 {
342     orientationPreferenceNone = 0,
343     orientationPreferenceLandscape = 0x1,
344     orientationPreferencePortrait = 0x2,
345     orientationPreferenceLandscapeFlipped = 0x4,
346     orientationPreferencePortraitFlipped = 0x8
347 };
348 
setOrientationPreference(Qt::ScreenOrientation o)349 bool QWindowsScreen::setOrientationPreference(Qt::ScreenOrientation o)
350 {
351     bool result = false;
352     if (QWindowsContext::user32dll.setDisplayAutoRotationPreferences) {
353         DWORD orientationPreference = 0;
354         switch (o) {
355         case Qt::PrimaryOrientation:
356             orientationPreference = orientationPreferenceNone;
357             break;
358         case Qt::PortraitOrientation:
359             orientationPreference = orientationPreferencePortrait;
360             break;
361         case Qt::LandscapeOrientation:
362             orientationPreference = orientationPreferenceLandscape;
363             break;
364         case Qt::InvertedPortraitOrientation:
365             orientationPreference = orientationPreferencePortraitFlipped;
366             break;
367         case Qt::InvertedLandscapeOrientation:
368             orientationPreference = orientationPreferenceLandscapeFlipped;
369             break;
370         }
371         result = QWindowsContext::user32dll.setDisplayAutoRotationPreferences(orientationPreference);
372     }
373     return result;
374 }
375 
orientationPreference()376 Qt::ScreenOrientation QWindowsScreen::orientationPreference()
377 {
378     Qt::ScreenOrientation result = Qt::PrimaryOrientation;
379     if (QWindowsContext::user32dll.getDisplayAutoRotationPreferences) {
380         DWORD orientationPreference = 0;
381         if (QWindowsContext::user32dll.getDisplayAutoRotationPreferences(&orientationPreference)) {
382             switch (orientationPreference) {
383             case orientationPreferenceLandscape:
384                 result = Qt::LandscapeOrientation;
385                 break;
386             case orientationPreferencePortrait:
387                 result = Qt::PortraitOrientation;
388                 break;
389             case orientationPreferenceLandscapeFlipped:
390                 result = Qt::InvertedLandscapeOrientation;
391                 break;
392             case orientationPreferencePortraitFlipped:
393                 result = Qt::InvertedPortraitOrientation;
394                 break;
395             }
396         }
397     }
398     return result;
399 }
400 
401 /*!
402     \brief Queries ClearType settings to check the pixel layout
403 */
subpixelAntialiasingTypeHint() const404 QPlatformScreen::SubpixelAntialiasingType QWindowsScreen::subpixelAntialiasingTypeHint() const
405 {
406     QPlatformScreen::SubpixelAntialiasingType type = QPlatformScreen::subpixelAntialiasingTypeHint();
407     if (type == QPlatformScreen::Subpixel_None) {
408         QSettings settings(QLatin1String(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Avalon.Graphics\DISPLAY1)"),
409                            QSettings::NativeFormat);
410         int registryValue = settings.value(QLatin1String("PixelStructure"), -1).toInt();
411         switch (registryValue) {
412         case 0:
413             type = QPlatformScreen::Subpixel_None;
414             break;
415         case 1:
416             type = QPlatformScreen::Subpixel_RGB;
417             break;
418         case 2:
419             type = QPlatformScreen::Subpixel_BGR;
420             break;
421         default:
422             type = QPlatformScreen::Subpixel_None;
423             break;
424         }
425     }
426     return type;
427 }
428 
429 /*!
430     \class QWindowsScreenManager
431     \brief Manages a list of QWindowsScreen.
432 
433     Listens for changes and notifies QWindowSystemInterface about changed/
434     added/deleted screens.
435 
436     \sa QWindowsScreen
437     \internal
438 */
439 
440 QWindowsScreenManager::QWindowsScreenManager() = default;
441 
442 
isSingleScreen()443 bool QWindowsScreenManager::isSingleScreen()
444 {
445     return QWindowsContext::instance()->screenManager().screens().size() < 2;
446 }
447 
448 /*!
449     \brief Triggers synchronization of screens (WM_DISPLAYCHANGE).
450 
451     Subsequent events are compressed since WM_DISPLAYCHANGE is sent to
452     each top level window.
453 */
454 
handleDisplayChange(WPARAM wParam,LPARAM lParam)455 bool QWindowsScreenManager::handleDisplayChange(WPARAM wParam, LPARAM lParam)
456 {
457     const int newDepth = int(wParam);
458     const WORD newHorizontalResolution = LOWORD(lParam);
459     const WORD newVerticalResolution = HIWORD(lParam);
460     if (newDepth != m_lastDepth || newHorizontalResolution != m_lastHorizontalResolution
461         || newVerticalResolution != m_lastVerticalResolution) {
462         m_lastDepth = newDepth;
463         m_lastHorizontalResolution = newHorizontalResolution;
464         m_lastVerticalResolution = newVerticalResolution;
465         qCDebug(lcQpaWindows) << __FUNCTION__ << "Depth=" << newDepth
466             << ", resolution " << newHorizontalResolution << 'x' << newVerticalResolution;
467         handleScreenChanges();
468     }
469     return false;
470 }
471 
indexOfMonitor(const QWindowsScreenManager::WindowsScreenList & screens,const QString & monitorName)472 static inline int indexOfMonitor(const QWindowsScreenManager::WindowsScreenList &screens,
473                                  const QString &monitorName)
474 {
475     for (int i= 0; i < screens.size(); ++i)
476         if (screens.at(i)->data().name == monitorName)
477             return i;
478     return -1;
479 }
480 
indexOfMonitor(const WindowsScreenDataList & screenData,const QString & monitorName)481 static inline int indexOfMonitor(const WindowsScreenDataList &screenData,
482                                  const QString &monitorName)
483 {
484     for (int i = 0; i < screenData.size(); ++i)
485         if (screenData.at(i).name == monitorName)
486             return i;
487     return -1;
488 }
489 
490 // Move a window to a new virtual screen, accounting for varying sizes.
moveToVirtualScreen(QWindow * w,const QScreen * newScreen)491 static void moveToVirtualScreen(QWindow *w, const QScreen *newScreen)
492 {
493     QRect geometry = w->geometry();
494     const QRect oldScreenGeometry = w->screen()->geometry();
495     const QRect newScreenGeometry = newScreen->geometry();
496     QPoint relativePosition = geometry.topLeft() - oldScreenGeometry.topLeft();
497     if (oldScreenGeometry.size() != newScreenGeometry.size()) {
498         const qreal factor =
499             qreal(QPoint(newScreenGeometry.width(), newScreenGeometry.height()).manhattanLength()) /
500             qreal(QPoint(oldScreenGeometry.width(), oldScreenGeometry.height()).manhattanLength());
501         relativePosition = (QPointF(relativePosition) * factor).toPoint();
502     }
503     geometry.moveTopLeft(relativePosition);
504     w->setGeometry(geometry);
505 }
506 
removeScreen(int index)507 void QWindowsScreenManager::removeScreen(int index)
508 {
509     qCDebug(lcQpaWindows) << "Removing Monitor:" << m_screens.at(index)->data();
510     QScreen *screen = m_screens.at(index)->screen();
511     QScreen *primaryScreen = QGuiApplication::primaryScreen();
512     // QTBUG-38650: When a screen is disconnected, Windows will automatically
513     // move the Window to another screen. This will trigger a geometry change
514     // event, but unfortunately after the screen destruction signal. To prevent
515     // QtGui from automatically hiding the QWindow, pretend all Windows move to
516     // the primary screen first (which is likely the correct, final screen).
517     // QTBUG-39320: Windows does not automatically move WS_EX_TOOLWINDOW (dock) windows;
518     // move those manually.
519     if (screen != primaryScreen) {
520         unsigned movedWindowCount = 0;
521         const QWindowList tlws = QGuiApplication::topLevelWindows();
522         for (QWindow *w : tlws) {
523             if (w->screen() == screen && w->handle() && w->type() != Qt::Desktop) {
524                 if (w->isVisible() && w->windowState() != Qt::WindowMinimized
525                     && (QWindowsWindow::baseWindowOf(w)->exStyle() & WS_EX_TOOLWINDOW)) {
526                     moveToVirtualScreen(w, primaryScreen);
527                 } else {
528                     QWindowSystemInterface::handleWindowScreenChanged(w, primaryScreen);
529                 }
530                 ++movedWindowCount;
531             }
532         }
533         if (movedWindowCount)
534             QWindowSystemInterface::flushWindowSystemEvents();
535     }
536     QWindowSystemInterface::handleScreenRemoved(m_screens.takeAt(index));
537 }
538 
539 /*!
540     \brief Synchronizes the screen list, adds new screens, removes deleted
541     ones and propagates resolution changes to QWindowSystemInterface.
542 */
543 
handleScreenChanges()544 bool QWindowsScreenManager::handleScreenChanges()
545 {
546     // Look for changed monitors, add new ones
547     const WindowsScreenDataList newDataList = monitorData();
548     const bool lockScreen = newDataList.size() == 1 && (newDataList.front().flags & QWindowsScreenData::LockScreen);
549     bool primaryScreenChanged = false;
550     for (const QWindowsScreenData &newData : newDataList) {
551         const int existingIndex = indexOfMonitor(m_screens, newData.name);
552         if (existingIndex != -1) {
553             m_screens.at(existingIndex)->handleChanges(newData);
554             if (existingIndex == 0)
555                 primaryScreenChanged = true;
556         } else {
557             auto *newScreen = new QWindowsScreen(newData);
558             m_screens.push_back(newScreen);
559             QWindowSystemInterface::handleScreenAdded(newScreen,
560                                                              newData.flags & QWindowsScreenData::PrimaryScreen);
561             qCDebug(lcQpaWindows) << "New Monitor: " << newData;
562         }    // exists
563     }        // for new screens.
564     // Remove deleted ones but keep main monitors if we get only the
565     // temporary lock screen to avoid window recreation (QTBUG-33062).
566     if (!lockScreen) {
567         for (int i = m_screens.size() - 1; i >= 0; --i) {
568             if (indexOfMonitor(newDataList, m_screens.at(i)->data().name) == -1)
569                 removeScreen(i);
570         }     // for existing screens
571     }     // not lock screen
572     if (primaryScreenChanged) {
573         if (auto theme = QWindowsTheme::instance()) // QTBUG-85734/Wine
574             theme->refreshFonts();
575     }
576     return true;
577 }
578 
clearScreens()579 void QWindowsScreenManager::clearScreens()
580 {
581     // Delete screens in reverse order to avoid crash in case of multiple screens
582     while (!m_screens.isEmpty())
583         QWindowSystemInterface::handleScreenRemoved(m_screens.takeLast());
584 }
585 
screenAtDp(const QPoint & p) const586 const QWindowsScreen *QWindowsScreenManager::screenAtDp(const QPoint &p) const
587 {
588     for (QWindowsScreen *scr : m_screens) {
589         if (scr->geometry().contains(p))
590             return scr;
591     }
592     return nullptr;
593 }
594 
screenForHwnd(HWND hwnd) const595 const QWindowsScreen *QWindowsScreenManager::screenForHwnd(HWND hwnd) const
596 {
597     HMONITOR hMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONULL);
598     if (hMonitor == nullptr)
599         return nullptr;
600     const auto it =
601         std::find_if(m_screens.cbegin(), m_screens.cend(),
602                      [hMonitor](const QWindowsScreen *s)
603                      {
604                          return s->data().hMonitor == hMonitor
605                              && (s->data().flags & QWindowsScreenData::VirtualDesktop) != 0;
606                      });
607     return it != m_screens.cend() ? *it : nullptr;
608 }
609 
610 QT_END_NAMESPACE
611