diff --git a/common/include/constants_dinput.h b/common/include/constants_dinput.h index d7bc53d1a2dc2b77dc3867f597506486e60193a8..4543d3268fcf5d4a690a7fee6359391edf951d98 100644 --- a/common/include/constants_dinput.h +++ b/common/include/constants_dinput.h @@ -48,7 +48,6 @@ namespace DistributedInput { const uint32_t SCREEN_MSG_MAX = 40 * 1024 * 1024; const uint32_t AUTH_SESSION_SIDE_SERVER = 0; const uint32_t IPC_VECTOR_MAX_SIZE = 32; - const uint32_t SIM_TOUCH_TRACKING_ID = 0x0001; /* * Device Type definitions diff --git a/services/common/include/dinput_softbus_define.h b/services/common/include/dinput_softbus_define.h index 2583ca2f832a38cc1c4cacdad86a16f1f9df5a34..70dd253ac072f925df4ab79688052c81c197fb1c 100644 --- a/services/common/include/dinput_softbus_define.h +++ b/services/common/include/dinput_softbus_define.h @@ -88,6 +88,7 @@ namespace DistributedInput { const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPDHID = 25; const uint32_t TRANS_SINK_MSG_ON_RELAY_STARTTYPE = 26; const uint32_t TRANS_SINK_MSG_ON_RELAY_STOPTYPE = 27; + const uint32_t TRANS_SINK_MSG_KEY_STATE_BATCH = 28; // src or sink const uint32_t TRANS_MSG_SRC_SINK_SPLIT = 30; diff --git a/services/common/include/dinput_source_trans_callback.h b/services/common/include/dinput_source_trans_callback.h index 58a6487fd96a8bd88c3cf7dc41872fa9ccb56a07..35d348ea976fe297abf1cc96fbc8a9169e9c11a5 100644 --- a/services/common/include/dinput_source_trans_callback.h +++ b/services/common/include/dinput_source_trans_callback.h @@ -33,6 +33,7 @@ public: virtual void OnResponseStopRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result) = 0; virtual void OnResponseKeyState(const std::string deviceId, const std::string &dhid, const uint32_t type, const uint32_t code, const uint32_t value) = 0; + virtual void OnResponseKeyStateBatch(const std::string deviceId, const std::string &object) = 0; virtual void OnReceivedEventRemoteInput(const std::string deviceId, const std::string &object) = 0; virtual void OnResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result, diff --git a/services/sink/transport/include/distributed_input_sink_transport.h b/services/sink/transport/include/distributed_input_sink_transport.h index e948a1c6a543039a9c99f40199a37776268615d9..df48cc2967bd8419bbbf218dd4713ba4323574d4 100644 --- a/services/sink/transport/include/distributed_input_sink_transport.h +++ b/services/sink/transport/include/distributed_input_sink_transport.h @@ -60,6 +60,7 @@ public: int32_t RespLatency(const int32_t sessionId, std::string &smsg); void SendKeyStateNodeMsg(const int32_t sessionId, const std::string &dhId, uint32_t type, const uint32_t btnCode, int32_t value); + void SendKeyStateNodeMsgBatch(const int32_t sessionId, const std::vector &events); class DInputSinkEventHandler : public AppExecFwk::EventHandler { public: @@ -93,6 +94,7 @@ private: void NotifyRelayStopTypeRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value); + void RecordEventLog(const std::vector &events); private: std::string mySessionName_; std::shared_ptr eventHandler_; diff --git a/services/sink/transport/src/distributed_input_sink_transport.cpp b/services/sink/transport/src/distributed_input_sink_transport.cpp index af2aefb903a2c7730ffee48ed2de02b2d64be541..0025ea3d3eb1115e57972e3575513bb73c66da0b 100644 --- a/services/sink/transport/src/distributed_input_sink_transport.cpp +++ b/services/sink/transport/src/distributed_input_sink_transport.cpp @@ -225,6 +225,46 @@ void DistributedInputSinkTransport::SendKeyStateNodeMsg(const int32_t sessionId, RecordEventLog(dhId, type, btnCode, value); } +void DistributedInputSinkTransport::SendKeyStateNodeMsgBatch(const int32_t sessionId, + const std::vector &events) +{ + if (sessionId <= 0) { + DHLOGE("SendKeyStateNodeMsgBatch error, sessionId <= 0."); + return; + } + DHLOGI("SendKeyStateNodeMsgBatch sessionId: %d, event size: %d ", sessionId, events.size()); + + int64_t currentTimeNs = GetCurrentTime() * 1000LL; + std::shared_ptr eventsJsonArr = std::make_shared(); + for (const auto &ev : events) { + nlohmann::json tmpJson; + tmpJson[INPUT_KEY_WHEN] = currentTimeNs; + tmpJson[INPUT_KEY_TYPE] = ev.type; + tmpJson[INPUT_KEY_CODE] = ev.code; + tmpJson[INPUT_KEY_VALUE] = ev.value; + tmpJson[INPUT_KEY_DESCRIPTOR] = ev.descriptor; + tmpJson[INPUT_KEY_PATH] = ev.path; + eventsJsonArr->push_back(tmpJson); + } + + nlohmann::json jsonStr; + jsonStr[DINPUT_SOFTBUS_KEY_CMD_TYPE] = TRANS_SINK_MSG_KEY_STATE_BATCH; + jsonStr[DINPUT_SOFTBUS_KEY_INPUT_DATA] = eventsJsonArr->dump(); + std::string msg = jsonStr.dump(); + int32_t ret = SendMessage(sessionId, msg); + if (ret != DH_SUCCESS) { + DHLOGE("SendKeyStateNodeMsgBatch error, SendMessage fail."); + } + RecordEventLog(events); +} + +void DistributedInputSinkTransport::RecordEventLog(const std::vector &events) +{ + for (auto &ev : events) { + RecordEventLog(ev.descriptor, ev.type, ev.code, ev.value); + } +} + void DistributedInputSinkTransport::RecordEventLog(const std::string &dhId, int32_t type, int32_t code, int32_t value) { std::string eventType; diff --git a/services/source/inputinject/include/distributed_input_inject.h b/services/source/inputinject/include/distributed_input_inject.h index b2855f425199fd99bee6c79d2e8096afbff6adda..7bb502817c559bbd4835d85e0244248d4f0cbf63 100644 --- a/services/source/inputinject/include/distributed_input_inject.h +++ b/services/source/inputinject/include/distributed_input_inject.h @@ -35,6 +35,7 @@ public: const std::string ¶meters); int32_t UnregisterDistributedHardware(const std::string &devId, const std::string &dhId); int32_t RegisterDistributedEvent(RawEvent *buffer, size_t bufferSize); + int32_t RegisterDistributedEvent(const std::vector &events); int32_t StructTransJson(const InputDevice &pBuf, std::string &strDescriptor); void StartInjectThread(); void StopInjectThread(); diff --git a/services/source/inputinject/include/distributed_input_node_manager.h b/services/source/inputinject/include/distributed_input_node_manager.h index 25c4dbc516c012c4b49e874d87f3b0f87925bba9..9f5ead5f9247b94138cd7d18d22a7be649c87c2d 100644 --- a/services/source/inputinject/include/distributed_input_node_manager.h +++ b/services/source/inputinject/include/distributed_input_node_manager.h @@ -47,6 +47,7 @@ public: int32_t GetDevice(const std::string &dhId, VirtualDevice *&device); void ReportEvent(const RawEvent rawEvent); + void ReportEvent(const std::vector &events); int32_t CloseDeviceLocked(const std::string &dhId); void StartInjectThread(); void StopInjectThread(); diff --git a/services/source/inputinject/src/distributed_input_inject.cpp b/services/source/inputinject/src/distributed_input_inject.cpp index 65483fef52574ad2f88c8cf5ebe03d22118332e3..d5065847a6a2b4c121309bf89ef0d4934d393994 100644 --- a/services/source/inputinject/src/distributed_input_inject.cpp +++ b/services/source/inputinject/src/distributed_input_inject.cpp @@ -147,13 +147,25 @@ int32_t DistributedInputInject::RegisterDistributedEvent(RawEvent *buffer, size_ DHLOGE("the DistributedInputNodeManager is null"); return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; } - DHLOGI("RegisterDistributedEvent start %zu\n", bufferSize); + for (size_t i = 0; i < bufferSize; i++) { inputNodeManager_->ReportEvent(buffer[i]); } return DH_SUCCESS; } +int32_t DistributedInputInject::RegisterDistributedEvent(const std::vector &events) +{ + std::lock_guard lock(inputNodeManagerMutex_); + if (inputNodeManager_ == nullptr) { + DHLOGE("the DistributedInputNodeManager is null"); + return ERR_DH_INPUT_SERVER_SOURCE_INJECT_NODE_MANAGER_IS_NULL; + } + + inputNodeManager_->ReportEvent(events); + return DH_SUCCESS; +} + void DistributedInputInject::StartInjectThread() { std::lock_guard lock(inputNodeManagerMutex_); diff --git a/services/source/inputinject/src/distributed_input_node_manager.cpp b/services/source/inputinject/src/distributed_input_node_manager.cpp index 9fc137e5ffb2833e558d7fe43a2ed0c7e9d0fcd8..4e195d6e549e16299fdb5f5db73f22b3510f11db 100644 --- a/services/source/inputinject/src/distributed_input_node_manager.cpp +++ b/services/source/inputinject/src/distributed_input_node_manager.cpp @@ -427,6 +427,15 @@ void DistributedInputNodeManager::ReportEvent(const RawEvent rawEvent) conditionVariable_.notify_all(); } +void DistributedInputNodeManager::ReportEvent(const std::vector &events) +{ + std::lock_guard lockGuard(injectThreadMutex_); + for (auto ev : events) { + injectQueue_.push(std::make_shared(ev)); + } + conditionVariable_.notify_all(); +} + void DistributedInputNodeManager::InjectEvent() { int32_t ret = pthread_setname_np(pthread_self(), EVENT_INJECT_THREAD_NAME); diff --git a/services/source/sourcemanager/include/dinput_source_listener.h b/services/source/sourcemanager/include/dinput_source_listener.h index 386da3ae4b2f4c02f6a89d60b4773d376c890487..5d5629d002fdcfb78f33314ab1970ba02924caeb 100644 --- a/services/source/sourcemanager/include/dinput_source_listener.h +++ b/services/source/sourcemanager/include/dinput_source_listener.h @@ -52,6 +52,7 @@ public: void OnResponseStopRemoteInputDhid(const std::string deviceId, const std::string &dhids, bool result) override; void OnResponseKeyState(const std::string deviceId, const std::string &dhid, const uint32_t type, const uint32_t code, const uint32_t value) override; + void OnResponseKeyStateBatch(const std::string deviceId, const std::string &event) override; void OnReceivedEventRemoteInput(const std::string deviceId, const std::string &event) override; void OnResponseRelayPrepareRemoteInput(int32_t sessionId, const std::string &deviceId, bool result, const std::string &object) override; diff --git a/services/source/sourcemanager/src/dinput_source_listener.cpp b/services/source/sourcemanager/src/dinput_source_listener.cpp index 6a84d50b08efbfbb1b354c4b2de69f5a7e59050f..1553424485c078832e5cdae718adddd44ac47831 100644 --- a/services/source/sourcemanager/src/dinput_source_listener.cpp +++ b/services/source/sourcemanager/src/dinput_source_listener.cpp @@ -325,24 +325,31 @@ void DInputSourceListener::OnResponseKeyState(const std::string deviceId, sourceManagerObj_->GetCallbackEventHandler()->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE); } -void DInputSourceListener::OnReceivedEventRemoteInput( - const std::string deviceId, const std::string &event) + +void DInputSourceListener::OnResponseKeyStateBatch(const std::string deviceId, const std::string &event) +{ + DHLOGI("OnResponseKeyStateBatch events, deviceId: %s.", GetAnonyString(deviceId).c_str()); + OnReceivedEventRemoteInput(deviceId, event); +} + +void DInputSourceListener::OnReceivedEventRemoteInput(const std::string deviceId, const std::string &event) { nlohmann::json inputData = nlohmann::json::parse(event, nullptr, false); if (inputData.is_discarded()) { DHLOGE("inputData parse failed!"); return; } - size_t jsonSize = inputData.size(); - DHLOGI("OnReceivedEventRemoteInput called, deviceId: %s, json size:%d.", - GetAnonyString(deviceId).c_str(), jsonSize); if (!inputData.is_array()) { DHLOGE("inputData not vector!"); return; } - RawEvent mEventBuffer[jsonSize]; + size_t jsonSize = inputData.size(); + DHLOGI("OnReceivedEventRemoteInput called, deviceId: %s, json size:%d.", + GetAnonyString(deviceId).c_str(), jsonSize); + + std::vector mEventBuffer(jsonSize); int idx = 0; for (auto it = inputData.begin(); it != inputData.end(); ++it) { nlohmann::json oneData = (*it); @@ -362,7 +369,8 @@ void DInputSourceListener::OnReceivedEventRemoteInput( oneData[INPUT_KEY_VALUE], oneData[INPUT_KEY_PATH]); ++idx; } - DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer, jsonSize); + + DistributedInputInject::GetInstance().RegisterDistributedEvent(mEventBuffer); } void DInputSourceListener::OnReceiveRelayPrepareResult(int32_t status, diff --git a/services/source/transport/include/distributed_input_source_transport.h b/services/source/transport/include/distributed_input_source_transport.h index 170bff4901f6f3b9c71b9900e186a971bd43cfa0..46349384b4c8d2f265c4e4b0eba8932717941e38 100644 --- a/services/source/transport/include/distributed_input_source_transport.h +++ b/services/source/transport/include/distributed_input_source_transport.h @@ -99,6 +99,7 @@ private: void NotifyResponseStartRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg); void NotifyResponseStopRemoteInputDhid(int32_t sessionId, const nlohmann::json &recMsg); void NotifyResponseKeyState(int32_t sessionId, const nlohmann::json &recMsg); + void NotifyResponseKeyStateBatch(int32_t sessionId, const nlohmann::json &recMsg); void NotifyReceivedEventRemoteInput(int32_t sessionId, const nlohmann::json &recMsg); void ReceiveSrcTSrcRelayPrepare(int32_t sessionId, const nlohmann::json &recMsg); void ReceiveSrcTSrcRelayUnprepare(int32_t sessionId, const nlohmann::json &recMsg); diff --git a/services/source/transport/src/distributed_input_source_transport.cpp b/services/source/transport/src/distributed_input_source_transport.cpp index e0a846ece477cf01a36527d573b6f9ff7ea1f00a..63ac55c3e4037e6e88efab43e01d556f3e294d51 100644 --- a/services/source/transport/src/distributed_input_source_transport.cpp +++ b/services/source/transport/src/distributed_input_source_transport.cpp @@ -79,6 +79,7 @@ void DistributedInputSourceTransport::RegRespFunMap() memberFuncMap_[TRANS_SINK_MSG_DHID_ONSTART] = &DistributedInputSourceTransport::NotifyResponseStartRemoteInputDhid; memberFuncMap_[TRANS_SINK_MSG_DHID_ONSTOP] = &DistributedInputSourceTransport::NotifyResponseStopRemoteInputDhid; memberFuncMap_[TRANS_SINK_MSG_KEY_STATE] = &DistributedInputSourceTransport::NotifyResponseKeyState; + memberFuncMap_[TRANS_SINK_MSG_KEY_STATE_BATCH] = &DistributedInputSourceTransport::NotifyResponseKeyStateBatch; memberFuncMap_[TRANS_SOURCE_TO_SOURCE_MSG_PREPARE] = &DistributedInputSourceTransport::ReceiveSrcTSrcRelayPrepare; memberFuncMap_[TRANS_SINK_MSG_ON_RELAY_PREPARE] = &DistributedInputSourceTransport::NotifyResponseRelayPrepareRemoteInput; @@ -1047,6 +1048,22 @@ void DistributedInputSourceTransport::NotifyResponseKeyState(int32_t sessionId, recMsg[DINPUT_SOFTBUS_KEY_KEYSTATE_VALUE]); } +void DistributedInputSourceTransport::NotifyResponseKeyStateBatch(int32_t sessionId, const nlohmann::json &recMsg) +{ + DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE_BATCH."); + if (!IsString(recMsg, DINPUT_SOFTBUS_KEY_INPUT_DATA)) { + DHLOGE("The DINPUT_SOFTBUS_KEY_INPUT_DATA is invalid"); + return; + } + std::string deviceId = DistributedInputTransportBase::GetInstance().GetDevIdBySessionId(sessionId); + if (deviceId.empty()) { + DHLOGE("OnBytesReceived cmdType is TRANS_SINK_MSG_KEY_STATE_BATCH, deviceId is error."); + return; + } + std::string inputDataStr = recMsg[DINPUT_SOFTBUS_KEY_INPUT_DATA]; + callback_->OnResponseKeyStateBatch(deviceId, inputDataStr); +} + void DistributedInputSourceTransport::NotifyReceivedEventRemoteInput(int32_t sessionId, const nlohmann::json &recMsg) { DHLOGI("OnBytesReceived cmdType is TRANS_SINK_MSG_BODY_DATA."); diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index fae078fcde3a8bbfbf9773dd42f257d096fcd955..357f8ef4d755e0daf31142d7fb1b14b8fb760a33 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -130,35 +130,41 @@ void DInputState::SimulateEventInjectToSrc(const int32_t sessionId, const std::v void DInputState::SimulateBtnTouchEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_ABS, ABS_MT_TRACKING_ID, SIM_TOUCH_TRACKING_ID); + int32_t simTrackingId = GetRandomInt32(); + std::vector simEvents; + RawEvent touchTrackingIdEv = { event.when, EV_ABS, ABS_MT_TRACKING_ID, simTrackingId, dhId, event.path }; + simEvents.push_back(touchTrackingIdEv); + std::pair absPos = GetAndClearABSPosition(event.descriptor); if (absPos.first != -1) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_ABS, ABS_MT_POSITION_X, absPos.first); + RawEvent absMTXEv = { event.when, EV_ABS, ABS_MT_POSITION_X, absPos.first, dhId, event.path }; + simEvents.push_back(absMTXEv); } if (absPos.second != -1) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_ABS, ABS_MT_POSITION_Y, absPos.second); + RawEvent absMTYEv = { event.when, EV_ABS, ABS_MT_POSITION_Y, absPos.second, dhId, event.path }; + simEvents.push_back(absMTYEv); } - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_KEY, event.code, KEY_DOWN_STATE); - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_KEY, BTN_TOOL_FINGER, KEY_DOWN_STATE); + RawEvent keyDownEv = { event.when, EV_KEY, event.code, KEY_DOWN_STATE, dhId, event.path }; + simEvents.push_back(keyDownEv); + RawEvent fingerEv = { event.when, EV_KEY, BTN_TOOL_FINGER, KEY_DOWN_STATE, dhId, event.path }; + simEvents.push_back(fingerEv); if (absPos.first != -1) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_ABS, ABS_X, absPos.first); + RawEvent absXEv = { event.when, EV_ABS, ABS_X, absPos.first, dhId, event.path }; + simEvents.push_back(absXEv); } if (absPos.second != -1) { - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_ABS, ABS_Y, absPos.second); + RawEvent absYEv = { event.when, EV_ABS, ABS_Y, absPos.second, dhId, event.path }; + simEvents.push_back(absYEv); } - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_MSC, MSC_TIMESTAMP, 0x0); - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, - EV_SYN, SYN_REPORT, 0x0); + + RawEvent mscEv = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path }; + simEvents.push_back(mscEv); + RawEvent sycReportEv = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; + simEvents.push_back(sycReportEv); + + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(sessionId, simEvents); } void DInputState::SimulateNormalEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event) diff --git a/utils/include/dinput_utils_tool.h b/utils/include/dinput_utils_tool.h index 3176520fa382edc92a747ba0da96f6fe3db17d86..b41e56450a8738d54056e1b459f4f90d0961a7f5 100644 --- a/utils/include/dinput_utils_tool.h +++ b/utils/include/dinput_utils_tool.h @@ -65,6 +65,7 @@ void ScanInputDevicesPath(const std::string &dirName, std::vector & void ResetVirtualDevicePressedKeys(const std::vector &nodePaths); std::string GetString(const std::vector &vec); +int32_t GetRandomInt32(); } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/dinput_utils_tool.cpp b/utils/src/dinput_utils_tool.cpp index 9eb87b7ab86662632a1aca0832f2ce7c1b64a4ea..bb0b59cf6f756d58c44e5f3d53b72ede0a3df61d 100644 --- a/utils/src/dinput_utils_tool.cpp +++ b/utils/src/dinput_utils_tool.cpp @@ -15,8 +15,11 @@ #include "dinput_utils_tool.h" +#include #include #include +#include + #include #include #include @@ -439,6 +442,14 @@ std::string GetString(const std::vector &vec) retStr += "]"; return retStr; } + +int32_t GetRandomInt32() +{ + std::default_random_engine engine(time(nullptr)); + + std::uniform_int_distribution distribution(0, INT32_MAX); + return distribution(engine); +} } // namespace DistributedInput } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file