Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosscreenmanager.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qohosscreenmanager.h>
5#include <cstdint>
6#include <memory>
7#include <utility>
8#include <qarkui/displaymanager.h>
9#include <qohosdisplayinfo.h>
10#include <qohosjsutils.h>
11#include <qohosplatformscreen.h>
12#include <qpa/qwindowsysteminterface.h>
13
15
16QOhosScreenManager::QOhosScreenManager()
17{
18 auto selfRef = QtOhos::QThreadSafeRef<QOhosScreenManager>(this);
19
20 std::vector<QOhosDisplayInfo> registeredDisplays;
21 QtOhos::runInJsThreadAndWait([&](QtOhos::JsState &jsState) {
22 m_jsScopeData = QtOhos::makeProxyWithJsThreadDeleter(
23 QArkUi::QOhosDisplayManager::create(
24 jsState, QArkUi::QOhosDisplayManager::CreateInfo{
25 .displaysUpdatedCb = [selfRef](QtOhos::JsState &, std::vector<QOhosDisplayInfo> displayInfos) {
26 selfRef.visitInQtThreadIfAlive([displayInfos = std::move(displayInfos)](QOhosScreenManager &self) {
27 self.rebuildScreenList(displayInfos);
28 });
29 },
30 .displayAvailableAreaChangedCb = [selfRef](QtOhos::JsState &, JsDisplayId displayId, QRectF availableArea) {
31 selfRef.visitInQtThreadIfAlive([displayId, availableArea](QOhosScreenManager &self) {
32 self.handleDisplayAvailableAreaChanged(displayId, availableArea);
33 });
34 },
35 }));
36 registeredDisplays = m_jsScopeData->getRegisteredDisplayInfos();
37 },
38 Q_FUNC_INFO);
39
40 rebuildScreenList(registeredDisplays);
41}
42
43QOhosScreenManager::QOhosPlatformScreenHolder::QOhosPlatformScreenHolder(
44 std::unique_ptr<QOhosPlatformScreen> screen)
45{
46 m_platformScreen = screen.get();
47 QWindowSystemInterface::handleScreenAdded(screen.release());
48}
49
50QOhosPlatformScreen *QOhosScreenManager::QOhosPlatformScreenHolder::platformScreenOrNull() const
51{
52 return m_platformScreen;
53}
54
55QOhosOptional<QOhosDisplayInfo> QOhosScreenManager::QOhosPlatformScreenHolder::displayInfoOrEmpty() const
56{
57 return m_platformScreen != nullptr
58 ? makeQOhosOptional(m_platformScreen->displayInfo())
59 : makeEmptyQOhosOptional();
60}
61
62QOhosOptional<QOhosDisplayInfo::JsDisplayId> QOhosScreenManager::QOhosPlatformScreenHolder::displayIdOrEmpty() const
63{
64 return qTransform(
65 displayInfoOrEmpty(),
66 [](const QOhosDisplayInfo &displayInfo) {
67 return displayInfo.id;
68 });
69}
70
71QOhosScreenManager::QOhosPlatformScreenHolder::~QOhosPlatformScreenHolder()
72{
73 if (m_platformScreen != nullptr)
74 QWindowSystemInterface::handleScreenRemoved(m_platformScreen);
75}
76
77QOhosScreenManager::QOhosPlatformScreenHolder *
78QOhosScreenManager::platformScreenHolderForDisplayIdOrNull(JsDisplayId displayId) const
79{
80 auto it = std::find_if(
81 m_displays.begin(), m_displays.end(),
82 [&](const std::unique_ptr<QOhosPlatformScreenHolder> &platformScreenHolder) {
83 return platformScreenHolder->displayIdOrEmpty() == displayId;
84 });
85
86 return it != m_displays.end()
87 ? it->get()
88 : nullptr;
89}
90
92{
93 auto *platformScreenHolder = platformScreenHolderForDisplayIdOrNull(displayId);
94 return platformScreenHolder != nullptr
95 ? platformScreenHolder->platformScreenOrNull()
96 : nullptr;
97}
98
100{
101 auto *platformScreen = platformScreenForDisplayIdOrNull(displayId);
102 if (platformScreen == nullptr)
103 qOhosReportFatalErrorAndAbort("Failed to find platform screen for display id: %f", displayId.value());
104 return platformScreen;
105}
106
107void QOhosScreenManager::handleDisplayAvailableAreaChanged(
108 JsDisplayId jsDisplayId, QRectF availableArea)
109{
110 auto *platformScreen = platformScreenForDisplayIdOrNull(jsDisplayId);
111 if (platformScreen != nullptr)
112 platformScreen->setAvailableGeometry(availableArea.toRect());
113}
114
115void QOhosScreenManager::rebuildScreenList(std::vector<QOhosDisplayInfo> updatedDisplayInfos)
116{
117 const auto primaryDisplayId = JsDisplayId(0);
118
119 if (updatedDisplayInfos.empty())
120 qOhosReportFatalErrorAndAbort("Empty display list. This should not happen");
121
122 auto currentDisplays = std::exchange(m_displays, {});
123
124 QOhosPlatformScreen *primaryScreen = nullptr;
125 for (const auto &displayInfo: updatedDisplayInfos) {
126 auto availablePlatformScreenHolderIt = std::find_if(
127 currentDisplays.begin(), currentDisplays.end(),
128 [](const std::unique_ptr<QOhosPlatformScreenHolder> &platformScreenHolder) {
129 return platformScreenHolder && platformScreenHolder->platformScreenOrNull() != nullptr;
130 });
131
132 auto platformScreenHolder = availablePlatformScreenHolderIt != currentDisplays.end()
133 ? std::move(*availablePlatformScreenHolderIt)
134 : std::make_unique<QOhosPlatformScreenHolder>(
135 std::make_unique<QOhosPlatformScreen>(
136 displayInfo,
137 [this]() {
138 std::vector<QOhosPlatformScreen *> result;
139 for (const auto &screenHolder : m_displays) {
140 auto *platformScreen = screenHolder->platformScreenOrNull();
141 if (platformScreen != nullptr)
142 result.push_back(platformScreen);
143 }
144 return result;
145 }));
146
147 auto *platformScreen = platformScreenHolder->platformScreenOrNull();
148 if (Q_UNLIKELY(platformScreen == nullptr))
149 qOhosReportFatalErrorAndAbort("platformScreenHolder->platformScreenOrNull() == nullptr. This should not happen.");
150
151 platformScreen->setDisplayInfo(displayInfo);
152 if (displayInfo.id == primaryDisplayId)
153 primaryScreen = platformScreen;
154
155 m_displays.push_back(std::move(platformScreenHolder));
156 }
157
158 if (primaryScreen != nullptr) {
159 QWindowSystemInterface::handlePrimaryScreenChanged(primaryScreen);
160 } else {
161 qCCritical(QtForOhos) << Q_FUNC_INFO << "no primary screen was found in the updated list";
162 }
163
164}
165
166QT_END_NAMESPACE
QOhosPlatformScreen * platformScreenForDisplayIdOrNull(QOhosDisplayInfo::JsDisplayId displayId) const
QOhosPlatformScreen * platformScreenForDisplayIdOrFail(QOhosDisplayInfo::JsDisplayId displayId) const
Combined button and popup list for selecting options.