]> git.tdb.fi Git - libs/gui.git/commitdiff
Add support for blocking ticks and EventDispatcher in GameController
authorMikko Rasa <tdb@tdb.fi>
Thu, 21 Jul 2016 00:01:03 +0000 (03:01 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 21 Jul 2016 00:35:16 +0000 (03:35 +0300)
Note: the parameterless tick() method was previously non-blocking but now
blocks until an event is received.

source/input/gamecontroller.h
source/input/generic/gamecontroller.cpp
source/input/linux/gamecontroller.cpp

index 9ebacee7becbc495c28b27b30e8118804657e698..5aaa6317322e0da37325359677d42147e331f4f8 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef GAMECONTROLLER_H_
 #define GAMECONTROLLER_H_
 
+#include <msp/io/eventdispatcher.h>
+#include <msp/time/timedelta.h>
 #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
index 9ba7130eb925097a7003711d364358ae3c44bf53..419c6781dd732e490fd48ea1f94073d94f214386 100644 (file)
@@ -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
index 7fc59ed6669d0f99f11758f352ac1a478550b570..8a4e8d43c2dbb3f06c9af66dd347dad721281f46 100644 (file)
@@ -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<void (GameController::*)()>(&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<char *>(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()