1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci#include "delegate_interface.h"
17c29fa5a6Sopenharmony_ci
18c29fa5a6Sopenharmony_ci#include <algorithm>
19c29fa5a6Sopenharmony_ci
20c29fa5a6Sopenharmony_ci#include "display_event_monitor.h"
21c29fa5a6Sopenharmony_ci#include "error_multimodal.h"
22c29fa5a6Sopenharmony_ci#include "input_event_handler.h"
23c29fa5a6Sopenharmony_ci#include "i_pointer_drawing_manager.h"
24c29fa5a6Sopenharmony_ci#include "mmi_log.h"
25c29fa5a6Sopenharmony_ci#include "touch_drawing_manager.h"
26c29fa5a6Sopenharmony_ci
27c29fa5a6Sopenharmony_ci#undef MMI_LOG_DOMAIN
28c29fa5a6Sopenharmony_ci#define MMI_LOG_DOMAIN MMI_LOG_SERVER
29c29fa5a6Sopenharmony_ci#undef MMI_LOG_TAG
30c29fa5a6Sopenharmony_ci#define MMI_LOG_TAG "DelegateInterface"
31c29fa5a6Sopenharmony_ci
32c29fa5a6Sopenharmony_cinamespace OHOS {
33c29fa5a6Sopenharmony_cinamespace MMI {
34c29fa5a6Sopenharmony_civoid DelegateInterface::Init()
35c29fa5a6Sopenharmony_ci{
36c29fa5a6Sopenharmony_ci    TOUCH_DRAWING_MGR->SetDelegateProxy(shared_from_this());
37c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_KEYBOARD
38c29fa5a6Sopenharmony_ci    DISPLAY_MONITOR->SetDelegateProxy(shared_from_this());
39c29fa5a6Sopenharmony_ci#endif // #ifdef OHOS_BUILD_ENABLE_KEYBOARD
40c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_POINTER_DRAWING
41c29fa5a6Sopenharmony_ci    IPointerDrawingManager::GetInstance()->SetDelegateProxy(shared_from_this());
42c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_POINTER_DRAWING
43c29fa5a6Sopenharmony_ci}
44c29fa5a6Sopenharmony_ci
45c29fa5a6Sopenharmony_ciint32_t DelegateInterface::OnPostSyncTask(DTaskCallback cb) const
46c29fa5a6Sopenharmony_ci{
47c29fa5a6Sopenharmony_ci    CHKPR(delegateTasks_, ERROR_NULL_POINTER);
48c29fa5a6Sopenharmony_ci    int32_t ret = delegateTasks_(cb);
49c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
50c29fa5a6Sopenharmony_ci        MMI_HILOGE("Failed to execute the task, ret: %{public}d", ret);
51c29fa5a6Sopenharmony_ci    }
52c29fa5a6Sopenharmony_ci    return ret;
53c29fa5a6Sopenharmony_ci}
54c29fa5a6Sopenharmony_ci
55c29fa5a6Sopenharmony_civoid DelegateInterface::OnInputEvent(
56c29fa5a6Sopenharmony_ci    InputHandlerType type, std::shared_ptr<PointerEvent> event) const
57c29fa5a6Sopenharmony_ci{
58c29fa5a6Sopenharmony_ci#if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
59c29fa5a6Sopenharmony_ci    OnInputEventHandler(type, event);
60c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
61c29fa5a6Sopenharmony_ci}
62c29fa5a6Sopenharmony_ci
63c29fa5a6Sopenharmony_ci#if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
64c29fa5a6Sopenharmony_civoid DelegateInterface::OnInputEventHandler(
65c29fa5a6Sopenharmony_ci    InputHandlerType type, std::shared_ptr<PointerEvent> event) const
66c29fa5a6Sopenharmony_ci{
67c29fa5a6Sopenharmony_ci    CHKPV(event);
68c29fa5a6Sopenharmony_ci    for (const auto &handler : handlers) {
69c29fa5a6Sopenharmony_ci        auto summary = handler.second;
70c29fa5a6Sopenharmony_ci        if (handler.first != type) {
71c29fa5a6Sopenharmony_ci            continue;
72c29fa5a6Sopenharmony_ci        }
73c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_MONITOR
74c29fa5a6Sopenharmony_ci        if (type == InputHandlerType::MONITOR &&
75c29fa5a6Sopenharmony_ci            (summary.eventType & HANDLE_EVENT_TYPE_POINTER) != HANDLE_EVENT_TYPE_POINTER) {
76c29fa5a6Sopenharmony_ci            continue;
77c29fa5a6Sopenharmony_ci        }
78c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_MONITOR
79c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
80c29fa5a6Sopenharmony_ci        uint32_t deviceTags = 0;
81c29fa5a6Sopenharmony_ci        if (type == InputHandlerType::INTERCEPTOR &&
82c29fa5a6Sopenharmony_ci            ((deviceTags & summary.deviceTags) == summary.deviceTags) &&
83c29fa5a6Sopenharmony_ci            !EventInterceptorHandler::CheckInputDeviceSource(event, summary.deviceTags)) {
84c29fa5a6Sopenharmony_ci            continue;
85c29fa5a6Sopenharmony_ci        }
86c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_INTERCEPTOR
87c29fa5a6Sopenharmony_ci        CHKPV(summary.cb);
88c29fa5a6Sopenharmony_ci        if (summary.mode == HandlerMode::SYNC) {
89c29fa5a6Sopenharmony_ci            summary.cb(event);
90c29fa5a6Sopenharmony_ci        } else {
91c29fa5a6Sopenharmony_ci            if (OnPostSyncTask(std::bind(summary.cb, event)) != RET_OK) {
92c29fa5a6Sopenharmony_ci                MMI_HILOGE("Failed to execute the task(%{public}s)", summary.handlerName.c_str());
93c29fa5a6Sopenharmony_ci            }
94c29fa5a6Sopenharmony_ci        }
95c29fa5a6Sopenharmony_ci    }
96c29fa5a6Sopenharmony_ci}
97c29fa5a6Sopenharmony_ci
98c29fa5a6Sopenharmony_ciint32_t DelegateInterface::AddHandler(InputHandlerType type, const HandlerSummary &summary)
99c29fa5a6Sopenharmony_ci{
100c29fa5a6Sopenharmony_ci    CHKPR(summary.cb, ERROR_NULL_POINTER);
101c29fa5a6Sopenharmony_ci    int32_t ret = RET_OK;
102c29fa5a6Sopenharmony_ci    if (HasHandler(summary.handlerName)) {
103c29fa5a6Sopenharmony_ci        MMI_HILOGW("The current handler(%{public}s) already exists", summary.handlerName.c_str());
104c29fa5a6Sopenharmony_ci        return ret;
105c29fa5a6Sopenharmony_ci    }
106c29fa5a6Sopenharmony_ci    const HandleEventType currentType = GetEventType(type);
107c29fa5a6Sopenharmony_ci    uint32_t currentTags = GetDeviceTags(type);
108c29fa5a6Sopenharmony_ci    handlers.emplace(type, summary);
109c29fa5a6Sopenharmony_ci    const HandleEventType newType = GetEventType(type);
110c29fa5a6Sopenharmony_ci    if (currentType != newType || ((currentTags & summary.deviceTags) != summary.deviceTags)) {
111c29fa5a6Sopenharmony_ci        uint32_t allDeviceTags = GetDeviceTags(type);
112c29fa5a6Sopenharmony_ci        if (type == InputHandlerType::INTERCEPTOR) {
113c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
114c29fa5a6Sopenharmony_ci            auto interceptorHandler = InputHandler->GetInterceptorHandler();
115c29fa5a6Sopenharmony_ci            CHKPR(interceptorHandler, ERROR_NULL_POINTER);
116c29fa5a6Sopenharmony_ci            ret = interceptorHandler->AddInputHandler(type,
117c29fa5a6Sopenharmony_ci                newType, summary.priority, allDeviceTags, nullptr);
118c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_INTERCEPTOR
119c29fa5a6Sopenharmony_ci        } else if (type == InputHandlerType::MONITOR) {
120c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_MONITOR
121c29fa5a6Sopenharmony_ci            auto monitorHandler = InputHandler->GetMonitorHandler();
122c29fa5a6Sopenharmony_ci            CHKPR(monitorHandler, ERROR_NULL_POINTER);
123c29fa5a6Sopenharmony_ci            ret = monitorHandler->AddInputHandler(type,
124c29fa5a6Sopenharmony_ci                newType, shared_from_this());
125c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_MONITOR
126c29fa5a6Sopenharmony_ci        }
127c29fa5a6Sopenharmony_ci    }
128c29fa5a6Sopenharmony_ci    if (ret != RET_OK) {
129c29fa5a6Sopenharmony_ci        RemoveLocal(type, summary.handlerName, currentTags);
130c29fa5a6Sopenharmony_ci    } else {
131c29fa5a6Sopenharmony_ci        MMI_HILOGI("Service Add Monitor Success, size:%{public}zu", handlers.size());
132c29fa5a6Sopenharmony_ci    }
133c29fa5a6Sopenharmony_ci    return ret;
134c29fa5a6Sopenharmony_ci}
135c29fa5a6Sopenharmony_ci
136c29fa5a6Sopenharmony_ciHandleEventType DelegateInterface::GetEventType(InputHandlerType type) const
137c29fa5a6Sopenharmony_ci{
138c29fa5a6Sopenharmony_ci    uint32_t eventType {HANDLE_EVENT_TYPE_NONE};
139c29fa5a6Sopenharmony_ci    if (handlers.empty()) {
140c29fa5a6Sopenharmony_ci        MMI_HILOGW("handlers is empty");
141c29fa5a6Sopenharmony_ci        return HANDLE_EVENT_TYPE_NONE;
142c29fa5a6Sopenharmony_ci    }
143c29fa5a6Sopenharmony_ci    for (const auto &handler : handlers) {
144c29fa5a6Sopenharmony_ci        if (handler.first == type) {
145c29fa5a6Sopenharmony_ci            eventType |= handler.second.eventType;
146c29fa5a6Sopenharmony_ci        }
147c29fa5a6Sopenharmony_ci    }
148c29fa5a6Sopenharmony_ci    return eventType;
149c29fa5a6Sopenharmony_ci}
150c29fa5a6Sopenharmony_ci
151c29fa5a6Sopenharmony_ciuint32_t DelegateInterface::GetDeviceTags(InputHandlerType type) const
152c29fa5a6Sopenharmony_ci{
153c29fa5a6Sopenharmony_ci    uint32_t deviceTags = 0;
154c29fa5a6Sopenharmony_ci    if (type == InputHandlerType::MONITOR) {
155c29fa5a6Sopenharmony_ci        return deviceTags;
156c29fa5a6Sopenharmony_ci    }
157c29fa5a6Sopenharmony_ci    if (handlers.empty()) {
158c29fa5a6Sopenharmony_ci        MMI_HILOGW("handlers is empty");
159c29fa5a6Sopenharmony_ci        return deviceTags;
160c29fa5a6Sopenharmony_ci    }
161c29fa5a6Sopenharmony_ci    for (const auto &handler : handlers) {
162c29fa5a6Sopenharmony_ci        if (handler.first == type) {
163c29fa5a6Sopenharmony_ci            deviceTags |= handler.second.deviceTags;
164c29fa5a6Sopenharmony_ci        }
165c29fa5a6Sopenharmony_ci    }
166c29fa5a6Sopenharmony_ci    return deviceTags;
167c29fa5a6Sopenharmony_ci}
168c29fa5a6Sopenharmony_ci
169c29fa5a6Sopenharmony_civoid DelegateInterface::RemoveLocal(InputHandlerType type, const std::string &name, uint32_t &deviceTags)
170c29fa5a6Sopenharmony_ci{
171c29fa5a6Sopenharmony_ci    for (auto it = handlers.cbegin(); it != handlers.cend(); ++it) {
172c29fa5a6Sopenharmony_ci        if (type != it->first) {
173c29fa5a6Sopenharmony_ci            continue;
174c29fa5a6Sopenharmony_ci        }
175c29fa5a6Sopenharmony_ci        if (it->second.handlerName != name) {
176c29fa5a6Sopenharmony_ci            continue;
177c29fa5a6Sopenharmony_ci        }
178c29fa5a6Sopenharmony_ci        handlers.erase(it);
179c29fa5a6Sopenharmony_ci        if (type == InputHandlerType::INTERCEPTOR) {
180c29fa5a6Sopenharmony_ci            deviceTags = it->second.deviceTags;
181c29fa5a6Sopenharmony_ci        }
182c29fa5a6Sopenharmony_ci        break;
183c29fa5a6Sopenharmony_ci    }
184c29fa5a6Sopenharmony_ci}
185c29fa5a6Sopenharmony_ci
186c29fa5a6Sopenharmony_ciint32_t DelegateInterface::GetPriority(InputHandlerType type) const
187c29fa5a6Sopenharmony_ci{
188c29fa5a6Sopenharmony_ci    for (auto it = handlers.cbegin(); it != handlers.cend(); ++it) {
189c29fa5a6Sopenharmony_ci        if (type == it->first) {
190c29fa5a6Sopenharmony_ci            return it->second.priority;
191c29fa5a6Sopenharmony_ci        }
192c29fa5a6Sopenharmony_ci    }
193c29fa5a6Sopenharmony_ci    return DEFUALT_INTERCEPTOR_PRIORITY;
194c29fa5a6Sopenharmony_ci}
195c29fa5a6Sopenharmony_ci
196c29fa5a6Sopenharmony_civoid DelegateInterface::RemoveHandler(InputHandlerType type, const std::string &name)
197c29fa5a6Sopenharmony_ci{
198c29fa5a6Sopenharmony_ci    const HandleEventType currentType = GetEventType(type);
199c29fa5a6Sopenharmony_ci    uint32_t currentTags = GetDeviceTags(type);
200c29fa5a6Sopenharmony_ci    uint32_t deviceTags = 0;
201c29fa5a6Sopenharmony_ci    RemoveLocal(type, name, deviceTags);
202c29fa5a6Sopenharmony_ci    const HandleEventType newType = GetEventType(type);
203c29fa5a6Sopenharmony_ci    const int32_t newLevel = GetPriority(type);
204c29fa5a6Sopenharmony_ci    const uint64_t newTags = GetDeviceTags(type);
205c29fa5a6Sopenharmony_ci    if (currentType != newType || ((currentTags & deviceTags) != 0)) {
206c29fa5a6Sopenharmony_ci        if (type == InputHandlerType::INTERCEPTOR) {
207c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
208c29fa5a6Sopenharmony_ci            auto interceptorHandler = InputHandler->GetInterceptorHandler();
209c29fa5a6Sopenharmony_ci            CHKPV(interceptorHandler);
210c29fa5a6Sopenharmony_ci            interceptorHandler->RemoveInputHandler(type,
211c29fa5a6Sopenharmony_ci                newType, newLevel, newTags, nullptr);
212c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_INTERCEPTOR
213c29fa5a6Sopenharmony_ci        }
214c29fa5a6Sopenharmony_ci        if (type == InputHandlerType::MONITOR) {
215c29fa5a6Sopenharmony_ci#ifdef OHOS_BUILD_ENABLE_MONITOR
216c29fa5a6Sopenharmony_ci            auto monitorHandler = InputHandler->GetMonitorHandler();
217c29fa5a6Sopenharmony_ci            CHKPV(monitorHandler);
218c29fa5a6Sopenharmony_ci            monitorHandler->RemoveInputHandler(type,
219c29fa5a6Sopenharmony_ci                newType, shared_from_this());
220c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_MONITOR
221c29fa5a6Sopenharmony_ci        }
222c29fa5a6Sopenharmony_ci    }
223c29fa5a6Sopenharmony_ci    MMI_HILOGI("Remove Handler:%{public}d:%{public}s-%{public}d:%{public}d, size:%{public}zu", type,
224c29fa5a6Sopenharmony_ci               name.c_str(), currentType, currentTags, handlers.size());
225c29fa5a6Sopenharmony_ci}
226c29fa5a6Sopenharmony_ci
227c29fa5a6Sopenharmony_cibool DelegateInterface::HasHandler(const std::string &name) const
228c29fa5a6Sopenharmony_ci{
229c29fa5a6Sopenharmony_ci    return std::find_if(handlers.cbegin(), handlers.cend(),
230c29fa5a6Sopenharmony_ci        [name](const auto &item) {
231c29fa5a6Sopenharmony_ci            return item.second.handlerName == name;
232c29fa5a6Sopenharmony_ci        }) != handlers.cend();
233c29fa5a6Sopenharmony_ci}
234c29fa5a6Sopenharmony_ci#endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
235c29fa5a6Sopenharmony_ci} // namespace MMI
236c29fa5a6Sopenharmony_ci} // namespace OHOS