1
0
Fork 0

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).
This commit is contained in:
William Light 2020-09-11 18:18:39 +02:00
parent 030388cf25
commit 5207d961d9
3 changed files with 17 additions and 6 deletions

View file

@ -2,7 +2,7 @@ use baseview::{Event, Window, WindowHandler};
fn main() { fn main() {
let window_open_options = baseview::WindowOpenOptions { let window_open_options = baseview::WindowOpenOptions {
title: "baseview", title: "baseview".into(),
width: 512, width: 512,
height: 512, height: 512,
parent: baseview::Parent::None, parent: baseview::Parent::None,
@ -22,7 +22,7 @@ impl WindowHandler for MyProgram {
fn on_frame(&mut self) {} 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 { match event {
Event::Mouse(e) => println!("Mouse event: {:?}", e), Event::Mouse(e) => println!("Mouse event: {:?}", e),
Event::Keyboard(e) => println!("Keyboard event: {:?}", e), Event::Keyboard(e) => println!("Keyboard event: {:?}", e),

View file

@ -28,8 +28,10 @@ pub enum Parent {
WithParent(*mut c_void), WithParent(*mut c_void),
} }
pub struct WindowOpenOptions<'a> { unsafe impl Send for Parent {}
pub title: &'a str,
pub struct WindowOpenOptions {
pub title: String,
pub width: usize, pub width: usize,
pub height: usize, pub height: usize,

View file

@ -1,5 +1,6 @@
use std::os::raw::{c_ulong, c_void}; use std::os::raw::{c_ulong, c_void};
use std::time::*; use std::time::*;
use std::thread;
use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{unix::XlibHandle, HasRawWindowHandle, RawWindowHandle};
@ -25,6 +26,16 @@ pub struct WindowHandle;
impl Window { impl Window {
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle { pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle {
let runner = thread::spawn(move || {
Self::window_thread::<H>(options);
});
let _ = runner.join();
WindowHandle
}
fn window_thread<H: WindowHandler>(options: WindowOpenOptions) {
// Connect to the X server // Connect to the X server
// FIXME: baseview error type instead of unwrap() // FIXME: baseview error type instead of unwrap()
let xcb_connection = XcbConnection::new().unwrap(); let xcb_connection = XcbConnection::new().unwrap();
@ -119,8 +130,6 @@ impl Window {
let mut handler = H::build(&mut window); let mut handler = H::build(&mut window);
window.run_event_loop(&mut handler); window.run_event_loop(&mut handler);
WindowHandle
} }
#[inline] #[inline]