Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Fix the macOS launch hang with the 3Dconnexion driver installed (#2841)

abteenz1666ca7018b8
diff --git a/apps/solidcraft/src/space_mouse.rs b/apps/solidcraft/src/space_mouse.rs
index b524c4a85e..00568cf085 100644
--- a/apps/solidcraft/src/space_mouse.rs
+++ b/apps/solidcraft/src/space_mouse.rs
@@ -362,25 +362,31 @@ fn reader(ctx: Context, shared: Arc<Mutex<Reading>>) {
fn start_driver_client(ctx: &Context, shared: &Arc<Mutex<Reading>>) {
use solidcraft_3dx_client::Event;
let (ctx, shared) = (ctx.clone(), Arc::clone(shared));
- let started = solidcraft_3dx_client::start("SolidCraft", move |event| match event {
- Event::DeviceAdded => DRIVER_CONNECTED.store(true, Ordering::Relaxed),
- Event::DeviceRemoved => {
- DRIVER_CONNECTED.store(false, Ordering::Relaxed);
- reset(&ctx, &shared);
- }
- Event::Axes { translation, rotation } => {
- if let Ok(mut reading) = shared.lock() {
- let now = Instant::now();
- reading.axes = Axes { translation, rotation };
- reading.at = [now; 6];
- ctx.request_repaint();
+ let started = solidcraft_3dx_client::start(
+ "SolidCraft",
+ move |event| match event {
+ Event::DeviceAdded => DRIVER_CONNECTED.store(true, Ordering::Relaxed),
+ Event::DeviceRemoved => {
+ DRIVER_CONNECTED.store(false, Ordering::Relaxed);
+ reset(&ctx, &shared);
}
- }
- Event::Buttons(_) => {}
- });
- match started {
- Ok(()) => DRIVER_REGISTERED.store(true, Ordering::Relaxed),
- Err(error) => log::info!("3D mouse: no driver client: {error}"),
+ Event::Axes { translation, rotation } => {
+ if let Ok(mut reading) = shared.lock() {
+ let now = Instant::now();
+ reading.axes = Axes { translation, rotation };
+ reading.at = [now; 6];
+ ctx.request_repaint();
+ }
+ }
+ Event::Buttons(_) => {}
+ },
+ |connected| match connected {
+ Ok(()) => DRIVER_REGISTERED.store(true, Ordering::Relaxed),
+ Err(error) => log::info!("3D mouse: no driver client: {error}"),
+ },
+ );
+ if let Err(error) = started {
+ log::info!("3D mouse: no driver client: {error}");
}
}
diff --git a/crates/threedx-client/examples/dump.rs b/crates/threedx-client/examples/dump.rs
index 172e4c6b1d..64cd30b879 100644
--- a/crates/threedx-client/examples/dump.rs
+++ b/crates/threedx-client/examples/dump.rs
@@ -3,7 +3,15 @@
//! use SolidCraft's own `.app` to see the cap move.
fn main() {
- if let Err(error) = solidcraft_3dx_client::start("SolidCraft dump", |event| println!("{event:?}")) {
+ if let Err(error) = solidcraft_3dx_client::start(
+ "SolidCraft dump",
+ |event| println!("{event:?}"),
+ |connected| {
+ if let Err(error) = connected {
+ eprintln!("{error}");
+ }
+ },
+ ) {
eprintln!("{error}");
return;
}
diff --git a/crates/threedx-client/src/lib.rs b/crates/threedx-client/src/lib.rs
index 77ef94f756..15082ed998 100644
--- a/crates/threedx-client/src/lib.rs
+++ b/crates/threedx-client/src/lib.rs
@@ -67,15 +67,21 @@ impl std::error::Error for Error {}
/// Connects to the driver and calls `sink` (on the client's own thread) for everything it reports.
/// `name` is how the driver lists this app. Only one client per process; the connection lasts
-/// until the process ends.
-pub fn start(name: &str, sink: impl Fn(Event) + Send + Sync + 'static) -> Result<(), Error> {
+/// until the process ends. Returns once the client thread runs, without waiting for the driver
+/// (which needs the main thread to register the client); `connected` gets that outcome later, on
+/// the client's thread.
+pub fn start(
+ name: &str,
+ sink: impl Fn(Event) + Send + Sync + 'static,
+ connected: impl FnOnce(Result<(), Error>) + Send + 'static,
+) -> Result<(), Error> {
#[cfg(target_os = "macos")]
{
- macos::start(name, Box::new(sink))
+ macos::start(name, Box::new(sink), Box::new(connected))
}
#[cfg(not(target_os = "macos"))]
{
- let _ = (name, sink);
+ let _ = (name, sink, connected);
Err(Error::Unsupported)
}
}
diff --git a/crates/threedx-client/src/macos.rs b/crates/threedx-client/src/macos.rs
index d12c7d66a7..5549425dd9 100644
--- a/crates/threedx-client/src/macos.rs
+++ b/crates/threedx-client/src/macos.rs
@@ -53,6 +53,7 @@ type RegisterClient = unsafe extern "C" fn(u32, *const u8, u16, u32) -> u16;
type SetMask = unsafe extern "C" fn(u16, u32);
type Sink = Box<dyn Fn(Event) + Send + Sync>;
+type Connected = Box<dyn FnOnce(Result<(), Error>) + Send>;
static SINK: OnceLock<Sink> = OnceLock::new();
/// The id the driver gave this client: it broadcasts every message to every client.
@@ -157,17 +158,19 @@ fn connect(name: &str) -> Result<(), Error> {
Ok(())
}
-pub fn start(name: &str, sink: Sink) -> Result<(), Error> {
+/// Starts the client thread and returns without waiting for the driver: its registration
+/// waits for the main thread, which is usually the caller (a wait here hangs the app at launch).
+/// `connected` gets the outcome on the client thread.
+pub fn start(name: &str, sink: Sink, connected: Connected) -> Result<(), Error> {
SINK.set(sink).map_err(|_| Error::AlreadyStarted)?;
let name = name.to_owned();
- let (ready, started) = std::sync::mpsc::channel();
std::thread::Builder::new()
.name("3dx-client".into())
.spawn(move || {
let result = connect(&name);
- let connected = result.is_ok();
- let _ = ready.send(result);
- if !connected {
+ let ok = result.is_ok();
+ connected(result);
+ if !ok {
return;
}
// The driver's messages arrive on this thread's run loop. Waking up now and then keeps
@@ -177,8 +180,8 @@ pub fn start(name: &str, sink: Sink) -> Result<(), Error> {
unsafe { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0, 0) };
}
})
- .map_err(|error| Error::Thread(error.to_string()))?;
- started.recv().map_err(|error| Error::Thread(error.to_string()))?
+ .map(|_| ())
+ .map_err(|error| Error::Thread(error.to_string()))
}
#[cfg(test)]