]> git.tdb.fi Git - libs/gui.git/blob - source/input/hub.cpp
Go back to using vector to store devices in a Hub
[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 {
15         name = "Hub";
16 }
17
18 void Hub::attach(Device &dev)
19 {
20         unsigned index = devices.size();
21         devices.push_back(&dev);
22         dev.signal_button_press.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_press), index), false));
23         dev.signal_button_release.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_release), index), false));
24         dev.signal_axis_motion.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), index), false));
25 }
26
27 std::string Hub::get_button_name(unsigned btn) const
28 {
29         unsigned dev_index = btn>>8;
30         if(dev_index>=devices.size())
31                 throw out_of_range("Hub::get_button_name");
32
33         const Device &dev = *devices[dev_index];
34         return dev.get_name()+": "+dev.get_button_name(btn&0xFF);
35 }
36
37 std::string Hub::get_axis_name(unsigned axis) const
38 {
39         unsigned dev_index = axis>>8;
40         if(dev_index>=devices.size())
41                 throw out_of_range("Hub::get_axis_name");
42
43         const Device &dev = *devices[dev_index];
44         return dev.get_name()+": "+dev.get_axis_name(axis&0xFF);
45 }
46
47 void Hub::button_press(unsigned btn, unsigned index)
48 {
49         if(btn<0x100)
50                 set_button_state((index<<8) | btn, true, true);
51 }
52
53 void Hub::button_release(unsigned btn, unsigned index)
54 {
55         if(btn<0x100)
56                 set_button_state((index<<8) | btn, false, true);
57 }
58
59 void Hub::axis_motion(unsigned axis, float value, float, unsigned index)
60 {
61         if(axis<0x100)
62                 set_axis_value((index<<8) | axis, value, true);
63 }
64
65 } // namespace Input
66 } // namespace Msp