Fix the macOS launch hang with the 3Dconnexion driver installed (#2841)
Thanks @abteenz.
This commit is contained in:
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user