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 priv->dev = new JsDevice(format("/dev/input/js%d", index));
18 name = priv->dev->get_name();
22 GameController::~GameController()
28 void GameController::tick()
33 unsigned len = priv->dev->read(reinterpret_cast<char *>(events), sizeof(events));
35 unsigned count = len/sizeof(js_event);
36 for(unsigned i=0; i<count; ++i)
38 unsigned type = events[i].type&0x7F;
39 bool init = events[i].type&JS_EVENT_INIT;
40 if(type==JS_EVENT_AXIS)
41 set_axis_value(events[i].number, events[i].value/32768.0f, !init);
42 else if(type==JS_EVENT_BUTTON)
43 set_button_state(events[i].number, events[i].value, !init);
46 if(len<sizeof(events))
52 JsDevice::JsDevice(const string &fn)
55 *handle = open(fn.c_str(), O_RDONLY|O_NONBLOCK);
57 throw system_error(format("open(%s)", fn));
60 string JsDevice::get_name() const
63 int ret = ioctl(*handle, JSIOCGNAME(sizeof(buf)), buf);
65 throw system_error("ioctl(JSIOCGNAME)");
69 unsigned JsDevice::do_read(char *buf, unsigned size)
71 return IO::sys_read(handle, buf, size);
74 unsigned JsDevice::do_write(const char *, unsigned)
76 throw IO::invalid_access(IO::M_WRITE);