bevy_app/terminal_ctrl_c_handler.rs
1use core::sync::atomic::{AtomicU8, Ordering};
2
3use bevy_ecs::message::MessageWriter;
4
5use crate::{App, AppExit, Plugin, Update};
6
7pub use ctrlc;
8
9/// Indicates that all [`App`]'s should exit.
10static SHOULD_EXIT: AtomicU8 = AtomicU8::new(0);
11
12/// Gracefully handles `Ctrl+C` by emitting a [`AppExit`] event. This plugin is part of the `DefaultPlugins`.
13///
14/// ```no_run
15/// # use bevy_app::{App, NoopPluginGroup as MinimalPlugins, PluginGroup, TerminalCtrlCHandlerPlugin};
16/// fn main() {
17/// App::new()
18/// .add_plugins(MinimalPlugins)
19/// .add_plugins(TerminalCtrlCHandlerPlugin)
20/// .run();
21/// }
22/// ```
23///
24/// If you want to setup your own `Ctrl+C` handler, you should call the
25/// [`TerminalCtrlCHandlerPlugin::gracefully_exit`] function in your handler if you want bevy to gracefully exit.
26/// ```no_run
27/// # use bevy_app::{App, NoopPluginGroup as DefaultPlugins, PluginGroup, TerminalCtrlCHandlerPlugin, ctrlc};
28/// fn main() {
29/// // Your own `Ctrl+C` handler
30/// ctrlc::set_handler(move || {
31/// // Other clean up code ...
32///
33/// TerminalCtrlCHandlerPlugin::gracefully_exit();
34/// });
35///
36/// App::new()
37/// .add_plugins(DefaultPlugins)
38/// .run();
39/// }
40/// ```
41#[derive(Default)]
42pub struct TerminalCtrlCHandlerPlugin;
43
44impl TerminalCtrlCHandlerPlugin {
45 /// When called the first time, it sends the [`AppExit`] event to all apps using
46 /// this plugin to make them gracefully exit.
47 ///
48 /// If called more than once, it exits immediately.
49 pub fn gracefully_exit() {
50 if SHOULD_EXIT.fetch_add(1, Ordering::SeqCst) > 0 {
51 log::error!("Received more than one ctrl+c. Skipping graceful shutdown.");
52 std::process::exit(Self::EXIT_CODE.into());
53 };
54 }
55
56 /// Sends a [`AppExit`] event when the user presses `Ctrl+C` on the terminal.
57 pub fn exit_on_flag(mut app_exit_writer: MessageWriter<AppExit>) {
58 if SHOULD_EXIT.load(Ordering::Relaxed) > 0 {
59 app_exit_writer.write(AppExit::from_code(Self::EXIT_CODE));
60 }
61 }
62
63 const EXIT_CODE: u8 = 130;
64}
65
66impl Plugin for TerminalCtrlCHandlerPlugin {
67 fn build(&self, app: &mut App) {
68 let result = ctrlc::try_set_handler(move || {
69 Self::gracefully_exit();
70 });
71 match result {
72 Ok(()) => {}
73 Err(ctrlc::Error::MultipleHandlers) => {
74 log::info!("Skipping installing `Ctrl+C` handler as one was already installed. Please call `TerminalCtrlCHandlerPlugin::gracefully_exit` in your own `Ctrl+C` handler if you want Bevy to gracefully exit on `Ctrl+C`.");
75 }
76 Err(err) => log::warn!("Failed to set `Ctrl+C` handler: {err}"),
77 }
78
79 app.add_systems(Update, TerminalCtrlCHandlerPlugin::exit_on_flag);
80 }
81}