Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/core/platforms/WaylandPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CCWlKeyboard>(m_waylandState.seat->sendGetKeyboard());
Expand Down Expand Up @@ -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<CCWlTouch>(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();
}
});
}

Expand Down Expand Up @@ -732,6 +748,44 @@ void CWaylandPlatform::onKeyboardLeave() {
resetKeyboardState();
}

void CWaylandPlatform::onTouchDown(SP<IWaylandWindow> 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();

Expand Down
8 changes: 8 additions & 0 deletions src/core/platforms/WaylandPlatform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ namespace Hyprtoolkit {

void onRepeatTimerFire();

void onTouchDown(SP<IWaylandWindow> 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<CWaylandSessionLockState> aquireSessionLock();

// dmabuf formats
Expand All @@ -92,6 +97,7 @@ namespace Hyprtoolkit {
Hyprutils::Memory::CSharedPointer<CCWpViewporter> viewporter;
Hyprutils::Memory::CSharedPointer<CCWlKeyboard> keyboard;
Hyprutils::Memory::CSharedPointer<CCWlPointer> pointer;
Hyprutils::Memory::CSharedPointer<CCWlTouch> touch;
Hyprutils::Memory::CSharedPointer<CCWpCursorShapeManagerV1> cursorShapeMgr;
Hyprutils::Memory::CSharedPointer<CCWpCursorShapeDeviceV1> cursorShapeDev;
Hyprutils::Memory::CSharedPointer<CCZwpTextInputManagerV3> textInputManager;
Expand Down Expand Up @@ -145,6 +151,8 @@ namespace Hyprtoolkit {
std::vector<WP<CWaylandLayer>> m_layers;
WP<IWaylandWindow> m_currentWindow; // window the pointer is over
WP<IWaylandWindow> m_keyboardWindow; // window the compositor gave keyboard focus
WP<IWaylandWindow> m_touchWindow; // window the tracked touch point went down on
std::optional<int32_t> 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.
Expand Down
85 changes: 85 additions & 0 deletions tests/unit/core/Touch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <gtest/gtest.h>

#include <format>

#include <core/platforms/WaylandPlatform.hpp>
#include <window/WaylandWindow.hpp>

#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<std::string> m_log;
};

TEST(WaylandPlatform, touchDrivesLeftButton) {
Tests::Tricks::createBackendSupport();

CWaylandPlatform platform;
auto window = makeShared<CMouseRecorder>();

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<std::string> 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<CMouseRecorder>();

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<std::string> 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<CMouseRecorder>();

platform.onTouchDown(nullptr, 1, {5, 5});
platform.onTouchDown(window, 2, {7, 7});
platform.onTouchUp(2);

const std::vector<std::string> EXPECTED = {"enter 7 7", "left down", "left up", "leave"};
EXPECT_EQ(window->m_log, EXPECTED);
}
Loading