Skip to content

Commit

Permalink
Merge pull request #144 from horriblename/feature/touch-margins
Browse files Browse the repository at this point in the history
Add option edge_margin
  • Loading branch information
horriblename authored Aug 11, 2024
2 parents 5c93341 + 80a8b29 commit 0bb3b82
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 42 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ plugin:touch_gestures {
# in milliseconds
long_press_delay = 400
# in pixels, the distance from the edge that is considered an edge
edge_margin = 10
experimental {
# send proper cancel events to windows instead of hacky touch_up events,
# NOT recommended as it crashed a few times, once it's stabilized I'll make it the default
Expand Down
26 changes: 13 additions & 13 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/GestureManager.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "GestureManager.hpp"
#include "globals.hpp"
#include "wayfire/touch/touch.hpp"
#include <algorithm>
#include <cstdint>
Expand Down Expand Up @@ -61,18 +62,21 @@ int handleLongPressTimer(void* data) {
return 0;
}

GestureManager::GestureManager() {
GestureManager::GestureManager() : IGestureManager(std::make_unique<HyprLogger>()) {
static auto const PSENSITIVITY =
(Hyprlang::FLOAT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:sensitivity")
->getDataStaticPtr();
static auto const LONG_PRESS_DELAY =
(Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:long_press_delay")
->getDataStaticPtr();
static auto const EDGE_MARGIN =
(Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:edge_margin")
->getDataStaticPtr();

this->addMultiFingerGesture(*PSENSITIVITY, *LONG_PRESS_DELAY);
this->addMultiFingerTap(*PSENSITIVITY, *LONG_PRESS_DELAY);
this->addLongPress(*PSENSITIVITY, *LONG_PRESS_DELAY);
this->addEdgeSwipeGesture(*PSENSITIVITY, *LONG_PRESS_DELAY);
this->addEdgeSwipeGesture(*PSENSITIVITY, *LONG_PRESS_DELAY, *EDGE_MARGIN);

this->long_press_timer = wl_event_loop_add_timer(g_pCompositor->m_sWLEventLoop, handleLongPressTimer, this);
}
Expand Down
3 changes: 2 additions & 1 deletion src/GestureManager.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once
#include "./gestures/Gestures.hpp"
#include "HyprLogger.hpp"
#include "gestures/Shared.hpp"
#include "globals.hpp"
#include <memory>

#define private public
#include <hyprland/src/debug/Log.hpp>
Expand Down
9 changes: 9 additions & 0 deletions src/HyprLogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "gestures/Logger.hpp"
#include "src/debug/Log.hpp"

class HyprLogger : public Logger {
public:
void debug(std::string s) {
Debug::log(INFO, s);
}
};
21 changes: 10 additions & 11 deletions src/gestures/Gestures.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "Gestures.hpp"
#include "Actions.hpp"
#include <functional>
#include <glm/glm.hpp>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <wayfire/touch/touch.hpp>

Expand Down Expand Up @@ -94,24 +92,24 @@ bool IGestureManager::onTouchMove(const wf::touch::gesture_event_t& ev) {
return this->eventForwardingInhibited();
}

GestureDirection IGestureManager::find_swipe_edges(wf::touch::point_t point) {
GestureDirection IGestureManager::find_swipe_edges(wf::touch::point_t point, int edge_margin) {
auto mon = this->getMonitorArea();

GestureDirection edge_directions = 0;

if (point.x <= mon.x + EDGE_SWIPE_THRESHOLD) {
if (point.x <= mon.x + edge_margin) {
edge_directions |= GESTURE_DIRECTION_LEFT;
}

if (point.x >= mon.x + mon.w - EDGE_SWIPE_THRESHOLD) {
if (point.x >= mon.x + mon.w - edge_margin) {
edge_directions |= GESTURE_DIRECTION_RIGHT;
}

if (point.y <= mon.y + EDGE_SWIPE_THRESHOLD) {
if (point.y <= mon.y + edge_margin) {
edge_directions |= GESTURE_DIRECTION_UP;
}

if (point.y >= mon.y + mon.h - EDGE_SWIPE_THRESHOLD) {
if (point.y >= mon.y + mon.h - edge_margin) {
edge_directions |= GESTURE_DIRECTION_DOWN;
}

Expand Down Expand Up @@ -228,11 +226,12 @@ void IGestureManager::addLongPress(const float* sensitivity, const int64_t* dela
this->addTouchGesture(std::make_unique<wf::touch::gesture_t>(std::move(long_press_actions), ack, cancel));
}

void IGestureManager::addEdgeSwipeGesture(const float* sensitivity, const int64_t* timeout) {
void IGestureManager::addEdgeSwipeGesture(const float* sensitivity, const int64_t* timeout,
const long int* edge_margin) {
auto edge = std::make_unique<CMultiAction>(SWIPE_INCORRECT_DRAG_TOLERANCE, sensitivity, timeout);
auto edge_ptr = edge.get();
auto edge_drag_begin = std::make_unique<OnCompleteAction>(std::move(edge), [=, this]() {
auto origin_edges = this->find_swipe_edges(m_sGestureState.get_center().origin);
auto origin_edges = this->find_swipe_edges(m_sGestureState.get_center().origin, *edge_margin);

if (origin_edges == 0) {
return;
Expand All @@ -258,8 +257,8 @@ void IGestureManager::addEdgeSwipeGesture(const float* sensitivity, const int64_
edge_swipe_actions.emplace_back(std::move(edge_drag_begin));
edge_swipe_actions.emplace_back(std::move(edge_release));

auto ack = [edge_ptr, this]() {
auto origin_edges = find_swipe_edges(m_sGestureState.get_center().origin);
auto ack = [edge_ptr, edge_margin, this]() {
auto origin_edges = find_swipe_edges(m_sGestureState.get_center().origin, *edge_margin);
auto direction = edge_ptr->target_direction;
auto dragEvent = DragGestureEvent{
.type = DragGestureType::EDGE_SWIPE,
Expand Down
8 changes: 5 additions & 3 deletions src/gestures/Gestures.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "CompletedGesture.hpp"
#include "DragGesture.hpp"
#include "Logger.hpp"
#include "Shared.hpp"
#include <functional>
#include <memory>
#include <optional>
#include <wayfire/touch/touch.hpp>
Expand All @@ -23,6 +23,7 @@ struct SMonitorArea {
*/
class IGestureManager {
public:
IGestureManager(std::unique_ptr<Logger> logger) : logger(std::move(logger)) {}
virtual ~IGestureManager() {}
// @return whether this touch event should be blocked from forwarding to the
// client window/surface
Expand All @@ -40,7 +41,7 @@ class IGestureManager {
void addMultiFingerGesture(const float* sensitivity, const int64_t* timeout);
void addMultiFingerTap(const float* sensitivity, const int64_t* timeout);
void addLongPress(const float* sensitivity, const int64_t* delay);
void addEdgeSwipeGesture(const float* sensitivity, const int64_t* timeout);
void addEdgeSwipeGesture(const float* sensitivity, const int64_t* timeout, const long* edge_margin);

std::optional<DragGestureEvent> getActiveDragGesture() const {
return activeDragGesture;
Expand All @@ -56,7 +57,7 @@ class IGestureManager {
std::vector<std::unique_ptr<wf::touch::gesture_t>> m_vGestures;
wf::touch::gesture_state_t m_sGestureState;

GestureDirection find_swipe_edges(wf::touch::point_t point);
GestureDirection find_swipe_edges(wf::touch::point_t point, int edge_margin);
virtual SMonitorArea getMonitorArea() const = 0;

// handles gesture events and returns whether or not the event is used.
Expand All @@ -78,6 +79,7 @@ class IGestureManager {
virtual void stopLongPressTimer() = 0;

private:
std::unique_ptr<Logger> logger;
bool inhibitTouchEvents;
std::optional<DragGestureEvent> activeDragGesture;

Expand Down
8 changes: 8 additions & 0 deletions src/gestures/Logger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once
#include <string>

class Logger {
public:
virtual ~Logger() {}
virtual void debug(std::string s) = 0;
};
1 change: 0 additions & 1 deletion src/gestures/Shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <string>

// Swipe params
constexpr static int EDGE_SWIPE_THRESHOLD = 10;
constexpr static double MIN_SWIPE_DISTANCE = 30;
constexpr static double MAX_SWIPE_DISTANCE = 450;
constexpr static double SWIPE_INCORRECT_DRAG_TOLERANCE = 150;
Expand Down
9 changes: 9 additions & 0 deletions src/gestures/test/CoutLogger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once
#include "../Logger.hpp"
#include <iostream>

class CoutLogger : public Logger {
void debug(std::string s) override {
std::cout << s << std::endl;
}
};
2 changes: 0 additions & 2 deletions src/gestures/test/MockGestureManager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "MockGestureManager.hpp"
#include "../Gestures.hpp"
#include <cassert>
#include <iostream>

#define CONFIG_SENSITIVITY 1.0
Expand Down
15 changes: 13 additions & 2 deletions src/gestures/test/MockGestureManager.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include "../Gestures.hpp"
#include "CoutLogger.hpp"
#include "wayfire/touch/touch.hpp"
#include <memory>
#include <vector>

constexpr double MONITOR_X = 0;
Expand All @@ -15,7 +17,8 @@ class Tester {

class CMockGestureManager final : public IGestureManager {
public:
CMockGestureManager(bool handlesDragEvents) : handlesDragEvents(handlesDragEvents) {}
CMockGestureManager(bool handlesDragEvents)
: IGestureManager(std::make_unique<CoutLogger>()), handlesDragEvents(handlesDragEvents) {}
~CMockGestureManager() {}

// if set to true, handleDragGesture() will return true
Expand All @@ -25,6 +28,14 @@ class CMockGestureManager final : public IGestureManager {
bool cancelled = false;
bool dragEnded = false;

struct {
double x, y;
} mon_offset = {MONITOR_X, MONITOR_Y};

struct {
double w, h;
} mon_size = {MONITOR_WIDTH, MONITOR_HEIGHT};

// creates a gesture manager that handles all drag gestures
static CMockGestureManager newDragHandler() {
return CMockGestureManager(true);
Expand Down Expand Up @@ -61,7 +72,7 @@ class CMockGestureManager final : public IGestureManager {

protected:
SMonitorArea getMonitorArea() const override {
return SMonitorArea{MONITOR_X, MONITOR_Y, MONITOR_WIDTH, MONITOR_HEIGHT};
return SMonitorArea{this->mon_offset.x, this->mon_offset.y, this->mon_size.w, this->mon_size.h};
}

private:
Expand Down
Loading

0 comments on commit 0bb3b82

Please sign in to comment.