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):
18 priv->dev = new JsDevice(format("/dev/input/js%d", index));
19 priv->dev->signal_data_available.connect(sigc::mem_fun(this, static_cast<void (GameController::*)()>(&GameController::tick)));
20 name = priv->dev->get_name();
24 GameController::~GameController()
30 void GameController::use_event_dispatcher(IO::EventDispatcher *ed)
33 event_disp->remove(*priv->dev);
36 event_disp->add(*priv->dev);
39 void GameController::tick()
45 if(!first && !IO::poll(*priv->dev, IO::P_INPUT, Time::zero))
49 unsigned len = priv->dev->read(reinterpret_cast<char *>(events), sizeof(events));
51 unsigned count = len/sizeof(js_event);
52 for(unsigned i=0; i<count; ++i)
54 unsigned type = events[i].type&0x7F;
55 bool init = events[i].type&JS_EVENT_INIT;
56 if(type==JS_EVENT_AXIS)
57 set_axis_value(events[i].number, events[i].value/32768.0f, !init);
58 else if(type==JS_EVENT_BUTTON)
59 set_button_state(events[i].number, events[i].value, !init);
62 if(len<sizeof(events))
68 void GameController::tick(const Time::TimeDelta &timeout)
70 if(IO::poll(*priv->dev, IO::P_INPUT, timeout))
75 JsDevice::JsDevice(const string &fn)
78 *handle = open(fn.c_str(), O_RDONLY);
80 throw system_error(format("open(%s)", fn));
81 set_events(IO::P_INPUT);
89 string JsDevice::get_name() const
91 char buf[128] = { 0 };
92 int ret = ioctl(*handle, JSIOCGNAME(sizeof(buf)), buf);
94 throw system_error("ioctl(JSIOCGNAME)");
98 unsigned JsDevice::do_read(char *buf, unsigned size)
100 return IO::sys_read(handle, buf, size);
103 unsigned JsDevice::do_write(const char *, unsigned)
105 throw IO::invalid_access(IO::M_WRITE);