linux: refactor LinuxPlatform to avoid recursive locking from the window callbacks
This commit is contained in:
parent
8d339ede1c
commit
282cc71df9
2 changed files with 49 additions and 34 deletions
|
@ -89,6 +89,8 @@ impl PlatformDispatcher for LinuxDispatcher {
|
||||||
let (_, runnable) = timed_tasks.pop().unwrap();
|
let (_, runnable) = timed_tasks.pop().unwrap();
|
||||||
runnable.run();
|
runnable.run();
|
||||||
ran = true;
|
ran = true;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ran
|
ran
|
||||||
|
|
|
@ -31,17 +31,19 @@ xcb::atoms_struct! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct LinuxPlatform(Mutex<LinuxPlatformState>);
|
pub(crate) struct LinuxPlatform {
|
||||||
|
|
||||||
pub(crate) struct LinuxPlatformState {
|
|
||||||
xcb_connection: Arc<xcb::Connection>,
|
xcb_connection: Arc<xcb::Connection>,
|
||||||
x_root_index: i32,
|
x_root_index: i32,
|
||||||
atoms: XcbAtoms,
|
atoms: XcbAtoms,
|
||||||
background_executor: BackgroundExecutor,
|
background_executor: BackgroundExecutor,
|
||||||
foreground_executor: ForegroundExecutor,
|
foreground_executor: ForegroundExecutor,
|
||||||
dispatcher: Arc<LinuxDispatcher>,
|
dispatcher: Arc<LinuxDispatcher>,
|
||||||
windows: HashMap<x::Window, Arc<LinuxWindowState>>,
|
|
||||||
text_system: Arc<LinuxTextSystem>,
|
text_system: Arc<LinuxTextSystem>,
|
||||||
|
state: Mutex<LinuxPlatformState>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct LinuxPlatformState {
|
||||||
|
windows: HashMap<x::Window, Arc<LinuxWindowState>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for LinuxPlatform {
|
impl Default for LinuxPlatform {
|
||||||
|
@ -57,52 +59,58 @@ impl LinuxPlatform {
|
||||||
|
|
||||||
let dispatcher = Arc::new(LinuxDispatcher::new());
|
let dispatcher = Arc::new(LinuxDispatcher::new());
|
||||||
|
|
||||||
Self(Mutex::new(LinuxPlatformState {
|
Self {
|
||||||
xcb_connection: Arc::new(xcb_connection),
|
xcb_connection: Arc::new(xcb_connection),
|
||||||
x_root_index,
|
x_root_index,
|
||||||
atoms,
|
atoms,
|
||||||
background_executor: BackgroundExecutor::new(dispatcher.clone()),
|
background_executor: BackgroundExecutor::new(dispatcher.clone()),
|
||||||
foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
|
foreground_executor: ForegroundExecutor::new(dispatcher.clone()),
|
||||||
dispatcher,
|
dispatcher,
|
||||||
windows: HashMap::default(),
|
|
||||||
text_system: Arc::new(LinuxTextSystem::new()),
|
text_system: Arc::new(LinuxTextSystem::new()),
|
||||||
}))
|
state: Mutex::new(LinuxPlatformState {
|
||||||
|
windows: HashMap::default(),
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Platform for LinuxPlatform {
|
impl Platform for LinuxPlatform {
|
||||||
fn background_executor(&self) -> BackgroundExecutor {
|
fn background_executor(&self) -> BackgroundExecutor {
|
||||||
self.0.lock().background_executor.clone()
|
self.background_executor.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn foreground_executor(&self) -> crate::ForegroundExecutor {
|
fn foreground_executor(&self) -> ForegroundExecutor {
|
||||||
self.0.lock().foreground_executor.clone()
|
self.foreground_executor.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
|
fn text_system(&self) -> Arc<dyn PlatformTextSystem> {
|
||||||
self.0.lock().text_system.clone()
|
self.text_system.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
|
fn run(&self, on_finish_launching: Box<dyn FnOnce()>) {
|
||||||
on_finish_launching();
|
on_finish_launching();
|
||||||
|
//Note: here and below, don't keep the lock() open when calling
|
||||||
|
// into window functions as they may invoke callbacks that need
|
||||||
|
// to immediately access the platform (self).
|
||||||
|
|
||||||
while !self.0.lock().windows.is_empty() {
|
while !self.state.lock().windows.is_empty() {
|
||||||
let event = self.0.lock().xcb_connection.wait_for_event().unwrap();
|
let event = self.xcb_connection.wait_for_event().unwrap();
|
||||||
match event {
|
match event {
|
||||||
xcb::Event::X(x::Event::ClientMessage(ev)) => {
|
xcb::Event::X(x::Event::ClientMessage(ev)) => {
|
||||||
if let x::ClientMessageData::Data32([atom, ..]) = ev.data() {
|
if let x::ClientMessageData::Data32([atom, ..]) = ev.data() {
|
||||||
let mut this = self.0.lock();
|
if atom == self.atoms.wm_del_window.resource_id() {
|
||||||
if atom == this.atoms.wm_del_window.resource_id() {
|
|
||||||
// window "x" button clicked by user, we gracefully exit
|
// window "x" button clicked by user, we gracefully exit
|
||||||
let window = this.windows.remove(&ev.window()).unwrap();
|
let window = self.state.lock().windows.remove(&ev.window()).unwrap();
|
||||||
window.destroy();
|
window.destroy();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
xcb::Event::X(x::Event::Expose(ev)) => {
|
xcb::Event::X(x::Event::Expose(ev)) => {
|
||||||
let this = self.0.lock();
|
let window = {
|
||||||
this.windows[&ev.window()].expose();
|
let state = self.state.lock();
|
||||||
|
Arc::clone(&state.windows[&ev.window()])
|
||||||
|
};
|
||||||
|
window.expose();
|
||||||
}
|
}
|
||||||
xcb::Event::X(x::Event::ConfigureNotify(ev)) => {
|
xcb::Event::X(x::Event::ConfigureNotify(ev)) => {
|
||||||
let bounds = Bounds {
|
let bounds = Bounds {
|
||||||
|
@ -115,12 +123,17 @@ impl Platform for LinuxPlatform {
|
||||||
height: ev.height().into(),
|
height: ev.height().into(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let this = self.0.lock();
|
let window = {
|
||||||
this.windows[&ev.window()].configure(bounds);
|
let state = self.state.lock();
|
||||||
|
Arc::clone(&state.windows[&ev.window()])
|
||||||
|
};
|
||||||
|
window.configure(bounds)
|
||||||
|
}
|
||||||
|
ref other => {
|
||||||
|
println!("Other event {:?}", other);
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
self.0.lock().dispatcher.tick_main();
|
self.dispatcher.tick_main();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,22 +150,20 @@ impl Platform for LinuxPlatform {
|
||||||
fn unhide_other_apps(&self) {}
|
fn unhide_other_apps(&self) {}
|
||||||
|
|
||||||
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
|
fn displays(&self) -> Vec<Rc<dyn PlatformDisplay>> {
|
||||||
let this = self.0.lock();
|
let setup = self.xcb_connection.get_setup();
|
||||||
let setup = this.xcb_connection.get_setup();
|
|
||||||
setup
|
setup
|
||||||
.roots()
|
.roots()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(root_id, _)| {
|
.map(|(root_id, _)| {
|
||||||
Rc::new(LinuxDisplay::new(&this.xcb_connection, root_id as i32))
|
Rc::new(LinuxDisplay::new(&self.xcb_connection, root_id as i32))
|
||||||
as Rc<dyn PlatformDisplay>
|
as Rc<dyn PlatformDisplay>
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
|
fn display(&self, id: DisplayId) -> Option<Rc<dyn PlatformDisplay>> {
|
||||||
let this = self.0.lock();
|
|
||||||
Some(Rc::new(LinuxDisplay::new(
|
Some(Rc::new(LinuxDisplay::new(
|
||||||
&this.xcb_connection,
|
&self.xcb_connection,
|
||||||
id.0 as i32,
|
id.0 as i32,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
|
@ -166,17 +177,19 @@ impl Platform for LinuxPlatform {
|
||||||
handle: AnyWindowHandle,
|
handle: AnyWindowHandle,
|
||||||
options: WindowOptions,
|
options: WindowOptions,
|
||||||
) -> Box<dyn PlatformWindow> {
|
) -> Box<dyn PlatformWindow> {
|
||||||
let mut this = self.0.lock();
|
let x_window = self.xcb_connection.generate_id();
|
||||||
let x_window = this.xcb_connection.generate_id();
|
|
||||||
|
|
||||||
let window_ptr = Arc::new(LinuxWindowState::new(
|
let window_ptr = Arc::new(LinuxWindowState::new(
|
||||||
options,
|
options,
|
||||||
&this.xcb_connection,
|
&self.xcb_connection,
|
||||||
this.x_root_index,
|
self.x_root_index,
|
||||||
x_window,
|
x_window,
|
||||||
&this.atoms,
|
&self.atoms,
|
||||||
));
|
));
|
||||||
this.windows.insert(x_window, Arc::clone(&window_ptr));
|
self.state
|
||||||
|
.lock()
|
||||||
|
.windows
|
||||||
|
.insert(x_window, Arc::clone(&window_ptr));
|
||||||
Box::new(LinuxWindow(window_ptr))
|
Box::new(LinuxWindow(window_ptr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue