diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index ec27c5a1a27e6b35cbf8a5f454198cec58aa88cd..dec0a9c45b97da92ad1cff97be9fae838411bc27 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -103,7 +103,7 @@ size_t InputHub::StartCollectInputEvents(RawEvent *buffer, size_t bufferSize) ScanInputDevices(DEVICE_PATH); } { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); while (!openingDevices_.empty()) { std::unique_ptr device = std::move(*openingDevices_.rbegin()); openingDevices_.pop_back(); @@ -156,7 +156,7 @@ size_t InputHub::GetEvents(RawEvent *buffer, size_t bufferSize) RawEvent* event = buffer; size_t capacity = bufferSize; while (pendingEventIndex_ < pendingEventCount_) { - std::unique_lock my_lock(operationMutex_); + std::lock_guard my_lock(operationMutex_); const struct epoll_event& eventItem = mPendingEventItems[pendingEventIndex_++]; if (eventItem.data.fd == iNotifyFd_) { if (eventItem.events & EPOLLIN) { @@ -265,7 +265,7 @@ size_t InputHub::DeviceIsExists(InputDeviceEvent *buffer, size_t bufferSize) size_t capacity = bufferSize; // Report any devices that had last been added/removed. { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); for (auto it = closingDevices_.begin(); it != closingDevices_.end();) { std::unique_ptr device = std::move(*it); DHLOGI("Reporting device closed: id=%s, name=%s\n", @@ -287,7 +287,7 @@ size_t InputHub::DeviceIsExists(InputDeviceEvent *buffer, size_t bufferSize) } { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); while (!openingDevices_.empty()) { std::unique_ptr device = std::move(*openingDevices_.rbegin()); openingDevices_.pop_back(); @@ -350,7 +350,7 @@ void InputHub::StopCollectInputHandler() void InputHub::GetDeviceHandler() { while (pendingEventIndex_ < pendingEventCount_) { - std::unique_lock my_lock(operationMutex_); + std::lock_guard my_lock(operationMutex_); const struct epoll_event& eventItem = mPendingEventItems[pendingEventIndex_++]; if (eventItem.data.fd == iNotifyFd_) { if (eventItem.events & EPOLLIN) { @@ -411,9 +411,9 @@ int32_t InputHub::RefreshEpollItem(bool isSleep) std::vector InputHub::GetAllInputDevices() { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); std::vector vecDevice; - for (const auto& [id, device] : devices_) { + for (const auto &[id, device] : devices_) { vecDevice.push_back(device->identifier); } return vecDevice; @@ -422,17 +422,17 @@ std::vector InputHub::GetAllInputDevices() void InputHub::ScanInputDevices(const std::string &dirName) { DHLOGI("ScanInputDevices enter, dirName %s.", dirName.c_str()); - std::vector vecInputDevPath; - ScanInputDevicesPath(dirName, vecInputDevPath); - for (const auto &tempPath: vecInputDevPath) { + std::vector inputDevPaths; + ScanInputDevicesPath(dirName, inputDevPaths); + for (const auto &tempPath: inputDevPaths) { OpenInputDeviceLocked(tempPath); } } bool InputHub::IsDeviceRegistered(const std::string &devicePath) { - std::unique_lock deviceLock(devicesMutex_); - for (const auto& [deviceId, device] : devices_) { + std::lock_guard deviceLock(devicesMutex_); + for (const auto &[deviceId, device] : devices_) { if (device->path == devicePath) { return true; // device was already registered } @@ -446,7 +446,7 @@ int32_t InputHub::OpenInputDeviceLocked(const std::string &devicePath) return DH_SUCCESS; } - std::unique_lock my_lock(operationMutex_); + std::lock_guard my_lock(operationMutex_); DHLOGI("Opening device: %s", devicePath.c_str()); int fd = OpenInputDeviceFdByPath(devicePath); if (fd == UN_INIT_FD_VALUE) { @@ -823,7 +823,7 @@ int32_t InputHub::RegisterFdForEpoll(int fd) void InputHub::AddDeviceLocked(std::unique_ptr device) { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); openingDevices_.push_back(std::move(device)); } @@ -836,7 +836,7 @@ void InputHub::CloseDeviceLocked(Device &device) UnregisterDeviceFromEpollLocked(device); device.Close(); { - std::unique_lock devicesLock(devicesMutex_); + std::lock_guard devicesLock(devicesMutex_); closingDevices_.push_back(std::move(devices_[device.id])); devices_.erase(device.id); } @@ -936,7 +936,7 @@ void InputHub::CloseDeviceByPathLocked(const std::string &devicePath) void InputHub::CloseAllDevicesLocked() { DHLOGI("Close All Devices"); - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); while (!devices_.empty()) { CloseDeviceForAllLocked(*(devices_.begin()->second)); } @@ -944,8 +944,8 @@ void InputHub::CloseAllDevicesLocked() InputHub::Device* InputHub::GetDeviceByPathLocked(const std::string &devicePath) { - std::unique_lock deviceLock(devicesMutex_); - for (const auto& [id, device] : devices_) { + std::lock_guard deviceLock(devicesMutex_); + for (const auto &[id, device] : devices_) { if (device->path == devicePath) { return device.get(); } @@ -955,8 +955,8 @@ InputHub::Device* InputHub::GetDeviceByPathLocked(const std::string &devicePath) InputHub::Device* InputHub::GetDeviceByFdLocked(int fd) { - std::unique_lock deviceLock(devicesMutex_); - for (const auto& [id, device] : devices_) { + std::lock_guard deviceLock(devicesMutex_); + for (const auto &[id, device] : devices_) { if (device->fd == fd) { return device.get(); } @@ -967,8 +967,8 @@ InputHub::Device* InputHub::GetDeviceByFdLocked(int fd) InputHub::Device* InputHub::GetSupportDeviceByFd(int fd) { DHLOGI("GetSupportDeviceByFd fd: %d", fd); - std::unique_lock deviceLock(devicesMutex_); - for (const auto& [id, device] : devices_) { + std::lock_guard deviceLock(devicesMutex_); + for (const auto &[id, device] : devices_) { if (device != nullptr && device->fd == fd) { DHLOGI("GetSupportDeviceByFd device: %d, fd: %d, path: %s, dhId: %s, classes=0x%x", id, device->fd, device->path.c_str(), GetAnonyString(device->identifier.descriptor).c_str(), device->classes); @@ -1023,7 +1023,7 @@ AffectDhIds InputHub::SetSupportInputType(bool enabled, const uint32_t &inputTyp AffectDhIds affDhIds; inputTypes_ = inputTypes; DHLOGI("SetSupportInputType: inputTypes=0x%x,", inputTypes_.load()); - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); for (const auto &[id, device] : devices_) { if (device->classes & inputTypes_) { device->isShare = enabled; @@ -1038,7 +1038,7 @@ AffectDhIds InputHub::SetSupportInputType(bool enabled, const uint32_t &inputTyp AffectDhIds InputHub::SetSharingDevices(bool enabled, std::vector dhIds) { AffectDhIds affDhIds; - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); DHLOGI("SetSharingDevices start"); for (auto dhId : dhIds) { DHLOGI("SetSharingDevices dhId: %s, size: %d, enabled: %d", GetAnonyString(dhId).c_str(), devices_.size(), @@ -1059,11 +1059,12 @@ AffectDhIds InputHub::SetSharingDevices(bool enabled, std::vector d return affDhIds; } -void InputHub::GetShareMousePathByDhId(std::vector dhIds, std::string &path, std::string &dhId) +void InputHub::GetSharedMousePathByDhId(const std::vector &dhIds, std::string &sharedMousePath, + std::string &sharedMouseDhId) { - DHLOGI("GetShareMousePathByDhId: devices_.size:%d,", devices_.size()); - std::unique_lock deviceLock(devicesMutex_); - for (auto dhId_ : dhIds) { + DHLOGI("GetSharedMousePathByDhId: devices_.size:%d,", devices_.size()); + std::lock_guard deviceLock(devicesMutex_); + for (const auto &dhId : dhIds) { for (const auto &[id, device] : devices_) { if (device == nullptr) { DHLOGE("device is nullptr"); @@ -1071,23 +1072,23 @@ void InputHub::GetShareMousePathByDhId(std::vector dhIds, std::stri } DHLOGI("descriptor:%s, isShare[%d], type[%d]", GetAnonyString(device->identifier.descriptor).c_str(), device->isShare, device->classes); - if ((device->identifier.descriptor == dhId_) && ((device->classes & INPUT_DEVICE_CLASS_CURSOR) != 0 || + if ((device->identifier.descriptor == dhId) && ((device->classes & INPUT_DEVICE_CLASS_CURSOR) != 0 || (device->classes & INPUT_DEVICE_CLASS_TOUCH) != 0 || ((device->classes & INPUT_DEVICE_CLASS_TOUCH_MT) != 0 && IsTouchPad(device->identifier)))) { - dhId = dhId_; - path = device->path; + sharedMouseDhId = dhId; + sharedMousePath = device->path; return; // return First shared mouse } } } } -void InputHub::GetShareKeyboardPathsByDhIds(std::vector dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds) +void InputHub::GetSharedKeyboardPathsByDhIds(const std::vector &dhIds, + std::vector &sharedKeyboardPaths, std::vector &sharedKeyboardDhIds) { - DHLOGI("GetShareKeyboardPathsByDhIds: devices_.size:%d,", devices_.size()); - std::unique_lock deviceLock(devicesMutex_); - for (auto dhId_ : dhIds) { + DHLOGI("GetSharedKeyboardPathsByDhIds: devices_.size:%d,", devices_.size()); + std::lock_guard deviceLock(devicesMutex_); + for (const auto &dhId : dhIds) { for (const auto &[id, device] : devices_) { if (device == nullptr) { DHLOGE("device is nullptr"); @@ -1095,10 +1096,10 @@ void InputHub::GetShareKeyboardPathsByDhIds(std::vector dhIds, std: } DHLOGI("descriptor:%s, isShare[%d], type[%d]", GetAnonyString(device->identifier.descriptor).c_str(), device->isShare, device->classes); - if ((device->identifier.descriptor == dhId_) && + if ((device->identifier.descriptor == dhId) && ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { - shareDhIds.push_back(dhId_); - shareDhidsPaths.push_back(device->path); + sharedKeyboardDhIds.push_back(dhId); + sharedKeyboardPaths.push_back(device->path); } } } @@ -1120,7 +1121,7 @@ void InputHub::GetDevicesInfoByType(const uint32_t inputTypes, std::map deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); for (const auto &[id, device] : devices_) { if (device->classes & dhType) { datas.insert(std::pair(device->fd, device->identifier.descriptor)); @@ -1131,7 +1132,7 @@ void InputHub::GetDevicesInfoByType(const uint32_t inputTypes, std::map dhidsVec, std::map &datas) { for (auto dhId : dhidsVec) { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); for (const auto &[id, device] : devices_) { if (device->identifier.descriptor == dhId) { datas.insert(std::pair(device->fd, dhId)); @@ -1142,7 +1143,7 @@ void InputHub::GetDevicesInfoByDhId(std::vector dhidsVec, std::map< bool InputHub::IsAllDevicesStoped() { - std::unique_lock deviceLock(devicesMutex_); + std::lock_guard deviceLock(devicesMutex_); for (const auto &[dhId, isShared] : sharedDHIds_) { DHLOGI("the dhId: %s, isShared: %d", GetAnonyString(dhId).c_str(), isShared); if (isShared) { @@ -1250,7 +1251,7 @@ bool InputHub::CheckTouchPointRegion(struct input_event readBuffer[], const AbsI { auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo(); - for (const auto& [id, sinkInfo] : sinkInfos) { + for (const auto &[id, sinkInfo] : sinkInfos) { auto info = sinkInfo.transformInfo; if ((absInfo.absX >= info.sinkWinPhyX) && (absInfo.absX <= (info.sinkWinPhyX + info.sinkProjPhyWidth)) && (absInfo.absY >= info.sinkWinPhyY) && (absInfo.absY <= (info.sinkWinPhyY + info.sinkProjPhyHeight))) { diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 4b5e488d474c22dd22cf3a4e1fbf63f90447b53f..b7344f9658400f41c7b894b354a1a6d9c0d15264 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -52,9 +52,10 @@ public: AffectDhIds SetSharingDevices(bool enabled, std::vector dhIds); 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); - void GetShareKeyboardPathsByDhIds(std::vector dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds); + void GetSharedMousePathByDhId(const std::vector &dhIds, std::string &sharedMousePath, + std::string &sharedMouseDhId); + void GetSharedKeyboardPathsByDhIds(const std::vector &dhIds, + std::vector &sharedKeyboardPaths, std::vector &sharedKeyboardDhIds); bool IsAllDevicesStoped(); void ScanInputDevices(const std::string &dirName); diff --git a/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp b/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp index b271e2e929f1774f8d460a56649f7b141f64a0ef..81a18cb653ad71066642f31ecc14ef0d2e226088 100644 --- a/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp +++ b/interfaces/inner_kits/test/unittest/mock/mock_distributed_input_client.cpp @@ -196,7 +196,7 @@ bool DistributedInputClient::IsTouchEventNeedFilterOut(const TouchScreenEvent &e { auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo(); - for (const auto& [id, sinkInfo] : sinkInfos) { + for (const auto &[id, sinkInfo] : sinkInfos) { auto info = sinkInfo.transformInfo; DHLOGI("event.absX:%d, info.sinkWinPhyX:%d, info.sinkProjPhyWidth:%d\n", event.absX, info.sinkWinPhyX, info.sinkProjPhyWidth); diff --git a/interfaces/ipc/src/distributed_input_client.cpp b/interfaces/ipc/src/distributed_input_client.cpp index 4b5ca096bb2993ff8ee73a847f257fca216d332b..b70bf9fc9f533831e1af544b7eaad04ffbb47189 100644 --- a/interfaces/ipc/src/distributed_input_client.cpp +++ b/interfaces/ipc/src/distributed_input_client.cpp @@ -560,7 +560,7 @@ bool DistributedInputClient::IsNeedFilterOut(const std::string &deviceId, const bool DistributedInputClient::IsTouchEventNeedFilterOut(const TouchScreenEvent &event) { std::lock_guard lock(operationMutex_); - for (const auto& info : screenTransInfos) { + for (const auto &info : screenTransInfos) { DHLOGI("sinkProjPhyWidth: %d sinkProjPhyHeight: %d", info.sinkProjPhyWidth, info.sinkProjPhyHeight); if ((event.absX >= info.sinkWinPhyX) && (event.absX <= (info.sinkWinPhyX + info.sinkProjPhyWidth)) && (event.absY >= info.sinkWinPhyY) && (event.absY <= (info.sinkWinPhyY + info.sinkProjPhyHeight))) { diff --git a/services/sink/inputcollector/include/distributed_input_collector.h b/services/sink/inputcollector/include/distributed_input_collector.h index 50e7a5f616dd4892347d34a389af2786030f3273..952b685f4f8da9bb50749fe0e9d1a12709a596ea 100644 --- a/services/sink/inputcollector/include/distributed_input_collector.h +++ b/services/sink/inputcollector/include/distributed_input_collector.h @@ -42,9 +42,9 @@ public: void Release(); AffectDhIds SetSharingTypes(bool enabled, const uint32_t &inputType); AffectDhIds SetSharingDhIds(bool enabled, std::vector dhIds); - void GetMouseNodePath(std::vector dhIds, std::string &mouseNodePath, std::string &dhid); - void GetShareKeyboardPathsByDhIds(std::vector dhIds, std::vector &shareDhidsPaths, - std::vector &shareDhIds); + void GetMouseNodePath(const std::vector &dhIds, std::string &mouseNodePath, std::string &dhid); + void GetSharedKeyboardPathsByDhIds(const std::vector &dhIds, + std::vector &sharedKeyboardPaths, std::vector &sharedKeyboardDhIds); // false for sharing device exist, true for all devices stop sharing bool IsAllDevicesStoped(); int32_t RegisterSharingDhIdListener(sptr sharingDhIdListener); diff --git a/services/sink/inputcollector/src/distributed_input_collector.cpp b/services/sink/inputcollector/src/distributed_input_collector.cpp index 822ef136d964f2042cace43a4222a8395d30634e..3f60ca8f9c6fbf4437b7e701d031cc529236e5b3 100644 --- a/services/sink/inputcollector/src/distributed_input_collector.cpp +++ b/services/sink/inputcollector/src/distributed_input_collector.cpp @@ -204,24 +204,24 @@ AffectDhIds DistributedInputCollector::SetSharingDhIds(bool enabled, std::vector return inputHub_->SetSharingDevices(enabled, dhIds); } -void DistributedInputCollector::GetMouseNodePath( - std::vector dhIds, std::string &mouseNodePath, std::string &dhid) +void DistributedInputCollector::GetMouseNodePath(const std::vector &dhIds, + std::string &mouseNodePath, std::string &dhId) { if (inputHub_ == nullptr) { DHLOGE("inputHub is nullptr!"); return; } - inputHub_->GetShareMousePathByDhId(dhIds, mouseNodePath, dhid); + inputHub_->GetSharedMousePathByDhId(dhIds, mouseNodePath, dhId); } -void DistributedInputCollector::GetShareKeyboardPathsByDhIds(std::vector dhIds, - std::vector &shareDhidsPaths, std::vector &shareDhIds) +void DistributedInputCollector::GetSharedKeyboardPathsByDhIds(const std::vector &dhIds, + std::vector &sharedKeyboardPaths, std::vector &sharedKeyboardDhIds) { if (inputHub_ == nullptr) { DHLOGE("inputHub is nullptr!"); return; } - inputHub_->GetShareKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds); + inputHub_->GetSharedKeyboardPathsByDhIds(dhIds, sharedKeyboardPaths, sharedKeyboardDhIds); } bool DistributedInputCollector::IsAllDevicesStoped() diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index d8d51db50dc6df357b55aa8d1c7240bf959f520d..27bd793c18cf2bca4f7a7576503c6586d6760bd7 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -238,11 +238,11 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInput( std::map deviceInfos; DistributedInputCollector::GetInstance().GetDeviceInfoByType(static_cast(DInputDeviceType::MOUSE), deviceInfos); - for (auto deviceInfo : deviceInfos) { + for (const auto &deviceInfo : deviceInfos) { DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); - std::vector vecStr; - StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, sessionId); + std::vector devDhIds; + SplitStringToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, devDhIds); + DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId); } } } @@ -300,10 +300,10 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(con return; } - std::vector vecStr; - StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, sessionId); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); + std::vector devDhIds; + SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, devDhIds); + DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, devDhIds); sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); } @@ -314,14 +314,14 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons DHLOGI("OnStopRemoteInputDhid called, sessionId: %d", sessionId); std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; - StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); + SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); sinkManagerObj_->DeleteStopDhids(sessionId, stopOnCmdDhIds, stopIndeedDhIds); (void)DistributedInputCollector::GetInstance().SetSharingDhIds(false, stopIndeedDhIds); AffectDhIds stopIndeedOnes; stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - DInputState::GetInstance().RecordDhids(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); + DInputState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, -1); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", sessionId); @@ -367,10 +367,10 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInpu return; } - std::vector vecStr; - StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); - AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, vecStr); + std::vector devDhIds; + SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, devDhIds); + DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId); + AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, devDhIds); sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); } @@ -381,14 +381,14 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput DHLOGI("onRelayStopDhidRemoteInput called, toSinkSessionId: %d", toSinkSessionId); std::vector stopIndeedDhIds; std::vector stopOnCmdDhIds; - StringSplitToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); + SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, stopOnCmdDhIds); sinkManagerObj_->DeleteStopDhids(toSinkSessionId, stopOnCmdDhIds, stopIndeedDhIds); AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(false, stopIndeedDhIds); AffectDhIds stopIndeedOnes; stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - DInputState::GetInstance().RecordDhids(stopOnCmdDhIds, DhidState::THROUGH_IN, -1); + DInputState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, -1); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", toSinkSessionId); @@ -461,9 +461,9 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartTypeRemoteInpu deviceInfos); for (auto deviceInfo : deviceInfos) { DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); - std::vector vecStr; - StringSplitToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_OUT, toSinkSessionId); + std::vector devDhIds; + SplitStringToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, devDhIds); + DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId); } } @@ -935,7 +935,7 @@ void DistributedInputSinkManager::CallBackScreenInfoChange() std::vector> transInfos; auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo(); std::vector info; - for (const auto& [id, sinkInfo] : sinkInfos) { + for (const auto &[id, sinkInfo] : sinkInfos) { info.clear(); info.emplace_back(sinkInfo.transformInfo.sinkWinPhyX); info.emplace_back(sinkInfo.transformInfo.sinkWinPhyY); @@ -945,7 +945,7 @@ void DistributedInputSinkManager::CallBackScreenInfoChange() } nlohmann::json screenMsg(transInfos); std::string str = screenMsg.dump(); - for (const auto& iter : getSinkScreenInfosCallbacks_) { + for (const auto &iter : getSinkScreenInfosCallbacks_) { iter->OnResult(str); } } @@ -957,7 +957,7 @@ void DistributedInputSinkManager::CleanExceptionalInfo(const SrcScreenInfo &srcS int32_t sessionId = srcScreenInfo.sessionId; auto sinkInfos = DInputContext::GetInstance().GetAllSinkScreenInfo(); - for (const auto& [id, sinkInfo] : sinkInfos) { + for (const auto &[id, sinkInfo] : sinkInfos) { auto srcInfo = sinkInfo.srcScreenInfo; if ((std::strcmp(srcInfo.uuid.c_str(), uuid.c_str()) == 0) && (srcInfo.sessionId != sessionId)) { DInputContext::GetInstance().RemoveSinkScreenInfo(id); diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index 397e348e71fe4e9d956c0ebd208f19e020e683de..0cbcf883be22f2a66c89f7166f0144b0ff08e4ad 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -54,7 +54,7 @@ public: void SyncNodeOnlineInfo(const std::string &srcDevId, const std::string &sinkDevId, const std::string &sinkNodeId, const std::string &sinkNodeDesc); void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &shareDhidsPaths, std::vector &shareDhIds); + std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); private: DistributedInputInject(); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 86c38adc490c8fb3199e453bc8eba354c19e0ec7..885c7f4ed259e99710e545ca62ba678ebf432b54 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -34,7 +34,7 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -const uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1; +constexpr uint32_t DINPUT_NODE_MANAGER_SCAN_ALL_NODE = 1; const std::string INPUT_NODE_DHID = "dhId"; class DistributedInputNodeManager { public: @@ -59,7 +59,7 @@ public: void ProcessInjectEvent(const std::shared_ptr &rawEvent); void GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &shareDhidsPaths, std::vector &shareDhIds); + std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds); void NotifyNodeMgrScanVirNode(const std::string &dhId); class DInputNodeManagerEventHandler : public AppExecFwk::EventHandler { @@ -89,7 +89,7 @@ private: void OpenInputDevice(const std::string &devicePath, const std::string &dhId); bool IsVirtualDev(int fd); bool GetDevDhIdByFd(int fd, std::string &dhId, std::string &physicalPath); - void SetPathForDevMap(std::string &dhId, const std::string &devicePath); + void SetPathForDevMap(const std::string &dhId, const std::string &devicePath); /* the key is dhId, and the value is virtualDevice */ std::map> virtualDeviceMap_; diff --git a/services/source/inputinject/include/virtual_device.h b/services/source/inputinject/include/virtual_device.h index 6eb08ab1382a703f5e8d104fed5b0f15a1c77e22..bcec29e2d5a8b48b48530b676a4b1484f03dc724 100644 --- a/services/source/inputinject/include/virtual_device.h +++ b/services/source/inputinject/include/virtual_device.h @@ -39,11 +39,11 @@ public: bool DoIoctl(int32_t fd, int32_t request, const uint32_t value); bool CreateKey(const InputDevice &inputDevice); void SetABSInfo(struct uinput_user_dev &inputUserDev, const InputDevice &inputDevice); - bool SetPhys(const std::string deviceName, std::string dhId); + bool SetPhys(const std::string &deviceName, const std::string &dhId); bool SetUp(const InputDevice &inputDevice, const std::string &devId, const std::string &dhId); bool InjectInputEvent(const input_event &event); - void SetNetWorkId(const std::string netWorkId); - void SetPath(const std::string path); + void SetNetWorkId(const std::string &netWorkId); + void SetPath(const std::string &path); std::string GetNetWorkId(); std::string GetPath(); uint16_t GetClasses(); @@ -55,7 +55,7 @@ private: int32_t fd_ = -1; std::string deviceName_; std::string netWorkId_; - std::string path_ {""}; + std::string path_; const uint16_t busType_; const uint16_t vendorId_; const uint16_t productId_; diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 9db0504d4a8063e6db6fdbaf75211895520342a3..237e5940e30190f028149b4dc6345fef7f7586eb 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -257,14 +257,14 @@ int32_t DistributedInputInject::GetVirtualTouchScreenFd() } void DistributedInputInject::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &shareDhidsPaths, std::vector &shareDhIds) + std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds) { std::lock_guard lock(inputNodeManagerMutex_); if (inputNodeManager_ == nullptr) { DHLOGE("inputNodeManager is nullptr"); return; } - inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, shareDhidsPaths, shareDhIds); + inputNodeManager_->GetVirtualKeyboardPathsByDhIds(dhIds, virKeyboardPaths, virKeyboardDhIds); } void DistributedInputInject::NotifyNodeMgrScanVirNode(const std::string &dhId) diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 710f8706ef34c3bcb949ecd06f120d6ac554b29f..db58cd147be43a8698f61777176a5afe47bcc7c3 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -230,7 +230,7 @@ bool DistributedInputNodeManager::GetDevDhIdByFd(int fd, std::string &dhId, std: return true; } -void DistributedInputNodeManager::SetPathForDevMap(std::string &dhId, const std::string &devicePath) +void DistributedInputNodeManager::SetPathForDevMap(const std::string &dhId, const std::string &devicePath) { std::lock_guard lock(virtualDeviceMapMutex_); auto iter = virtualDeviceMap_.begin(); @@ -268,22 +268,22 @@ void DistributedInputNodeManager::OpenInputDevice(const std::string &devicePath, } void DistributedInputNodeManager::GetVirtualKeyboardPathsByDhIds(const std::vector &dhIds, - std::vector &shareDhidsPaths, std::vector &shareDhIds) + std::vector &virKeyboardPaths, std::vector &virKeyboardDhIds) { std::lock_guard lock(virtualDeviceMapMutex_); - for (auto dhId_ : dhIds) { + for (const auto &dhId : dhIds) { auto iter = virtualDeviceMap_.begin(); while (iter != virtualDeviceMap_.end()) { if (iter->second == nullptr) { DHLOGE("device is nullptr"); continue; } - if ((iter->first.compare(dhId_) == 0) && + if ((iter->first.compare(dhId) == 0) && ((iter->second->GetClasses() & INPUT_DEVICE_CLASS_KEYBOARD) != 0)) { DHLOGI("Found vir keyboard path %s, dhid %s", iter->second->GetPath().c_str(), - GetAnonyString(dhId_).c_str()); - shareDhidsPaths.push_back(iter->second->GetPath()); - shareDhIds.push_back(dhId_); + GetAnonyString(dhId).c_str()); + virKeyboardPaths.push_back(iter->second->GetPath()); + virKeyboardDhIds.push_back(dhId); } iter++; } diff --git a/services/source/inputinject/src/virtual_device.cpp b/services/source/inputinject/src/virtual_device.cpp index c8024b36679bb2900ec5f54d7f9c85db16d928ec..a0b83c58178cfb1b927b882a8baa84afa59edfe3 100644 --- a/services/source/inputinject/src/virtual_device.cpp +++ b/services/source/inputinject/src/virtual_device.cpp @@ -101,7 +101,7 @@ void VirtualDevice::SetABSInfo(struct uinput_user_dev &inputUserDev, const Input } } -bool VirtualDevice::SetPhys(const std::string deviceName, std::string dhId) +bool VirtualDevice::SetPhys(const std::string &deviceName, const std::string &dhId) { std::string phys; phys.append(deviceName).append(pid_).append("/").append(pid_).append("|") @@ -179,13 +179,13 @@ bool VirtualDevice::InjectInputEvent(const input_event &event) return true; } -void VirtualDevice::SetNetWorkId(const std::string netWorkId) +void VirtualDevice::SetNetWorkId(const std::string &netWorkId) { DHLOGI("SetNetWorkId %s\n", GetAnonyString(netWorkId).c_str()); netWorkId_ = netWorkId; } -void VirtualDevice::SetPath(const std::string path) +void VirtualDevice::SetPath(const std::string &path) { path_ = path; } diff --git a/services/source/sourcemanager/src/dinput_source_listener.cpp b/services/source/sourcemanager/src/dinput_source_listener.cpp index 29b87dde0937a4e16a3db310b96ceb5eac4b8f5e..cedd3db06d56d16a56dac661f438bad708896829 100644 --- a/services/source/sourcemanager/src/dinput_source_listener.cpp +++ b/services/source/sourcemanager/src/dinput_source_listener.cpp @@ -255,9 +255,9 @@ void DInputSourceListener::OnResponseStartRemoteInputDhid( sourceManagerObj_->SetDeviceMapValue(deviceId, DINPUT_SOURCE_SWITCH_ON); } - std::vector vecStr; - StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_IN, -1); + std::vector devDhIds; + SplitStringToVector(dhids, INPUT_STRING_SPLIT_POINT, devDhIds); + DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_IN, -1); auto jsonArrayMsg = std::make_shared(); nlohmann::json tmpJson; diff --git a/services/source/sourcemanager/src/distributed_input_source_manager.cpp b/services/source/sourcemanager/src/distributed_input_source_manager.cpp index aaf0344ddcb4b3121ae00fe34f499f2ea69acba8..f3968c1fb9efcd975847f8ecdb635082c517ca4b 100644 --- a/services/source/sourcemanager/src/distributed_input_source_manager.cpp +++ b/services/source/sourcemanager/src/distributed_input_source_manager.cpp @@ -1401,7 +1401,7 @@ void DistributedInputSourceManager::RunWhiteListCallback(const std::string &devI DHLOGE("addWhiteListCallbacks_ is empty."); return; } - for (const auto& it : addWhiteListCallbacks_) { + for (const auto &it : addWhiteListCallbacks_) { it->OnResult(devId, object); } } @@ -1449,7 +1449,7 @@ void DistributedInputSourceManager::RunUnprepareCallback(const std::string &devI DHLOGE("ProcessEvent DINPUT_SOURCE_MANAGER_UNPREPARE_MSG delWhiteListCallback is null."); return; } - for (const auto& it : delWhiteListCallbacks_) { + for (const auto &it : delWhiteListCallbacks_) { it->OnResult(devId); } return; @@ -1490,7 +1490,7 @@ void DistributedInputSourceManager::RunStartDhidCallback(const std::string &sink const int32_t &status) { std::vector dhidsVec; - StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); + SplitStringToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_START_DHID_MSG dhIds:%s, vec-size:%d", GetAnonyString(dhIds).c_str(), dhidsVec.size()); std::string localNetWorkId = GetLocalNetworkId(); @@ -1512,7 +1512,7 @@ void DistributedInputSourceManager::RunStopDhidCallback(const std::string &sinkI const int32_t &status) { std::vector dhidsVec; - StringSplitToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); + SplitStringToVector(dhIds, INPUT_STRING_SPLIT_POINT, dhidsVec); std::string localNetworkId = GetLocalNetworkId(); if (localNetworkId.empty()) { return; @@ -1533,7 +1533,7 @@ void DistributedInputSourceManager::RunRelayStartDhidCallback(const std::string const int32_t status, const std::string &dhids) { std::vector dhidsVec; - StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec); + SplitStringToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec); DHLOGI("ProcessEvent DINPUT_SOURCE_MANAGER_RELAY_STARTDHID_RESULT_MMI dhIds:%s, vec-size:%d", dhids.c_str(), dhidsVec.size()); bool isCbRun = false; @@ -1557,7 +1557,7 @@ void DistributedInputSourceManager::RunRelayStopDhidCallback(const std::string & const int32_t status, const std::string &dhids) { std::vector dhidsVec; - StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec); + SplitStringToVector(dhids, INPUT_STRING_SPLIT_POINT, dhidsVec); bool isCbRun = false; for (std::vector::iterator iter = relayStpDhidCallbacks_.begin(); iter != relayStpDhidCallbacks_.end(); ++iter) { @@ -1951,7 +1951,7 @@ void DistributedInputSourceManager::DeviceOfflineListener::DeleteNodeInfoAndNoti } std::set nodeSet = sourceManagerContext_->GetSyncNodeInfo(offlineDevId); std::string localNetWorkId = GetLocalNetworkId(); - for (const auto& node : nodeSet) { + for (const auto &node : nodeSet) { DHLOGI("DeleteNodeInfoAndNotify device: %s, dhId: %s", GetAnonyString(offlineDevId).c_str(), GetAnonyString(node.dhId).c_str()); // Notify multimodal diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index c037f186004e345825f514b7b47156d94ebf5596..8c61fc504e798bd6e3392c6318ab7b8bae448e18 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -296,9 +296,9 @@ int32_t DistributedInputSourceTransport::StartRemoteInputDhids(int32_t srcTsrcSe } DHLOGI("StartRemoteInputDhids srcTsrcSeId:%d, sinkSessionId:%d.", srcTsrcSeId, sinkSessionId); - std::vector vecStr; - StringSplitToVector(dhids, INPUT_STRING_SPLIT_POINT, vecStr); - DInputState::GetInstance().RecordDhids(vecStr, DhidState::THROUGH_IN, -1); + std::vector devDhIds; + SplitStringToVector(dhids, INPUT_STRING_SPLIT_POINT, devDhIds); + DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_IN, -1); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_START_DHID_FOR_REL; @@ -763,7 +763,7 @@ int32_t DistributedInputSourceTransport::StopRemoteInput(const std::string &devi } DHLOGI("StopRemoteInput sessionId:%d.", sessionId); - DInputState::GetInstance().RecordDhids(dhids, DhidState::THROUGH_OUT, -1); + DInputState::GetInstance().RecordDhIds(dhids, DhIdState::THROUGH_OUT, -1); nlohmann::json jsonStr; jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SOURCE_MSG_STOP_DHID; diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index 8dd9b490e591a3370cc24326497f6db5199b3659..dd3f3c01926f48a1a8b6dbff1cddc6454fa313a0 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -21,6 +21,8 @@ #include #include +#include "single_instance.h" + namespace OHOS { namespace DistributedHardware { namespace DistributedInput { @@ -29,40 +31,36 @@ namespace DistributedInput { * 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 { +enum class DhIdState { THROUGH_IN = 0, THROUGH_OUT, }; class DInputState { + DECLARE_SINGLE_INSTANCE_BASE(DInputState); public: - static DInputState &GetInstance() - { - static DInputState instance; - return instance; - }; - int32_t Init(); int32_t Release(); - int32_t RecordDhids(const std::vector &dhids, DhidState state, const int32_t &sessionId); - int32_t RemoveDhids(const std::vector &dhids); - DhidState GetStateByDhid(std::string &dhid); + int32_t RecordDhIds(const std::vector &dhIds, DhIdState state, const int32_t sessionId); + int32_t RemoveDhIds(const std::vector &dhIds); + DhIdState GetStateByDhid(const std::string &dhId); private: + DInputState() = default; ~DInputState(); - void CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector &dhids); - void CheckKeyboardState(std::string &dhid, std::string &keyboardNodePath, - std::vector &keyboardPressedKeys, int &fd); - void SpecEventInject(const int32_t &sessionId, std::vector dhids); + void CreateSpecialEventInjectThread(const int32_t sessionId, const std::vector &dhIds); + void CheckKeyboardState(const std::string &dhId, const std::string &keyboardNodePath, + std::vector &pressedKeys, int &fd); + void SpecEventInject(const int32_t sessionId, const std::vector &dhIds); void RecordEventLog(const input_event &event); - void WriteEventToDev(int &fd, const input_event &event); - void CheckMouseKeyState(const int32_t &sessionId, const std::string &mouseNodePath, + void WriteEventToDev(const int fd, const input_event &event); + void SyncMouseKeyState(const int32_t sessionId, const std::string &mouseNodePath, const std::string &mouseNodeDhId); private: std::mutex operationMutex_; - std::map dhidStateMap_; + std::map dhIdStateMap_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 7dc529026981a0cfa46fd87471a43f961388da3b..60fd54f00d803ace5f80f4bd040d507d1c0aa37d 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -33,6 +33,7 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { +IMPLEMENT_SINGLE_INSTANCE(DInputState); DInputState::~DInputState() { Release(); @@ -47,52 +48,52 @@ int32_t DInputState::Init() int32_t DInputState::Release() { DHLOGI("DInputState Release."); - std::unique_lock mapLock(operationMutex_); - dhidStateMap_.clear(); + std::lock_guard mapLock(operationMutex_); + dhIdStateMap_.clear(); return DH_SUCCESS; } -int32_t DInputState::RecordDhids(const std::vector &dhids, DhidState state, const int32_t &sessionId) +int32_t DInputState::RecordDhIds(const std::vector &dhIds, DhIdState state, const int32_t sessionId) { - DHLOGI("RecordDhids dhids size = %zu", dhids.size()); - std::unique_lock mapLock(operationMutex_); - for (auto &dhid : dhids) { + DHLOGI("RecordDhIds dhIds size = %zu", dhIds.size()); + std::lock_guard mapLock(operationMutex_); + for (const auto &dhid : dhIds) { DHLOGD("add dhid : %s, state : %d.", GetAnonyString(dhid).c_str(), state); - dhidStateMap_[dhid] = state; + dhIdStateMap_[dhid] = state; } - if (state == DhidState::THROUGH_OUT) { - CreateSpecialEventInjectThread(sessionId, dhids); + if (state == DhIdState::THROUGH_OUT) { + CreateSpecialEventInjectThread(sessionId, dhIds); } return DH_SUCCESS; } -int32_t DInputState::RemoveDhids(const std::vector &dhids) +int32_t DInputState::RemoveDhIds(const std::vector &dhIds) { - DHLOGI("RemoveDhids dhids size = %zu", dhids.size()); - std::unique_lock mapLock(operationMutex_); - for (auto &dhid : dhids) { + DHLOGI("RemoveDhIds dhIds size = %zu", dhIds.size()); + std::lock_guard mapLock(operationMutex_); + for (const auto &dhid : dhIds) { DHLOGD("delete dhid : %s", GetAnonyString(dhid).c_str()); - dhidStateMap_.erase(dhid); + dhIdStateMap_.erase(dhid); } return DH_SUCCESS; } -DhidState DInputState::GetStateByDhid(std::string &dhid) +DhIdState DInputState::GetStateByDhid(const std::string &dhId) { - std::unique_lock mapLock(operationMutex_); - if (dhidStateMap_.find(dhid) == dhidStateMap_.end()) { - DHLOGE("dhid : %s not exist.", GetAnonyString(dhid).c_str()); - return DhidState::THROUGH_IN; + std::lock_guard mapLock(operationMutex_); + if (dhIdStateMap_.find(dhId) == dhIdStateMap_.end()) { + DHLOGE("dhId : %s not exist.", GetAnonyString(dhId).c_str()); + return DhIdState::THROUGH_IN; } - return dhidStateMap_[dhid]; + return dhIdStateMap_[dhId]; } -void DInputState::CreateSpecialEventInjectThread(const int32_t &sessionId, const std::vector &dhids) +void DInputState::CreateSpecialEventInjectThread(const int32_t sessionId, const std::vector &dhIds) { - DHLOGI("CreateSpecialEventInjectThread enter, dhids.size = %d, sessionId = %d.", dhids.size(), sessionId); + DHLOGI("CreateSpecialEventInjectThread enter, dhIds.size = %d, sessionId = %d.", dhIds.size(), sessionId); std::thread specEventInjectThread = - std::thread(&DInputState::SpecEventInject, this, sessionId, dhids); + std::thread(&DInputState::SpecEventInject, this, sessionId, dhIds); int32_t ret = pthread_setname_np(specEventInjectThread.native_handle(), CHECK_KEY_STATUS_THREAD_NAME); if (ret != 0) { DHLOGE("specEventInjectThread setname failed."); @@ -118,7 +119,7 @@ void DInputState::RecordEventLog(const input_event &event) eventType.c_str(), event.code, event.value); } -void DInputState::WriteEventToDev(int &fd, const input_event &event) +void DInputState::WriteEventToDev(const int fd, const input_event &event) { if (write(fd, &event, sizeof(event)) < static_cast(sizeof(event))) { DHLOGE("could not inject event, removed? (fd: %d)", fd); @@ -127,33 +128,35 @@ void DInputState::WriteEventToDev(int &fd, const input_event &event) RecordEventLog(event); } -void DInputState::SpecEventInject(const int32_t &sessionId, std::vector dhids) +void DInputState::SpecEventInject(const int32_t sessionId, const std::vector &dhIds) { - DHLOGI("SpecEveInject enter"); + DHLOGI("SpecEveInject enter, sessionId %d, dhIds size %d", sessionId, dhIds.size()); // mouse event send to remote device if (sessionId != -1) { std::string mouseNodePath; std::string mouseNodeDhId; - DistributedInputCollector::GetInstance().GetMouseNodePath(dhids, mouseNodePath, mouseNodeDhId); - CheckMouseKeyState(sessionId, mouseNodePath, mouseNodeDhId); + DistributedInputCollector::GetInstance().GetMouseNodePath(dhIds, mouseNodePath, mouseNodeDhId); + SyncMouseKeyState(sessionId, mouseNodePath, mouseNodeDhId); } // keyboard up event inject local device std::vector keyboardNodePaths; std::vector keyboardNodeDhIds; - DistributedInputCollector::GetInstance().GetShareKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); - DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhids, keyboardNodePaths, keyboardNodeDhIds); + DistributedInputCollector::GetInstance().GetSharedKeyboardPathsByDhIds(dhIds, keyboardNodePaths, keyboardNodeDhIds); + DistributedInputInject::GetInstance().GetVirtualKeyboardPathsByDhIds(dhIds, keyboardNodePaths, keyboardNodeDhIds); size_t len = keyboardNodePaths.size(); for (size_t i = 0; i < len; ++i) { - std::vector keyboardPressedKeys; + std::vector pressedKeys; int fd = UN_INIT_FD_VALUE; - CheckKeyboardState(keyboardNodeDhIds[i], keyboardNodePaths[i], keyboardPressedKeys, fd); - for (auto &code : keyboardPressedKeys) { - struct input_event event = { - .type = EV_KEY, - .code = code, - .value = KEY_UP_STATE - }; + CheckKeyboardState(keyboardNodeDhIds[i], keyboardNodePaths[i], pressedKeys, fd); + struct input_event event = { + .type = EV_KEY, + .code = 0, + .value = KEY_UP_STATE + }; + for (auto &code : pressedKeys) { + event.type = EV_KEY; + event.code = code; WriteEventToDev(fd, event); event.type = EV_SYN; event.code = 0; @@ -163,10 +166,10 @@ void DInputState::SpecEventInject(const int32_t &sessionId, std::vector &keyboardPressedKeys, int &fd) +void DInputState::CheckKeyboardState(const std::string &dhId, const std::string &keyboardNodePath, + std::vector &pressedKeys, int &fd) { - DHLOGI("CheckKeyboardState enter, dhid %s, keyboardNodePath %s.", GetAnonyString(dhid).c_str(), + DHLOGI("CheckKeyboardState enter, dhId %s, keyboardNodePath %s.", GetAnonyString(dhId).c_str(), keyboardNodePath.c_str()); char canonicalPath[PATH_MAX] = {0x00}; if (keyboardNodePath.length() == 0 || keyboardNodePath.length() >= PATH_MAX || @@ -181,32 +184,32 @@ void DInputState::CheckKeyboardState(std::string &dhid, std::string &keyboardNod } uint32_t count = 0; - unsigned long keystate[NLONGS(KEY_CNT)] = { 0 }; + unsigned long keyState[NLONGS(KEY_CNT)] = { 0 }; while (true) { if (count > READ_RETRY_MAX) { break; } - int rc = ioctl(fd, EVIOCGKEY(sizeof(keystate)), keystate); + int rc = ioctl(fd, EVIOCGKEY(sizeof(keyState)), keyState); if (rc < 0) { DHLOGE("read all key state failed, rc=%d ", rc); count += 1; std::this_thread::sleep_for(std::chrono::milliseconds(READ_SLEEP_TIME_MS)); continue; } - for (int32_t yalv = 0; yalv < KEY_MAX; yalv++) { - if (BitIsSet(keystate, yalv)) { - DHLOGD("yalv = %d, not up.", yalv); - keyboardPressedKeys.push_back(yalv); + for (int32_t keyIndex = 0; keyIndex < KEY_MAX; keyIndex++) { + if (BitIsSet(keyState, keyIndex)) { + DHLOGD("keyIndex = %d, not up.", keyIndex); + pressedKeys.push_back(keyIndex); } } break; } } -void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string &mouseNodePath, +void DInputState::SyncMouseKeyState(const int32_t sessionId, const std::string &mouseNodePath, const std::string &mouseNodeDhId) { - DHLOGI("CheckMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), + DHLOGI("SyncMouseKeyState enter, mouseNodePath %s, mouseNodeDhId %s, sessionId %d.", mouseNodePath.c_str(), GetAnonyString(mouseNodeDhId).c_str(), sessionId); char canonicalPath[PATH_MAX] = {0x00}; if (mouseNodePath.length() == 0 || mouseNodePath.length() >= PATH_MAX || @@ -224,28 +227,28 @@ void DInputState::CheckMouseKeyState(const int32_t &sessionId, const std::string int leftKeyVal = 0; int rightKeyVal = 0; int midKeyVal = 0; - unsigned long keystate[NLONGS(KEY_CNT)] = { 0 }; + unsigned long keyState[NLONGS(KEY_CNT)] = { 0 }; while (true) { if (count > READ_RETRY_MAX) { break; } // Query all key state - int rc = ioctl(fd, EVIOCGKEY(sizeof(keystate)), keystate); + int rc = ioctl(fd, EVIOCGKEY(sizeof(keyState)), keyState); if (rc < 0) { DHLOGE("read all key state failed, rc=%d ", rc); count += 1; std::this_thread::sleep_for(std::chrono::milliseconds(READ_SLEEP_TIME_MS)); continue; } - leftKeyVal = BitIsSet(keystate, BTN_LEFT); + leftKeyVal = BitIsSet(keyState, BTN_LEFT); if (leftKeyVal != 0) { DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, mouseNodeDhId, BTN_LEFT); } - rightKeyVal = BitIsSet(keystate, BTN_RIGHT); + rightKeyVal = BitIsSet(keyState, BTN_RIGHT); if (rightKeyVal != 0) { DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, mouseNodeDhId, BTN_RIGHT); } - midKeyVal = BitIsSet(keystate, BTN_MIDDLE); + midKeyVal = BitIsSet(keyState, BTN_MIDDLE); if (midKeyVal != 0) { DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, mouseNodeDhId, BTN_MIDDLE); } diff --git a/services/transportbase/include/distributed_input_transport_base.h b/services/transportbase/include/distributed_input_transport_base.h index ea7568d62b8e81968b338ee259708c36951b0fff..5e43dda5d9a5ccc712e8d7ce4454f6fcad89ca42 100644 --- a/services/transportbase/include/distributed_input_transport_base.h +++ b/services/transportbase/include/distributed_input_transport_base.h @@ -29,6 +29,7 @@ #include "event_handler.h" #include "nlohmann/json.hpp" #include "securec.h" +#include "single_instance.h" #include "dinput_sink_manager_callback.h" #include "dinput_source_manager_callback.h" @@ -40,12 +41,9 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { class DistributedInputTransportBase { + DECLARE_SINGLE_INSTANCE_BASE(DistributedInputTransportBase); public: - static DistributedInputTransportBase &GetInstance(); - ~DistributedInputTransportBase(); - int32_t Init(); - int32_t StartSession(const std::string &remoteDevId); void StopSession(const std::string &remoteDevId); @@ -67,6 +65,8 @@ public: int32_t SendMsg(int32_t sessionId, std::string &message); private: + DistributedInputTransportBase() = default; + ~DistributedInputTransportBase(); int32_t CheckDeviceSessionState(const std::string &remoteDevId); bool CheckRecivedData(const std::string &message); void HandleSession(int32_t sessionId, const std::string &message); diff --git a/services/transportbase/src/distributed_input_transport_base.cpp b/services/transportbase/src/distributed_input_transport_base.cpp index 581e81564c4e083ca604cfee9f84f4c9ebd708df..4371130aa27ce96c791b92c801862c476ba911b4 100644 --- a/services/transportbase/src/distributed_input_transport_base.cpp +++ b/services/transportbase/src/distributed_input_transport_base.cpp @@ -63,13 +63,7 @@ static SessionAttribute g_sessionAttr = { LINK_TYPE_BR } }; - -DistributedInputTransportBase &DistributedInputTransportBase::GetInstance() -{ - static DistributedInputTransportBase instance; - return instance; -} - +IMPLEMENT_SINGLE_INSTANCE(DistributedInputTransportBase); DistributedInputTransportBase::~DistributedInputTransportBase() { DHLOGI("Release Transport Session"); diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index d055a7c43582cd0ff67b77603a5007509c6e82e8..819e6f3e8af6a55cdfde675cba85ca77beef9800 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -50,9 +50,9 @@ std::string GetNodeDesc(std::string parameters); std::string GetAnonyString(const std::string &value); std::string GetAnonyInt32(const int32_t value); std::string Sha256(const std::string &string); -void CloseFd(int &fd); +void CloseFd(int fd); int BitIsSet(const unsigned long *array, int bit); -void StringSplitToVector(const std::string &str, const char split, std::vector &vecStr); +void SplitStringToVector(const std::string &str, const char split, std::vector &vecStr); int OpenInputDeviceFdByPath(const std::string &devicePath); std::string ConvertErrNo(); void ScanInputDevicesPath(const std::string &dirName, std::vector &vecInputDevPath); diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index f8f0d9325075b16071200430d3d92c7f7bb5848d..317c4431cd53dcd8b6ffb6358c06916bd85ad7e6 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -293,7 +293,7 @@ std::string Sha256(const std::string &in) return reinterpret_cast(out); } -void CloseFd(int &fd) +void CloseFd(int fd) { if (fd < 0) { DHLOGE("No fd need to beclosed."); @@ -308,10 +308,10 @@ int BitIsSet(const unsigned long *array, int bit) return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS))); } -void StringSplitToVector(const std::string &str, const char split, std::vector &vecStr) +void SplitStringToVector(const std::string &str, const char split, std::vector &vecStr) { if (str.empty()) { - DHLOGE("StringSplitToVector param str is error."); + DHLOGE("SplitStringToVector param str is error."); return; } std::string strTmp = str + split;