From febb9da310a87af45cd6c83e7e0a67af3075ff3d Mon Sep 17 00:00:00 2001 From: j4kuuu Date: Wed, 11 Nov 2026 20:41:22 +0100 Subject: [PATCH] core/wayland: emulate the left mouse button from wl_touch bind wl_touch and feed the first touch point through the existing mouse path (enter, press, move, release, leave), so buttons, sliders and the rest work on a touchscreen without per-element changes. other fingers are ignored while one is held. --- src/core/platforms/WaylandPlatform.cpp | 54 ++++++++++++++++ src/core/platforms/WaylandPlatform.hpp | 8 +++ tests/unit/core/Touch.cpp | 85 ++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 tests/unit/core/Touch.cpp diff --git a/src/core/platforms/WaylandPlatform.cpp b/src/core/platforms/WaylandPlatform.cpp index e93ec47..25f5f06 100644 --- a/src/core/platforms/WaylandPlatform.cpp +++ b/src/core/platforms/WaylandPlatform.cpp @@ -271,6 +271,7 @@ void CWaylandPlatform::initSeat() { m_waylandState.seat->setCapabilities([this](CCWlSeat* r, wl_seat_capability cap) { const bool HAS_KEYBOARD = ((uint32_t)cap) & WL_SEAT_CAPABILITY_KEYBOARD; const bool HAS_POINTER = ((uint32_t)cap) & WL_SEAT_CAPABILITY_POINTER; + const bool HAS_TOUCH = ((uint32_t)cap) & WL_SEAT_CAPABILITY_TOUCH; if (HAS_KEYBOARD && !m_waylandState.keyboard) { m_waylandState.keyboard = makeShared(m_waylandState.seat->sendGetKeyboard()); @@ -428,6 +429,21 @@ void CWaylandPlatform::initSeat() { m_waylandState.pointer.reset(); m_waylandState.cursorShapeDev.reset(); } + + if (HAS_TOUCH && !m_waylandState.touch) { + m_waylandState.touch = makeShared(m_waylandState.seat->sendGetTouch()); + + m_waylandState.touch->setDown([this](CCWlTouch* r, uint32_t serial, uint32_t time, wl_proxy* surf, int32_t id, wl_fixed_t x, wl_fixed_t y) { + onTouchDown(windowForSurf(surf), id, {wl_fixed_to_double(x), wl_fixed_to_double(y)}); + }); + m_waylandState.touch->setMotion( + [this](CCWlTouch* r, uint32_t time, int32_t id, wl_fixed_t x, wl_fixed_t y) { onTouchMotion(id, {wl_fixed_to_double(x), wl_fixed_to_double(y)}); }); + m_waylandState.touch->setUp([this](CCWlTouch* r, uint32_t serial, uint32_t time, int32_t id) { onTouchUp(id); }); + m_waylandState.touch->setCancel([this](CCWlTouch* r) { onTouchCancel(); }); + } else if (!HAS_TOUCH && m_waylandState.touch) { + onTouchCancel(); + m_waylandState.touch.reset(); + } }); } @@ -732,6 +748,44 @@ void CWaylandPlatform::onKeyboardLeave() { resetKeyboardState(); } +void CWaylandPlatform::onTouchDown(SP window, int32_t id, const Vector2D& local) { + if (!window || m_touchId) + return; + + m_touchId = id; + m_touchWindow = window; + window->mouseEnter(local); + window->mouseButton(Input::MOUSE_BUTTON_LEFT, true); +} + +void CWaylandPlatform::onTouchMotion(int32_t id, const Vector2D& local) { + if (id != m_touchId) + return; + + if (const auto window = m_touchWindow.lock()) + window->mouseMove(local); +} + +void CWaylandPlatform::onTouchUp(int32_t id) { + if (id != m_touchId) + return; + + onTouchCancel(); +} + +void CWaylandPlatform::onTouchCancel() { + const auto window = m_touchWindow.lock(); + m_touchWindow.reset(); + m_touchId.reset(); + + if (!window) + return; + + // buttons act on press, so this release never clicks on a cancel, it only ends a drag + window->mouseButton(Input::MOUSE_BUTTON_LEFT, false); + window->mouseLeave(); +} + void CWaylandPlatform::resetKeyboardState() { stopRepeatTimer(); diff --git a/src/core/platforms/WaylandPlatform.hpp b/src/core/platforms/WaylandPlatform.hpp index 06fd293..0ed63cf 100644 --- a/src/core/platforms/WaylandPlatform.hpp +++ b/src/core/platforms/WaylandPlatform.hpp @@ -70,6 +70,11 @@ namespace Hyprtoolkit { void onRepeatTimerFire(); + void onTouchDown(SP window, int32_t id, const Hyprutils::Math::Vector2D& local); + void onTouchMotion(int32_t id, const Hyprutils::Math::Vector2D& local); + void onTouchUp(int32_t id); + void onTouchCancel(); + SP aquireSessionLock(); // dmabuf formats @@ -92,6 +97,7 @@ namespace Hyprtoolkit { Hyprutils::Memory::CSharedPointer viewporter; Hyprutils::Memory::CSharedPointer keyboard; Hyprutils::Memory::CSharedPointer pointer; + Hyprutils::Memory::CSharedPointer touch; Hyprutils::Memory::CSharedPointer cursorShapeMgr; Hyprutils::Memory::CSharedPointer cursorShapeDev; Hyprutils::Memory::CSharedPointer textInputManager; @@ -145,6 +151,8 @@ namespace Hyprtoolkit { std::vector> m_layers; WP m_currentWindow; // window the pointer is over WP m_keyboardWindow; // window the compositor gave keyboard focus + WP m_touchWindow; // window the tracked touch point went down on + std::optional m_touchId; uint32_t m_currentMods = 0; // HT modifiers, not xkb uint32_t m_lastEnterSerial = 0; // xdg_toplevel.resize requires the serial of a button PRESS, not release. diff --git a/tests/unit/core/Touch.cpp b/tests/unit/core/Touch.cpp new file mode 100644 index 0000000..f75b540 --- /dev/null +++ b/tests/unit/core/Touch.cpp @@ -0,0 +1,85 @@ +#include + +#include + +#include +#include + +#include "../tricks/Tricks.hpp" + +using namespace Hyprtoolkit; +using namespace Hyprutils::Math; + +class CMouseRecorder : public CWaylandWindow { + public: + CMouseRecorder() : CWaylandWindow(SWindowCreationData{}) { + ; + } + + void mouseEnter(const Vector2D& local) override { + m_log.emplace_back(std::format("enter {} {}", local.x, local.y)); + } + + void mouseMove(const Vector2D& local) override { + m_log.emplace_back(std::format("move {} {}", local.x, local.y)); + } + + void mouseButton(const Input::eMouseButton button, bool state) override { + m_log.emplace_back(std::format("{} {}", button == Input::MOUSE_BUTTON_LEFT ? "left" : "other", state ? "down" : "up")); + } + + void mouseLeave() override { + m_log.emplace_back("leave"); + } + + std::vector m_log; +}; + +TEST(WaylandPlatform, touchDrivesLeftButton) { + Tests::Tricks::createBackendSupport(); + + CWaylandPlatform platform; + auto window = makeShared(); + + platform.onTouchDown(window, 3, {10, 20}); + // a second finger while the first is held is ignored, down, motion and up alike + platform.onTouchDown(window, 4, {50, 50}); + platform.onTouchMotion(4, {60, 60}); + platform.onTouchMotion(3, {15, 25}); + platform.onTouchUp(4); + platform.onTouchUp(3); + platform.onTouchMotion(3, {99, 99}); + + const std::vector EXPECTED = {"enter 10 20", "left down", "move 15 25", "left up", "leave"}; + EXPECT_EQ(window->m_log, EXPECTED); +} + +TEST(WaylandPlatform, touchCancelReleasesAndFreesTheSlot) { + Tests::Tricks::createBackendSupport(); + + CWaylandPlatform platform; + auto window = makeShared(); + + platform.onTouchDown(window, 1, {5, 5}); + platform.onTouchCancel(); + // the compositor sends no up for a cancelled point, a late one must not release twice + platform.onTouchUp(1); + platform.onTouchDown(window, 2, {7, 7}); + + const std::vector EXPECTED = {"enter 5 5", "left down", "left up", "leave", "enter 7 7", "left down"}; + EXPECT_EQ(window->m_log, EXPECTED); +} + +TEST(WaylandPlatform, touchOnUnknownSurfaceIsIgnored) { + Tests::Tricks::createBackendSupport(); + + CWaylandPlatform platform; + auto window = makeShared(); + + platform.onTouchDown(nullptr, 1, {5, 5}); + platform.onTouchDown(window, 2, {7, 7}); + platform.onTouchUp(2); + + const std::vector EXPECTED = {"enter 7 7", "left down", "left up", "leave"}; + EXPECT_EQ(window->m_log, EXPECTED); +}