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(std::shared_ptr<MMI::PointerEvent> pointerEvent);
100 bool IsActive(std::shared_ptr<MMI::PointerEvent> pointerEvent);
101 void ResetPressedEvents();
102 double GetDamplingCoefficient(DamplingDirection direction) const;
103 bool DampPointerMotion(std::shared_ptr<MMI::PointerEvent> pointerEvent);
104 void ExecuteInner();
105 void HandleStopTimer();
106
107 IContext *env_ { nullptr };
108 bool enable_ { false };
109 bool freezing_ { false };
110 int32_t xDir_ { 0 };
111 int32_t movement_ { 0 };
112 size_t nDropped_ { 0 };
113 bool scanState_ { true };
114 int32_t pointerEventTimer_ { -1 };
115 double rawDxRightRemainder_ { 0.0 };
116 double rawDxLeftRemainder_ { 0.0 };
117 std::string remoteNetworkId_;
118 std::array<double, N_DAMPLING_DIRECTIONS> damplingCoefficients_;
119 std::shared_ptr<DSoftbusObserver> observer_;
120 std::shared_ptr<MMI::PointerEvent> pointerEvent_;
121 std::shared_ptr<MMI::KeyEvent> keyEvent_;
122 std::shared_mutex lock_;
123 std::unordered_map<int32_t, int32_t> remote2VirtualIds_;
124 void TagRemoteEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent);
125 void OnNotifyCrossDrag(std::shared_ptr<MMI::PointerEvent> pointerEvent);
126 };
127
IsLocalEvent(const InputPointerEvent & event)128 inline bool InputEventBuilder::IsLocalEvent(const InputPointerEvent &event)
129 {
130 return (event.deviceId >= 0 && event.deviceId < MIN_MMI_VIRTUAL_DEVICE_ID);
131 }
132 } // namespace Cooperate
133 } // namespace DeviceStatus
134 } // namespace Msdp
135 } // namespace OHOS
136 #endif // INPUT_EVENT_BUILDER_H
137