]> git.tdb.fi Git - libs/gui.git/blob - source/input/hub.cpp
e7eb0fc115581a1400e603640103e57b620c5d0c
[libs/gui.git] / source / input / hub.cpp
1 #include "hub.h"
2 #include <stdexcept>
3 #include <sigc++/bind.h>
4 #include <sigc++/bind_return.h>
5 #include <msp/core/hash.h>
6 #include <msp/core/maputils.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 Device *Hub::find_subdevice(DeviceType t, unsigned n)
29 {
30         for(Device *d: devices)
31                 if(Device *dev = d->find_subdevice(t, 0))
32                 {
33                         if(!n)
34                                 return dev;
35                         --n;
36                 }
37         return 0;
38 }
39
40 Device *Hub::find_subdevice(const string &n)
41 {
42         if(n==name)
43                 return this;
44         for(Device *d: devices)
45                 if(Device *dev = d->find_subdevice(n))
46                         return dev;
47         return 0;
48 }
49
50 string Hub::get_button_name(unsigned btn) const
51 {
52         unsigned dev_index = btn>>8;
53         if(dev_index>=devices.size())
54                 throw out_of_range("Hub::get_button_name");
55
56         const Device &dev = *devices[dev_index];
57         return dev.get_name()+": "+dev.get_button_name(btn&0xFF);
58 }
59
60 string Hub::get_axis_name(unsigned axis) const
61 {
62         unsigned dev_index = axis>>8;
63         if(dev_index>=devices.size())
64                 throw out_of_range("Hub::get_axis_name");
65
66         const Device &dev = *devices[dev_index];
67         return dev.get_name()+": "+dev.get_axis_name(axis&0xFF);
68 }
69
70 void Hub::button_press(unsigned btn, unsigned index)
71 {
72         if(btn<0x100)
73                 set_button_state((index<<8) | btn, true, true);
74 }
75
76 void Hub::button_release(unsigned btn, unsigned index)
77 {
78         if(btn<0x100)
79                 set_button_state((index<<8) | btn, false, true);
80 }
81
82 void Hub::axis_motion(unsigned axis, float value, float, unsigned index)
83 {
84         if(axis<0x100)
85                 set_axis_value((index<<8) | axis, value, true);
86 }
87
88 } // namespace Input
89 } // namespace Msp