From 5cb9a421ea3ff6d5fd2504f5e38f6014082dcb67 Mon Sep 17 00:00:00 2001 From: joncruz Date: Sat, 22 Mar 2008 21:40:30 +0000 Subject: [PATCH] Tracking of live axes --- src/device-manager.cpp | 30 +++++++++++++++++++++++++++- src/device-manager.h | 3 +++ src/ui/dialog/input.cpp | 44 ++++++++++++++++++++++++++++++----------- 3 files changed, 64 insertions(+), 13 deletions(-) diff --git a/src/device-manager.cpp b/src/device-manager.cpp index 9cf4583c6..dcf86edb7 100644 --- a/src/device-manager.cpp +++ b/src/device-manager.cpp @@ -50,6 +50,8 @@ public: virtual gint getNumKeys() const {return device->num_keys;} virtual Glib::ustring getLink() const {return link;} virtual void setLink( Glib::ustring const& link ) {this->link = link;} + virtual gint getLiveAxes() const {return liveAxes;} + virtual void setLiveAxes(gint axes) {liveAxes = axes;} virtual gint getLiveButtons() const {return liveButtons;} virtual void setLiveButtons(gint buttons) {liveButtons = buttons;} @@ -66,6 +68,7 @@ private: Glib::ustring name; Gdk::InputSource source; Glib::ustring link; + guint liveAxes; guint liveButtons; }; @@ -94,6 +97,7 @@ InputDeviceImpl::InputDeviceImpl(GdkDevice* device) name(device->name ? device->name : ""), source(static_cast(device->source)), link(), + liveAxes(0), liveButtons(0) { switch ( source ) { @@ -127,15 +131,18 @@ public: DeviceManagerImpl(); virtual std::list getDevices(); virtual sigc::signal& > signalDeviceChanged(); + virtual sigc::signal& > signalAxesChanged(); virtual sigc::signal& > signalButtonsChanged(); virtual sigc::signal& > signalLinkChanged(); + virtual void addAxis(Glib::ustring const & id, gint axis); virtual void addButton(Glib::ustring const & id, gint button); virtual void setLinkedTo(Glib::ustring const & id, Glib::ustring const& link); protected: std::list devices; sigc::signal& > signalDeviceChangedPriv; + sigc::signal& > signalAxesChangedPriv; sigc::signal& > signalButtonsChangedPriv; sigc::signal& > signalLinkChangedPriv; }; @@ -179,6 +186,11 @@ sigc::signal& > DeviceManagerImpl::signalD return signalDeviceChangedPriv; } +sigc::signal& > DeviceManagerImpl::signalAxesChanged() +{ + return signalAxesChangedPriv; +} + sigc::signal& > DeviceManagerImpl::signalButtonsChanged() { return signalButtonsChangedPriv; @@ -189,6 +201,23 @@ sigc::signal& > DeviceManagerImpl::signalL return signalLinkChangedPriv; } +void DeviceManagerImpl::addAxis(Glib::ustring const & id, gint axis) +{ + if ( axis >= 0 && axis < static_cast(bitVals.size()) ) { + std::list::iterator it = std::find_if(devices.begin(), devices.end(), IdMatcher(id)); + if ( it != devices.end() ) { + gint mask = bitVals[axis]; + if ( (mask & (*it)->getLiveAxes()) == 0 ) { + (*it)->setLiveAxes((*it)->getLiveAxes() | mask); + + // Only signal if a new axis was added + (*it)->reference(); + signalAxesChangedPriv.emit(Glib::RefPtr(*it)); + } + } + } +} + void DeviceManagerImpl::addButton(Glib::ustring const & id, gint button) { if ( button >= 0 && button < static_cast(bitVals.size()) ) { @@ -206,7 +235,6 @@ void DeviceManagerImpl::addButton(Glib::ustring const & id, gint button) } } - void DeviceManagerImpl::setLinkedTo(Glib::ustring const & id, Glib::ustring const& link) { std::list::iterator it = std::find_if(devices.begin(), devices.end(), IdMatcher(id)); diff --git a/src/device-manager.h b/src/device-manager.h index f441180a6..1bfb5120c 100644 --- a/src/device-manager.h +++ b/src/device-manager.h @@ -26,6 +26,7 @@ public: virtual gint getNumAxes() const = 0; virtual gint getNumKeys() const = 0; virtual Glib::ustring getLink() const = 0; + virtual gint getLiveAxes() const = 0; virtual gint getLiveButtons() const = 0; protected: @@ -43,9 +44,11 @@ public: virtual std::list getDevices() = 0; virtual sigc::signal& > signalDeviceChanged() = 0; + virtual sigc::signal& > signalAxesChanged() = 0; virtual sigc::signal& > signalButtonsChanged() = 0; virtual sigc::signal& > signalLinkChanged() = 0; + virtual void addAxis(Glib::ustring const & id, gint axis) = 0; virtual void addButton(Glib::ustring const & id, gint button) = 0; virtual void setLinkedTo(Glib::ustring const & id, Glib::ustring const& link) = 0; diff --git a/src/ui/dialog/input.cpp b/src/ui/dialog/input.cpp index 1400ce93f..34e503d08 100644 --- a/src/ui/dialog/input.cpp +++ b/src/ui/dialog/input.cpp @@ -395,6 +395,7 @@ private: void linkComboChanged(); void resyncToSelection(); void handleDeviceChange(const Glib::RefPtr& device); + void updateDeviceAxes(const Glib::RefPtr& device); void updateDeviceButtons(const Glib::RefPtr& device); void updateDeviceLinks(const Glib::RefPtr& device); }; @@ -674,6 +675,7 @@ InputDialogImpl::InputDialogImpl() : g_warning("No devices found"); } Inkscape::DeviceManager::getManager().signalDeviceChanged().connect(sigc::mem_fun(*this, &InputDialogImpl::handleDeviceChange)); + Inkscape::DeviceManager::getManager().signalAxesChanged().connect(sigc::mem_fun(*this, &InputDialogImpl::updateDeviceAxes)); Inkscape::DeviceManager::getManager().signalButtonsChanged().connect(sigc::mem_fun(*this, &InputDialogImpl::updateDeviceButtons)); Inkscape::DeviceManager::getManager().signalLinkChanged().connect(sigc::mem_fun(*this, &InputDialogImpl::updateDeviceLinks)); @@ -686,6 +688,23 @@ void InputDialogImpl::handleDeviceChange(const Glib::RefPtr& /*devi // g_message("OUCH!!!! for %p hits %s", &device, device->getId().c_str()); } +void InputDialogImpl::updateDeviceAxes(const Glib::RefPtr& device) +{ + gint live = device->getLiveAxes(); + + std::map > existing = axesMap[device->getId()]; + gint mask = 0x1; + for ( gint num = 0; num < 32; num++, mask <<= 1) { + if ( (mask & live) != 0 ) { + if ( (existing.find(num) == existing.end()) || (existing[num].first < 2) ) { + axesMap[device->getId()][num].first = 2; + axesMap[device->getId()][num].second = 0.0; + } + } + } + updateTestAxes( device->getId(), 0 ); +} + void InputDialogImpl::updateDeviceButtons(const Glib::RefPtr& device) { gint live = device->getLiveButtons(); @@ -877,31 +896,32 @@ void InputDialogImpl::mapAxesValues( Glib::ustring const& key, guint numAxes, gd { static gdouble epsilon = 0.0001; if ( (numAxes > 0) && axes) { - for ( guint axesNum = 0; axesNum < numAxes; axesNum++ ) { + for ( guint axisNum = 0; axisNum < numAxes; axisNum++ ) { // 0 == new, 1 == set value, 2 == changed value, 3 == active - gdouble diff = axesMap[key][axesNum].second - axes[axesNum]; - switch(axesMap[key][axesNum].first) { + gdouble diff = axesMap[key][axisNum].second - axes[axisNum]; + switch(axesMap[key][axisNum].first) { case 0: { - axesMap[key][axesNum].first = 1; - axesMap[key][axesNum].second = axes[axesNum]; + axesMap[key][axisNum].first = 1; + axesMap[key][axisNum].second = axes[axisNum]; } break; case 1: { if ( (diff > epsilon) || (diff < -epsilon) ) { -// g_message("Axis %d changed on %s]", axesNum, key.c_str()); - axesMap[key][axesNum].first = 3; - axesMap[key][axesNum].second = axes[axesNum]; +// g_message("Axis %d changed on %s]", axisNum, key.c_str()); + axesMap[key][axisNum].first = 3; + axesMap[key][axisNum].second = axes[axisNum]; updateTestAxes(key, dev); + DeviceManager::getManager().addAxis(key, axisNum); } } break; case 2: { if ( (diff > epsilon) || (diff < -epsilon) ) { - axesMap[key][axesNum].first = 3; - axesMap[key][axesNum].second = axes[axesNum]; + axesMap[key][axisNum].first = 3; + axesMap[key][axisNum].second = axes[axisNum]; updateTestAxes(key, dev); } } @@ -909,9 +929,9 @@ void InputDialogImpl::mapAxesValues( Glib::ustring const& key, guint numAxes, gd case 3: { if ( (diff > epsilon) || (diff < -epsilon) ) { - axesMap[key][axesNum].second = axes[axesNum]; + axesMap[key][axisNum].second = axes[axisNum]; } else { - axesMap[key][axesNum].first = 2; + axesMap[key][axisNum].first = 2; updateTestAxes(key, dev); } } -- 2.30.2