1 /*
2 * Copyright (c) 2024 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 "delegate_interface.h"
17
18 #include "display_event_monitor.h"
19 #include "error_multimodal.h"
20 #include "input_event_handler.h"
21 #include "i_pointer_drawing_manager.h"
22 #include "mmi_log.h"
23 #include "touch_drawing_manager.h"
24
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "DelegateInterface"
27
28 namespace OHOS {
29 namespace MMI {
Init()30 void DelegateInterface::Init()
31 {
32 TOUCH_DRAWING_MGR->SetDelegateProxy(shared_from_this());
33 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
34 DISPLAY_MONITOR->SetDelegateProxy(shared_from_this());
35 #endif // #ifdef OHOS_BUILD_ENABLE_KEYBOARD
36 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
37 IPointerDrawingManager::GetInstance()->SetDelegateProxy(shared_from_this());
38 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
39 }
40
OnPostSyncTask(DTaskCallback cb) const41 int32_t DelegateInterface::OnPostSyncTask(DTaskCallback cb) const
42 {
43 CHKPR(delegateTasks_, ERROR_NULL_POINTER);
44 int32_t ret = delegateTasks_(cb);
45 if (ret != RET_OK) {
46 MMI_HILOGE("Failed to execute the task, ret: %{public}d", ret);
47 }
48 return ret;
49 }
50
OnInputEvent(InputHandlerType type,std::shared_ptr<PointerEvent> event) const51 void DelegateInterface::OnInputEvent(
52 InputHandlerType type, std::shared_ptr<PointerEvent> event) const
53 {
54 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
55 OnInputEventHandler(type, event);
56 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
57 }
58
59 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
OnInputEventHandler(InputHandlerType type,std::shared_ptr<PointerEvent> event) const60 void DelegateInterface::OnInputEventHandler(
61 InputHandlerType type, std::shared_ptr<PointerEvent> event) const
62 {
63 CHKPV(event);
64 for (const auto &handler : handlers) {
65 auto summary = handler.second;
66 if (handler.first != type) {
67 continue;
68 }
69 if (type == InputHandlerType::MONITOR &&
70 (summary.eventType & HANDLE_EVENT_TYPE_POINTER) != HANDLE_EVENT_TYPE_POINTER) {
71 continue;
72 }
73 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
74 uint32_t deviceTags = 0;
75 if (type == InputHandlerType::INTERCEPTOR &&
76 ((deviceTags & summary.deviceTags) == summary.deviceTags) &&
77 !EventInterceptorHandler::CheckInputDeviceSource(event, summary.deviceTags)) {
78 continue;
79 }
80 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
81 CHKPV(summary.cb);
82 if (summary.mode == HandlerMode::SYNC) {
83 summary.cb(event);
84 } else {
85 if (OnPostSyncTask(std::bind(summary.cb, event)) != RET_OK) {
86 MMI_HILOGE("Failed to execute the task(%{public}s)", summary.handlerName.c_str());
87 }
88 }
89 }
90 }
91
AddHandler(InputHandlerType type,HandlerSummary summary)92 int32_t DelegateInterface::AddHandler(InputHandlerType type, HandlerSummary summary)
93 {
94 CHKPR(summary.cb, ERROR_NULL_POINTER);
95 int32_t ret = RET_OK;
96 for (const auto &handler : handlers) {
97 if (handler.second.handlerName == summary.handlerName) {
98 MMI_HILOGW("The current handler(%{public}s) already exists", summary.handlerName.c_str());
99 return ret;
100 }
101 }
102 const HandleEventType currentType = GetEventType(type);
103 uint32_t currentTags = GetDeviceTags(type);
104 handlers.emplace(type, summary);
105 const HandleEventType newType = GetEventType(type);
106 if (currentType != newType || ((currentTags & summary.deviceTags) != summary.deviceTags)) {
107 uint32_t allDeviceTags = GetDeviceTags(type);
108 if (type == InputHandlerType::INTERCEPTOR) {
109 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
110 auto interceptorHandler = InputHandler->GetInterceptorHandler();
111 CHKPR(interceptorHandler, ERROR_NULL_POINTER);
112 ret = interceptorHandler->AddInputHandler(type,
113 newType, summary.priority, allDeviceTags, nullptr);
114 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
115 } else if (type == InputHandlerType::MONITOR) {
116 #ifdef OHOS_BUILD_ENABLE_MONITOR
117 auto monitorHandler = InputHandler->GetMonitorHandler();
118 CHKPR(monitorHandler, ERROR_NULL_POINTER);
119 ret = monitorHandler->AddInputHandler(type,
120 newType, shared_from_this());
121 #endif // OHOS_BUILD_ENABLE_MONITOR
122 }
123 }
124 if (ret != RET_OK) {
125 RemoveLocal(type, summary.handlerName, currentTags);
126 } else {
127 MMI_HILOGI("Service Add Monitor Success, size:%{public}zu", handlers.size());
128 }
129 return ret;
130 }
131
GetEventType(InputHandlerType type) const132 HandleEventType DelegateInterface::GetEventType(InputHandlerType type) const
133 {
134 uint32_t eventType {HANDLE_EVENT_TYPE_NONE};
135 if (handlers.empty()) {
136 MMI_HILOGW("handlers is empty");
137 return HANDLE_EVENT_TYPE_NONE;
138 }
139 for (const auto &handler : handlers) {
140 if (handler.first == type) {
141 eventType |= handler.second.eventType;
142 }
143 }
144 return eventType;
145 }
146
GetDeviceTags(InputHandlerType type) const147 uint32_t DelegateInterface::GetDeviceTags(InputHandlerType type) const
148 {
149 uint32_t deviceTags = 0;
150 if (type == InputHandlerType::MONITOR) {
151 return deviceTags;
152 }
153 if (handlers.empty()) {
154 MMI_HILOGW("handlers is empty");
155 return deviceTags;
156 }
157 for (const auto &handler : handlers) {
158 if (handler.first == type) {
159 deviceTags |= handler.second.deviceTags;
160 }
161 }
162 return deviceTags;
163 }
164
RemoveLocal(InputHandlerType type,std::string name,uint32_t & deviceTags)165 void DelegateInterface::RemoveLocal(InputHandlerType type, std::string name, uint32_t &deviceTags)
166 {
167 for (auto it = handlers.cbegin(); it != handlers.cend(); ++it) {
168 if (type != it->first) {
169 continue;
170 }
171 if (it->second.handlerName != name) {
172 continue;
173 }
174 handlers.erase(it);
175 if (type == InputHandlerType::INTERCEPTOR) {
176 deviceTags = it->second.deviceTags;
177 }
178 break;
179 }
180 }
181
GetPriority(InputHandlerType type) const182 int32_t DelegateInterface::GetPriority(InputHandlerType type) const
183 {
184 for (auto it = handlers.cbegin(); it != handlers.cend(); ++it) {
185 if (type == it->first) {
186 return it->second.priority;
187 }
188 }
189 return DEFUALT_INTERCEPTOR_PRIORITY;
190 }
191
RemoveHandler(InputHandlerType type,std::string name)192 void DelegateInterface::RemoveHandler(InputHandlerType type, std::string name)
193 {
194 const HandleEventType currentType = GetEventType(type);
195 uint32_t currentTags = GetDeviceTags(type);
196 uint32_t deviceTags = 0;
197 RemoveLocal(type, name, deviceTags);
198 const HandleEventType newType = GetEventType(type);
199 const int32_t newLevel = GetPriority(type);
200 const uint64_t newTags = GetDeviceTags(type);
201 if (currentType != newType || ((currentTags & deviceTags) != 0)) {
202 if (type == InputHandlerType::INTERCEPTOR) {
203 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
204 auto interceptorHandler = InputHandler->GetInterceptorHandler();
205 CHKPV(interceptorHandler);
206 interceptorHandler->RemoveInputHandler(type,
207 newType, newLevel, newTags, nullptr);
208 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
209 }
210 if (type == InputHandlerType::MONITOR) {
211 #ifdef OHOS_BUILD_ENABLE_MONITOR
212 auto monitorHandler = InputHandler->GetMonitorHandler();
213 CHKPV(monitorHandler);
214 monitorHandler->RemoveInputHandler(type,
215 newType, shared_from_this());
216 #endif // OHOS_BUILD_ENABLE_MONITOR
217 }
218 }
219 MMI_HILOGI("Remove Handler:%{public}d:%{public}s-%{public}d:%{public}d, size:%{public}zu", type,
220 name.c_str(), currentType, currentTags, handlers.size());
221 }
222 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
223 } // namespace MMI
224 } // namespace OHOS