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