1 /*
2 * Copyright (c) 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 "session/container/include/window_event_channel.h"
17
18 #include <functional>
19 #include <utility>
20
21 #include <axis_event.h>
22 #include <key_event.h>
23 #include <pointer_event.h>
24
25 #include "anr_handler.h"
26 #include "window_manager_hilog.h"
27
28 namespace OHOS::Rosen {
29 namespace {
30 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "WindowEventChannel" };
31 }
32
TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent)33 WSError WindowEventChannel::TransferKeyEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent)
34 {
35 WLOGFD("WindowEventChannel receive key event");
36 PrintKeyEvent(keyEvent);
37 bool isConsumed = false;
38 return TransferKeyEventForConsumed(keyEvent, isConsumed);
39 }
40
TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent> & pointerEvent)41 WSError WindowEventChannel::TransferPointerEvent(const std::shared_ptr<MMI::PointerEvent>& pointerEvent)
42 {
43 WLOGFD("WindowEventChannel receive pointer event");
44 PrintPointerEvent(pointerEvent);
45 if (!sessionStage_) {
46 WLOGFE("session stage is null!");
47 return WSError::WS_ERROR_NULLPTR;
48 }
49 if (pointerEvent == nullptr) {
50 WLOGFE("PointerEvent is null!");
51 return WSError::WS_ERROR_NULLPTR;
52 }
53 auto pointerAction = pointerEvent->GetPointerAction();
54 if (pointerAction == MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW ||
55 pointerAction == MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW ||
56 pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW ||
57 pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW) {
58 WLOGFI("InputTracking id:%{public}d, Dispatch by skipping receipt, action:%{public}s,"
59 " persistentId:%{public}d", pointerEvent->GetId(),
60 pointerEvent->DumpPointerAction(), sessionStage_->GetPersistentId());
61 } else {
62 DelayedSingleton<ANRHandler>::GetInstance()->SetSessionStage(pointerEvent->GetId(), sessionStage_);
63 WLOGFD("Dispatch normally, action:%{public}s, eventId:%{public}d, persistentId:%{public}d",
64 pointerEvent->DumpPointerAction(), pointerEvent->GetId(), sessionStage_->GetPersistentId());
65 }
66 sessionStage_->NotifyPointerEvent(pointerEvent);
67 return WSError::WS_OK;
68 }
69
TransferBackpressedEventForConsumed(bool & isConsumed)70 WSError WindowEventChannel::TransferBackpressedEventForConsumed(bool& isConsumed)
71 {
72 WLOGFD("WindowEventChannel receive backpressed event");
73 if (!sessionStage_) {
74 WLOGFE("session stage is null!");
75 return WSError::WS_ERROR_NULLPTR;
76 }
77 sessionStage_->NotifyBackpressedEvent(isConsumed);
78 return WSError::WS_OK;
79 }
80
TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool & isConsumed)81 WSError WindowEventChannel::TransferKeyEventForConsumed(
82 const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed)
83 {
84 WLOGFD("WindowEventChannel receive key event");
85 if (!sessionStage_) {
86 WLOGFE("session stage is null!");
87 return WSError::WS_ERROR_NULLPTR;
88 }
89 if (keyEvent == nullptr) {
90 WLOGFE("keyEvent is nullptr");
91 return WSError::WS_ERROR_NULLPTR;
92 }
93 DelayedSingleton<ANRHandler>::GetInstance()->SetSessionStage(keyEvent->GetId(), sessionStage_);
94 sessionStage_->NotifyKeyEvent(keyEvent, isConsumed);
95 keyEvent->MarkProcessed();
96 return WSError::WS_OK;
97 }
98
TransferFocusActiveEvent(bool isFocusActive)99 WSError WindowEventChannel::TransferFocusActiveEvent(bool isFocusActive)
100 {
101 WLOGFD("WindowEventChannel receive focus active event");
102 if (!sessionStage_) {
103 WLOGFE("session stage is null!");
104 return WSError::WS_ERROR_NULLPTR;
105 }
106 sessionStage_->NotifyFocusActiveEvent(isFocusActive);
107 return WSError::WS_OK;
108 }
109
OnDispatchEventProcessed(int32_t eventId,int64_t actionTime)110 void WindowEventChannel::OnDispatchEventProcessed(int32_t eventId, int64_t actionTime)
111 {
112 DelayedSingleton<ANRHandler>::GetInstance()->HandleEventConsumed(eventId, actionTime);
113 }
114
PrintKeyEvent(const std::shared_ptr<MMI::KeyEvent> & event)115 void WindowEventChannel::PrintKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event)
116 {
117 if (event == nullptr) {
118 WLOGFE("event is nullptr");
119 return;
120 }
121 std::vector<MMI::KeyEvent::KeyItem> eventItems = event->GetKeyItems();
122 WLOGFD("KeyCode:%{public}d,KeyAction:%{public}s,keyItemsCount:%{public}zu", event->GetKeyCode(),
123 MMI::KeyEvent::ActionToString(event->GetKeyAction()), eventItems.size());
124 for (const auto &item : eventItems) {
125 WLOGFD("KeyCode:%{public}d,IsPressed:%{public}d,GetUnicode:%{public}d",
126 item.GetKeyCode(), item.IsPressed(), item.GetUnicode());
127 }
128 }
129
PrintPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)130 void WindowEventChannel::PrintPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event)
131 {
132 if (event == nullptr) {
133 WLOGFE("event is nullptr");
134 return;
135 }
136 std::vector<int32_t> pointerIds = event->GetPointerIds();
137 std::string str;
138 std::vector<uint8_t> buffer = event->GetBuffer();
139 for (const auto &buff : buffer) {
140 str += std::to_string(buff);
141 }
142 auto pointerAction = event->GetPointerAction();
143 if (pointerAction == MMI::PointerEvent::POINTER_ACTION_MOVE ||
144 pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_MOVE) {
145 WLOGFD("PointerAction:%{public}s,SourceType:%{public}s,ButtonId:%{public}d,"
146 "VerticalAxisValue:%{public}.2f,HorizontalAxisValue:%{public}.2f,"
147 "PointerId:%{public}d,PointerCount:%{public}zu,EventNumber:%{public}d,"
148 "BufferCount:%{public}zu,Buffer:%{public}s",
149 event->DumpPointerAction(), event->DumpSourceType(), event->GetButtonId(),
150 event->GetAxisValue(MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL),
151 event->GetAxisValue(MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL),
152 event->GetPointerId(), pointerIds.size(), event->GetId(), buffer.size(), str.c_str());
153
154 for (const auto &pointerId : pointerIds) {
155 MMI::PointerEvent::PointerItem item;
156 if (!event->GetPointerItem(pointerId, item)) {
157 WLOGFE("Invalid pointer: %{public}d.", pointerId);
158 return;
159 }
160 WLOGFD("pointerId:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,"
161 "DisplayX:%{public}d,DisplayY:%{public}d,WindowX:%{public}d,WindowY:%{public}d,",
162 pointerId, item.GetDownTime(), item.IsPressed(), item.GetDisplayX(), item.GetDisplayY(),
163 item.GetWindowX(), item.GetWindowY());
164 }
165 } else {
166 PrintInfoPointerEvent(event);
167 }
168 }
169
PrintInfoPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)170 void WindowEventChannel::PrintInfoPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event)
171 {
172 if (event == nullptr) {
173 WLOGFE("event is nullptr");
174 return;
175 }
176 std::vector<int32_t> pointerIds = event->GetPointerIds();
177 std::string str;
178 std::vector<uint8_t> buffer = event->GetBuffer();
179 for (const auto &buff : buffer) {
180 str += std::to_string(buff);
181 }
182 WLOGFI("PointerAction:%{public}s,SourceType:%{public}s,ButtonId:%{public}d,"
183 "VerticalAxisValue:%{public}.2f,HorizontalAxisValue:%{public}.2f,"
184 "PointerId:%{public}d,PointerCount:%{public}zu,EventNumber:%{public}d,"
185 "BufferCount:%{public}zu,Buffer:%{public}s",
186 event->DumpPointerAction(), event->DumpSourceType(), event->GetButtonId(),
187 event->GetAxisValue(MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL),
188 event->GetAxisValue(MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL),
189 event->GetPointerId(), pointerIds.size(), event->GetId(), buffer.size(), str.c_str());
190
191 for (const auto &pointerId : pointerIds) {
192 MMI::PointerEvent::PointerItem item;
193 if (!event->GetPointerItem(pointerId, item)) {
194 WLOGFE("Invalid pointer: %{public}d.", pointerId);
195 return;
196 }
197 WLOGFI("pointerId:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,"
198 "DisplayX:%{public}d,DisplayY:%{public}d,WindowX:%{public}d,WindowY:%{public}d,",
199 pointerId, item.GetDownTime(), item.IsPressed(), item.GetDisplayX(), item.GetDisplayY(),
200 item.GetWindowX(), item.GetWindowY());
201 }
202 }
203
TransferFocusState(bool focusState)204 WSError WindowEventChannel::TransferFocusState(bool focusState)
205 {
206 WLOGFD("WindowEventChannel receive focus state event: %{public}d", static_cast<int>(focusState));
207 if (!sessionStage_) {
208 WLOGFE("session stage is null!");
209 return WSError::WS_ERROR_NULLPTR;
210 }
211 sessionStage_->NotifyFocusStateEvent(focusState);
212 return WSError::WS_OK;
213 }
214
TransferSearchElementInfo(int64_t elementId,int32_t mode,int64_t baseParent,std::list<Accessibility::AccessibilityElementInfo> & infos)215 WSError WindowEventChannel::TransferSearchElementInfo(int64_t elementId, int32_t mode, int64_t baseParent,
216 std::list<Accessibility::AccessibilityElementInfo>& infos)
217 {
218 if (!sessionStage_) {
219 WLOGFE("session stage is null!");
220 return WSError::WS_ERROR_NULLPTR;
221 }
222 return sessionStage_->NotifySearchElementInfoByAccessibilityId(elementId, mode, baseParent, infos);
223 }
224
TransferSearchElementInfosByText(int64_t elementId,const std::string & text,int64_t baseParent,std::list<Accessibility::AccessibilityElementInfo> & infos)225 WSError WindowEventChannel::TransferSearchElementInfosByText(int64_t elementId, const std::string& text,
226 int64_t baseParent, std::list<Accessibility::AccessibilityElementInfo>& infos)
227 {
228 if (!sessionStage_) {
229 WLOGFE("session stage is null!");
230 return WSError::WS_ERROR_NULLPTR;
231 }
232 return sessionStage_->NotifySearchElementInfosByText(elementId, text, baseParent, infos);
233 }
234
TransferFindFocusedElementInfo(int64_t elementId,int32_t focusType,int64_t baseParent,Accessibility::AccessibilityElementInfo & info)235 WSError WindowEventChannel::TransferFindFocusedElementInfo(int64_t elementId, int32_t focusType, int64_t baseParent,
236 Accessibility::AccessibilityElementInfo& info)
237 {
238 if (!sessionStage_) {
239 WLOGFE("session stage is null!");
240 return WSError::WS_ERROR_NULLPTR;
241 }
242 return sessionStage_->NotifyFindFocusedElementInfo(elementId, focusType, baseParent, info);
243 }
244
TransferFocusMoveSearch(int64_t elementId,int32_t direction,int64_t baseParent,Accessibility::AccessibilityElementInfo & info)245 WSError WindowEventChannel::TransferFocusMoveSearch(int64_t elementId, int32_t direction, int64_t baseParent,
246 Accessibility::AccessibilityElementInfo& info)
247 {
248 if (!sessionStage_) {
249 WLOGFE("session stage is null!");
250 return WSError::WS_ERROR_NULLPTR;
251 }
252 return sessionStage_->NotifyFocusMoveSearch(elementId, direction, baseParent, info);
253 }
254
TransferExecuteAction(int64_t elementId,const std::map<std::string,std::string> & actionArguments,int32_t action,int64_t baseParent)255 WSError WindowEventChannel::TransferExecuteAction(int64_t elementId,
256 const std::map<std::string, std::string>& actionArguments, int32_t action,
257 int64_t baseParent)
258 {
259 if (!sessionStage_) {
260 WLOGFE("session stage is null!");
261 return WSError::WS_ERROR_NULLPTR;
262 }
263 return sessionStage_->NotifyExecuteAction(elementId, actionArguments, action, baseParent);
264 }
265 } // namespace OHOS::Rosen
266