From 55b9282852cd52932feac7a7e9388d309f3f55dc Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 9 Dec 2023 20:38:55 +0800 Subject: [PATCH 1/2] sim btn_touch Signed-off-by: hwzhangchuang --- common/include/input_hub.cpp | 87 +++++++++++++++++---------- common/include/input_hub.h | 3 +- services/state/include/dinput_state.h | 4 +- services/state/src/dinput_state.cpp | 30 ++++++++- 4 files changed, 88 insertions(+), 36 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index aa8dc8a..70aaccd 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -201,6 +201,11 @@ size_t InputHub::GetEvents(RawEvent *buffer, size_t bufferSize) return event - buffer; } +bool InputHub::IsCuror(Device *device) +{ + return device->classes & INPUT_DEVICE_CLASS_CURSOR; +} + bool InputHub::IsTouchPad(Device *device) { return ((device->classes & INPUT_DEVICE_CLASS_TOUCH_MT) || (device->classes & INPUT_DEVICE_CLASS_TOUCH)) && @@ -218,6 +223,53 @@ bool InputHub::IsTouchPad(const InputDevice &inputDevice) return true; } +void InputHub::MatchAndDealEvent(const RawEvent &event) +{ + // Deal key state + if (event.type == EV_KEY && event.code != BTN_TOOL_FINGER && event.value == KEY_DOWN_STATE) { + DInputState::GetInstance().AddKeyDownState(event); + RecordChangeEventLog(event); + } + + if (event.type == EV_KEY && event.value == KEY_UP_STATE) { + DInputState::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); + } + + 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); + } + + 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::RecordDeviceChangeStates(Device *device, struct input_event readBuffer[], const size_t count) { DHLOGD("RecordDeviceChangeStates enter."); @@ -229,46 +281,17 @@ void InputHub::RecordDeviceChangeStates(Device *device, struct input_event readB } for (size_t i = 0; i < count; i++) { - RawEvent event; const struct input_event& iev = readBuffer[i]; + RawEvent event; event.when = ProcessEventTimestamp(iev); event.type = iev.type; event.code = iev.code; event.value = iev.value; event.path = device->path; event.descriptor = isTouchEvent ? touchDescriptor : device->identifier.descriptor; - - // Deal key state - if (event.type == EV_KEY && event.code != BTN_TOOL_FINGER && event.value == KEY_DOWN_STATE) { - DInputState::GetInstance().AddKeyDownState(event); - RecordChangeEventLog(event); - } - - if (event.type == EV_KEY && event.value == KEY_UP_STATE) { - DInputState::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); - } - - if (IsTouchPad(device) && event.type == EV_KEY && event.code == BTN_MOUSE && event.value == KEY_UP_STATE && - !DInputState::GetInstance().IsDhIdDown(event.descriptor)) { - DHLOGI("Find touchpad UP state that not down effective at sink side, dhId: %s", - event.descriptor.c_str()); - DInputState::GetInstance().SimulateTouchPadUpState(event.descriptor, event); - } - DHLOGD("RecordDeviceChangeStates end."); + MatchAndDealEvent(event); } + DHLOGD("RecordDeviceChangeStates end."); } size_t InputHub::CollectEvent(RawEvent *buffer, size_t &capacity, Device *device, struct input_event readBuffer[], diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 9e6e64c..853b263 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -150,6 +150,7 @@ private: bool ContainsNonZeroByte(const uint8_t *array, uint32_t startIndex, uint32_t endIndex); int64_t ProcessEventTimestamp(const input_event &event); + bool IsCuror(Device *device); bool IsTouchPad(const InputDevice &inputDevice); bool IsTouchPad(Device *device); @@ -179,7 +180,7 @@ private: * Record Mouse/KeyBoard/TouchPad state such as key down. */ void RecordDeviceChangeStates(Device *device, struct input_event readBuffer[], const size_t count); - + void MatchAndDealEvent(const RawEvent &event); /* * Scan the input device node and save info. */ diff --git a/services/state/include/dinput_state.h b/services/state/include/dinput_state.h index d16a9f9..c075d33 100644 --- a/services/state/include/dinput_state.h +++ b/services/state/include/dinput_state.h @@ -65,7 +65,9 @@ public: void RefreshABSPosition(const std::string &dhId, int32_t absX, int32_t absY); std::pair GetAndClearABSPosition(const std::string &dhId); - void SimulateTouchPadUpState(const std::string &dhId, const struct RawEvent &event); + 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); /** * @brief check is one device in down state * diff --git a/services/state/src/dinput_state.cpp b/services/state/src/dinput_state.cpp index 470c4f7..66cd0d2 100644 --- a/services/state/src/dinput_state.cpp +++ b/services/state/src/dinput_state.cpp @@ -168,12 +168,12 @@ void DInputState::SimulateBtnTouchEvent(const int32_t sessionId, const std::stri DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsgBatch(sessionId, simEvents); } -void DInputState::SimulateTouchPadUpState(const std::string &dhId, const struct RawEvent &event) +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 UP state to source, dhId: %s, dx: %d, dy: %d", dhId.c_str(), dx, dy); + 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 }; @@ -211,6 +211,32 @@ void DInputState::SimulateTouchPadUpState(const std::string &dhId, const struct 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) { DistributedInputSinkTransport::GetInstance().SendKeyStateNodeMsg(sessionId, dhId, -- Gitee From b98701372d40414f0b2ca925a8119d151ab3ff87 Mon Sep 17 00:00:00 2001 From: hwzhangchuang Date: Sat, 9 Dec 2023 20:47:16 +0800 Subject: [PATCH 2/2] add Signed-off-by: hwzhangchuang --- common/include/input_hub.cpp | 4 ++-- common/include/input_hub.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/common/include/input_hub.cpp b/common/include/input_hub.cpp index 70aaccd..141c761 100644 --- a/common/include/input_hub.cpp +++ b/common/include/input_hub.cpp @@ -223,7 +223,7 @@ bool InputHub::IsTouchPad(const InputDevice &inputDevice) return true; } -void InputHub::MatchAndDealEvent(const RawEvent &event) +void InputHub::MatchAndDealEvent(Device *device, const RawEvent &event) { // Deal key state if (event.type == EV_KEY && event.code != BTN_TOOL_FINGER && event.value == KEY_DOWN_STATE) { @@ -289,7 +289,7 @@ void InputHub::RecordDeviceChangeStates(Device *device, struct input_event readB event.value = iev.value; event.path = device->path; event.descriptor = isTouchEvent ? touchDescriptor : device->identifier.descriptor; - MatchAndDealEvent(event); + MatchAndDealEvent(device, event); } DHLOGD("RecordDeviceChangeStates end."); } diff --git a/common/include/input_hub.h b/common/include/input_hub.h index 853b263..a2be41f 100644 --- a/common/include/input_hub.h +++ b/common/include/input_hub.h @@ -180,7 +180,7 @@ private: * Record Mouse/KeyBoard/TouchPad state such as key down. */ void RecordDeviceChangeStates(Device *device, struct input_event readBuffer[], const size_t count); - void MatchAndDealEvent(const RawEvent &event); + void MatchAndDealEvent(Device *device, const RawEvent &event); /* * Scan the input device node and save info. */ -- Gitee