mirror of
https://github.com/openharmony/distributedhardware_distributed_input.git
synced 2026-07-19 17:43:35 -04:00
@@ -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",
|
||||
|
||||
@@ -59,6 +59,8 @@ public:
|
||||
void GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
|
||||
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds);
|
||||
|
||||
void NotifyNodeMgrScanVirNode(const std::string &dhId);
|
||||
|
||||
private:
|
||||
DistributedInputInject();
|
||||
~DistributedInputInject();
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#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<std::string> &dhIds,
|
||||
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds);
|
||||
void NotifyNodeMgrScanVirNode(const std::string &dhId);
|
||||
|
||||
class DInputNodeManagerEventHandler : public AppExecFwk::EventHandler {
|
||||
public:
|
||||
DInputNodeManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &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<int32_t, nodeMgrFunc> eventFuncMap_;
|
||||
DistributedInputNodeManager *nodeManagerObj_;
|
||||
};
|
||||
|
||||
private:
|
||||
void AddDeviceLocked(const std::string& dhId, std::unique_ptr<VirtualDevice> 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> inputHub_;
|
||||
int32_t virtualTouchScreenFd_;
|
||||
std::once_flag callOnceFlag_;
|
||||
std::shared_ptr<DInputNodeManagerEventHandler> callBackHandler_;
|
||||
};
|
||||
} // namespace DistributedInput
|
||||
} // namespace DistributedHardware
|
||||
|
||||
@@ -259,12 +259,23 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd()
|
||||
void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
|
||||
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<std::mutex> lock(inputNodeManagerMutex_);
|
||||
if (inputNodeManager_ == nullptr) {
|
||||
DHLOGE("inputNodeManager is nullptr");
|
||||
return;
|
||||
}
|
||||
inputNodeManager_->NotifyNodeMgrScanVirNode(dhId);
|
||||
}
|
||||
} // namespace DistributedInput
|
||||
} // namespace DistributedHardware
|
||||
} // namespace OHOS
|
||||
@@ -34,11 +34,14 @@ namespace DistributedInput {
|
||||
DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false),
|
||||
isInjectThreadRunning_(false), inputHub_(std::make_unique<InputHub>()), virtualTouchScreenFd_(UN_INIT_FD_VALUE)
|
||||
{
|
||||
DHLOGI("DistributedInputNodeManager ctor");
|
||||
std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
|
||||
callBackHandler_ = std::make_shared<DistributedInputNodeManager::DInputNodeManagerEventHandler>(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<std::string> 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<AppExecFwk::EventRunner> &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<nlohmann::json> dataMsg = event->GetSharedObject<nlohmann::json>();
|
||||
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<nlohmann::json> jsonArrayMsg = std::make_shared<nlohmann::json>();
|
||||
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<std::mutex> 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<std::string> &dhIds,
|
||||
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(virtualDeviceMapMutex_);
|
||||
for (auto dhId_ : dhIds) {
|
||||
auto iter = virtualDeviceMap_.begin();
|
||||
while (iter != virtualDeviceMap_.end()) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user