From 76e5851b62ec183afa47606f6d0062ef6b53dc51 Mon Sep 17 00:00:00 2001 From: William Light Date: Fri, 11 Sep 2020 19:37:46 +0200 Subject: [PATCH] x11: don't return from Window::open() until the window is actually open --- src/x11/window.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/x11/window.rs b/src/x11/window.rs index 6f7fcb8..f57534a 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -1,4 +1,5 @@ use std::os::raw::{c_ulong, c_void}; +use std::sync::mpsc; use std::time::*; use std::thread; @@ -31,17 +32,27 @@ impl WindowHandle { } } +type WindowOpenResult = Result<(), ()>; impl Window { pub fn open(options: WindowOpenOptions) -> WindowHandle { + let (tx, rx) = mpsc::sync_channel::(1); + + let thread = thread::spawn(move || { + if let Err(e) = Self::window_thread::(options, tx.clone()) { + let _ = tx.send(Err(e)); + } + }); + + // FIXME: placeholder types for returning errors in the future + let _ = rx.recv(); + WindowHandle { - thread: thread::spawn(move || { - Self::window_thread::(options); - }) + thread } } - fn window_thread(options: WindowOpenOptions) { + fn window_thread(options: WindowOpenOptions, tx: mpsc::SyncSender) -> WindowOpenResult { // Connect to the X server // FIXME: baseview error type instead of unwrap() let xcb_connection = XcbConnection::new().unwrap(); @@ -135,7 +146,10 @@ impl Window { let mut handler = H::build(&mut window); + let _ = tx.send(Ok(())); + window.run_event_loop(&mut handler); + Ok(()) } #[inline] @@ -227,7 +241,7 @@ impl Window { xcb::CLIENT_MESSAGE => { let event = unsafe { xcb::cast_event::(&event) }; - // what an absolute trajedy this all is + // what an absolute tragedy this all is let data = event.data().data; let (_, data32, _) = unsafe { data.align_to::() };