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