From: Mikko Rasa Date: Thu, 21 Jul 2016 00:01:03 +0000 (+0300) Subject: Add support for blocking ticks and EventDispatcher in GameController X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=97ea3f88027c577811bcd9717ad506f254d393f5;p=libs%2Fgui.git Add support for blocking ticks and EventDispatcher in GameController Note: the parameterless tick() method was previously non-blocking but now blocks until an event is received. --- diff --git a/source/input/gamecontroller.h b/source/input/gamecontroller.h index 9ebacee..5aaa631 100644 --- a/source/input/gamecontroller.h +++ b/source/input/gamecontroller.h @@ -1,6 +1,8 @@ #ifndef GAMECONTROLLER_H_ #define GAMECONTROLLER_H_ +#include +#include #include "device.h" namespace Msp { @@ -12,12 +14,16 @@ private: struct Private; Private *priv; + IO::EventDispatcher *event_disp; public: GameController(unsigned); virtual ~GameController(); + void use_event_dispatcher(IO::EventDispatcher *); + void tick(); + void tick(const Time::TimeDelta &); }; } // namespace Input diff --git a/source/input/generic/gamecontroller.cpp b/source/input/generic/gamecontroller.cpp index 9ba7130..419c678 100644 --- a/source/input/generic/gamecontroller.cpp +++ b/source/input/generic/gamecontroller.cpp @@ -15,9 +15,17 @@ GameController::~GameController() { } +void GameController::use_event_dispatcher(IO::EventDispatcher *) +{ +} + void GameController::tick() { } +void GameController::tick(const Time::TimeDelta &) +{ +} + } // namespace Input } // namespace Msp diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp index 7fc59ed..8a4e8d4 100644 --- a/source/input/linux/gamecontroller.cpp +++ b/source/input/linux/gamecontroller.cpp @@ -12,11 +12,13 @@ namespace Msp { namespace Input { GameController::GameController(unsigned index): - priv(new Private) + priv(new Private), + event_disp(0) { priv->dev = new JsDevice(format("/dev/input/js%d", index)); + priv->dev->signal_data_available.connect(sigc::mem_fun(this, static_cast(&GameController::tick))); name = priv->dev->get_name(); - tick(); + tick(Time::zero); } GameController::~GameController() @@ -25,11 +27,25 @@ GameController::~GameController() delete priv; } +void GameController::use_event_dispatcher(IO::EventDispatcher *ed) +{ + if(event_disp) + event_disp->remove(*priv->dev); + event_disp = ed; + if(event_disp) + event_disp->add(*priv->dev); +} + void GameController::tick() { js_event events[16]; + bool first = true; while(1) { + if(!first && !IO::poll(*priv->dev, IO::P_INPUT, Time::zero)) + break; + + first = false; unsigned len = priv->dev->read(reinterpret_cast(events), sizeof(events)); unsigned count = len/sizeof(js_event); @@ -49,12 +65,20 @@ void GameController::tick() } +void GameController::tick(const Time::TimeDelta &timeout) +{ + if(IO::poll(*priv->dev, IO::P_INPUT, timeout)) + tick(); +} + + JsDevice::JsDevice(const string &fn) { mode = IO::M_READ; - *handle = open(fn.c_str(), O_RDONLY|O_NONBLOCK); + *handle = open(fn.c_str(), O_RDONLY); if(!handle) throw system_error(format("open(%s)", fn)); + set_events(IO::P_INPUT); } JsDevice::~JsDevice()