From f01fa56a13100ed04c94c9d8b17fbdcf0f0a8bad Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 17 Nov 2022 11:41:50 +0200 Subject: [PATCH] Go back to using vector to store devices in a Hub Hashed names generate ridiculously large button and axis indices, and Device stores them all in vectors. The predictability issue will be solved in a different way. --- source/input/hub.cpp | 44 ++++++++++++++++++++++++++------------------ source/input/hub.h | 4 ++-- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/source/input/hub.cpp b/source/input/hub.cpp index b28882f..635ea8a 100644 --- a/source/input/hub.cpp +++ b/source/input/hub.cpp @@ -17,41 +17,49 @@ Hub::Hub() void Hub::attach(Device &dev) { - unsigned tag = hash<20>(dev.get_name()); - while(devices.count(tag)) - ++tag; - - 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)); + 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)); } std::string Hub::get_button_name(unsigned btn) const { - const Device &dev = *get_item(devices, btn>>12); - return dev.get_name()+": "+dev.get_button_name(btn&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); } std::string Hub::get_axis_name(unsigned axis) const { - const Device &dev = *get_item(devices, axis>>12); - return dev.get_name()+": "+dev.get_axis_name(axis&0xFFF); + 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 diff --git a/source/input/hub.h b/source/input/hub.h index 31eeb94..b89196e 100644 --- a/source/input/hub.h +++ b/source/input/hub.h @@ -1,7 +1,7 @@ #ifndef MSP_INPUT_INPUTHUB_H_ #define MSP_INPUT_INPUTHUB_H_ -#include +#include #include #include "device.h" @@ -15,7 +15,7 @@ aggregate of them. Button and axis numbers are mapped to unique values. class Hub: public Device, public sigc::trackable { protected: - std::map devices; + std::vector devices; public: Hub(); -- 2.45.2