Files
qelectrotech-source-mirror/sources/spacemouse/spnavbackend.cpp
T
ispyisail 17ebbffbca Add 3D mouse speed, direction, dead zone and twist-to-zoom settings
The 3D mouse's pan and zoom speeds were fixed guesses, and each sample
was applied as it came, so the speed on screen depended on how often
the driver sends samples -- different for every platform and device.

Motion now goes through SpaceMouseMotion::map(), which scales each
sample by the time since the previous one, and applies the user's
settings from a new "Mouvement" section of Configuration > Souris 3D:
pan and zoom speed, a dead zone, inverting each axis, and zooming by
push/pull (as before) or by twisting the cap. The defaults keep the
previous behaviour. Zoom is now exponential in the deflection, so the
factor stays positive however hard the cap is pulled (1 + z/1000 went
negative past z = -1000) and an equal push and pull cancel out. Sub-
pixel pan is carried over between samples instead of being rounded
away. The backend now reports all six axes.

tst_spacemousemotion covers the mapping without a device and is built
whether or not QET_ENABLE_SPACEMOUSE is on. The new behaviour was also
checked end to end with tools/spnav-shim (qelectrotech-docker): twist
with a dead zone of 10 ignores push/pull and small drift, and a twist of
60 gives the same frame as a push of 50 with the defaults.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 09:56:04 +12:00

85 lines
2.4 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#include "spnavbackend.h"
#include <QSocketNotifier>
#include <spnav.h>
/**
@brief SpnavBackend::SpnavBackend
Try to connect to spacenavd. Failure -- no daemon running, no device
attached -- is left silent: it is the expected state for most users and
must never surface as an error dialog or a log warning on every
ordinary startup.
@param parent
*/
SpnavBackend::SpnavBackend(QObject *parent) :
SpaceMouseBackend(parent)
{
if (spnav_open() == -1) {
return;
}
m_available = true;
m_notifier = new QSocketNotifier(spnav_fd(), QSocketNotifier::Read, this);
connect(m_notifier, &QSocketNotifier::activated,
this, &SpnavBackend::readEvents);
}
/**
@brief SpnavBackend::~SpnavBackend
*/
SpnavBackend::~SpnavBackend()
{
if (m_available) {
spnav_close();
}
}
/**
@brief SpnavBackend::readEvents
Called when the spacenavd socket has data available. Drains every event
currently queued -- spnav_poll_event() returns 0 once the queue is
empty -- rather than handling just one per activation, so events cannot
silently back up if several arrive between two Qt event loop turns.
*/
void SpnavBackend::readEvents()
{
spnav_event event;
while (spnav_poll_event(&event))
{
if (event.type == SPNAV_EVENT_MOTION) {
SpaceMouseSample sample;
sample.x = event.motion.x;
sample.y = event.motion.y;
sample.z = event.motion.z;
sample.rx = event.motion.rx;
sample.ry = event.motion.ry;
sample.rz = event.motion.rz;
emit motion(sample);
}
else if (event.type == SPNAV_EVENT_BUTTON && event.button.press) {
//Release (press == 0) is not reported -- SpaceMouseListener
//triggers a QET action on press, the same way a keyboard
//shortcut triggers on key-down, and has no use for release.
emit buttonPressed(event.button.bnum);
}
}
}