]> git.tdb.fi Git - libs/gui.git/commitdiff
Add functions to look up subdevices of composite input devices
authorMikko Rasa <tdb@tdb.fi>
Sat, 19 Nov 2022 11:52:09 +0000 (13:52 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 19 Nov 2022 11:52:35 +0000 (13:52 +0200)
source/input/device.cpp
source/input/device.h
source/input/hub.cpp
source/input/hub.h

index 452af22543e4f57db85a9a5271dad5c2648c96a1..d5b3b864af3c854d42de216196ab42a17952a364 100644 (file)
@@ -1,6 +1,8 @@
 #include <msp/strings/format.h>
 #include "device.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Input {
 
@@ -11,6 +13,16 @@ Device::Device(DeviceType t):
 Device::~Device()
 { }
 
+Device *Device::find_subdevice(const string &n)
+{
+       return (n==name ? this : 0);
+}
+
+Device *Device::find_subdevice(DeviceType t, unsigned i)
+{
+       return (t==type && i==0 ? this : 0);
+}
+
 bool Device::get_button_state(unsigned btn) const
 {
        if(btn>=buttons.size())
index 853ec96c902ebe4cdf82942822fa10563ab69849..4544776ac3a36f1f6b36d7dfe0d7a79cdaacb232 100644 (file)
@@ -69,6 +69,8 @@ public:
 
        DeviceType get_type() const { return type; }
        const std::string &get_name() const { return name; }
+       virtual Device *find_subdevice(DeviceType, unsigned = 0);
+       virtual Device *find_subdevice(const std::string &);
        bool get_button_state(unsigned) const;
        float get_axis_value(unsigned) const;
 
index b83d10d22431b6b89b7126c9f5fb7a61f38c97fe..e45cd6576b3bd02b890f2ab96606fc125fee63dc 100644 (file)
@@ -25,6 +25,28 @@ void Hub::attach(Device &dev)
        dev.signal_axis_motion.connect(sigc::bind_return(sigc::bind(sigc::mem_fun(this, &Hub::axis_motion), index), false));
 }
 
+Device *Hub::find_subdevice(DeviceType t, unsigned n)
+{
+       for(vector<Device *>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
+               if(Device *dev = (*i)->find_subdevice(t, 0))
+               {
+                       if(!n)
+                               return dev;
+                       --n;
+               }
+       return 0;
+}
+
+Device *Hub::find_subdevice(const string &n)
+{
+       if(n==name)
+               return this;
+       for(vector<Device *>::const_iterator i=devices.begin(); i!=devices.end(); ++i)
+               if(Device *dev = (*i)->find_subdevice(n))
+                       return dev;
+       return 0;
+}
+
 std::string Hub::get_button_name(unsigned btn) const
 {
        unsigned dev_index = btn>>8;
index b89196ecae793a5b9704e506dc6063263e1259b1..55d58765f1d705dabeeec7ff5eb028a4f65a46b8 100644 (file)
@@ -23,6 +23,9 @@ public:
        /// Attaches an input device to the hub.
        void attach(Device &dev);
 
+       virtual Device *find_subdevice(DeviceType, unsigned = 0);
+       virtual Device *find_subdevice(const std::string &);
+
        virtual std::string get_button_name(unsigned) const;
        virtual std::string get_axis_name(unsigned) const;
 protected: