/* * Copyright (C) 2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "window_adapter.h" #include #include "global.h" #include "window.h" #include "wm_common.h" namespace OHOS { namespace MiscServices { using namespace OHOS::Rosen; using WMError = OHOS::Rosen::WMError; // LCOV_EXCL_START WindowAdapter::~WindowAdapter() { } WindowAdapter &WindowAdapter::GetInstance() { static WindowAdapter windowAdapter; return windowAdapter; } // LCOV_EXCL_STOP void WindowAdapter::GetFocusInfo(OHOS::Rosen::FocusChangeInfo &focusInfo, uint64_t displayId) { #ifdef SCENE_BOARD_ENABLE WindowManagerLite::GetInstance().GetFocusWindowInfo(focusInfo, displayId); #else WindowManager::GetInstance().GetFocusWindowInfo(focusInfo, displayId); #endif } bool WindowAdapter::GetCallingWindowInfo( const uint32_t windId, const int32_t userId, CallingWindowInfo &callingWindowInfo) { #ifdef SCENE_BOARD_ENABLE IMSA_HILOGD("[%{public}d,%{public}d] run in.", userId, windId); callingWindowInfo.windowId_ = static_cast(windId); callingWindowInfo.userId_ = userId; auto wmErr = WindowManagerLite::GetInstance().GetCallingWindowInfo(callingWindowInfo); if (wmErr != WMError::WM_OK) { IMSA_HILOGE("[%{public}d,%{public}d,%{public}d] failed to get calling window info.", userId, windId, wmErr); return false; } IMSA_HILOGD("callingWindowInfo:%{public}s", WindowDisplayChangeListener::CallingWindowInfoToString(callingWindowInfo).c_str()); return true; #else IMSA_HILOGE("capability not supported"); return false; #endif } // LCOV_EXCL_START void WindowAdapter::RegisterCallingWindowInfoChangedListener(const WindowDisplayChangeHandler &handle) { #ifdef SCENE_BOARD_ENABLE sptr listener = new (std::nothrow) WindowDisplayChangeListener(handle); if (listener == nullptr) { IMSA_HILOGE("failed to create listener"); return; } auto wmErr = WindowManagerLite::GetInstance().RegisterCallingWindowDisplayChangedListener(listener); IMSA_HILOGI("register focus changed listener ret: %{public}d", wmErr); #endif } // LCOV_EXCL_STOP bool WindowAdapter::ListWindowInfo(std::vector> &windowInfos) { #ifdef SCENE_BOARD_ENABLE WindowInfoOption option; WMError ret = WindowManagerLite::GetInstance().ListWindowInfo(option, windowInfos); if (ret != WMError::WM_OK) { IMSA_HILOGE("ListWindowInfo failed, ret: %{public}d", ret); return false; } return true; #else IMSA_HILOGE("capability not supported"); return false; #endif } uint64_t WindowAdapter::GetDisplayIdByWindowId(int32_t callingWindowId) { #ifdef SCENE_BOARD_ENABLE if (callingWindowId == DEFAULT_DISPLAY_ID) { FocusChangeInfo info; WindowManagerLite::GetInstance().GetFocusWindowInfo(info); callingWindowId = info.windowId_; } std::vector> windowInfos; if (!ListWindowInfo(windowInfos)) { return DEFAULT_DISPLAY_ID; } auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingWindowId](const auto &windowInfo) { if (windowInfo == nullptr) { return false; } return windowInfo->windowMetaInfo.windowId == callingWindowId; }); if (iter == windowInfos.end()) { IMSA_HILOGE("not found window info with windowId: %{public}d", callingWindowId); return DEFAULT_DISPLAY_ID; } auto callingDisplayId = (*iter)->windowDisplayInfo.displayId; IMSA_HILOGD("window windowId: %{public}d, displayId: %{public}" PRIu64 "", callingWindowId, callingDisplayId); return callingDisplayId; #else IMSA_HILOGI("capability not supported"); return DEFAULT_DISPLAY_ID; #endif } uint64_t WindowAdapter::GetDisplayIdByPid(int64_t callingPid) { #ifdef SCENE_BOARD_ENABLE std::vector> windowInfos; if (!ListWindowInfo(windowInfos)) { return DEFAULT_DISPLAY_ID; } auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingPid](const auto &windowInfo) { if (windowInfo == nullptr) { return false; } return windowInfo->windowMetaInfo.pid == callingPid; }); if (iter == windowInfos.end()) { IMSA_HILOGE("not found window info with pid: %{public}" PRId64 "", callingPid); return DEFAULT_DISPLAY_ID; } auto callingDisplayId = (*iter)->windowDisplayInfo.displayId; IMSA_HILOGD("window pid: %{public}" PRId64 ", displayId: %{public}" PRIu64 "", callingPid, callingDisplayId); return callingDisplayId; #else IMSA_HILOGI("capability not supported"); return DEFAULT_DISPLAY_ID; #endif } bool WindowAdapter::GetDisplayId(int64_t callingPid, uint64_t &displayId) { displayId = DEFAULT_DISPLAY_ID; #ifdef SCENE_BOARD_ENABLE std::vector> windowInfos; if (!ListWindowInfo(windowInfos)) { return false; } auto iter = std::find_if(windowInfos.begin(), windowInfos.end(), [&callingPid](const auto &windowInfo) { if (windowInfo == nullptr) { return false; } return windowInfo->windowMetaInfo.pid == callingPid; }); if (iter == windowInfos.end()) { IMSA_HILOGE("not found window info with pid: %{public}" PRId64 "", callingPid); return false; } auto callingDisplayId = (*iter)->windowDisplayInfo.displayId; IMSA_HILOGD("window pid: %{public}" PRId64 ", displayId: %{public}" PRIu64 "", callingPid, callingDisplayId); displayId = callingDisplayId; return true; #else IMSA_HILOGI("capability not supported"); return true; #endif } } // namespace MiscServices } // namespace OHOS