From: Mikko Rasa Date: Mon, 31 Dec 2007 18:26:00 +0000 (+0000) Subject: Add names for input devices X-Git-Tag: 0.9~10 X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=77d8b98374ab07be0d14c9c88502c6a803041040;p=libs%2Fgui.git Add names for input devices Add support for retrieving button and axis names Some minor tweaks --- diff --git a/Build b/Build index 19c48cb..3386119 100644 --- 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" diff --git a/source/control.cpp b/source/control.cpp index ef1170c..823202d 100644 --- a/source/control.cpp +++ b/source/control.cpp @@ -6,6 +6,7 @@ Distributed under the LGPL */ #include +#include #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) diff --git a/source/control.h b/source/control.h index ccb16c5..f7146b4 100644 --- a/source/control.h +++ b/source/control.h @@ -36,6 +36,7 @@ struct ControlSource ControlSource(); ControlSource(Device &, ControlSrcType, unsigned); + std::string str() const; }; /** diff --git a/source/glcontext.h b/source/glcontext.h index 36130e1..297bc34 100644 --- a/source/glcontext.h +++ b/source/glcontext.h @@ -45,7 +45,7 @@ private: #endif public: - GLContext(Window &wnd, const GLOptions &opts); + GLContext(Window &wnd, const GLOptions &opts=GLOptions()); ~GLContext(); void swap_buffers(); diff --git a/source/inputdevice.cpp b/source/inputdevice.cpp index e994d9f..f3092e1 100644 --- a/source/inputdevice.cpp +++ b/source/inputdevice.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #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()) diff --git a/source/inputdevice.h b/source/inputdevice.h index 4dd0022..d1c96f2 100644 --- a/source/inputdevice.h +++ b/source/inputdevice.h @@ -8,6 +8,7 @@ Distributed under the LGPL #ifndef MSP_GBASE_INPUTDEVICE_H_ #define MSP_GBASE_INPUTDEVICE_H_ +#include #include #include @@ -27,6 +28,7 @@ public: sigc::signal signal_axis_motion; protected: + std::string name; std::vector buttons; std::vector 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); diff --git a/source/inputhub.cpp b/source/inputhub.cpp index 27a340a..1fa95d0 100644 --- a/source/inputhub.cpp +++ b/source/inputhub.cpp @@ -6,11 +6,17 @@ Distributed under the LGPL */ #include +#include #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 diff --git a/source/inputhub.h b/source/inputhub.h index b20fd43..c66ae98 100644 --- a/source/inputhub.h +++ b/source/inputhub.h @@ -23,6 +23,8 @@ protected: std::vector 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); diff --git a/source/keyboard.cpp b/source/keyboard.cpp index ab01e62..2e3c92d 100644 --- a/source/keyboard.cpp +++ b/source/keyboard.cpp @@ -5,19 +5,37 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include +#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); diff --git a/source/keyboard.h b/source/keyboard.h index e300603..3e0efa2 100644 --- a/source/keyboard.h +++ b/source/keyboard.h @@ -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); }; diff --git a/source/mouse.cpp b/source/mouse.cpp index dc36125..931b204 100644 --- a/source/mouse.cpp +++ b/source/mouse.cpp @@ -5,6 +5,7 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include #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); diff --git a/source/mouse.h b/source/mouse.h index 4199f84..ccc2e8e 100644 --- a/source/mouse.h +++ b/source/mouse.h @@ -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); diff --git a/source/window.cpp b/source/window.cpp index 7f6f6d8..da877e9 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -148,7 +148,7 @@ void Window::init() wndcl.cbWndExtra=sizeof(Window *); wndcl.hInstance=reinterpret_cast(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);