Platform/Unix/X11Mouse: adding a basic implementation to get things going.
This commit is contained in:
parent
cfe064f02c
commit
816cf3a072
2 changed files with 87 additions and 1 deletions
|
|
@ -1,15 +1,33 @@
|
||||||
|
|
||||||
|
#include <Spectre/System/Event.h>
|
||||||
|
#include <Spectre/System/Log.h>
|
||||||
#include "X11Mouse.h"
|
#include "X11Mouse.h"
|
||||||
|
|
||||||
|
#include "X11SharedDisplay.h"
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
|
X11Mouse::X11Mouse() :
|
||||||
|
m_disp(NULL),
|
||||||
|
m_btn_state(0)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
X11Mouse::~X11Mouse()
|
||||||
|
{
|
||||||
|
if (m_disp) {
|
||||||
|
XReleaseDisplay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void X11Mouse::init()
|
void X11Mouse::init()
|
||||||
{
|
{
|
||||||
|
m_disp = XGetDisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector2f X11Mouse::getPosition() const
|
Vector2f X11Mouse::getPosition() const
|
||||||
{
|
{
|
||||||
|
// TODO: Translate to window.
|
||||||
return m_position;
|
return m_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,12 +38,68 @@ Vector2f X11Mouse::getAbsPosition() const
|
||||||
|
|
||||||
bool X11Mouse::isButtonDown(Mouse::Button button) 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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void X11Mouse::update(InputModule *input)
|
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
|
} // namespace sp
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,16 @@
|
||||||
#define PLATFORM_UNIX_X11MOUSE_H
|
#define PLATFORM_UNIX_X11MOUSE_H
|
||||||
|
|
||||||
#include <Spectre/Input/Mouse.h>
|
#include <Spectre/Input/Mouse.h>
|
||||||
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
class X11Mouse : public Mouse
|
class X11Mouse : public Mouse
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
|
X11Mouse();
|
||||||
|
~X11Mouse();
|
||||||
|
|
||||||
virtual void init();
|
virtual void init();
|
||||||
|
|
||||||
// Get mouse position
|
// Get mouse position
|
||||||
|
|
@ -18,12 +22,20 @@ public :
|
||||||
|
|
||||||
virtual bool isButtonDown(Mouse::Button button) const;
|
virtual bool isButtonDown(Mouse::Button button) const;
|
||||||
|
|
||||||
|
// Translate a XEvent to sp::Event, Called from X11EventQueue
|
||||||
|
static bool handleMessage(XEvent* xevent, Event& event);
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
|
|
||||||
virtual void update(InputModule *input);
|
virtual void update(InputModule *input);
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
|
::Display* m_disp;
|
||||||
|
::Window m_win;
|
||||||
|
|
||||||
Vector2f m_position;
|
Vector2f m_position;
|
||||||
|
|
||||||
|
unsigned int m_btn_state;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue