X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Finput%2Fhub.cpp;h=13c88d63b18b32cb064c6feb5ef3b94ab4f43ab9;hb=2438c3cdd14087bfc3375ed1f5521ad5c0c09bbc;hp=e6969372f9240c728d6b15deeb2f0f591caefe98;hpb=1238b80d08c1983dfa8914f78b294cd44b1a275b;p=libs%2Fgui.git diff --git a/source/input/hub.cpp b/source/input/hub.cpp index e696937..13c88d6 100644 --- a/source/input/hub.cpp +++ b/source/input/hub.cpp @@ -1,55 +1,57 @@ -/* $Id$ - -This file is part of libmspgbase -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - +#include #include -#include +#include +#include +#include #include "hub.h" +using namespace std; + namespace Msp { namespace Input { Hub::Hub() { - name="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_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)); } std::string Hub::get_button_name(unsigned btn) const { - unsigned dev_index=btn>>12; - if(dev_index>devices.size()) - throw InvalidParameterValue("Button does not exist"); - - const Device &dev=*devices[dev_index]; + const Device &dev = *get_item(devices, btn>>12); return dev.get_name()+": "+dev.get_button_name(btn&0xFFF); } -void Hub::button_press(unsigned btn, unsigned index) +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); +} + +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