]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/hub.cpp
Update .gitignore to include build products on Windows
[libs/gui.git] / source / input / hub.cpp
index b28882f6aad8733944f9002d4abfe6c308c1088f..1916126c9ff3f3be420de975bb3b93ea03ad9f55 100644 (file)
@@ -1,57 +1,88 @@
+#include "hub.h"
 #include <stdexcept>
 #include <sigc++/bind.h>
 #include <sigc++/bind_return.h>
 #include <msp/core/hash.h>
 #include <msp/core/maputils.h>
-#include "hub.h"
 
 using namespace std;
 
 namespace Msp {
 namespace Input {
 
-Hub::Hub()
+Hub::Hub():
+       Device(UNSPECIFIED)
 {
        name = "Hub";
 }
 
 void Hub::attach(Device &dev)
 {
-       unsigned tag = hash<20>(dev.get_name());
-       while(devices.count(tag))
-               ++tag;
+       unsigned index = devices.size();
+       devices.push_back(&dev);
+       dev.signal_button_press.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_press), index), false));
+       dev.signal_button_release.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_release), index), false));
+       dev.signal_axis_motion.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), index), false));
+}
 
-       devices[tag] = &dev;
-       dev.signal_button_press.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_press), tag), false));
-       dev.signal_button_release.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::button_release), tag), false));
-       dev.signal_axis_motion.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), tag), false));
+Device *Hub::find_subdevice(DeviceType t, unsigned n)
+{
+       for(Device *d: devices)
+               if(Device *dev = d->find_subdevice(t, 0))
+               {
+                       if(!n)
+                               return dev;
+                       --n;
+               }
+       return nullptr;
 }
 
-std::string Hub::get_button_name(unsigned btn) const
+Device *Hub::find_subdevice(const string &n)
 {
-       const Device &dev = *get_item(devices, btn>>12);
-       return dev.get_name()+": "+dev.get_button_name(btn&0xFFF);
+       if(n==name)
+               return this;
+       for(Device *d: devices)
+               if(Device *dev = d->find_subdevice(n))
+                       return dev;
+       return nullptr;
 }
 
-std::string Hub::get_axis_name(unsigned axis) const
+string Hub::get_button_name(unsigned btn) const
 {
-       const Device &dev = *get_item(devices, axis>>12);
-       return dev.get_name()+": "+dev.get_axis_name(axis&0xFFF);
+       unsigned dev_index = btn>>8;
+       if(dev_index>=devices.size())
+               throw out_of_range("Hub::get_button_name");
+
+       const Device &dev = *devices[dev_index];
+       return dev.get_name()+": "+dev.get_button_name(btn&0xFF);
+}
+
+string Hub::get_axis_name(unsigned axis) const
+{
+       unsigned dev_index = axis>>8;
+       if(dev_index>=devices.size())
+               throw out_of_range("Hub::get_axis_name");
+
+       const Device &dev = *devices[dev_index];
+       return dev.get_name()+": "+dev.get_axis_name(axis&0xFF);
 }
 
-void Hub::button_press(unsigned btn, unsigned tag)
+void Hub::button_press(unsigned btn, unsigned index)
 {
-       set_button_state((tag<<12) | (btn&0xFFF), true, true);
+       if(btn<0x100)
+               set_button_state((index<<8) | btn, true, true);
 }
 
-void Hub::button_release(unsigned btn, unsigned tag)
+void Hub::button_release(unsigned btn, unsigned index)
 {
-       set_button_state((tag<<12) | (btn&0xFFF), false, true);
+       if(btn<0x100)
+               set_button_state((index<<8) | btn, false, true);
 }
 
-void Hub::axis_motion(unsigned axis, float value, float, unsigned tag)
+void Hub::axis_motion(unsigned axis, float value, float, unsigned index)
 {
-       set_axis_value((tag<<12) | (axis&0xFFF), value, true);
+       if(axis<0x100)
+               set_axis_value((index<<8) | axis, value, true);
 }
 
 } // namespace Input