1 /*
2  * Copyright (c) 2022-2023 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 "screen_client_window_adapter.h"
17 
18 #include "transaction/rs_interfaces.h"
19 #include "window_option.h"
20 #include "wm_common.h"
21 
22 #include "dscreen_constants.h"
23 #include "dscreen_errcode.h"
24 #include "dscreen_hisysevent.h"
25 #include "dscreen_log.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 IMPLEMENT_SINGLE_INSTANCE(ScreenClientWindowAdapter);
CreateWindow(std::shared_ptr<WindowProperty> & windowProperty,int32_t windowId)30 sptr<Surface> ScreenClientWindowAdapter::CreateWindow(std::shared_ptr<WindowProperty> &windowProperty,
31     int32_t windowId)
32 {
33     if (windowProperty == nullptr) {
34         DHLOGE("windowProperty is nullptr.");
35         return nullptr;
36     }
37     sptr<Rosen::WindowOption> option(new Rosen::WindowOption());
38     if (option == nullptr) {
39         DHLOGE("option is nullptr.");
40         return nullptr;
41     }
42     option->SetWindowType(Rosen::WindowType::WINDOW_TYPE_FLOAT);
43     option->SetWindowMode(Rosen::WindowMode::WINDOW_MODE_FLOATING);
44     option->SetDisplayId(windowProperty->displayId);
45     std::string windowName = SCREEN_CLIENT_WINDOW + std::to_string(windowId);
46     sptr<Rosen::Window> window = Rosen::Window::Create(windowName, option);
47     if (window == nullptr || window->GetSurfaceNode() == nullptr) {
48         DHLOGE("Create screen client window failed.");
49         return nullptr;
50     }
51     auto surface = window->GetSurfaceNode()->GetSurface();
52     if (surface == nullptr) {
53         window->Destroy();
54         DHLOGE("surface is nullptr");
55         return nullptr;
56     }
57     surface_ = surface;
58     Rosen::RSTransactionProxy::GetInstance()->FlushImplicitTransaction();
59     std::shared_ptr<Rosen::IInputEventConsumer> listener =
60         std::make_shared<ScreenClientInputEventListener>(ScreenClientInputEventListener());
61     window->SetInputEventConsumer(listener);
62     DHLOGD("Create window name is %{public}s.", windowName.c_str());
63     if (window->Resize(windowProperty->width, windowProperty->height) != OHOS::Rosen::WMError::WM_OK) {
64         window->Destroy();
65         DHLOGE("Window resize failed.");
66         return nullptr;
67     }
68     if (window->MoveTo(windowProperty->startX, windowProperty->startY) != OHOS::Rosen::WMError::WM_OK) {
69         window->Destroy();
70         DHLOGE("Window moveto failed.");
71         return nullptr;
72     }
73     {
74         std::lock_guard<std::mutex> dataLock(windowIdMapMutex_);
75         windowIdMap_.emplace(windowId, window);
76     }
77     DHLOGD("Get surface windowId = %{public}" PRId32 " success.", windowId);
78     return surface;
79 }
80 
ShowWindow(int32_t windowId)81 int32_t ScreenClientWindowAdapter::ShowWindow(int32_t windowId)
82 {
83     sptr<Rosen::Window> window = nullptr;
84     {
85         std::lock_guard<std::mutex> dataLock(windowIdMapMutex_);
86         auto iter = windowIdMap_.find(windowId);
87         if (iter == windowIdMap_.end()) {
88             DHLOGE("Invalid windowId windowId = %{public}" PRId32, windowId);
89             return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR;
90         }
91         window = iter->second;
92     }
93     if (window == nullptr) {
94         DHLOGE("Current window is null.");
95         return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR;
96     }
97     if (window->Show() != OHOS::Rosen::WMError::WM_OK) {
98         DHLOGE("Show window failed.");
99         return ERR_DH_SCREEN_SCREENCLIENT_SHOW_WINDOW_ERROR;
100     }
101     DHLOGD("Show window windowId = %{public}" PRId32 " success.", windowId);
102     return DH_SUCCESS;
103 }
104 
HideWindow(int32_t windowId)105 int32_t ScreenClientWindowAdapter::HideWindow(int32_t windowId)
106 {
107     sptr<Rosen::Window> window = nullptr;
108     {
109         std::lock_guard<std::mutex> dataLock(windowIdMapMutex_);
110         auto iter = windowIdMap_.find(windowId);
111         if (iter == windowIdMap_.end()) {
112             DHLOGE("Invalid windowId windowId = %{public}" PRId32, windowId);
113             return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR;
114         }
115         window = iter->second;
116     }
117     if (window == nullptr) {
118         DHLOGE("Current window is null.");
119         return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR;
120     }
121     if (window->Hide() != OHOS::Rosen::WMError::WM_OK) {
122         DHLOGE("Hide window failed.");
123         return ERR_DH_SCREEN_SCREENCLIENT_HIDE_WINDOW_ERROR;
124     }
125     DHLOGD("Hide window windowId = %{public}" PRId32 " success.", windowId);
126     return DH_SUCCESS;
127 }
128 
MoveWindow(int32_t windowId,int32_t startX,int32_t startY)129 int32_t ScreenClientWindowAdapter::MoveWindow(int32_t windowId, int32_t startX, int32_t startY)
130 {
131     sptr<Rosen::Window> window = nullptr;
132     {
133         std::lock_guard<std::mutex> dataLock(windowIdMapMutex_);
134         auto iter = windowIdMap_.find(windowId);
135         if (iter == windowIdMap_.end()) {
136             DHLOGE("Invalid windowId windowId = %{public}" PRId32, windowId);
137             return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR;
138         }
139         window = iter->second;
140     }
141     if (window == nullptr) {
142         DHLOGE("Current window is null windowId = %{public}" PRId32, windowId);
143         return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR;
144     }
145     if (window->MoveTo(startX, startY) != OHOS::Rosen::WMError::WM_OK) {
146         DHLOGE("Move window failed windowId = %{public}" PRId32, windowId);
147         return ERR_DH_SCREEN_SCREENCLIENT_MOVE_WINDOW_ERROR;
148     }
149     DHLOGD("MoveTo window windowId = %{public}" PRId32 " success.", windowId);
150     return DH_SUCCESS;
151 }
152 
RemoveWindow(int32_t windowId)153 int32_t ScreenClientWindowAdapter::RemoveWindow(int32_t windowId)
154 {
155     sptr<Rosen::Window> window = nullptr;
156     {
157         std::lock_guard<std::mutex> dataLock(windowIdMapMutex_);
158         auto iter = windowIdMap_.find(windowId);
159         if (iter == windowIdMap_.end()) {
160             DHLOGE("Invalid windowId windowId = %{public}" PRId32, windowId);
161             return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
162         }
163         window = iter->second;
164         windowIdMap_.erase(windowId);
165     }
166     if (window == nullptr) {
167         DHLOGE("Current window is null windowId = %{public}" PRId32, windowId);
168         return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
169     }
170     if (window->Hide() != OHOS::Rosen::WMError::WM_OK) {
171         DHLOGE("Remove window window failed windowId = %{public}" PRId32, windowId);
172         return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
173     }
174     if (window->Destroy() != OHOS::Rosen::WMError::WM_OK) {
175         DHLOGE("Remove window window failed windowId = %{public}" PRId32, windowId);
176         return ERR_DH_SCREEN_SCREENCLIENT_REMOVE_WINDOW_ERROR;
177     }
178     DHLOGD("Remove window success windowId = %{public}" PRId32, windowId);
179     return DH_SUCCESS;
180 }
181 
DestroyAllWindow()182 int32_t ScreenClientWindowAdapter::DestroyAllWindow()
183 {
184     DHLOGD("DestroyAllWindow");
185     std::lock_guard<std::mutex> dataLock(windowIdMapMutex_);
186     for (const auto &item : windowIdMap_) {
187         auto window = item.second;
188         if (window == nullptr) {
189             DHLOGE("window is nullptr. windowId = %{public}" PRId32, item.first);
190             continue;
191         }
192         if (OHOS::Rosen::WMError::WM_OK != window->Destroy()) {
193             DHLOGE("Destroy window failed. windowId = %{public}" PRId32, item.first);
194             continue;
195         }
196     }
197     windowIdMap_.clear();
198     return DH_SUCCESS;
199 }
200 
201 
OnInputEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent) const202 bool ScreenClientInputEventListener::OnInputEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent) const
203 {
204     if (pointerEvent == nullptr) {
205         DHLOGE("OnInputEvent failed, pointerEvent is nullptr.");
206         return false;
207     }
208     DHLOGI("onInputEvent, pointerEvent");
209     pointerEvent->MarkProcessed();
210     return true;
211 }
212 
OnInputEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent) const213 bool ScreenClientInputEventListener::OnInputEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent) const
214 {
215     if (keyEvent == nullptr) {
216         DHLOGE("OnInputEvent failed, keyEvent is nullptr.");
217         return false;
218     }
219     DHLOGI("onInputEvent, keyEvent");
220     keyEvent->MarkProcessed();
221     return true;
222 }
223 
OnInputEvent(const std::shared_ptr<MMI::AxisEvent> & axisEvent) const224 bool ScreenClientInputEventListener::OnInputEvent(const std::shared_ptr<MMI::AxisEvent>& axisEvent) const
225 {
226     if (axisEvent == nullptr) {
227         DHLOGE("OnInputEvent failed, axisEvent is nullptr.");
228         return false;
229     }
230     DHLOGI("onInputEvent, axisEvent");
231     axisEvent->MarkProcessed();
232     return true;
233 }
234 } // namespace DistributedHardware
235 } // namespace OHOS