1f857971dSopenharmony_ci/* 2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f857971dSopenharmony_ci * you may not use this file except in compliance with the License. 5f857971dSopenharmony_ci * You may obtain a copy of the License at 6f857971dSopenharmony_ci * 7f857971dSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f857971dSopenharmony_ci * 9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f857971dSopenharmony_ci * See the License for the specific language governing permissions and 13f857971dSopenharmony_ci * limitations under the License. 14f857971dSopenharmony_ci */ 15f857971dSopenharmony_ci 16f857971dSopenharmony_ci#ifndef COOPERATE_CONTEXT_H 17f857971dSopenharmony_ci#define COOPERATE_CONTEXT_H 18f857971dSopenharmony_ci 19f857971dSopenharmony_ci#include "event_handler.h" 20f857971dSopenharmony_ci#include "nocopyable.h" 21f857971dSopenharmony_ci 22f857971dSopenharmony_ci#ifdef ENABLE_PERFORMANCE_CHECK 23f857971dSopenharmony_ci#include <chrono> 24f857971dSopenharmony_ci#include <mutex> 25f857971dSopenharmony_ci#endif // ENABLE_PERFORMANCE_CHECK 26f857971dSopenharmony_ci 27f857971dSopenharmony_ci#include "common_event_adapter.h" 28f857971dSopenharmony_ci#include "common_event_observer.h" 29f857971dSopenharmony_ci#include "cooperate_events.h" 30f857971dSopenharmony_ci#include "ddm_adapter.h" 31f857971dSopenharmony_ci#include "dsoftbus_handler.h" 32f857971dSopenharmony_ci#include "event_manager.h" 33f857971dSopenharmony_ci#include "hot_area.h" 34f857971dSopenharmony_ci#include "input_device_mgr.h" 35f857971dSopenharmony_ci#include "input_event_transmission/input_event_builder.h" 36f857971dSopenharmony_ci#include "input_event_transmission/input_event_interceptor.h" 37f857971dSopenharmony_ci#include "i_context.h" 38f857971dSopenharmony_ci#include "mouse_location.h" 39f857971dSopenharmony_ci 40f857971dSopenharmony_cinamespace OHOS { 41f857971dSopenharmony_cinamespace Msdp { 42f857971dSopenharmony_cinamespace DeviceStatus { 43f857971dSopenharmony_cinamespace Cooperate { 44f857971dSopenharmony_ciclass Context final { 45f857971dSopenharmony_cipublic: 46f857971dSopenharmony_ci Context(IContext *env); 47f857971dSopenharmony_ci ~Context() = default; 48f857971dSopenharmony_ci DISALLOW_COPY_AND_MOVE(Context); 49f857971dSopenharmony_ci 50f857971dSopenharmony_ci void AttachSender(Channel<CooperateEvent>::Sender sender); 51f857971dSopenharmony_ci void AddObserver(std::shared_ptr<ICooperateObserver> observer); 52f857971dSopenharmony_ci void RemoveObserver(std::shared_ptr<ICooperateObserver> observer); 53f857971dSopenharmony_ci void Enable(); 54f857971dSopenharmony_ci void Disable(); 55f857971dSopenharmony_ci 56f857971dSopenharmony_ci Channel<CooperateEvent>::Sender Sender() const; 57f857971dSopenharmony_ci std::shared_ptr<AppExecFwk::EventHandler> EventHandler() const; 58f857971dSopenharmony_ci std::string Local() const; 59f857971dSopenharmony_ci std::string Peer() const; 60f857971dSopenharmony_ci int32_t StartDeviceId() const; 61f857971dSopenharmony_ci Coordinate CursorPosition() const; 62f857971dSopenharmony_ci NormalizedCoordinate NormalizedCursorPosition() const; 63f857971dSopenharmony_ci uint32_t CooperateFlag() const; 64f857971dSopenharmony_ci 65f857971dSopenharmony_ci bool IsLocal(const std::string &networkId) const; 66f857971dSopenharmony_ci bool IsPeer(const std::string &networkId) const; 67f857971dSopenharmony_ci bool NeedHideCursor() const; 68f857971dSopenharmony_ci 69f857971dSopenharmony_ci void EnableCooperate(const EnableCooperateEvent &event); 70f857971dSopenharmony_ci void DisableCooperate(const DisableCooperateEvent &event); 71f857971dSopenharmony_ci void StartCooperate(const StartCooperateEvent &event); 72f857971dSopenharmony_ci void RemoteStartSuccess(const DSoftbusStartCooperateFinished &event); 73f857971dSopenharmony_ci void RelayCooperate(const DSoftbusRelayCooperate &event); 74f857971dSopenharmony_ci void OnPointerEvent(const InputPointerEvent &event); 75f857971dSopenharmony_ci void UpdateCooperateFlag(const UpdateCooperateFlagEvent &event); 76f857971dSopenharmony_ci void UpdateCursorPosition(); 77f857971dSopenharmony_ci void ResetCursorPosition(); 78f857971dSopenharmony_ci 79f857971dSopenharmony_ci bool IsAllowCooperate(); 80f857971dSopenharmony_ci void OnStartCooperate(StartCooperateData &data); 81f857971dSopenharmony_ci void OnRemoteStartCooperate(RemoteStartCooperateData &data); 82f857971dSopenharmony_ci void OnTransitionOut(); 83f857971dSopenharmony_ci void OnTransitionIn(); 84f857971dSopenharmony_ci void OnBack(); 85f857971dSopenharmony_ci void OnRelayCooperation(const std::string &networkId, const NormalizedCoordinate &cursorPos); 86f857971dSopenharmony_ci void OnResetCooperation(); 87f857971dSopenharmony_ci void CloseDistributedFileConnection(const std::string &remoteNetworkId); 88f857971dSopenharmony_ci 89f857971dSopenharmony_ci#ifdef ENABLE_PERFORMANCE_CHECK 90f857971dSopenharmony_ci void StartTrace(const std::string &name); 91f857971dSopenharmony_ci void FinishTrace(const std::string &name); 92f857971dSopenharmony_ci#endif // ENABLE_PERFORMANCE_CHECK 93f857971dSopenharmony_ci 94f857971dSopenharmony_ci DSoftbusHandler dsoftbus_; 95f857971dSopenharmony_ci EventManager eventMgr_; 96f857971dSopenharmony_ci HotArea hotArea_; 97f857971dSopenharmony_ci MouseLocation mouseLocation_; 98f857971dSopenharmony_ci InputDeviceMgr inputDevMgr_; 99f857971dSopenharmony_ci InputEventBuilder inputEventBuilder_; 100f857971dSopenharmony_ci InputEventInterceptor inputEventInterceptor_; 101f857971dSopenharmony_ci CommonEventAdapter commonEvent_; 102f857971dSopenharmony_ci 103f857971dSopenharmony_ciprivate: 104f857971dSopenharmony_ci int32_t StartEventHandler(); 105f857971dSopenharmony_ci void StopEventHandler(); 106f857971dSopenharmony_ci // The DDM is abbreviation for 'Distributed Device Manager'. 107f857971dSopenharmony_ci void EnableDDM(); 108f857971dSopenharmony_ci void DisableDDM(); 109f857971dSopenharmony_ci int32_t EnableDevMgr(); 110f857971dSopenharmony_ci void DisableDevMgr(); 111f857971dSopenharmony_ci int32_t EnableInputDevMgr(); 112f857971dSopenharmony_ci void DisableInputDevMgr(); 113f857971dSopenharmony_ci void SetCursorPosition(const Coordinate &cursorPos); 114f857971dSopenharmony_ci 115f857971dSopenharmony_ci IContext *env_ { nullptr }; 116f857971dSopenharmony_ci Channel<CooperateEvent>::Sender sender_; 117f857971dSopenharmony_ci std::string remoteNetworkId_; 118f857971dSopenharmony_ci int32_t startDeviceId_ { -1 }; 119f857971dSopenharmony_ci uint32_t flag_ {}; 120f857971dSopenharmony_ci Coordinate cursorPos_ {}; 121f857971dSopenharmony_ci std::shared_ptr<AppExecFwk::EventHandler> eventHandler_; 122f857971dSopenharmony_ci std::shared_ptr<IBoardObserver> boardObserver_; 123f857971dSopenharmony_ci std::shared_ptr<IDeviceObserver> hotplugObserver_; 124f857971dSopenharmony_ci std::set<std::shared_ptr<ICooperateObserver>> observers_; 125f857971dSopenharmony_ci 126f857971dSopenharmony_ci#ifdef ENABLE_PERFORMANCE_CHECK 127f857971dSopenharmony_ci std::mutex lock_; 128f857971dSopenharmony_ci std::map<std::string, std::chrono::time_point<std::chrono::steady_clock>> traces_; 129f857971dSopenharmony_ci#endif // ENABLE_PERFORMANCE_CHECK 130f857971dSopenharmony_ci}; 131f857971dSopenharmony_ci 132f857971dSopenharmony_ciclass HotplugObserver final : public IDeviceObserver { 133f857971dSopenharmony_cipublic: 134f857971dSopenharmony_ci explicit HotplugObserver(Channel<CooperateEvent>::Sender sender) : sender_(sender) {} 135f857971dSopenharmony_ci ~HotplugObserver() = default; 136f857971dSopenharmony_ci 137f857971dSopenharmony_ci void OnDeviceAdded(std::shared_ptr<IDevice> dev) override; 138f857971dSopenharmony_ci void OnDeviceRemoved(std::shared_ptr<IDevice> dev) override; 139f857971dSopenharmony_ci 140f857971dSopenharmony_ciprivate: 141f857971dSopenharmony_ci Channel<CooperateEvent>::Sender sender_; 142f857971dSopenharmony_ci}; 143f857971dSopenharmony_ci 144f857971dSopenharmony_ciinline Channel<CooperateEvent>::Sender Context::Sender() const 145f857971dSopenharmony_ci{ 146f857971dSopenharmony_ci return sender_; 147f857971dSopenharmony_ci} 148f857971dSopenharmony_ci 149f857971dSopenharmony_ciinline std::shared_ptr<AppExecFwk::EventHandler> Context::EventHandler() const 150f857971dSopenharmony_ci{ 151f857971dSopenharmony_ci return eventHandler_; 152f857971dSopenharmony_ci} 153f857971dSopenharmony_ci 154f857971dSopenharmony_ciinline std::string Context::Local() const 155f857971dSopenharmony_ci{ 156f857971dSopenharmony_ci return DSoftbusHandler::GetLocalNetworkId(); 157f857971dSopenharmony_ci} 158f857971dSopenharmony_ci 159f857971dSopenharmony_ciinline std::string Context::Peer() const 160f857971dSopenharmony_ci{ 161f857971dSopenharmony_ci return remoteNetworkId_; 162f857971dSopenharmony_ci} 163f857971dSopenharmony_ci 164f857971dSopenharmony_ciinline int32_t Context::StartDeviceId() const 165f857971dSopenharmony_ci{ 166f857971dSopenharmony_ci return startDeviceId_; 167f857971dSopenharmony_ci} 168f857971dSopenharmony_ci 169f857971dSopenharmony_ciinline Coordinate Context::CursorPosition() const 170f857971dSopenharmony_ci{ 171f857971dSopenharmony_ci return cursorPos_; 172f857971dSopenharmony_ci} 173f857971dSopenharmony_ci 174f857971dSopenharmony_ciinline uint32_t Context::CooperateFlag() const 175f857971dSopenharmony_ci{ 176f857971dSopenharmony_ci return flag_; 177f857971dSopenharmony_ci} 178f857971dSopenharmony_ci 179f857971dSopenharmony_ciinline bool Context::IsLocal(const std::string &networkId) const 180f857971dSopenharmony_ci{ 181f857971dSopenharmony_ci return (networkId == DSoftbusHandler::GetLocalNetworkId()); 182f857971dSopenharmony_ci} 183f857971dSopenharmony_ci 184f857971dSopenharmony_ciinline bool Context::IsPeer(const std::string &networkId) const 185f857971dSopenharmony_ci{ 186f857971dSopenharmony_ci return (networkId == remoteNetworkId_); 187f857971dSopenharmony_ci} 188f857971dSopenharmony_ci 189f857971dSopenharmony_ciinline bool Context::NeedHideCursor() const 190f857971dSopenharmony_ci{ 191f857971dSopenharmony_ci return (flag_ & COOPERATE_FLAG_HIDE_CURSOR); 192f857971dSopenharmony_ci} 193f857971dSopenharmony_ci} // namespace Cooperate 194f857971dSopenharmony_ci} // namespace DeviceStatus 195f857971dSopenharmony_ci} // namespace Msdp 196f857971dSopenharmony_ci} // namespace OHOS 197f857971dSopenharmony_ci#endif // COOPERATE_CONTEXT_H 198