1 /* 2 * Copyright (c) 2022-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 COORDINATION_SM_H 17 #define COORDINATION_SM_H 18 19 #include <functional> 20 21 #include "singleton.h" 22 23 #include "devicestatus_define.h" 24 #include "device_manager_callback.h" 25 #include "distributed_input_adapter.h" 26 #include "dm_device_info.h" 27 #include "input_manager.h" 28 #include "i_coordination_state.h" 29 #include "i_input_event_consumer.h" 30 #include "i_input_event_filter.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 static constexpr int32_t SUBSTR_NETWORKID_LEN = 6; 36 enum class CoordinationState { 37 STATE_FREE = 0, 38 STATE_IN = 1, 39 STATE_OUT = 2 40 }; 41 42 enum class CoordinationMsg { 43 COORDINATION_ON_SUCCESS = 0, 44 COORDINATION_ON_FAIL = 1, 45 COORDINATION_OFF_SUCCESS = 2, 46 COORDINATION_OFF_FAIL = 3, 47 COORDINATION_START = 4, 48 COORDINATION_START_SUCCESS = 5, 49 COORDINATION_START_FAIL = 6, 50 COORDINATION_STOP = 7, 51 COORDINATION_STOP_SUCCESS = 8, 52 COORDINATION_STOP_FAIL = 9, 53 COORDINATION_NULL = 10 54 }; 55 56 enum class CooStateChangeType { 57 STATE_NONE = -1, 58 STATE_FREE_TO_IN = 0, 59 STATE_FREE_TO_OUT = 1, 60 STATE_IN_TO_FREE = 2, 61 STATE_OUT_TO_FREE = 3 62 }; 63 64 struct PointerFilter : public MMI::IInputEventFilter { OnInputEventPointerFilter65 bool OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override 66 { 67 return false; 68 } UpdateCurrentFilterIdPointerFilter69 inline void UpdateCurrentFilterId(int32_t filterId) 70 { 71 filterId_ = filterId; 72 } 73 bool OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override; 74 private: 75 mutable int32_t filterId_ { -1 }; 76 }; 77 78 class CoordinationSM final { 79 DECLARE_DELAYED_SINGLETON(CoordinationSM); 80 81 class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 82 void OnRemoteDied() override; 83 }; 84 85 class DmDeviceStateCallback : public DistributedHardware::DeviceStateCallback { 86 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 87 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 88 void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 89 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 90 }; 91 92 class InterceptorConsumer : public MMI::IInputEventConsumer { 93 public: 94 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override; 95 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override; 96 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override; 97 }; 98 99 class MonitorConsumer : public MMI::IInputEventConsumer { 100 public: MonitorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> cb)101 explicit MonitorConsumer(std::function<void (std::shared_ptr<MMI::PointerEvent>)> cb) : callback_(cb) {} 102 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override; 103 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override; 104 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override; 105 private: 106 std::function<void (std::shared_ptr<MMI::PointerEvent>)> callback_ { nullptr }; 107 }; 108 109 public: 110 void SetAbsolutionLocation(double xPercent, double yPercent); 111 DISALLOW_COPY_AND_MOVE(CoordinationSM); 112 void Init(); 113 void OnSessionLost(SessionPtr session); 114 void PrepareCoordination(); 115 void UnprepareCoordination(); 116 int32_t ActivateCoordination(const std::string &remoteNetworkId, int32_t startDeviceId); 117 int32_t DeactivateCoordination(bool isUnchained); 118 int32_t GetCoordinationState(const std::string &deviceId); 119 void StartRemoteCoordination(const std::string &remoteNetworkId, bool buttonIsPressed); 120 void StartPointerEventFilter(); 121 void StartRemoteCoordinationResult(bool isSuccess, 122 const std::string &startDeviceDhid, int32_t xPercent, int32_t yPercent); 123 void StopRemoteCoordination(bool isUnchained); 124 void StopRemoteCoordinationResult(bool isSuccess); 125 void StartCoordinationOtherResult(const std::string &remoteNetworkId); 126 void UpdateState(CoordinationState state); 127 void UpdatePreparedDevices(const std::string &remoteNetworkId, const std::string &originNetworkId); 128 std::pair<std::string, std::string> GetPreparedDevices() const; 129 CoordinationState GetCurrentCoordinationState() const; 130 void OnCoordinationChanged(const std::string &networkId, bool isOpen); 131 void OnKeyboardOnline(const std::string &dhid); 132 void OnPointerOffline(const std::string &dhid, const std::vector<std::string> &keyboards); 133 void OnKeyboardOffline(const std::string &dhid); 134 bool InitDeviceManager(); 135 void OnDeviceOnline(const std::string &networkId); 136 void OnDeviceOffline(const std::string &networkId); 137 void OnStartFinish(bool isSuccess, const std::string &remoteNetworkId, int32_t startDeviceId); 138 void OnStopFinish(bool isSuccess, const std::string &remoteNetworkId); 139 bool IsStarting() const; 140 bool IsStopping() const; 141 void Reset(const std::string &networkId); 142 void Dump(int32_t fd); 143 std::string GetDeviceCoordinationState(CoordinationState value) const; 144 void UpdateLastPointerEventCallback(std::shared_ptr<MMI::PointerEvent> pointerEvent); 145 std::shared_ptr<MMI::PointerEvent> GetLastPointerEvent() const; 146 void RemoveMonitor(); 147 void RemoveInterceptor(); 148 bool IsNeedFilterOut(const std::string &deviceId, const std::shared_ptr<MMI::KeyEvent> keyEvent); 149 void RegisterStateChange(CooStateChangeType type, 150 std::function<void(CoordinationState, CoordinationState)> callback); 151 bool UnchainCoordination(const std::string &localNetworkId, const std::string &remoteNetworkId); 152 void SetUnchainStatus(bool isUnchained); 153 void NotifyChainRemoved(); 154 void NotifyUnchainedResult(const std::string &remoteNetworkId, bool isSuccess); 155 void SetSinkNetworkId(const std::string &sinkNetworkId); 156 void RegisterRemoteNetworkId(std::function<void(std::string)> callback); 157 void RegisterMouseLocation(std::function<void(int32_t, int32_t)> callback); 158 void RegisterNotifyDragCancel(std::function<void(void)> callback); 159 void OnInterceptorInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent); 160 void OnInterceptorInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 161 void OnMonitorInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 162 void OnSoftbusSessionClosed(const std::string &NetworkId); 163 164 private: 165 void Reset(bool adjustAbsolutionLocation = false); 166 void OnCloseCoordination(const std::string &networkId, bool isLocal); 167 void NotifyRemoteStartFail(const std::string &remoteNetworkId); 168 void NotifyRemoteStartSuccess(const std::string &remoteNetworkId, const std::string &startDeviceDhid); 169 void NotifyRemoteStopFinish(bool isSuccess, const std::string &remoteNetworkId); 170 bool UpdateMouseLocation(); 171 void StateChangedNotify(CoordinationState oldState, CoordinationState newState); 172 void ChangeNotify(CooStateChangeType type, CoordinationState oldState, CoordinationState newState); 173 void NotifyRemoteNetworkId(const std::string &remoteNetworkId); 174 void NotifyMouseLocation(int32_t x, int32_t y); 175 void SetPointerVisible(); 176 void OnPostInterceptorKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent); 177 void OnPostInterceptorPointerEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 178 void OnPostMonitorInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent); 179 void OnReset(const std::string &NetworkId); 180 std::shared_ptr<ICoordinationState> GetCurrentState(); 181 182 private: 183 std::pair<std::string, std::string> preparedNetworkId_; 184 std::string startDeviceDhid_; 185 std::string remoteNetworkId_; 186 std::string sinkNetworkId_; 187 bool isUnchained_ { false }; 188 CoordinationState currentState_ { CoordinationState::STATE_FREE }; 189 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_ { nullptr }; 190 std::shared_ptr<DistributedHardware::DeviceStateCallback> stateCallback_ { nullptr }; 191 std::vector<std::string> onlineDevice_; 192 mutable std::mutex mutex_; 193 std::atomic<bool> isStarting_ { false }; 194 std::atomic<bool> isStopping_ { false }; 195 std::pair<int32_t, int32_t> mouseLocation_ { std::make_pair(0, 0) }; 196 std::shared_ptr<MMI::PointerEvent> lastPointerEvent_ { nullptr }; 197 int32_t displayX_ { -1 }; 198 int32_t displayY_ { -1 }; 199 int32_t interceptorId_ { -1 }; 200 int32_t monitorId_ { -1 }; 201 int32_t filterId_ { -1 }; 202 std::map<CooStateChangeType, std::function<void(CoordinationState, CoordinationState)>> stateChangedCallbacks_; 203 std::function<void(std::string)> remoteNetworkIdCallback_; 204 std::function<void(int32_t, int32_t)> mouseLocationCallback_; 205 std::function<void(void)> notifyDragCancelCallback_; 206 std::map<CoordinationState, std::shared_ptr<ICoordinationState>> coordinationStates_; 207 std::shared_ptr<AppExecFwk::EventRunner> runner_ { nullptr }; 208 std::shared_ptr<CoordinationEventHandler> eventHandler_ { nullptr }; 209 }; 210 211 #define DIS_HARDWARE DistributedHardware::DeviceManager::GetInstance() 212 #define COOR_SM OHOS::DelayedSingleton<CoordinationSM>::GetInstance() 213 } // namespace DeviceStatus 214 } // namespace Msdp 215 } // namespace OHOS 216 #endif // COORDINATION_SM_H 217