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