]> git.tdb.fi Git - libs/gui.git/blob - source/input/device.h
Add a dedicated exception class for unavailable input devices
[libs/gui.git] / source / input / device.h
1 #ifndef MSP_INPUT_INPUTDEVICE_H_
2 #define MSP_INPUT_INPUTDEVICE_H_
3
4 #include <string>
5 #include <vector>
6 #include <stdexcept>
7 #include <sigc++/signal.h>
8
9 namespace Msp {
10 namespace Input {
11
12 class device_not_available: public std::runtime_error
13 {
14 public:
15         device_not_available(const std::string &w): std::runtime_error(w) { }
16         virtual ~device_not_available() throw() { }
17 };
18
19
20 /**
21 Base class for input devices.  Input devices have two types of controls:
22 buttons and axes.  Buttons are either on or off.  Axes have a floating point
23 value nominally in the range [-1, 1].
24
25 Event handlers return a boolean indicating whether the event is considered
26 processed.  If a handler returns true, no further handlers are invoked.
27 */
28 class Device
29 {
30 protected:
31         struct EventAccumulator
32         {
33                 typedef void result_type;
34
35                 template<typename Iter>
36                 result_type operator()(Iter begin, Iter end) const
37                 {
38                         for(Iter i=begin; i!=end; ++i)
39                                 if(*i)
40                                         return;
41                 }
42         };
43
44 public:
45         sigc::signal<bool, unsigned>::accumulated<EventAccumulator> signal_button_press;
46         sigc::signal<bool, unsigned>::accumulated<EventAccumulator> signal_button_release;
47         sigc::signal<bool, unsigned, float, float>::accumulated<EventAccumulator> signal_axis_motion;
48
49 protected:
50         std::string name;
51         std::vector<char> buttons;
52         std::vector<float> axes;
53
54         Device();
55 public:
56         virtual ~Device();
57
58         const std::string &get_name() const { return name; }
59         bool get_button_state(unsigned) const;
60         float get_axis_value(unsigned) const;
61
62         virtual std::string get_button_name(unsigned) const;
63         virtual std::string get_axis_name(unsigned) const;
64 protected:
65         void set_button_state(unsigned, bool, bool);
66         void set_axis_value(unsigned, float, bool);
67 };
68
69 } // namespace Input
70 } // namespace Msp
71
72 #endif