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 #ifndef INPUT_EVENT_BUILDER_H
17 #define INPUT_EVENT_BUILDER_H
18
19 #include <shared_mutex>
20
21 #include "display_manager.h"
22 #include "key_event.h"
23 #include "nocopyable.h"
24 #include "pointer_event.h"
25
26 #include "cooperate_events.h"
27 #include "i_context.h"
28 #include "i_dsoftbus_adapter.h"
29 #include "net_packet.h"
30
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 namespace {
35 constexpr int32_t MIN_MMI_VIRTUAL_DEVICE_ID { 1000 };
36 }
37 namespace Cooperate {
38 class Context;
39
40 class InputEventBuilder final {
41 class DSoftbusObserver final : public IDSoftbusObserver {
42 public:
DSoftbusObserver(InputEventBuilder & parent)43 DSoftbusObserver(InputEventBuilder &parent) : parent_(parent) {}
44 ~DSoftbusObserver() = default;
45
OnBind(const std::string & networkId)46 void OnBind(const std::string &networkId) override {}
OnShutdown(const std::string & networkId)47 void OnShutdown(const std::string &networkId) override {}
OnConnected(const std::string & networkId)48 void OnConnected(const std::string &networkId) override {}
49
OnPacket(const std::string & networkId,Msdp::NetPacket & packet)50 bool OnPacket(const std::string &networkId, Msdp::NetPacket &packet) override
51 {
52 return parent_.OnPacket(networkId, packet);
53 }
54
OnRawData(const std::string & networkId,const void * data,uint32_t dataLen)55 bool OnRawData(const std::string &networkId, const void *data, uint32_t dataLen) override
56 {
57 return false;
58 }
59
60 private:
61 InputEventBuilder &parent_;
62 };
63
64 struct CursorPosition {
65 Rosen::DisplayId displayId { Rosen::DISPLAY_ID_INVALID };
66 Coordinate pos {};
67 };
68
69 enum DamplingDirection : size_t {
70 DAMPLING_DIRECTION_UP = 0,
71 DAMPLING_DIRECTION_DOWN,
72 DAMPLING_DIRECTION_LEFT,
73 DAMPLING_DIRECTION_RIGHT,
74 N_DAMPLING_DIRECTIONS,
75 };
76
77 public:
78 InputEventBuilder(IContext *env);
79 ~InputEventBuilder();
80 DISALLOW_COPY_AND_MOVE(InputEventBuilder);
81
82 void Enable(Context &context);
83 void Disable();
84 void Update(Context &context);
85 void Freeze();
86 void Thaw();
87 void SetDamplingCoefficient(uint32_t direction, double coefficient);
88 void UpdateVirtualDeviceIdMap(const std::unordered_map<int32_t, int32_t> &remote2VirtualIds);
89
90 static bool IsLocalEvent(const InputPointerEvent &event);
91
92 private:
93 bool OnPacket(const std::string &networkId, Msdp::NetPacket &packet);
94 void OnPointerEvent(Msdp::NetPacket &packet);
95 void OnKeyEvent(Msdp::NetPacket &packet);
96 void TurnOffChannelScan();
97 void TurnOnChannelScan();
98 int32_t SetWifiScene(unsigned int scene);
99 bool UpdatePointerEvent();
100 void UpdateKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent);
101 bool IsActive(std::shared_ptr<MMI::PointerEvent> pointerEvent);
102 void ResetPressedEvents();
103 double GetDamplingCoefficient(DamplingDirection direction) const;
104 bool DampPointerMotion(std::shared_ptr<MMI::PointerEvent> pointerEvent);
105 void ExecuteInner();
106 void HandleStopTimer();
107 void CheckLatency(int64_t sourceActionTime, int64_t interceptorTime,
108 int64_t builderRecvTime, std::shared_ptr<MMI::PointerEvent> pointerEvent);
109
110 IContext *env_ { nullptr };
111 bool enable_ { false };
112 bool freezing_ { false };
113 int32_t xDir_ { 0 };
114 int32_t movement_ { 0 };
115 size_t nDropped_ { 0 };
116 bool scanState_ { true };
117 int32_t pointerEventTimer_ { -1 };
118 double rawDxRightRemainder_ { 0.0 };
119 double rawDxLeftRemainder_ { 0.0 };
120 int64_t driveEventTimeDT_ { -1 };
121 int64_t cooperateInterceptorTimeDT_ { -1 };
122 int64_t crossPlatformTimeDT_ { -1 };
123 int64_t preDriveEventTime_ { -1 };
124 int64_t preInterceptorTime_ { -1 };
125 int64_t preCrossPlatformTime_ { -1 };
126 int32_t pointerSpeed_ { -1 };
127 int32_t touchPadSpeed_ { -1 };
128 std::string remoteNetworkId_;
129 std::string localNetworkId_;
130 std::array<double, N_DAMPLING_DIRECTIONS> damplingCoefficients_;
131 std::shared_ptr<DSoftbusObserver> observer_;
132 std::shared_ptr<MMI::PointerEvent> pointerEvent_;
133 std::shared_ptr<MMI::KeyEvent> keyEvent_;
134 std::shared_mutex lock_;
135 std::unordered_map<int32_t, int32_t> remote2VirtualIds_;
136 void TagRemoteEvent(std::shared_ptr<MMI::KeyEvent> KeyEvent);
137 void TagRemoteEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent);
138 void OnNotifyCrossDrag(std::shared_ptr<MMI::PointerEvent> pointerEvent);
139 };
140
IsLocalEvent(const InputPointerEvent & event)141 inline bool InputEventBuilder::IsLocalEvent(const InputPointerEvent &event)
142 {
143 return (event.deviceId >= 0 && event.deviceId < MIN_MMI_VIRTUAL_DEVICE_ID);
144 }
145 } // namespace Cooperate
146 } // namespace DeviceStatus
147 } // namespace Msdp
148 } // namespace OHOS
149 #endif // INPUT_EVENT_BUILDER_H
150