• 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 "oh_input_interceptor.h"
17 #include "input_manager_impl.h"
18 #include "mmi_log.h"
19 
20 #undef MMI_LOG_TAG
21 #define MMI_LOG_TAG "OHInputInterceptor"
22 
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr int32_t INVALID_INTERCEPTOR_ID = -1;
27 }
Start(OHInterceptorType type)28 int32_t OHInputInterceptor::Start(OHInterceptorType type)
29 {
30     CALL_DEBUG_ENTER;
31     std::lock_guard<std::mutex> guard(mutex_);
32     if (type == INTERCEPTOR_TYPE_KEY) {
33         if (keyInterceptorId_ < 0) {
34             keyInterceptorId_ = InputMgrImpl.AddInterceptor(shared_from_this(), DEFUALT_INTERCEPTOR_PRIORITY,
35                 CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD));
36         }
37         return keyInterceptorId_;
38     } else {
39         if (pointerInterceptorId_ < 0) {
40             uint32_t deviceTags = CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_MAX) -
41                 CapabilityToTags(InputDeviceCapability::INPUT_DEV_CAP_KEYBOARD);
42             pointerInterceptorId_ = InputMgrImpl.AddInterceptor(shared_from_this(),
43                 DEFUALT_INTERCEPTOR_PRIORITY, deviceTags);
44         }
45         return pointerInterceptorId_;
46     }
47 }
48 
Stop(OHInterceptorType type)49 int32_t OHInputInterceptor::Stop(OHInterceptorType type)
50 {
51     CALL_DEBUG_ENTER;
52     std::lock_guard<std::mutex> guard(mutex_);
53     if (keyInterceptorId_ < 0 && pointerInterceptorId_ < 0) {
54         MMI_HILOGE("There is no interceptor to be removed");
55         return RET_ERR;
56     }
57     if (type == INTERCEPTOR_TYPE_KEY && keyInterceptorId_ != INVALID_INTERCEPTOR_ID) {
58         int32_t ret = InputMgrImpl.RemoveInterceptor(keyInterceptorId_);
59         if (ret != RET_OK) {
60             return ret;
61         }
62         keyInterceptorId_ = INVALID_INTERCEPTOR_ID;
63     } else if (type == INTERCEPTOR_TYPE_POINTER && pointerInterceptorId_ != INVALID_INTERCEPTOR_ID) {
64         int32_t ret = InputMgrImpl.RemoveInterceptor(pointerInterceptorId_);
65         if (ret != RET_OK) {
66             return ret;
67         }
68         pointerInterceptorId_ = INVALID_INTERCEPTOR_ID;
69     }
70     return RET_OK;
71 }
72 
SetCallback(std::function<void (std::shared_ptr<PointerEvent>)> callback)73 void OHInputInterceptor::SetCallback(std::function<void(std::shared_ptr<PointerEvent>)> callback)
74 {
75     std::lock_guard<std::mutex> guard(mutex_);
76     pointerCallback_ = callback;
77 }
78 
SetCallback(std::function<void (std::shared_ptr<KeyEvent>)> callback)79 void OHInputInterceptor::SetCallback(std::function<void(std::shared_ptr<KeyEvent>)> callback)
80 {
81     std::lock_guard<std::mutex> guard(mutex_);
82     keyCallback_ = callback;
83 }
84 
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const85 void OHInputInterceptor::OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const
86 {
87     CALL_DEBUG_ENTER;
88     std::lock_guard<std::mutex> guard(mutex_);
89     CHKPV(pointerCallback_);
90     pointerCallback_(pointerEvent);
91 }
92 
OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const93 void OHInputInterceptor::OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const
94 {
95     CALL_DEBUG_ENTER;
96     std::lock_guard<std::mutex> guard(mutex_);
97     CHKPV(keyCallback_);
98     keyCallback_(keyEvent);
99 }
100 
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const101 void OHInputInterceptor::OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const {}
102 }
103 }