1 /* 2 * Copyright (c) 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_STATE_BASE_H 17 #define DISTRIBUTED_INPUT_STATE_BASE_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 #include <linux/input.h> 24 25 #include "constants_dinput.h" 26 #include "single_instance.h" 27 #include "touchpad_event_fragment_mgr.h" 28 29 namespace OHOS { 30 namespace DistributedHardware { 31 namespace DistributedInput { 32 /* 33 * This enumeration class represents the two states of the peropheral: 34 * THROUGH_IN : The state indicates the peripheral takes effect on the local device. 35 * THROUGH_OUT : The state indicates that the peripheral takes effect at the remote device. 36 */ 37 enum class DhIdState { 38 THROUGH_IN = 0, 39 THROUGH_OUT, 40 }; 41 42 class DInputSinkState { 43 DECLARE_SINGLE_INSTANCE_BASE(DInputSinkState); 44 public: 45 int32_t Init(); 46 int32_t Release(); 47 int32_t RecordDhIds(const std::vector<std::string> &dhIds, DhIdState state, const int32_t sessionId); 48 int32_t RemoveDhIds(const std::vector<std::string> &dhIds); 49 DhIdState GetStateByDhid(const std::string &dhId); 50 51 void AddKeyDownState(struct RawEvent event); 52 void RemoveKeyDownState(struct RawEvent event); 53 /** 54 * If user pressed some keys before we prepare distributed input sucess, because the monitor thread not start, 55 * we CAN NOT monitor those keys. 56 * So, we check all keys state before start input device monitor thread. 57 * But the keys state not contain the pressed order, so we use the key REPEAT event to move the corresponding 58 * cached key last. 59 */ 60 void CheckAndSetLongPressedKeyOrder(struct RawEvent event); 61 62 /** 63 * Clear Device stats if unprepare. 64 */ 65 void ClearDeviceStates(); 66 void SimulateMouseBtnMouseUpState(const std::string &dhId, const struct RawEvent &event); 67 void SimulateTouchPadStateReset(const std::vector<RawEvent> &events); 68 /** 69 * @brief check is one device in down state 70 * 71 * @param dhId the dhid 72 * @return true in down state 73 * @return false NOT in down state 74 */ 75 bool IsDhIdDown(const std::string &dhId); 76 std::shared_ptr<TouchPadEventFragmentMgr> GetTouchPadEventFragMgr(); 77 private: 78 DInputSinkState() = default; 79 ~DInputSinkState(); 80 // Simulate device state to the pass through target device. 81 void SimulateEventInjectToSrc(const int32_t sessionId, const std::vector<std::string> &dhIds); 82 void SimulateKeyDownEvents(const int32_t sessionId, const std::string &dhId); 83 void SimulateKeyDownEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event); 84 void SimulateTouchPadEvents(const int32_t sessionId, const std::string &dhId); 85 private: 86 std::mutex operationMutex_; 87 std::map<std::string, DhIdState> dhIdStateMap_; 88 89 std::mutex keyDownStateMapMtx_; 90 // Record key down state of each device dhid 91 std::unordered_map<std::string, std::vector<struct RawEvent>> keyDownStateMap_; 92 93 std::mutex absPosMtx_; 94 // Record abs x/y of touchpad 95 std::unordered_map<std::string, std::pair<int32_t, int32_t>> absPositionsMap_; 96 std::atomic<int32_t> lastSessionId_ {0}; 97 98 std::shared_ptr<TouchPadEventFragmentMgr> touchPadEventFragMgr_; 99 }; 100 } // namespace DistributedInput 101 } // namespace DistributedHardware 102 } // namespace OHOS 103 #endif // DISTRIBUTED_INPUT_STATE_BASE_H