X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Flinux%2Fgamecontroller.cpp;h=d0029e8fe3c4bbad1249bb4fdcea6b1baca1d7b6;hb=75a3f0fd4152a71aefec72cb053846646071af03;hp=8a4e8d43c2dbb3f06c9af66dd347dad721281f46;hpb=97ea3f88027c577811bcd9717ad506f254d393f5;p=libs%2Fgui.git diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp index 8a4e8d4..d0029e8 100644 --- a/source/input/linux/gamecontroller.cpp +++ b/source/input/linux/gamecontroller.cpp @@ -1,6 +1,8 @@ #include #include +#include #include +#include #include #include #include "gamecontroller.h" @@ -11,11 +13,21 @@ using namespace std; namespace Msp { namespace Input { +vector GameController::Private::detected_controllers; + GameController::GameController(unsigned index): - priv(new Private), + Device(GAME_CONTROLLER), 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(Time::zero); @@ -27,6 +39,23 @@ GameController::~GameController() delete priv; } +unsigned GameController::detect() +{ + Private::detected_controllers.clear(); + + FS::Path dev_input = "/dev/input"; + vector devices = FS::list_filtered(dev_input, "^js[0-9]+"); + sort(devices); + for(vector::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) @@ -51,11 +80,11 @@ void GameController::tick() unsigned count = len/sizeof(js_event); for(unsigned i=0; i