Files
qelectrotech-source-mirror/sources/spacemouse/hidbackend.h
T
ispyisail aaeaff55cc Add a hidapi 3D mouse backend that needs no 3Dconnexion driver
The 3D mouse only worked on Linux, through spacenavd. This adds a second
backend that reads the device directly over USB through hidapi, with no
3Dconnexion driver or SDK: the route to Windows and macOS (discussion
#599), and usable on Linux without spacenavd.

SpaceMouseHid decodes the raw reports from the device's own report
descriptor -- where each axis and button sits, its range, absolute or
relative -- so no per-model table is needed, with the classic report
1/2/3 layout as a fallback when the descriptor cannot be read and the
0x1c button list newer devices send. Absolute axes are rescaled to
+-500 exactly as spacenavd does, so both backends give QET the same
values. HidBackend polls from the main thread (fast while moving, slow
when still), emits one sample per poll, and looks for a device every 3 s
so plugging one in or back in needs no restart.

QET_SPACEMOUSE_BACKEND (auto, spnav, hid) picks the backend; auto keeps
libspnav on Linux when it is found and uses hidapi otherwise. hidapi is
found through pkg-config as hidapi-hidraw (Linux) or hidapi (MSYS2,
Homebrew).

A sample arriving in the same millisecond as the previous one now counts
for no time instead of a full period, so a burst of queued samples no
longer moves the view further than the time it covers.

Tested without a device: tst_spacemousehid (descriptor parsing, broken
and hostile descriptors, every report form, recordings from real devices
once they are added to fixtures/spacemouse), and end to end on Linux
through a virtual USB device created with /dev/uhid: the same moves give
byte-identical screenshots through the hidapi and libspnav backends, an
absolute axis is rescaled as spacenavd does, buttons trigger their
bound action, and unplugging and replugging while QET runs (including
with a dialog open that a device button opened) reconnects cleanly.
Not tested on Windows, macOS or real hardware.

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

70 lines
2.1 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/>.
*/
#ifndef HIDBACKEND_H
#define HIDBACKEND_H
#include "spacemousebackend.h"
#include "spacemousehid.h"
class QTimer;
struct hid_device_;
/**
@brief The HidBackend class
SpaceMouseBackend that reads the device directly over USB through
hidapi, with no 3Dconnexion driver or SDK: the backend for Windows and
macOS, and for Linux without spacenavd. Only compiled in when
QET_SPACEMOUSE_BACKEND_HID is defined (cmake/find_spacemouse.cmake).
The raw reports are decoded by SpaceMouseHid, from the device's own
report descriptor, into the same values spacenavd would give.
hidapi has no event to wait on, so the device is polled from the main
thread: fast while it is moving, slowly once it has been still for a
moment. With no device, it looks for one every few seconds, so plugging
one in works without restarting QElectroTech. As with every backend,
no device is the normal case and is never reported as an error.
*/
class HidBackend : public SpaceMouseBackend
{
Q_OBJECT
public:
explicit HidBackend(QObject *parent = nullptr);
~HidBackend() override;
bool isAvailable() const override { return m_device != nullptr; }
private slots:
void scan();
void readReports();
private:
void close();
bool m_hid_initialised = false;
hid_device_ *m_device = nullptr;
SpaceMouseHid::Decoder m_decoder;
QTimer *m_scan_timer = nullptr;
QTimer *m_read_timer = nullptr;
int m_idle_polls = 0;
bool m_reading = false;
};
#endif // HIDBACKEND_H