From 5207d961d9cd53098012c0729125562f2c5b6dc9 Mon Sep 17 00:00:00 2001 From: William Light Date: Fri, 11 Sep 2020 18:18:39 +0200 Subject: [PATCH] x11: run window in a separate thread there's a thread.join() to mimic the previous semantics (which are the same as on the other platforms). --- examples/open_window.rs | 4 ++-- src/lib.rs | 6 ++++-- src/x11/window.rs | 13 +++++++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/examples/open_window.rs b/examples/open_window.rs index 04e2b73..87b82a4 100644 --- a/examples/open_window.rs +++ b/examples/open_window.rs @@ -2,7 +2,7 @@ use baseview::{Event, Window, WindowHandler}; fn main() { let window_open_options = baseview::WindowOpenOptions { - title: "baseview", + title: "baseview".into(), width: 512, height: 512, parent: baseview::Parent::None, @@ -22,7 +22,7 @@ impl WindowHandler for MyProgram { fn on_frame(&mut self) {} - fn on_event(&mut self, window: &mut Window, event: Event) { + fn on_event(&mut self, _window: &mut Window, event: Event) { match event { Event::Mouse(e) => println!("Mouse event: {:?}", e), Event::Keyboard(e) => println!("Keyboard event: {:?}", e), diff --git a/src/lib.rs b/src/lib.rs index ce1fde1..d03dc60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,8 +28,10 @@ pub enum Parent { WithParent(*mut c_void), } -pub struct WindowOpenOptions<'a> { - pub title: &'a str, +unsafe impl Send for Parent {} + +pub struct WindowOpenOptions { + pub title: String, pub width: usize, pub height: usize, diff --git a/src/x11/window.rs b/src/x11/window.rs index dc28bc0..a0ad71d 100644 --- a/src/x11/window.rs +++ b/src/x11/window.rs @@ -1,5 +1,6 @@ use std::os::raw::{c_ulong, c_void}; use std::time::*; +use std::thread; use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle}; @@ -25,6 +26,16 @@ pub struct WindowHandle; impl Window { pub fn open(options: WindowOpenOptions) -> WindowHandle { + let runner = thread::spawn(move || { + Self::window_thread::(options); + }); + + let _ = runner.join(); + + WindowHandle + } + + fn window_thread(options: WindowOpenOptions) { // Connect to the X server // FIXME: baseview error type instead of unwrap() let xcb_connection = XcbConnection::new().unwrap(); @@ -119,8 +130,6 @@ impl Window { let mut handler = H::build(&mut window); window.run_event_loop(&mut handler); - - WindowHandle } #[inline]