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,std::function<int32_t (DTaskCallback)> asyncFun)38 explicit DelegateInterface(std::function<int32_t(DTaskCallback)> delegate, 39 std::function<int32_t(DTaskCallback)> asyncFun) : delegateTasks_(delegate), asyncDelegateTasks_(asyncFun) {} 40 void Init(); 41 int32_t OnPostSyncTask(DTaskCallback cb) const; 42 int32_t OnPostAsyncTask(DTaskCallback cb) const; 43 44 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) 45 struct HandlerSummary { 46 std::string handlerName; 47 HandleEventType eventType { HANDLE_EVENT_TYPE_NONE }; 48 HandlerMode mode { HandlerMode::SYNC }; 49 int32_t priority {}; 50 uint32_t deviceTags {}; 51 TouchGestureType gestureType { TOUCH_GESTURE_TYPE_NONE }; 52 int32_t fingers {}; 53 TaskCallback cb; 54 }; 55 56 void RemoveHandler(InputHandlerType handlerType, const std::string &name); 57 int32_t AddHandler(InputHandlerType handlerType, const HandlerSummary &summary); 58 bool HasHandler(const std::string &name) const; 59 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR 60 61 private: 62 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) 63 uint32_t GetDeviceTags(InputHandlerType type) const; 64 int32_t GetPriority(InputHandlerType type) const; 65 HandleEventType GetEventType(InputHandlerType type) const; 66 std::optional<HandlerSummary> RemoveLocal(InputHandlerType type, const std::string &name, uint32_t &deviceTags); 67 void OnInputEventHandler(InputHandlerType type, std::shared_ptr<PointerEvent> event) const; 68 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR OnInputEvent(InputHandlerType type,std::shared_ptr<KeyEvent> event)69 virtual void OnInputEvent(InputHandlerType type, std::shared_ptr<KeyEvent> event) const override {} 70 virtual void OnInputEvent(InputHandlerType type, std::shared_ptr<PointerEvent> event) const override; OnInputEvent(InputHandlerType type,std::shared_ptr<AxisEvent> event)71 virtual void OnInputEvent(InputHandlerType type, std::shared_ptr<AxisEvent> event) const override {} 72 #ifdef OHOS_BUILD_ENABLE_MONITOR 73 bool MonitorExpectEvent(const HandlerSummary &monitor, std::shared_ptr<PointerEvent> event) const; 74 #endif // OHOS_BUILD_ENABLE_MONITOR 75 76 private: 77 std::function<int32_t(DTaskCallback)> delegateTasks_; 78 std::function<int32_t(DTaskCallback)> asyncDelegateTasks_; 79 #if defined(OHOS_BUILD_ENABLE_INTERCEPTOR) || defined(OHOS_BUILD_ENABLE_MONITOR) 80 std::unordered_multimap<InputHandlerType, HandlerSummary> handlers_; 81 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR || OHOS_BUILD_ENABLE_MONITOR 82 }; 83 } // namespace MMI 84 } // namespace OHOS 85 #endif // DELEGATE_INTERFACE_H