• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 COOPERATE_CONTEXT_H
17 #define COOPERATE_CONTEXT_H
18 
19 #include "display_info.h"
20 #include "display_manager.h"
21 #include "event_handler.h"
22 #include "nocopyable.h"
23 
24 #ifdef ENABLE_PERFORMANCE_CHECK
25 #include <chrono>
26 #include <mutex>
27 #endif // ENABLE_PERFORMANCE_CHECK
28 
29 #include "common_event_adapter.h"
30 #include "common_event_observer.h"
31 #include "cooperate_events.h"
32 #include "ddm_adapter.h"
33 #include "dsoftbus_handler.h"
34 #include "event_manager.h"
35 #include "hot_area.h"
36 #include "input_device_mgr.h"
37 #include "input_event_transmission/input_event_builder.h"
38 #include "input_event_transmission/input_event_interceptor.h"
39 #include "i_context.h"
40 #include "mouse_location.h"
41 
42 namespace OHOS {
43 namespace Msdp {
44 namespace DeviceStatus {
45 namespace {
46     constexpr uint32_t COOPERATE_WITH_CROSS_DRAGGING { 0x1 };
47 }
48 namespace Cooperate {
49 class Context final {
50 public:
51     Context(IContext *env);
52     ~Context() = default;
53     DISALLOW_COPY_AND_MOVE(Context);
54 
55     void AttachSender(Channel<CooperateEvent>::Sender sender);
56     void AddObserver(std::shared_ptr<ICooperateObserver> observer);
57     void RemoveObserver(std::shared_ptr<ICooperateObserver> observer);
58     void Enable();
59     void Disable();
60 
61     Channel<CooperateEvent>::Sender Sender() const;
62     std::shared_ptr<AppExecFwk::EventHandler> EventHandler() const;
63     std::string Local() const;
64     std::string Peer() const;
65     int32_t StartDeviceId() const;
66     Coordinate CursorPosition() const;
67     NormalizedCoordinate NormalizedCursorPosition() const;
68     uint32_t CooperateFlag() const;
69 
70     bool IsLocal(const std::string &networkId) const;
71     bool IsPeer(const std::string &networkId) const;
72     bool NeedHideCursor() const;
73     bool IsCooperateWithCrossDrag() const;
74     bool NeedFreezeCursor() const;
75 
76     void EnableCooperate(const EnableCooperateEvent &event);
77     void DisableCooperate(const DisableCooperateEvent &event);
78     void ResetPriv();
79     void StartCooperate(const StartCooperateEvent &event);
80     void RemoteStartSuccess(const DSoftbusStartCooperateFinished &event);
81     void RelayCooperate(const DSoftbusRelayCooperate &event);
82     void OnPointerEvent(const InputPointerEvent &event);
83     void UpdateCooperateFlag(const UpdateCooperateFlagEvent &event);
84     void UpdateCursorPosition();
85     void ResetCursorPosition();
86     int32_t GetPointerSpeed();
87     void SetPointerSpeed(int32_t speed);
88     int32_t GetTouchPadSpeed();
89     void SetTouchPadSpeed(int32_t speed);
90     void SetVirtualTrackpadDeviceId(int32_t deviceId);
91     int32_t GetVirtualTrackpadDeviceId();
92     void ResetVirtualTrackpadDeviceId();
93     void AdjustPointerPos(DSoftbusCooperateOptions& dSoftbusCooperateOptions);
94     bool IsAllowCooperate();
95     void OnStartCooperate(StartCooperateData &data);
96     void OnRemoteStartCooperate(RemoteStartCooperateData &data);
97     void OnStopCooperate();
98     void OnTransitionOut();
99     void OnTransitionIn();
100     void OnBack();
101     void OnRelayCooperation(const std::string &networkId, const NormalizedCoordinate &cursorPos);
102     void OnResetCooperation();
103     void CloseDistributedFileConnection(const std::string &remoteNetworkId);
104     void OnRemoteStart(const DSoftbusCooperateWithOptionsFinished &event);
105     void StartCooperateWithOptions(const StartWithOptionsEvent &event);
106     void UpdateCooperateOptions(const StartWithOptionsEvent &event);
107     void StorePeerPointerSpeed(int32_t speed);
108     void ClearPeerPointerSpeed();
109     void StoreOriginPointerSpeed();
110     void StorePeerTouchPadSpeed(int32_t speed);
111     void ClearPeerTouchPadSpeed();
112     void StoreOriginTouchPadSpeed();
113 
114 #ifdef ENABLE_PERFORMANCE_CHECK
115     void StartTrace(const std::string &name);
116     void FinishTrace(const std::string &name);
117 #endif // ENABLE_PERFORMANCE_CHECK
118 
119     DSoftbusHandler dsoftbus_;
120     EventManager eventMgr_;
121     HotArea hotArea_;
122     MouseLocation mouseLocation_;
123     InputDeviceMgr inputDevMgr_;
124     InputEventBuilder inputEventBuilder_;
125     InputEventInterceptor inputEventInterceptor_;
126     CommonEventAdapter commonEvent_;
127 
128 private:
129     int32_t StartEventHandler();
130     void StopEventHandler();
131     // The DDM is abbreviation for 'Distributed Device Manager'.
132     void EnableDDM();
133     void DisableDDM();
134     int32_t EnableDevMgr();
135     void DisableDevMgr();
136     int32_t EnableInputDevMgr();
137     void DisableInputDevMgr();
138     void SetCursorPosition(const Coordinate &cursorPos);
139     void StopCooperateSetCursorPosition(const Coordinate &cursorPos);
140     Coordinate GetCursorPos(const Coordinate &cursorPos);
141 
142     IContext *env_ { nullptr };
143     Channel<CooperateEvent>::Sender sender_;
144     std::string remoteNetworkId_;
145     int32_t startDeviceId_ { -1 };
146     uint32_t flag_ {};
147     uint32_t priv_ { 0 };
148     Coordinate cursorPos_ {};
149     CooperateOptions cooperateOptions_ {};
150     int32_t currentDisplayId_ { 0 };
151     int32_t peerPointerSpeed_ { -1 };
152     int32_t originPointerSpeed_ { -1 };
153     int32_t peerTouchPadSpeed_ { -1 };
154     int32_t originTouchPadSpeed_ { -1 };
155     int32_t VirtualTrackpadDeviceId_ { -1 };
156     std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
157     std::shared_ptr<IBoardObserver> boardObserver_;
158     std::shared_ptr<IDeviceObserver> hotplugObserver_;
159     std::set<std::shared_ptr<ICooperateObserver>> observers_;
160 
161 #ifdef ENABLE_PERFORMANCE_CHECK
162     std::mutex lock_;
163     std::map<std::string, std::chrono::time_point<std::chrono::steady_clock>> traces_;
164 #endif // ENABLE_PERFORMANCE_CHECK
165 };
166 
167 class HotplugObserver final : public IDeviceObserver {
168 public:
HotplugObserver(Channel<CooperateEvent>::Sender sender)169     explicit HotplugObserver(Channel<CooperateEvent>::Sender sender) : sender_(sender) {}
170     ~HotplugObserver() = default;
171 
172     void OnDeviceAdded(std::shared_ptr<IDevice> dev) override;
173     void OnDeviceRemoved(std::shared_ptr<IDevice> dev) override;
174 
175 private:
176     Channel<CooperateEvent>::Sender sender_;
177 };
178 
Sender()179 inline Channel<CooperateEvent>::Sender Context::Sender() const
180 {
181     return sender_;
182 }
183 
EventHandler()184 inline std::shared_ptr<AppExecFwk::EventHandler> Context::EventHandler() const
185 {
186     return eventHandler_;
187 }
188 
Local()189 inline std::string Context::Local() const
190 {
191     return DSoftbusHandler::GetLocalNetworkId();
192 }
193 
Peer()194 inline std::string Context::Peer() const
195 {
196     return remoteNetworkId_;
197 }
198 
StartDeviceId()199 inline int32_t Context::StartDeviceId() const
200 {
201     return startDeviceId_;
202 }
203 
CursorPosition()204 inline Coordinate Context::CursorPosition() const
205 {
206     return cursorPos_;
207 }
208 
CooperateFlag()209 inline uint32_t Context::CooperateFlag() const
210 {
211     return flag_;
212 }
213 
IsLocal(const std::string & networkId)214 inline bool Context::IsLocal(const std::string &networkId) const
215 {
216     return (networkId == DSoftbusHandler::GetLocalNetworkId());
217 }
218 
IsPeer(const std::string & networkId)219 inline bool Context::IsPeer(const std::string &networkId) const
220 {
221     return (networkId == remoteNetworkId_);
222 }
223 
NeedHideCursor()224 inline bool Context::NeedHideCursor() const
225 {
226     return (flag_ & COOPERATE_FLAG_HIDE_CURSOR);
227 }
228 
IsCooperateWithCrossDrag()229 inline bool Context::IsCooperateWithCrossDrag() const
230 {
231     return (priv_ & COOPERATE_WITH_CROSS_DRAGGING);
232 }
233 
NeedFreezeCursor()234 inline bool Context::NeedFreezeCursor() const
235 {
236     return (flag_ & COOPERATE_FLAG_FREEZE_CURSOR);
237 }
238 } // namespace Cooperate
239 } // namespace DeviceStatus
240 } // namespace Msdp
241 } // namespace OHOS
242 #endif // COOPERATE_CONTEXT_H
243