From 28ed099437d865b8ba6ec1b69d0fec1f63a81afc Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sat, 8 Jun 2024 15:14:59 +0200 Subject: [PATCH 01/27] add visualize_touch config value --- src/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index ebc7464..8ea2dc5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,6 +108,8 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { Hyprlang::CConfigValue((Hyprlang::INT)1)); HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:emulate_touchpad_swipe", Hyprlang::CConfigValue((Hyprlang::INT)0)); + HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch", + Hyprlang::CConfigValue((Hyprlang::INT)0)); #pragma GCC diagnostic pop HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bind", onNewBind, Hyprlang::SHandlerOptions{}); From 6cd8229a68f91e3e399c5fb3e486cd9b871dd25a Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sat, 8 Jun 2024 22:33:10 +0200 Subject: [PATCH 02/27] add basic TouchVisualizer class --- src/TouchVisualizer.cpp | 70 +++++++++++++++++++++++++++++++++++++ src/TouchVisualizer.hpp | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/TouchVisualizer.cpp create mode 100644 src/TouchVisualizer.hpp diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp new file mode 100644 index 0000000..86570f3 --- /dev/null +++ b/src/TouchVisualizer.cpp @@ -0,0 +1,70 @@ +#include "TouchVisualizer.hpp" +#include "src/devices/ITouch.hpp" +#include "src/macros.hpp" +#include "src/render/Renderer.hpp" +#include + +void Visualizer::onRender() { + if (this->finger_positions.size() < 1) { + return; + } + + // this->finger_positions +} + +void Visualizer::onTouchDown(ITouch::SDownEvent ev) { + this->finger_positions.push_back(std::pair(ev.touchID, ev.pos)); + this->prev_finger_positions.push_back(std::pair(ev.touchID, ev.pos)); +} + +void Visualizer::onTouchUp(ITouch::SUpEvent ev) { + for (auto iter = this->prev_finger_positions.begin(); iter != this->prev_finger_positions.end(); iter++) { + this->prev_finger_positions.erase(iter); + } + + for (auto iter = this->finger_positions.begin(); iter != this->finger_positions.end(); iter++) { + this->finger_positions.erase(iter); + } +} + +void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { + for (auto& pair : this->finger_positions) { + if (pair.first != ev.touchID) + continue; + + for (auto& prev : this->prev_finger_positions) { + if (prev.first != ev.touchID) + continue; + + prev.second = pair.second; + pair.second = ev.pos; + + return; + } + break; + } +} + +void Visualizer::damageAll() { + CBox dm; + for (const auto& point : this->prev_finger_positions) { + dm = CBox{point.second.x - TOUCH_POINT_RADIUS, point.second.y - TOUCH_POINT_RADIUS, + static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; + g_pHyprRenderer->damageBox(&dm); + } +} + +void Visualizer::damageFinger(uint64_t id) { + for (const auto& point : this->prev_finger_positions) { + if (point.first != id) { + continue; + } + + CBox dm = {point.second.x - TOUCH_POINT_RADIUS, point.second.y - TOUCH_POINT_RADIUS, + static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; + g_pHyprRenderer->damageBox(&dm); + return; + } + + RASSERT(false, "Visualizer tried to damage non-existent finger id {}", id) +} diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp new file mode 100644 index 0000000..c7d289a --- /dev/null +++ b/src/TouchVisualizer.hpp @@ -0,0 +1,76 @@ +// OpenGL utility functions are copied from https://github.com/hyprwm/hyprland-plugins +// under BSD 3-Clause License. A copy of the license can be found in the LICENSE file + +#include "src/devices/ITouch.hpp" +#include +#include +#include +#include + +GLuint CompileShader(const GLuint& type, std::string src) { + auto shader = glCreateShader(type); + + auto shaderSource = src.c_str(); + + glShaderSource(shader, 1, (const GLchar**)&shaderSource, nullptr); + glCompileShader(shader); + + GLint ok; + glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); + + if (ok == GL_FALSE) + throw std::runtime_error("compileShader() failed!"); + + return shader; +} + +GLuint CreateProgram(const std::string& vert, const std::string& frag) { + auto vertCompiled = CompileShader(GL_VERTEX_SHADER, vert); + + if (!vertCompiled) + throw std::runtime_error("Compiling vshader failed."); + + auto fragCompiled = CompileShader(GL_FRAGMENT_SHADER, frag); + + if (!fragCompiled) + throw std::runtime_error("Compiling fshader failed."); + + auto prog = glCreateProgram(); + glAttachShader(prog, vertCompiled); + glAttachShader(prog, fragCompiled); + glLinkProgram(prog); + + glDetachShader(prog, vertCompiled); + glDetachShader(prog, fragCompiled); + glDeleteShader(vertCompiled); + glDeleteShader(fragCompiled); + + GLint ok; + glGetProgramiv(prog, GL_LINK_STATUS, &ok); + + if (ok == GL_FALSE) + throw std::runtime_error("createProgram() failed! GL_LINK_STATUS not OK!"); + + return prog; +} + +const std::string vertexShaderSource = R"#( + +)#"; + +class Visualizer { + public: + void onRender(); + void damageAll(); + void damageFinger(uint64_t id); + + void onTouchDown(ITouch::SDownEvent); + void onTouchUp(ITouch::SUpEvent); + void onTouchMotion(ITouch::SMotionEvent); + + private: + bool tempDamaged = false; + const int TOUCH_POINT_RADIUS = 15; + std::vector> finger_positions; + std::vector> prev_finger_positions; +}; From 961b94238f8ef0472a8a8faf5b0d7804687c4d98 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 00:29:54 +0200 Subject: [PATCH 03/27] remove unused --- src/TouchVisualizer.hpp | 54 ----------------------------------------- 1 file changed, 54 deletions(-) diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index c7d289a..be2fdf8 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -1,63 +1,9 @@ -// OpenGL utility functions are copied from https://github.com/hyprwm/hyprland-plugins -// under BSD 3-Clause License. A copy of the license can be found in the LICENSE file - #include "src/devices/ITouch.hpp" #include #include #include #include -GLuint CompileShader(const GLuint& type, std::string src) { - auto shader = glCreateShader(type); - - auto shaderSource = src.c_str(); - - glShaderSource(shader, 1, (const GLchar**)&shaderSource, nullptr); - glCompileShader(shader); - - GLint ok; - glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); - - if (ok == GL_FALSE) - throw std::runtime_error("compileShader() failed!"); - - return shader; -} - -GLuint CreateProgram(const std::string& vert, const std::string& frag) { - auto vertCompiled = CompileShader(GL_VERTEX_SHADER, vert); - - if (!vertCompiled) - throw std::runtime_error("Compiling vshader failed."); - - auto fragCompiled = CompileShader(GL_FRAGMENT_SHADER, frag); - - if (!fragCompiled) - throw std::runtime_error("Compiling fshader failed."); - - auto prog = glCreateProgram(); - glAttachShader(prog, vertCompiled); - glAttachShader(prog, fragCompiled); - glLinkProgram(prog); - - glDetachShader(prog, vertCompiled); - glDetachShader(prog, fragCompiled); - glDeleteShader(vertCompiled); - glDeleteShader(fragCompiled); - - GLint ok; - glGetProgramiv(prog, GL_LINK_STATUS, &ok); - - if (ok == GL_FALSE) - throw std::runtime_error("createProgram() failed! GL_LINK_STATUS not OK!"); - - return prog; -} - -const std::string vertexShaderSource = R"#( - -)#"; - class Visualizer { public: void onRender(); From 1d8657b28de0889fe5663093d0fbb312172eb270 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 00:30:09 +0200 Subject: [PATCH 04/27] meson: build Visualizer --- src/meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/src/meson.build b/src/meson.build index 74510dd..157bd04 100644 --- a/src/meson.build +++ b/src/meson.build @@ -12,6 +12,7 @@ shared_module('hyprgrass', 'main.cpp', 'GestureManager.cpp', 'VecSet.cpp', + 'TouchVisualizer.cpp', cpp_args: ['-DWLR_USE_UNSTABLE'], link_with: [gestures], dependencies: [ From 38f0532e2768a42950be9e083eae6406258327ca Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 00:30:26 +0200 Subject: [PATCH 05/27] visualizer: try to draw something --- src/TouchVisualizer.cpp | 61 +++++++++++++++++++++++++++++++++++++++-- src/TouchVisualizer.hpp | 12 ++++++-- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 86570f3..ceb685d 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -1,15 +1,72 @@ #include "TouchVisualizer.hpp" #include "src/devices/ITouch.hpp" +#include "src/helpers/Region.hpp" #include "src/macros.hpp" +#include "src/render/OpenGL.hpp" #include "src/render/Renderer.hpp" +#include +#include #include +CBox boxAroundCenter(Vector2D center, double radius) { + return CBox(center.x, center.y, radius, radius); +} + +Visualizer::Visualizer() { + this->texture.allocate(); + + this->cairoSurface = + cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS); + auto cairo = cairo_create(this->cairoSurface); + + const double R = TOUCH_POINT_RADIUS; + auto radpat = cairo_pattern_create_radial(R, R, R, R, R, R); + cairo_pattern_add_color_stop_rgba(radpat, 0, 1.0, 1.0, 1.0, 0.8); + cairo_set_source(cairo, radpat); + cairo_fill(cairo); + + cairo_pattern_destroy(radpat); + cairo_destroy(cairo); +} + +Visualizer::~Visualizer() { + if (this->cairoSurface) + cairo_surface_destroy(this->cairoSurface); +} + void Visualizer::onRender() { if (this->finger_positions.size() < 1) { return; } - // this->finger_positions + const auto pos = this->finger_positions[0].second; + const auto last_pos = this->prev_finger_positions[0].second; + auto dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); + auto last_dmg = boxAroundCenter(last_pos, TOUCH_POINT_RADIUS); + + CRegion damage = {pos.x - TOUCH_POINT_RADIUS, pos.y - TOUCH_POINT_RADIUS, + static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; + + // FIXME: I am almost 100% certain this is wrong + const auto monitor = g_pCompositor->m_pLastMonitor.get(); + const auto monSize = monitor->vecSize; + + // idk what this does + g_pHyprRenderer->damageBox(&dmg); + g_pHyprRenderer->damageBox(&last_dmg); + + const auto data = cairo_image_surface_get_data(this->cairoSurface); + + // no deallocate??? + this->texture.allocate(); + glBindTexture(GL_TEXTURE_2D, this->texture.m_iTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, monSize.x, monSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f); + + g_pCompositor->scheduleFrameForMonitor(monitor); } void Visualizer::onTouchDown(ITouch::SDownEvent ev) { @@ -54,7 +111,7 @@ void Visualizer::damageAll() { } } -void Visualizer::damageFinger(uint64_t id) { +void Visualizer::damageFinger(int32_t id) { for (const auto& point : this->prev_finger_positions) { if (point.first != id) { continue; diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index be2fdf8..e6d8869 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -1,4 +1,6 @@ #include "src/devices/ITouch.hpp" +#include "src/render/Texture.hpp" +#include #include #include #include @@ -6,17 +8,21 @@ class Visualizer { public: + Visualizer(); + ~Visualizer(); void onRender(); void damageAll(); - void damageFinger(uint64_t id); + void damageFinger(int32_t id); void onTouchDown(ITouch::SDownEvent); void onTouchUp(ITouch::SUpEvent); void onTouchMotion(ITouch::SMotionEvent); private: + CTexture texture; + cairo_surface_t* cairoSurface; bool tempDamaged = false; const int TOUCH_POINT_RADIUS = 15; - std::vector> finger_positions; - std::vector> prev_finger_positions; + std::vector> finger_positions; + std::vector> prev_finger_positions; }; From 4fa5d29802c874e44f6399dccee9dbd4d1c541ad Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 00:30:37 +0200 Subject: [PATCH 06/27] main: wire up visualizer --- src/main.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8ea2dc5..c935961 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,7 @@ #include "GestureManager.hpp" +#include "TouchVisualizer.hpp" #include "globals.hpp" +#include "src/SharedDefs.hpp" #include "version.hpp" #include @@ -11,25 +13,31 @@ #include #include +#include #include const CHyprColor s_pluginColor = {0x61 / 255.0f, 0xAF / 255.0f, 0xEF / 255.0f, 1.0f}; +inline std::unique_ptr g_pVisualizer; + void hkOnTouchDown(void* _, SCallbackInfo& cbinfo, std::any e) { auto ev = std::any_cast(e); + g_pVisualizer->onTouchDown(ev); cbinfo.cancelled = g_pGestureManager->onTouchDown(ev); } void hkOnTouchUp(void* _, SCallbackInfo& cbinfo, std::any e) { auto ev = std::any_cast(e); + g_pVisualizer->onTouchUp(ev); cbinfo.cancelled = g_pGestureManager->onTouchUp(ev); } void hkOnTouchMove(void* _, SCallbackInfo& cbinfo, std::any e) { auto ev = std::any_cast(e); + g_pVisualizer->onTouchMotion(ev); cbinfo.cancelled = g_pGestureManager->onTouchMove(ev); } @@ -37,6 +45,18 @@ static void onPreConfigReload() { g_pGestureManager->internalBinds.clear(); } +void onRenderStage(eRenderStage stage) { + static auto const LONG_PRESS_DELAY = + (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") + ->getDataStaticPtr(); + + if (stage != RENDER_LAST_MOMENT || **LONG_PRESS_DELAY) { + return; + } + + g_pVisualizer->onRender(); +} + void listInternalBinds(std::string) { Debug::log(LOG, "[hyprgrass] Listing internal binds:"); for (const auto& bind : g_pGestureManager->internalBinds) { @@ -115,7 +135,8 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bind", onNewBind, Hyprlang::SHandlerOptions{}); HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bindm", onNewBind, Hyprlang::SHandlerOptions{}); static auto P0 = HyprlandAPI::registerCallbackDynamic( - PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { onPreConfigReload(); }); + PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { + onPreConfigReload(); }); HyprlandAPI::addDispatcher(PHANDLE, "touchBind", [&](std::string args) { HyprlandAPI::addNotification( @@ -144,6 +165,7 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { HyprlandAPI::reloadConfig(); g_pGestureManager = std::make_unique(); + g_pVisualizer = std::make_unique(); return {"hyprgrass", "Touchscreen gestures", "horriblename", HYPRGRASS_VERSION}; } From 4e353b15a1e33fb16e2f61c66065856f877a9fa4 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 04:27:06 +0200 Subject: [PATCH 07/27] dumb moment --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c935961..c0c6eaf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,7 @@ void onRenderStage(eRenderStage stage) { (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") ->getDataStaticPtr(); - if (stage != RENDER_LAST_MOMENT || **LONG_PRESS_DELAY) { + if (stage != RENDER_LAST_MOMENT || !**LONG_PRESS_DELAY) { return; } From 58638d618f9d12fa586cfdb6e3366d61c27f5934 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 04:31:06 +0200 Subject: [PATCH 08/27] add render hook --- src/main.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index c0c6eaf..18cdddf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,7 @@ #include "src/SharedDefs.hpp" #include "version.hpp" +#include #include #include #include @@ -129,7 +130,7 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:emulate_touchpad_swipe", Hyprlang::CConfigValue((Hyprlang::INT)0)); HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch", - Hyprlang::CConfigValue((Hyprlang::INT)0)); + Hyprlang::CConfigValue((Hyprlang::INT)1)); #pragma GCC diagnostic pop HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bind", onNewBind, Hyprlang::SHandlerOptions{}); @@ -161,6 +162,8 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { static auto P1 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "touchDown", hkOnTouchDown); static auto P2 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "touchUp", hkOnTouchUp); static auto P3 = HyprlandAPI::registerCallbackDynamic(PHANDLE, "touchMove", hkOnTouchMove); + static auto P4 = HyprlandAPI::registerCallbackDynamic( + PHANDLE, "render", [](void*, SCallbackInfo, std::any arg) { onRenderStage(std::any_cast(arg)); }); HyprlandAPI::reloadConfig(); From d6de5620cf59e720ba253102c99d9c8491eee508 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 04:31:22 +0200 Subject: [PATCH 09/27] FINALLY DRAWS SOMETHING --- src/TouchVisualizer.cpp | 53 ++++++++++++++--------------------------- src/TouchVisualizer.hpp | 7 +++--- 2 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index ceb685d..6d6cdbe 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -13,8 +13,6 @@ CBox boxAroundCenter(Vector2D center, double radius) { } Visualizer::Visualizer() { - this->texture.allocate(); - this->cairoSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS); auto cairo = cairo_create(this->cairoSurface); @@ -39,22 +37,24 @@ void Visualizer::onRender() { return; } - const auto pos = this->finger_positions[0].second; - const auto last_pos = this->prev_finger_positions[0].second; + // FIXME: I am almost 100% certain this is wrong + const auto monitor = g_pCompositor->m_pLastMonitor.get(); + const auto monSize = monitor->vecSize; + + const auto pos = this->finger_positions[0] * monSize; + const auto last_pos = this->prev_finger_positions[0] * monSize; auto dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); auto last_dmg = boxAroundCenter(last_pos, TOUCH_POINT_RADIUS); CRegion damage = {pos.x - TOUCH_POINT_RADIUS, pos.y - TOUCH_POINT_RADIUS, static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; - // FIXME: I am almost 100% certain this is wrong - const auto monitor = g_pCompositor->m_pLastMonitor.get(); - const auto monSize = monitor->vecSize; - - // idk what this does g_pHyprRenderer->damageBox(&dmg); g_pHyprRenderer->damageBox(&last_dmg); + // idk what this does + g_pCompositor->scheduleFrameForMonitor(monitor); + const auto data = cairo_image_surface_get_data(this->cairoSurface); // no deallocate??? @@ -64,42 +64,25 @@ void Visualizer::onRender() { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, monSize.x, monSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f); - g_pCompositor->scheduleFrameForMonitor(monitor); + Debug::log(LOG, "drawing at {}, {}, {}, {}", dmg.x, dmg.y, dmg.w, dmg.h); + + g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } void Visualizer::onTouchDown(ITouch::SDownEvent ev) { - this->finger_positions.push_back(std::pair(ev.touchID, ev.pos)); - this->prev_finger_positions.push_back(std::pair(ev.touchID, ev.pos)); + this->finger_positions.emplace(ev.touchID, ev.pos); + this->prev_finger_positions.emplace(std::pair(ev.touchID, ev.pos)); } void Visualizer::onTouchUp(ITouch::SUpEvent ev) { - for (auto iter = this->prev_finger_positions.begin(); iter != this->prev_finger_positions.end(); iter++) { - this->prev_finger_positions.erase(iter); - } - - for (auto iter = this->finger_positions.begin(); iter != this->finger_positions.end(); iter++) { - this->finger_positions.erase(iter); - } + this->finger_positions.erase(ev.touchID); + this->prev_finger_positions.erase(ev.touchID); } void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { - for (auto& pair : this->finger_positions) { - if (pair.first != ev.touchID) - continue; - - for (auto& prev : this->prev_finger_positions) { - if (prev.first != ev.touchID) - continue; - - prev.second = pair.second; - pair.second = ev.pos; - - return; - } - break; - } + this->prev_finger_positions[ev.touchID] = this->finger_positions[ev.touchID]; + this->finger_positions[ev.touchID] = ev.pos; } void Visualizer::damageAll() { diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index e6d8869..b38887a 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -3,8 +3,7 @@ #include #include #include -#include -#include +#include class Visualizer { public: @@ -23,6 +22,6 @@ class Visualizer { cairo_surface_t* cairoSurface; bool tempDamaged = false; const int TOUCH_POINT_RADIUS = 15; - std::vector> finger_positions; - std::vector> prev_finger_positions; + std::unordered_map finger_positions; + std::unordered_map prev_finger_positions; }; From e684af58633b033ea96e981e52068d005e2de392 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 9 Jun 2024 13:16:11 +0200 Subject: [PATCH 10/27] remove debug log --- src/TouchVisualizer.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 6d6cdbe..72eafa1 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -65,8 +65,6 @@ void Visualizer::onRender() { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, monSize.x, monSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - Debug::log(LOG, "drawing at {}, {}, {}, {}", dmg.x, dmg.y, dmg.w, dmg.h); - g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } From dcad2878cad74ce46270b752f55023b6a40960f2 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 16 Jun 2024 18:03:52 +0200 Subject: [PATCH 11/27] fix: circle --- src/TouchVisualizer.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 72eafa1..5efeb4e 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -1,6 +1,5 @@ #include "TouchVisualizer.hpp" #include "src/devices/ITouch.hpp" -#include "src/helpers/Region.hpp" #include "src/macros.hpp" #include "src/render/OpenGL.hpp" #include "src/render/Renderer.hpp" @@ -13,17 +12,16 @@ CBox boxAroundCenter(Vector2D center, double radius) { } Visualizer::Visualizer() { + const int R = TOUCH_POINT_RADIUS; + this->cairoSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS); - auto cairo = cairo_create(this->cairoSurface); + auto cairo = cairo_create(cairoSurface); - const double R = TOUCH_POINT_RADIUS; - auto radpat = cairo_pattern_create_radial(R, R, R, R, R, R); - cairo_pattern_add_color_stop_rgba(radpat, 0, 1.0, 1.0, 1.0, 0.8); - cairo_set_source(cairo, radpat); + cairo_arc(cairo, R, R, R, 0, 2 * PI); + cairo_set_source_rgba(cairo, 0.8, 0.8, 0.1, 0.8); cairo_fill(cairo); - cairo_pattern_destroy(radpat); cairo_destroy(cairo); } @@ -46,24 +44,21 @@ void Visualizer::onRender() { auto dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); auto last_dmg = boxAroundCenter(last_pos, TOUCH_POINT_RADIUS); - CRegion damage = {pos.x - TOUCH_POINT_RADIUS, pos.y - TOUCH_POINT_RADIUS, - static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; - g_pHyprRenderer->damageBox(&dmg); g_pHyprRenderer->damageBox(&last_dmg); // idk what this does g_pCompositor->scheduleFrameForMonitor(monitor); - const auto data = cairo_image_surface_get_data(this->cairoSurface); + const unsigned char* data = cairo_image_surface_get_data(this->cairoSurface); - // no deallocate??? - this->texture.allocate(); - glBindTexture(GL_TEXTURE_2D, this->texture.m_iTexID); + this->texture->allocate(); + glBindTexture(GL_TEXTURE_2D, this->texture->m_iTexID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, monSize.x, monSize.y, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS, 0, GL_RGBA, + GL_UNSIGNED_BYTE, data); g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } From 44b690ebc3ecf38f89ebb9e2cb6da2e60db10c2f Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 16 Jun 2024 18:04:16 +0200 Subject: [PATCH 12/27] fix: hl breaking changes --- src/TouchVisualizer.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index b38887a..863a10e 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -1,4 +1,5 @@ #include "src/devices/ITouch.hpp" +#include "src/helpers/memory/SharedPtr.hpp" #include "src/render/Texture.hpp" #include #include @@ -18,7 +19,7 @@ class Visualizer { void onTouchMotion(ITouch::SMotionEvent); private: - CTexture texture; + SP texture = makeShared(); cairo_surface_t* cairoSurface; bool tempDamaged = false; const int TOUCH_POINT_RADIUS = 15; From 5ed3638131f9d1c3a3c252659b061c35a2c868eb Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 16 Jun 2024 18:04:27 +0200 Subject: [PATCH 13/27] nix: export inputs for debugging --- flake.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/flake.nix b/flake.nix index 85fc6e9..593d013 100644 --- a/flake.nix +++ b/flake.nix @@ -10,6 +10,9 @@ inherit (hyprland.inputs) nixpkgs; withPkgsFor = fn: nixpkgs.lib.genAttrs (builtins.attrNames hyprland.packages) (system: fn system nixpkgs.legacyPackages.${system}); in { + # for debugging + inherit inputs; + packages = withPkgsFor (system: pkgs: let hyprgrassPackage = pkgs.callPackage ./nix/default.nix { inherit (hyprland.packages.${system}) hyprland; From 24b696f3ed2dbcda71b2afc1f849c6351934cfed Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Thu, 20 Jun 2024 21:19:16 +0200 Subject: [PATCH 14/27] tweak numbers --- src/TouchVisualizer.cpp | 2 +- src/TouchVisualizer.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 5efeb4e..71ef96f 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -19,7 +19,7 @@ Visualizer::Visualizer() { auto cairo = cairo_create(cairoSurface); cairo_arc(cairo, R, R, R, 0, 2 * PI); - cairo_set_source_rgba(cairo, 0.8, 0.8, 0.1, 0.8); + cairo_set_source_rgba(cairo, 0.8, 0.8, 0.1, 0.6); cairo_fill(cairo); cairo_destroy(cairo); diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index 863a10e..2c79784 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -22,7 +22,7 @@ class Visualizer { SP texture = makeShared(); cairo_surface_t* cairoSurface; bool tempDamaged = false; - const int TOUCH_POINT_RADIUS = 15; + const int TOUCH_POINT_RADIUS = 30; std::unordered_map finger_positions; std::unordered_map prev_finger_positions; }; From 8b955475482c77c9d3ae6ed7a15af7a4cc88e375 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Thu, 20 Jun 2024 21:19:28 +0200 Subject: [PATCH 15/27] fix: use monitor pixel size --- src/TouchVisualizer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 71ef96f..b38c82d 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -37,7 +37,7 @@ void Visualizer::onRender() { // FIXME: I am almost 100% certain this is wrong const auto monitor = g_pCompositor->m_pLastMonitor.get(); - const auto monSize = monitor->vecSize; + const auto monSize = monitor->vecPixelSize; const auto pos = this->finger_positions[0] * monSize; const auto last_pos = this->prev_finger_positions[0] * monSize; From 3f2132e55f218978a295cd54f6fcab27c4638ae7 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Tue, 25 Jun 2024 19:44:06 +0200 Subject: [PATCH 16/27] draw all fingers --- src/TouchVisualizer.cpp | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index b38c82d..bc0c3b9 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -39,28 +39,31 @@ void Visualizer::onRender() { const auto monitor = g_pCompositor->m_pLastMonitor.get(); const auto monSize = monitor->vecPixelSize; - const auto pos = this->finger_positions[0] * monSize; - const auto last_pos = this->prev_finger_positions[0] * monSize; - auto dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); - auto last_dmg = boxAroundCenter(last_pos, TOUCH_POINT_RADIUS); + for (int i = 0; i < this->finger_positions.size(); i++) { + const auto pos = this->finger_positions[i] * monSize + monitor->vecPosition; + const auto last_pos = this->prev_finger_positions[i] * monSize + monitor->vecPosition; + this->prev_finger_positions[i] = this->finger_positions[i]; + auto dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); + auto last_dmg = boxAroundCenter(last_pos, TOUCH_POINT_RADIUS); - g_pHyprRenderer->damageBox(&dmg); - g_pHyprRenderer->damageBox(&last_dmg); + g_pHyprRenderer->damageBox(&dmg); + g_pHyprRenderer->damageBox(&last_dmg); - // idk what this does - g_pCompositor->scheduleFrameForMonitor(monitor); + // idk what this does + g_pCompositor->scheduleFrameForMonitor(monitor); - const unsigned char* data = cairo_image_surface_get_data(this->cairoSurface); + const unsigned char* data = cairo_image_surface_get_data(this->cairoSurface); - this->texture->allocate(); - glBindTexture(GL_TEXTURE_2D, this->texture->m_iTexID); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + this->texture->allocate(); + glBindTexture(GL_TEXTURE_2D, this->texture->m_iTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS, 0, GL_RGBA, - GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS, 0, GL_RGBA, + GL_UNSIGNED_BYTE, data); - g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); + g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); + } } void Visualizer::onTouchDown(ITouch::SDownEvent ev) { @@ -74,8 +77,7 @@ void Visualizer::onTouchUp(ITouch::SUpEvent ev) { } void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { - this->prev_finger_positions[ev.touchID] = this->finger_positions[ev.touchID]; - this->finger_positions[ev.touchID] = ev.pos; + this->finger_positions[ev.touchID] = ev.pos; } void Visualizer::damageAll() { From 79dd6c01c07e1ec38e8821c8b42aa6b742e25df7 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Wed, 23 Oct 2024 17:55:11 +0200 Subject: [PATCH 17/27] fix: renamed upstream header --- src/TouchVisualizer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index 2c79784..a85f8d5 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -1,9 +1,9 @@ #include "src/devices/ITouch.hpp" -#include "src/helpers/memory/SharedPtr.hpp" #include "src/render/Texture.hpp" #include #include #include +#include #include class Visualizer { From 629605df2738ffa295c9de02bbd20387ea335b8f Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Wed, 23 Oct 2024 18:31:33 +0200 Subject: [PATCH 18/27] damage finger on touch events --- src/TouchVisualizer.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index bc0c3b9..a960c4f 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -69,15 +69,19 @@ void Visualizer::onRender() { void Visualizer::onTouchDown(ITouch::SDownEvent ev) { this->finger_positions.emplace(ev.touchID, ev.pos); this->prev_finger_positions.emplace(std::pair(ev.touchID, ev.pos)); + this->damageFinger(ev.touchID); } void Visualizer::onTouchUp(ITouch::SUpEvent ev) { + this->damageFinger(ev.touchID); this->finger_positions.erase(ev.touchID); this->prev_finger_positions.erase(ev.touchID); } void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { + this->damageFinger(ev.touchID); this->finger_positions[ev.touchID] = ev.pos; + this->damageFinger(ev.touchID); } void Visualizer::damageAll() { From 3b50ac26a6adfce6644af689cf5495e72343aeac Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Wed, 23 Oct 2024 18:54:34 +0200 Subject: [PATCH 19/27] remove unused damageAll --- src/TouchVisualizer.cpp | 9 --------- src/TouchVisualizer.hpp | 1 - 2 files changed, 10 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index a960c4f..c88187c 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -84,15 +84,6 @@ void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { this->damageFinger(ev.touchID); } -void Visualizer::damageAll() { - CBox dm; - for (const auto& point : this->prev_finger_positions) { - dm = CBox{point.second.x - TOUCH_POINT_RADIUS, point.second.y - TOUCH_POINT_RADIUS, - static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; - g_pHyprRenderer->damageBox(&dm); - } -} - void Visualizer::damageFinger(int32_t id) { for (const auto& point : this->prev_finger_positions) { if (point.first != id) { diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index a85f8d5..904a8c8 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -11,7 +11,6 @@ class Visualizer { Visualizer(); ~Visualizer(); void onRender(); - void damageAll(); void damageFinger(int32_t id); void onTouchDown(ITouch::SDownEvent); From 95b4b0faf279795651a45168ced6604bcdeff0e3 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:27:30 +0200 Subject: [PATCH 20/27] render texture only in ctor --- src/TouchVisualizer.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index c88187c..e5a8a4d 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -6,9 +6,10 @@ #include #include #include +#include CBox boxAroundCenter(Vector2D center, double radius) { - return CBox(center.x, center.y, radius, radius); + return CBox(center.x - radius, center.y - radius, 2 * radius, 2 * radius); } Visualizer::Visualizer() { @@ -23,6 +24,16 @@ Visualizer::Visualizer() { cairo_fill(cairo); cairo_destroy(cairo); + + const unsigned char* data = cairo_image_surface_get_data(this->cairoSurface); + + this->texture->allocate(); + glBindTexture(GL_TEXTURE_2D, this->texture->m_iTexID); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS, 0, GL_RGBA, + GL_UNSIGNED_BYTE, data); } Visualizer::~Visualizer() { @@ -35,7 +46,6 @@ void Visualizer::onRender() { return; } - // FIXME: I am almost 100% certain this is wrong const auto monitor = g_pCompositor->m_pLastMonitor.get(); const auto monSize = monitor->vecPixelSize; @@ -52,16 +62,6 @@ void Visualizer::onRender() { // idk what this does g_pCompositor->scheduleFrameForMonitor(monitor); - const unsigned char* data = cairo_image_surface_get_data(this->cairoSurface); - - this->texture->allocate(); - glBindTexture(GL_TEXTURE_2D, this->texture->m_iTexID); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2 * TOUCH_POINT_RADIUS, 2 * TOUCH_POINT_RADIUS, 0, GL_RGBA, - GL_UNSIGNED_BYTE, data); - g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } } From 23b744b478df23daed281c0c3a58df193a346124 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:28:16 +0200 Subject: [PATCH 21/27] pre-determine pixel coords on touch events --- src/TouchVisualizer.cpp | 67 +++++++++++++++++++++++++---------------- src/TouchVisualizer.hpp | 11 +++++-- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index e5a8a4d..b75d7c7 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -41,6 +41,28 @@ Visualizer::~Visualizer() { cairo_surface_destroy(this->cairoSurface); } +void Visualizer::onPreRender() { + for (auto& finger : this->finger_positions) { + const auto pos = finger.second.curr; + + // idk how to properly damage stuff but if I just always damage two oldest frames I can't be wrong :) + if (finger.second.last_rendered2.has_value()) { + CBox dmg = boxAroundCenter(finger.second.last_rendered2.value(), TOUCH_POINT_RADIUS); + g_pHyprRenderer->damageBox(&dmg); + } + finger.second.last_rendered2 = finger.second.last_rendered; + + if (finger.second.last_rendered.has_value()) { + CBox dmg = boxAroundCenter(finger.second.last_rendered.value(), TOUCH_POINT_RADIUS); + g_pHyprRenderer->damageBox(&dmg); + } + finger.second.last_rendered = std::optional(finger.second.curr); + CBox dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); + + g_pHyprRenderer->damageBox(&dmg); + } +} + void Visualizer::onRender() { if (this->finger_positions.size() < 1) { return; @@ -49,52 +71,45 @@ void Visualizer::onRender() { const auto monitor = g_pCompositor->m_pLastMonitor.get(); const auto monSize = monitor->vecPixelSize; - for (int i = 0; i < this->finger_positions.size(); i++) { - const auto pos = this->finger_positions[i] * monSize + monitor->vecPosition; - const auto last_pos = this->prev_finger_positions[i] * monSize + monitor->vecPosition; - this->prev_finger_positions[i] = this->finger_positions[i]; - auto dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); - auto last_dmg = boxAroundCenter(last_pos, TOUCH_POINT_RADIUS); - + for (auto& finger : this->finger_positions) { + CBox dmg = boxAroundCenter(finger.second.curr, TOUCH_POINT_RADIUS); g_pHyprRenderer->damageBox(&dmg); - g_pHyprRenderer->damageBox(&last_dmg); // idk what this does - g_pCompositor->scheduleFrameForMonitor(monitor); - g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } } void Visualizer::onTouchDown(ITouch::SDownEvent ev) { - this->finger_positions.emplace(ev.touchID, ev.pos); - this->prev_finger_positions.emplace(std::pair(ev.touchID, ev.pos)); - this->damageFinger(ev.touchID); + auto mon = g_pCompositor->m_pLastMonitor.get(); + this->finger_positions.emplace(ev.touchID, FingerPos{ev.pos * mon->vecPixelSize + mon->vecPosition, std::nullopt}); + g_pCompositor->scheduleFrameForMonitor(mon); } void Visualizer::onTouchUp(ITouch::SUpEvent ev) { this->damageFinger(ev.touchID); this->finger_positions.erase(ev.touchID); - this->prev_finger_positions.erase(ev.touchID); + g_pCompositor->scheduleFrameForMonitor(g_pCompositor->m_pLastMonitor.get()); } void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { - this->damageFinger(ev.touchID); - this->finger_positions[ev.touchID] = ev.pos; - this->damageFinger(ev.touchID); + auto mon = g_pCompositor->m_pLastMonitor.get(); + this->finger_positions[ev.touchID] = {ev.pos * mon->vecPixelSize + mon->vecPosition, std::nullopt}; + g_pCompositor->scheduleFrameForMonitor(mon); } void Visualizer::damageFinger(int32_t id) { - for (const auto& point : this->prev_finger_positions) { - if (point.first != id) { - continue; - } + auto finger = this->finger_positions.at(id); + + CBox dm = boxAroundCenter(finger.curr, TOUCH_POINT_RADIUS); + g_pHyprRenderer->damageBox(&dm); - CBox dm = {point.second.x - TOUCH_POINT_RADIUS, point.second.y - TOUCH_POINT_RADIUS, - static_cast(2 * TOUCH_POINT_RADIUS), static_cast(2 * TOUCH_POINT_RADIUS)}; + if (finger.last_rendered.has_value()) { + dm = boxAroundCenter(finger.last_rendered.value(), TOUCH_POINT_RADIUS); + g_pHyprRenderer->damageBox(&dm); + } + if (finger.last_rendered2.has_value()) { + dm = boxAroundCenter(finger.last_rendered2.value(), TOUCH_POINT_RADIUS); g_pHyprRenderer->damageBox(&dm); - return; } - - RASSERT(false, "Visualizer tried to damage non-existent finger id {}", id) } diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index 904a8c8..e790c5b 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -4,12 +4,20 @@ #include #include #include +#include #include +struct FingerPos { + Vector2D curr; + std::optional last_rendered; + std::optional last_rendered2; +}; + class Visualizer { public: Visualizer(); ~Visualizer(); + void onPreRender(); void onRender(); void damageFinger(int32_t id); @@ -22,6 +30,5 @@ class Visualizer { cairo_surface_t* cairoSurface; bool tempDamaged = false; const int TOUCH_POINT_RADIUS = 30; - std::unordered_map finger_positions; - std::unordered_map prev_finger_positions; + std::unordered_map finger_positions; }; From bf4651d8dc817fe563c9b3a0696868f034b91b6d Mon Sep 17 00:00:00 2001 From: Ching Pei Yang <59727193+horriblename@users.noreply.github.com> Date: Wed, 23 Oct 2024 21:28:25 +0200 Subject: [PATCH 22/27] hook prerender --- src/main.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 18cdddf..2aac59a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,15 +47,11 @@ static void onPreConfigReload() { } void onRenderStage(eRenderStage stage) { - static auto const LONG_PRESS_DELAY = - (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") - ->getDataStaticPtr(); - - if (stage != RENDER_LAST_MOMENT || !**LONG_PRESS_DELAY) { - return; + if (stage == RENDER_PRE) { + g_pVisualizer->onPreRender(); + } else if (stage == RENDER_LAST_MOMENT) { + g_pVisualizer->onRender(); } - - g_pVisualizer->onRender(); } void listInternalBinds(std::string) { From 5b5c0a57c9a690cae4891872ec7f9c2c4aad1f88 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sat, 9 Nov 2024 17:38:28 +0100 Subject: [PATCH 23/27] adapt upstream breaking changes --- src/TouchVisualizer.cpp | 18 +++--------------- src/TouchVisualizer.hpp | 1 - 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index b75d7c7..6855401 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -45,13 +45,6 @@ void Visualizer::onPreRender() { for (auto& finger : this->finger_positions) { const auto pos = finger.second.curr; - // idk how to properly damage stuff but if I just always damage two oldest frames I can't be wrong :) - if (finger.second.last_rendered2.has_value()) { - CBox dmg = boxAroundCenter(finger.second.last_rendered2.value(), TOUCH_POINT_RADIUS); - g_pHyprRenderer->damageBox(&dmg); - } - finger.second.last_rendered2 = finger.second.last_rendered; - if (finger.second.last_rendered.has_value()) { CBox dmg = boxAroundCenter(finger.second.last_rendered.value(), TOUCH_POINT_RADIUS); g_pHyprRenderer->damageBox(&dmg); @@ -75,13 +68,12 @@ void Visualizer::onRender() { CBox dmg = boxAroundCenter(finger.second.curr, TOUCH_POINT_RADIUS); g_pHyprRenderer->damageBox(&dmg); - // idk what this does g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } } void Visualizer::onTouchDown(ITouch::SDownEvent ev) { - auto mon = g_pCompositor->m_pLastMonitor.get(); + auto mon = g_pCompositor->m_pLastMonitor.lock(); this->finger_positions.emplace(ev.touchID, FingerPos{ev.pos * mon->vecPixelSize + mon->vecPosition, std::nullopt}); g_pCompositor->scheduleFrameForMonitor(mon); } @@ -89,11 +81,11 @@ void Visualizer::onTouchDown(ITouch::SDownEvent ev) { void Visualizer::onTouchUp(ITouch::SUpEvent ev) { this->damageFinger(ev.touchID); this->finger_positions.erase(ev.touchID); - g_pCompositor->scheduleFrameForMonitor(g_pCompositor->m_pLastMonitor.get()); + g_pCompositor->scheduleFrameForMonitor(g_pCompositor->m_pLastMonitor.lock()); } void Visualizer::onTouchMotion(ITouch::SMotionEvent ev) { - auto mon = g_pCompositor->m_pLastMonitor.get(); + auto mon = g_pCompositor->m_pLastMonitor.lock(); this->finger_positions[ev.touchID] = {ev.pos * mon->vecPixelSize + mon->vecPosition, std::nullopt}; g_pCompositor->scheduleFrameForMonitor(mon); } @@ -108,8 +100,4 @@ void Visualizer::damageFinger(int32_t id) { dm = boxAroundCenter(finger.last_rendered.value(), TOUCH_POINT_RADIUS); g_pHyprRenderer->damageBox(&dm); } - if (finger.last_rendered2.has_value()) { - dm = boxAroundCenter(finger.last_rendered2.value(), TOUCH_POINT_RADIUS); - g_pHyprRenderer->damageBox(&dm); - } } diff --git a/src/TouchVisualizer.hpp b/src/TouchVisualizer.hpp index e790c5b..6483501 100644 --- a/src/TouchVisualizer.hpp +++ b/src/TouchVisualizer.hpp @@ -10,7 +10,6 @@ struct FingerPos { Vector2D curr; std::optional last_rendered; - std::optional last_rendered2; }; class Visualizer { From 9b9fa30ceb93ab6449fd8b689599cc673697a3fc Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sat, 9 Nov 2024 18:13:06 +0100 Subject: [PATCH 24/27] move damage out of prerender --- src/TouchVisualizer.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 6855401..4c2660f 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -41,20 +41,7 @@ Visualizer::~Visualizer() { cairo_surface_destroy(this->cairoSurface); } -void Visualizer::onPreRender() { - for (auto& finger : this->finger_positions) { - const auto pos = finger.second.curr; - - if (finger.second.last_rendered.has_value()) { - CBox dmg = boxAroundCenter(finger.second.last_rendered.value(), TOUCH_POINT_RADIUS); - g_pHyprRenderer->damageBox(&dmg); - } - finger.second.last_rendered = std::optional(finger.second.curr); - CBox dmg = boxAroundCenter(pos, TOUCH_POINT_RADIUS); - - g_pHyprRenderer->damageBox(&dmg); - } -} +void Visualizer::onPreRender() {} void Visualizer::onRender() { if (this->finger_positions.size() < 1) { @@ -65,6 +52,11 @@ void Visualizer::onRender() { const auto monSize = monitor->vecPixelSize; for (auto& finger : this->finger_positions) { + if (finger.second.last_rendered.has_value()) { + CBox dmg = boxAroundCenter(finger.second.last_rendered.value(), TOUCH_POINT_RADIUS); + g_pHyprRenderer->damageBox(&dmg); + } + CBox dmg = boxAroundCenter(finger.second.curr, TOUCH_POINT_RADIUS); g_pHyprRenderer->damageBox(&dmg); From 541736ef2f2fe522a87686441edda04a9ef8db1e Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Sun, 10 Nov 2024 01:20:16 +0100 Subject: [PATCH 25/27] damage monitor cuz i can't be arsed anymore --- src/TouchVisualizer.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/TouchVisualizer.cpp b/src/TouchVisualizer.cpp index 4c2660f..3a339a6 100644 --- a/src/TouchVisualizer.cpp +++ b/src/TouchVisualizer.cpp @@ -48,18 +48,18 @@ void Visualizer::onRender() { return; } - const auto monitor = g_pCompositor->m_pLastMonitor.get(); - const auto monSize = monitor->vecPixelSize; + const auto monitor = g_pCompositor->m_pLastMonitor.lock(); + + // HACK: should not damage monitor, however, I don't understand jackshit + // about damage so here we are. + // If you know how to do damage properly I BEG OF YOU PLEASE ABSOLVE ME + // OF MY SINS + if (this->finger_positions.size()) { + g_pHyprRenderer->damageMonitor(monitor); + } for (auto& finger : this->finger_positions) { - if (finger.second.last_rendered.has_value()) { - CBox dmg = boxAroundCenter(finger.second.last_rendered.value(), TOUCH_POINT_RADIUS); - g_pHyprRenderer->damageBox(&dmg); - } - CBox dmg = boxAroundCenter(finger.second.curr, TOUCH_POINT_RADIUS); - g_pHyprRenderer->damageBox(&dmg); - g_pHyprOpenGL->renderTexture(this->texture, &dmg, 1.f, 0, true); } } From a7b6aa6b84582baaaca72ec0cf960e5456f55138 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Thu, 14 Nov 2024 16:47:29 +0100 Subject: [PATCH 26/27] fix: check config before visualize --- src/main.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2aac59a..c8011a4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,21 +24,36 @@ inline std::unique_ptr g_pVisualizer; void hkOnTouchDown(void* _, SCallbackInfo& cbinfo, std::any e) { auto ev = std::any_cast(e); - g_pVisualizer->onTouchDown(ev); + static auto const VISUALIZE = + (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") + ->getDataStaticPtr(); + + if (**VISUALIZE) + g_pVisualizer->onTouchDown(ev); cbinfo.cancelled = g_pGestureManager->onTouchDown(ev); } void hkOnTouchUp(void* _, SCallbackInfo& cbinfo, std::any e) { auto ev = std::any_cast(e); - g_pVisualizer->onTouchUp(ev); + static auto const VISUALIZE = + (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") + ->getDataStaticPtr(); + + if (**VISUALIZE) + g_pVisualizer->onTouchUp(ev); cbinfo.cancelled = g_pGestureManager->onTouchUp(ev); } void hkOnTouchMove(void* _, SCallbackInfo& cbinfo, std::any e) { auto ev = std::any_cast(e); - g_pVisualizer->onTouchMotion(ev); + static auto const VISUALIZE = + (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") + ->getDataStaticPtr(); + + if (**VISUALIZE) + g_pVisualizer->onTouchMotion(ev); cbinfo.cancelled = g_pGestureManager->onTouchMove(ev); } @@ -47,9 +62,11 @@ static void onPreConfigReload() { } void onRenderStage(eRenderStage stage) { - if (stage == RENDER_PRE) { - g_pVisualizer->onPreRender(); - } else if (stage == RENDER_LAST_MOMENT) { + static auto const VISUALIZE = + (Hyprlang::INT* const*)HyprlandAPI::getConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch") + ->getDataStaticPtr(); + + if (stage == RENDER_LAST_MOMENT && **VISUALIZE) { g_pVisualizer->onRender(); } } @@ -126,7 +143,7 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:emulate_touchpad_swipe", Hyprlang::CConfigValue((Hyprlang::INT)0)); HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch", - Hyprlang::CConfigValue((Hyprlang::INT)1)); + Hyprlang::CConfigValue((Hyprlang::INT)0)); #pragma GCC diagnostic pop HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bind", onNewBind, Hyprlang::SHandlerOptions{}); From 76188ad8ec95b300fb055f83a2c8da9af2077490 Mon Sep 17 00:00:00 2001 From: Ching Pei Yang Date: Fri, 13 Dec 2024 04:07:52 +0100 Subject: [PATCH 27/27] remove outdated diagnostic hints --- src/main.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index c8011a4..5700b55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -124,8 +124,6 @@ APICALL EXPORT std::string PLUGIN_API_VERSION() { APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { PHANDLE = handle; -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:workspace_swipe_fingers", Hyprlang::CConfigValue((Hyprlang::INT)3)); HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:workspace_swipe_edge", @@ -144,13 +142,11 @@ APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) { Hyprlang::CConfigValue((Hyprlang::INT)0)); HyprlandAPI::addConfigValue(PHANDLE, "plugin:touch_gestures:debug:visualize_touch", Hyprlang::CConfigValue((Hyprlang::INT)0)); -#pragma GCC diagnostic pop HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bind", onNewBind, Hyprlang::SHandlerOptions{}); HyprlandAPI::addConfigKeyword(PHANDLE, "hyprgrass-bindm", onNewBind, Hyprlang::SHandlerOptions{}); static auto P0 = HyprlandAPI::registerCallbackDynamic( - PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { - onPreConfigReload(); }); + PHANDLE, "preConfigReload", [&](void* self, SCallbackInfo& info, std::any data) { onPreConfigReload(); }); HyprlandAPI::addDispatcher(PHANDLE, "touchBind", [&](std::string args) { HyprlandAPI::addNotification(