]> git.tdb.fi Git - libs/gui.git/blob - source/input/linux/gamecontroller.cpp
Add support for blocking ticks and EventDispatcher in GameController
[libs/gui.git] / source / input / linux / gamecontroller.cpp
1 #include <fcntl.h>
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"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace Input {
13
14 GameController::GameController(unsigned index):
15         priv(new Private),
16         event_disp(0)
17 {
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();
21         tick(Time::zero);
22 }
23
24 GameController::~GameController()
25 {
26         delete priv->dev;
27         delete priv;
28 }
29
30 void GameController::use_event_dispatcher(IO::EventDispatcher *ed)
31 {
32         if(event_disp)
33                 event_disp->remove(*priv->dev);
34         event_disp = ed;
35         if(event_disp)
36                 event_disp->add(*priv->dev);
37 }
38
39 void GameController::tick()
40 {
41         js_event events[16];
42         bool first = true;
43         while(1)
44         {
45                 if(!first && !IO::poll(*priv->dev, IO::P_INPUT, Time::zero))
46                         break;
47
48                 first = false;
49                 unsigned len = priv->dev->read(reinterpret_cast<char *>(events), sizeof(events));
50
51                 unsigned count = len/sizeof(js_event);
52                 for(unsigned i=0; i<count; ++i)
53                 {
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);
60                 }
61
62                 if(len<sizeof(events))
63                         break;
64         }
65 }
66
67
68 void GameController::tick(const Time::TimeDelta &timeout)
69 {
70         if(IO::poll(*priv->dev, IO::P_INPUT, timeout))
71                 tick();
72 }
73
74
75 JsDevice::JsDevice(const string &fn)
76 {
77         mode = IO::M_READ;
78         *handle = open(fn.c_str(), O_RDONLY);
79         if(!handle)
80                 throw system_error(format("open(%s)", fn));
81         set_events(IO::P_INPUT);
82 }
83
84 JsDevice::~JsDevice()
85 {
86         sys_close(handle);
87 }
88
89 string JsDevice::get_name() const
90 {
91         char buf[128] = { 0 };
92         int ret = ioctl(*handle, JSIOCGNAME(sizeof(buf)), buf);
93         if(ret<0)
94                 throw system_error("ioctl(JSIOCGNAME)");
95         return buf;
96 }
97
98 unsigned JsDevice::do_read(char *buf, unsigned size)
99 {
100         return IO::sys_read(handle, buf, size);
101 }
102
103 unsigned JsDevice::do_write(const char *, unsigned)
104 {
105         throw IO::invalid_access(IO::M_WRITE);
106 }
107
108 } // namespace Input
109 } // namespace Msp