]> git.tdb.fi Git - libs/gui.git/blob - source/input/linux/gamecontroller.cpp
Enumerate available game controllers
[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/fs/dir.h>
5 #include <msp/io/handle_private.h>
6 #include <msp/strings/format.h>
7 #include "gamecontroller.h"
8 #include "gamecontroller_platform.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace Input {
14
15 vector<string> GameController::Private::detected_controllers;
16
17 GameController::GameController(unsigned index):
18         event_disp(0)
19 {
20         if(!detect_done)
21                 detect();
22         if(index>=Private::detected_controllers.size())
23                 throw device_not_available(format("GameController(%d)", index));
24
25         JsDevice *device = new JsDevice(Private::detected_controllers[index]);
26
27         priv = new Private;
28         priv->dev = device;
29         priv->dev->signal_data_available.connect(sigc::mem_fun(this, static_cast<void (GameController::*)()>(&GameController::tick)));
30         name = priv->dev->get_name();
31         tick(Time::zero);
32 }
33
34 GameController::~GameController()
35 {
36         delete priv->dev;
37         delete priv;
38 }
39
40 unsigned GameController::detect()
41 {
42         Private::detected_controllers.clear();
43
44         FS::Path dev_input = "/dev/input";
45         list<string> devices = FS::list_filtered(dev_input, "^js[0-9]+");
46         for(list<string>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
47                 // TODO check permissions
48                 Private::detected_controllers.push_back((dev_input / *i).str());
49
50         detect_done = true;
51         n_detected_controllers = Private::detected_controllers.size();
52
53         return Private::detected_controllers.size();
54 }
55
56 void GameController::use_event_dispatcher(IO::EventDispatcher *ed)
57 {
58         if(event_disp)
59                 event_disp->remove(*priv->dev);
60         event_disp = ed;
61         if(event_disp)
62                 event_disp->add(*priv->dev);
63 }
64
65 void GameController::tick()
66 {
67         js_event events[16];
68         bool first = true;
69         while(1)
70         {
71                 if(!first && !IO::poll(*priv->dev, IO::P_INPUT, Time::zero))
72                         break;
73
74                 first = false;
75                 unsigned len = priv->dev->read(reinterpret_cast<char *>(events), sizeof(events));
76
77                 unsigned count = len/sizeof(js_event);
78                 for(unsigned i=0; i<count; ++i)
79                 {
80                         unsigned type = events[i].type&0x7F;
81                         bool init = events[i].type&JS_EVENT_INIT;
82                         if(type==JS_EVENT_AXIS)
83                                 set_axis_value(events[i].number, events[i].value/32768.0f, !init);
84                         else if(type==JS_EVENT_BUTTON)
85                                 set_button_state(events[i].number, events[i].value, !init);
86                 }
87
88                 if(len<sizeof(events))
89                         break;
90         }
91 }
92
93
94 void GameController::tick(const Time::TimeDelta &timeout)
95 {
96         if(IO::poll(*priv->dev, IO::P_INPUT, timeout))
97                 tick();
98 }
99
100
101 JsDevice::JsDevice(const string &fn)
102 {
103         mode = IO::M_READ;
104         *handle = open(fn.c_str(), O_RDONLY);
105         if(!handle)
106                 throw system_error(format("open(%s)", fn));
107         set_events(IO::P_INPUT);
108 }
109
110 JsDevice::~JsDevice()
111 {
112         sys_close(handle);
113 }
114
115 string JsDevice::get_name() const
116 {
117         char buf[128] = { 0 };
118         int ret = ioctl(*handle, JSIOCGNAME(sizeof(buf)), buf);
119         if(ret<0)
120                 throw system_error("ioctl(JSIOCGNAME)");
121         return buf;
122 }
123
124 unsigned JsDevice::do_read(char *buf, unsigned size)
125 {
126         return IO::sys_read(handle, buf, size);
127 }
128
129 unsigned JsDevice::do_write(const char *, unsigned)
130 {
131         throw IO::invalid_access(IO::M_WRITE);
132 }
133
134 } // namespace Input
135 } // namespace Msp