• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_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:
~DistributedInputClient()47     ~DistributedInputClient(){};
48 
49     static DistributedInputClient &GetInstance();
50 
51     int32_t InitSource();
52 
53     int32_t ReleaseSource();
54 
55     int32_t InitSink();
56 
57     int32_t ReleaseSink();
58 
59     int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
60         const std::string &parameters, const std::shared_ptr<RegisterCallback> &callback);
61 
62     int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
63         const std::shared_ptr<UnregisterCallback> &callback);
64 
65     int32_t PrepareRemoteInput(const std::string &deviceId, sptr<IPrepareDInputCallback> callback);
66 
67     int32_t UnprepareRemoteInput(const std::string &deviceId, sptr<IUnprepareDInputCallback> callback);
68 
69     int32_t StartRemoteInput(
70         const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback);
71 
72     int32_t StopRemoteInput(
73         const std::string &deviceId, const uint32_t &inputTypes, sptr<IStopDInputCallback> callback);
74 
75     int32_t StartRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes,
76         sptr<IStartDInputCallback> callback);
77 
78     int32_t StopRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes,
79         sptr<IStopDInputCallback> callback);
80 
81     int32_t PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
82         sptr<IPrepareDInputCallback> callback);
83     int32_t UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
84         sptr<IUnprepareDInputCallback> callback);
85 
86     int32_t StartRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
87         sptr<IStartStopDInputsCallback> callback);
88     int32_t StopRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
89         sptr<IStartStopDInputsCallback> callback);
90 
91     int32_t StartRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector<std::string> &dhIds,
92         sptr<IStartStopDInputsCallback> callback);
93     int32_t StopRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector<std::string> &dhIds,
94         sptr<IStartStopDInputsCallback> callback);
95 
96     bool IsNeedFilterOut(const std::string &deviceId, const BusinessEvent &event);
97     bool IsTouchEventNeedFilterOut(const TouchScreenEvent &event);
98 
99     bool IsStartDistributedInput(const std::string &dhId);
100 
101     int32_t NotifyStartDScreen(const std::string &networkId, const std::string &srcDevId, const uint64_t srcWinId);
102     int32_t NotifyStopDScreen(const std::string &networkId, const std::string &srcScreenInfoKey);
103 
104     int32_t RegisterInputNodeListener(sptr<InputNodeListener> listener);
105     int32_t UnregisterInputNodeListener(sptr<InputNodeListener> listener);
106 
107     int32_t RegisterSimulationEventListener(sptr<ISimulationEventListener> listener);
108     int32_t UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener);
109 
110     void CheckSourceRegisterCallback();
111     void CheckWhiteListCallback();
112     void CheckNodeMonitorCallback();
113     void CheckKeyStateCallback();
114 
115     void CheckSinkRegisterCallback();
116     void CheckSharingDhIdsCallback();
117     void CheckSinkScreenInfoCallback();
118 
119 public:
120     class RegisterDInputCb : public OHOS::DistributedHardware::DistributedInput::RegisterDInputCallbackStub {
121     public:
122         RegisterDInputCb() = default;
123         ~RegisterDInputCb() override = default;
124         void OnResult(const std::string &devId, const std::string &dhId, const int32_t &status) override;
125     };
126 
127     class UnregisterDInputCb : public OHOS::DistributedHardware::DistributedInput::UnregisterDInputCallbackStub {
128     public:
129         UnregisterDInputCb() = default;
130         ~UnregisterDInputCb() override = default;
131         void OnResult(const std::string &devId, const std::string &dhId, const int32_t &status) override;
132     };
133 
134     class AddWhiteListInfosCb : public OHOS::DistributedHardware::DistributedInput::AddWhiteListInfosCallbackStub {
135     public:
136         AddWhiteListInfosCb() = default;
137         ~AddWhiteListInfosCb() override = default;
138         void OnResult(const std::string &deviceId, const std::string &strJson) override;
139     };
140 
141     class DelWhiteListInfosCb : public OHOS::DistributedHardware::DistributedInput::DelWhiteListInfosCallbackStub {
142     public:
143         DelWhiteListInfosCb() = default;
144         ~DelWhiteListInfosCb() override = default;
145         void OnResult(const std::string &deviceId) override;
146     };
147 
148     class SharingDhIdListenerCb : public OHOS::DistributedHardware::DistributedInput::SharingDhIdListenerStub {
149     public:
150         SharingDhIdListenerCb() = default;
151         ~SharingDhIdListenerCb() override = default;
152         int32_t OnSharing(std::string dhId) override;
153         int32_t OnNoSharing(std::string dhId) override;
154     };
155 
156     class GetSinkScreenInfosCb : public OHOS::DistributedHardware::DistributedInput::GetSinkScreenInfosCallbackStub {
157     public:
158         GetSinkScreenInfosCb() = default;
159         ~GetSinkScreenInfosCb() override = default;
160         void OnResult(const std::string &strJson) override;
161     };
162 
163     class DInputClientEventHandler : public AppExecFwk::EventHandler {
164     public:
165         explicit DInputClientEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner);
166         ~DInputClientEventHandler() override = default;
167 
168         void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
169     };
170 
171 private:
172     DistributedInputClient();
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