1 /* 2 * Copyright (c) 2021-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 DISTRIBUTED_INPUT_SINK_MANAGER_SERVICE_H 17 #define DISTRIBUTED_INPUT_SINK_MANAGER_SERVICE_H 18 19 #include <cstring> 20 #include <set> 21 #include <map> 22 #include <mutex> 23 #include <sys/types.h> 24 #include <unistd.h> 25 #include <vector> 26 27 #include "event_handler.h" 28 #include "ipublisher_listener.h" 29 #include "publisher_listener_stub.h" 30 #include "nlohmann/json.hpp" 31 #include "screen.h" 32 #include "singleton.h" 33 #include "system_ability.h" 34 #include "system_ability_status_change_stub.h" 35 36 #include "constants_dinput.h" 37 #include "dinput_sink_manager_callback.h" 38 #include "dinput_sink_trans_callback.h" 39 #include "distributed_input_sink_stub.h" 40 #include "distributed_input_sink_event_handler.h" 41 #include "dinput_sink_state.h" 42 43 namespace OHOS { 44 namespace DistributedHardware { 45 namespace DistributedInput { 46 enum class ServiceSinkRunningState { STATE_NOT_START, STATE_RUNNING }; 47 const std::string EVENT_HANDLER_TASKNAME_START_TYPE = "start_type_handle_task"; 48 const std::string EVENT_HANDLER_TASKNAME_START_DHID = "start_dhid_handle_task"; 49 50 class DistributedInputSinkManager : public SystemAbility, public DistributedInputSinkStub { 51 DECLARE_SYSTEM_ABILITY(DistributedInputSinkManager) 52 53 public: 54 DistributedInputSinkManager(int32_t saId, bool runOnCreate); 55 ~DistributedInputSinkManager() override; 56 57 class DInputSinkMgrListener : public DInputSinkManagerCallback { 58 public: 59 explicit DInputSinkMgrListener(DistributedInputSinkManager *manager); 60 virtual ~DInputSinkMgrListener(); 61 void ResetSinkMgrResStatus() override; 62 private: 63 DistributedInputSinkManager *sinkManagerObj_; 64 }; 65 66 class DInputSinkListener : public DInputSinkTransCallback { 67 public: 68 explicit DInputSinkListener(DistributedInputSinkManager *manager); 69 ~DInputSinkListener() override; 70 void OnPrepareRemoteInput(const int32_t &sessionId, const std::string &deviceId) override; 71 void OnUnprepareRemoteInput(const int32_t &sessionId) override; 72 void OnStartRemoteInput(const int32_t &sessionId, const uint32_t &inputTypes) override; 73 void OnStopRemoteInput(const int32_t &sessionId, const uint32_t &inputTypes) override; 74 void OnStartRemoteInputDhid(const int32_t &sessionId, const std::string &strDhids) override; 75 void OnStopRemoteInputDhid(const int32_t &sessionId, const std::string &strDhids) override; 76 77 void OnRelayPrepareRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, 78 const std::string &deviceId) override; 79 void OnRelayUnprepareRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, 80 const std::string &deviceId) override; 81 void OnRelayStartDhidRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, 82 const std::string &deviceId, const std::string &strDhids) override; 83 void OnRelayStopDhidRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, 84 const std::string &deviceId, const std::string &strDhids) override; 85 void OnRelayStartTypeRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, 86 const std::string &deviceId, uint32_t inputTypes) override; 87 void OnRelayStopTypeRemoteInput(const int32_t &toSrcSessionId, const int32_t &toSinkSessionId, 88 const std::string &deviceId, uint32_t inputTypes) override; 89 90 private: 91 DistributedInputSinkManager *sinkManagerObj_; 92 }; 93 94 class ProjectWindowListener : public PublisherListenerStub { 95 public: 96 explicit ProjectWindowListener(DistributedInputSinkManager *manager); 97 ~ProjectWindowListener() override; 98 void OnMessage(const DHTopic topic, const std::string &message) override; 99 100 private: 101 int32_t ParseMessage(const std::string &message, std::string &srcDeviceId, uint64_t &srcWinId, 102 SinkScreenInfo &sinkScreenInfo); 103 int32_t UpdateSinkScreenInfoCache(const std::string &srcDevId, const uint64_t srcWinId, 104 const SinkScreenInfo &sinkScreenInfoTmp); 105 uint32_t GetScreenWidth(); 106 uint32_t GetScreenHeight(); 107 108 private: 109 sptr<Rosen::Screen> screen_; 110 std::mutex handleScreenMutex_; 111 DistributedInputSinkManager *sinkManagerObj_; 112 }; 113 114 class PluginStartListener : public PublisherListenerStub { 115 public: 116 explicit PluginStartListener() = default; 117 ~PluginStartListener() override; 118 void OnMessage(const DHTopic topic, const std::string &message) override; 119 }; 120 121 class DScreenSinkSvrRecipient : public IRemoteObject::DeathRecipient { 122 public: 123 DScreenSinkSvrRecipient(const std::string &srcDevId, const uint64_t srcWinId); 124 ~DScreenSinkSvrRecipient() override; 125 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 126 127 private: 128 std::string srcDevId_; 129 uint64_t srcWinId_; 130 }; 131 132 public: 133 void OnStart() override; 134 135 void OnStop() override; 136 137 int32_t Init() override; 138 139 int32_t Release() override; 140 141 int32_t RegisterGetSinkScreenInfosCallback(sptr<IGetSinkScreenInfosCallback> callback) override; 142 143 uint32_t GetSinkScreenInfosCbackSize(); 144 145 DInputServerType GetStartTransFlag(); 146 147 void SetStartTransFlag(const DInputServerType flag); 148 149 uint32_t GetInputTypes(); 150 151 void SetInputTypes(const uint32_t &inputTypes); 152 153 /* 154 * GetEventHandler, get the ui_service manager service's handler. 155 * 156 * @return Returns EventHandler ptr. 157 */ 158 std::shared_ptr<DistributedInputSinkEventHandler> GetEventHandler(); 159 160 int32_t NotifyStartDScreen(const SrcScreenInfo &srcScreenInfo) override; 161 162 int32_t NotifyStopDScreen(const std::string &srcScreenInfoKey) override; 163 164 int32_t RegisterSharingDhIdListener(sptr<ISharingDhIdListener> sharingDhIdListener) override; 165 166 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 167 168 void QueryLocalWhiteList(nlohmann::json &jsonStr); 169 170 void ClearResourcesStatus(); 171 172 private: 173 void CleanExceptionalInfo(const SrcScreenInfo &srcScreenInfo); 174 void CallBackScreenInfoChange(); 175 176 private: 177 ServiceSinkRunningState serviceRunningState_ = ServiceSinkRunningState::STATE_NOT_START; 178 DInputServerType isStartTrans_ = DInputServerType::NULL_SERVER_TYPE; 179 std::shared_ptr<DistributedInputSinkManager::DInputSinkListener> statuslistener_; 180 std::shared_ptr<DistributedInputSinkManager::DInputSinkMgrListener> sinkMgrListener_; 181 std::set<sptr<IGetSinkScreenInfosCallback>> getSinkScreenInfosCallbacks_; 182 183 std::shared_ptr<AppExecFwk::EventRunner> runner_; 184 std::shared_ptr<DistributedInputSinkEventHandler> handler_; 185 std::mutex mutex_; 186 bool InitAuto(); 187 DInputDeviceType inputTypes_; 188 sptr<ProjectWindowListener> projectWindowListener_ = nullptr; 189 sptr<PluginStartListener> pluginStartListener_ = nullptr; 190 std::set<std::string> sharingDhIds_; 191 std::map<int32_t, std::set<std::string>> sharingDhIdsMap_; 192 void StoreStartDhids(int32_t sessionId, const std::vector<std::string> &dhIds); 193 194 /* 195 * Stop dhids on cmd, 196 * stoDhIds: dhIds on cmd 197 * stopIndeedDhIds: dhId that need stop capture event. 198 */ 199 void DeleteStopDhids(int32_t sessionId, const std::vector<std::string> stopDhIds, 200 std::vector<std::string> &stopIndeedDhIds); 201 }; 202 } // namespace DistributedInput 203 } // namespace DistributedHardware 204 } // namespace OHOS 205 206 #endif // DISTRIBUTED_INPUT_SINK_MANAGER_SERVICE_H 207