1 /*
2 * Copyright (c) 2023 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 "input_event_transmission/input_event_interceptor.h"
17
18 #include <unordered_set>
19
20 #include "cooperate_context.h"
21 #include "devicestatus_define.h"
22 #include "input_event_transmission/input_event_serialization.h"
23 #include "utility.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "InputEventInterceptor"
27
28 namespace OHOS {
29 namespace Msdp {
30 namespace DeviceStatus {
31 namespace Cooperate {
32 std::unordered_set<int32_t> InputEventInterceptor::filterKeys_ {
33 MMI::KeyEvent::KEYCODE_BACK,
34 MMI::KeyEvent::KEYCODE_VOLUME_UP,
35 MMI::KeyEvent::KEYCODE_VOLUME_DOWN,
36 MMI::KeyEvent::KEYCODE_POWER,
37 };
38
39 std::unordered_set<int32_t> InputEventInterceptor::filterPointers_ {
40 MMI::PointerEvent::POINTER_ACTION_ENTER_WINDOW,
41 MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW,
42 MMI::PointerEvent::POINTER_ACTION_PULL_IN_WINDOW,
43 MMI::PointerEvent::POINTER_ACTION_PULL_OUT_WINDOW,
44 };
45
~InputEventInterceptor()46 InputEventInterceptor::~InputEventInterceptor()
47 {
48 Disable();
49 }
50
Enable(Context & context)51 void InputEventInterceptor::Enable(Context &context)
52 {
53 CALL_INFO_TRACE;
54 if (interceptorId_ > 0) {
55 return;
56 }
57 auto cursorPos = context.CursorPosition();
58 FI_HILOGI("Cursor transite out at (%{public}d, %{public}d)", cursorPos.x, cursorPos.y);
59 remoteNetworkId_ = context.Peer();
60 sender_ = context.Sender();
61 interceptorId_ = env_->GetInput().AddInterceptor(
62 [this](std::shared_ptr<MMI::PointerEvent> pointerEvent) { this->OnPointerEvent(pointerEvent); },
63 [this](std::shared_ptr<MMI::KeyEvent> keyEvent) { this->OnKeyEvent(keyEvent); });
64 if (interceptorId_ < 0) {
65 FI_HILOGE("Input::AddInterceptor fail");
66 }
67 }
68
Disable()69 void InputEventInterceptor::Disable()
70 {
71 CALL_INFO_TRACE;
72 if (interceptorId_ > 0) {
73 env_->GetInput().RemoveInterceptor(interceptorId_);
74 interceptorId_ = -1;
75 }
76 }
77
Update(Context & context)78 void InputEventInterceptor::Update(Context &context)
79 {
80 remoteNetworkId_ = context.Peer();
81 FI_HILOGI("Update peer to \'%{public}s\'", Utility::Anonymize(remoteNetworkId_).c_str());
82 }
83
OnPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)84 void InputEventInterceptor::OnPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)
85 {
86 CHKPV(pointerEvent);
87 if (auto pointerAction = pointerEvent->GetPointerAction();
88 filterPointers_.find(pointerAction) != filterPointers_.end()) {
89 FI_HILOGI("Current pointerAction:%{public}d, skip", static_cast<int32_t>(pointerAction));
90 return;
91 }
92 if (auto pointerAction = pointerEvent->GetPointerAction();
93 pointerAction == MMI::PointerEvent::POINTER_ACTION_CANCEL) {
94 auto originAction = pointerEvent->GetOriginPointerAction();
95 FI_HILOGI("Reset to origin action:%{public}d", static_cast<int32_t>(originAction));
96 pointerEvent->SetPointerAction(originAction);
97 }
98 NetPacket packet(MessageId::DSOFTBUS_INPUT_POINTER_EVENT);
99
100 int32_t ret = InputEventSerialization::Marshalling(pointerEvent, packet);
101 if (ret != RET_OK) {
102 FI_HILOGE("Failed to serialize pointer event");
103 return;
104 }
105 FI_HILOGI("PointerEvent(No:%{public}d,Source:%{public}s,Action:%{public}s)",
106 pointerEvent->GetId(), pointerEvent->DumpSourceType(), pointerEvent->DumpPointerAction());
107 env_->GetDSoftbus().SendPacket(remoteNetworkId_, packet);
108 }
109
OnKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)110 void InputEventInterceptor::OnKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)
111 {
112 CHKPV(keyEvent);
113 if (filterKeys_.find(keyEvent->GetKeyCode()) != filterKeys_.end()) {
114 keyEvent->AddFlag(MMI::AxisEvent::EVENT_FLAG_NO_INTERCEPT);
115 env_->GetInput().SimulateInputEvent(keyEvent);
116 return;
117 }
118 NetPacket packet(MessageId::DSOFTBUS_INPUT_KEY_EVENT);
119
120 int32_t ret = InputEventSerialization::KeyEventToNetPacket(keyEvent, packet);
121 if (ret != RET_OK) {
122 FI_HILOGE("Failed to serialize key event");
123 return;
124 }
125 FI_HILOGD("KeyEvent(No:%{public}d,Key:%{private}d,Action:%{public}d)",
126 keyEvent->GetId(), keyEvent->GetKeyCode(), keyEvent->GetKeyAction());
127 env_->GetDSoftbus().SendPacket(remoteNetworkId_, packet);
128 }
129
ReportPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)130 void InputEventInterceptor::ReportPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent)
131 {
132 MMI::PointerEvent::PointerItem pointerItem;
133
134 if (!pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem)) {
135 FI_HILOGE("Corrupted pointer event");
136 return;
137 }
138 auto ret = sender_.Send(CooperateEvent(
139 CooperateEventType::INPUT_POINTER_EVENT,
140 InputPointerEvent {
141 .deviceId = pointerEvent->GetDeviceId(),
142 .pointerAction = pointerEvent->GetPointerAction(),
143 .sourceType = pointerEvent->GetSourceType(),
144 .position = Coordinate {
145 .x = pointerItem.GetDisplayX(),
146 .y = pointerItem.GetDisplayY(),
147 }
148 }));
149 if (ret != Channel<CooperateEvent>::NO_ERROR) {
150 FI_HILOGE("Failed to send event via channel, error:%{public}d", ret);
151 }
152 }
153 } // namespace Cooperate
154 } // namespace DeviceStatus
155 } // namespace Msdp
156 } // namespace OHOS
157