X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Flinux%2Fgamecontroller.cpp;h=f0139e96baeb4b04f150bf463c5fa7f84701a83f;hb=8d0f0839d1fc5195fce1b77c36e7f3b5b114e696;hp=2fc624e7b83db87e677c0c2bbe5fdda0fb0ccd53;hpb=47e65794986e2b926448b3c7f99628eec40b6bb8;p=libs%2Fgui.git diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp index 2fc624e..f0139e9 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,10 +13,17 @@ using namespace std; namespace Msp { namespace Input { +vector GameController::Private::detected_controllers; + GameController::GameController(unsigned index): event_disp(0) { - JsDevice *device = 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; @@ -29,6 +38,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) @@ -97,12 +123,12 @@ string JsDevice::get_name() const return buf; } -unsigned JsDevice::do_read(char *buf, unsigned size) +size_t JsDevice::do_read(char *buf, size_t size) { return IO::sys_read(handle, buf, size); } -unsigned JsDevice::do_write(const char *, unsigned) +size_t JsDevice::do_write(const char *, size_t) { throw IO::invalid_access(IO::M_WRITE); }