166f3657fSopenharmony_ci/*
266f3657fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
366f3657fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
466f3657fSopenharmony_ci * you may not use this file except in compliance with the License.
566f3657fSopenharmony_ci * You may obtain a copy of the License at
666f3657fSopenharmony_ci *
766f3657fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
866f3657fSopenharmony_ci *
966f3657fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1066f3657fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1166f3657fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1266f3657fSopenharmony_ci * See the License for the specific language governing permissions and
1366f3657fSopenharmony_ci * limitations under the License.
1466f3657fSopenharmony_ci */
1566f3657fSopenharmony_ci
1666f3657fSopenharmony_ci#include "screen_client.h"
1766f3657fSopenharmony_ci
1866f3657fSopenharmony_ci#include "dscreen_errcode.h"
1966f3657fSopenharmony_ci#include "dscreen_log.h"
2066f3657fSopenharmony_ci#include "screen_client_window_adapter.h"
2166f3657fSopenharmony_ci
2266f3657fSopenharmony_cinamespace OHOS {
2366f3657fSopenharmony_cinamespace DistributedHardware {
2466f3657fSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(ScreenClient);
2566f3657fSopenharmony_ciint32_t ScreenClient::AddWindow(std::shared_ptr<WindowProperty> &windowProperty)
2666f3657fSopenharmony_ci{
2766f3657fSopenharmony_ci    if (windowProperty == nullptr) {
2866f3657fSopenharmony_ci        DHLOGE("windowProperty is nullptr.");
2966f3657fSopenharmony_ci        return ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR;
3066f3657fSopenharmony_ci    }
3166f3657fSopenharmony_ci    int32_t windowId = ++windowId_;
3266f3657fSopenharmony_ci    sptr<Surface> surface = ScreenClientWindowAdapter::GetInstance().CreateWindow(windowProperty, windowId);
3366f3657fSopenharmony_ci    if (surface == nullptr) {
3466f3657fSopenharmony_ci        DHLOGE("Surface is nullptr.");
3566f3657fSopenharmony_ci        return ERR_DH_SCREEN_SCREENCLIENT_ADD_WINDOW_ERROR;
3666f3657fSopenharmony_ci    }
3766f3657fSopenharmony_ci    {
3866f3657fSopenharmony_ci        std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
3966f3657fSopenharmony_ci        surfaceMap_.emplace(windowId, surface);
4066f3657fSopenharmony_ci    }
4166f3657fSopenharmony_ci    DHLOGI("Add window ID = %{public}" PRId32 " success.", windowId);
4266f3657fSopenharmony_ci    return windowId;
4366f3657fSopenharmony_ci}
4466f3657fSopenharmony_ci
4566f3657fSopenharmony_ciint32_t ScreenClient::ShowWindow(int32_t windowId)
4666f3657fSopenharmony_ci{
4766f3657fSopenharmony_ci    {
4866f3657fSopenharmony_ci        std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
4966f3657fSopenharmony_ci        auto iter = surfaceMap_.find(windowId);
5066f3657fSopenharmony_ci        if (iter == surfaceMap_.end()) {
5166f3657fSopenharmony_ci            DHLOGE("windowId ID = %{public}" PRId32 " is non-existent.", windowId);
5266f3657fSopenharmony_ci            return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR;
5366f3657fSopenharmony_ci        }
5466f3657fSopenharmony_ci    }
5566f3657fSopenharmony_ci    int32_t ret = ScreenClientWindowAdapter::GetInstance().ShowWindow(windowId);
5666f3657fSopenharmony_ci    if (ret != DH_SUCCESS) {
5766f3657fSopenharmony_ci        DHLOGE("Show window ID = %{public}" PRId32 " failed.", windowId);
5866f3657fSopenharmony_ci        return ret;
5966f3657fSopenharmony_ci    }
6066f3657fSopenharmony_ci    DHLOGI("Show window ID = %{public}" PRId32 " success.", windowId);
6166f3657fSopenharmony_ci    return ret;
6266f3657fSopenharmony_ci}
6366f3657fSopenharmony_ci
6466f3657fSopenharmony_ciint32_t ScreenClient::HideWindow(int32_t windowId)
6566f3657fSopenharmony_ci{
6666f3657fSopenharmony_ci    {
6766f3657fSopenharmony_ci        std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
6866f3657fSopenharmony_ci        auto iter = surfaceMap_.find(windowId);
6966f3657fSopenharmony_ci        if (iter == surfaceMap_.end()) {
7066f3657fSopenharmony_ci            DHLOGE("windowId ID = %{public}" PRId32 " is non-existent.", windowId);
7166f3657fSopenharmony_ci            return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR;
7266f3657fSopenharmony_ci        }
7366f3657fSopenharmony_ci    }
7466f3657fSopenharmony_ci    int32_t ret = ScreenClientWindowAdapter::GetInstance().HideWindow(windowId);
7566f3657fSopenharmony_ci    if (ret != DH_SUCCESS) {
7666f3657fSopenharmony_ci        DHLOGE("Hide window ID = %{public}" PRId32 " failed.", windowId);
7766f3657fSopenharmony_ci        return ret;
7866f3657fSopenharmony_ci    }
7966f3657fSopenharmony_ci    DHLOGI("Hide window ID = %{public}" PRId32 " success.", windowId);
8066f3657fSopenharmony_ci    return ret;
8166f3657fSopenharmony_ci}
8266f3657fSopenharmony_ci
8366f3657fSopenharmony_ciint32_t ScreenClient::MoveWindow(int32_t windowId, int32_t startX, int32_t startY)
8466f3657fSopenharmony_ci{
8566f3657fSopenharmony_ci    {
8666f3657fSopenharmony_ci        std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
8766f3657fSopenharmony_ci        auto iter = surfaceMap_.find(windowId);
8866f3657fSopenharmony_ci        if (iter == surfaceMap_.end()) {
8966f3657fSopenharmony_ci            DHLOGE("windowId ID = %{public}" PRId32 " is non-existent.", windowId);
9066f3657fSopenharmony_ci            return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR;
9166f3657fSopenharmony_ci        }
9266f3657fSopenharmony_ci    }
9366f3657fSopenharmony_ci    int32_t ret = ScreenClientWindowAdapter::GetInstance().MoveWindow(windowId, startX, startY);
9466f3657fSopenharmony_ci    if (ret != DH_SUCCESS) {
9566f3657fSopenharmony_ci        DHLOGE("Move window ID = %{public}" PRId32 " failed.", windowId);
9666f3657fSopenharmony_ci        return ret;
9766f3657fSopenharmony_ci    }
9866f3657fSopenharmony_ci    DHLOGD("Move window ID = %{public}" PRId32 " success.", windowId);
9966f3657fSopenharmony_ci    return ret;
10066f3657fSopenharmony_ci}
10166f3657fSopenharmony_ci
10266f3657fSopenharmony_cisptr<Surface> ScreenClient::GetSurface(int32_t windowId)
10366f3657fSopenharmony_ci{
10466f3657fSopenharmony_ci    sptr<Surface> surface = nullptr;
10566f3657fSopenharmony_ci    {
10666f3657fSopenharmony_ci        std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
10766f3657fSopenharmony_ci        auto iter = surfaceMap_.find(windowId);
10866f3657fSopenharmony_ci        if (iter == surfaceMap_.end()) {
10966f3657fSopenharmony_ci            DHLOGE("windowId ID = %{public}" PRId32 " is non-existent.", windowId);
11066f3657fSopenharmony_ci            return nullptr;
11166f3657fSopenharmony_ci        }
11266f3657fSopenharmony_ci        surface = iter->second;
11366f3657fSopenharmony_ci    }
11466f3657fSopenharmony_ci    DHLOGD("Get surface ID = %{public}" PRId32 " success.", windowId);
11566f3657fSopenharmony_ci    return surface;
11666f3657fSopenharmony_ci}
11766f3657fSopenharmony_ci
11866f3657fSopenharmony_ciint32_t ScreenClient::RemoveWindow(int32_t windowId)
11966f3657fSopenharmony_ci{
12066f3657fSopenharmony_ci    {
12166f3657fSopenharmony_ci        std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
12266f3657fSopenharmony_ci        auto iter = surfaceMap_.find(windowId);
12366f3657fSopenharmony_ci        if (iter == surfaceMap_.end()) {
12466f3657fSopenharmony_ci            DHLOGE("windowId ID = %{public}" PRId32 " is non-existent.", windowId);
12566f3657fSopenharmony_ci            return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
12666f3657fSopenharmony_ci        }
12766f3657fSopenharmony_ci        surfaceMap_.erase(windowId);
12866f3657fSopenharmony_ci    }
12966f3657fSopenharmony_ci    int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(windowId);
13066f3657fSopenharmony_ci    if (ret != DH_SUCCESS) {
13166f3657fSopenharmony_ci        DHLOGE("windowId ID = %{public}" PRId32 " remove failed.", windowId);
13266f3657fSopenharmony_ci        return ret;
13366f3657fSopenharmony_ci    }
13466f3657fSopenharmony_ci    DHLOGD("windowId ID = %{public}" PRId32 " remove success.", windowId);
13566f3657fSopenharmony_ci    return ret;
13666f3657fSopenharmony_ci}
13766f3657fSopenharmony_ci
13866f3657fSopenharmony_ciint32_t ScreenClient::DestroyAllWindow()
13966f3657fSopenharmony_ci{
14066f3657fSopenharmony_ci    DHLOGD("DestroyAllWindow");
14166f3657fSopenharmony_ci    int32_t ret = ScreenClientWindowAdapter::GetInstance().DestroyAllWindow();
14266f3657fSopenharmony_ci    if (ret != DH_SUCCESS) {
14366f3657fSopenharmony_ci        DHLOGE("DestroyAllWindow failed.");
14466f3657fSopenharmony_ci        return ret;
14566f3657fSopenharmony_ci    }
14666f3657fSopenharmony_ci    std::lock_guard<std::mutex> dataLock(surfaceMapMutex_);
14766f3657fSopenharmony_ci    surfaceMap_.clear();
14866f3657fSopenharmony_ci    windowId_ = INVALID_WINDOW_ID;
14966f3657fSopenharmony_ci    return DH_SUCCESS;
15066f3657fSopenharmony_ci}
15166f3657fSopenharmony_ci} // namespace DistributedHardware
15266f3657fSopenharmony_ci} // namespace OHOS