#ifndef GAMECONTROLLER_H_
#define GAMECONTROLLER_H_
+#include <msp/io/eventdispatcher.h>
+#include <msp/time/timedelta.h>
#include "device.h"
namespace Msp {
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
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()
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);
}
+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()