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 40namespace OHOS { 41namespace Msdp { 42namespace DeviceStatus { 43namespace Cooperate { 44class Context final { 45public: 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 DSoftbusHandler dsoftbus_; 95 EventManager eventMgr_; 96 HotArea hotArea_; 97 MouseLocation mouseLocation_; 98 InputDeviceMgr inputDevMgr_; 99 InputEventBuilder inputEventBuilder_; 100 InputEventInterceptor inputEventInterceptor_; 101 CommonEventAdapter commonEvent_; 102 103private: 104 int32_t StartEventHandler(); 105 void StopEventHandler(); 106 // The DDM is abbreviation for 'Distributed Device Manager'. 107 void 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 132class HotplugObserver final : public IDeviceObserver { 133public: 134 explicit HotplugObserver(Channel<CooperateEvent>::Sender sender) : sender_(sender) {} 135 ~HotplugObserver() = default; 136 137 void OnDeviceAdded(std::shared_ptr<IDevice> dev) override; 138 void OnDeviceRemoved(std::shared_ptr<IDevice> dev) override; 139 140private: 141 Channel<CooperateEvent>::Sender sender_; 142}; 143 144inline Channel<CooperateEvent>::Sender Context::Sender() const 145{ 146 return sender_; 147} 148 149inline std::shared_ptr<AppExecFwk::EventHandler> Context::EventHandler() const 150{ 151 return eventHandler_; 152} 153 154inline std::string Context::Local() const 155{ 156 return DSoftbusHandler::GetLocalNetworkId(); 157} 158 159inline std::string Context::Peer() const 160{ 161 return remoteNetworkId_; 162} 163 164inline int32_t Context::StartDeviceId() const 165{ 166 return startDeviceId_; 167} 168 169inline Coordinate Context::CursorPosition() const 170{ 171 return cursorPos_; 172} 173 174inline uint32_t Context::CooperateFlag() const 175{ 176 return flag_; 177} 178 179inline bool Context::IsLocal(const std::string &networkId) const 180{ 181 return (networkId == DSoftbusHandler::GetLocalNetworkId()); 182} 183 184inline bool Context::IsPeer(const std::string &networkId) const 185{ 186 return (networkId == remoteNetworkId_); 187} 188 189inline bool Context::NeedHideCursor() const 190{ 191 return (flag_ & COOPERATE_FLAG_HIDE_CURSOR); 192} 193} // namespace Cooperate 194} // namespace DeviceStatus 195} // namespace Msdp 196} // namespace OHOS 197#endif // COOPERATE_CONTEXT_H 198