From 8348936a7eda7c46870193a2baa93917547f45f0 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Fri, 1 Dec 2023 21:12:58 +0800 Subject: [PATCH] add device id mark for virtual driver Signed-off-by: hwzhangchuang --- common/include/input_hub.cpp | 2 +- common/include/input_hub.h | 2 - .../include/distributed_input_inject.h | 13 +- .../include/distributed_input_node_manager.h | 54 +++-- .../src/distributed_input_inject.cpp | 70 ++---- .../src/distributed_input_node_manager.cpp | 226 +++++++++--------- .../distributed_input_sourceinject_test.cpp | 52 +--- .../src/dinput_source_listener.cpp | 2 +- .../src/distributed_input_source_manager.cpp | 11 +- .../distributed_input_source_transport.h | 2 +- .../distributed_input_source_transport.cpp | 12 +- 11 files changed, 185 insertions(+), 261 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 4f08970..d6d7201 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -44,7 +44,7 @@ const uint32_t SLEEP_TIME_US = 100 * 1000; const std::string MOUSE_NODE_KEY = "mouse"; } -InputHub::InputHub() : epollFd_(0), iNotifyFd_(0), inputWd_(0), needToScanDevices_(true), nextDeviceId_(1), +InputHub::InputHub() : epollFd_(0), iNotifyFd_(0), inputWd_(0), needToScanDevices_(true), mPendingEventItems{}, pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), deviceChanged_(false), inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false) { diff --git a/common/include/input_hub.h b/common/include/input_hub.h index d702e2d..83ff337 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -213,9 +213,7 @@ private: std::set skipDevicePaths_; std::atomic needToScanDevices_; - std::string deviceId_; std::string touchDescriptor; - std::atomic nextDeviceId_; // The array of pending epoll events and the index of the next event to be handled. struct epoll_event mPendingEventItems[EPOLL_MAX_EVENTS]; diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 7bb5028..2055e99 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -34,8 +34,7 @@ public: int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, const std::string ¶meters); int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId); - int32_t RegisterDistributedEvent(RawEvent *buffer, size_t bufferSize); - int32_t RegisterDistributedEvent(const std::vector &events); + int32_t RegisterDistributedEvent(const std::string deviceId, const std::vector &events); int32_t StructTransJson(const InputDevice &pBuf, std::string &strDescriptor); void StartInjectThread(); void StopInjectThread(); @@ -43,16 +42,14 @@ public: const uint32_t sourceWinHeight); int32_t CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId, const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight); - int32_t RemoveVirtualTouchScreenNode(const std::string &dhId); + int32_t RemoveVirtualTouchScreenNode(const std::string &deviceId, const std::string &dhId); int32_t GetVirtualTouchScreenFd(); - int32_t GetDhIdsByInputType(const std::string &devId, const uint32_t &inputTypes, std::vector &dhIds); int32_t RegisterInjectEventCb(sptr callback); int32_t UnregisterInjectEventCb(); - void NotifyNodeMgrScanVirNode(const std::string &dhId); - void InputDeviceEventInject(const std::shared_ptr &rawEvent); - void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); + void NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId); + void GetVirtualKeyboardPaths(const std::string &deviceId, const std::vector &dhIds, + std::vector &virKeyboardPaths); private: DistributedInputInject(); ~DistributedInputInject(); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 9f5ead5..a684c47 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -37,7 +37,19 @@ namespace DistributedHardware { namespace DistributedInput { constexpr uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1; constexpr uint32_t DINPUT_INJECT_EVENT_FAIL = 2; +const std::string INPUT_NODE_DEVID = "devId"; const std::string INPUT_NODE_DHID = "dhId"; +/** + * @brief the unique id for the input peripheral. + * left is device networkid, right is dhId in that device. + */ +using DhUniqueID = std::pair; +/** + * @brief a batch events form one device + * left: device networid where these events from + * right: the event batch + */ +using EventBatch = std::pair>; class DistributedInputNodeManager { public: DistributedInputNodeManager(); @@ -45,25 +57,29 @@ public: int32_t OpenDevicesNode(const std::string &devId, const std::string &dhId, const std::string ¶meters); - int32_t GetDevice(const std::string &dhId, VirtualDevice *&device); - void ReportEvent(const RawEvent rawEvent); - void ReportEvent(const std::vector &events); - int32_t CloseDeviceLocked(const std::string &dhId); + int32_t GetDevice(const std::string &networkId, const std::string &dhId, VirtualDevice *&device); + void ReportEvent(const std::string deviceId, const std::vector &events); + int32_t CloseDeviceLocked(const std::string &networkId, const std::string &dhId); void StartInjectThread(); void StopInjectThread(); int32_t CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId, const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight); - int32_t RemoveVirtualTouchScreenNode(const std::string &dhId); + int32_t RemoveVirtualTouchScreenNode(const std::string &deviceId, const std::string &dhId); int32_t GetVirtualTouchScreenFd(); int32_t GetDeviceInfo(std::string &deviceId); - void GetDevicesInfoByType(const std::string &networkId, uint32_t inputTypes, std::map &datas); - void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); - void ProcessInjectEvent(const std::shared_ptr &rawEvent); + void ProcessInjectEvent(const EventBatch &events); - void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); - void NotifyNodeMgrScanVirNode(const std::string &dhId); + /** + * @brief Get the Virtual Keyboard Paths By Dh Ids object + * + * @param dhKeys list for device identify({networkId, dhId}) + * @param virKeyboardPaths the matched keyboard device + * @param virKeyboardDhIds matched keyboard device identify + */ + void GetVirtualKeyboardPaths(const std::vector &dhUniqueIds, + std::vector &virKeyboardPaths); + void NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId); void RegisterInjectEventCb(sptr callback); void UnregisterInjectEventCb(); @@ -84,7 +100,7 @@ public: }; private: - void AddDeviceLocked(const std::string &dhId, std::unique_ptr device); + void AddDeviceLocked(const std::string &networkId, const std::string &dhId, std::unique_ptr device); int32_t CreateHandle(const InputDevice &inputDevice, const std::string &devId, const std::string &dhId); void ParseInputDeviceJson(const std::string &str, InputDevice &pBuf); void ParseInputDevice(const nlohmann::json &inputDeviceJson, InputDevice &pBuf); @@ -92,15 +108,15 @@ private: void ParseInputDeviceEvents(const nlohmann::json &inputDeviceJson, InputDevice &pBuf); void InjectEvent(); - void ScanSinkInputDevices(const std::string &dhId); - void OpenInputDevice(const std::string &devicePath, const std::string &dhId); + void ScanSinkInputDevices(const std::string &deviceId, const std::string &dhId); + bool MatchAndSavePhysicalPath(const std::string &devicePath, const std::string &deviceId, const std::string &dhId); bool IsVirtualDev(int fd); - bool GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath); - void SetPathForDevMap(const std::string &dhId, const std::string &devicePath); + bool GetDevDhUniqueIdByFd(int fd, DhUniqueID &dhUnqueId, std::string &physicalPath); + void SetPathForVirDev(const DhUniqueID &dhUniqueId, const std::string &devicePath); void RunInjectEventCallback(const std::string &dhId, const uint32_t injectEvent); - /* the key is dhId, and the value is virtualDevice */ - std::map> virtualDeviceMap_; + /* the key is {networkId, dhId}, and the value is virtualDevice */ + std::map> virtualDeviceMap_; std::mutex virtualDeviceMapMutex_; std::atomic isInjectThreadCreated_; std::atomic isInjectThreadRunning_; @@ -108,7 +124,7 @@ private: std::thread eventInjectThread_; std::mutex injectThreadMutex_; std::condition_variable conditionVariable_; - std::queue> injectQueue_; + std::queue injectQueue_; std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index d506584..1abbe2b 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -74,7 +74,7 @@ int32_t DistributedInputInject::UnregisterDistributedHardware(const std::string DHLOGE("the DistributedInputNodeManager is null\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - if (inputNodeManager_->CloseDeviceLocked(dhId) < 0) { + if (inputNodeManager_->CloseDeviceLocked(devId, dhId) < 0) { DHLOGE("delete virtual device error\n"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_UNREGISTER_FAIL; } @@ -83,22 +83,6 @@ int32_t DistributedInputInject::UnregisterDistributedHardware(const std::string return DH_SUCCESS; } -int32_t DistributedInputInject::GetDhIdsByInputType(const std::string &devId, const uint32_t &inputTypes, - std::vector &dhIds) -{ - std::lock_guard lock(inputNodeManagerMutex_); - if (inputNodeManager_ == nullptr) { - DHLOGE("the inputNodeListener is nullptr"); - return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; - } - std::map datas; - inputNodeManager_->GetDevicesInfoByType(devId, inputTypes, datas); - for (const auto &data_ : datas) { - dhIds.push_back(data_.second); - } - return DH_SUCCESS; -} - int32_t DistributedInputInject::StructTransJson(const InputDevice &pBuf, std::string &strDescriptor) { DHLOGI("[%s] %d, %d, %d, %d, %s.\n", (pBuf.name).c_str(), pBuf.bus, pBuf.vendor, pBuf.product, pBuf.version, @@ -126,21 +110,7 @@ int32_t DistributedInputInject::StructTransJson(const InputDevice &pBuf, std::st return DH_SUCCESS; } -void DistributedInputInject::InputDeviceEventInject(const std::shared_ptr &rawEvent) -{ - std::lock_guard lock(inputNodeManagerMutex_); - if (inputNodeManager_ == nullptr) { - DHLOGE("the inputNodeListener is nullptr"); - return; - } - if (rawEvent == nullptr) { - DHLOGE("the rawEvent is nullptr"); - return; - } - inputNodeManager_->ProcessInjectEvent(rawEvent); -} - -int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent *buffer, size_t bufferSize) +int32_t DistributedInputInject::RegisterDistributedEvent(const std::string deviceId, const std::vector &events) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { @@ -148,21 +118,7 @@ int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent *buffer, size_ return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - for (size_t i = 0; i < bufferSize; i++) { - inputNodeManager_->ReportEvent(buffer[i]); - } - return DH_SUCCESS; -} - -int32_t DistributedInputInject::RegisterDistributedEvent(const std::vector &events) -{ - std::lock_guard lock(inputNodeManagerMutex_); - if (inputNodeManager_ == nullptr) { - DHLOGE("the DistributedInputNodeManager is null"); - return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; - } - - inputNodeManager_->ReportEvent(events); + inputNodeManager_->ReportEvent(deviceId, events); return DH_SUCCESS; } @@ -201,14 +157,14 @@ int32_t DistributedInputInject::CreateVirtualTouchScreenNode(const std::string & return inputNodeManager_->CreateVirtualTouchScreenNode(devId, dhId, srcWinId, sourcePhyWidth, sourcePhyHeight); } -int32_t DistributedInputInject::RemoveVirtualTouchScreenNode(const std::string &dhId) +int32_t DistributedInputInject::RemoveVirtualTouchScreenNode(const std::string &deviceId, const std::string &dhId) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - return inputNodeManager_->RemoveVirtualTouchScreenNode(dhId); + return inputNodeManager_->RemoveVirtualTouchScreenNode(deviceId, dhId); } int32_t DistributedInputInject::GetVirtualTouchScreenFd() @@ -237,25 +193,31 @@ int32_t DistributedInputInject::UnregisterInjectEventCb() return DH_SUCCESS; } -void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds) +void DistributedInputInject::GetVirtualKeyboardPaths(const std::string &deviceId, + const std::vector &dhIds, std::vector &virKeyboardPaths) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } - inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, virKeyboardPaths, virKeyboardDhIds); + std::vector dhUniqIds; + std::for_each(dhIds.begin(), dhIds.end(), + [&deviceId, &dhUniqIds](std::string dhId) { + DhUniqueID id = {deviceId, dhId}; + dhUniqIds.push_back(id); + }); + inputNodeManager_->GetVirtualKeyboardPaths(dhUniqIds, virKeyboardPaths); } -void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId) +void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } - inputNodeManager_->NotifyNodeMgrScanVirNode(dhId); + inputNodeManager_->NotifyNodeMgrScanVirNode(devId, dhId); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 4e195d6..83dce47 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -152,13 +152,17 @@ void DistributedInputNodeManager::ParseInputDeviceEvents(const nlohmann::json &i } } -void DistributedInputNodeManager::ScanSinkInputDevices(const std::string &dhId) +void DistributedInputNodeManager::ScanSinkInputDevices(const std::string &deviceId, const std::string &dhId) { - DHLOGI("ScanSinkInputDevices enter, dhId %s.", dhId.c_str()); + DHLOGI("ScanSinkInputDevices enter, deviceId:%s, dhId %s.", + GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str()); std::vector vecInputDevPath; ScanInputDevicesPath(DEVICE_PATH, vecInputDevPath); for (auto &tempPath: vecInputDevPath) { - OpenInputDevice(tempPath, dhId); + if (MatchAndSavePhysicalPath(tempPath, deviceId, dhId)) { + DHLOGI("Set physical path success"); + break; + } } } @@ -197,15 +201,17 @@ void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode( std::shared_ptr dataMsg = event->GetSharedObject(); auto it = dataMsg->begin(); nlohmann::json innerMsg = *(it); + std::string devId = innerMsg[INPUT_NODE_DEVID]; std::string devicedhId = innerMsg[INPUT_NODE_DHID]; - nodeManagerObj_->ScanSinkInputDevices(devicedhId); + nodeManagerObj_->ScanSinkInputDevices(devId, devicedhId); } -void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &dhId) +void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &devId, const std::string &dhId) { DHLOGI("NotifyNodeMgrScanVirNode enter."); std::shared_ptr jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; + tmpJson[INPUT_NODE_DEVID] = devId; tmpJson[INPUT_NODE_DHID] = dhId; jsonArrayMsg->push_back(tmpJson); AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( @@ -232,7 +238,7 @@ bool DistributedInputNodeManager::IsVirtualDev(int fd) return true; } -bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath) +bool DistributedInputNodeManager::GetDevDhUniqueIdByFd(int fd, DhUniqueID &dhUnqueId, std::string &physicalPath) { char buffer[INPUT_EVENT_BUFFER_SIZE] = {0}; if (ioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &buffer) < 1) { @@ -242,23 +248,32 @@ bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string &dhId, std: buffer[sizeof(buffer) - 1] = '\0'; physicalPath = buffer; - DHLOGD("GetDevDhIdByFd physicalPath %s.", physicalPath.c_str()); - dhId = physicalPath.substr(physicalPath.find(DH_ID_PREFIX)); - if (dhId.size() == 0) { - DHLOGE("Get dev dhid failed."); + DHLOGD("GetDevDhUniqueIdByFd physicalPath %s.", physicalPath.c_str()); + std::vector phyPathVec; + SplitStringToVector(physicalPath, '|', phyPathVec); + if (phyPathVec.size() != 3) { + DHLOGE("The physical path is invalid"); return false; } - DHLOGD("Device dhId %s.", GetAnonyString(dhId).c_str()); + std::string deviceId = phyPathVec[1]; + std::string dhId = phyPathVec[2]; + if (deviceId.empty() || dhId.empty()) { + DHLOGE("Get dev deviceid and dhid failed."); + return false; + } + dhUnqueId.first = deviceId; + dhUnqueId.second = dhId; + DHLOGD("Device deviceid: %s, dhId %s.", GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str()); return true; } -void DistributedInputNodeManager::SetPathForDevMap(const std::string &dhId, const std::string &devicePath) +void DistributedInputNodeManager::SetPathForVirDev(const DhUniqueID &dhUniqueId, const std::string &devicePath) { std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { - DHLOGD("Virtual device map dhid %s.", iter->first.c_str()); - if (dhId.compare(iter->first) == 0) { + DHLOGD("Check Virtual device deviceId %s, dhid %s.", iter->first.first.c_str(), iter->first.second.c_str()); + if (iter->first == dhUniqueId) { DHLOGD("Found the virtual device, set path :%s", devicePath.c_str()); iter->second->SetPath(devicePath); break; @@ -267,45 +282,59 @@ void DistributedInputNodeManager::SetPathForDevMap(const std::string &dhId, cons } } -void DistributedInputNodeManager::OpenInputDevice(const std::string &devicePath, const std::string &dhId) +bool DistributedInputNodeManager::MatchAndSavePhysicalPath(const std::string &devicePath, + const std::string &deviceId, const std::string &dhId) { DHLOGI("Opening input device path: %s", devicePath.c_str()); - std::string curDhId; + DhUniqueID curDhUniqueId; std::string physicalPath; int fd = OpenInputDeviceFdByPath(devicePath); if (fd == UN_INIT_FD_VALUE) { DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str()); - return; + return false; } if (!IsVirtualDev(fd)) { DHLOGE("The dev not virtual, devicePath %s.", devicePath.c_str()); - return; + CloseFd(fd); + return false; } - if (!GetDevDhIdByFd(fd, curDhId, physicalPath) || dhId != curDhId) { - DHLOGE("This is not same dev, curDhId %s.", devicePath.c_str()); - return; + if (!GetDevDhUniqueIdByFd(fd, curDhUniqueId, physicalPath)) { + DHLOGE("Get unique id failed, device path %s.", devicePath.c_str()); + CloseFd(fd); + return false; } - DHLOGD("curDhId %s.", GetAnonyString(curDhId).c_str()); - SetPathForDevMap(curDhId, devicePath); + + DHLOGD("This opening node deviceId: %s, dhId %s, to match node deviceId: %s, dhId: %s", + GetAnonyString(curDhUniqueId.first).c_str(), GetAnonyString(curDhUniqueId.second).c_str(), + GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str()); + + if (curDhUniqueId.first != deviceId || curDhUniqueId.second != dhId) { + DHLOGW("It is not the target vir node, skip it."); + CloseFd(fd); + return false; + } + + SetPathForVirDev(curDhUniqueId, devicePath); + CloseFd(fd); + return true; } -void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds) +void DistributedInputNodeManager::GetVirtualKeyboardPaths(const std::vector &dhUniqueIds, + std::vector &virKeyboardPaths) { std::lock_guard lock(virtualDeviceMapMutex_); - for (const auto &dhId : dhIds) { + for (const auto &dhUniqueId : dhUniqueIds) { auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { if (iter->second == nullptr) { DHLOGE("device is nullptr"); continue; } - if ((iter->first.compare(dhId) == 0) && + if ((iter->first == dhUniqueId) && ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { - DHLOGI("Found vir keyboard path %s, dhid %s", iter->second->GetPath().c_str(), - GetAnonyString(dhId).c_str()); + DHLOGI("Found vir keyboard path %s, deviceId %s, dhid %s", iter->second->GetPath().c_str(), + GetAnonyString(dhUniqueId.first).c_str(), GetAnonyString(dhUniqueId.second).c_str()); virKeyboardPaths.push_back(iter->second->GetPath()); - virKeyboardDhIds.push_back(dhId); } iter++; } @@ -325,7 +354,7 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice DHLOGE("could not create new virtual device\n"); return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } - AddDeviceLocked(inputDevice.descriptor, std::move(virtualDevice)); + AddDeviceLocked(devId, inputDevice.descriptor, std::move(virtualDevice)); return DH_SUCCESS; } @@ -344,14 +373,14 @@ int32_t DistributedInputNodeManager::CreateVirtualTouchScreenNode(const std::str return ERR_DH_INPUT_SERVER_SOURCE_CREATE_HANDLE_FAIL; } virtualTouchScreenFd_ = device->GetDeviceFd(); - AddDeviceLocked(dhId, std::move(device)); + AddDeviceLocked(devId, dhId, std::move(device)); DHLOGI("CreateVirtualTouchScreenNode end, dhId: %s", GetAnonyString(dhId).c_str()); return DH_SUCCESS; } -int32_t DistributedInputNodeManager::RemoveVirtualTouchScreenNode(const std::string &dhId) +int32_t DistributedInputNodeManager::RemoveVirtualTouchScreenNode(const std::string &deviceId, const std::string &dhId) { - return CloseDeviceLocked(dhId); + return CloseDeviceLocked(deviceId, dhId); } int32_t DistributedInputNodeManager::GetVirtualTouchScreenFd() @@ -359,34 +388,43 @@ int32_t DistributedInputNodeManager::GetVirtualTouchScreenFd() return virtualTouchScreenFd_; } -void DistributedInputNodeManager::AddDeviceLocked(const std::string &dhId, std::unique_ptr device) +void DistributedInputNodeManager::AddDeviceLocked(const std::string &networkId, const std::string &dhId, + std::unique_ptr device) { - DHLOGI("dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGI("AddDeviceLocked deviceId=%s, dhId=%s", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); std::lock_guard lock(virtualDeviceMapMutex_); - auto [dev_it, inserted] = virtualDeviceMap_.insert_or_assign(dhId, std::move(device)); + auto [dev_it, inserted] = virtualDeviceMap_.insert_or_assign( + {networkId, dhId}, std::move(device)); if (!inserted) { - DHLOGI("Device id %s exists, replaced. \n", GetAnonyString(dhId).c_str()); + DHLOGI("Device exists, deviceId=%s, dhId=%s, replaced. \n", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); } } -int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &dhId) +int32_t DistributedInputNodeManager::CloseDeviceLocked(const std::string &networkId, const std::string &dhId) { - DHLOGI("CloseDeviceLocked called, dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGI("CloseDeviceLocked called, deviceId=%s, dhId=%s", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); std::lock_guard lock(virtualDeviceMapMutex_); - std::map>::iterator iter = virtualDeviceMap_.find(dhId); + DhUniqueID dhUniqueId = {networkId, dhId}; + std::map>::iterator iter = virtualDeviceMap_.find(dhUniqueId); if (iter != virtualDeviceMap_.end()) { - DHLOGI("CloseDeviceLocked called success, dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGI("CloseDeviceLocked called success, deviceId=%s, dhId=%s", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); virtualDeviceMap_.erase(iter); return DH_SUCCESS; } - DHLOGE("CloseDeviceLocked called failure, dhId=%s", GetAnonyString(dhId).c_str()); + DHLOGE("CloseDeviceLocked called failure, deviceId=%s, dhId=%s", + GetAnonyString(networkId).c_str(), GetAnonyString(dhId).c_str()); return ERR_DH_INPUT_SERVER_SOURCE_CLOSE_DEVICE_FAIL; } -int32_t DistributedInputNodeManager::GetDevice(const std::string &dhId, VirtualDevice *&device) +int32_t DistributedInputNodeManager::GetDevice(const std::string &networkId, const std::string &dhId, + VirtualDevice *&device) { std::lock_guard lock(virtualDeviceMapMutex_); - auto iter = virtualDeviceMap_.find(dhId); + auto iter = virtualDeviceMap_.find({networkId, dhId}); if (iter != virtualDeviceMap_.end()) { device = iter->second.get(); return DH_SUCCESS; @@ -420,19 +458,10 @@ void DistributedInputNodeManager::StopInjectThread() } } -void DistributedInputNodeManager::ReportEvent(const RawEvent rawEvent) +void DistributedInputNodeManager::ReportEvent(const std::string deviceId, const std::vector &events) { std::lock_guard lockGuard(injectThreadMutex_); - injectQueue_.push(std::make_shared(rawEvent)); - conditionVariable_.notify_all(); -} - -void DistributedInputNodeManager::ReportEvent(const std::vector &events) -{ - std::lock_guard lockGuard(injectThreadMutex_); - for (auto ev : events) { - injectQueue_.push(std::make_shared(ev)); - } + injectQueue_.push({deviceId, events}); conditionVariable_.notify_all(); } @@ -444,7 +473,7 @@ void DistributedInputNodeManager::InjectEvent() } DHLOGI("start"); while (isInjectThreadRunning_.load()) { - std::shared_ptr event = nullptr; + EventBatch events; { std::unique_lock waitEventLock(injectThreadMutex_); conditionVariable_.wait(waitEventLock, @@ -452,16 +481,12 @@ void DistributedInputNodeManager::InjectEvent() if (injectQueue_.empty()) { continue; } - event = injectQueue_.front(); + events = injectQueue_.front(); injectQueue_.pop(); } - if (event == nullptr) { - DHLOGD("event is null!"); - continue; - } DHLOGD("process event, inject queue size: %zu", injectQueue_.size()); - ProcessInjectEvent(event); + ProcessInjectEvent(events); } DHLOGI("end"); } @@ -488,23 +513,27 @@ void DistributedInputNodeManager::RunInjectEventCallback(const std::string &dhId SessionStateCallback_->OnResult(dhId, DINPUT_INJECT_EVENT_FAIL); } -void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptr &rawEvent) +void DistributedInputNodeManager::ProcessInjectEvent(const EventBatch &events) { - std::string dhId = rawEvent->descriptor; - struct input_event event = { - .type = rawEvent->type, - .code = rawEvent->code, - .value = rawEvent->value - }; - DHLOGI("InjectEvent dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"", - GetAnonyString(dhId).c_str(), event.type, event.code, event.value, rawEvent->when); - VirtualDevice* device = nullptr; - if (GetDevice(dhId, device) < 0) { - DHLOGE("could not find the device"); - return; - } - if (device != nullptr) { - device->InjectInputEvent(event); + std::string deviceId = events.first; + for (const auto &rawEvent : events.second) { + std::string dhId = rawEvent.descriptor; + struct input_event event = { + .type = rawEvent.type, + .code = rawEvent.code, + .value = rawEvent.value + }; + DHLOGI("InjectEvent deviceId: %s, dhId: %s, eventType: %d, eventCode: %d, eventValue: %d, when: " PRId64"", + GetAnonyString(deviceId).c_str(), GetAnonyString(dhId).c_str(), + event.type, event.code, event.value, rawEvent.when); + VirtualDevice* device = nullptr; + if (GetDevice(deviceId, dhId, device) < 0) { + DHLOGE("could not find the device"); + return; + } + if (device != nullptr) { + device->InjectInputEvent(event); + } } } @@ -522,45 +551,6 @@ int32_t DistributedInputNodeManager::GetDeviceInfo(std::string &deviceId) DHLOGI("device id is %s", GetAnonyString(deviceId).c_str()); return DH_SUCCESS; } - -void DistributedInputNodeManager::GetDevicesInfoByType(const std::string &networkId, uint32_t inputTypes, - std::map &datas) -{ - uint32_t inputType = 0; - - if ((inputTypes & static_cast(DInputDeviceType::MOUSE)) != 0) { - inputType |= INPUT_DEVICE_CLASS_CURSOR; - } - - if ((inputTypes & static_cast(DInputDeviceType::KEYBOARD)) != 0) { - inputType |= INPUT_DEVICE_CLASS_KEYBOARD; - } - - if ((inputTypes & static_cast(DInputDeviceType::MOUSE)) != 0) { - inputType |= INPUT_DEVICE_CLASS_TOUCH; - } - - std::lock_guard lock(virtualDeviceMapMutex_); - for (const auto &[id, virdevice] : virtualDeviceMap_) { - if ((virdevice->GetDeviceType() & inputType) && (virdevice->GetNetWorkId() == networkId)) { - datas.insert(std::pair(virdevice->GetDeviceFd(), id)); - } - } -} - -void DistributedInputNodeManager::GetDevicesInfoByDhId( - std::vector dhidsVec, std::map &datas) -{ - for (auto dhId : dhidsVec) { - std::lock_guard lock(virtualDeviceMapMutex_); - for (const auto &[id, virdevice] : virtualDeviceMap_) { - if (id == dhId) { - datas.insert(std::pair(virdevice->GetDeviceFd(), id)); - break; - } - } - } -} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp index 598f7a7..476caaf 100644 --- a/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp +++ b/services/source/inputinject/test/sourceinjectunittest/distributed_input_sourceinject_test.cpp @@ -287,33 +287,6 @@ HWTEST_F(DistributedInputSourceInjectTest, RegisterDistributedEvent04, testing:: EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); } -HWTEST_F(DistributedInputSourceInjectTest, GetDhIdsByInputType_001, testing::ext::TestSize.Level1) -{ - std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; - std::vector dhIds; - std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - dhIds.push_back(dhId); - std::shared_ptr rawEvent = nullptr; - DistributedInputInject::GetInstance().InputDeviceEventInject(rawEvent); - DistributedInputInject::GetInstance().inputNodeManager_ = std::make_unique(); - int32_t ret = DistributedInputInject::GetInstance().GetDhIdsByInputType(devId, - static_cast(DInputDeviceType::ALL), dhIds); - EXPECT_EQ(DH_SUCCESS, ret); -} - -HWTEST_F(DistributedInputSourceInjectTest, GetDhIdsByInputType_002, testing::ext::TestSize.Level1) -{ - DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; - std::shared_ptr rawEvent = std::make_shared(); - DistributedInputInject::GetInstance().InputDeviceEventInject(rawEvent); - - std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; - std::vector dhIds; - int32_t ret = DistributedInputInject::GetInstance().GetDhIdsByInputType(devId, - static_cast(DInputDeviceType::ALL), dhIds); - EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); -} - HWTEST_F(DistributedInputSourceInjectTest, GenerateVirtualTouchScreenDHId_001, testing::ext::TestSize.Level1) { std::string ret = DistributedInputInject::GetInstance().GenerateVirtualTouchScreenDHId(1, 1860, 980); @@ -337,8 +310,9 @@ HWTEST_F(DistributedInputSourceInjectTest, GetVirtualTouchScreenFd_001, testing: HWTEST_F(DistributedInputSourceInjectTest, RemoveVirtualTouchScreenNode_001, testing::ext::TestSize.Level1) { + std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(dhId); + int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(devId, dhId); EXPECT_EQ(DH_SUCCESS, ret); } @@ -353,9 +327,10 @@ HWTEST_F(DistributedInputSourceInjectTest, CreateVirtualTouchScreenNode_002, tes HWTEST_F(DistributedInputSourceInjectTest, RemoveVirtualTouchScreenNode_002, testing::ext::TestSize.Level1) { + std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; DistributedInputInject::GetInstance().inputNodeManager_ = nullptr; - int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(dhId); + int32_t ret = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode(devId, dhId); EXPECT_EQ(ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL, ret); } @@ -379,27 +354,12 @@ HWTEST_F(DistributedInputSourceInjectTest, CreateVirtualTouchScreenNode_003, tes HWTEST_F(DistributedInputSourceInjectTest, RemoveVirtualTouchScreenNode_003, testing::ext::TestSize.Level1) { + std::string devId = "umkyu1b165e1be98151891erbe8r91ev"; std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->RemoveVirtualTouchScreenNode(dhId); + int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->RemoveVirtualTouchScreenNode(devId, dhId); EXPECT_EQ(DH_SUCCESS, ret); } -HWTEST_F(DistributedInputSourceInjectTest, GetVirtualTouchScreenFd_003, testing::ext::TestSize.Level1) -{ - std::string networkId = "umkyu1b165e1be98151891erbe8r91ev"; - uint32_t inputTypes = 1; - std::map datas; - DistributedInputInject::GetInstance().inputNodeManager_->GetDevicesInfoByType(networkId, inputTypes, datas); - - inputTypes = 2; - DistributedInputInject::GetInstance().inputNodeManager_->GetDevicesInfoByType(networkId, inputTypes, datas); - - std::vector dhidsVec; - DistributedInputInject::GetInstance().inputNodeManager_->GetDevicesInfoByDhId(dhidsVec, datas); - int32_t ret = DistributedInputInject::GetInstance().inputNodeManager_->GetVirtualTouchScreenFd(); - EXPECT_NE(-1, ret); -} - HWTEST_F(DistributedInputSourceInjectTest, GetDevice_001, testing::ext::TestSize.Level1) { std::string dhId = "1ds56v18e1v21v8v1erv15r1v8r1j1ty8"; diff --git a/services/source/sourcemanager/src/dinput_source_listener.cpp b/services/source/sourcemanager/src/dinput_source_listener.cpp index 1553424..5aadc16 100644 --- a/services/source/sourcemanager/src/dinput_source_listener.cpp +++ b/services/source/sourcemanager/src/dinput_source_listener.cpp @@ -370,7 +370,7 @@ void DInputSourceListener::OnReceivedEventRemoteInput(const std::string deviceId ++idx; } - DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer); + DistributedInputInject::GetInstance().RegisterDistributedEvent(deviceId, mEventBuffer); } void DInputSourceListener::OnReceiveRelayPrepareResult(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 a0fbb8a..48f4c2d 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -302,7 +302,7 @@ int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::st callback->OnResult(devId, dhId, DH_SUCCESS); // 5. Notify node mgr to scan vir dev node info - DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(dhId); + DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(devId, dhId); return DH_SUCCESS; } @@ -1580,7 +1580,8 @@ void DistributedInputSourceManager::RunKeyStateCallback(const std::string &sinkI mEventBuffer.code = code; mEventBuffer.value = value; mEventBuffer.descriptor = dhId; - DistributedInputInject::GetInstance().RegisterDistributedEvent(&mEventBuffer, DINPUT_SOURCE_WRITE_EVENT_SIZE); + std::vector eventBuffers = {mEventBuffer}; + DistributedInputInject::GetInstance().RegisterDistributedEvent(sinkId, eventBuffers); return; } @@ -1801,7 +1802,7 @@ void DistributedInputSourceManager::StopDScreenListener::OnMessage(const DHTopic SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey); int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode( - srcScreenInfo.sourcePhyId); + sourceDevId, srcScreenInfo.sourcePhyId); if (removeNodeRes != DH_SUCCESS) { DHLOGE("Remove virtual touch screen node failed!"); return; @@ -1880,9 +1881,9 @@ void DistributedInputSourceManager::DScreenSourceSvrRecipient::OnRemoteDied(cons } std::string screenInfoKey = DInputContext::GetInstance().GetScreenInfoKey(srcDevId_, srcWinId_); SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(screenInfoKey); - + std::string sourceDevId = GetLocalNetworkId(); int32_t removeNodeRes = DistributedInputInject::GetInstance().RemoveVirtualTouchScreenNode( - srcScreenInfo.sourcePhyId); + sourceDevId, srcScreenInfo.sourcePhyId); if (removeNodeRes != DH_SUCCESS) { DHLOGE("Remove virtual touch screen node failed!"); return; diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 6441f19..88bcba6 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -139,7 +139,7 @@ private: std::vector SplitDhIdString(const std::string &dhIdsString); void RegRespFunMap(); - void ResetKeyboardKeyState(const std::vector &dhids); + void ResetKeyboardKeyState(const std::string &deviceId, const std::vector &dhids); private: std::mutex operationMutex_; std::set sessionIdSet_; diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index 5bd4c31..031f10c 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -326,7 +326,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInputDhids(int32_t srcTsrcSeI } DHLOGI("StopRemoteInputDhids srcTsrcSeId:%d, sinkSessionId:%d.", srcTsrcSeId, sinkSessionId); std::vector dhIdsVec = SplitDhIdString(dhids); - ResetKeyboardKeyState(dhIdsVec); + ResetKeyboardKeyState(deviceId, dhIdsVec); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID_FOR_REL; @@ -754,13 +754,13 @@ int32_t DistributedInputSourceTransport::StartRemoteInput(const std::string &dev return DH_SUCCESS; } -void DistributedInputSourceTransport::ResetKeyboardKeyState(const std::vector &dhids) +void DistributedInputSourceTransport::ResetKeyboardKeyState(const std::string &deviceId, + const std::vector &dhids) { std::vector keyboardNodePaths; - std::vector keyboardNodeDhIds; - DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); + DistributedInputInject::GetInstance().GetVirtualKeyboardPaths(deviceId, dhids, keyboardNodePaths); DHLOGI("Try reset keyboard states, dhIds: %s, nodePaths: %s", - GetString(keyboardNodeDhIds).c_str(), GetString(keyboardNodePaths).c_str()); + GetString(dhids).c_str(), GetString(keyboardNodePaths).c_str()); ResetVirtualDevicePressedKeys(keyboardNodePaths); } @@ -773,7 +773,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi return ERR_DH_INPUT_SERVER_SOURCE_TRANSPORT_STOP_FAIL; } DHLOGI("StopRemoteInput sessionId:%d.", sessionId); - ResetKeyboardKeyState(dhids); + ResetKeyboardKeyState(deviceId, dhids); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID;