Files
distributedhardware_distrib…/services/source/inputinject/src/distributed_input_inject.cpp
T
hwzhangchuang d3c3fc9fdf add
Signed-off-by: hwzhangchuang <zhangchuang.zhang@huawei.com>
2023-11-28 17:56:20 +08:00

250 lines
8.7 KiB
C++

/*
* Copyright (c) 2021-2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "distributed_input_inject.h"
#include <sstream>
#include "nlohmann/json.hpp"
#include "dinput_errcode.h"
#include "dinput_log.h"
#include "dinput_utils_tool.h"
namespace OHOS {
namespace DistributedHardware {
namespace DistributedInput {
DistributedInputInject::DistributedInputInject()
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
inputNodeManager_ = std::make_unique<DistributedInputNodeManager>();
}
DistributedInputInject::~DistributedInputInject()
{
DHLOGI("~DistributedInputInject");
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
inputNodeManager_.reset();
inputNodeManager_ = nullptr;
}
DistributedInputInject &DistributedInputInject::GetInstance()
{
static DistributedInputInject instance;
return instance;
}
int32_t DistributedInputInject::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
const std::string &parameters)
{
DHLOGI("RegisterDistributedHardware called, deviceId: %s, dhId: %s, parameters: %s",
GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str(), SetAnonyId(parameters).c_str());
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("the DistributedInputNodeManager is null\n");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
if (inputNodeManager_->OpenDevicesNode(devId, dhId, parameters) < 0) {
DHLOGE("create virtual device error\n");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_REGISTER_FAIL;
}
DHLOGI("RegisterDistributedHardware success");
return DH_SUCCESS;
}
int32_t DistributedInputInject::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId)
{
DHLOGI("UnregisterDistributedHardware called, deviceId: %s, dhId: %s",
GetAnonyString(devId).c_str(), GetAnonyString(dhId).c_str());
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("the DistributedInputNodeManager is null\n");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
if (inputNodeManager_->CloseDeviceLocked(dhId) < 0) {
DHLOGE("delete virtual device error\n");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_UNREGISTER_FAIL;
}
DHLOGI("UnregisterDistributedHardware success");
return DH_SUCCESS;
}
int32_t DistributedInputInject::GetDhIdsByInputType(const std::string &devId, const uint32_t &inputTypes,
std::vector<std::string> &dhIds)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("the inputNodeListener is nullptr");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
std::map<int32_t, std::string> 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,
GetAnonyString(pBuf.descriptor).c_str());
nlohmann::json tmpJson;
tmpJson[DEVICE_NAME] = pBuf.name;
tmpJson[PHYSICAL_PATH] = pBuf.physicalPath;
tmpJson[UNIQUE_ID] = pBuf.uniqueId;
tmpJson[BUS] = pBuf.bus;
tmpJson[VENDOR] = pBuf.vendor;
tmpJson[PRODUCT] = pBuf.product;
tmpJson[VERSION] = pBuf.version;
tmpJson[DESCRIPTOR] = pBuf.descriptor;
tmpJson[CLASSES] = pBuf.classes;
tmpJson[EVENT_TYPES] = pBuf.eventTypes;
tmpJson[EVENT_KEYS] = pBuf.eventKeys;
tmpJson[ABS_TYPES] = pBuf.absTypes;
tmpJson[ABS_INFOS] = pBuf.absInfos;
tmpJson[REL_TYPES] = pBuf.relTypes;
tmpJson[PROPERTIES] = pBuf.properties;
std::ostringstream stream;
stream << tmpJson.dump();
strDescriptor = stream.str();
return DH_SUCCESS;
}
void DistributedInputInject::InputDeviceEventInject(const std::shared_ptr<RawEvent> &rawEvent)
{
std::lock_guard<std::mutex> 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)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("the DistributedInputNodeManager is null");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
DHLOGI("RegisterDistributedEvent start %zu\n", bufferSize);
for (size_t i = 0; i < bufferSize; i++) {
inputNodeManager_->ReportEvent(buffer[i]);
}
return DH_SUCCESS;
}
void DistributedInputInject::StartInjectThread()
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ != nullptr) {
inputNodeManager_->StartInjectThread();
}
}
void DistributedInputInject::StopInjectThread()
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ != nullptr) {
inputNodeManager_->StopInjectThread();
}
}
std::string DistributedInputInject::GenerateVirtualTouchScreenDHId(const uint64_t sourceWinId,
const uint32_t sourceWinWidth, const uint32_t sourceWinHeight)
{
std::string uniqueInfo = GetLocalNetworkId() + std::to_string(sourceWinId) +
std::to_string(sourceWinWidth) + std::to_string(sourceWinHeight);
return DH_ID_PREFIX + Sha256(uniqueInfo);
}
int32_t DistributedInputInject::CreateVirtualTouchScreenNode(const std::string &devId, const std::string &dhId,
const uint64_t srcWinId, const uint32_t sourcePhyWidth, const uint32_t sourcePhyHeight)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
return inputNodeManager_->CreateVirtualTouchScreenNode(devId, dhId, srcWinId, sourcePhyWidth, sourcePhyHeight);
}
int32_t DistributedInputInject::RemoveVirtualTouchScreenNode(const std::string &dhId)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL;
}
return inputNodeManager_->RemoveVirtualTouchScreenNode(dhId);
}
int32_t DistributedInputInject::GetVirtualTouchScreenFd()
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return UN_INIT_FD_VALUE;
}
return inputNodeManager_->GetVirtualTouchScreenFd();
}
int32_t DistributedInputInject::RegisterInjectEventCb(sptr<ISessionStateCallback> callback)
{
if (callback == nullptr) {
DHLOGE("RegisterInjectEventCb callback is null.");
return ERR_DH_INPUT_SERVER_SOURCE_MANAGER_INJECT_EVENT_CB_IS_NULL;
}
inputNodeManager_->RegisterInjectEventCb(callback);
return DH_SUCCESS;
}
int32_t DistributedInputInject::UnregisterInjectEventCb()
{
inputNodeManager_->UnregisterInjectEventCb();
return DH_SUCCESS;
}
void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
std::vector<std::string> &virKeyboardPaths, std::vector<std::string> &virKeyboardDhIds)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return;
}
inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, virKeyboardPaths, virKeyboardDhIds);
}
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