1
0
Fork 0

Platform/Unix/X11Mouse: use X11Display::getFocused() to get the coordinates relative to the focused window.

This commit is contained in:
Henrik Hautakoski 2020-12-20 16:09:16 +01:00
parent 227d58725d
commit f71cfa86f2
2 changed files with 27 additions and 7 deletions

View file

@ -1,6 +1,7 @@
#include <Spectre/System/Event.h> #include <Spectre/System/Event.h>
#include <Spectre/System/Log.h> #include <Spectre/System/Log.h>
#include "X11Display.h"
#include "X11Mouse.h" #include "X11Mouse.h"
#include "X11SharedDisplay.h" #include "X11SharedDisplay.h"
@ -23,17 +24,17 @@ X11Mouse::~X11Mouse()
void X11Mouse::init() void X11Mouse::init()
{ {
m_disp = XGetDisplay(); m_disp = XGetDisplay();
updateFocusedWindow();
} }
Vector2f X11Mouse::getPosition() const Vector2f X11Mouse::getPosition() const
{ {
// TODO: Translate to window.
return m_position; return m_position;
} }
Vector2f X11Mouse::getAbsPosition() const Vector2f X11Mouse::getAbsPosition() const
{ {
return m_position; return m_abs_position;
} }
bool X11Mouse::isButtonDown(Mouse::Button button) const bool X11Mouse::isButtonDown(Mouse::Button button) const
@ -58,15 +59,30 @@ void X11Mouse::update(InputModule *input)
::Window root, child; ::Window root, child;
int rx, ry, x = 0, y = 0; int rx, ry, x = 0, y = 0;
updateFocusedWindow();
// Query position and button state. // Query position and button state.
XQueryPointer(m_disp, ::XDefaultRootWindow(m_disp), XQueryPointer(m_disp,
m_win ? m_win : ::XDefaultRootWindow(m_disp),
&root, &child, &root, &child,
&rx, &ry, &rx, &ry,
&x, &y, &m_btn_state); &x, &y, &m_btn_state);
// Update position // Update abs position (relative to root).
m_position.x = x; m_abs_position.x = rx;
m_position.y = y; m_abs_position.y = ry;
// Update window position.
if (m_win) {
m_position.x = x;
m_position.y = y;
}
}
void X11Mouse::updateFocusedWindow()
{
X11Display *focus = X11Display::getFocused();
m_win = focus ? (::Window) focus->getHandle() : 0;
} }
bool X11Mouse::handleMessage(XEvent* xevent, Event& event) bool X11Mouse::handleMessage(XEvent* xevent, Event& event)

View file

@ -29,12 +29,16 @@ protected :
virtual void update(InputModule *input); virtual void update(InputModule *input);
void updateFocusedWindow();
protected : protected :
::Display* m_disp; ::Display* m_disp;
::Window m_win; ::Window m_win; // Focused window.
Vector2f m_position; Vector2f m_position;
Vector2f m_abs_position;
unsigned int m_btn_state; unsigned int m_btn_state;
}; };