21const std::string displayCallbackNameChangeEvent =
"change";
22const std::string displayCallbackNameAddEvent =
"add";
23const std::string displayCallbackNameRemoveEvent =
"remove";
25std::shared_ptr<::NativeDisplayManager_DisplaysInfo> enumerateAllDisplaysOrFail()
27 ::NativeDisplayManager_DisplaysInfo *displayListPtr =
nullptr;
28 callArkUiOrFailOnErrorResult(
29 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_CreateAllDisplays),
31 if (displayListPtr ==
nullptr) {
32 qOhosReportFatalErrorAndAbort(
33 "%s: OH_NativeDisplayManager_CreateAllDisplays returned empty displayListPtr",
37 return std::shared_ptr<::NativeDisplayManager_DisplaysInfo>(
39 [](::NativeDisplayManager_DisplaysInfo *displayListPtr) {
41 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_DestroyAllDisplays),
46QPoint getGlobalDisplayOffsetOfDisplay(
QOhosDisplayInfo::JsDisplayId displayId)
50 auto getDisplayPositionResult = callArkUi(
51 Q_OHOS_NAMED_FUNC(::OH_NativeDisplayManager_GetDisplayPosition),
52 static_cast<
std::uint64_t>(displayId.value()),
56 if (getDisplayPositionResult == ::DISPLAY_MANAGER_OK) {
57 result = QPoint(x, y);
58 }
else if (getDisplayPositionResult == ::DISPLAY_MANAGER_ERROR_ILLEGAL_PARAM) {
63 qOhosReportFatalErrorAndAbort(
64 "OH_NativeDisplayManager_GetDisplayPosition returned unexpected error: %d", getDisplayPositionResult);
73 QNapi::Object displayModule,
const std::string &eventName,
74 QOhosConsumer<QtOhos::JsState &, JsDisplayId> handleFunction)
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);
89 auto optDisplay = QOhosDisplayInfo::tryGetDisplayById(jsState, displayId);
90 if (!optDisplay.hasValue()) {
92 "%s: Display with id: %f went missing during its registration.",
93 Q_FUNC_INFO, displayId.value());
97 auto displayInfo = QOhosDisplayInfo::makeFromOhosDisplayObject(jsState, optDisplay.value());
98 if (displayInfo.shouldIgnoreDisplay())
101 auto availableAreaChangeHandle = QtOhos::registerOnOffMethodsBasedEventHandler(
103 "availableAreaChange",
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);
115 std::tie(std::ignore, added) = m_perDisplayDestroyNotifiers.insert(
116 std::make_pair(displayId, std::move(availableAreaChangeHandle)));
118 m_registeredDisplayInfos.push_back(displayInfo);
120 qOhosPrintfError(
"Duplicate display added event for display id: %f", displayId.value());
133 return m_registeredDisplayInfos;
138 rebuildRegisteredDisplayList(jsState);
140 m_availableAreaChangedCb = std::move(createInfo.displayAvailableAreaChangedCb);
142 const std::string displayEventNames[] = {
143 displayCallbackNameChangeEvent,
144 displayCallbackNameAddEvent,
145 displayCallbackNameRemoveEvent,
148 auto displaysUpdatedCb = QtOhos::moveToSharedPtr(std::move(createInfo.displaysUpdatedCb));
150 auto displayModule = jsState.eval<QNapi::Object>(
"@ohos.display");
151 for (
const auto &eventName: displayEventNames) {
152 registerDisplayCallbackListener(
155 [
this, displaysUpdatedCb](QtOhos::JsState &jsState, JsDisplayId) {
156 rebuildRegisteredDisplayList(jsState);
157 (*displaysUpdatedCb)(jsState, m_registeredDisplayInfos);
164 m_perDisplayDestroyNotifiers = {};
165 m_registeredDisplayInfos = {};
167 auto displayListPtr = enumerateAllDisplaysOrFail();
168 for (
const auto &nativeDisplayInfo : QSpan(displayListPtr->displaysInfo, displayListPtr->displaysLength)) {
169 if (!tryRegisterDisplay(jsState, JsDisplayId(nativeDisplayInfo.id))) {
171 "%s: Failed to register display (%d) during display initialization.",
173 nativeDisplayInfo.id);
180 return getGlobalDisplayOffsetOfDisplay(jsDisplayId) + displayOffset;