From fc5c52f5f20b2b76c6d4b9d2c0ad4b7dcef36e9b Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Wed, 27 Dec 2023 22:02:17 +0800 Subject: [PATCH 1/3] improve touchpad reset functions Signed-off-by: hwzhangchuang --- bundle.json | 2 +- common/include/input_hub.cpp | 62 +++-- common/include/input_hub.h | 4 +- inputdevicehandler/BUILD.gn | 2 +- services/sink/inputcollector/BUILD.gn | 2 +- .../test/sinkcollectorunittest/BUILD.gn | 2 +- services/sink/sinkmanager/BUILD.gn | 2 +- .../include/distributed_input_sink_manager.h | 2 +- .../src/distributed_input_sink_manager.cpp | 16 +- .../test/sinkmanagerunittest/BUILD.gn | 2 +- .../transport/test/sinktransunittest/BUILD.gn | 2 +- services/source/inputinject/BUILD.gn | 2 +- .../test/sourcemanagerunittest/BUILD.gn | 2 +- .../test/sourcetransunittest/BUILD.gn | 2 +- services/state/BUILD.gn | 10 +- .../{dinput_state.h => dinput_sink_state.h} | 27 ++- .../state/include/touchpad_event_fragment.h | 38 +++ .../include/touchpad_event_fragment_mgr.h | 57 +++++ ...dinput_state.cpp => dinput_sink_state.cpp} | 224 +++++------------- .../state/src/touchpad_event_fragment.cpp | 66 ++++++ .../state/src/touchpad_event_fragment_mgr.cpp | 113 +++++++++ 21 files changed, 406 insertions(+), 233 deletions(-) rename services/state/include/{dinput_state.h => dinput_sink_state.h} (82%) create mode 100644 services/state/include/touchpad_event_fragment.h create mode 100644 services/state/include/touchpad_event_fragment_mgr.h rename services/state/src/{dinput_state.cpp => dinput_sink_state.cpp} (42%) create mode 100644 services/state/src/touchpad_event_fragment.cpp create mode 100644 services/state/src/touchpad_event_fragment_mgr.cpp diff --git a/bundle.json b/bundle.json index 47194c0..74a07cc 100755 --- a/bundle.json +++ b/bundle.json @@ -64,7 +64,7 @@ "//foundation/distributedhardware/distributed_input/services/sink/transport:libdinput_sink_trans", "//foundation/distributedhardware/distributed_input/services/sink/inputcollector:libdinput_collector", "//foundation/distributedhardware/distributed_input/services/transportbase:libdinput_trans_base", - "//foundation/distributedhardware/distributed_input/services/state:libdinput_state", + "//foundation/distributedhardware/distributed_input/services/state:libdinput_sink_state", "//foundation/distributedhardware/distributed_input/sourcehandler:libdinput_source_handler", "//foundation/distributedhardware/distributed_input/sinkhandler:libdinput_sink_handler", "//foundation/distributedhardware/distributed_input/inputdevicehandler:libdinput_handler", diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 4b7fc7e..ef403e1 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -33,7 +33,7 @@ #include "dinput_context.h" #include "dinput_errcode.h" #include "dinput_log.h" -#include "dinput_state.h" +#include "dinput_sink_state.h" #include "dinput_utils_tool.h" namespace OHOS { @@ -220,49 +220,43 @@ bool InputHub::IsTouchPad(const InputDevice &inputDevice) void InputHub::MatchAndDealEvent(Device *device, const RawEvent &event) { - // Deal key state - DealKeyEvent(event); - - if (IsTouchPad(device) && event.type == EV_KEY && event.code == BTN_MOUSE && event.value == KEY_UP_STATE && - !DInputState::GetInstance().IsDhIdDown(event.descriptor)) { - DHLOGI("Find touchpad BTN_MOUSE UP state that not down effective at sink side, dhId: %s", - event.descriptor.c_str()); - DInputState::GetInstance().SimulateTouchPadBtnMouseUpState(event.descriptor, event); - } - - if (IsTouchPad(device) && event.type == EV_KEY && event.code == BTN_TOUCH && event.value == KEY_UP_STATE && - !DInputState::GetInstance().IsDhIdDown(event.descriptor)) { - DHLOGI("Find touchpad BTN_TOUCH UP state that not down effective at sink side, dhId: %s", - event.descriptor.c_str()); - DInputState::GetInstance().SimulateTouchPadBtnTouchUpState(event.descriptor, event); + bool isTouchPad = IsTouchPad(device); + if (!isTouchPad) { + // Deal Normal key state, such as keys of keyboard or mouse + DealNormalKeyEvent(device, event); + } else { + // Deal TouchPad events + DealTouchPadEvent(event); } +} - if (IsCuror(device) && event.type == EV_KEY && event.code == BTN_MOUSE && event.value == KEY_UP_STATE && - !DInputState::GetInstance().IsDhIdDown(event.descriptor)) { - DHLOGI("Find mouse BTN_MOUSE UP state that not down effective at sink side, dhId: %s", - event.descriptor.c_str()); - DInputState::GetInstance().SimulateMouseBtnMouseUpState(event.descriptor, event); +void InputHub::DealTouchPadEvent(const RawEvent &event) +{ + auto ret = DInputSinkState::GetInstance().GetTouchPadEventFragMgr()->PushEvent(event.descriptor, event); + if (ret.first) { + DInputSinkState::GetInstance().SimulateTouchPadStateReset(ret.second); } } -void InputHub::DealKeyEvent(const RawEvent &event) +void InputHub::DealNormalKeyEvent(Device *device, const RawEvent &event) { - if (event.type == EV_KEY && event.code != BTN_TOOL_FINGER && event.value == KEY_DOWN_STATE) { - DInputState::GetInstance().AddKeyDownState(event); + if (event.type == EV_KEY && event.value == KEY_DOWN_STATE) { + DInputSinkState::GetInstance().AddKeyDownState(event); RecordChangeEventLog(event); } if (event.type == EV_KEY && event.value == KEY_UP_STATE) { - DInputState::GetInstance().RemoveKeyDownState(event); + // Deal mouse left keydown reset + if (IsCuror(device) && event.code == BTN_MOUSE && + !DInputSinkState::GetInstance().IsDhIdDown(event.descriptor)) { + DHLOGI("Find mouse BTN_MOUSE UP state that not down effective at sink side, dhId: %s", + event.descriptor.c_str()); + DInputSinkState::GetInstance().SimulateMouseBtnMouseUpState(event.descriptor, event); + } + DInputSinkState::GetInstance().RemoveKeyDownState(event); RecordChangeEventLog(event); } if (event.type == EV_KEY && event.value == KEY_REPEAT) { - DInputState::GetInstance().CheckAndSetLongPressedKeyOrder(event); - } - if (event.type == EV_ABS && (event.code == ABS_MT_POSITION_X || event.code == ABS_X)) { - DInputState::GetInstance().RefreshABSPosition(event.descriptor, event.value, -1); - } - if (event.type == EV_ABS && (event.code == ABS_MT_POSITION_Y || event.code == ABS_Y)) { - DInputState::GetInstance().RefreshABSPosition(event.descriptor, -1, event.value); + DInputSinkState::GetInstance().CheckAndSetLongPressedKeyOrder(event); } } @@ -1457,7 +1451,7 @@ void InputHub::SavePressedKeyState(const InputHub::Device *dev, int32_t keyCode) .descriptor = dev->identifier.descriptor, .path = dev->path }; - DInputState::GetInstance().AddKeyDownState(event); + DInputSinkState::GetInstance().AddKeyDownState(event); DHLOGI("Find Pressed key: %d, device path: %s, dhId: %s", keyCode, dev->path.c_str(), dev->identifier.descriptor.c_str()); } @@ -1559,7 +1553,7 @@ void InputHub::RecordDeviceStates() void InputHub::ClearDeviceStates() { DHLOGI("Clear Device state"); - DInputState::GetInstance().ClearDeviceStates(); + DInputSinkState::GetInstance().ClearDeviceStates(); } void InputHub::ClearSkipDevicePaths() diff --git a/common/include/input_hub.h b/common/include/input_hub.h index e8bc03e..a12e230 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -182,7 +183,8 @@ private: */ void RecordDeviceChangeStates(Device *device, struct input_event readBuffer[], const size_t count); void MatchAndDealEvent(Device *device, const RawEvent &event); - void DealKeyEvent(const RawEvent &event); + void DealTouchPadEvent(const RawEvent &event); + void DealNormalKeyEvent(Device *device, const RawEvent &event); /* * Scan the input device node and save info. */ diff --git a/inputdevicehandler/BUILD.gn b/inputdevicehandler/BUILD.gn index 10e95fb..e5ec72c 100755 --- a/inputdevicehandler/BUILD.gn +++ b/inputdevicehandler/BUILD.gn @@ -52,7 +52,7 @@ ohos_shared_library("libdinput_handler") { ] deps = [ - "${services_state_path}:libdinput_state", + "${services_state_path}:libdinput_sink_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_shared", diff --git a/services/sink/inputcollector/BUILD.gn b/services/sink/inputcollector/BUILD.gn index 7ff67cd..d897d12 100755 --- a/services/sink/inputcollector/BUILD.gn +++ b/services/sink/inputcollector/BUILD.gn @@ -51,7 +51,7 @@ ohos_shared_library("libdinput_collector") { ] deps = [ - "${services_state_path}:libdinput_state", + "${services_state_path}:libdinput_sink_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_shared", diff --git a/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn b/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn index 7fa5be6..1e82d68 100644 --- a/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn +++ b/services/sink/inputcollector/test/sinkcollectorunittest/BUILD.gn @@ -62,7 +62,7 @@ ohos_unittest("distributed_input_inner_sinkcollector_test") { ] deps = [ - "${services_state_path}:libdinput_state", + "${services_state_path}:libdinput_sink_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_shared", diff --git a/services/sink/sinkmanager/BUILD.gn b/services/sink/sinkmanager/BUILD.gn index 08b08ea..d914f57 100644 --- a/services/sink/sinkmanager/BUILD.gn +++ b/services/sink/sinkmanager/BUILD.gn @@ -63,7 +63,7 @@ ohos_shared_library("libdinput_sink") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${distributedinput_path}/services/state:libdinput_state", + "${distributedinput_path}/services/state:libdinput_sink_state", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${innerkits_path}:libdinput_sdk", "${services_sink_path}/inputcollector:libdinput_collector", diff --git a/services/sink/sinkmanager/include/distributed_input_sink_manager.h b/services/sink/sinkmanager/include/distributed_input_sink_manager.h index 67a21ea..9595857 100644 --- a/services/sink/sinkmanager/include/distributed_input_sink_manager.h +++ b/services/sink/sinkmanager/include/distributed_input_sink_manager.h @@ -38,7 +38,7 @@ #include "dinput_sink_trans_callback.h" #include "distributed_input_sink_stub.h" #include "distributed_input_sink_event_handler.h" -#include "dinput_state.h" +#include "dinput_sink_state.h" namespace OHOS { namespace DistributedHardware { diff --git a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp index 50173bb..38680bf 100644 --- a/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp +++ b/services/sink/sinkmanager/src/distributed_input_sink_manager.cpp @@ -242,7 +242,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInput( DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); std::vector devDhIds; SplitStringToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, devDhIds); - DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId); + DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId); } } } @@ -302,7 +302,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStartRemoteInputDhid(con std::vector devDhIds; SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, devDhIds); - DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId); + DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, sessionId); AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, devDhIds); sinkManagerObj_->StoreStartDhids(sessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); @@ -321,7 +321,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnStopRemoteInputDhid(cons stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - DInputState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, sessionId); + DInputSinkState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, sessionId); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", sessionId); @@ -369,7 +369,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartDhidRemoteInpu std::vector devDhIds; SplitStringToVector(strDhids, INPUT_STRING_SPLIT_POINT, devDhIds); - DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId); + DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId); AffectDhIds affDhIds = DistributedInputCollector::GetInstance().SetSharingDhIds(true, devDhIds); sinkManagerObj_->StoreStartDhids(toSinkSessionId, affDhIds.sharingDhIds); DistributedInputCollector::GetInstance().ReportDhIdSharingState(affDhIds); @@ -388,7 +388,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStopDhidRemoteInput stopIndeedOnes.noSharingDhIds = stopIndeedDhIds; DistributedInputCollector::GetInstance().ReportDhIdSharingState(stopIndeedOnes); - DInputState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, toSinkSessionId); + DInputSinkState::GetInstance().RecordDhIds(stopOnCmdDhIds, DhIdState::THROUGH_IN, toSinkSessionId); if (DistributedInputCollector::GetInstance().IsAllDevicesStoped()) { DHLOGE("All dhid stop sharing, sessionId: %d is closed.", toSinkSessionId); @@ -463,7 +463,7 @@ void DistributedInputSinkManager::DInputSinkListener::OnRelayStartTypeRemoteInpu DHLOGI("deviceInfo dhId, %s", GetAnonyString(deviceInfo.second).c_str()); std::vector devDhIds; SplitStringToVector(deviceInfo.second, INPUT_STRING_SPLIT_POINT, devDhIds); - DInputState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId); + DInputSinkState::GetInstance().RecordDhIds(devDhIds, DhIdState::THROUGH_OUT, toSinkSessionId); } } @@ -611,9 +611,9 @@ int32_t DistributedInputSinkManager::Init() return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; } - ret = DInputState::GetInstance().Init(); + ret = DInputSinkState::GetInstance().Init(); if (ret != DH_SUCCESS) { - DHLOGE("DInputState init fail!"); + DHLOGE("DInputSinkState init fail!"); return ERR_DH_INPUT_SERVER_SINK_MANAGER_INIT_FAIL; } diff --git a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn index 7a71503..61fcfe1 100755 --- a/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn +++ b/services/sink/sinkmanager/test/sinkmanagerunittest/BUILD.gn @@ -92,7 +92,7 @@ ohos_unittest("distributed_input_sinkmanager_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${distributedinput_path}/services/state:libdinput_state", + "${distributedinput_path}/services/state:libdinput_sink_state", "${distributedinput_path}/services/transportbase:libdinput_trans_base", "${services_sink_path}/transport:libdinput_sink_trans", "${utils_path}:libdinput_utils", diff --git a/services/sink/transport/test/sinktransunittest/BUILD.gn b/services/sink/transport/test/sinktransunittest/BUILD.gn index a0972ee..c0495fd 100755 --- a/services/sink/transport/test/sinktransunittest/BUILD.gn +++ b/services/sink/transport/test/sinktransunittest/BUILD.gn @@ -79,7 +79,7 @@ ohos_unittest("distributed_input_sinktrans_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${distributedinput_path}/services/state:libdinput_state", + "${distributedinput_path}/services/state:libdinput_sink_state", "${services_sink_path}/inputcollector:libdinput_collector", "${services_sink_path}/sinkmanager:libdinput_sink", "${utils_path}:libdinput_utils", diff --git a/services/source/inputinject/BUILD.gn b/services/source/inputinject/BUILD.gn index 5b7a6da..7e30f63 100644 --- a/services/source/inputinject/BUILD.gn +++ b/services/source/inputinject/BUILD.gn @@ -57,7 +57,7 @@ ohos_shared_library("libdinput_inject") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${services_state_path}:libdinput_state", + "${services_state_path}:libdinput_sink_state", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", "//third_party/openssl:libcrypto_shared", diff --git a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn index 1b5e06b..636db2d 100755 --- a/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn +++ b/services/source/sourcemanager/test/sourcemanagerunittest/BUILD.gn @@ -111,7 +111,7 @@ ohos_unittest("distributed_input_sourcemanager_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${distributedinput_path}/services/state:libdinput_state", + "${distributedinput_path}/services/state:libdinput_sink_state", "${innerkits_path}:libdinput_sdk", "${services_source_path}/transport:libdinput_source_trans", "${utils_path}:libdinput_utils", diff --git a/services/source/transport/test/sourcetransunittest/BUILD.gn b/services/source/transport/test/sourcetransunittest/BUILD.gn index d5ffdbe..5300f8d 100755 --- a/services/source/transport/test/sourcetransunittest/BUILD.gn +++ b/services/source/transport/test/sourcetransunittest/BUILD.gn @@ -75,7 +75,7 @@ ohos_unittest("distributed_input_sourcetrans_test") { deps = [ "${dfx_utils_path}:libdinput_dfx_utils", - "${distributedinput_path}/services/state:libdinput_state", + "${distributedinput_path}/services/state:libdinput_sink_state", "${services_source_path}/inputinject:libdinput_inject", "${services_source_path}/sourcemanager:libdinput_source", "${utils_path}:libdinput_utils", diff --git a/services/state/BUILD.gn b/services/state/BUILD.gn index bde060c..085ea08 100644 --- a/services/state/BUILD.gn +++ b/services/state/BUILD.gn @@ -15,7 +15,7 @@ import("//build/ohos.gni") import( "//foundation/distributedhardware/distributed_input/distributedinput.gni") -ohos_shared_library("libdinput_state") { +ohos_shared_library("libdinput_sink_state") { sanitize = { boundary_sanitize = true integer_overflow = true @@ -38,11 +38,15 @@ ohos_shared_library("libdinput_state") { "${fwk_common_path}/utils/include", ] - sources = [ "src/dinput_state.cpp" ] + sources = [ + "src/dinput_sink_state.cpp", + "src/touchpad_event_fragment.cpp", + "src/touchpad_event_fragment_mgr.cpp" + ] defines = [ "HI_LOG_ENABLE", - "DH_LOG_TAG=\"distributedinputstate\"", + "DH_LOG_TAG=\"distributeDInputSinkState\"", "LOG_DOMAIN=0xD004100", ] diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_sink_state.h similarity index 82% rename from services/state/include/dinput_state.h rename to services/state/include/dinput_sink_state.h index c075d33..5844b1f 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_sink_state.h @@ -17,12 +17,14 @@ #define DISTRIBUTED_INPUT_STATE_BASE_H #include +#include #include #include #include -#include "constants_dinput.h" +#include "constants_dinput.h" #include "single_instance.h" +#include "touchpad_event_fragment_mgr.h" namespace OHOS { namespace DistributedHardware { @@ -37,8 +39,8 @@ enum class DhIdState { THROUGH_OUT, }; -class DInputState { - DECLARE_SINGLE_INSTANCE_BASE(DInputState); +class DInputSinkState { + DECLARE_SINGLE_INSTANCE_BASE(DInputSinkState); public: int32_t Init(); int32_t Release(); @@ -61,13 +63,8 @@ public: * Clear Device stats if unprepare. */ void ClearDeviceStates(); - - void RefreshABSPosition(const std::string &dhId, int32_t absX, int32_t absY); - std::pair GetAndClearABSPosition(const std::string &dhId); - - void SimulateTouchPadBtnMouseUpState(const std::string &dhId, const struct RawEvent &event); - void SimulateTouchPadBtnTouchUpState(const std::string &dhId, const struct RawEvent &event); void SimulateMouseBtnMouseUpState(const std::string &dhId, const struct RawEvent &event); + void SimulateTouchPadStateReset(const std::vector &events); /** * @brief check is one device in down state * @@ -76,13 +73,15 @@ public: * @return false NOT in down state */ bool IsDhIdDown(const std::string &dhId); + std::shared_ptr GetTouchPadEventFragMgr(); private: - DInputState() = default; - ~DInputState(); + DInputSinkState() = default; + ~DInputSinkState(); // Simulate device state to the pass through target device. void SimulateEventInjectToSrc(const int32_t sessionId, const std::vector &dhIds); - void SimulateBtnTouchEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event); - void SimulateNormalEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event); + void SimulateKeyDownEvents(const int32_t sessionId, const std::string &dhId); + void SimulateKeyDownEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event); + void SimulateTouchPadEvents(const int32_t sessionId, const std::string &dhId); private: std::mutex operationMutex_; std::map dhIdStateMap_; @@ -95,6 +94,8 @@ private: // Record abs x/y of touchpad std::unordered_map> absPositionsMap_; std::atomic lastSessionId_ {0}; + + std::shared_ptr touchPadEventFragMgr_; }; } // namespace DistributedInput } // namespace DistributedHardware diff --git a/services/state/include/touchpad_event_fragment.h b/services/state/include/touchpad_event_fragment.h new file mode 100644 index 0000000..fdae11c --- /dev/null +++ b/services/state/include/touchpad_event_fragment.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#include "constants_dinput.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class TouchPadEventFragment { +public: + TouchPadEventFragment() : events({}) {}; + + bool IsShouldDrop(); + bool IsTouchPadOptFinish() const; + bool IsTouchPadOptStart() const; + void PushEvent(const RawEvent &event); + std::vector GetEvents(); +private: + std::vector events; +}; +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/state/include/touchpad_event_fragment_mgr.h b/services/state/include/touchpad_event_fragment_mgr.h new file mode 100644 index 0000000..3bce00e --- /dev/null +++ b/services/state/include/touchpad_event_fragment_mgr.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "touchpad_event_fragment.h" + +#include +#include +#include +#include + +#include "constants_dinput.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class TouchPadEventFragmentMgr { +public: + TouchPadEventFragmentMgr() : fragments_({}) {} + + /** + * @brief Push the touchpad event + * + * @param dhId the device dhId where event from + * @param event the event + * @return std::pair> + * true for NOT whole touchpad event that need simulate back to the other device. + * false for DO Nothing. + */ + std::pair> PushEvent(const std::string &dhId, const RawEvent &event); + void Clear(const std::string &dhId); + std::vector GetAndClearEvents(const std::string &dhId); + +private: + bool IsPositionEvent(const RawEvent &event); + bool IsSynEvent(const RawEvent &event); + bool IsWholeTouchFragments(const std::vector &fragments); + std::pair> DealSynEvent(const std::string &dhId); +private: + std::mutex fragmentsMtx_; + // record the event fragments for the dhid. { dhId, { events }} + std::map> fragments_; +}; +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_sink_state.cpp similarity index 42% rename from services/state/src/dinput_state.cpp rename to services/state/src/dinput_sink_state.cpp index 66cd0d2..345a681 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_sink_state.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include "dinput_state.h" +#include "dinput_sink_state.h" #include #include @@ -31,34 +31,31 @@ namespace OHOS { namespace DistributedHardware { namespace DistributedInput { -IMPLEMENT_SINGLE_INSTANCE(DInputState); -DInputState::~DInputState() +IMPLEMENT_SINGLE_INSTANCE(DInputSinkState); +DInputSinkState::~DInputSinkState() { Release(); } -int32_t DInputState::Init() +int32_t DInputSinkState::Init() { - DHLOGI("DInputState Init."); + DHLOGI("DInputSinkState Init."); + touchPadEventFragMgr_ = std::make_shared(); return DH_SUCCESS; } -int32_t DInputState::Release() +int32_t DInputSinkState::Release() { - DHLOGI("DInputState Release."); + DHLOGI("DInputSinkState Release."); { std::lock_guard mapLock(operationMutex_); dhIdStateMap_.clear(); } ClearDeviceStates(); - { - std::lock_guard lock(absPosMtx_); - absPositionsMap_.clear(); - } return DH_SUCCESS; } -int32_t DInputState::RecordDhIds(const std::vector &dhIds, DhIdState state, const int32_t sessionId) +int32_t DInputSinkState::RecordDhIds(const std::vector &dhIds, DhIdState state, const int32_t sessionId) { DHLOGI("RecordDhIds dhIds size = %zu", dhIds.size()); std::lock_guard mapLock(operationMutex_); @@ -74,7 +71,7 @@ int32_t DInputState::RecordDhIds(const std::vector &dhIds, DhIdStat return DH_SUCCESS; } -int32_t DInputState::RemoveDhIds(const std::vector &dhIds) +int32_t DInputSinkState::RemoveDhIds(const std::vector &dhIds) { DHLOGI("RemoveDhIds dhIds size = %zu", dhIds.size()); std::lock_guard mapLock(operationMutex_); @@ -85,7 +82,12 @@ int32_t DInputState::RemoveDhIds(const std::vector &dhIds) return DH_SUCCESS; } -DhIdState DInputState::GetStateByDhid(const std::string &dhId) +std::shared_ptr DInputSinkState::GetTouchPadEventFragMgr() +{ + return this->touchPadEventFragMgr_; +} + +DhIdState DInputSinkState::GetStateByDhid(const std::string &dhId) { std::lock_guard mapLock(operationMutex_); if (dhIdStateMap_.find(dhId) == dhIdStateMap_.end()) { @@ -95,7 +97,25 @@ DhIdState DInputState::GetStateByDhid(const std::string &dhId) return dhIdStateMap_[dhId]; } -void DInputState::SimulateEventInjectToSrc(const int32_t sessionId, const std::vector &dhIds) +void DInputSinkState::SimulateMouseBtnMouseUpState(const std::string &dhId, const struct RawEvent &event) +{ + DHLOGI("Sinmulate Mouse BTN_MOUSE UP state to source, dhId: %s", dhId.c_str()); + int32_t scanId = GetRandomInt32(); + RawEvent mscScanEv = { event.when, EV_MSC, MSC_SCAN, scanId, dhId, event.path }; + RawEvent btnMouseUpEv = { event.when, EV_KEY, BTN_MOUSE, KEY_UP_STATE, dhId, event.path }; + RawEvent sycReportEv = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; + + std::vector simEvents = { mscScanEv, btnMouseUpEv, sycReportEv }; + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(lastSessionId_, simEvents); +} + +void DInputSinkState::SimulateTouchPadStateReset(const std::vector &events) +{ + DHLOGI("SimulateTouchPadStateReset events size: %d", events.size()); + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(lastSessionId_, events); +} + +void DInputSinkState::SimulateEventInjectToSrc(const int32_t sessionId, const std::vector &dhIds) { DHLOGI("SimulateEventInject enter, sessionId %d, dhIds size %d", sessionId, dhIds.size()); // mouse/keyboard/touchpad/touchscreen event send to remote device if these device pass through. @@ -105,139 +125,31 @@ void DInputState::SimulateEventInjectToSrc(const int32_t sessionId, const std::v } for (const std::string &dhId : dhIds) { - // check if this device is key event - std::lock_guard mapLock(keyDownStateMapMtx_); - auto iter = keyDownStateMap_.find(dhId); - if (iter == keyDownStateMap_.end()) { - DHLOGI("The shared Device not has down state key, dhId: %s", dhId.c_str()); - continue; - } - - for (const auto &event : iter->second) { - if (event.code == BTN_TOUCH) { - DHLOGI("Simulate Touch Down event for device path: %s, dhId: %s", - event.path.c_str(), event.descriptor.c_str()); - SimulateBtnTouchEvent(sessionId, dhId, event); - } else { - DHLOGI("Simulate Key event for device path: %s, dhId: %s", - event.path.c_str(), event.descriptor.c_str()); - SimulateNormalEvent(sessionId, dhId, event); - } - } - - keyDownStateMap_.erase(dhId); + SimulateKeyDownEvents(sessionId, dhId); + SimulateTouchPadEvents(sessionId, dhId); } } -void DInputState::SimulateBtnTouchEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event) +void DInputSinkState::SimulateKeyDownEvents(const int32_t sessionId, const std::string &dhId) { - 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) { - RawEvent absMTXEv = { event.when, EV_ABS, ABS_MT_POSITION_X, absPos.first, dhId, event.path }; - simEvents.push_back(absMTXEv); - } - if (absPos.second != -1) { - RawEvent absMTYEv = { event.when, EV_ABS, ABS_MT_POSITION_Y, absPos.second, dhId, event.path }; - simEvents.push_back(absMTYEv); + // check if this device is key event + std::lock_guard mapLock(keyDownStateMapMtx_); + auto iter = keyDownStateMap_.find(dhId); + if (iter == keyDownStateMap_.end()) { + DHLOGI("The shared Device not has down state key, dhId: %s", dhId.c_str()); + return; } - 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) { - RawEvent absXEv = { event.when, EV_ABS, ABS_X, absPos.first, dhId, event.path }; - simEvents.push_back(absXEv); - } - if (absPos.second != -1) { - RawEvent absYEv = { event.when, EV_ABS, ABS_Y, absPos.second, dhId, event.path }; - simEvents.push_back(absYEv); + for (const auto &event : iter->second) { + DHLOGI("Simulate Key event for device path: %s, dhId: %s", + event.path.c_str(), event.descriptor.c_str()); + SimulateKeyDownEvent(sessionId, dhId, event); } - 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); + keyDownStateMap_.erase(dhId); } -void DInputState::SimulateTouchPadBtnMouseUpState(const std::string &dhId, const struct RawEvent &event) -{ - std::pair touchPos = GetAndClearABSPosition(dhId); - int32_t dx = touchPos.first; - int32_t dy = touchPos.second; - DHLOGI("Sinmulate touch pad BTN_MOUSE UP state to source, dhId: %s, dx: %d, dy: %d", dhId.c_str(), dx, dy); - int32_t simTrackingId = GetRandomInt32(); - RawEvent touchTrackingIdEv1 = { event.when, EV_ABS, ABS_MT_TRACKING_ID, simTrackingId, dhId, event.path }; - RawEvent btnToolFingerDownEv = { event.when, EV_KEY, BTN_TOOL_FINGER, KEY_DOWN_STATE, dhId, event.path }; - RawEvent btnToolDoubleTapUpEv = { event.when, EV_KEY, BTN_TOOL_DOUBLETAP, KEY_UP_STATE, dhId, event.path }; - RawEvent mscEv1 = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path }; - RawEvent sycReportEv1 = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; - - RawEvent absMtSlot = { event.when, EV_ABS, ABS_MT_SLOT, 0x0, dhId, event.path }; - RawEvent absMtPosX1 = { event.when, EV_ABS, ABS_MT_POSITION_X, dx, dhId, event.path }; - RawEvent absMtPosY1 = { event.when, EV_ABS, ABS_MT_POSITION_Y, dy, dhId, event.path }; - RawEvent absPosX1 = { event.when, EV_ABS, ABS_X, dx, dhId, event.path }; - RawEvent absPosY1 = { event.when, EV_ABS, ABS_Y, dy, dhId, event.path }; - RawEvent mscEv2 = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path }; - RawEvent sycReportEv2 = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; - - RawEvent absMtPosX2 = { event.when, EV_ABS, ABS_MT_POSITION_X, dx, dhId, event.path }; - RawEvent absMtPosY2 = { event.when, EV_ABS, ABS_MT_POSITION_Y, dy, dhId, event.path }; - RawEvent btnMouseUpEv = { event.when, EV_KEY, BTN_MOUSE, KEY_UP_STATE, dhId, event.path }; - RawEvent absPosX2 = { event.when, EV_ABS, ABS_X, dx, dhId, event.path }; - RawEvent absPosY2 = { event.when, EV_ABS, ABS_Y, dy, dhId, event.path }; - RawEvent mscEv3 = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path }; - RawEvent sycReportEv3 = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; - - RawEvent touchTrackingIdEv2 = { event.when, EV_ABS, ABS_MT_TRACKING_ID, simTrackingId, dhId, event.path }; - RawEvent btnTouchUpEv = { event.when, EV_KEY, BTN_TOUCH, KEY_UP_STATE, dhId, event.path }; - RawEvent btnToolFingerUpEv = { event.when, EV_KEY, BTN_TOOL_FINGER, KEY_UP_STATE, dhId, event.path }; - RawEvent mscEv4 = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path }; - RawEvent sycReportEv4 = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; - - std::vector simEvents = { - touchTrackingIdEv1, btnToolFingerDownEv, btnToolDoubleTapUpEv, mscEv1, sycReportEv1, - absMtSlot, absMtPosX1, absMtPosY1, absPosX1, absPosY1, mscEv2, sycReportEv2, - absMtPosX2, absMtPosY2, btnMouseUpEv, absPosX2, absPosY2, mscEv3, sycReportEv3, - touchTrackingIdEv2, btnTouchUpEv, btnToolFingerUpEv, mscEv4, sycReportEv4 }; - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(lastSessionId_, simEvents); -} - -void DInputState::SimulateTouchPadBtnTouchUpState(const std::string &dhId, const struct RawEvent &event) -{ - DHLOGI("Sinmulate touch pad BTN_TOUCH UP state to source, dhId: %s", dhId.c_str()); - int32_t simTrackingId = GetRandomInt32(); - RawEvent touchTrackingIdEv = { event.when, EV_ABS, ABS_MT_TRACKING_ID, simTrackingId, dhId, event.path }; - RawEvent btnTouchUpEv = { event.when, EV_KEY, BTN_TOUCH, KEY_UP_STATE, dhId, event.path }; - RawEvent btnToolFingerUpEv = { event.when, EV_KEY, BTN_TOOL_FINGER, KEY_UP_STATE, dhId, event.path }; - RawEvent mscEv = { event.when, EV_MSC, MSC_TIMESTAMP, 0x0, dhId, event.path }; - RawEvent sycReportEv = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; - - std::vector simEvents = { touchTrackingIdEv, btnTouchUpEv, btnToolFingerUpEv, mscEv, sycReportEv }; - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(lastSessionId_, simEvents); -} - -void DInputState::SimulateMouseBtnMouseUpState(const std::string &dhId, const struct RawEvent &event) -{ - DHLOGI("Sinmulate Mouse BTN_MOUSE UP state to source, dhId: %s", dhId.c_str()); - int32_t scanId = GetRandomInt32(); - RawEvent mscScanEv = { event.when, EV_MSC, MSC_SCAN, scanId, dhId, event.path }; - RawEvent btnMouseUpEv = { event.when, EV_KEY, BTN_MOUSE, KEY_UP_STATE, dhId, event.path }; - RawEvent sycReportEv = { event.when, EV_SYN, SYN_REPORT, 0x0, dhId, event.path }; - - std::vector simEvents = { mscScanEv, btnMouseUpEv, sycReportEv }; - DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(lastSessionId_, simEvents); -} - -void DInputState::SimulateNormalEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event) +void DInputSinkState::SimulateKeyDownEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event) { DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, EV_KEY, event.code, KEY_DOWN_STATE); @@ -245,45 +157,31 @@ void DInputState::SimulateNormalEvent(const int32_t sessionId, const std::string EV_SYN, SYN_REPORT, 0x0); } -void DInputState::RefreshABSPosition(const std::string &dhId, int32_t absX, int32_t absY) +void DInputSinkState::SimulateTouchPadEvents(const int32_t sessionId, const std::string &dhId) { - std::lock_guard lock(absPosMtx_); - if (absX != -1) { - absPositionsMap_[dhId].first = absX; - } - - if (absY != -1) { - absPositionsMap_[dhId].second = absY; - } -} - -std::pair DInputState::GetAndClearABSPosition(const std::string &dhId) -{ - std::lock_guard lock(absPosMtx_); - std::pair absPos = { -1, -1 }; - if (absPositionsMap_.find(dhId) == absPositionsMap_.end()) { - return absPos; + std::vector events = this->touchPadEventFragMgr_->GetAndClearEvents(dhId); + if (events.empty()) { + return; } - absPos = absPositionsMap_[dhId]; - absPositionsMap_.erase(dhId); - return absPos; + DHLOGI("SimulateTouchPadEvents dhId: %s, event size: %d", dhId.c_str(), events.size()); + DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(sessionId, events); } -bool DInputState::IsDhIdDown(const std::string &dhId) +bool DInputSinkState::IsDhIdDown(const std::string &dhId) { std::lock_guard mapLock(keyDownStateMapMtx_); auto iter = keyDownStateMap_.find(dhId); return iter != keyDownStateMap_.end(); } -void DInputState::AddKeyDownState(struct RawEvent event) +void DInputSinkState::AddKeyDownState(struct RawEvent event) { std::lock_guard mapLock(keyDownStateMapMtx_); keyDownStateMap_[event.descriptor].push_back(event); } -void DInputState::RemoveKeyDownState(struct RawEvent event) +void DInputSinkState::RemoveKeyDownState(struct RawEvent event) { std::lock_guard mapLock(keyDownStateMapMtx_); auto iter = keyDownStateMap_.find(event.descriptor); @@ -303,7 +201,7 @@ void DInputState::RemoveKeyDownState(struct RawEvent event) } } -void DInputState::CheckAndSetLongPressedKeyOrder(struct RawEvent event) +void DInputSinkState::CheckAndSetLongPressedKeyOrder(struct RawEvent event) { std::lock_guard mapLock(keyDownStateMapMtx_); auto iter = keyDownStateMap_.find(event.descriptor); @@ -338,7 +236,7 @@ void DInputState::CheckAndSetLongPressedKeyOrder(struct RawEvent event) DHLOGI("Find long pressed key: %d, move the cached pressed key: %d to the last position", event.code, backEv.code); } -void DInputState::ClearDeviceStates() +void DInputSinkState::ClearDeviceStates() { std::lock_guard mapLock(keyDownStateMapMtx_); keyDownStateMap_.clear(); diff --git a/services/state/src/touchpad_event_fragment.cpp b/services/state/src/touchpad_event_fragment.cpp new file mode 100644 index 0000000..91f7de5 --- /dev/null +++ b/services/state/src/touchpad_event_fragment.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "touchpad_event_fragment.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +namespace { + constexpr size_t MIN_EVENT_FRAG_SIZE = 2; +} +bool TouchPadEventFragment::IsShouldDrop() +{ + return events.size() == MIN_EVENT_FRAG_SIZE; +} + +bool TouchPadEventFragment::IsTouchPadOptFinish() const +{ + bool isFinish = false; + for (const auto &ev : events) { + if (ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == KEY_UP_STATE) { + isFinish = true; + break; + } + } + + return isFinish; +} + +bool TouchPadEventFragment::IsTouchPadOptStart() const +{ + bool isStart = false; + for (const auto &ev : events) { + if (ev.type == EV_KEY && ev.code == BTN_TOUCH && ev.value == KEY_DOWN_STATE) { + isStart = true; + break; + } + } + + return isStart; +} + +void TouchPadEventFragment::PushEvent(const RawEvent &event) +{ + this->events.push_back(event); +} + +std::vector TouchPadEventFragment::GetEvents() +{ + return this->events; +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/state/src/touchpad_event_fragment_mgr.cpp b/services/state/src/touchpad_event_fragment_mgr.cpp new file mode 100644 index 0000000..b1726fc --- /dev/null +++ b/services/state/src/touchpad_event_fragment_mgr.cpp @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "touchpad_event_fragment_mgr.h" + +#include + +#include "dinput_log.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +bool TouchPadEventFragmentMgr::IsPositionEvent(const RawEvent &event) +{ + if (event.type == EV_ABS && (event.code == ABS_MT_POSITION_X || event.code == ABS_MT_POSITION_Y || + event.code == ABS_X || event.code == ABS_Y)) { + return true; + } + + return false; +} + +bool TouchPadEventFragmentMgr::IsSynEvent(const RawEvent &event) +{ + return event.type == EV_SYN && event.code == SYN_REPORT; +} + +bool TouchPadEventFragmentMgr::IsWholeTouchFragments(const std::vector &events) +{ + return events.front().IsTouchPadOptStart() && events.back().IsTouchPadOptFinish(); +} + +std::pair> TouchPadEventFragmentMgr::PushEvent(const std::string &dhId, const RawEvent &event) +{ + if (IsPositionEvent(event)) { + return {false, {}}; + } + std::lock_guard lock(fragmentsMtx_); + if (fragments_.find(dhId) == fragments_.end()) { + fragments_[dhId] = {{}}; + } + + fragments_[dhId].back().PushEvent(event); + if (IsSynEvent(event)) { + return DealSynEvent(dhId); + } + return {false, {}}; +} + +std::pair> TouchPadEventFragmentMgr::DealSynEvent(const std::string &dhId) +{ + if (fragments_[dhId].back().IsTouchPadOptFinish()) { + bool needSim = false; + std::vector allEvents = {}; + if (!IsWholeTouchFragments(fragments_[dhId])) { + // If not whole touch events, this means the down event occurs on the other device, + // so we need simulate the up actions to the other side to reset the touchpad states. + for (auto &frag : fragments_[dhId]) { + std::vector fragEvents = frag.GetEvents(); + allEvents.insert(allEvents.end(), fragEvents.begin(), fragEvents.end()); + } + needSim = true; + DHLOGI("Find NOT Whole touchpad events need send back, dhId: %s", dhId.c_str()); + } + fragments_[dhId].clear(); + fragments_[dhId].push_back({}); + return {needSim, allEvents}; + } + + if (fragments_[dhId].back().IsShouldDrop()) { + fragments_[dhId].pop_back(); + } + fragments_[dhId].push_back({}); + return {false, {}}; +} + +void TouchPadEventFragmentMgr::Clear(const std::string &dhId) +{ + std::lock_guard lock(fragmentsMtx_); + fragments_.erase(dhId); +} + +std::vector TouchPadEventFragmentMgr::GetAndClearEvents(const std::string &dhId) +{ + std::lock_guard lock(fragmentsMtx_); + std::vector allEvents; + if (fragments_.find(dhId) == fragments_.end()) { + return {}; + } + + for (auto &frag : fragments_[dhId]) { + std::vector fragEvents = frag.GetEvents(); + allEvents.insert(allEvents.end(), fragEvents.begin(), fragEvents.end()); + } + + fragments_.erase(dhId); + return allEvents; +} +} // namespace DistributedInput +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file -- Gitee From 67fa2680fcb41f63e5dd0f3ed4344a766fc11507 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Wed, 27 Dec 2023 22:16:34 +0800 Subject: [PATCH 2/3] add Signed-off-by: hwzhangchuang --- services/state/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/state/BUILD.gn b/services/state/BUILD.gn index 085ea08..2b70e8e 100644 --- a/services/state/BUILD.gn +++ b/services/state/BUILD.gn @@ -41,7 +41,7 @@ ohos_shared_library("libdinput_sink_state") { sources = [ "src/dinput_sink_state.cpp", "src/touchpad_event_fragment.cpp", - "src/touchpad_event_fragment_mgr.cpp" + "src/touchpad_event_fragment_mgr.cpp", ] defines = [ -- Gitee From cae0bf5b270b9cacceb55fe78010ae9804e335f0 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Wed, 27 Dec 2023 22:25:04 +0800 Subject: [PATCH 3/3] add Signed-off-by: hwzhangchuang --- services/state/include/touchpad_event_fragment.h | 6 +++++- services/state/include/touchpad_event_fragment_mgr.h | 6 +++++- services/state/src/dinput_sink_state.cpp | 3 ++- services/state/src/touchpad_event_fragment_mgr.cpp | 3 ++- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/services/state/include/touchpad_event_fragment.h b/services/state/include/touchpad_event_fragment.h index fdae11c..69d6e25 100644 --- a/services/state/include/touchpad_event_fragment.h +++ b/services/state/include/touchpad_event_fragment.h @@ -13,6 +13,9 @@ * limitations under the License. */ +#ifndef DISTRIBUTED_INPUT_TOUCHPAD_EVENT_FRAGMENT_H +#define DISTRIBUTED_INPUT_TOUCHPAD_EVENT_FRAGMENT_H + #include #include @@ -35,4 +38,5 @@ private: }; } // namespace DistributedInput } // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file +} // namespace OHOS +#endif // DISTRIBUTED_INPUT_TOUCHPAD_EVENT_FRAGMENT_H \ No newline at end of file diff --git a/services/state/include/touchpad_event_fragment_mgr.h b/services/state/include/touchpad_event_fragment_mgr.h index 3bce00e..a406421 100644 --- a/services/state/include/touchpad_event_fragment_mgr.h +++ b/services/state/include/touchpad_event_fragment_mgr.h @@ -13,6 +13,9 @@ * limitations under the License. */ +#ifndef DISTRIBUTED_INPUT_TOUCHPAD_EVENT_FRAGMENT_MGR_H +#define DISTRIBUTED_INPUT_TOUCHPAD_EVENT_FRAGMENT_MGR_H + #include "touchpad_event_fragment.h" #include @@ -54,4 +57,5 @@ private: }; } // namespace DistributedInput } // namespace DistributedHardware -} // namespace OHOS \ No newline at end of file +} // namespace OHOS +#endif // DISTRIBUTED_INPUT_TOUCHPAD_EVENT_FRAGMENT_MGR_H \ No newline at end of file diff --git a/services/state/src/dinput_sink_state.cpp b/services/state/src/dinput_sink_state.cpp index 345a681..5311a2b 100644 --- a/services/state/src/dinput_sink_state.cpp +++ b/services/state/src/dinput_sink_state.cpp @@ -149,7 +149,8 @@ void DInputSinkState::SimulateKeyDownEvents(const int32_t sessionId, const std:: keyDownStateMap_.erase(dhId); } -void DInputSinkState::SimulateKeyDownEvent(const int32_t sessionId, const std::string &dhId, const struct RawEvent &event) +void DInputSinkState::SimulateKeyDownEvent(const int32_t sessionId, const std::string &dhId, + const struct RawEvent &event) { DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, EV_KEY, event.code, KEY_DOWN_STATE); diff --git a/services/state/src/touchpad_event_fragment_mgr.cpp b/services/state/src/touchpad_event_fragment_mgr.cpp index b1726fc..64a4e09 100644 --- a/services/state/src/touchpad_event_fragment_mgr.cpp +++ b/services/state/src/touchpad_event_fragment_mgr.cpp @@ -42,7 +42,8 @@ bool TouchPadEventFragmentMgr::IsWholeTouchFragments(const std::vector> TouchPadEventFragmentMgr::PushEvent(const std::string &dhId, const RawEvent &event) +std::pair> TouchPadEventFragmentMgr::PushEvent(const std::string &dhId, + const RawEvent &event) { if (IsPositionEvent(event)) { return {false, {}}; -- Gitee