1 /* 2 * Copyright (c) 2022-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_ADAPTER_H 17 #define DISTRIBUTED_INPUT_ADAPTER_H 18 19 #include <functional> 20 #include <map> 21 22 #include <string> 23 #include <vector> 24 25 #include "distributed_input_kit.h" 26 #include "i_start_stop_d_inputs_call_back.h" 27 #include "nocopyable.h" 28 #include "prepare_d_input_call_back_stub.h" 29 #include "simulation_event_listener_stub.h" 30 #include "singleton.h" 31 #include "start_d_input_call_back_stub.h" 32 #include "start_stop_d_inputs_call_back_stub.h" 33 #include "start_stop_result_call_back_stub.h" 34 #include "stop_d_input_call_back_stub.h" 35 #include "unprepare_d_input_call_back_stub.h" 36 #include "register_session_state_callback_stub.h" 37 38 namespace OHOS { 39 namespace Msdp { 40 namespace DeviceStatus { 41 class DistributedInputAdapter final { 42 DECLARE_DELAYED_SINGLETON(DistributedInputAdapter); 43 public: 44 using DInputCallback = std::function<void(bool)>; 45 using SimulateEventCallback = std::function<void(uint32_t type, uint32_t code, int32_t value)>; 46 DISALLOW_COPY_AND_MOVE(DistributedInputAdapter); 47 48 bool IsNeedFilterOut(const std::string &networkId, 49 const DistributedHardware::DistributedInput::BusinessEvent &event); 50 51 int32_t StartRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, 52 const std::vector<std::string> &inputDeviceDhids, DInputCallback callback); 53 int32_t StopRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, 54 const std::vector<std::string> &inputDeviceDhids, DInputCallback callback); 55 56 int32_t StopRemoteInput(const std::string &originNetworkId, 57 const std::vector<std::string> &inputDeviceDhids, DInputCallback callback); 58 59 int32_t PrepareRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, 60 DInputCallback callback); 61 int32_t UnPrepareRemoteInput(const std::string &remoteNetworkId, const std::string &originNetworkId, 62 DInputCallback callback); 63 64 int32_t PrepareRemoteInput(const std::string &networkId, DInputCallback callback); 65 int32_t UnPrepareRemoteInput(const std::string &networkId, DInputCallback callback); 66 int32_t RegisterSessionStateCb(std::function<void(uint32_t)> callback); 67 int32_t UnregisterSessionStateCb(); 68 69 private: 70 enum class CallbackType { 71 StartDInputCallback, 72 StartDInputCallbackDHIds, 73 StartDInputCallbackSink, 74 StopDInputCallback, 75 StopDInputCallbackDHIds, 76 StopDInputCallbackSink, 77 PrepareStartDInputCallback, 78 UnPrepareStopDInputCallback, 79 PrepareStartDInputCallbackSink, 80 UnPrepareStopDInputCallbackSink 81 }; 82 83 struct TimerInfo { 84 int32_t times { 0 }; 85 int32_t timerId { 0 }; 86 }; 87 88 class StartDInputCallback final : public DistributedHardware::DistributedInput::StartDInputCallbackStub { 89 public: 90 void OnResult(const std::string &devID, const uint32_t &inputTypes, const int32_t &status) override; 91 }; 92 93 class StopDInputCallback final : public DistributedHardware::DistributedInput::StopDInputCallbackStub { 94 public: 95 void OnResult(const std::string &devID, const uint32_t &inputTypes, const int32_t &status) override; 96 }; 97 98 class StartDInputCallbackDHIds final : 99 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 100 public: 101 void OnResultDhids(const std::string &devID, const int32_t &status) override; 102 }; 103 104 class StopDInputCallbackDHIds final : 105 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 106 public: 107 void OnResultDhids(const std::string &devID, const int32_t &status) override; 108 }; 109 110 class StartDInputCallbackSink final : 111 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 112 public: 113 void OnResultDhids(const std::string &devID, const int32_t &status) override; 114 }; 115 116 class StopDInputCallbackSink final : 117 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 118 public: 119 void OnResultDhids(const std::string &devID, const int32_t &status) override; 120 }; 121 122 class PrepareStartDInputCallback final : 123 public DistributedHardware::DistributedInput::PrepareDInputCallbackStub { 124 public: 125 void OnResult(const std::string &devID, const int32_t &status) override; 126 }; 127 128 class UnPrepareStopDInputCallback final : 129 public DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { 130 public: 131 void OnResult(const std::string &devID, const int32_t &status) override; 132 }; 133 134 class PrepareStartDInputCallbackSink final : 135 public DistributedHardware::DistributedInput::PrepareDInputCallbackStub { 136 public: 137 void OnResult(const std::string &devID, const int32_t &status) override; 138 }; 139 140 class UnPrepareStopDInputCallbackSink final : 141 public DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { 142 public: 143 void OnResult(const std::string &devID, const int32_t &status) override; 144 }; 145 146 class SessionStateCallback final : 147 public DistributedHardware::DistributedInput::RegisterSessionStateCallbackStub { 148 public: 149 SessionStateCallback() = default; 150 ~SessionStateCallback() = default; SessionStateCallback(std::function<void (uint32_t)> callback)151 SessionStateCallback(std::function<void(uint32_t)> callback) : callback_(callback) {} 152 void OnResult(const std::string &devID, const uint32_t status) override; 153 private: 154 std::function<void(uint32_t)> callback_; 155 }; 156 157 void SaveCallback(CallbackType cbtype, DInputCallback callback); 158 void AddTimer(const CallbackType &type); 159 void RemoveTimer(const CallbackType &type); 160 void ProcessDInputCallback(CallbackType cbtype, int32_t status); 161 std::map<CallbackType, TimerInfo> watchings_; 162 std::map<CallbackType, DInputCallback> callbacks_; 163 std::mutex adapterLock_; 164 }; 165 166 #define D_INPUT_ADAPTER OHOS::DelayedSingleton<DistributedInputAdapter>::GetInstance() 167 } // namespace DeviceStatus 168 } // namespace Msdp 169 } // namespace OHOS 170 171 #endif // DISTRIBUTED_INPUT_ADAPTER_H 172