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 "input_event_handler.h"
20 #include "cursor_drawing_component.h"
21 #include "property_reader.h"
22 #include "pointer_device_manager.h"
23 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
24 #include "touch_drawing_manager.h"
25 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
26
27 #undef MMI_LOG_DOMAIN
28 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
29 #undef MMI_LOG_TAG
30 #define MMI_LOG_TAG "DelegateInterface"
31
32 namespace OHOS {
33 namespace MMI {
Init()34 void DelegateInterface::Init()
35 {
36 #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
37 TOUCH_DRAWING_MGR->SetDelegateProxy(shared_from_this());
38 #endif // #ifdef OHOS_BUILD_ENABLE_TOUCH_DRAWING
39 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
40 DISPLAY_MONITOR->SetDelegateProxy(shared_from_this());
41 #endif // #ifdef OHOS_BUILD_ENABLE_KEYBOARD
42 #ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
43 POINTER_DEV_MGR.SetDelegateProxy(shared_from_this());
44 #endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
45 PropReader->SetDelegateProxy(shared_from_this());
46 }
47
OnPostSyncTask(DTaskCallback cb) const48 int32_t DelegateInterface::OnPostSyncTask(DTaskCallback cb) const
49 {
50 CHKPR(delegateTasks_, ERROR_NULL_POINTER);
51 int32_t ret = delegateTasks_(cb);
52 if (ret != RET_OK) {
53 MMI_HILOGE("Failed to execute the task, ret:%{public}d", ret);
54 }
55 return ret;
56 }
57
OnPostAsyncTask(DTaskCallback cb) const58 int32_t DelegateInterface::OnPostAsyncTask(DTaskCallback cb) const
59 {
60 CHKPR(asyncDelegateTasks_, ERROR_NULL_POINTER);
61 int32_t ret = asyncDelegateTasks_(cb);
62 if (ret != RET_OK) {
63 MMI_HILOGE("Failed to execute the async task, ret:%{public}d", ret);
64 }
65 return ret;
66 }
67
OnInputEvent(InputHandlerType type,std::shared_ptr<PointerEvent> event) const68 void DelegateInterface::OnInputEvent(
69 InputHandlerType type, std::shared_ptr<PointerEvent> event) const
70 {
71 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
72 OnInputEventHandler(type, event);
73 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
74 }
75
76 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
OnInputEventHandler(InputHandlerType type,std::shared_ptr<PointerEvent> event) const77 void DelegateInterface::OnInputEventHandler(
78 InputHandlerType type, std::shared_ptr<PointerEvent> event) const
79 {
80 CHKPV(event);
81 for (const auto &handler : handlers_) {
82 auto summary = handler.second;
83 if (handler.first != type) {
84 continue;
85 }
86 #ifdef OHOS_BUILD_ENABLE_MONITOR
87 if ((type == InputHandlerType::MONITOR) && !MonitorExpectEvent(summary, event)) {
88 continue;
89 }
90 #endif // OHOS_BUILD_ENABLE_MONITOR
91 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
92 uint32_t deviceTags = 0;
93 if (type == InputHandlerType::INTERCEPTOR &&
94 ((deviceTags & summary.deviceTags) == summary.deviceTags) &&
95 !EventInterceptorHandler::CheckInputDeviceSource(event, summary.deviceTags)) {
96 continue;
97 }
98 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
99 CHKPV(summary.cb);
100 if (summary.mode == HandlerMode::SYNC) {
101 summary.cb(event);
102 } else {
103 if (OnPostSyncTask(std::bind(summary.cb, event)) != RET_OK) {
104 MMI_HILOGE("Failed to execute the task(%{public}s)", summary.handlerName.c_str());
105 }
106 }
107 }
108 }
109
AddHandler(InputHandlerType type,const HandlerSummary & summary)110 int32_t DelegateInterface::AddHandler(InputHandlerType type, const HandlerSummary &summary)
111 {
112 CHKPR(summary.cb, ERROR_NULL_POINTER);
113 int32_t ret = RET_OK;
114 if (HasHandler(summary.handlerName)) {
115 MMI_HILOGW("The current handler(%{public}s) already exists", summary.handlerName.c_str());
116 return ret;
117 }
118 const HandleEventType currentType = GetEventType(type);
119 uint32_t currentTags = GetDeviceTags(type);
120 handlers_.emplace(type, summary);
121 const HandleEventType newType = GetEventType(type);
122 if (currentType != newType || ((currentTags & summary.deviceTags) != summary.deviceTags)) {
123 uint32_t allDeviceTags = GetDeviceTags(type);
124 if (type == InputHandlerType::INTERCEPTOR) {
125 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
126 auto interceptorHandler = InputHandler->GetInterceptorHandler();
127 CHKPR(interceptorHandler, ERROR_NULL_POINTER);
128 ret = interceptorHandler->AddInputHandler(type,
129 newType, summary.priority, allDeviceTags, nullptr);
130 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
131 } else if (type == InputHandlerType::MONITOR) {
132 #ifdef OHOS_BUILD_ENABLE_MONITOR
133 auto monitorHandler = InputHandler->GetMonitorHandler();
134 CHKPR(monitorHandler, ERROR_NULL_POINTER);
135 ret = monitorHandler->AddInputHandler(type, newType, shared_from_this(),
136 summary.gestureType, summary.fingers);
137 #endif // OHOS_BUILD_ENABLE_MONITOR
138 }
139 }
140 if (ret != RET_OK) {
141 RemoveLocal(type, summary.handlerName, currentTags);
142 } else {
143 MMI_HILOGI("Service Add Monitor Success, size:%{public}zu", handlers_.size());
144 }
145 return ret;
146 }
147
GetEventType(InputHandlerType type) const148 HandleEventType DelegateInterface::GetEventType(InputHandlerType type) const
149 {
150 uint32_t eventType { HANDLE_EVENT_TYPE_NONE };
151 if (handlers_.empty()) {
152 MMI_HILOGW("handlers is empty");
153 return HANDLE_EVENT_TYPE_NONE;
154 }
155 for (const auto &handler : handlers_) {
156 if (handler.first == type) {
157 eventType |= handler.second.eventType;
158 }
159 }
160 return eventType;
161 }
162
GetDeviceTags(InputHandlerType type) const163 uint32_t DelegateInterface::GetDeviceTags(InputHandlerType type) const
164 {
165 uint32_t deviceTags = 0;
166 if (type == InputHandlerType::MONITOR) {
167 return deviceTags;
168 }
169 if (handlers_.empty()) {
170 MMI_HILOGW("handlers is empty");
171 return deviceTags;
172 }
173 for (const auto &handler : handlers_) {
174 if (handler.first == type) {
175 deviceTags |= handler.second.deviceTags;
176 }
177 }
178 return deviceTags;
179 }
180
RemoveLocal(InputHandlerType type,const std::string & name,uint32_t & deviceTags)181 std::optional<DelegateInterface::HandlerSummary> DelegateInterface::RemoveLocal(
182 InputHandlerType type, const std::string &name, uint32_t &deviceTags)
183 {
184 for (auto it = handlers_.cbegin(); it != handlers_.cend(); ++it) {
185 if (type != it->first) {
186 continue;
187 }
188 if (it->second.handlerName != name) {
189 continue;
190 }
191 auto summary = it->second;
192 handlers_.erase(it);
193 if (type == InputHandlerType::INTERCEPTOR) {
194 deviceTags = summary.deviceTags;
195 }
196 return summary;
197 }
198 return std::nullopt;
199 }
200
GetPriority(InputHandlerType type) const201 int32_t DelegateInterface::GetPriority(InputHandlerType type) const
202 {
203 for (auto it = handlers_.cbegin(); it != handlers_.cend(); ++it) {
204 if (type == it->first) {
205 return it->second.priority;
206 }
207 }
208 return DEFUALT_INTERCEPTOR_PRIORITY;
209 }
210
RemoveHandler(InputHandlerType type,const std::string & name)211 void DelegateInterface::RemoveHandler(InputHandlerType type, const std::string &name)
212 {
213 const HandleEventType currentType = GetEventType(type);
214 uint32_t currentTags = GetDeviceTags(type);
215 uint32_t deviceTags = 0;
216 auto handlerOpt = RemoveLocal(type, name, deviceTags);
217 if (!handlerOpt) {
218 return;
219 }
220 const HandleEventType newType = GetEventType(type);
221 const int32_t newLevel = GetPriority(type);
222 const uint64_t newTags = GetDeviceTags(type);
223 if (currentType != newType || ((currentTags & deviceTags) != 0)) {
224 if (type == InputHandlerType::INTERCEPTOR) {
225 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
226 auto interceptorHandler = InputHandler->GetInterceptorHandler();
227 CHKPV(interceptorHandler);
228 interceptorHandler->RemoveInputHandler(type,
229 newType, newLevel, newTags, nullptr);
230 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
231 }
232 if (type == InputHandlerType::MONITOR) {
233 #ifdef OHOS_BUILD_ENABLE_MONITOR
234 auto monitorHandler = InputHandler->GetMonitorHandler();
235 CHKPV(monitorHandler);
236 monitorHandler->RemoveInputHandler(type, newType, shared_from_this(),
237 handlerOpt->gestureType, handlerOpt->fingers);
238 #endif // OHOS_BUILD_ENABLE_MONITOR
239 }
240 }
241 MMI_HILOGI("Remove Handler:%{public}d:%{public}s-%{public}d:%{public}d, size:%{public}zu", type,
242 name.c_str(), currentType, currentTags, handlers_.size());
243 }
244
HasHandler(const std::string & name) const245 bool DelegateInterface::HasHandler(const std::string &name) const
246 {
247 return std::find_if(handlers_.cbegin(), handlers_.cend(),
248 [name](const auto &item) {
249 return item.second.handlerName == name;
250 }) != handlers_.cend();
251 }
252 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
253 #ifdef OHOS_BUILD_ENABLE_MONITOR
MonitorExpectEvent(const HandlerSummary & monitor,std::shared_ptr<PointerEvent> event) const254 bool DelegateInterface::MonitorExpectEvent(const HandlerSummary &monitor, std::shared_ptr<PointerEvent> event) const
255 {
256 if (GestureMonitorHandler::IsTouchGestureEvent(event->GetPointerAction())) {
257 return ((monitor.eventType & HANDLE_EVENT_TYPE_TOUCH_GESTURE) == HANDLE_EVENT_TYPE_TOUCH_GESTURE);
258 } else {
259 return ((monitor.eventType & HANDLE_EVENT_TYPE_POINTER) == HANDLE_EVENT_TYPE_POINTER);
260 }
261 }
262 #endif // OHOS_BUILD_ENABLE_MONITOR
263 } // namespace MMI
264 } // namespace OHOS