diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 07b4208..0c69735 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -56,6 +56,7 @@ ohos_shared_library("libdinput_inject") { external_deps = [ "c_utils:utils", "dsoftbus:softbus_client", + "eventhandler:libeventhandler", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index d1d293f..68d556e 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -59,6 +59,8 @@ public: void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); + void NotifyNodeMgrScanVirNode(const std::string &dhId); + 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 e38def6..c900322 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -24,6 +24,7 @@ #include #include +#include "event_handler.h" #include "nlohmann/json.hpp" #include "constants_dinput.h" @@ -33,6 +34,8 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +const uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1; +const std::string INPUT_NODE_DHID = "dhId"; class DistributedInputNodeManager { public: DistributedInputNodeManager(); @@ -57,6 +60,24 @@ public: void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds); + void NotifyNodeMgrScanVirNode(const std::string &dhId); + + class DInputNodeManagerEventHandler : public AppExecFwk::EventHandler { + public: + DInputNodeManagerEventHandler(const std::shared_ptr &runner, + DistributedInputNodeManager *manager); + ~DInputNodeManagerEventHandler() override; + + void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; + private: + void ScanAllNode(const AppExecFwk::InnerEvent::Pointer &event); + + using nodeMgrFunc = void (DInputNodeManagerEventHandler::*)( + const AppExecFwk::InnerEvent::Pointer &event); + std::map eventFuncMap_; + DistributedInputNodeManager *nodeManagerObj_; + }; + private: void AddDeviceLocked(const std::string& dhId, std::unique_ptr device); int32_t CreateHandle(const InputDevice& inputDevice, const std::string& devId, const std::string& dhId); @@ -64,8 +85,8 @@ private: void VerifyInputDevice(const nlohmann::json& inputDeviceJson, InputDevice& pBuf); void InjectEvent(); - void ScanSinkInputDevices(const std::string& dirName); - void OpenInputDevice(const std::string& devicePath); + void ScanSinkInputDevices(const std::string& dhId); + void OpenInputDevice(const std::string& devicePath, const std::string& dhId); bool IsVirtualDev(int fd); bool GetDevDhIdByFd(int fd, std::string& dhId, std::string& physicalPath); void SetPathForDevMap(std::string& dhId, const std::string& devicePath); @@ -83,6 +104,7 @@ private: std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; + std::shared_ptr callBackHandler_; }; } // 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 1de63d9..010d6d6 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -259,12 +259,23 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { + std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds); } + +void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("inputNodeManager is nullptr"); + return; + } + inputNodeManager_->NotifyNodeMgrScanVirNode(dhId); +} } // 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 d90535e..47fcdac 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -34,11 +34,14 @@ namespace DistributedInput { DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { + DHLOGI("DistributedInputNodeManager ctor"); + std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); + callBackHandler_ = std::make_shared(runner, this); } DistributedInputNodeManager::~DistributedInputNodeManager() { - DHLOGI("destructor start"); + DHLOGI("DistributedInputNodeManager dtor"); isInjectThreadCreated_.store(false); isInjectThreadRunning_.store(false); if (eventInjectThread_.joinable()) { @@ -65,7 +68,6 @@ int32_t DistributedInputNodeManager::OpenDevicesNode(const std::string& devId, c DHLOGE("Can not create virtual node!"); return ERR_DH_INPUT_SERVER_SOURCE_OPEN_DEVICE_NODE_FAIL; } - ScanSinkInputDevices(DEVICE_PATH); return DH_SUCCESS; } @@ -128,16 +130,67 @@ void DistributedInputNodeManager::VerifyInputDevice(const nlohmann::json& inputD } } -void DistributedInputNodeManager::ScanSinkInputDevices(const std::string& dirName) +void DistributedInputNodeManager::ScanSinkInputDevices(const std::string& dhId) { - DHLOGI("ScanSinkInputDevices enter, dirName %s.", dirName.c_str()); + DHLOGI("ScanSinkInputDevices enter, dhId %s.", dhId.c_str()); std::vector vecInputDevPath; - ScanInputDevicesPath(dirName, vecInputDevPath); + ScanInputDevicesPath(DEVICE_PATH, vecInputDevPath); for (auto &tempPath: vecInputDevPath) { - OpenInputDevice(tempPath); + OpenInputDevice(tempPath, dhId); } } +void DistributedInputNodeManager::DInputNodeManagerEventHandler::ProcessEvent( + const AppExecFwk::InnerEvent::Pointer &event) +{ + DHLOGI("ProcessEvent enter."); + auto iter = eventFuncMap_.find(event->GetInnerEventId()); + if (iter == eventFuncMap_.end()) { + DHLOGE("Event Id %d is undefined.", event->GetInnerEventId()); + return; + } + nodeMgrFunc &func = iter->second; + (this->*func)(event); +} + +DistributedInputNodeManager::DInputNodeManagerEventHandler::DInputNodeManagerEventHandler( + const std::shared_ptr &runner, DistributedInputNodeManager *manager) + : AppExecFwk::EventHandler(runner) +{ + eventFuncMap_[DINPUT_NODE_MANAGER_SCAN_ALL_NODE] = &DInputNodeManagerEventHandler::ScanAllNode; + + nodeManagerObj_ = manager; +} + +DistributedInputNodeManager::DInputNodeManagerEventHandler::~DInputNodeManagerEventHandler() +{ + eventFuncMap_.clear(); + nodeManagerObj_ = nullptr; +} + +void DistributedInputNodeManager::DInputNodeManagerEventHandler::ScanAllNode( + const AppExecFwk::InnerEvent::Pointer &event) +{ + DHLOGI("ScanAllNode enter."); + std::shared_ptr dataMsg = event->GetSharedObject(); + auto it = dataMsg->begin(); + nlohmann::json innerMsg = *(it); + std::string devicedhId = innerMsg[INPUT_NODE_DHID]; + nodeManagerObj_->ScanSinkInputDevices(devicedhId); +} + +void DistributedInputNodeManager::NotifyNodeMgrScanVirNode(const std::string &dhId) +{ + DHLOGI("NotifyNodeMgrScanVirNode enter."); + std::shared_ptr jsonArrayMsg = std::make_shared(); + nlohmann::json tmpJson; + tmpJson[INPUT_NODE_DHID] = dhId; + jsonArrayMsg->push_back(tmpJson); + AppExecFwk::InnerEvent::Pointer msgEvent = AppExecFwk::InnerEvent::Get( + DINPUT_NODE_MANAGER_SCAN_ALL_NODE, jsonArrayMsg, 0); + callBackHandler_->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); +} + bool DistributedInputNodeManager::IsVirtualDev(int fd) { char buffer[256] = {0}; @@ -179,6 +232,7 @@ bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string& dhId, std: void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, 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()); @@ -191,30 +245,32 @@ void DistributedInputNodeManager::SetPathForDevMap(std::string& dhId, const std: } } -void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath) +void DistributedInputNodeManager::OpenInputDevice(const std::string& devicePath, const std::string& dhId) { DHLOGI("Opening input device path: %s", devicePath.c_str()); - std::string dhId; + std::string curDhId; std::string physicalPath; int fd = OpenInputDeviceFdByPath(devicePath); if (fd == -1) { DHLOGE("The fd open failed, devicePath %s.", devicePath.c_str()); return; } - if (IsVirtualDev(fd) == false) { + if (!IsVirtualDev(fd)) { DHLOGE("The dev not virtual, devicePath %s.", devicePath.c_str()); return; } - if (GetDevDhIdByFd(fd, dhId, physicalPath) == false) { - DHLOGE("Get dev dhid failed, devicePath %s.", devicePath.c_str()); + if (!GetDevDhIdByFd(fd, curDhId, physicalPath) || dhId != curDhId) { + DHLOGE("This is not same dev, curDhId %s.", devicePath.c_str()); return; } - SetPathForDevMap(dhId, devicePath); + DHLOGI("curDhId %s.", GetAnonyString(curDhId).c_str()); + SetPathForDevMap(curDhId, devicePath); } void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, std::vector &shareDhidsPaths, std::vector &shareDhIds) { + std::lock_guard lock(virtualDeviceMapMutex_); for (auto dhId_ : dhIds) { auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index 6c9c46b..4070002 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -1132,6 +1132,9 @@ int32_t DistributedInputSourceManager::RegisterDistributedHardware(const std::st } cli->SyncNodeInfoRemoteInput(GetLocalNetworkId(), dhId, GetNodeDesc(parameters)); + + // 6. Notify node mgr to scan vir dev node info + DistributedInputInject::GetInstance().NotifyNodeMgrScanVirNode(dhId); return DH_SUCCESS; } diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index f17f0a3..1ecdbfa 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -209,7 +209,7 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), GetAnonyString(mouseNodeDhId).c_str(), sessionId); char canonicalPath[PATH_MAX] = {0x00}; - if (mouseNodePath.length() == 0 || mouseNodePath.length() > PATH_MAX || + if (mouseNodePath.length() == 0 || mouseNodePath.length() >= PATH_MAX || realpath(mouseNodePath.c_str(), canonicalPath) == nullptr) { DHLOGE("mouse Nodepath check fail, error path: %s", mouseNodePath.c_str()); return;