1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "inner_window.h"
17
18#include "window_manager_hilog.h"
19#include "surface_draw.h"
20
21namespace OHOS {
22namespace Rosen {
23namespace {
24constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "InnerWindow"};
25const std::string IMAGE_PLACE_HOLDER_PNG_PATH = "/etc/window/resources/bg_place_holder.png";
26}
27WM_IMPLEMENT_SINGLE_INSTANCE(PlaceHolderWindow)
28
29void PlaceholderWindowListener::OnTouchOutside() const
30{
31    WLOGFD("place holder touch outside");
32    PlaceHolderWindow::GetInstance().Destroy();
33}
34
35void PlaceholderWindowListener::AfterUnfocused()
36{
37    WLOGFD("place holder after unfocused");
38    PlaceHolderWindow::GetInstance().Destroy();
39}
40
41bool PlaceholderInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
42{
43    WLOGFD("place holder get key event");
44    PlaceHolderWindow::GetInstance().Destroy();
45    return true;
46}
47
48bool PlaceholderInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
49{
50    WLOGFD("place holder get point event");
51    PlaceHolderWindow::GetInstance().Destroy();
52    return true;
53}
54
55bool PlaceholderInputEventConsumer::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
56{
57    // do nothing
58    return false;
59}
60
61void PlaceHolderWindow::Create(std::string name, DisplayId displyId, Rect rect, WindowMode mode)
62{
63    WLOGFD("create inner display id: %{public}" PRIu64"", displyId);
64    if (window_ != nullptr) {
65        WLOGFW("window has created.");
66        return;
67    }
68    sptr<WindowOption> option = new (std::nothrow) WindowOption();
69    if (option == nullptr) {
70        WLOGFE("window option is nullptr.");
71        return;
72    }
73    option->SetFocusable(false);
74    option->SetWindowMode(mode);
75    option->SetWindowRect(rect);
76    option->SetWindowType(WindowType::WINDOW_TYPE_PLACEHOLDER);
77    window_ = Window::Create(name, option);
78    if (window_ == nullptr) {
79        WLOGFE("window is nullptr.");
80        return;
81    }
82    window_->AddWindowFlag(WindowFlag::WINDOW_FLAG_FORBID_SPLIT_MOVE);
83    RegisterWindowListener();
84    SetInputEventConsumer();
85    if (!OHOS::Rosen::SurfaceDraw::DrawImage(window_->GetSurfaceNode(), rect.width_, rect.height_,
86        IMAGE_PLACE_HOLDER_PNG_PATH)) {
87        WLOGE("draw surface failed");
88        return;
89    }
90    window_->Show();
91    WLOGFD("create palce holder Window end");
92}
93
94void PlaceHolderWindow::RegisterWindowListener()
95{
96    if (window_ == nullptr) {
97        WLOGFE("Window is nullptr, register window listener failed.");
98        return;
99    }
100    if (windowListener_ == nullptr) {
101        windowListener_ = new (std::nothrow) PlaceholderWindowListener();
102    }
103    window_->RegisterTouchOutsideListener(windowListener_);
104    window_->RegisterLifeCycleListener(windowListener_);
105}
106
107void PlaceHolderWindow::UnRegisterWindowListener()
108{
109    if (window_ == nullptr || windowListener_ == nullptr) {
110        WLOGFE("Window or listener is nullptr, unregister window listener failed.");
111        return;
112    }
113    window_->UnregisterTouchOutsideListener(windowListener_);
114    window_->UnregisterLifeCycleListener(windowListener_);
115}
116
117void PlaceHolderWindow::SetInputEventConsumer()
118{
119    if (window_ == nullptr) {
120        WLOGFE("Window is nullptr, set window input event consumer failed.");
121        return;
122    }
123    if (inputEventConsumer_ == nullptr) {
124        inputEventConsumer_ = std::make_shared<PlaceholderInputEventConsumer>();
125    }
126    window_->SetInputEventConsumer(inputEventConsumer_);
127}
128
129void PlaceHolderWindow::Destroy()
130{
131    WLOGI("destroy place holder window begin.");
132    if (window_ != nullptr) {
133        WLOGI("destroy place holder window not nullptr.");
134        UnRegisterWindowListener();
135        window_->SetInputEventConsumer(nullptr);
136        window_->Destroy();
137    }
138    window_ = nullptr;
139    WLOGI("destroy place holder window end.");
140}
141} // Rosen
142} // OHOS