1 /*
2  * Copyright (c) 2021-2024 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_CLIENT_H
17 #define DISTRIBUTED_INPUT_CLIENT_H
18 
19 #include <atomic>
20 #include <iostream>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 
25 #include "event_handler.h"
26 
27 #include "add_white_list_infos_call_back_stub.h"
28 #include "del_white_list_infos_call_back_stub.h"
29 #include "get_sink_screen_infos_call_back_stub.h"
30 #include "i_distributed_source_input.h"
31 #include "i_distributed_sink_input.h"
32 #include "i_sharing_dhid_listener.h"
33 #include "register_d_input_call_back_stub.h"
34 #include "unregister_d_input_call_back_stub.h"
35 #include "sharing_dhid_listener_stub.h"
36 #include "start_stop_d_inputs_call_back_stub.h"
37 
38 #include "dinput_sa_manager.h"
39 #include "idistributed_hardware_source.h"
40 #include "idistributed_hardware_sink.h"
41 
42 namespace OHOS {
43 namespace DistributedHardware {
44 namespace DistributedInput {
45 class DistributedInputClient {
46 public:
47     DistributedInputClient();
~DistributedInputClient()48     ~DistributedInputClient(){};
49 
50     static DistributedInputClient &GetInstance();
51 
52     int32_t InitSource();
53 
54     int32_t ReleaseSource();
55 
56     int32_t InitSink();
57 
58     int32_t ReleaseSink();
59 
60     int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
61         const std::string &parameters, const std::shared_ptr<RegisterCallback> &callback);
62 
63     int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
64         const std::shared_ptr<UnregisterCallback> &callback);
65 
66     int32_t PrepareRemoteInput(const std::string &deviceId, sptr<IPrepareDInputCallback> callback);
67 
68     int32_t UnprepareRemoteInput(const std::string &deviceId, sptr<IUnprepareDInputCallback> callback);
69 
70     int32_t StartRemoteInput(
71         const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback);
72 
73     int32_t StopRemoteInput(
74         const std::string &deviceId, const uint32_t &inputTypes, sptr<IStopDInputCallback> callback);
75 
76     int32_t StartRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes,
77         sptr<IStartDInputCallback> callback);
78 
79     int32_t StopRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes,
80         sptr<IStopDInputCallback> callback);
81 
82     int32_t PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
83         sptr<IPrepareDInputCallback> callback);
84     int32_t UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
85         sptr<IUnprepareDInputCallback> callback);
86 
87     int32_t StartRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
88         sptr<IStartStopDInputsCallback> callback);
89     int32_t StopRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
90         sptr<IStartStopDInputsCallback> callback);
91 
92     int32_t StartRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector<std::string> &dhIds,
93         sptr<IStartStopDInputsCallback> callback);
94     int32_t StopRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector<std::string> &dhIds,
95         sptr<IStartStopDInputsCallback> callback);
96 
97     bool IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event);
98     bool IsTouchEventNeedFilterOut(const TouchScreenEvent &event);
99 
100     bool IsStartDistributedInput(const std::string &dhId);
101 
102     int32_t NotifyStartDScreen(const std::string &networkId, const std::string &srcDevId, const uint64_t srcWinId);
103     int32_t NotifyStopDScreen(const std::string &networkId, const std::string &srcScreenInfoKey);
104 
105     int32_t RegisterSimulationEventListener(sptr<ISimulationEventListener> listener);
106     int32_t UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener);
107 
108     int32_t RegisterSessionStateCb(sptr<ISessionStateCallback> callback);
109     int32_t UnregisterSessionStateCb();
110 
111     void CheckSourceRegisterCallback();
112     void CheckWhiteListCallback();
113     void CheckNodeMonitorCallback();
114     void CheckKeyStateCallback();
115 
116     void CheckSinkRegisterCallback();
117     void CheckSharingDhIdsCallback();
118     void CheckSinkScreenInfoCallback();
119 
120 public:
121     class RegisterDInputCb : public OHOS::DistributedHardware::DistributedInput::RegisterDInputCallbackStub {
122     public:
123         RegisterDInputCb() = default;
124         ~RegisterDInputCb() override = default;
125         void OnResult(const std::string &devId, const std::string &dhId, const int32_t &status) override;
126     };
127 
128     class UnregisterDInputCb : public OHOS::DistributedHardware::DistributedInput::UnregisterDInputCallbackStub {
129     public:
130         UnregisterDInputCb() = default;
131         ~UnregisterDInputCb() override = default;
132         void OnResult(const std::string &devId, const std::string &dhId, const int32_t &status) override;
133     };
134 
135     class AddWhiteListInfosCb : public OHOS::DistributedHardware::DistributedInput::AddWhiteListInfosCallbackStub {
136     public:
137         AddWhiteListInfosCb() = default;
138         ~AddWhiteListInfosCb() override = default;
139         void OnResult(const std::string &deviceId, const std::string &strJson) override;
140     };
141 
142     class DelWhiteListInfosCb : public OHOS::DistributedHardware::DistributedInput::DelWhiteListInfosCallbackStub {
143     public:
144         DelWhiteListInfosCb() = default;
145         ~DelWhiteListInfosCb() override = default;
146         void OnResult(const std::string &deviceId) override;
147     };
148 
149     class SharingDhIdListenerCb : public OHOS::DistributedHardware::DistributedInput::SharingDhIdListenerStub {
150     public:
151         SharingDhIdListenerCb() = default;
152         ~SharingDhIdListenerCb() override = default;
153         int32_t OnSharing(const std::string &dhId) override;
154         int32_t OnNoSharing(const std::string &dhId) override;
155     };
156 
157     class GetSinkScreenInfosCb : public OHOS::DistributedHardware::DistributedInput::GetSinkScreenInfosCallbackStub {
158     public:
159         GetSinkScreenInfosCb() = default;
160         ~GetSinkScreenInfosCb() override = default;
161         void OnResult(const std::string &strJson) override;
162     };
163 
164     class DInputClientEventHandler : public AppExecFwk::EventHandler {
165     public:
166         explicit DInputClientEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
167         ~DInputClientEventHandler() override = default;
168 
169         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
170     };
171 
172 private:
173     bool IsJsonData(std::string strData) const;
174     void AddWhiteListInfos(const std::string &deviceId, const std::string &strJson) const;
175     void DelWhiteListInfos(const std::string &deviceId) const;
176     void UpdateSinkScreenInfos(const std::string &strJson);
177     sptr<IDistributedSinkInput> GetRemoteDInput(const std::string &networkId) const;
178 
179 private:
180     static std::shared_ptr<DistributedInputClient> instance;
181 
182     DInputServerType serverType_ = DInputServerType::NULL_SERVER_TYPE;
183     DInputDeviceType inputTypes_ = DInputDeviceType::NONE;
184 
185     std::set<sptr<AddWhiteListInfosCb>> addWhiteListCallbacks_;
186     std::set<sptr<DelWhiteListInfosCb>> delWhiteListCallbacks_;
187     sptr<InputNodeListener> regNodeListener_ = nullptr;
188     sptr<InputNodeListener> unregNodeListener_ = nullptr;
189     sptr<ISimulationEventListener> regSimulationEventListener_ = nullptr;
190     sptr<ISimulationEventListener> unregSimulationEventListener_ = nullptr;
191     std::set<sptr<ISharingDhIdListener>> sharingDhIdListeners_;
192     std::set<sptr<GetSinkScreenInfosCb>> getSinkScreenInfosCallbacks_;
193 
194     std::shared_ptr<DistributedInputClient::DInputClientEventHandler> eventHandler_;
195 
196     std::atomic<bool> isAddWhiteListCbReg_;
197     std::atomic<bool> isDelWhiteListCbReg_;
198     std::atomic<bool> isNodeMonitorCbReg_;
199     std::atomic<bool> isSimulationEventCbReg_;
200     std::atomic<bool> isSharingDhIdsReg_;
201     std::atomic<bool> isGetSinkScreenInfosCbReg_;
202 
203     struct DHardWareFwkRegistInfo {
204         std::string devId;
205         std::string dhId;
206         std::shared_ptr<RegisterCallback> callback = nullptr;
207     };
208 
209     struct DHardWareFwkUnRegistInfo {
210         std::string devId;
211         std::string dhId;
212         std::shared_ptr<UnregisterCallback> callback = nullptr;
213     };
214 
215     std::vector<DHardWareFwkRegistInfo> dHardWareFwkRstInfos_;
216     std::vector<DHardWareFwkUnRegistInfo> dHardWareFwkUnRstInfos_;
217     std::vector<TransformInfo> screenTransInfos_;
218     std::mutex operationMutex_;
219 
220     std::mutex sharingDhIdsMtx_;
221     // sharing local dhids
222     std::set<std::string> sharingDhIds_;
223 };
224 } // namespace DistributedInput
225 } // namespace DistributedHardware
226 } // namespace OHOS
227 
228 #endif // DISTRIBUTED_INPUT_CLIENT_H
229