+#include "gamecontroller.h"
+#include "gamecontroller_platform.h"
#include <fcntl.h>
#include <linux/joystick.h>
+#include <msp/core/algorithm.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"
-#include "gamecontroller_platform.h"
using namespace std;
namespace Msp {
namespace Input {
+vector<string> GameController::Private::detected_controllers;
+
GameController::GameController(unsigned index):
- event_disp(0)
+ Device(GAME_CONTROLLER)
{
- 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";
+ vector<string> devices = FS::list_filtered(dev_input, "^js[0-9]+");
+ sort(devices);
+ for(const string &f: devices)
+ // TODO check permissions
+ Private::detected_controllers.push_back((dev_input/f).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)
unsigned count = len/sizeof(js_event);
for(unsigned i=0; i<count; ++i)
{
- unsigned type = events[i].type&0x7F;
+ unsigned etype = events[i].type&0x7F;
bool init = events[i].type&JS_EVENT_INIT;
- if(type==JS_EVENT_AXIS)
+ if(etype==JS_EVENT_AXIS)
set_axis_value(events[i].number, events[i].value/32768.0f, !init);
- else if(type==JS_EVENT_BUTTON)
+ else if(etype==JS_EVENT_BUTTON)
set_button_state(events[i].number, events[i].value, !init);
}
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);
}