]> git.tdb.fi Git - libs/gui.git/commitdiff
Add names for input devices
authorMikko Rasa <tdb@tdb.fi>
Mon, 31 Dec 2007 18:26:00 +0000 (18:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Mon, 31 Dec 2007 18:26:00 +0000 (18:26 +0000)
Add support for retrieving button and axis names
Some minor tweaks

13 files changed:
Build
source/control.cpp
source/control.h
source/glcontext.h
source/inputdevice.cpp
source/inputdevice.h
source/inputhub.cpp
source/inputhub.h
source/keyboard.cpp
source/keyboard.h
source/mouse.cpp
source/mouse.h
source/window.cpp

diff --git a/Build b/Build
index 19c48cb3844b50c40b81d249e670f04a04aa05a0..338611954e8895141c2d05f90761d77c71302709 100644 (file)
--- a/Build
+++ b/Build
@@ -6,6 +6,7 @@ package "mspgbase"
        version "0.0";
 
        require "mspcore";
+       require "mspstrings";
        require "sigc++-2.0";
        // The OpenGL stuff is hackish, but only way to do it right now
        if "arch!=win32"
index ef1170cc84b66b42bb8ac0659e97adf1a8f978b6..823202d3e11544ea2c45d1c0a54c8ce807f85d6a 100644 (file)
@@ -6,6 +6,7 @@ Distributed under the LGPL
 */
 
 #include <msp/core/except.h>
+#include <msp/strings/lexicalcast.h>
 #include "control.h"
 #include "inputdevice.h"
 
@@ -24,6 +25,18 @@ ControlSource::ControlSource(Device &d, ControlSrcType t, unsigned i):
        index(i)
 { }
 
+std::string ControlSource::str() const
+{
+       if(type==BUTTON)
+               return dev->get_button_name(index);
+       else if(type==AXIS_POS || type==AXIS_NEG)
+               return dev->get_axis_name(index);
+       else if(type==NONE)
+               return "None";
+
+       return lexical_cast(index);
+}
+
 
 Control::Control():
        capture_dev(0)
index ccb16c51cabf42b70063cea68983159f8fed63c5..f7146b47f4502345d8d00a01988fb4b36cc78f3c 100644 (file)
@@ -36,6 +36,7 @@ struct ControlSource
 
        ControlSource();
        ControlSource(Device &, ControlSrcType, unsigned);
+       std::string str() const;
 };
 
 /**
index 36130e1c55df8b8d048ea5a2000a12cc367724d0..297bc34b4a4f7c40fe3da8cbefbc258552ae8999 100644 (file)
@@ -45,7 +45,7 @@ private:
 #endif
 
 public:
-       GLContext(Window &wnd, const GLOptions &opts);
+       GLContext(Window &wnd, const GLOptions &opts=GLOptions());
        ~GLContext();
 
        void swap_buffers();
index e994d9fc3021052fbf29fd2bd7e785ce50de0ae6..f3092e1dd6727178e1a17bed2b8cdaad255ed4fd 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/strings/formatter.h>
 #include "inputdevice.h"
 
 namespace Msp {
@@ -26,6 +27,16 @@ float Device::get_axis_value(unsigned axis) const
        return axes[axis];
 }
 
+std::string Device::get_button_name(unsigned btn) const
+{
+       return format("Button %d", btn);
+}
+
+std::string Device::get_axis_name(unsigned axis) const
+{
+       return format("Axis %d", axis);
+}
+
 void Device::set_button_state(unsigned btn, bool state, bool event)
 {
        if(btn>=buttons.size())
index 4dd002291599cb3766c38337fa451dbf86c2622e..d1c96f23be04e890d56755bc2ef55b4043c5aa8b 100644 (file)
@@ -8,6 +8,7 @@ Distributed under the LGPL
 #ifndef MSP_GBASE_INPUTDEVICE_H_
 #define MSP_GBASE_INPUTDEVICE_H_
 
+#include <string>
 #include <vector>
 #include <sigc++/signal.h>
 
@@ -27,6 +28,7 @@ public:
        sigc::signal<void, unsigned, float, float> signal_axis_motion;
 
 protected:
+       std::string name;
        std::vector<char>  buttons;
        std::vector<float> axes;
        float axis_threshold;
@@ -35,9 +37,13 @@ protected:
        Device() { }
 public:
        virtual ~Device() { }
+       const std::string &get_name() const { return name; }
        bool  get_button_state(unsigned) const;
        float get_axis_value(unsigned) const;
        float get_axis_threshold() const { return axis_threshold; }
+
+       virtual std::string get_button_name(unsigned) const;
+       virtual std::string get_axis_name(unsigned) const;
 protected:
        void set_button_state(unsigned, bool, bool);
        void set_axis_value(unsigned, float, bool);
index 27a340a134015433e19cace7ed65824f784558ef..1fa95d0fc3f41a3dfbf5c75b7636881dd4e1239e 100644 (file)
@@ -6,11 +6,17 @@ Distributed under the LGPL
 */
 
 #include <sigc++/bind.h>
+#include <msp/core/except.h>
 #include "inputhub.h"
 
 namespace Msp {
 namespace Input {
 
+Hub::Hub()
+{
+       name="Hub";
+}
+
 unsigned Hub::attach(Device &dev)
 {
        unsigned index=devices.size();
@@ -21,19 +27,29 @@ unsigned Hub::attach(Device &dev)
        return index;
 }
 
+std::string Hub::get_button_name(unsigned btn) const
+{
+       unsigned dev_index=btn>>12;
+       if(dev_index>devices.size())
+               throw InvalidParameterValue("Button does not exist");
+
+       const Device &dev=*devices[dev_index];
+       return dev.get_name()+": "+dev.get_button_name(btn&0xFFF);
+}
+
 void Hub::button_press(unsigned btn, unsigned index)
 {
-       set_button_state(index<<8 | btn&0xFF, true, true);
+       set_button_state(index<<12 | btn&0xFFF, true, true);
 }
 
 void Hub::button_release(unsigned btn, unsigned index)
 {
-       set_button_state(index<<8 | btn&0xFF, false, true);
+       set_button_state(index<<12 | btn&0xFFF, false, true);
 }
 
 void Hub::axis_motion(unsigned axis, float value, float, unsigned index)
 {
-       set_axis_value(index<<8 | axis&0xFF, value, true);
+       set_axis_value(index<<12 | axis&0xFFF, value, true);
 }
 
 } // namespace Input
index b20fd43468fffc093d03e043bbc4d105d6fe60e3..c66ae98d71ce6a132f0600ec420e56f54100c681 100644 (file)
@@ -23,6 +23,8 @@ protected:
        std::vector<Device *> devices;
 
 public:
+       Hub();
+
        /**
        Attaches an input device to the hub.
 
@@ -31,6 +33,8 @@ public:
        @return  Index of the device within the hub
        */
        unsigned attach(Device &dev);
+
+       virtual std::string get_button_name(unsigned) const;
 protected:
        void button_press(unsigned, unsigned);
        void button_release(unsigned, unsigned);
index ab01e6261eff14eb7345de57c34e1cd0412607bd..2e3c92df97fdb88ec2d974d4cd3a912502ab417e 100644 (file)
@@ -5,19 +5,37 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/strings/formatter.h>
+#include "display.h"
 #include "keyboard.h"
 
 namespace Msp {
 namespace Input {
 
-Keyboard::Keyboard(Graphics::Window &window)
+Keyboard::Keyboard(Graphics::Window &w):
+       window(w)
 {
+       name="Keyboard";
+
        buttons.resize(256, false);
 
        window.signal_key_press.connect(sigc::mem_fun(this, &Keyboard::key_press));
        window.signal_key_release.connect(sigc::mem_fun(this, &Keyboard::key_release));
 }
 
+std::string Keyboard::get_button_name(unsigned btn) const
+{
+#ifndef WIN32
+       KeySym ksym=XKeycodeToKeysym(window.get_display().get_display(), btn, 0);
+       return XKeysymToString(ksym);
+#else
+       char buf[128];
+       if(!GetKeyNameText(btn<<16, buf, sizeof(buf)))
+               return format("Key %d", btn);
+       return buf;
+#endif
+}
+
 void Keyboard::key_press(unsigned key, unsigned, unsigned)
 {
        set_button_state(key, true, true);
index e3006034e359a3aee66e8a180942045e0cbfbe13..3e0efa23a2146488307a9f219c719e1b8b223eb1 100644 (file)
@@ -16,9 +16,14 @@ namespace Input {
 
 class Keyboard: public Device
 {
+private:
+       Graphics::Window &window;
+
 public:
        Keyboard(Graphics::Window &);
-protected:
+
+       virtual std::string get_button_name(unsigned) const;
+private:
        void key_press(unsigned, unsigned, unsigned);
        void key_release(unsigned, unsigned);
 };
index dc361252bc988150c97c7aed5c1b8fb71699d70e..931b204f8a2655805f18509259ffc5c3781587ea 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
+#include <msp/strings/formatter.h>
 #include "mouse.h"
 
 namespace Msp {
@@ -14,6 +15,8 @@ Mouse::Mouse(Graphics::Window &w):
        window(w),
        axis_scale(0.01)
 {
+       name="Mouse";
+
        buttons.resize(3);
        axes.resize(2);
 
@@ -22,6 +25,25 @@ Mouse::Mouse(Graphics::Window &w):
        window.signal_pointer_motion.connect(sigc::mem_fun(this, &Mouse::pointer_motion));
 }
 
+std::string Mouse::get_button_name(unsigned btn) const
+{
+       switch(btn)
+       {
+       case 1:
+               return "Left";
+       case 2:
+               return "Middle";
+       case 3:
+               return "Right";
+       case 4:
+               return "Wheel Up";
+       case 5:
+               return "Wheel Down";
+       default:
+               return format("Button %d", btn);
+       }
+}
+
 void Mouse::button_press(int, int, unsigned btn, unsigned)
 {
        set_button_state(btn, true, true);
index 4199f844b29d47c89fd03931c0a2374e75c2a2b3..ccc2e8ee3182b1c11cb6120c118d41e99c2d6e6e 100644 (file)
@@ -22,6 +22,7 @@ private:
 
 public:
        Mouse(Graphics::Window &);
+       virtual std::string get_button_name(unsigned) const;
 private:
        void button_press(int, int, unsigned, unsigned);
        void button_release(int, int, unsigned, unsigned);
index 7f6f6d8fa0e6c77463df993397d01b9dc10ff777..da877e966679b8ffd8f66b362a9f8feec2b6e695 100644 (file)
@@ -148,7 +148,7 @@ void Window::init()
                wndcl.cbWndExtra=sizeof(Window *);
                wndcl.hInstance=reinterpret_cast<HINSTANCE>(Application::get_data());
                wndcl.hIcon=0;
-               wndcl.hCursor=0;
+               wndcl.hCursor=LoadCursor(0, IDC_ARROW);
                wndcl.hbrBackground=0;
                wndcl.lpszMenuName=0;
                wndcl.lpszClassName="mspgbase";
@@ -264,10 +264,10 @@ int Window::wndproc(UINT msg, WPARAM wp, LPARAM lp)
        switch(msg)
        {
        case WM_KEYDOWN:
-               signal_key_press.emit(wp, 0, wp);
+               signal_key_press.emit((lp>>16)&0x1FF, 0, wp);
                break;
        case WM_KEYUP:
-               signal_key_release.emit(wp, 0);
+               signal_key_release.emit((lp>>16)&0x1FF, 0);
                break;
        case WM_LBUTTONDOWN:
                signal_button_press.emit(GET_X_LPARAM(lp), GET_Y_LPARAM(lp), 1, 0);