X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Flinux%2Fgamecontroller.cpp;h=38d9d2b222901ab6d6fd69462035655f7efb4c77;hb=06b42c8c1bd84c67c23a4245cbd5d3fc6f66f12b;hp=d5fed3baba92783c7e7e347bf0fd2fced4723100;hpb=c430f2d7498e9c2daa515ee9aba3a95257e5e999;p=libs%2Fgui.git diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp index d5fed3b..38d9d2b 100644 --- a/source/input/linux/gamecontroller.cpp +++ b/source/input/linux/gamecontroller.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include "gamecontroller.h" @@ -11,12 +12,23 @@ using namespace std; namespace Msp { namespace Input { +vector GameController::Private::detected_controllers; + GameController::GameController(unsigned index): - priv(new Private) + event_disp(0) { - priv->dev = new JsDevice(format("/dev/input/js%d", index)); + if(!detect_done) + detect(); + if(index>=Private::detected_controllers.size()) + throw device_not_available(format("GameController(%d)", index)); + + JsDevice *device = new JsDevice(Private::detected_controllers[index]); + + priv = new Private; + priv->dev = device; + 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 +37,42 @@ GameController::~GameController() delete priv; } +unsigned GameController::detect() +{ + Private::detected_controllers.clear(); + + FS::Path dev_input = "/dev/input"; + list devices = FS::list_filtered(dev_input, "^js[0-9]+"); + devices.sort(); + for(list::const_iterator i=devices.begin(); i!=devices.end(); ++i) + // TODO check permissions + Private::detected_controllers.push_back((dev_input / *i).str()); + + detect_done = true; + n_detected_controllers = Private::detected_controllers.size(); + + return Private::detected_controllers.size(); +} + +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 +92,25 @@ 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() +{ + sys_close(handle); } string JsDevice::get_name() const