From 7a4324b2ff38ef542175c30bbd35efba399487e1 Mon Sep 17 00:00:00 2001 From: Almamu Date: Fri, 8 Nov 2024 03:20:25 +0100 Subject: [PATCH] chore: use proper defines for wayland buttons and improve click handling code --- src/WallpaperEngine/Input/CMouseInput.h | 5 ++-- .../Input/Drivers/CGLFWMouseInput.cpp | 25 ++----------------- .../Input/Drivers/CWaylandMouseInput.cpp | 23 +++-------------- .../Render/Drivers/CWaylandOpenGLDriver.cpp | 7 +++--- .../Drivers/Output/CWaylandOutputViewport.h | 4 +-- .../Render/Wallpapers/CWeb.cpp | 7 ++++-- src/WallpaperEngine/Render/Wallpapers/CWeb.h | 3 +++ 7 files changed, 20 insertions(+), 54 deletions(-) diff --git a/src/WallpaperEngine/Input/CMouseInput.h b/src/WallpaperEngine/Input/CMouseInput.h index 9aee5fe..9ab41b8 100644 --- a/src/WallpaperEngine/Input/CMouseInput.h +++ b/src/WallpaperEngine/Input/CMouseInput.h @@ -4,9 +4,8 @@ namespace WallpaperEngine::Input { enum MouseClickStatus : int { - Waiting = 0, - Released = 1, - Clicked = 2 + Released = 0, + Clicked = 1 }; /** diff --git a/src/WallpaperEngine/Input/Drivers/CGLFWMouseInput.cpp b/src/WallpaperEngine/Input/Drivers/CGLFWMouseInput.cpp index 6a1f5b6..ce96911 100644 --- a/src/WallpaperEngine/Input/Drivers/CGLFWMouseInput.cpp +++ b/src/WallpaperEngine/Input/Drivers/CGLFWMouseInput.cpp @@ -19,29 +19,8 @@ void CGLFWMouseInput::update () { int leftClickState = glfwGetMouseButton (this->m_driver->getWindow (), GLFW_MOUSE_BUTTON_LEFT); int rightClickState = glfwGetMouseButton (this->m_driver->getWindow (), GLFW_MOUSE_BUTTON_RIGHT); - if (leftClickState == GLFW_RELEASE) { - if (this->m_leftClick == MouseClickStatus::Released) { - this->m_leftClick = MouseClickStatus::Waiting; - } - - if (this->m_leftClick == MouseClickStatus::Clicked) { - this->m_leftClick = MouseClickStatus::Released; - } - } else { - this->m_leftClick = MouseClickStatus::Clicked; - } - - if (rightClickState == GLFW_RELEASE) { - if (this->m_rightClick == MouseClickStatus::Released) { - this->m_rightClick = MouseClickStatus::Waiting; - } - - if (this->m_rightClick == MouseClickStatus::Clicked) { - this->m_rightClick = MouseClickStatus::Released; - } - } else { - this->m_rightClick = MouseClickStatus::Clicked; - } + this->m_leftClick = leftClickState == GLFW_RELEASE ? MouseClickStatus::Released : MouseClickStatus::Clicked; + this->m_rightClick = rightClickState == GLFW_RELEASE ? MouseClickStatus::Released : MouseClickStatus::Clicked; // update current mouse position glfwGetCursorPos (this->m_driver->getWindow (), &this->m_mousePosition.x, &this->m_mousePosition.y); diff --git a/src/WallpaperEngine/Input/Drivers/CWaylandMouseInput.cpp b/src/WallpaperEngine/Input/Drivers/CWaylandMouseInput.cpp index 06fe635..36940ec 100644 --- a/src/WallpaperEngine/Input/Drivers/CWaylandMouseInput.cpp +++ b/src/WallpaperEngine/Input/Drivers/CWaylandMouseInput.cpp @@ -7,24 +7,7 @@ CWaylandMouseInput::CWaylandMouseInput (WallpaperEngine::Render::Drivers::CWayla waylandDriver (driver), m_pos () {} -void CWaylandMouseInput::update () { - if (!this->waylandDriver->getApp ().getContext ().settings.mouse.enabled) { - return; - } - - if (!waylandDriver->viewportInFocus || !waylandDriver->viewportInFocus->rendering) { - return; - } - - // TODO: IS CLEARING STATE HERE A GOOD SOLUTION? OR SHOULD BE HANDLED SOMEWHERE ELSE? - if (waylandDriver->viewportInFocus->leftClick == MouseClickStatus::Released) { - waylandDriver->viewportInFocus->leftClick = MouseClickStatus::Waiting; - } - - if (waylandDriver->viewportInFocus->rightClick == MouseClickStatus::Released) { - waylandDriver->viewportInFocus->rightClick = MouseClickStatus::Waiting; - } -} +void CWaylandMouseInput::update () {} glm::dvec2 CWaylandMouseInput::position () const { if (!this->waylandDriver->getApp ().getContext ().settings.mouse.enabled) { @@ -41,12 +24,12 @@ WallpaperEngine::Input::MouseClickStatus CWaylandMouseInput::leftClick () const if (waylandDriver->viewportInFocus && waylandDriver->viewportInFocus->rendering) return waylandDriver->viewportInFocus->leftClick; - return MouseClickStatus::Waiting; + return MouseClickStatus::Released; } WallpaperEngine::Input::MouseClickStatus CWaylandMouseInput::rightClick () const { if (waylandDriver->viewportInFocus && waylandDriver->viewportInFocus->rendering) return waylandDriver->viewportInFocus->rightClick; - return MouseClickStatus::Waiting; + return MouseClickStatus::Released; } \ No newline at end of file diff --git a/src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.cpp b/src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.cpp index 80dc9bf..faa442f 100644 --- a/src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.cpp +++ b/src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.cpp @@ -9,6 +9,7 @@ extern "C" { #include "wlr-layer-shell-unstable-v1-protocol.h" #include "xdg-shell-protocol.h" +#include } #undef class #undef namespace @@ -56,15 +57,13 @@ static void handlePointerButton (void* data, struct wl_pointer* wl_pointer, uint if (!driver->viewportInFocus) return; - sLog.debug("Button", button, " state ", button_state); - - if (button == 272) { + if (button == BTN_LEFT) { if (button_state == WL_POINTER_BUTTON_STATE_PRESSED) { driver->viewportInFocus->leftClick = WallpaperEngine::Input::MouseClickStatus::Clicked; } else if (button_state == WL_POINTER_BUTTON_STATE_RELEASED) { driver->viewportInFocus->leftClick = WallpaperEngine::Input::MouseClickStatus::Released; } - } else if (button == 273) { + } else if (button == BTN_RIGHT) { if (button_state == WL_POINTER_BUTTON_STATE_PRESSED) { driver->viewportInFocus->rightClick = WallpaperEngine::Input::MouseClickStatus::Clicked; } else if (button_state == WL_POINTER_BUTTON_STATE_RELEASED) { diff --git a/src/WallpaperEngine/Render/Drivers/Output/CWaylandOutputViewport.h b/src/WallpaperEngine/Render/Drivers/Output/CWaylandOutputViewport.h index afb14c6..9c75bb7 100644 --- a/src/WallpaperEngine/Render/Drivers/Output/CWaylandOutputViewport.h +++ b/src/WallpaperEngine/Render/Drivers/Output/CWaylandOutputViewport.h @@ -45,8 +45,8 @@ class CWaylandOutputViewport final : public COutputViewport { zwlr_layer_surface_v1* layerSurface = nullptr; wl_callback* frameCallback = nullptr; glm::dvec2 mousePos = {0, 0}; - WallpaperEngine::Input::MouseClickStatus leftClick = WallpaperEngine::Input::MouseClickStatus::Waiting; - WallpaperEngine::Input::MouseClickStatus rightClick = WallpaperEngine::Input::MouseClickStatus::Waiting; + WallpaperEngine::Input::MouseClickStatus leftClick = WallpaperEngine::Input::MouseClickStatus::Released; + WallpaperEngine::Input::MouseClickStatus rightClick = WallpaperEngine::Input::MouseClickStatus::Released; wl_cursor* pointer = nullptr; wl_surface* cursorSurface = nullptr; bool callbackInitialized = false; diff --git a/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp b/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp index 4a342d6..f1790e2 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp +++ b/src/WallpaperEngine/Render/Wallpapers/CWeb.cpp @@ -93,13 +93,16 @@ void CWeb::updateMouse (glm::ivec4 viewport) { m_browser->GetHost ()->SendMouseMoveEvent (evt, false); // TODO: ANY OTHER MOUSE EVENTS TO SEND? - if (leftClick != WallpaperEngine::Input::MouseClickStatus::Waiting) { + if (leftClick != this->m_leftClick) { m_browser->GetHost ()->SendMouseClickEvent (evt, CefBrowserHost::MouseButtonType::MBT_LEFT, leftClick == WallpaperEngine::Input::MouseClickStatus::Released, 1); } - if (rightClick != WallpaperEngine::Input::MouseClickStatus::Waiting) { + if (rightClick != this->m_rightClick) { m_browser->GetHost ()->SendMouseClickEvent (evt, CefBrowserHost::MouseButtonType::MBT_RIGHT, rightClick == WallpaperEngine::Input::MouseClickStatus::Released, 1); } + + this->m_leftClick = leftClick; + this->m_rightClick = rightClick; } CWeb::~CWeb () { diff --git a/src/WallpaperEngine/Render/Wallpapers/CWeb.h b/src/WallpaperEngine/Render/Wallpapers/CWeb.h index 4fe0fa6..858beb1 100644 --- a/src/WallpaperEngine/Render/Wallpapers/CWeb.h +++ b/src/WallpaperEngine/Render/Wallpapers/CWeb.h @@ -109,6 +109,9 @@ namespace WallpaperEngine::Render int m_width; int m_height; + WallpaperEngine::Input::MouseClickStatus m_leftClick; + WallpaperEngine::Input::MouseClickStatus m_rightClick; + glm::vec2 m_mousePosition; glm::vec2 m_mousePositionLast; };