spacemouse 0.1.0

Cross-platform HID Driver for 3Dconnexion SpaceMouse Devices
Documentation
  • Size
  • Source code size: 34.0 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 297.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • LibrePCB/spacemouse-rs
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ubruhin

SpaceMouse Driver

Cross-platform (Windows/macOS/Linux) driver for 3Dconnexion SpaceMouse and compatible 3D-mouse devices. Accesses the device by raw HID capture (through the hidapi crate), thus doesn't require any additional software or driver to be installed. In particular, neither 3DxWare nor spacenavd is needed.

The crate was built for LibrePCB's SpaceMouse support, but usable by any application.

Example

use spacemouse::{SpaceMouseEvent, SpaceMouse};
use std::time::Duration;

let mut mouse = SpaceMouse::new()?;

// Poll for the device, e.g. every second, until it is found.
while !mouse.connect() {
  std::thread::sleep(Duration::from_secs(1));
}

// Read events until the device is lost (e.g. unplugged).
loop {
  match mouse.read(Duration::from_millis(200))? {
    Some(SpaceMouseEvent::Translation { x, y, z }) => {
      println!("translation: {x} {y} {z}");
    }
    Some(SpaceMouseEvent::Rotation { x, y, z }) => {
      println!("rotation: {x} {y} {z}");
    }
    _ => {}
  }
}

In addition to this synchronous API, this crate also offers a threaded API which handles all the high-level logic and just calls your callback on any event. See SpaceMouseThread for details.

Ready-to-run programs are available in the examples directory:

cargo run --example synchronous
cargo run --example threaded

Documentation

Available at docs.rs.

Why hidapi

This crate depends on the hidapi crate for cross-platform HID access (hidraw on Linux, IOHIDManager on macOS, hid.dll on Windows).

On Linux, the pure-Rust hidraw backend of hidapi is used (the linux-native-basic-udev feature), so building and running this crate needs neither libudev nor libusb; no -dev packages have to be installed. On macOS and Windows, hidapi vendors a small amount of C source code (the underlying hidapi C library) and compiles it as part of cargo build, via the cc crate.

Linux Device Permissions

On Linux, hidraw device nodes are readable by root only by default. To allow accessing a SpaceMouse as a normal user, install the provided udev rule 70-spacemouse.rules:

sudo cp udev/70-spacemouse.rules /etc/udev/rules.d/
sudo udevadm control --reload-rules && sudo udevadm trigger

Then unplug and replug the device (or reboot) for the rule to take effect.

Status & Limitations

This library is in an early stage of development. The API is still subject to change, and only a small set of real devices has been tested so far. Feedback and bug reports are welcome.

Known limitations:

  • Only the classic USB HID devices are supported. Newer wireless models may not work.
  • Only translation and rotation events are reported. Button and other HID reports are silently ignored.
  • Only the first compatible device found is used — there is no way to select a specific device yet.
  • The hidapi feature flags are hardcoded in this crate for now. For more control over the used HID access method, they would need to be exposed as crate features.

License

Library is licensed under MIT license, see LICENSE for details.