From 83cbe9af3f0d4c59b07f0854dc77cb038e50ec2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20Cort=C3=A9s?= Date: Thu, 30 May 2024 14:58:40 -0300 Subject: [PATCH] expo: add option for gesture fingers (#174) --- hyprexpo/README.md | 3 +- hyprexpo/main.cpp | 81 +++++++++++++++++++++++-------------------- hyprexpo/overview.cpp | 9 ++++- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/hyprexpo/README.md b/hyprexpo/README.md index 24175c4..d3c15cb 100644 --- a/hyprexpo/README.md +++ b/hyprexpo/README.md @@ -15,7 +15,8 @@ plugin { bg_col = rgb(111111) workspace_method = center current # [center/first] [workspace] e.g. first 1 or center m+1 - enable_gesture = true # laptop touchpad, 4 fingers + enable_gesture = true # laptop touchpad + gesture_fingers = 3 # 3 or 4 gesture_distance = 300 # how far is the "max" gesture_positive = true # positive = swipe down. Negative = swipe up. } diff --git a/hyprexpo/main.cpp b/hyprexpo/main.cpp index 676084e..08811ce 100644 --- a/hyprexpo/main.cpp +++ b/hyprexpo/main.cpp @@ -14,15 +14,9 @@ inline CFunctionHook* g_pRenderWorkspaceHook = nullptr; inline CFunctionHook* g_pAddDamageHookA = nullptr; inline CFunctionHook* g_pAddDamageHookB = nullptr; -inline CFunctionHook* g_pSwipeBeginHook = nullptr; -inline CFunctionHook* g_pSwipeEndHook = nullptr; -inline CFunctionHook* g_pSwipeUpdateHook = nullptr; -typedef void (*origRenderWorkspace)(void*, CMonitor*, PHLWORKSPACE, timespec*, const CBox&); -typedef void (*origAddDamageA)(void*, const CBox*); -typedef void (*origAddDamageB)(void*, const pixman_region32_t*); -typedef void (*origSwipeBegin)(void*, wlr_pointer_swipe_begin_event*); -typedef void (*origSwipeEnd)(void*, wlr_pointer_swipe_end_event*); -typedef void (*origSwipeUpdate)(void*, wlr_pointer_swipe_update_event*); +typedef void (*origRenderWorkspace)(void*, CMonitor*, PHLWORKSPACE, timespec*, const CBox&); +typedef void (*origAddDamageA)(void*, const CBox*); +typedef void (*origAddDamageB)(void*, const pixman_region32_t*); // Do NOT change this function. APICALL EXPORT std::string PLUGIN_API_VERSION() { @@ -61,43 +55,52 @@ static void hkAddDamageB(void* thisptr, const pixman_region32_t* rg) { g_pOverview->onDamageReported(); } -static float gestured = 0; +static float gestured = 0; +bool swipeActive = false; +char swipeDirection = 0; // 0 = no direction, 'v' = vertical, 'h' = horizontal -// -static void swipeBegin(void* self, SCallbackInfo& info, std::any param) { - static auto* const* PENABLE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:enable_gesture")->getDataStaticPtr(); - - if (g_pOverview) { - info.cancelled = true; - return; - } - - auto e = std::any_cast(param); - - if (!**PENABLE || e.fingers != 4) - return; - - info.cancelled = true; - - renderingOverview = true; - g_pOverview = std::make_unique(g_pCompositor->m_pLastMonitor->activeWorkspace, true); - renderingOverview = false; - - gestured = 0; +static void swipeBegin(void* self, SCallbackInfo& info, std::any param) { + swipeActive = false; + swipeDirection = 0; } static void swipeUpdate(void* self, SCallbackInfo& info, std::any param) { - if (!g_pOverview) + static auto* const* PENABLE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:enable_gesture")->getDataStaticPtr(); + static auto* const* FINGERS = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:gesture_fingers")->getDataStaticPtr(); + static auto* const* PPOSITIVE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:gesture_positive")->getDataStaticPtr(); + auto e = std::any_cast(param); + + if (!swipeDirection) { + if (std::abs(e.delta.x) > std::abs(e.delta.y)) + swipeDirection = 'h'; + else if (std::abs(e.delta.y) > std::abs(e.delta.x)) + swipeDirection = 'v'; + else + swipeDirection = 0; + } + + if (swipeActive || g_pOverview) + info.cancelled = true; + + if (!**PENABLE || e.fingers != **FINGERS || swipeDirection != 'v') return; - static auto* const* PPOSITIVE = (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:hyprexpo:gesture_positive")->getDataStaticPtr(); - info.cancelled = true; - - auto e = std::any_cast(param); + if (!swipeActive) { + if (g_pOverview) + return; + if ((**PPOSITIVE ? 1.0 : -1.0) * e.delta.y <= 0) + return; + renderingOverview = true; + g_pOverview = std::make_unique(g_pCompositor->m_pLastMonitor->activeWorkspace, true); + renderingOverview = false; + gestured = 0; + swipeActive = true; + } gestured += (**PPOSITIVE ? 1.0 : -1.0) * e.delta.y; - + if (gestured <= 0.01) // plugin will crash if swipe ends at <= 0 + gestured = 0.01; g_pOverview->onSwipeUpdate(gestured); } @@ -105,6 +108,7 @@ static void swipeEnd(void* self, SCallbackInfo& info, std::any param) { if (!g_pOverview) return; + swipeActive = false; info.cancelled = true; g_pOverview->onSwipeEnd(); @@ -112,6 +116,8 @@ static void swipeEnd(void* self, SCallbackInfo& info, std::any param) { static void onExpoDispatcher(std::string arg) { + if (swipeActive) + return; if (arg == "toggle") { if (g_pOverview) g_pOverview->close(); @@ -204,6 +210,7 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprexpo:enable_gesture", Hyprlang::INT{1}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprexpo:gesture_distance", Hyprlang::INT{200}); HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprexpo:gesture_positive", Hyprlang::INT{1}); + HyprlandAPI::addConfigValue(PHANDLE, "plugin:hyprexpo:gesture_fingers", Hyprlang::INT{4}); HyprlandAPI::reloadConfig(); diff --git a/hyprexpo/overview.cpp b/hyprexpo/overview.cpp index 4945d90..30cd4ee 100644 --- a/hyprexpo/overview.cpp +++ b/hyprexpo/overview.cpp @@ -439,10 +439,17 @@ void COverview::onSwipeUpdate(double delta) { } void COverview::onSwipeEnd() { + const auto SIZEMIN = pMonitor->vecSize; + const auto SIZEMAX = pMonitor->vecSize * pMonitor->vecSize / (pMonitor->vecSize / SIDE_LENGTH); + const auto PERC = (size.value() - SIZEMIN).x / (SIZEMAX - SIZEMIN).x; + if (PERC > 0.5) { + close(); + return; + } size = pMonitor->vecSize; pos = {0, 0}; size.setCallbackOnEnd([this](void*) { redrawAll(true); }); swipeWasCommenced = true; -} \ No newline at end of file +}