diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a6f12d..260532a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,7 +75,7 @@ if (ENABLE_WAYLAND) add_compile_definitions(ENABLE_WAYLAND) set(WAYLAND_SOURCES "src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.h" "src/WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.cpp" "src/WallpaperEngine/Render/Drivers/Detectors/CWaylandFullScreenDetector.cpp" "src/WallpaperEngine/Render/Drivers/Detectors/CWaylandFullScreenDetector.h" "src/WallpaperEngine/Render/Drivers/Output/CWaylandOutput.cpp" "src/WallpaperEngine/Render/Drivers/Output/CWaylandOutput.h" - "xdg-shell-protocol.c" "wlr-layer-shell-unstable-v1-protocol.c") + "src/WallpaperEngine/Input/CWaylandMouseInput.cpp" "src/WallpaperEngine/Input/CWaylandMouseInput.h" "xdg-shell-protocol.c" "wlr-layer-shell-unstable-v1-protocol.c") else() set(WAYLAND_SOURCES "") endif() @@ -142,6 +142,8 @@ add_executable( src/WallpaperEngine/Input/CInputContext.h src/WallpaperEngine/Input/CMouseInput.cpp src/WallpaperEngine/Input/CMouseInput.h + src/WallpaperEngine/Input/CGLFWMouseInput.cpp + src/WallpaperEngine/Input/CGLFWMouseInput.h src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.h src/WallpaperEngine/Render/Shaders/Variables/CShaderVariable.cpp diff --git a/src/WallpaperEngine/Input/CGLFWMouseInput.cpp b/src/WallpaperEngine/Input/CGLFWMouseInput.cpp new file mode 100644 index 0000000..83b62cf --- /dev/null +++ b/src/WallpaperEngine/Input/CGLFWMouseInput.cpp @@ -0,0 +1,21 @@ +#include +#include "CGLFWMouseInput.h" + +using namespace WallpaperEngine::Input; + +CGLFWMouseInput::CGLFWMouseInput (GLFWwindow* window) : m_reportedPosition (), m_mousePosition (), m_window (window) {} + +void CGLFWMouseInput::update () +{ + if (!m_window) + return; + + // update current mouse position + glfwGetCursorPos (this->m_window, &this->m_mousePosition.x, &this->m_mousePosition.y); + // interpolate to the new position + this->m_reportedPosition = glm::mix (this->m_reportedPosition, this->m_mousePosition, 1.0); +} + +glm::dvec2 CGLFWMouseInput::position() const { + return this->m_reportedPosition; +} \ No newline at end of file diff --git a/src/WallpaperEngine/Input/CGLFWMouseInput.h b/src/WallpaperEngine/Input/CGLFWMouseInput.h new file mode 100644 index 0000000..90cb46b --- /dev/null +++ b/src/WallpaperEngine/Input/CGLFWMouseInput.h @@ -0,0 +1,41 @@ +#pragma once + +#include "CMouseInput.h" + +#include +#include "GLFW/glfw3.h" + +namespace WallpaperEngine::Input +{ + /** + * Handles mouse input for the background + */ + class CGLFWMouseInput : public CMouseInput + { + public: + explicit CGLFWMouseInput(GLFWwindow* window); + + /** + * Takes current mouse position and updates it + */ + void update () override; + + /** + * The virtual pointer's position + */ + glm::dvec2 position () const override; + + private: + /** + * The GLFW window to get mouse position from + */ + GLFWwindow* m_window = nullptr; + + /** + * The current mouse position + */ + glm::dvec2 m_mousePosition; + glm::dvec2 m_reportedPosition; + }; +} + diff --git a/src/WallpaperEngine/Input/CInputContext.cpp b/src/WallpaperEngine/Input/CInputContext.cpp index e495357..22509d5 100644 --- a/src/WallpaperEngine/Input/CInputContext.cpp +++ b/src/WallpaperEngine/Input/CInputContext.cpp @@ -1,28 +1,32 @@ #include "CInputContext.h" #include "WallpaperEngine/Render/Drivers/CX11OpenGLDriver.h" +#include "WallpaperEngine/Input/CGLFWMouseInput.h" +#ifdef ENABLE_WAYLAND #include "WallpaperEngine/Render/Drivers/CWaylandOpenGLDriver.h" +#include "WallpaperEngine/Input/CWaylandMouseInput.h" +#endif using namespace WallpaperEngine::Input; using namespace WallpaperEngine::Render::Drivers; -CInputContext::CInputContext (CX11OpenGLDriver& videoDriver) : - m_mouse (videoDriver.getWindow ()) +CInputContext::CInputContext (CX11OpenGLDriver& videoDriver) { + m_mouse = std::make_unique(videoDriver.getWindow()); } #ifdef ENABLE_WAYLAND -CInputContext::CInputContext (CWaylandOpenGLDriver& videoDriver) : - m_mouse (&videoDriver) +CInputContext::CInputContext (CWaylandOpenGLDriver& videoDriver) { + m_mouse = std::make_unique(&videoDriver); } #endif void CInputContext::update () { - this->m_mouse.update (); + this->m_mouse->update (); } const CMouseInput& CInputContext::getMouseInput () const { - return this->m_mouse; + return *this->m_mouse.get(); } \ No newline at end of file diff --git a/src/WallpaperEngine/Input/CInputContext.h b/src/WallpaperEngine/Input/CInputContext.h index 4a74e03..3d51423 100644 --- a/src/WallpaperEngine/Input/CInputContext.h +++ b/src/WallpaperEngine/Input/CInputContext.h @@ -25,6 +25,6 @@ namespace WallpaperEngine::Input [[nodiscard]] const CMouseInput& getMouseInput () const; private: - CMouseInput m_mouse; + std::unique_ptr m_mouse; }; } diff --git a/src/WallpaperEngine/Input/CMouseInput.cpp b/src/WallpaperEngine/Input/CMouseInput.cpp index 4ac21a7..2998032 100644 --- a/src/WallpaperEngine/Input/CMouseInput.cpp +++ b/src/WallpaperEngine/Input/CMouseInput.cpp @@ -1,29 +1,4 @@ #include #include "CMouseInput.h" -using namespace WallpaperEngine::Input; - -CMouseInput::CMouseInput (GLFWwindow* window) : position (), m_mousePosition (), m_window (window) {} -#ifdef ENABLE_WAYLAND -CMouseInput::CMouseInput(WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver* driver) { - waylandDriver = driver; -} -#endif - -void CMouseInput::update () -{ - if (!m_window) { -#ifdef ENABLE_WAYLAND - if (!waylandDriver || !waylandDriver->lastLSInFocus) - return; - - this->position = waylandDriver->lastLSInFocus->mousePos; -#endif - return; - } - - // update current mouse position - glfwGetCursorPos (this->m_window, &this->m_mousePosition.x, &this->m_mousePosition.y); - // interpolate to the new position - this->position = glm::mix (this->position, this->m_mousePosition, 1.0); -} \ No newline at end of file +using namespace WallpaperEngine::Input; \ No newline at end of file diff --git a/src/WallpaperEngine/Input/CMouseInput.h b/src/WallpaperEngine/Input/CMouseInput.h index 3281116..2b07d4b 100644 --- a/src/WallpaperEngine/Input/CMouseInput.h +++ b/src/WallpaperEngine/Input/CMouseInput.h @@ -16,38 +16,15 @@ namespace WallpaperEngine::Input class CMouseInput { public: - explicit CMouseInput(GLFWwindow* window); -#ifdef ENABLE_WAYLAND - explicit CMouseInput(WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver* driver); -#endif - /** * Takes current mouse position and updates it */ - void update (); + virtual void update () = 0; /** * The virtual pointer's position */ - glm::dvec2 position; - - private: - /** - * The GLFW window to get mouse position from - */ - GLFWwindow* m_window = nullptr; - - /** - * The current mouse position - */ - glm::dvec2 m_mousePosition; - -#ifdef ENABLE_WAYLAND - /** - * Wayland: Driver - */ - WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver* waylandDriver = nullptr; -#endif + virtual glm::dvec2 position () const = 0; }; } diff --git a/src/WallpaperEngine/Input/CWaylandMouseInput.cpp b/src/WallpaperEngine/Input/CWaylandMouseInput.cpp new file mode 100644 index 0000000..85a520c --- /dev/null +++ b/src/WallpaperEngine/Input/CWaylandMouseInput.cpp @@ -0,0 +1,20 @@ +#include +#include "CWaylandMouseInput.h" + +using namespace WallpaperEngine::Input; + +CWaylandMouseInput::CWaylandMouseInput(WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver* driver) { + waylandDriver = driver; +} + +void CWaylandMouseInput::update () +{ + ; +} + +glm::dvec2 CWaylandMouseInput::position() const { + if (!waylandDriver || !waylandDriver->lastLSInFocus) + return {0, 0}; + + return waylandDriver->lastLSInFocus->mousePos; +} \ No newline at end of file diff --git a/src/WallpaperEngine/Input/CWaylandMouseInput.h b/src/WallpaperEngine/Input/CWaylandMouseInput.h new file mode 100644 index 0000000..50ace5a --- /dev/null +++ b/src/WallpaperEngine/Input/CWaylandMouseInput.h @@ -0,0 +1,39 @@ +#pragma once + +#include "CMouseInput.h" +#include "../Render/Drivers/CWaylandOpenGLDriver.h" + +#include +#include "GLFW/glfw3.h" + + +namespace WallpaperEngine::Input +{ + /** + * Handles mouse input for the background + */ + class CWaylandMouseInput : public CMouseInput + { + public: + explicit CWaylandMouseInput(WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver* driver); + + /** + * Takes current mouse position and updates it + */ + void update () override; + + /** + * The virtual pointer's position + */ + glm::dvec2 position () const override; + + private: + /** + * Wayland: Driver + */ + WallpaperEngine::Render::Drivers::CWaylandOpenGLDriver* waylandDriver = nullptr; + + glm::dvec2 pos; + }; +} + diff --git a/src/WallpaperEngine/Render/CScene.cpp b/src/WallpaperEngine/Render/CScene.cpp index c345ede..3f8f80c 100644 --- a/src/WallpaperEngine/Render/CScene.cpp +++ b/src/WallpaperEngine/Render/CScene.cpp @@ -248,7 +248,7 @@ void CScene::renderFrame (glm::ivec4 viewport) void CScene::updateMouse (glm::ivec4 viewport) { // update virtual mouse position first - glm::dvec2 position = this->getContext ().getInputContext ().getMouseInput ().position; + glm::dvec2 position = this->getContext ().getInputContext ().getMouseInput ().position(); // TODO: PROPERLY TRANSLATE THESE TO WHAT'S VISIBLE ON SCREEN (FOR BACKGROUNDS THAT DO NOT EXACTLY FIT ON SCREEN) // rollover the position to the last