From dd8a4a0583a8bea515d14f148c18e685c1d4e41a Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Fri, 16 Oct 2020 19:35:32 +0200 Subject: [PATCH] Platform/Unix/X11Mouse: adding a basic implementation to get things going. --- source/Platform/Unix/X11Mouse.cpp | 76 ++++++++++++++++++++++++++++++- source/Platform/Unix/X11Mouse.h | 12 +++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/source/Platform/Unix/X11Mouse.cpp b/source/Platform/Unix/X11Mouse.cpp index 48ef3bc..35bf945 100644 --- a/source/Platform/Unix/X11Mouse.cpp +++ b/source/Platform/Unix/X11Mouse.cpp @@ -1,15 +1,33 @@ +#include +#include #include "X11Mouse.h" +#include "X11SharedDisplay.h" + namespace sp { +X11Mouse::X11Mouse() : +m_disp(NULL), +m_btn_state(0) +{ +} + +X11Mouse::~X11Mouse() +{ + if (m_disp) { + XReleaseDisplay(); + } +} + void X11Mouse::init() { - + m_disp = XGetDisplay(); } Vector2f X11Mouse::getPosition() const { + // TODO: Translate to window. return m_position; } @@ -20,12 +38,68 @@ Vector2f X11Mouse::getAbsPosition() const bool X11Mouse::isButtonDown(Mouse::Button button) const { + // TODO: Button1 and 2 is defined in x11 and + // therefore clashes with Mouse::Button::Button1 and 2. + switch(button) { + case Mouse::Button::Left : + return m_btn_state & Button1Mask; + case Mouse::Button::Right : + return m_btn_state & Button3Mask; + case Mouse::Button::Middle : + return m_btn_state & Button2Mask; + default : + return false; + } return false; } void X11Mouse::update(InputModule *input) { + ::Window root, child; + int rx, ry, x = 0, y = 0; + // Query position and button state. + XQueryPointer(m_disp, ::XDefaultRootWindow(m_disp), + &root, &child, + &rx, &ry, + &x, &y, &m_btn_state); + + // Update position + m_position.x = x; + m_position.y = y; +} + +bool X11Mouse::handleMessage(XEvent* xevent, Event& event) +{ + if (xevent->type == MotionNotify) { + event.type = Event::MouseMove; + event.mouseMove.x = xevent->xmotion.x; + event.mouseMove.y = xevent->xmotion.y; + return true; + } + + if (xevent->type == ButtonPress || xevent->type == ButtonRelease) { + + Mouse::Button trans = Mouse::Button::Unknown; + + switch(xevent->xbutton.button) { + case Button1 : trans = Mouse::Button::Left; break; + case Button2 : trans = Mouse::Button::Middle; break; + case Button3 : trans = Mouse::Button::Right; break; + // TODO: name clash, need to rename Mouse::Button enums. + // case Button4 : trans = Mouse::Button::Button1; break; // clashes with X11's "Button1" define. + // case Button5 : trans = Mouse::Button::Button2; break; // clashes with X11's "Button2" define. + } + + if (trans != Mouse::Button::Unknown) { + event.type = Event::MouseButton; + event.mouseButton.pressed = xevent->type == ButtonPress; + event.mouseButton.button = trans; + return true; + } + } + + return false; } } // namespace sp diff --git a/source/Platform/Unix/X11Mouse.h b/source/Platform/Unix/X11Mouse.h index 55c044e..84c326c 100644 --- a/source/Platform/Unix/X11Mouse.h +++ b/source/Platform/Unix/X11Mouse.h @@ -3,12 +3,16 @@ #define PLATFORM_UNIX_X11MOUSE_H #include +#include namespace sp { class X11Mouse : public Mouse { public : + X11Mouse(); + ~X11Mouse(); + virtual void init(); // Get mouse position @@ -18,12 +22,20 @@ public : virtual bool isButtonDown(Mouse::Button button) const; + // Translate a XEvent to sp::Event, Called from X11EventQueue + static bool handleMessage(XEvent* xevent, Event& event); + protected : virtual void update(InputModule *input); protected : + ::Display* m_disp; + ::Window m_win; + Vector2f m_position; + + unsigned int m_btn_state; }; } // namespace sp