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
displaymanager.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 <qarkui/displaymanager.h>
5
6#include <QtCore/private/qcore_ohos_p.h>
7#include <QtCore/qspan.h>
8#include <memory>
9#include <qarkui/qarkuiutils.h>
10#include <qohosdisplayinfo.h>
11#include <qohosjsutils.h>
12#include <window_manager/oh_display_info.h>
13#include <window_manager/oh_display_manager.h>
14
16
17namespace QArkUi {
18
19namespace {
20
21const std::string displayCallbackNameChangeEvent = "change";
22const std::string displayCallbackNameAddEvent = "add";
23const std::string displayCallbackNameRemoveEvent = "remove";
24
25std::shared_ptr<::NativeDisplayManager_DisplaysInfo> enumerateAllDisplaysOrFail()
26{
27 ::NativeDisplayManager_DisplaysInfo *displayListPtr = nullptr;
28 callArkUiOrFailOnErrorResult(
29 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_CreateAllDisplays),
30 &displayListPtr);
31 if (displayListPtr == nullptr) {
32 qOhosReportFatalErrorAndAbort(
33 "%s: OH_NativeDisplayManager_CreateAllDisplays returned empty displayListPtr",
34 Q_FUNC_INFO);
35 }
36
37 return std::shared_ptr<::NativeDisplayManager_DisplaysInfo>(
38 displayListPtr,
39 [](::NativeDisplayManager_DisplaysInfo *displayListPtr) {
40 callArkUi(
41 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_DestroyAllDisplays),
42 displayListPtr);
43 });
44}
45
46QPoint getGlobalDisplayOffsetOfDisplay(QOhosDisplayInfo::JsDisplayId displayId)
47{
48 std::int32_t x = 0;
49 std::int32_t y = 0;
50 auto getDisplayPositionResult = callArkUi(
51 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_GetDisplayPosition),
52 static_cast<std::uint64_t>(displayId.value()),
53 &x, &y);
54
55 QPoint result;
56 if (getDisplayPositionResult == ::DISPLAY_MANAGER_OK) {
57 result = QPoint(x, y);
58 } else if (getDisplayPositionResult == ::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM) {
59 // NOTE: According to the documentation Queries for other screens return DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM.
60 // 0, 0 offset is a default for those cases as they are not part of the virtual desktop
61 result = QPoint();
62 } else {
63 qOhosReportFatalErrorAndAbort(
64 "OH_NativeDisplayManager_GetDisplayPosition returned unexpected error: %d", getDisplayPositionResult);
65 }
66
67 return result;
68}
69
70}
71
72void QOhosDisplayManager::registerDisplayCallbackListener(
73 QNapi::Object displayModule, const std::string &eventName,
74 QOhosConsumer<QtOhos::JsState &, JsDisplayId> handleFunction)
75{
76 m_destroyNotifiers.push_back(
77 QtOhos::registerOnOffMethodsBasedEventHandler(
78 displayModule, eventName,
79 [handleFunction = std::move(handleFunction)](const QtOhos::CallbackInfo &cbInfo) {
80 auto changedDisplayIdValue = cbInfo.getFirstArg<QNapi::Number>(Q_FUNC_INFO);
81 auto changedDisplayId = JsDisplayId{changedDisplayIdValue};
82 handleFunction(cbInfo.jsState(), changedDisplayId);
83 }));
84}
85
86bool QOhosDisplayManager::tryRegisterDisplay(
87 QtOhos::JsState &jsState, JsDisplayId displayId)
88{
89 auto optDisplay = QOhosDisplayInfo::tryGetDisplayById(jsState, displayId);
90 if (!optDisplay.hasValue()) {
91 qOhosPrintfError(
92 "%s: Display with id: %f went missing during its registration.",
93 Q_FUNC_INFO, displayId.value());
94 return false;
95 }
96
97 auto displayInfo = QOhosDisplayInfo::makeFromOhosDisplayObject(jsState, optDisplay.value());
98 if (displayInfo.shouldIgnoreDisplay())
99 return false;
100
101 auto availableAreaChangeHandle = QtOhos::registerOnOffMethodsBasedEventHandler(
102 optDisplay.value(),
103 "availableAreaChange",
104 [this, displayId](const QtOhos::CallbackInfo &cbInfo) {
105 auto availableArea = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
106 auto availableAreaQRectF = QRectF(
107 availableArea.get<QNapi::Number>("left"),
108 availableArea.get<QNapi::Number>("top"),
109 availableArea.get<QNapi::Number>("width"),
110 availableArea.get<QNapi::Number>("height"));
111 m_availableAreaChangedCb(cbInfo.jsState(), displayId, availableAreaQRectF);
112 });
113
114 bool added = false;
115 std::tie(std::ignore, added) = m_perDisplayDestroyNotifiers.insert(
116 std::make_pair(displayId, std::move(availableAreaChangeHandle)));
117 if (added)
118 m_registeredDisplayInfos.push_back(displayInfo);
119 else
120 qOhosPrintfError("Duplicate display added event for display id: %f", displayId.value());
121
122 return added;
123}
124
126 QtOhos::JsState &jsState, CreateInfo createInfo)
127{
128 return std::shared_ptr<QOhosDisplayManager>(new QOhosDisplayManager(jsState, std::move(createInfo)));
129}
130
132{
133 return m_registeredDisplayInfos;
134}
135
136QOhosDisplayManager::QOhosDisplayManager(QtOhos::JsState &jsState, CreateInfo createInfo)
137{
138 rebuildRegisteredDisplayList(jsState);
139
140 m_availableAreaChangedCb = std::move(createInfo.displayAvailableAreaChangedCb);
141
142 const std::string displayEventNames[] = {
143 displayCallbackNameChangeEvent,
144 displayCallbackNameAddEvent,
145 displayCallbackNameRemoveEvent,
146 };
147
148 auto displaysUpdatedCb = QtOhos::moveToSharedPtr(std::move(createInfo.displaysUpdatedCb));
149
150 auto displayModule = jsState.eval<QNapi::Object>("@ohos.display");
151 for (const auto &eventName: displayEventNames) {
152 registerDisplayCallbackListener(
153 displayModule,
154 eventName,
155 [this, displaysUpdatedCb](QtOhos::JsState &jsState, JsDisplayId) {
156 rebuildRegisteredDisplayList(jsState);
157 (*displaysUpdatedCb)(jsState, m_registeredDisplayInfos);
158 });
159 }
160}
161
162void QOhosDisplayManager::rebuildRegisteredDisplayList(QtOhos::JsState &jsState)
163{
164 m_perDisplayDestroyNotifiers = {};
165 m_registeredDisplayInfos = {};
166
167 auto displayListPtr = enumerateAllDisplaysOrFail();
168 for (const auto &nativeDisplayInfo : QSpan(displayListPtr->displaysInfo, displayListPtr->displaysLength)) {
169 if (!tryRegisterDisplay(jsState, JsDisplayId(nativeDisplayInfo.id))) {
170 qOhosPrintfError(
171 "%s: Failed to register display (%d) during display initialization.",
172 Q_FUNC_INFO,
173 nativeDisplayInfo.id);
174 }
175 }
176}
177
178QPoint mapFromDisplayToGlobal(const QPoint &displayOffset, QOhosDisplayInfo::JsDisplayId jsDisplayId)
179{
180 return getGlobalDisplayOffsetOfDisplay(jsDisplayId) + displayOffset;
181}
182
183}
184
185QT_END_NAMESPACE
std::vector< QOhosDisplayInfo > getRegisteredDisplayInfos()
static std::shared_ptr< QOhosDisplayManager > create(QtOhos::JsState &jsState, CreateInfo createInfo)
QOhosDisplayInfo::JsDisplayId JsDisplayId
QPoint mapFromDisplayToGlobal(const QPoint &displayOffset, QOhosDisplayInfo::JsDisplayId jsDisplayId)
Combined button and popup list for selecting options.