]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/linux/gamecontroller.cpp
Add a type enumeration for devices
[libs/gui.git] / source / input / linux / gamecontroller.cpp
index c00379d4c70e9e595c0c1257a7ac92881f574861..d0029e8fe3c4bbad1249bb4fdcea6b1baca1d7b6 100644 (file)
@@ -1,5 +1,6 @@
 #include <fcntl.h>
 #include <linux/joystick.h>
+#include <msp/core/algorithm.h>
 #include <msp/core/systemerror.h>
 #include <msp/fs/dir.h>
 #include <msp/io/handle_private.h>
@@ -15,6 +16,7 @@ namespace Input {
 vector<string> GameController::Private::detected_controllers;
 
 GameController::GameController(unsigned index):
+       Device(GAME_CONTROLLER),
        event_disp(0)
 {
        if(!detect_done)
@@ -42,8 +44,9 @@ unsigned GameController::detect()
        Private::detected_controllers.clear();
 
        FS::Path dev_input = "/dev/input";
-       list<string> devices = FS::list_filtered(dev_input, "^js[0-9]+");
-       for(list<string>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
+       vector<string> devices = FS::list_filtered(dev_input, "^js[0-9]+");
+       sort(devices);
+       for(vector<string>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
                // TODO check permissions
                Private::detected_controllers.push_back((dev_input / *i).str());
 
@@ -77,11 +80,11 @@ void GameController::tick()
                unsigned count = len/sizeof(js_event);
                for(unsigned i=0; i<count; ++i)
                {
-                       unsigned type = events[i].type&0x7F;
+                       unsigned etype = events[i].type&0x7F;
                        bool init = events[i].type&JS_EVENT_INIT;
-                       if(type==JS_EVENT_AXIS)
+                       if(etype==JS_EVENT_AXIS)
                                set_axis_value(events[i].number, events[i].value/32768.0f, !init);
-                       else if(type==JS_EVENT_BUTTON)
+                       else if(etype==JS_EVENT_BUTTON)
                                set_button_state(events[i].number, events[i].value, !init);
                }
 
@@ -121,12 +124,12 @@ string JsDevice::get_name() const
        return buf;
 }
 
-unsigned JsDevice::do_read(char *buf, unsigned size)
+size_t JsDevice::do_read(char *buf, size_t size)
 {
        return IO::sys_read(handle, buf, size);
 }
 
-unsigned JsDevice::do_write(const char *, unsigned)
+size_t JsDevice::do_write(const char *, size_t)
 {
        throw IO::invalid_access(IO::M_WRITE);
 }