From 3b0d63f09b6bfc5de215e27eef8a5934eb4d3851 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 19 Nov 2022 13:52:09 +0200 Subject: [PATCH] Add functions to look up subdevices of composite input devices --- source/input/device.cpp | 12 ++++++++++++ source/input/device.h | 2 ++ source/input/hub.cpp | 22 ++++++++++++++++++++++ source/input/hub.h | 3 +++ 4 files changed, 39 insertions(+) diff --git a/source/input/device.cpp b/source/input/device.cpp index 452af22..d5b3b86 100644 --- a/source/input/device.cpp +++ b/source/input/device.cpp @@ -1,6 +1,8 @@ #include #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()) diff --git a/source/input/device.h b/source/input/device.h index 853ec96..4544776 100644 --- a/source/input/device.h +++ b/source/input/device.h @@ -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; diff --git a/source/input/hub.cpp b/source/input/hub.cpp index b83d10d..e45cd65 100644 --- a/source/input/hub.cpp +++ b/source/input/hub.cpp @@ -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::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::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; diff --git a/source/input/hub.h b/source/input/hub.h index b89196e..55d5876 100644 --- a/source/input/hub.h +++ b/source/input/hub.h @@ -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: -- 2.43.0