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 Cooperate {
44  class Context final {
45  public:
46      Context(IContext *env);
47      ~Context() = default;
48      DISALLOW_COPY_AND_MOVE(Context);
49  
50      void AttachSender(Channel<CooperateEvent>::Sender sender);
51      void AddObserver(std::shared_ptr<ICooperateObserver> observer);
52      void RemoveObserver(std::shared_ptr<ICooperateObserver> observer);
53      void Enable();
54      void Disable();
55  
56      Channel<CooperateEvent>::Sender Sender() const;
57      std::shared_ptr<AppExecFwk::EventHandler> EventHandler() const;
58      std::string Local() const;
59      std::string Peer() const;
60      int32_t StartDeviceId() const;
61      Coordinate CursorPosition() const;
62      NormalizedCoordinate NormalizedCursorPosition() const;
63      uint32_t CooperateFlag() const;
64  
65      bool IsLocal(const std::string &networkId) const;
66      bool IsPeer(const std::string &networkId) const;
67      bool NeedHideCursor() const;
68  
69      void EnableCooperate(const EnableCooperateEvent &event);
70      void DisableCooperate(const DisableCooperateEvent &event);
71      void StartCooperate(const StartCooperateEvent &event);
72      void RemoteStartSuccess(const DSoftbusStartCooperateFinished &event);
73      void RelayCooperate(const DSoftbusRelayCooperate &event);
74      void OnPointerEvent(const InputPointerEvent &event);
75      void UpdateCooperateFlag(const UpdateCooperateFlagEvent &event);
76      void UpdateCursorPosition();
77      void ResetCursorPosition();
78  
79      bool IsAllowCooperate();
80      void OnStartCooperate(StartCooperateData &data);
81      void OnRemoteStartCooperate(RemoteStartCooperateData &data);
82      void OnTransitionOut();
83      void OnTransitionIn();
84      void OnBack();
85      void OnRelayCooperation(const std::string &networkId, const NormalizedCoordinate &cursorPos);
86      void OnResetCooperation();
87      void CloseDistributedFileConnection(const std::string &remoteNetworkId);
88  
89  #ifdef ENABLE_PERFORMANCE_CHECK
90      void StartTrace(const std::string &name);
91      void FinishTrace(const std::string &name);
92  #endif // ENABLE_PERFORMANCE_CHECK
93  
94      DDMAdapter ddm_;
95      DSoftbusHandler dsoftbus_;
96      EventManager eventMgr_;
97      HotArea hotArea_;
98      MouseLocation mouseLocation_;
99      InputDeviceMgr inputDevMgr_;
100      InputEventBuilder inputEventBuilder_;
101      InputEventInterceptor inputEventInterceptor_;
102      CommonEventAdapter commonEvent_;
103  
104  private:
105      int32_t StartEventHandler();
106      void StopEventHandler();
107      int32_t EnableDDM();
108      void DisableDDM();
109      int32_t EnableDevMgr();
110      void DisableDevMgr();
111      int32_t EnableInputDevMgr();
112      void DisableInputDevMgr();
113      void SetCursorPosition(const Coordinate &cursorPos);
114  
115      IContext *env_ { nullptr };
116      Channel<CooperateEvent>::Sender sender_;
117      std::string remoteNetworkId_;
118      int32_t startDeviceId_ { -1 };
119      uint32_t flag_ {};
120      Coordinate cursorPos_ {};
121      std::shared_ptr<AppExecFwk::EventHandler> eventHandler_;
122      std::shared_ptr<IBoardObserver> boardObserver_;
123      std::shared_ptr<IDeviceObserver> hotplugObserver_;
124      std::set<std::shared_ptr<ICooperateObserver>> observers_;
125  
126  #ifdef ENABLE_PERFORMANCE_CHECK
127      std::mutex lock_;
128      std::map<std::string, std::chrono::time_point<std::chrono::steady_clock>> traces_;
129  #endif // ENABLE_PERFORMANCE_CHECK
130  };
131  
Sender()132  inline Channel<CooperateEvent>::Sender Context::Sender() const
133  {
134      return sender_;
135  }
136  
EventHandler()137  inline std::shared_ptr<AppExecFwk::EventHandler> Context::EventHandler() const
138  {
139      return eventHandler_;
140  }
141  
Local()142  inline std::string Context::Local() const
143  {
144      return DSoftbusHandler::GetLocalNetworkId();
145  }
146  
Peer()147  inline std::string Context::Peer() const
148  {
149      return remoteNetworkId_;
150  }
151  
StartDeviceId()152  inline int32_t Context::StartDeviceId() const
153  {
154      return startDeviceId_;
155  }
156  
CursorPosition()157  inline Coordinate Context::CursorPosition() const
158  {
159      return cursorPos_;
160  }
161  
CooperateFlag()162  inline uint32_t Context::CooperateFlag() const
163  {
164      return flag_;
165  }
166  
IsLocal(const std::string & networkId)167  inline bool Context::IsLocal(const std::string &networkId) const
168  {
169      return (networkId == DSoftbusHandler::GetLocalNetworkId());
170  }
171  
IsPeer(const std::string & networkId)172  inline bool Context::IsPeer(const std::string &networkId) const
173  {
174      return (networkId == remoteNetworkId_);
175  }
176  
NeedHideCursor()177  inline bool Context::NeedHideCursor() const
178  {
179      return (flag_ & COOPERATE_FLAG_HIDE_CURSOR);
180  }
181  } // namespace Cooperate
182  } // namespace DeviceStatus
183  } // namespace Msdp
184  } // namespace OHOS
185  #endif // COOPERATE_CONTEXT_H
186