mirror of
https://github.com/openharmony/distributedhardware_distributed_input.git
synced 2026-07-18 16:04:40 -04:00
@@ -44,8 +44,9 @@ 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),
|
||||
mPendingEventItems{}, pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), deviceChanged_(false),
|
||||
InputHub::InputHub(bool isPluginMonitor) : epollFd_(-1), iNotifyFd_(-1), inputWd_(-1),
|
||||
isPluginMonitor_(isPluginMonitor), needToScanDevices_(true), mPendingEventItems{},
|
||||
pendingEventCount_(0), pendingEventIndex_(0), pendingINotify_(false), deviceChanged_(false),
|
||||
inputTypes_(0), isStartCollectEvent_(false), isStartCollectHandler_(false)
|
||||
{
|
||||
Initialize();
|
||||
@@ -64,21 +65,25 @@ int32_t InputHub::Initialize()
|
||||
return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL;
|
||||
}
|
||||
|
||||
iNotifyFd_ = inotify_init();
|
||||
inputWd_ = inotify_add_watch(iNotifyFd_, DEVICE_PATH, IN_DELETE | IN_CREATE);
|
||||
if (inputWd_ < 0) {
|
||||
DHLOGE(
|
||||
"Could not register INotify for %s: %s", DEVICE_PATH, ConvertErrNo().c_str());
|
||||
return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL;
|
||||
}
|
||||
if (isPluginMonitor_) {
|
||||
DHLOGI("Init InputHub for device plugin monitor");
|
||||
iNotifyFd_ = inotify_init();
|
||||
inputWd_ = inotify_add_watch(iNotifyFd_, DEVICE_PATH, IN_DELETE | IN_CREATE);
|
||||
if (inputWd_ < 0) {
|
||||
DHLOGE("Could not register INotify for %s: %s", DEVICE_PATH, ConvertErrNo().c_str());
|
||||
return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL;
|
||||
}
|
||||
|
||||
struct epoll_event eventItem = {};
|
||||
eventItem.events = EPOLLIN;
|
||||
eventItem.data.fd = iNotifyFd_;
|
||||
int result = epoll_ctl(epollFd_, EPOLL_CTL_ADD, iNotifyFd_, &eventItem);
|
||||
if (result != 0) {
|
||||
DHLOGE("Could not add INotify to epoll instance. errno=%d", errno);
|
||||
return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL;
|
||||
struct epoll_event eventItem = {};
|
||||
eventItem.events = EPOLLIN;
|
||||
eventItem.data.fd = iNotifyFd_;
|
||||
int result = epoll_ctl(epollFd_, EPOLL_CTL_ADD, iNotifyFd_, &eventItem);
|
||||
if (result != 0) {
|
||||
DHLOGE("Could not add INotify to epoll instance. errno=%d", errno);
|
||||
return ERR_DH_INPUT_HUB_EPOLL_INIT_FAIL;
|
||||
}
|
||||
} else {
|
||||
DHLOGI("Init InputHub for read device events");
|
||||
}
|
||||
|
||||
return DH_SUCCESS;
|
||||
@@ -87,11 +92,22 @@ int32_t InputHub::Initialize()
|
||||
int32_t InputHub::Release()
|
||||
{
|
||||
CloseAllDevicesLocked();
|
||||
if (epollFd_ != -1) {
|
||||
::close(epollFd_);
|
||||
epollFd_ = -1;
|
||||
}
|
||||
|
||||
if (iNotifyFd_ != -1) {
|
||||
::close(iNotifyFd_);
|
||||
iNotifyFd_ = -1;
|
||||
}
|
||||
|
||||
if (isPluginMonitor_) {
|
||||
StopCollectInputHandler();
|
||||
} else {
|
||||
StopCollectInputEvents();
|
||||
}
|
||||
|
||||
::close(epollFd_);
|
||||
::close(iNotifyFd_);
|
||||
StopCollectInputEvents();
|
||||
StopCollectInputHandler();
|
||||
sharedDHIds_.clear();
|
||||
return DH_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
int32_t absYIndex;
|
||||
};
|
||||
|
||||
InputHub();
|
||||
explicit InputHub(bool isPluginMonitor);
|
||||
~InputHub();
|
||||
size_t StartCollectInputEvents(RawEvent *buffer, size_t bufferSize);
|
||||
size_t StartCollectInputHandler(InputDeviceEvent *buffer, size_t bufferSize);
|
||||
@@ -103,6 +103,11 @@ public:
|
||||
void SavePressedKeyState(const Device *dev, int32_t keyCode);
|
||||
void ClearDeviceStates();
|
||||
void ClearSkipDevicePaths();
|
||||
/*
|
||||
* Scan the input device node and save info.
|
||||
*/
|
||||
void ScanAndRecordInputDevices();
|
||||
|
||||
private:
|
||||
int32_t Initialize();
|
||||
int32_t Release();
|
||||
@@ -185,10 +190,6 @@ private:
|
||||
void MatchAndDealEvent(Device *device, const RawEvent &event);
|
||||
void DealTouchPadEvent(const RawEvent &event);
|
||||
void DealNormalKeyEvent(Device *device, const RawEvent &event);
|
||||
/*
|
||||
* Scan the input device node and save info.
|
||||
*/
|
||||
void ScanAndRecordInputDevices();
|
||||
|
||||
/*
|
||||
* Check is this node has been scaned for collecting info.
|
||||
@@ -209,6 +210,11 @@ private:
|
||||
int epollFd_;
|
||||
int iNotifyFd_;
|
||||
int inputWd_;
|
||||
/*
|
||||
* true: for just monitor device plugin/unplugin;
|
||||
* false: for read device events.
|
||||
*/
|
||||
bool isPluginMonitor_;
|
||||
|
||||
std::vector<std::unique_ptr<Device>> openingDevices_;
|
||||
std::vector<std::unique_ptr<Device>> closingDevices_;
|
||||
|
||||
@@ -44,7 +44,7 @@ IMPLEMENT_SINGLE_INSTANCE(DistributedInputHandler);
|
||||
DistributedInputHandler::DistributedInputHandler()
|
||||
: collectThreadID_(-1), isCollectingEvents_(false), isStartCollectEventThread(false)
|
||||
{
|
||||
inputHub_ = std::make_unique<InputHub>();
|
||||
inputHub_ = std::make_unique<InputHub>(true);
|
||||
this->m_listener = nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ HWTEST_F(DInputHandlerTest, FindDevicesInfoByType_001, testing::ext::TestSize.Le
|
||||
dInputHandler.NotifyHardWare(2);
|
||||
std::map<std::string, std::string> ret = dInputHandler.QueryExtraInfo();
|
||||
EXPECT_EQ(0, ret.size());
|
||||
dInputHandler.inputHub_ = std::make_unique<InputHub>();
|
||||
dInputHandler.inputHub_ = std::make_unique<InputHub>(true);
|
||||
dInputHandler.StartInputMonitorDeviceThread();
|
||||
}
|
||||
} // namespace DistributedInput
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace DistributedInput {
|
||||
DistributedInputCollector::DistributedInputCollector() : mEventBuffer{}, collectThreadID_(-1),
|
||||
isCollectingEvents_(false), isStartGetDeviceHandlerThread(false), inputTypes_(0)
|
||||
{
|
||||
inputHub_ = std::make_unique<InputHub>();
|
||||
inputHub_ = std::make_unique<InputHub>(false);
|
||||
}
|
||||
|
||||
DistributedInputCollector::~DistributedInputCollector()
|
||||
@@ -265,6 +265,7 @@ void DistributedInputCollector::ClearSkipDevicePaths()
|
||||
return;
|
||||
}
|
||||
inputHub_->ClearSkipDevicePaths();
|
||||
inputHub_->ScanAndRecordInputDevices();
|
||||
}
|
||||
} // namespace DistributedInput
|
||||
} // namespace DistributedHardware
|
||||
|
||||
+1
-1
@@ -97,7 +97,7 @@ HWTEST_F(DistributedInputCollectorTest, IsAllDevicesStoped02, testing::ext::Test
|
||||
|
||||
HWTEST_F(DistributedInputCollectorTest, SetSharingTypes01, testing::ext::TestSize.Level1)
|
||||
{
|
||||
DistributedInputCollector::GetInstance().inputHub_ = std::make_unique<InputHub>();
|
||||
DistributedInputCollector::GetInstance().inputHub_ = std::make_unique<InputHub>(false);
|
||||
bool enabled = true;
|
||||
uint32_t inputType = static_cast<uint32_t>(DInputDeviceType::ALL);
|
||||
|
||||
|
||||
@@ -124,7 +124,6 @@ private:
|
||||
std::mutex injectThreadMutex_;
|
||||
std::condition_variable conditionVariable_;
|
||||
std::queue<EventBatch> injectQueue_;
|
||||
std::unique_ptr<InputHub> inputHub_;
|
||||
int32_t virtualTouchScreenFd_;
|
||||
std::once_flag callOnceFlag_;
|
||||
std::shared_ptr<DInputNodeManagerEventHandler> callBackHandler_;
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace OHOS {
|
||||
namespace DistributedHardware {
|
||||
namespace DistributedInput {
|
||||
DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false),
|
||||
isInjectThreadRunning_(false), inputHub_(std::make_unique<InputHub>()), virtualTouchScreenFd_(UN_INIT_FD_VALUE)
|
||||
isInjectThreadRunning_(false), virtualTouchScreenFd_(UN_INIT_FD_VALUE)
|
||||
{
|
||||
DHLOGI("DistributedInputNodeManager ctor");
|
||||
std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create(true);
|
||||
@@ -345,7 +345,6 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice
|
||||
const std::string &dhId)
|
||||
{
|
||||
std::unique_lock<std::mutex> my_lock(operationMutex_);
|
||||
std::call_once(callOnceFlag_, [this]() { inputHub_->ScanInputDevices(DEVICE_PATH); });
|
||||
std::unique_ptr<VirtualDevice> virtualDevice = std::make_unique<VirtualDevice>(inputDevice);
|
||||
|
||||
virtualDevice->SetNetWorkId(devId);
|
||||
|
||||
@@ -305,12 +305,12 @@ int OpenInputDeviceFdByPath(const std::string &devicePath)
|
||||
DHLOGI("path: %s is a dir.", devicePath.c_str());
|
||||
return -1;
|
||||
}
|
||||
int fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK);
|
||||
int fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC);
|
||||
int32_t count = 0;
|
||||
while ((fd < 0) && (count < MAX_RETRY_COUNT)) {
|
||||
++count;
|
||||
usleep(SLEEP_TIME_US);
|
||||
fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC | O_NONBLOCK);
|
||||
fd = open(canonicalDevicePath, O_RDWR | O_CLOEXEC);
|
||||
DHLOGE("could not open the path: %s, errno: %s; retry: %d", devicePath.c_str(), ConvertErrNo().c_str(), count);
|
||||
}
|
||||
if (count >= MAX_RETRY_COUNT) {
|
||||
|
||||
Reference in New Issue
Block a user