]> git.tdb.fi Git - libs/gui.git/commitdiff
Use hashes to make button/axis indices in Hub predictable
authorMikko Rasa <tdb@tdb.fi>
Sun, 4 Sep 2011 19:46:31 +0000 (22:46 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 4 Sep 2011 19:46:31 +0000 (22:46 +0300)
source/input/hub.cpp
source/input/hub.h

index cbf9f78a689e083d79057945ba84eb218aba3685..4a2d5533aafeb36bea172bee347e0bd4d25ae184 100644 (file)
@@ -1,5 +1,7 @@
 #include <stdexcept>
 #include <sigc++/bind.h>
+#include <msp/core/hash.h>
+#include <msp/core/maputils.h>
 #include "hub.h"
 
 using namespace std;
@@ -12,49 +14,43 @@ Hub::Hub()
        name = "Hub";
 }
 
-unsigned Hub::attach(Device &dev)
+void Hub::attach(Device &dev)
 {
-       unsigned index = devices.size();
-       devices.push_back(&dev);
-       dev.signal_button_press.connect(sigc::bind(sigc::mem_fun(this, &Hub::button_press), index));
-       dev.signal_button_release.connect(sigc::bind(sigc::mem_fun(this, &Hub::button_release), index));
-       dev.signal_axis_motion.connect(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), index));
-       return index;
+       unsigned tag = hash32(dev.get_name(), 20);
+       while(devices.count(tag))
+               ++tag;
+
+       devices[tag] = &dev;
+       dev.signal_button_press.connect(sigc::bind(sigc::mem_fun(this, &Hub::button_press), tag));
+       dev.signal_button_release.connect(sigc::bind(sigc::mem_fun(this, &Hub::button_release), tag));
+       dev.signal_axis_motion.connect(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), tag));
 }
 
 std::string Hub::get_button_name(unsigned btn) const
 {
-       unsigned dev_index = btn>>12;
-       if(dev_index>=devices.size())
-               throw invalid_argument("Hub::get_button_name");
-
-       const Device &dev = *devices[dev_index];
+       const Device &dev = *get_item(devices, btn>>12);
        return dev.get_name()+": "+dev.get_button_name(btn&0xFFF);
 }
 
 std::string Hub::get_axis_name(unsigned axis) const
 {
-       unsigned dev_index = axis>>12;
-       if(dev_index>=devices.size())
-               throw invalid_argument("Hub::get_axis_name");
-
-       const Device &dev = *devices[dev_index];
+       const Device &dev = *get_item(devices, axis>>12);
        return dev.get_name()+": "+dev.get_axis_name(axis&0xFFF);
 }
 
-void Hub::button_press(unsigned btn, unsigned index)
+void Hub::button_press(unsigned btn, unsigned tag)
 {
-       set_button_state((index<<12) | (btn&0xFFF), true, true);
+       set_button_state((tag<<12) | (btn&0xFFF), true, true);
 }
 
-void Hub::button_release(unsigned btn, unsigned index)
+void Hub::button_release(unsigned btn, unsigned tag)
 {
-       set_button_state((index<<12) | (btn&0xFFF), false, true);
+       set_button_state((tag<<12) | (btn&0xFFF), false, true);
 }
 
-void Hub::axis_motion(unsigned axis, float value, float, unsigned index)
+void Hub::axis_motion(unsigned axis, float value, float, unsigned tag)
 {
-       set_axis_value((index<<12) | (axis&0xFFF), value, true);
+       set_axis_value((tag<<12) | (axis&0xFFF), value, true);
 }
 
 } // namespace Input
index 8ec2fad7fcb3dd60af82a5f6dbab52c28a7571c3..8d41bcee41393bc0314825db0c44b1fc2b65fc61 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef MSP_INPUT_INPUTHUB_H_
 #define MSP_INPUT_INPUTHUB_H_
 
+#include <map>
 #include "device.h"
 
 namespace Msp {
@@ -13,14 +14,13 @@ aggregate of them.  Button and axis numbers are mapped to unique values.
 class Hub: public Device
 {
 protected:
-       std::vector<Device *> devices;
+       std::map<unsigned, Device *> devices;
 
 public:
        Hub();
 
-       /** Attaches an input device to the hub.  Returns the index of the device
-       within the hub. */
-       unsigned attach(Device &dev);
+       /// Attaches an input device to the hub.
+       void attach(Device &dev);
 
        virtual std::string get_button_name(unsigned) const;
        virtual std::string get_axis_name(unsigned) const;