diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 80b3c54..b6fb28e 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -1016,7 +1016,7 @@ void InputHub::GetShareMousePathByDhId(std::vector dhIds, std::stri } } -void InputHub::GetDevicesInfoByType(int32_t inputTypes, std::map &datas) +void InputHub::GetDevicesInfoByType(const uint32_t inputTypes, std::map &datas) { uint32_t input_types_ = 0; diff --git a/common/include/input_hub.h b/common/include/input_hub.h index bcc162e..cef98f8 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -51,7 +51,7 @@ public: // return efftive dhids AffectDhIds SetSharingDevices(bool enabled, std::vector dhIds); void GetDeviceDhIdByFd(int32_t fd, std::string &dhId); - void GetDevicesInfoByType(int32_t inputTypes, std::map &datas); + void GetDevicesInfoByType(const uint32_t inputTypes, std::map &datas); void GetDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); void GetShareMousePathByDhId(std::vector dhIds, std::string &path, std::string &dhId); bool IsAllDevicesStoped(); diff --git a/inputdevicehandler/include/distributed_input_handler.h b/inputdevicehandler/include/distributed_input_handler.h index 9af7833..a8a49e8 100644 --- a/inputdevicehandler/include/distributed_input_handler.h +++ b/inputdevicehandler/include/distributed_input_handler.h @@ -47,7 +47,7 @@ public: API_EXPORT virtual void RegisterPluginListener(std::shared_ptr listener) override; API_EXPORT virtual void UnRegisterPluginListener() override; - API_EXPORT void FindDevicesInfoByType(int32_t inputTypes, std::map &datas); + API_EXPORT void FindDevicesInfoByType(const uint32_t inputTypes, std::map &datas); API_EXPORT void FindDevicesInfoByDhId(std::vector dhidsVec, std::map &datas); private: DistributedInputHandler(); diff --git a/inputdevicehandler/src/distributed_input_handler.cpp b/inputdevicehandler/src/distributed_input_handler.cpp index 5482625..981d15b 100644 --- a/inputdevicehandler/src/distributed_input_handler.cpp +++ b/inputdevicehandler/src/distributed_input_handler.cpp @@ -82,7 +82,7 @@ int32_t DistributedInputHandler::Initialize() return DH_SUCCESS; } -void DistributedInputHandler::FindDevicesInfoByType(int32_t inputTypes, std::map &datas) +void DistributedInputHandler::FindDevicesInfoByType(const uint32_t inputTypes, std::map &datas) { if (inputHub_ != nullptr) { inputHub_->GetDevicesInfoByType(inputTypes, datas); diff --git a/services/sink/inputcollector/include/distributed_input_collector.h b/services/sink/inputcollector/include/distributed_input_collector.h index 5ca4554..6cad8f6 100644 --- a/services/sink/inputcollector/include/distributed_input_collector.h +++ b/services/sink/inputcollector/include/distributed_input_collector.h @@ -47,6 +47,7 @@ public: bool IsAllDevicesStoped(); int32_t RegisterSharingDhIdListener(sptr sharingDhIdListener); void ReportDhIdSharingState(const AffectDhIds &dhIds); + void GetDeviceInfoByType(const uint32_t inputTypes, std::map& deviceInfo); private: DistributedInputCollector(); diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 7701de2..9416f6f 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -97,6 +97,10 @@ void DistributedInputCollector::StartCollectEventsThread() { while (isCollectingEvents_) { memset_s(&mEventBuffer, sizeof(mEventBuffer), 0, sizeof(mEventBuffer)); + if (inputHub_ == nullptr) { + DHLOGI("inputHub is nullptr!"); + return; + } size_t count = inputHub_->StartCollectInputEvents(mEventBuffer, INPUT_EVENT_BUFFER_SIZE); if (count > 0) { DHLOGI("Count: %zu", count); @@ -131,6 +135,10 @@ void DistributedInputCollector::StopCollectEventsThread() { isCollectingEvents_ = false; isStartGetDeviceHandlerThread = false; + if (inputHub_ == nullptr) { + DHLOGI("inputHub is nullptr!"); + return; + } inputHub_->StopCollectInputEvents(); if (collectThreadID_ != (pthread_t)(-1)) { DHLOGI("DistributedInputCollector::Wait collect thread exit"); @@ -193,11 +201,19 @@ AffectDhIds DistributedInputCollector::SetSharingDhIds(bool enabled, std::vector void DistributedInputCollector::GetMouseNodePath( std::vector dhIds, std::string &mouseNodePath, std::string &dhid) { + if (inputHub_ == nullptr) { + DHLOGI("inputHub is nullptr!"); + return; + } inputHub_->GetShareMousePathByDhId(dhIds, mouseNodePath, dhid); } bool DistributedInputCollector::IsAllDevicesStoped() { + if (inputHub_ == nullptr) { + DHLOGI("inputHub is nullptr!"); + return false; + } return inputHub_->IsAllDevicesStoped(); } @@ -208,6 +224,16 @@ int32_t DistributedInputCollector::RegisterSharingDhIdListener(sptr& deviceInfo) +{ + if (inputHub_ == nullptr) { + DHLOGI("inputHub is nullptr!"); + return; + } + inputHub_->GetDevicesInfoByType(inputTypes, deviceInfo); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 089ce3c..2416c78 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -161,6 +161,18 @@ void DistributedInputSinkManager::DInputSinkListener::onStartRemoteInput( sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); } + + bool isMouse = (sinkManagerObj_->GetInputTypes() & static_cast(DInputDeviceType::MOUSE)) != 0; + if (isMouse) { + std::map deviceInfos; + DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast(DInputDeviceType::MOUSE), + deviceInfos); + for (auto deviceInfo : deviceInfos) { + DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); + std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, + deviceInfo.second).detach(); + } + } } void DistributedInputSinkManager::DInputSinkListener::onStopRemoteInput( @@ -221,7 +233,7 @@ void DistributedInputSinkManager::DInputSinkListener::onStartRemoteInputDhid(con DHLOGW("onStartRemoteInputDhid called, this is the only session."); } - CheckKeyState(sessionId, strDhids); + std::thread(&DistributedInputSinkManager::DInputSinkListener::CheckKeyState, this, sessionId, strDhids).detach(); // add the dhids if (startRes == DH_SUCCESS) { std::vector vecStr; @@ -309,11 +321,12 @@ void DistributedInputSinkManager::DInputSinkListener::CheckKeyState(const int32_ DHLOGE("open mouse Node Path error:", errno); return; } - DHLOGI("mouse Node Path fd: %d", fd); uint32_t count = 0; int rc = 0; - int keyVal = 0; + int leftKeyVal = 0; + int rightKeyVal = 0; + int midKeyVal = 0; unsigned long keystate[NLONGS(KEY_CNT)] = { 0 }; while (true) { if (count > READ_RETRY_MAX) { @@ -323,25 +336,23 @@ void DistributedInputSinkManager::DInputSinkListener::CheckKeyState(const int32_ rc = ioctl(fd, EVIOCGKEY(sizeof(keystate)), keystate); if (rc < 0) { DHLOGE("read all key state failed, rc: ", rc); + count += 1; SleepTimeMs(); continue; } - - keyVal = bit_is_set(keystate, BTN_LEFT); - if (keyVal != 0) { - DHLOGI("mouse left button is down."); - nlohmann::json jsonStr; - jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_KEY_STATE; - jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_DHID] = dhid; - jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_TYPE] = EV_KEY; - jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_CODE] = BTN_LEFT; - jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_VALUE] = KEY_DOWN_STATE; - std::string smsg = jsonStr.dump(); - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, smsg); - break; + leftKeyVal = bit_is_set(keystate, BTN_LEFT); + if (leftKeyVal != 0) { + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhid, BTN_LEFT); } - SleepTimeMs(); - count += 1; + rightKeyVal = bit_is_set(keystate, BTN_RIGHT); + if (rightKeyVal != 0) { + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhid, BTN_RIGHT); + } + midKeyVal = bit_is_set(keystate, BTN_MIDDLE); + if (midKeyVal != 0) { + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhid, BTN_MIDDLE); + } + break; } if (fd > 0) { close(fd); diff --git a/services/sink/transport/include/distributed_input_sink_transport.h b/services/sink/transport/include/distributed_input_sink_transport.h index 6458fd2..83b0fa1 100644 --- a/services/sink/transport/include/distributed_input_sink_transport.h +++ b/services/sink/transport/include/distributed_input_sink_transport.h @@ -46,7 +46,7 @@ public: int32_t RespStartRemoteInput(const int32_t sessionId, std::string &smsg); int32_t RespStopRemoteInput(const int32_t sessionId, std::string &smsg); int32_t RespLatency(const int32_t sessionId, std::string &smsg); - void SendKeyStateNodeMsg(const int32_t sessionId, std::string &smsg); + void SendKeyStateNodeMsg(const int32_t sessionId, const std::string& dhId, const uint32_t btnCode); int32_t OnSessionOpened(int32_t sessionId, int32_t result); void OnSessionClosed(int32_t sessionId); diff --git a/services/sink/transport/src/distributed_input_sink_transport.cpp b/services/sink/transport/src/distributed_input_sink_transport.cpp index 8c7eb92..e877af4 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -243,14 +243,22 @@ int32_t DistributedInputSinkTransport::RespLatency(const int32_t sessionId, std: return DH_SUCCESS; } -void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId, std::string &smsg) +void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId, const std::string& dhId, + const uint32_t btnCode) { if (sessionId <= 0) { DHLOGE("SendKeyStateNodeMsg error, sessionId <= 0."); return; } - DHLOGI("SendKeyStateNodeMsg sessionId: %d, smsg:%s.", sessionId, smsg.c_str()); - int32_t ret = SendMessage(sessionId, smsg); + DHLOGI("SendKeyStateNodeMsg sessionId: %d, btnCode: %d.", sessionId, btnCode); + nlohmann::json jsonStr; + jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_KEY_STATE; + jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_DHID] = dhId; + jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_TYPE] = EV_KEY; + jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_CODE] = btnCode; + jsonStr[DINPUT_SOFTBUS_KEY_KEYSTATE_VALUE] = KEY_DOWN_STATE; + std::string msg = jsonStr.dump(); + int32_t ret = SendMessage(sessionId, msg); if (ret != DH_SUCCESS) { DHLOGE("SendKeyStateNodeMsg error, SendMessage fail."); }