1 /* 2 * Copyright (c) 2022 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 INPUT_DEVICE_COOPERATE_SM_H 17 #define INPUT_DEVICE_COOPERATE_SM_H 18 19 #include "bytrace_adapter.h" 20 #include "singleton.h" 21 22 #include "device_manager_callback.h" 23 #include "distributed_input_adapter.h" 24 #include "dm_device_info.h" 25 #include "i_input_device_cooperate_state.h" 26 #include "input_event_handler.h" 27 28 struct libinput_event; 29 namespace OHOS { 30 namespace MMI { 31 enum class CooperateState { 32 STATE_FREE = 0, 33 STATE_IN = 1, 34 STATE_OUT = 2, 35 }; 36 37 enum class CooperateMsg { 38 COOPERATE_ON_SUCESS = 0, 39 COOPERATE_ON_FAIL = 1, 40 COOPERATE_OFF_SUCESS = 2, 41 COOPERATE_OFF_FAIL = 3, 42 COOPERATE_START = 4, 43 COOPERATE_START_SUCESS = 5, 44 COOPERATE_START_FAIL = 6, 45 COOPERATE_STOP = 7, 46 COOPERATE_STOP_SUCESS = 8, 47 COOPERATE_STOP_FIAL = 9, 48 COOPERATE_NULL = 10, 49 }; 50 51 class InputDeviceCooperateSM final { 52 DECLARE_DELAYED_SINGLETON(InputDeviceCooperateSM); 53 class DeviceInitCallBack : public DistributedHardware::DmInitCallback { 54 void OnRemoteDied() override; 55 }; 56 57 class MmiDeviceStateCallback : public DistributedHardware::DeviceStateCallback { 58 void OnDeviceOnline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 59 void OnDeviceChanged(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 60 void OnDeviceReady(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 61 void OnDeviceOffline(const DistributedHardware::DmDeviceInfo &deviceInfo) override; 62 }; 63 public: 64 using DelegateTasksCallback = std::function<int32_t(std::function<int32_t()>)>; 65 DISALLOW_COPY_AND_MOVE(InputDeviceCooperateSM); 66 void Init(DelegateTasksCallback delegateTasksCallback); 67 void EnableInputDeviceCooperate(bool enabled); 68 int32_t StartInputDeviceCooperate(const std::string &remoteNetworkId, int32_t startInputDeviceId); 69 int32_t StopInputDeviceCooperate(); 70 void GetCooperateState(const std::string &deviceId); 71 void SetVirtualKeyBoardDevId(int32_t deviceId); 72 int32_t GetVirtualKeyBoardDevId(); 73 void StartRemoteCooperate(const std::string &remoteNetworkId); 74 void StartRemoteCooperateResult(bool isSuccess, const std::string &startDhid, int32_t xPercent, int32_t yPercent); 75 void StopRemoteCooperate(); 76 void StopRemoteCooperateResult(bool isSuccess); 77 void StartCooperateOtherResult(const std::string &srcNetworkId); 78 void HandleEvent(struct libinput_event *event); 79 void UpdateState(CooperateState state); 80 void UpdatePreparedDevices(const std::string &srcNetworkId, const std::string &sinkNetworkId); 81 std::pair<std::string, std::string> GetPreparedDevices() const; 82 CooperateState GetCurrentCooperateState() const; 83 void OnCooperateChanged(const std::string &networkId, bool isOpen); 84 void OnKeyboardOnline(const std::string &dhid); 85 void OnPointerOffline(const std::string &dhid, const std::string &sinkNetworkId, 86 const std::vector<std::string> &keyboards); 87 bool InitDeviceManager(); 88 void OnDeviceOnline(const std::string &networkId); 89 void OnDeviceOffline(const std::string &networkId); 90 void OnStartFinish(bool isSuccess, const std::string &remoteNetworkId, int32_t startInputDeviceId); 91 void OnStopFinish(bool isSuccess, const std::string &remoteNetworkId); 92 bool IsStarting() const; 93 bool IsStopping() const; 94 void Reset(const std::string &networkId); 95 void Dump(int32_t fd, const std::vector<std::string> &args); 96 97 private: 98 void Reset(bool adjustAbsolutionLocation = false); 99 void CheckPointerEvent(struct libinput_event *event); 100 void OnCloseCooperation(const std::string &networkId, bool isLocal); 101 void NotifyRemoteStartFail(const std::string &remoteNetworkId); 102 void NotifyRemoteStartSuccess(const std::string &remoteNetworkId, const std::string &startDhid); 103 void NotifyRemoteStopFinish(bool isSuccess, const std::string &remoteNetworkId); 104 bool UpdateMouseLocation(); 105 std::shared_ptr<IInputDeviceCooperateState> currentStateSM_ { nullptr }; 106 std::pair<std::string, std::string> preparedNetworkId_; 107 std::string startDhid_ ; 108 std::string srcNetworkId_; 109 int32_t virtualKeyBoardId_ { -1 }; 110 CooperateState cooperateState_ { CooperateState::STATE_FREE }; 111 std::shared_ptr<DistributedHardware::DmInitCallback> initCallback_ { nullptr }; 112 std::shared_ptr<DistributedHardware::DeviceStateCallback> stateCallback_ { nullptr }; 113 std::vector<std::string> onlineDevice_; 114 mutable std::mutex mutex_; 115 std::atomic<bool> isStarting_ { false }; 116 std::atomic<bool> isStopping_ { false }; 117 std::pair<int32_t, int32_t> mouseLocation_ { std::make_pair(0, 0) }; 118 DelegateTasksCallback delegateTasksCallback_ { nullptr }; 119 }; 120 121 #define DisHardware DistributedHardware::DeviceManager::GetInstance() 122 #define InputDevCooSM ::OHOS::DelayedSingleton<InputDeviceCooperateSM>::GetInstance() 123 } // namespace MMI 124 } // namespace OHOS 125 #endif // INPUT_DEVICE_COOPERATE_SM_H 126