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