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