]> git.tdb.fi Git - libs/gui.git/blob - source/input/hub.cpp
Add a type enumeration for devices
[libs/gui.git] / source / input / hub.cpp
1 #include <stdexcept>
2 #include <sigc++/bind.h>
3 #include <sigc++/bind_return.h>
4 #include <msp/core/hash.h>
5 #include <msp/core/maputils.h>
6 #include "hub.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace Input {
12
13 Hub::Hub():
14         Device(UNSPECIFIED)
15 {
16         name = "Hub";
17 }
18
19 void Hub::attach(Device &dev)
20 {
21         unsigned index = devices.size();
22         devices.push_back(&dev);
23         dev.signal_button_press.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_press), index), false));
24         dev.signal_button_release.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_release), index), false));
25         dev.signal_axis_motion.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), index), false));
26 }
27
28 std::string Hub::get_button_name(unsigned btn) const
29 {
30         unsigned dev_index = btn>>8;
31         if(dev_index>=devices.size())
32                 throw out_of_range("Hub::get_button_name");
33
34         const Device &dev = *devices[dev_index];
35         return dev.get_name()+": "+dev.get_button_name(btn&0xFF);
36 }
37
38 std::string Hub::get_axis_name(unsigned axis) const
39 {
40         unsigned dev_index = axis>>8;
41         if(dev_index>=devices.size())
42                 throw out_of_range("Hub::get_axis_name");
43
44         const Device &dev = *devices[dev_index];
45         return dev.get_name()+": "+dev.get_axis_name(axis&0xFF);
46 }
47
48 void Hub::button_press(unsigned btn, unsigned index)
49 {
50         if(btn<0x100)
51                 set_button_state((index<<8) | btn, true, true);
52 }
53
54 void Hub::button_release(unsigned btn, unsigned index)
55 {
56         if(btn<0x100)
57                 set_button_state((index<<8) | btn, false, true);
58 }
59
60 void Hub::axis_motion(unsigned axis, float value, float, unsigned index)
61 {
62         if(axis<0x100)
63                 set_axis_value((index<<8) | axis, value, true);
64 }
65
66 } // namespace Input
67 } // namespace Msp