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 #ifndef DELEGATE_INTERFACE_H
17 #define DELEGATE_INTERFACE_H
18 
19 #include <functional>
20 
21 #include "nocopyable.h"
22 
23 #include "delegate_tasks.h"
24 #include "i_input_event_handler.h"
25 
26 namespace OHOS {
27 namespace MMI {
28 enum class HandlerMode {
29     SYNC,
30     ASYNC
31 };
32 using TaskCallback = std::function<int32_t(std::shared_ptr<PointerEvent>)>;
33 class DelegateInterface final :
34     public IInputEventHandler::IInputEventConsumer,
35     public std::enable_shared_from_this<DelegateInterface> {
36 public:
37     DISALLOW_COPY_AND_MOVE(DelegateInterface);
DelegateInterface(std::function<int32_t (DTaskCallback)> delegate)38     explicit DelegateInterface(std::function<int32_t(DTaskCallback)> delegate)
39         : delegateTasks_(delegate) {}
40     void Init();
41     int32_t OnPostSyncTask(DTaskCallback cb) const;
42 
43 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
44     struct HandlerSummary {
45         std::string handlerName;
46         HandleEventType eventType;
47         HandlerMode mode;
48         int32_t priority;
49         uint32_t deviceTags;
50         TaskCallback cb;
51     };
52     void RemoveHandler(InputHandlerType handlerType, std::string name);
53     int32_t AddHandler(InputHandlerType handlerType, HandlerSummary summary);
54 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
55 
56 private:
57 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
58     uint32_t GetDeviceTags(InputHandlerType type) const;
59     int32_t GetPriority(InputHandlerType type) const;
60     HandleEventType GetEventType(InputHandlerType type) const;
61     void RemoveLocal(InputHandlerType type, std::string name, uint32_t &deviceTags);
62     void OnInputEventHandler(InputHandlerType type, std::shared_ptr<PointerEvent> event) const;
63 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
OnInputEvent(InputHandlerType type,std::shared_ptr<KeyEvent> event)64     virtual void OnInputEvent(InputHandlerType type, std::shared_ptr<KeyEvent> event) const override {}
65     virtual void OnInputEvent(InputHandlerType type, std::shared_ptr<PointerEvent> event) const override;
OnInputEvent(InputHandlerType type,std::shared_ptr<AxisEvent> event)66     virtual void OnInputEvent(InputHandlerType type, std::shared_ptr<AxisEvent> event) const override {}
67 
68 private:
69     std::function<int32_t(DTaskCallback)> delegateTasks_;
70 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR)
71     std::unordered_multimap<InputHandlerType, HandlerSummary> handlers;
72 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR
73 };
74 } // namespace MMI
75 } // namespace OHOS
76 #endif // DELEGATE_INTERFACE_H