1 /*
2 * Copyright (c) 2021-2022 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 #include "interceptor_manager.h"
16 #include <cinttypes>
17 #include "bytrace.h"
18 #include "define_multimodal.h"
19 #include "error_multimodal.h"
20 #include "souceType.h"
21
22 namespace OHOS {
23 namespace MMI {
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InterceptorManager" };
26 }
27
InterceptorManager()28 InterceptorManager::InterceptorManager()
29 {
30 InterceptorItemId = 0;
31 }
32
~InterceptorManager()33 InterceptorManager::~InterceptorManager() {}
34
AddInterceptor(int32_t sourceType,std::function<void (std::shared_ptr<PointerEvent>)> interceptor)35 int32_t InterceptorManager::AddInterceptor(int32_t sourceType,
36 std::function<void(std::shared_ptr<PointerEvent>)> interceptor)
37 {
38 CHKPR(interceptor, INVALID_INTERCEPTOR_ID);
39 InterceptorItem interceptorItem;
40 interceptorItem.id_ = ++InterceptorItemId;
41 interceptorItem.sourceType = sourceType;
42 interceptorItem.callback = interceptor;
43 interceptor_.push_back(interceptorItem);
44 MMIEventHdl.AddInterceptor(interceptorItem.sourceType, interceptorItem.id_);
45 MMI_LOGD("Add sourceType:%{public}d Touchpad to InterceptorManager success", sourceType);
46 return interceptorItem.id_;
47 }
48
AddInterceptor(std::function<void (std::shared_ptr<KeyEvent>)> interceptor)49 int32_t InterceptorManager::AddInterceptor(std::function<void(std::shared_ptr<KeyEvent>)> interceptor)
50 {
51 CHKPR(interceptor, ERROR_NULL_POINTER);
52 InterceptorItem interceptorItem;
53 interceptorItem.id_ = ++InterceptorItemId;
54 interceptorItem.sourceType = SOURCETYPE_KEY;
55 interceptorItem.callback_ = interceptor;
56 interceptor_.push_back(interceptorItem);
57 if (MMIEventHdl.AddInterceptor(interceptorItem.sourceType, interceptorItem.id_) == RET_OK) {
58 MMI_LOGD("Add AddInterceptor KeyEvent to InterceptorManager success");
59 return MMI_STANDARD_EVENT_SUCCESS;
60 }
61 return MMI_STANDARD_EVENT_INVALID_PARAM;
62 }
63
RemoveInterceptor(int32_t interceptorId)64 void InterceptorManager::RemoveInterceptor(int32_t interceptorId)
65 {
66 if (interceptorId <= 0) {
67 MMI_LOGE("interceptorId invalid");
68 return;
69 }
70 InterceptorItem interceptorItem;
71 interceptorItem.id_ = interceptorId;
72 auto iter = std::find(interceptor_.begin(), interceptor_.end(), interceptorItem);
73 if (iter == interceptor_.end()) {
74 MMI_LOGE("InterceptorItem does not exist");
75 } else {
76 iter = interceptor_.erase(iter);
77 MMIEventHdl.RemoveInterceptor(interceptorItem.id_);
78 MMI_LOGD("InterceptorItem id:%{public}d removed success", interceptorId);
79 }
80 }
81
OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent,int32_t id)82 int32_t InterceptorManager::OnPointerEvent(std::shared_ptr<PointerEvent> pointerEvent, int32_t id)
83 {
84 CHKPR(pointerEvent, ERROR_NULL_POINTER);
85 PointerEvent::PointerItem pointer;
86 CHKR(pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointer), PARAM_INPUT_FAIL, RET_ERR);
87 MMI_LOGD("Interceptor-clienteventTouchpad:actionTime:%{public}" PRId64 ","
88 "sourceType:%{public}d,pointerAction:%{public}d,"
89 "pointer:%{public}d,point.x:%{public}d,point.y:%{public}d,press:%{public}d",
90 pointerEvent->GetActionTime(), pointerEvent->GetSourceType(), pointerEvent->GetPointerAction(),
91 pointerEvent->GetPointerId(), pointer.GetGlobalX(), pointer.GetGlobalY(), pointer.IsPressed());
92 InterceptorItem interceptorItem;
93 interceptorItem.id_ = id;
94 auto iter = std::find(interceptor_.begin(), interceptor_.end(), interceptorItem);
95 if (iter != interceptor_.end() && iter->callback != nullptr) {
96 MMI_LOGD("interceptor callback execute");
97 iter->callback(pointerEvent);
98 }
99 return MMI_STANDARD_EVENT_SUCCESS;
100 }
101
OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent)102 int32_t InterceptorManager::OnKeyEvent(std::shared_ptr<KeyEvent> keyEvent)
103 {
104 CHKPR(keyEvent, ERROR_NULL_POINTER);
105 int32_t keyId = keyEvent->GetId();
106 const std::string keyEventString = "keyEventFilter";
107 FinishAsyncTrace(BYTRACE_TAG_MULTIMODALINPUT, keyEventString, keyId);
108 for (auto &item : interceptor_) {
109 if (item.sourceType == SOURCETYPE_KEY) {
110 MMI_LOGD("interceptor callback execute");
111 item.callback_(keyEvent);
112 }
113 }
114 return MMI_STANDARD_EVENT_SUCCESS;
115 }
116 } // namespace MMI
117 } // namespace OHOS