mirror of
https://github.com/rust-windowing/winit.git
synced 2026-08-31 13:50:05 -04:00
Add exit code to control flow and impl on linux
This commit is contained in:
@@ -87,8 +87,9 @@ pub enum ControlFlow {
|
|||||||
WaitUntil(Instant),
|
WaitUntil(Instant),
|
||||||
/// Send a `LoopDestroyed` event and stop the event loop. This variant is *sticky* - once set,
|
/// Send a `LoopDestroyed` event and stop the event loop. This variant is *sticky* - once set,
|
||||||
/// `control_flow` cannot be changed from `Exit`, and any future attempts to do so will result
|
/// `control_flow` cannot be changed from `Exit`, and any future attempts to do so will result
|
||||||
/// in the `control_flow` parameter being reset to `Exit`.
|
/// in the `control_flow` parameter being reset to `Exit`. The contained number will be used as
|
||||||
Exit,
|
/// exit code, if the platform supports exiting with one.
|
||||||
|
Exit(i32),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ControlFlow {
|
impl Default for ControlFlow {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ pub trait EventLoopExtRunReturn {
|
|||||||
/// underlying OS APIs, which cannot be hidden by `winit` without severe stability repercussions.
|
/// underlying OS APIs, which cannot be hidden by `winit` without severe stability repercussions.
|
||||||
///
|
///
|
||||||
/// You are strongly encouraged to use `run`, unless the use of this is absolutely necessary.
|
/// You are strongly encouraged to use `run`, unless the use of this is absolutely necessary.
|
||||||
fn run_return<F>(&mut self, event_handler: F)
|
fn run_return<F>(&mut self, event_handler: F) -> i32
|
||||||
where
|
where
|
||||||
F: FnMut(
|
F: FnMut(
|
||||||
Event<'_, Self::UserEvent>,
|
Event<'_, Self::UserEvent>,
|
||||||
@@ -45,7 +45,7 @@ pub trait EventLoopExtRunReturn {
|
|||||||
impl<T> EventLoopExtRunReturn for EventLoop<T> {
|
impl<T> EventLoopExtRunReturn for EventLoop<T> {
|
||||||
type UserEvent = T;
|
type UserEvent = T;
|
||||||
|
|
||||||
fn run_return<F>(&mut self, event_handler: F)
|
fn run_return<F>(&mut self, event_handler: F) -> i32
|
||||||
where
|
where
|
||||||
F: FnMut(
|
F: FnMut(
|
||||||
Event<'_, Self::UserEvent>,
|
Event<'_, Self::UserEvent>,
|
||||||
|
|||||||
@@ -655,7 +655,7 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy)
|
x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_return<F>(&mut self, callback: F)
|
pub fn run_return<F>(&mut self, callback: F) -> i32
|
||||||
where
|
where
|
||||||
F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
||||||
{
|
{
|
||||||
@@ -743,8 +743,9 @@ fn sticky_exit_callback<T, F>(
|
|||||||
{
|
{
|
||||||
// make ControlFlow::Exit sticky by providing a dummy
|
// make ControlFlow::Exit sticky by providing a dummy
|
||||||
// control flow reference if it is already Exit.
|
// control flow reference if it is already Exit.
|
||||||
let mut dummy = ControlFlow::Exit;
|
let mut dummy;
|
||||||
let cf = if *control_flow == ControlFlow::Exit {
|
let cf = if let ControlFlow::Exit(code) = *control_flow {
|
||||||
|
dummy = ControlFlow::Exit(code);
|
||||||
&mut dummy
|
&mut dummy
|
||||||
} else {
|
} else {
|
||||||
control_flow
|
control_flow
|
||||||
|
|||||||
@@ -206,11 +206,11 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
where
|
where
|
||||||
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow) + 'static,
|
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow) + 'static,
|
||||||
{
|
{
|
||||||
self.run_return(callback);
|
let exit_code = self.run_return(callback);
|
||||||
process::exit(0)
|
process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_return<F>(&mut self, mut callback: F)
|
pub fn run_return<F>(&mut self, mut callback: F) -> i32
|
||||||
where
|
where
|
||||||
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
|
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
|
||||||
{
|
{
|
||||||
@@ -235,7 +235,9 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
// really an option. Instead we inform that the event loop got destroyed. We may
|
// really an option. Instead we inform that the event loop got destroyed. We may
|
||||||
// communicate an error that something was terminated, but winit doesn't provide us
|
// communicate an error that something was terminated, but winit doesn't provide us
|
||||||
// with an API to do that via some event.
|
// with an API to do that via some event.
|
||||||
loop {
|
// Still, we set the exit code as 1 (non-zero) to inform at least about something
|
||||||
|
// "unusual".
|
||||||
|
let exit_code = loop {
|
||||||
// Handle pending user events. We don't need back buffer, since we can't dispatch
|
// Handle pending user events. We don't need back buffer, since we can't dispatch
|
||||||
// user events indirectly via callback to the user.
|
// user events indirectly via callback to the user.
|
||||||
for user_event in pending_user_events.borrow_mut().drain(..) {
|
for user_event in pending_user_events.borrow_mut().drain(..) {
|
||||||
@@ -434,17 +436,17 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
if let Ok(dispatched) = queue.dispatch_pending(state, |_, _, _| unimplemented!()) {
|
if let Ok(dispatched) = queue.dispatch_pending(state, |_, _, _| unimplemented!()) {
|
||||||
dispatched > 0
|
dispatched > 0
|
||||||
} else {
|
} else {
|
||||||
break;
|
break 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match control_flow {
|
match control_flow {
|
||||||
ControlFlow::Exit => break,
|
ControlFlow::Exit(code) => break code,
|
||||||
ControlFlow::Poll => {
|
ControlFlow::Poll => {
|
||||||
// Non-blocking dispatch.
|
// Non-blocking dispatch.
|
||||||
let timeout = Duration::from_millis(0);
|
let timeout = Duration::from_millis(0);
|
||||||
if self.loop_dispatch(Some(timeout)).is_err() {
|
if self.loop_dispatch(Some(timeout)).is_err() {
|
||||||
break;
|
break 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(
|
callback(
|
||||||
@@ -461,7 +463,7 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.loop_dispatch(timeout).is_err() {
|
if self.loop_dispatch(timeout).is_err() {
|
||||||
break;
|
break 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(
|
callback(
|
||||||
@@ -484,7 +486,7 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.loop_dispatch(Some(duration)).is_err() {
|
if self.loop_dispatch(Some(duration)).is_err() {
|
||||||
break;
|
break 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
@@ -510,9 +512,10 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
callback(Event::LoopDestroyed, &self.window_target, &mut control_flow);
|
callback(Event::LoopDestroyed, &self.window_target, &mut control_flow);
|
||||||
|
exit_code
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
&self.target
|
&self.target
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_return<F>(&mut self, mut callback: F)
|
pub fn run_return<F>(&mut self, mut callback: F) -> i32
|
||||||
where
|
where
|
||||||
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
||||||
{
|
{
|
||||||
@@ -266,7 +266,7 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
let mut events = Events::with_capacity(8);
|
let mut events = Events::with_capacity(8);
|
||||||
let mut cause = StartCause::Init;
|
let mut cause = StartCause::Init;
|
||||||
|
|
||||||
loop {
|
let exit_code = loop {
|
||||||
sticky_exit_callback(
|
sticky_exit_callback(
|
||||||
crate::event::Event::NewEvents(cause),
|
crate::event::Event::NewEvents(cause),
|
||||||
&self.target,
|
&self.target,
|
||||||
@@ -329,7 +329,7 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
let (deadline, timeout);
|
let (deadline, timeout);
|
||||||
|
|
||||||
match control_flow {
|
match control_flow {
|
||||||
ControlFlow::Exit => break,
|
ControlFlow::Exit(code) => break code,
|
||||||
ControlFlow::Poll => {
|
ControlFlow::Poll => {
|
||||||
cause = StartCause::Poll;
|
cause = StartCause::Poll;
|
||||||
deadline = None;
|
deadline = None;
|
||||||
@@ -376,21 +376,22 @@ impl<T: 'static> EventLoop<T> {
|
|||||||
requested_resume: deadline,
|
requested_resume: deadline,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
callback(
|
callback(
|
||||||
crate::event::Event::LoopDestroyed,
|
crate::event::Event::LoopDestroyed,
|
||||||
&self.target,
|
&self.target,
|
||||||
&mut control_flow,
|
&mut control_flow,
|
||||||
);
|
);
|
||||||
|
exit_code
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run<F>(mut self, callback: F) -> !
|
pub fn run<F>(mut self, callback: F) -> !
|
||||||
where
|
where
|
||||||
F: 'static + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
F: 'static + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
|
||||||
{
|
{
|
||||||
self.run_return(callback);
|
let exit_code = self.run_return(callback);
|
||||||
::std::process::exit(0);
|
::std::process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn drain_events<F>(&mut self, callback: &mut F, control_flow: &mut ControlFlow)
|
fn drain_events<F>(&mut self, callback: &mut F, control_flow: &mut ControlFlow)
|
||||||
|
|||||||
Reference in New Issue
Block a user