--- /dev/null
+#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
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();
#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"
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;
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)