use brep_render::controls::SixDofMotion;
use brep_render::engine_state::EngineState;
use brep_render::style::RenderSettings;
use std::sync::Mutex;
pub const FULL_SCALE: f64 = 350.0;
const DEADZONE: f64 = 0.04;
const ROTATE_RAD_PER_S: f64 = 2.0;
const PAN_VIEWS_PER_S: f64 = 1.0;
const ZOOM_E_PER_S: f64 = 1.5;
const MAX_DT: f64 = 0.05;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Report {
Translation([i16; 3]),
Rotation([i16; 3]),
Motion([i16; 6]),
Buttons(u32),
}
fn axes<const N: usize>(payload: &[u8]) -> [i16; N] {
std::array::from_fn(|i| i16::from_le_bytes([payload[2 * i], payload[2 * i + 1]]))
}
pub fn parse(report_id: u8, payload: &[u8]) -> Option<Report> {
match report_id {
1 if payload.len() >= 12 => Some(Report::Motion(axes(payload))),
1 if payload.len() >= 6 => Some(Report::Translation(axes(payload))),
2 if payload.len() >= 6 => Some(Report::Rotation(axes(payload))),
3 if !payload.is_empty() => {
let mut bits = [0u8; 4];
let n = payload.len().min(4);
bits[..n].copy_from_slice(&payload[..n]);
Some(Report::Buttons(u32::from_le_bytes(bits)))
}
_ => None,
}
}
pub fn parse_numbered(bytes: &[u8]) -> Option<Report> {
let (&id, payload) = bytes.split_first()?;
parse(id, payload)
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Hub {
pub axes: [i16; 6],
pub buttons: u32,
pressed: u32,
pub device: Option<String>,
pub status: Option<String>,
pub reports: u64,
}
impl Hub {
pub fn apply(&mut self, report: Report) {
match report {
Report::Translation(t) => self.axes[..3].copy_from_slice(&t),
Report::Rotation(r) => self.axes[3..].copy_from_slice(&r),
Report::Motion(m) => self.axes = m,
Report::Buttons(b) => {
self.pressed |= b & !self.buttons;
self.buttons = b;
}
}
self.reports += 1;
}
}
static HUB: Mutex<Hub> = Mutex::new(Hub {
axes: [0; 6],
buttons: 0,
pressed: 0,
device: None,
status: None,
reports: 0,
});
static REPAINT: Mutex<Option<egui::Context>> = Mutex::new(None);
fn hub() -> std::sync::MutexGuard<'static, Hub> {
HUB.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn wake() {
if let Some(ctx) = REPAINT.lock().ok().and_then(|c| c.clone()) {
ctx.request_repaint();
}
}
pub fn install(ctx: &egui::Context) {
if let Ok(mut slot) = REPAINT.lock() {
*slot = Some(ctx.clone());
}
#[cfg(all(not(target_arch = "wasm32"), feature = "spacemouse"))]
native::start();
}
pub fn feed(report_id: u8, payload: &[u8]) -> bool {
let Some(report) = parse(report_id, payload) else { return false };
hub().apply(report);
wake();
true
}
pub fn set_device(name: Option<String>) {
let mut hub = hub();
if name.is_none() {
hub.axes = [0; 6];
hub.buttons = 0;
} else {
hub.status = None;
}
hub.device = name;
drop(hub);
wake();
}
pub fn set_status(status: Option<String>) {
hub().status = status;
wake();
}
pub fn snapshot() -> Hub {
hub().clone()
}
pub fn status_line() -> String {
let hub = hub();
match (&hub.device, &hub.status) {
(Some(name), _) => format!("Connected: {name}"),
(None, Some(status)) => status.clone(),
#[cfg(target_arch = "wasm32")]
(None, None) => "No device connected. Connect grants this page access to one.".into(),
#[cfg(not(target_arch = "wasm32"))]
(None, None) => "No device found. A SpaceMouse is picked up when it is plugged in.".into(),
}
}
pub fn request_device() {
#[cfg(target_arch = "wasm32")]
web::request();
}
fn axis(raw: i16, invert: bool) -> f64 {
let v = (f64::from(raw) / FULL_SCALE).clamp(-1.5, 1.5);
let magnitude = ((v.abs() - DEADZONE) / (1.0 - DEADZONE)).max(0.0);
let v = magnitude.copysign(v);
if invert { -v } else { v }
}
pub fn motion(axes_raw: [i16; 6], settings: &RenderSettings, dt: f64, view_px: f64) -> SixDofMotion {
let speed = f64::from(settings.spacemouse_sensitivity) * dt.clamp(0.0, MAX_DT);
let a: [f64; 6] = std::array::from_fn(|i| axis(axes_raw[i], settings.spacemouse_invert[i]));
let pan_px = PAN_VIEWS_PER_S * view_px.max(1.0) * speed;
let rot = ROTATE_RAD_PER_S * speed;
SixDofMotion {
pan: [a[0] * pan_px, a[2] * pan_px],
zoom: (-a[1] * ZOOM_E_PER_S * speed).exp(),
rotate: [a[3] * rot, -a[5] * rot, a[4] * rot],
}
}
pub fn drive(ctx: &egui::Context, state: &mut EngineState) {
let (axes_raw, pressed) = {
let mut hub = hub();
(hub.axes, std::mem::take(&mut hub.pressed))
};
if !state.settings.spacemouse_enabled {
return;
}
#[cfg(not(target_arch = "wasm32"))]
if !ctx.input(|i| i.focused) {
return;
}
if pressed & 1 != 0 && !state.camera_rotation_locked() {
state.standard_view("ISO");
}
if pressed & 2 != 0 {
state.zoom_to_fit();
}
if axes_raw == [0; 6] {
return;
}
let dt = f64::from(ctx.input(|i| i.stable_dt));
let view_px = state.camera.width.min(state.camera.height);
let m = motion(axes_raw, &state.settings, dt, view_px);
if !m.is_idle() {
state.six_dof(&m);
}
ctx.request_repaint();
}
#[cfg(all(not(target_arch = "wasm32"), feature = "spacemouse"))]
mod native {
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
static STARTED: AtomicBool = AtomicBool::new(false);
const NOT_SIX_DOF: &[u16] = &[0xc650, 0xc651, 0xc654, 0xc655, 0xc656, 0xc657, 0xc658, 0xc664, 0xc668, 0xc665, 0xc62c, 0xc641];
const LOGITECH_SIX_DOF: &[u16] = &[0xc603, 0xc605, 0xc606, 0xc621, 0xc623, 0xc625, 0xc626, 0xc627, 0xc628, 0xc629, 0xc62b, 0xc640];
pub(super) fn is_six_dof(vendor: u16, product: u16, usage_page: u16, usage: u16) -> bool {
let multi_axis = (usage_page, usage) == (1, 8);
match vendor {
0x046d => LOGITECH_SIX_DOF.contains(&product),
0x256f if usage_page != 0 => multi_axis,
0x256f => !NOT_SIX_DOF.contains(&product),
_ => multi_axis,
}
}
pub(super) fn start() {
if STARTED.swap(true, Ordering::SeqCst) {
return;
}
let _ = std::thread::Builder::new().name("spacemouse".into()).spawn(run);
}
fn run() {
let mut api = match hidapi::HidApi::new() {
Ok(api) => api,
Err(err) => {
super::set_status(Some(format!("SpaceMouse support is unavailable: {err}")));
return;
}
};
let mut last_failure: Option<String> = None;
loop {
let _ = api.refresh_devices();
let mut found: Vec<&hidapi::DeviceInfo> = api
.device_list()
.filter(|d| is_six_dof(d.vendor_id(), d.product_id(), d.usage_page(), d.usage()))
.collect();
found.sort_by_key(|d| (d.usage_page(), d.usage()) != (1, 8));
let Some(info) = found.first().map(|d| (*d).clone()) else {
std::thread::sleep(Duration::from_secs(2));
continue;
};
let name = info.product_string().filter(|s| !s.is_empty()).unwrap_or("SpaceMouse").to_string();
match info.open_device(&api) {
Ok(device) => {
last_failure = None;
super::set_device(Some(name));
read(&device);
super::set_device(None);
}
Err(err) => {
let message = failure(&name, &err.to_string());
if last_failure.as_deref() != Some(message.as_str()) {
super::set_status(Some(message.clone()));
last_failure = Some(message);
}
std::thread::sleep(Duration::from_secs(3));
}
}
}
}
fn read(device: &hidapi::HidDevice) {
let mut buf = [0u8; 64];
loop {
match device.read_timeout(&mut buf, 500) {
Ok(0) => {}
Ok(n) => {
if let Some(report) = super::parse_numbered(&buf[..n]) {
super::hub().apply(report);
super::wake();
}
}
Err(_) => return,
}
}
}
fn failure(name: &str, err: &str) -> String {
let hint = if cfg!(target_os = "linux") {
" On Linux, give your user access with a udev rule, e.g. \
/etc/udev/rules.d/99-spacemouse.rules: \
KERNEL==\"hidraw*\", ATTRS{idVendor}==\"256f\", MODE=\"0660\", TAG+=\"uaccess\" \
(and 046d for older Logitech-branded devices), then replug it."
} else {
""
};
format!("Found {name} but could not open it: {err}.{hint}")
}
}
#[cfg(target_arch = "wasm32")]
pub mod web {
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
#[wasm_bindgen]
pub fn spacemouse_report(report_id: u8, data: &[u8]) -> bool {
super::feed(report_id, data)
}
#[wasm_bindgen]
pub fn spacemouse_device(name: Option<String>) {
super::set_device(name);
}
#[wasm_bindgen]
pub fn spacemouse_status(text: String) {
super::set_status(Some(text));
}
pub(super) fn request() {
let shim = web_sys::window()
.and_then(|w| js_sys::Reflect::get(w.as_ref(), &JsValue::from_str("__brepSpaceMouse")).ok())
.filter(|v| v.is_object());
let request = shim
.as_ref()
.and_then(|s| js_sys::Reflect::get(s, &JsValue::from_str("request")).ok())
.and_then(|f| f.dyn_into::<js_sys::Function>().ok());
match (shim, request) {
(Some(shim), Some(request)) => {
let _ = request.call0(&shim);
}
_ => super::set_status(Some("SpaceMouse support did not load on this page.".into())),
}
}
}