1 /* 2 * Copyright (c) 2022 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 37 #include "define_multimodal.h" 38 39 namespace OHOS { 40 namespace MMI { 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 &deviceId, 49 const DistributedHardware::DistributedInput::BusinessEvent &event); 50 51 int32_t StartRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, 52 DInputCallback callback); 53 int32_t StopRemoteInput(const std::string &srcId, const std::string &sinkId, const uint32_t &inputTypes, 54 DInputCallback callback); 55 56 int32_t StartRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector<std::string> &dhIds, 57 DInputCallback callback); 58 int32_t StopRemoteInput(const std::string &srcId, const std::string &sinkId, const std::vector<std::string> &dhIds, 59 DInputCallback callback); 60 61 int32_t StartRemoteInput(const std::string &deviceId, 62 const std::vector<std::string> &dhIds, DInputCallback callback); 63 int32_t StopRemoteInput(const std::string &deviceId, 64 const std::vector<std::string> &dhIds, DInputCallback callback); 65 66 int32_t PrepareRemoteInput(const std::string &srcId, const std::string &sinkId, DInputCallback callback); 67 int32_t UnPrepareRemoteInput(const std::string &srcId, const std::string &sinkId, DInputCallback callback); 68 69 int32_t PrepareRemoteInput(const std::string &deviceId, DInputCallback callback); 70 int32_t UnPrepareRemoteInput(const std::string &deviceId, DInputCallback callback); 71 72 int32_t RegisterEventCallback(SimulateEventCallback callback); 73 int32_t UnregisterEventCallback(SimulateEventCallback callback); 74 75 private: 76 enum class CallbackType { 77 StartDInputCallback, 78 StartDInputCallbackDHIds, 79 StartDInputCallbackSink, 80 StopDInputCallback, 81 StopDInputCallbackDHIds, 82 StopDInputCallbackSink, 83 PrepareStartDInputCallback, 84 UnPrepareStopDInputCallback, 85 PrepareStartDInputCallbackSink, 86 UnPrepareStopDInputCallbackSink, 87 }; 88 89 struct TimerInfo { 90 int32_t times { 0 }; 91 int32_t timerId { 0 }; 92 }; 93 94 class StartDInputCallback final : public DistributedHardware::DistributedInput::StartDInputCallbackStub { 95 public: 96 void OnResult(const std::string &devId, const uint32_t &inputTypes, const int32_t &status) override; 97 }; 98 99 class StopDInputCallback final : public DistributedHardware::DistributedInput::StopDInputCallbackStub { 100 public: 101 void OnResult(const std::string &devId, const uint32_t &inputTypes, const int32_t &status) override; 102 }; 103 104 class StartDInputCallbackDHIds final : 105 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 106 public: 107 void OnResultDhids(const std::string &devId, const int32_t &status) override; 108 }; 109 110 class StopDInputCallbackDHIds final : 111 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 112 public: 113 void OnResultDhids(const std::string &devId, const int32_t &status) override; 114 }; 115 116 class StartDInputCallbackSink final : 117 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 118 public: 119 void OnResultDhids(const std::string &devId, const int32_t &status) override; 120 }; 121 122 class StopDInputCallbackSink final : 123 public DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { 124 public: 125 void OnResultDhids(const std::string &devId, const int32_t &status) override; 126 }; 127 128 class PrepareStartDInputCallback final : 129 public DistributedHardware::DistributedInput::PrepareDInputCallbackStub { 130 public: 131 void OnResult(const std::string &devId, const int32_t &status) override; 132 }; 133 134 class UnPrepareStopDInputCallback final : 135 public DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { 136 public: 137 void OnResult(const std::string &devId, const int32_t &status) override; 138 }; 139 140 class PrepareStartDInputCallbackSink final : 141 public DistributedHardware::DistributedInput::PrepareDInputCallbackStub { 142 public: 143 void OnResult(const std::string &devId, const int32_t &status) override; 144 }; 145 146 class UnPrepareStopDInputCallbackSink final : 147 public DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { 148 public: 149 void OnResult(const std::string &devId, const int32_t &status) override; 150 }; 151 152 class SimulateEventCallbackImpl final : 153 public DistributedHardware::DistributedInput::SimulationEventListenerStub { 154 public: 155 int32_t OnSimulationEvent(uint32_t type, uint32_t code, int32_t value) override; 156 }; 157 158 void SaveCallback(CallbackType type, DInputCallback callback); 159 void AddTimer(const CallbackType &type); 160 void RemoveTimer(const CallbackType &type); 161 void ProcessDInputCallback(CallbackType type, int32_t status); 162 void OnSimulationEvent(uint32_t type, uint32_t code, int32_t value); 163 std::map<CallbackType, TimerInfo> watchingMap_; 164 std::map<CallbackType, DInputCallback> callbackMap_; 165 SimulateEventCallback SimulateEventCallback_ = { nullptr }; 166 sptr<SimulateEventCallbackImpl> simulationEventListener_ { nullptr }; 167 std::mutex adapterLock_; 168 }; 169 170 #define DistributedAdapter ::OHOS::DelayedSingleton<DistributedInputAdapter>::GetInstance() 171 } // namespace MMI 172 } // namespace OHOS 173 174 #endif // DISTRIBUTED_INPUT_ADAPTER_H 175