From: Mikko Rasa Date: Thu, 11 Sep 2014 09:37:22 +0000 (+0300) Subject: Add a game controller input device X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=051149e5026ff64f3ec477cf19080124b82de619;p=libs%2Fgui.git Add a game controller input device Currently only implemented for Linux. --- diff --git a/Build b/Build index 7972020..5ff4f78 100644 --- a/Build +++ b/Build @@ -104,6 +104,10 @@ package "mspgui" { source "source/graphics/devil"; }; + if_arch "linux" + { + overlay "linux"; + }; if_arch "windows" { overlay "windows"; diff --git a/source/input/gamecontroller.h b/source/input/gamecontroller.h new file mode 100644 index 0000000..9ebacee --- /dev/null +++ b/source/input/gamecontroller.h @@ -0,0 +1,26 @@ +#ifndef GAMECONTROLLER_H_ +#define GAMECONTROLLER_H_ + +#include "device.h" + +namespace Msp { +namespace Input { + +class GameController: public Device +{ +private: + struct Private; + + Private *priv; + +public: + GameController(unsigned); + virtual ~GameController(); + + void tick(); +}; + +} // namespace Input +} // namespace Msp + +#endif diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp new file mode 100644 index 0000000..bf69049 --- /dev/null +++ b/source/input/linux/gamecontroller.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include +#include "gamecontroller.h" +#include "gamecontroller_platform.h" + +using namespace std; + +namespace Msp { +namespace Input { + +GameController::GameController(unsigned index): + priv(new Private) +{ + priv->dev = new JsDevice(format("/dev/input/js%d", index)); + name = priv->dev->get_name(); + tick(); +} + +GameController::~GameController() +{ + delete priv->dev; + delete priv; +} + +void GameController::tick() +{ + js_event events[16]; + while(1) + { + unsigned len = priv->dev->read(reinterpret_cast(events), sizeof(events)); + + unsigned count = len/sizeof(js_event); + for(unsigned i=0; i +#include + +namespace Msp { +namespace Input { + +class JsDevice: public IO::EventObject +{ +private: + IO::Handle handle; + +public: + JsDevice(const std::string &); + + std::string get_name() const; + +protected: + virtual unsigned do_read(char *, unsigned); + virtual unsigned do_write(const char *, unsigned); + +public: + virtual const IO::Handle &get_event_handle() { return handle; } +}; + + +struct GameController::Private +{ + JsDevice *dev; +}; + +} // namespace Input +} // namespace Msp + +#endif