]> git.tdb.fi Git - libs/gui.git/commitdiff
Enumerate available game controllers
authorMikko Rasa <tdb@tdb.fi>
Mon, 19 Sep 2016 20:39:14 +0000 (23:39 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 20 Sep 2016 08:13:54 +0000 (11:13 +0300)
Assign indices from zero to N-1 internally.  This makes it easier for
applications to use game controllers as the first available controller
will always have index zero.

source/input/gamecontroller.cpp [new file with mode: 0644]
source/input/gamecontroller.h
source/input/generic/gamecontroller.cpp
source/input/linux/gamecontroller.cpp
source/input/linux/gamecontroller_platform.h

diff --git a/source/input/gamecontroller.cpp b/source/input/gamecontroller.cpp
new file mode 100644 (file)
index 0000000..1ad8757
--- /dev/null
@@ -0,0 +1,18 @@
+#include "gamecontroller.h"
+
+namespace Msp {
+namespace Input {
+
+bool GameController::detect_done = false;
+unsigned GameController::n_detected_controllers = 0;
+
+bool GameController::is_available(unsigned index)
+{
+       if(!detect_done)
+               detect();
+
+       return index<n_detected_controllers;
+}
+
+} // namespace Input
+} // namespace Msp
index 84c50b51c377cd7105947f8109cdfb25d982b155..863f66f3e5a338bea6d8cdb2acdcb3968f7f43b3 100644 (file)
@@ -16,10 +16,16 @@ private:
        Private *priv;
        IO::EventDispatcher *event_disp;
 
+       static bool detect_done;
+       static unsigned n_detected_controllers;
+
 public:
        GameController(unsigned);
        virtual ~GameController();
 
+       static unsigned detect();
+       static bool is_available(unsigned = 0);
+
        void use_event_dispatcher(IO::EventDispatcher *);
 
        void tick();
index 419c6781dd732e490fd48ea1f94073d94f214386..9ab6738ab24ebc7b4a68de226bada7029a91a5a8 100644 (file)
@@ -15,6 +15,12 @@ GameController::~GameController()
 {
 }
 
+unsigned GameController::detect()
+{
+       detect_done = true;
+       return 0;
+}
+
 void GameController::use_event_dispatcher(IO::EventDispatcher *)
 {
 }
index 2fc624e7b83db87e677c0c2bbe5fdda0fb0ccd53..c00379d4c70e9e595c0c1257a7ac92881f574861 100644 (file)
@@ -1,6 +1,7 @@
 #include <fcntl.h>
 #include <linux/joystick.h>
 #include <msp/core/systemerror.h>
+#include <msp/fs/dir.h>
 #include <msp/io/handle_private.h>
 #include <msp/strings/format.h>
 #include "gamecontroller.h"
@@ -11,10 +12,17 @@ using namespace std;
 namespace Msp {
 namespace Input {
 
+vector<string> 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 +37,22 @@ GameController::~GameController()
        delete priv;
 }
 
+unsigned GameController::detect()
+{
+       Private::detected_controllers.clear();
+
+       FS::Path dev_input = "/dev/input";
+       list<string> devices = FS::list_filtered(dev_input, "^js[0-9]+");
+       for(list<string>::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)
index cd6383984f73d0b03d2647bb556e7a28d04e30e2..2951076806323b63a64d3a06863d93af10c637ed 100644 (file)
@@ -30,6 +30,8 @@ public:
 struct GameController::Private
 {
        JsDevice *dev;
+
+       static std::vector<std::string> detected_controllers;
 };
 
 } // namespace Input