• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         WLOGFD("Dispatch by skipping receipt, action:%{public}s, eventId:%{public}d, persistentId:%{public}d",
59             pointerEvent->DumpPointerAction(), pointerEvent->GetId(), sessionStage_->GetPersistentId());
60     } else {
61         DelayedSingleton<ANRHandler>::GetInstance()->SetSessionStage(pointerEvent->GetId(), sessionStage_);
62         pointerEvent->SetProcessedCallback(dispatchCallback_);
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 
TransferKeyEventForConsumed(const std::shared_ptr<MMI::KeyEvent> & keyEvent,bool & isConsumed)70 WSError WindowEventChannel::TransferKeyEventForConsumed(
71     const std::shared_ptr<MMI::KeyEvent>& keyEvent, bool& isConsumed)
72 {
73     WLOGFD("WindowEventChannel receive key event");
74     if (!sessionStage_) {
75         WLOGFE("session stage is null!");
76         return WSError::WS_ERROR_NULLPTR;
77     }
78     DelayedSingleton<ANRHandler>::GetInstance()->SetSessionStage(keyEvent->GetId(), sessionStage_);
79     if (keyEvent != nullptr) {
80         WLOGFD("SetProcessedCallback enter");
81         keyEvent->SetProcessedCallback(dispatchCallback_);
82         WLOGFD("SetProcessedCallback leave");
83     }
84     sessionStage_->NotifyKeyEvent(keyEvent, isConsumed);
85     keyEvent->MarkProcessed();
86     return WSError::WS_OK;
87 }
88 
TransferFocusActiveEvent(bool isFocusActive)89 WSError WindowEventChannel::TransferFocusActiveEvent(bool isFocusActive)
90 {
91     WLOGFD("WindowEventChannel receive focus active event");
92     if (!sessionStage_) {
93         WLOGFE("session stage is null!");
94         return WSError::WS_ERROR_NULLPTR;
95     }
96     sessionStage_->NotifyFocusActiveEvent(isFocusActive);
97     return WSError::WS_OK;
98 }
99 
OnDispatchEventProcessed(int32_t eventId,int64_t actionTime)100 void WindowEventChannel::OnDispatchEventProcessed(int32_t eventId, int64_t actionTime)
101 {
102     DelayedSingleton<ANRHandler>::GetInstance()->HandleEventConsumed(eventId, actionTime);
103 }
104 
PrintKeyEvent(const std::shared_ptr<MMI::KeyEvent> & event)105 void WindowEventChannel::PrintKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event)
106 {
107     if (event == nullptr) {
108         WLOGFE("event is nullptr");
109         return;
110     }
111     std::vector<MMI::KeyEvent::KeyItem> eventItems = event->GetKeyItems();
112     WLOGFD("KeyCode:%{public}d,KeyAction:%{public}s,keyItemsCount:%{public}zu", event->GetKeyCode(),
113         MMI::KeyEvent::ActionToString(event->GetKeyAction()), eventItems.size());
114     for (const auto &item : eventItems) {
115         WLOGFD("KeyCode:%{public}d,IsPressed:%{public}d,GetUnicode:%{public}d",
116             item.GetKeyCode(), item.IsPressed(), item.GetUnicode());
117     }
118 }
119 
PrintPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)120 void WindowEventChannel::PrintPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event)
121 {
122     if (event == nullptr) {
123         WLOGFE("event is nullptr");
124         return;
125     }
126     std::vector<int32_t> pointerIds = event->GetPointerIds();
127     std::string str;
128     std::vector<uint8_t> buffer = event->GetBuffer();
129     for (const auto &buff : buffer) {
130         str += std::to_string(buff);
131     }
132     WLOGFD("PointerAction:%{public}s,SourceType:%{public}s,ButtonId:%{public}d,"
133         "VerticalAxisValue:%{public}.2f,HorizontalAxisValue:%{public}.2f,"
134         "PointerId:%{public}d,PointerCount:%{public}zu,EventNumber:%{public}d,"
135         "BufferCount:%{public}zu,Buffer:%{public}s",
136         event->DumpPointerAction(), event->DumpSourceType(), event->GetButtonId(),
137         event->GetAxisValue(MMI::PointerEvent::AXIS_TYPE_SCROLL_VERTICAL),
138         event->GetAxisValue(MMI::PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL),
139         event->GetPointerId(), pointerIds.size(), event->GetId(), buffer.size(), str.c_str());
140 
141     for (const auto &pointerId : pointerIds) {
142         MMI::PointerEvent::PointerItem item;
143         if (!event->GetPointerItem(pointerId, item)) {
144             WLOGFE("Invalid pointer: %{public}d.", pointerId);
145             return;
146         }
147         WLOGFD("pointerId:%{public}d,DownTime:%{public}" PRId64 ",IsPressed:%{public}d,"
148             "DisplayX:%{public}d,DisplayY:%{public}d,WindowX:%{public}d,WindowY:%{public}d,",
149             pointerId, item.GetDownTime(), item.IsPressed(), item.GetDisplayX(), item.GetDisplayY(),
150             item.GetWindowX(), item.GetWindowY());
151     }
152 }
153 
TransferFocusState(bool focusState)154 WSError WindowEventChannel::TransferFocusState(bool focusState)
155 {
156     WLOGFD("WindowEventChannel receive focus state event: %{public}d", static_cast<int>(focusState));
157     if (!sessionStage_) {
158         WLOGFE("session stage is null!");
159         return WSError::WS_ERROR_NULLPTR;
160     }
161     sessionStage_->NotifyFocusStateEvent(focusState);
162     return WSError::WS_OK;
163 }
164 } // namespace OHOS::Rosen
165