diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index d4deb46d24ec972af1f02135ef6417b6108af441..e7604ff4d78eb0ec8ed5d710cae58486f2468507 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -39,6 +39,8 @@ namespace DistributedInput { const char INPUT_STRING_SPLIT_POINT = '.'; const uint32_t KEY_DOWN_STATE = 1; const uint32_t KEY_UP_STATE = 0; + const uint32_t KEY_LONGPRESS_STATE = 2; + const uint32_t KEY_OTHER_TYPE = 5; const uint32_t READ_SLEEP_TIME_MS = 50; const uint32_t READ_RETRY_MAX = 5; const uint32_t DH_ID_LENGTH_MAX = 256; @@ -320,6 +322,16 @@ namespace DistributedInput { OPENED = 0x02, CLOSING = 0x03, }; + + /* + * This enumeration class represents the two states of the peropheral: + * THROUGH_IN : The state indicates the peripheral takes effect on the local device. + * THROUGH_OUT : The state indicates that the peripheral takes effect at the remote device. + */ + enum class DhIdState { + THROUGH_IN = 0, + THROUGH_OUT, + }; } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 0cbcf883be22f2a66c89f7166f0144b0ff08e4ad..bd93c557690e68bf8994cdf7648b50b73d0aec70 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -55,6 +55,8 @@ public: const std::string &sinkNodeDesc); void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); + void UpdateSpecEventFirstStatus(bool status); + void UpdateSpecEventState(DhIdState state); private: DistributedInputInject(); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 885c7f4ed259e99710e545ca62ba678ebf432b54..db423e48d7d233073f074a458e804e14752b6b1b 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -61,6 +61,9 @@ public: void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); void NotifyNodeMgrScanVirNode(const std::string &dhId); + void UpdateSpecEventFirstStatus(bool status); + void UpdateSpecEventState(DhIdState state); + void InjectInputEvent(const std::string &dhId, const struct input_event &event); class DInputNodeManagerEventHandler : public AppExecFwk::EventHandler { public: @@ -105,6 +108,8 @@ private: int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; std::shared_ptr callBackHandler_; + std::atomic isFirst_; + DhIdState specEventState_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 237e5940e30190f028149b4dc6345fef7f7586eb..ea4a948c7b609760057b2b42291484df1416b11b 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -276,6 +276,26 @@ void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId) } inputNodeManager_->NotifyNodeMgrScanVirNode(dhId); } + +void DistributedInputInject::UpdateSpecEventFirstStatus(bool status) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return; + } + inputNodeManager_->UpdateSpecEventFirstStatus(status); +} + +void DistributedInputInject::UpdateSpecEventState(DhIdState state) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return; + } + inputNodeManager_->UpdateSpecEventState(state); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index db58cd147be43a8698f61777176a5afe47bcc7c3..bdde0f5fd0ee3e37c9d8c8a8a7d81e2f91656df0 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -32,7 +32,8 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), - isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) + isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE), + isFirst_(false), specEventState_(DhIdState::THROUGH_OUT) { DHLOGI("DistributedInputNodeManager ctor"); std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); @@ -179,6 +180,18 @@ void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode( nodeManagerObj_->ScanSinkInputDevices(devicedhId); } +void DistributedInputNodeManager::UpdateSpecEventFirstStatus(bool status) +{ + DHLOGI("UpdateSpecEventFirstStatus enter, status is %d", status); + isFirst_.store(status); +} + +void DistributedInputNodeManager::UpdateSpecEventState(DhIdState state) +{ + DHLOGI("UpdateSpecEventState enter."); + specEventState_ = state; +} + void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &dhId) { DHLOGI("NotifyNodeMgrScanVirNode enter."); @@ -435,6 +448,18 @@ void DistributedInputNodeManager::InjectEvent() DHLOGI("end"); } +void DistributedInputNodeManager::InjectInputEvent(const std::string &dhId, const struct input_event &event) +{ + VirtualDevice* device = nullptr; + if (GetDevice(dhId, device) < 0) { + DHLOGE("could not find the device"); + return; + } + if (device != nullptr) { + device->InjectInputEvent(event); + } +} + void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptr &rawEvent) { std::string dhId = rawEvent->descriptor; @@ -445,14 +470,33 @@ void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptrwhen); - VirtualDevice* device = nullptr; - if (GetDevice(dhId, device) < 0) { - DHLOGE("could not find the device"); - return; - } - if (device != nullptr) { - device->InjectInputEvent(event); + if (event.type == EV_KEY) { + if (event.value == KEY_UP_STATE) { + UpdateSpecEventFirstStatus(false); + } + + if (event.value == KEY_LONGPRESS_STATE && !isFirst_.load() && specEventState_ == DhIdState::THROUGH_IN) { + struct input_event inputEvent = { + .type = event.type, + .code = event.code, + .value = KEY_DOWN_STATE + }; + DHLOGI("InjectEvent dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"", + GetAnonyString(dhId).c_str(), event.type, event.code, event.value, rawEvent->when); + InjectInputEvent(dhId, inputEvent); + + inputEvent = { + .type = KEY_OTHER_TYPE, + .code = 0, + .value = 0 + }; + DHLOGI("InjectEvent dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"", + GetAnonyString(dhId).c_str(), event.type, event.code, event.value, rawEvent->when); + InjectInputEvent(dhId, inputEvent); + UpdateSpecEventFirstStatus(true); + } } + InjectInputEvent(dhId, event); } int32_t DistributedInputNodeManager::GetDeviceInfo(std::string &deviceId) diff --git a/services/source/sourcemanager/include/distributed_input_source_manager.h b/services/source/sourcemanager/include/distributed_input_source_manager.h index 6e783d05d98323fd3a2743d646d94c8693a7b578..dfad600d21b026d8ca8e851ad91478f97c72e996 100644 --- a/services/source/sourcemanager/include/distributed_input_source_manager.h +++ b/services/source/sourcemanager/include/distributed_input_source_manager.h @@ -283,6 +283,7 @@ public: uint32_t GetInputTypesMap(const std::string deviceId); uint32_t GetAllInputTypesMap(); void ClearResourcesStatus(); + void UpdateSpecEventStatus(); public: void RunRelayPrepareCallback(const std::string &srcId, const std::string &sinkId, const int32_t status); diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index f3968c1fb9efcd975847f8ecdb635082c517ca4b..4cfaeb4c7af3a27c2b3785e10e67a35adf4cfee2 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -79,6 +79,7 @@ void DistributedInputSourceManager::DInputSrcMgrListener::ResetSrcMgrResStatus() return; } sourceManagerObj_->ClearResourcesStatus(); + sourceManagerObj_->UpdateSpecEventStatus(); } void DistributedInputSourceManager::OnStart() @@ -1709,6 +1710,12 @@ void DistributedInputSourceManager::ClearResourcesStatus() relayUnpreCallbacks_.clear(); } +void DistributedInputSourceManager::UpdateSpecEventStatus() +{ + DistributedInputInject::GetInstance().UpdateSpecEventFirstStatus(false); + DistributedInputInject::GetInstance().UpdateSpecEventState(DhIdState::THROUGH_OUT); +} + void DistributedInputSourceManager::SetInputTypesMap(const std::string deviceId, uint32_t value) { if (value == static_cast(DInputDeviceType::NONE)) { diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index dd3f3c01926f48a1a8b6dbff1cddc6454fa313a0..623a74ec4f692beef813a72e7a9dd84662ba9ad3 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -20,22 +20,13 @@ #include #include #include +#include "constants_dinput.h" #include "single_instance.h" namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -/* - * This enumeration class represents the two states of the peropheral: - * THROUGH_IN : The state indicates the peripheral takes effect on the local device. - * THROUGH_OUT : The state indicates that the peripheral takes effect at the remote device. -*/ -enum class DhIdState { - THROUGH_IN = 0, - THROUGH_OUT, -}; - class DInputState { DECLARE_SINGLE_INSTANCE_BASE(DInputState); public: diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 60fd54f00d803ace5f80f4bd040d507d1c0aa37d..6c19bca820e7fe029e6b3c58c179689c5a19f153 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -65,6 +65,10 @@ int32_t DInputState::RecordDhIds(const std::vector &dhIds, DhIdStat if (state == DhIdState::THROUGH_OUT) { CreateSpecialEventInjectThread(sessionId, dhIds); } + + DistributedInputInject::GetInstance().UpdateSpecEventFirstStatus(false); + DistributedInputInject::GetInstance().UpdateSpecEventState(state); + return DH_SUCCESS; } @@ -142,7 +146,6 @@ void DInputState::SpecEventInject(const int32_t sessionId, const std::vector keyboardNodePaths; std::vector keyboardNodeDhIds; - DistributedInputCollector::GetInstance().GetSharedKeyboardPathsByDhIds(dhIds, keyboardNodePaths, keyboardNodeDhIds); DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhIds, keyboardNodePaths, keyboardNodeDhIds); size_t len = keyboardNodePaths.size(); for (size_t i = 0; i < len; ++i) {