/*
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 .
*/
#include "spnavbackend.h"
#include
#include
/**
@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) {
//A 6-DOF device also reports rotation (rx, ry, rz); QET's
//view has nothing rotation maps to, so those three axes are
//read by nothing here.
emit motion(event.motion.x, event.motion.y, event.motion.z);
}
//Button events (SPNAV_EVENT_BUTTON) are deliberately not
//handled: mapping device buttons to QET actions is the "Related,
//not proposed here" follow-up in discussion #599, not this
//phase.
}
}