2 #include <linux/joystick.h>
3 #include <msp/core/systemerror.h>
4 #include <msp/io/handle_private.h>
5 #include <msp/strings/format.h>
6 #include "gamecontroller.h"
7 #include "gamecontroller_platform.h"
14 GameController::GameController(unsigned index):
17 JsDevice *device = new JsDevice(format("/dev/input/js%d", index));
21 priv->dev->signal_data_available.connect(sigc::mem_fun(this, static_cast<void (GameController::*)()>(&GameController::tick)));
22 name = priv->dev->get_name();
26 GameController::~GameController()
32 void GameController::use_event_dispatcher(IO::EventDispatcher *ed)
35 event_disp->remove(*priv->dev);
38 event_disp->add(*priv->dev);
41 void GameController::tick()
47 if(!first && !IO::poll(*priv->dev, IO::P_INPUT, Time::zero))
51 unsigned len = priv->dev->read(reinterpret_cast<char *>(events), sizeof(events));
53 unsigned count = len/sizeof(js_event);
54 for(unsigned i=0; i<count; ++i)
56 unsigned type = events[i].type&0x7F;
57 bool init = events[i].type&JS_EVENT_INIT;
58 if(type==JS_EVENT_AXIS)
59 set_axis_value(events[i].number, events[i].value/32768.0f, !init);
60 else if(type==JS_EVENT_BUTTON)
61 set_button_state(events[i].number, events[i].value, !init);
64 if(len<sizeof(events))
70 void GameController::tick(const Time::TimeDelta &timeout)
72 if(IO::poll(*priv->dev, IO::P_INPUT, timeout))
77 JsDevice::JsDevice(const string &fn)
80 *handle = open(fn.c_str(), O_RDONLY);
82 throw system_error(format("open(%s)", fn));
83 set_events(IO::P_INPUT);
91 string JsDevice::get_name() const
93 char buf[128] = { 0 };
94 int ret = ioctl(*handle, JSIOCGNAME(sizeof(buf)), buf);
96 throw system_error("ioctl(JSIOCGNAME)");
100 unsigned JsDevice::do_read(char *buf, unsigned size)
102 return IO::sys_read(handle, buf, size);
105 unsigned JsDevice::do_write(const char *, unsigned)
107 throw IO::invalid_access(IO::M_WRITE);