diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index ef403e11f80231779dbe397e70dcf4feefc45d4a..5a756516993d37174099e5e75db4bf0f03347e60 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -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; } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index a12e2309b02bbf1b54e94837f98ddebc32c947e2..4d31827906c03a8eab3321a18a15300f6a35e6f2 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -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> openingDevices_; std::vector> closingDevices_; diff --git a/inputdevicehandler/src/distributed_input_handler.cpp b/inputdevicehandler/src/distributed_input_handler.cpp index c99ff35a03c8c3b0e07a7e369a5d0798ee04d4d5..6f2e6c47e2c78e420f4dd6c1f95ff7d1d8d69e00 100644 --- a/inputdevicehandler/src/distributed_input_handler.cpp +++ b/inputdevicehandler/src/distributed_input_handler.cpp @@ -44,7 +44,7 @@ IMPLEMENT_SINGLE_INSTANCE(DistributedInputHandler); DistributedInputHandler::DistributedInputHandler() : collectThreadID_(-1), isCollectingEvents_(false), isStartCollectEventThread(false) { - inputHub_ = std::make_unique(); + inputHub_ = std::make_unique(true); this->m_listener = nullptr; } diff --git a/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp b/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp index bcf713bcab6e6372656a69be4f1edcbf4e8f1dc4..47765dc141da3858fe673b52bf711a502c13bd22 100644 --- a/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp +++ b/inputdevicehandler/test/inputhandlertest/distributed_input_handler_test.cpp @@ -75,7 +75,7 @@ HWTEST_F(DInputHandlerTest, FindDevicesInfoByType_001, testing::ext::TestSize.Le dInputHandler.NotifyHardWare(2); std::map ret = dInputHandler.QueryExtraInfo(); EXPECT_EQ(0, ret.size()); - dInputHandler.inputHub_ = std::make_unique(); + dInputHandler.inputHub_ = std::make_unique(true); dInputHandler.StartInputMonitorDeviceThread(); } } // namespace DistributedInput diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 27c80938fa0f38e5527cf41bfdbc706cc2a2e69c..48e05d95efa7d266e5aa183ad474681e9848d4c2 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -39,7 +39,7 @@ namespace DistributedInput { DistributedInputCollector::DistributedInputCollector() : mEventBuffer{}, collectThreadID_(-1), isCollectingEvents_(false), isStartGetDeviceHandlerThread(false), inputTypes_(0) { - inputHub_ = std::make_unique(); + inputHub_ = std::make_unique(false); } DistributedInputCollector::~DistributedInputCollector() @@ -265,6 +265,7 @@ void DistributedInputCollector::ClearSkipDevicePaths() return; } inputHub_->ClearSkipDevicePaths(); + inputHub_->ScanAndRecordInputDevices(); } } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp b/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp index 0c2d5effa22f14116fcead9fe6f6ef5fcb33f2ff..30a46bd38a763cfeb6211a1ca81bdaaeb694d570 100644 --- a/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp +++ b/services/sink/inputcollector/test/sinkcollectorunittest/distributed_input_collector_test.cpp @@ -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(); + DistributedInputCollector::GetInstance().inputHub_ = std::make_unique(false); bool enabled = true; uint32_t inputType = static_cast(DInputDeviceType::ALL); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index ace7b0ee437f9d7228a5f00b816458dbb652c64e..0dc111b794f72b414e946c43d6df1d9f417dd64e 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -124,7 +124,6 @@ private: std::mutex injectThreadMutex_; std::condition_variable conditionVariable_; std::queue injectQueue_; - std::unique_ptr inputHub_; int32_t virtualTouchScreenFd_; std::once_flag callOnceFlag_; std::shared_ptr callBackHandler_; diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 8b3954cd89d0380c045aabbad46a59746a90c7ab..25e589e721ba9fa4975a0f26f1d189227a16d3c8 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -32,7 +32,7 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { DistributedInputNodeManager::DistributedInputNodeManager() : isInjectThreadCreated_(false), - isInjectThreadRunning_(false), inputHub_(std::make_unique()), virtualTouchScreenFd_(UN_INIT_FD_VALUE) + isInjectThreadRunning_(false), virtualTouchScreenFd_(UN_INIT_FD_VALUE) { DHLOGI("DistributedInputNodeManager ctor"); std::shared_ptr runner = AppExecFwk::EventRunner::Create(true); @@ -345,7 +345,6 @@ int32_t DistributedInputNodeManager::CreateHandle(const InputDevice &inputDevice const std::string &dhId) { std::unique_lock my_lock(operationMutex_); - std::call_once(callOnceFlag_, [this]() { inputHub_->ScanInputDevices(DEVICE_PATH); }); std::unique_ptr virtualDevice = std::make_unique(inputDevice); virtualDevice->SetNetWorkId(devId); diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index b6eb6e57dff6f7dc6d779e5b536aa02e53d85a5a..a457c4fc5a2dd32f8fb4bb2b679ba2eff2dca7dd 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -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) {