长按键盘对端事件注入

Signed-off-by: wuqi0105 <wuqi100@h-partners.com>
This commit is contained in:
wuqi0105
2023-09-21 11:25:04 +08:00
parent b305d95d97
commit e41fae1c22
9 changed files with 107 additions and 19 deletions
+12
View File
@@ -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
@@ -55,6 +55,8 @@ public:
const std::string &sinkNodeDesc);
void GetVirtualKeyboardPathsByDhIds(const std::vector<std::string> &dhIds,
std::vector<std::string> &shareDhidsPaths, std::vector<std::string> &shareDhIds);
void UpdateSpecEventFirstStatus(bool status);
void UpdateSpecEventState(DhidState state);
private:
DistributedInputInject();
@@ -61,6 +61,9 @@ 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);
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<DInputNodeManagerEventHandler> callBackHandler_;
std::atomic<bool> isFirst_;
DhidState specEventState_;
};
} // namespace DistributedInput
} // namespace DistributedHardware
@@ -276,6 +276,26 @@ void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId)
}
inputNodeManager_->NotifyNodeMgrScanVirNode(dhId);
}
void DistributedInputInject::UpdateSpecEventFirstStatus(bool status)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return;
}
inputNodeManager_->UpdateSpecEventFirstStatus(status);
}
void DistributedInputInject::UpdateSpecEventState(DhidState state)
{
std::lock_guard<std::mutex> lock(inputNodeManagerMutex_);
if (inputNodeManager_ == nullptr) {
DHLOGE("inputNodeManager is nullptr");
return;
}
inputNodeManager_->UpdateSpecEventState(state);
}
} // namespace DistributedInput
} // namespace DistributedHardware
} // namespace OHOS
@@ -32,7 +32,8 @@ namespace OHOS {
namespace DistributedHardware {
namespace DistributedInput {
DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false),
isInjectThreadRunning_(false), inputHub_(std::make_unique<InputHub>()), virtualTouchScreenFd_(UN_INIT_FD_VALUE)
isInjectThreadRunning_(false), inputHub_(std::make_unique<InputHub>()), virtualTouchScreenFd_(UN_INIT_FD_VALUE),
isFirst_(false), specEventState_(DhidState::THROUGH_OUT)
{
DHLOGI("DistributedInputNodeManager ctor");
std::shared_ptr<AppExecFwk::EventRunner> 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 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> &rawEvent)
{
std::string dhId = rawEvent->descriptor;
@@ -445,14 +470,33 @@ void DistributedInputNodeManager::ProcessInjectEvent(const std::shared_ptr<RawEv
};
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);
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)
@@ -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);
@@ -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<uint32_t>(DInputDeviceType::NONE)) {
+1 -10
View File
@@ -20,20 +20,11 @@
#include <mutex>
#include <string>
#include <linux/input.h>
#include "constants_dinput.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 {
public:
static DInputState &GetInstance()
+7 -1
View File
@@ -63,6 +63,13 @@ int32_t DInputState::RecordDhids(const std::vector<std::string> &dhids, DhidStat
if (state == DhidState::THROUGH_OUT) {
CreateSpecialEventInjectThread(sessionId, dhids);
DistributedInputInject::GetInstance().UpdateSpecEventFirstStatus(false);
DistributedInputInject::GetInstance().UpdateSpecEventState(state);
}
if (state == DhidState::THROUGH_IN) {
DistributedInputInject::GetInstance().UpdateSpecEventFirstStatus(false);
DistributedInputInject::GetInstance().UpdateSpecEventState(state);
}
return DH_SUCCESS;
}
@@ -141,7 +148,6 @@ void DInputState::SpecEventInject(const int32_t &sessionId, std::vector<std::str
// keyboard up event inject local device
std::vector<std::string> keyboardNodePaths;
std::vector<std::string> keyboardNodeDhIds;
DistributedInputCollector::GetInstance().GetShareKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds);
DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds);
size_t len = keyboardNodePaths.size();
for (size_t i = 0; i < len; ++i) {