spacemouse 0.1.0

Cross-platform HID Driver for 3Dconnexion SpaceMouse Devices
Documentation
//! Background capture thread running a [`SpaceMouse`].

use crate::event::SpaceMouseEvent;
use crate::spacemouse::SpaceMouse;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::{Duration, Instant};

/// Default wait between (re-)scans while no compatible device is open.
const DEFAULT_RESCAN_INTERVAL: Duration = Duration::from_secs(2);

/// Lower bound for a configured rescan interval, to avoid churning on device
/// enumeration.
const MIN_RESCAN_INTERVAL: Duration = Duration::from_millis(100);

/// Blocking-read timeout while a device is open. Chosen short enough that a
/// stop or LED request is noticed promptly, long enough to avoid
/// busy-looping the background thread when the device is idle.
const READ_TIMEOUT: Duration = Duration::from_millis(200);

/// Request sent from a [`SpaceMouseThread`] handle to its background thread.
enum Command {
  /// Set whether the device's LED is lit.
  SetLed(bool),
  /// Stop the thread.
  Stop,
}

/// Cross-platform SpaceMouse HID capture thread.
///
/// Handle to a background thread, which continuously
/// (re-)discovers a compatible device, reads its raw HID reports, and
/// forwards the resulting [`SpaceMouseEvent`]s to the callback given to
/// [`SpaceMouseThread::new`]. The thread stops when the handle is dropped.
/// Constructing one is a safe no-op on a machine without a compatible device
/// connected. A device that is plugged in later, or unplugged and replugged,
/// is picked up automatically. If the HID subsystem cannot be initialized,
/// the thread ends immediately and reports nothing.
///
/// # Example
///
/// Print all events of a connected device until Enter is pressed:
///
/// ```no_run
/// use spacemouse::{SpaceMouseEvent, SpaceMouseThread};
/// use std::io;
///
/// fn on_event_callback(event: SpaceMouseEvent) {
///   match event {
///     SpaceMouseEvent::Connected => {
///       println!("connected");
///     }
///     SpaceMouseEvent::Disconnected => {
///       println!("disconnected");
///     }
///     SpaceMouseEvent::Translation { x, y, z } => {
///       println!("translation: {x} {y} {z}");
///     }
///     SpaceMouseEvent::Rotation { x, y, z } => {
///       println!("rotation: {x} {y} {z}");
///     }
///     _ => {}
///   }
/// }
///
/// // Start the background thread, which automatically connects to the device.
/// let thread = SpaceMouseThread::new(on_event_callback);
///
/// println!("Listening for SpaceMouse events. Press Enter to quit.");
/// let _ = io::stdin().read_line(&mut String::new());
///
/// // Dropping the handle stops the background thread.
/// drop(thread);
/// ```
pub struct SpaceMouseThread {
  /// Whether a compatible device is currently open. Kept outside the
  /// thread so [`SpaceMouseThread::is_connected`] doesn't have to
  /// synchronize with it via the callback.
  connected: Arc<AtomicBool>,
  /// Sends requests into the background thread - see
  /// [`SpaceMouseThread::set_led`] and [`Drop::drop`].
  command_sender: mpsc::Sender<Command>,
  /// The background thread, joined on drop. Always `Some` until
  /// [`Drop::drop`] runs.
  thread: Option<thread::JoinHandle<()>>,
}

impl SpaceMouseThread {
  /// Start capturing in a new background thread.
  ///
  /// `on_event` is invoked on the background thread, *not* the caller's
  /// thread, for every [`SpaceMouseEvent`]. Callers that need events on a
  /// specific thread are responsible for the hop themselves.
  ///
  /// Waits between scans (while no compatible device is open) using the
  /// default scan interval. See [`SpaceMouseThread::with_rescan_interval`]
  /// for a configurable scan period.
  pub fn new<F>(on_event: F) -> Self
  where
    F: Fn(SpaceMouseEvent) + Send + 'static,
  {
    Self::with_rescan_interval(DEFAULT_RESCAN_INTERVAL, on_event)
  }

  /// Like [`SpaceMouseThread::new`], but waits `rescan_interval` between scans
  /// while no compatible device is open.
  ///
  /// Shorter intervals pick up a newly connected device sooner, at the cost
  /// of enumerating the system's HID devices more often. Values below 100 ms
  /// are raised to 100 ms.
  pub fn with_rescan_interval<F>(rescan_interval: Duration, on_event: F) -> Self
  where
    F: Fn(SpaceMouseEvent) + Send + 'static,
  {
    let rescan_interval = rescan_interval.max(MIN_RESCAN_INTERVAL);
    let connected = Arc::new(AtomicBool::new(false));
    let (command_sender, commands) = mpsc::channel();
    let thread_connected = Arc::clone(&connected);
    let thread = thread::spawn(move || {
      run(&thread_connected, &commands, rescan_interval, &on_event);
    });
    Self {
      connected,
      command_sender,
      thread: Some(thread),
    }
  }

  /// Whether a compatible device is currently open.
  pub fn is_connected(&self) -> bool {
    self.connected.load(Ordering::Relaxed)
  }

  /// Set whether the device's LED is lit.
  ///
  /// Applied immediately if a device is currently open, and reapplied
  /// whenever a device is (re)opened. Devices without an LED and write
  /// failures are silently ignored.
  pub fn set_led(&self, enabled: bool) {
    // Send only fails once the background thread has exited, e.g. during drop.
    let _ = self.command_sender.send(Command::SetLed(enabled));
  }
}

impl Drop for SpaceMouseThread {
  fn drop(&mut self) {
    // Send only fails if the background thread has already exited.
    let _ = self.command_sender.send(Command::Stop);
    if let Some(t) = self.thread.take() {
      // Only fails if the background thread itself panicked; there is no
      // sensible way to surface that from a `Drop` impl, so it's dropped
      // here rather than propagated.
      let _ = t.join();
    }
  }
}

/// Outcome of draining the pending commands.
struct Drained {
  /// Whether the desired LED state changed.
  led_changed: bool,
  /// Whether a stop was requested.
  stop: bool,
}

/// Apply all pending commands without blocking.
fn drain_commands(
  commands: &mpsc::Receiver<Command>,
  desired_led: &mut bool,
) -> Drained {
  let mut drained = Drained {
    led_changed: false,
    stop: false,
  };
  loop {
    match commands.try_recv() {
      Ok(Command::SetLed(enabled)) => {
        if enabled != *desired_led {
          *desired_led = enabled;
          drained.led_changed = true;
        }
      }
      Ok(Command::Stop) | Err(mpsc::TryRecvError::Disconnected) => {
        drained.stop = true;
        break;
      }
      Err(mpsc::TryRecvError::Empty) => break,
    }
  }
  drained
}

/// Wait up to `timeout` for a stop request, applying LED commands that
/// arrive in the meantime. Returns whether a stop was requested.
fn wait_for_stop(
  commands: &mpsc::Receiver<Command>,
  timeout: Duration,
  desired_led: &mut bool,
) -> bool {
  let deadline = Instant::now() + timeout;
  loop {
    let remaining = deadline.saturating_duration_since(Instant::now());
    match commands.recv_timeout(remaining) {
      Ok(Command::SetLed(enabled)) => *desired_led = enabled,
      Ok(Command::Stop) | Err(mpsc::RecvTimeoutError::Disconnected) => {
        return true;
      }
      Err(mpsc::RecvTimeoutError::Timeout) => return false,
    }
  }
}

/// Body of the background capture thread.
///
/// Repeatedly looks for a compatible device, reads from it until it's lost
/// (or a stop is requested), then goes back to looking for one - so a
/// hot-unplugged/replugged device recovers on its own without recreating
/// [`SpaceMouseThread`].
fn run<F>(
  connected: &AtomicBool,
  commands: &mpsc::Receiver<Command>,
  rescan_interval: Duration,
  on_event: &F,
) where
  F: Fn(SpaceMouseEvent),
{
  // Failing to initialize the HID subsystem is not expected to recover.
  let Ok(mut mouse) = SpaceMouse::new() else {
    return;
  };

  // LED state is connection-bound
  let mut desired_led = true;

  loop {
    // Track any LED command(s) that arrive even if no device is connected.
    // This ensures that when a device is connected, its LED is set to the
    // correct state.
    if drain_commands(commands, &mut desired_led).stop {
      return;
    }

    if !mouse.connect() {
      if wait_for_stop(commands, rescan_interval, &mut desired_led) {
        return;
      }
      continue;
    }

    connected.store(true, Ordering::Relaxed);
    on_event(SpaceMouseEvent::Connected);
    let _ = mouse.set_led(desired_led);

    loop {
      // Apply any pending LED change before honoring a stop request, so a
      // command sent just before shutdown still reaches the device.
      let drained = drain_commands(commands, &mut desired_led);
      if drained.led_changed {
        let _ = mouse.set_led(desired_led);
      }
      if drained.stop {
        connected.store(false, Ordering::Relaxed);
        on_event(SpaceMouseEvent::Disconnected);
        return;
      }
      match mouse.read(READ_TIMEOUT) {
        Ok(Some(event)) => on_event(event),
        // Timed out or unrecognized report - just loop around to re-check
        // commands.
        Ok(None) => {}
        // The device is gone (unplugged, I/O error, ...): stop reading and
        // go back to scanning for a (possibly different) device.
        Err(_) => break,
      }
    }

    connected.store(false, Ordering::Relaxed);
    on_event(SpaceMouseEvent::Disconnected);

    // Make sure there is a delay in the reconnect loop, to avoid hot-looping.
    if wait_for_stop(commands, rescan_interval, &mut desired_led) {
      return;
    }
  }
}