From ea80b578807c1853860eb08ce78bdf01cea28e34 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 27 Jan 2022 22:56:15 +0100 Subject: [PATCH] Added check for Console existence on panic --- ctru-rs/examples/thread-basic.rs | 1 + ctru-rs/src/console.rs | 16 ++++++++++++++-- ctru-rs/src/lib.rs | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index 7657718..6b400f5 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -13,6 +13,7 @@ fn main() { let hid = Hid::init().unwrap(); let gfx = Gfx::default(); let _console = Console::init(gfx.top_screen.borrow_mut()); + drop(_console); let prio = thread::current().priority(); println!("Main thread prio: {}\n", prio); diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 2df1495..bc96dd0 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -5,6 +5,20 @@ use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, Print use crate::gfx::Screen; +static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) }; + +pub fn console_exists() -> bool { + unsafe { + let current_console = ctru_sys::consoleSelect(&mut EMPTY_CONSOLE); + + let res = (*current_console).consoleInitialised; + + ctru_sys::consoleSelect(current_console); + + res + } +} + pub struct Console<'screen> { context: Box, screen: RefMut<'screen, dyn Screen>, @@ -49,8 +63,6 @@ impl<'screen> Console<'screen> { impl Drop for Console<'_> { fn drop(&mut self) { - static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) }; - unsafe { // Safety: We are about to deallocate the PrintConsole data pointed // to by libctru. Without this drop code libctru would have a diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 92a2f80..4f6558a 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -20,7 +20,7 @@ pub fn init() { default_hook(info); // Only for panics in the main thread - if main_thread == thread::current().id() { + if main_thread == thread::current().id() && console::console_exists() { println!("\nPress SELECT to exit the software"); let hid = services::hid::Hid::init().unwrap();