From 85f901f36e4dc675517e29ed7c3d994393a9437a Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 27 Jan 2022 12:29:58 +0100 Subject: [PATCH 1/5] Alternative panic hook exit --- ctru-rs/examples/thread-basic.rs | 2 +- ctru-rs/src/lib.rs | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index a96d15f..7657718 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -16,7 +16,7 @@ fn main() { let prio = thread::current().priority(); println!("Main thread prio: {}\n", prio); - + for ix in 0..3 { thread::Builder::new() .priority(prio - 1) diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 23be6b8..e6cd577 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -16,15 +16,9 @@ pub fn init() { let new_hook = Box::new(move |info: &PanicInfo| { println!("\x1b[1;31m\n--------------------------------------------------"); default_hook(info); - println!("\nPress SELECT to exit the software"); - let hid = services::hid::Hid::init().unwrap(); - - loop { - hid.scan_input(); - if hid.keys_down().contains(services::hid::KeyPad::KEY_SELECT) { - break; - } - } + println!("\nThe thread will exit in 5 seconds"); + + thread::sleep(std::time::Duration::from_secs(5)); }); std::panic::set_hook(new_hook); } From 32d17e552a11c9618c8900354895d1950d6ca8e9 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 27 Jan 2022 16:26:30 +0100 Subject: [PATCH 2/5] Distinct behaviour for main and non-main threads --- ctru-rs/examples/thread-basic.rs | 2 +- ctru-rs/src/lib.rs | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index a96d15f..7657718 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -16,7 +16,7 @@ fn main() { let prio = thread::current().priority(); println!("Main thread prio: {}\n", prio); - + for ix in 0..3 { thread::Builder::new() .priority(prio - 1) diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 23be6b8..6946231 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -11,20 +11,27 @@ pub fn init() { use std::panic::PanicInfo; + let main_thread = thread::current().id(); + // Panic Hook setup let default_hook = std::panic::take_hook(); let new_hook = Box::new(move |info: &PanicInfo| { - println!("\x1b[1;31m\n--------------------------------------------------"); + println!("\x1b[1;31m\n--------------------------------------------------"); // Red ANSI Color Code default_hook(info); - println!("\nPress SELECT to exit the software"); - let hid = services::hid::Hid::init().unwrap(); - loop { - hid.scan_input(); - if hid.keys_down().contains(services::hid::KeyPad::KEY_SELECT) { - break; + // Only for panics in the main thread + if main_thread == thread::current().id() { + println!("\nPress SELECT to exit the software"); + let hid = services::hid::Hid::init().unwrap(); + + loop { + hid.scan_input(); + if hid.keys_down().contains(services::hid::KeyPad::KEY_SELECT) { + break; + } } } + println!("\x1b[1;37m\n"); // Get back to white }); std::panic::set_hook(new_hook); } From 1b743711d83fb5e80c1fcbb873c9b6fd371ed74b Mon Sep 17 00:00:00 2001 From: Meziu <55318903+Meziu@users.noreply.github.com> Date: Thu, 27 Jan 2022 18:45:51 +0100 Subject: [PATCH 3/5] Update ctru-rs/src/lib.rs Co-authored-by: Ian Chamberlain --- ctru-rs/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 6946231..92a2f80 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -31,7 +31,7 @@ pub fn init() { } } } - println!("\x1b[1;37m\n"); // Get back to white + println!("\x1b[0m"); // Get back to white }); std::panic::set_hook(new_hook); } From ea80b578807c1853860eb08ce78bdf01cea28e34 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 27 Jan 2022 22:56:15 +0100 Subject: [PATCH 4/5] 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(); From 9ea0232902373e817b43506c77b9ad12347a82c0 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 28 Jan 2022 14:28:56 +0100 Subject: [PATCH 5/5] Removed ANSI formatting from panic hook --- ctru-rs/examples/thread-basic.rs | 1 - ctru-rs/src/console.rs | 25 +++++++++++++------------ ctru-rs/src/lib.rs | 4 +--- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/ctru-rs/examples/thread-basic.rs b/ctru-rs/examples/thread-basic.rs index 6b400f5..7657718 100644 --- a/ctru-rs/examples/thread-basic.rs +++ b/ctru-rs/examples/thread-basic.rs @@ -13,7 +13,6 @@ 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 bc96dd0..ec03a3d 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -7,18 +7,6 @@ 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>, @@ -36,6 +24,19 @@ impl<'screen> Console<'screen> { Console { context, screen } } + /// Returns true if a valid Console to print on is selected + pub fn exists() -> bool { + unsafe { + let current_console = ctru_sys::consoleSelect(&mut EMPTY_CONSOLE); + + let res = (*current_console).consoleInitialised; + + ctru_sys::consoleSelect(current_console); + + res + } + } + /// Select this console as the current target for stdout pub fn select(&self) { unsafe { diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 4f6558a..5f487a9 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -16,11 +16,10 @@ pub fn init() { // Panic Hook setup let default_hook = std::panic::take_hook(); let new_hook = Box::new(move |info: &PanicInfo| { - println!("\x1b[1;31m\n--------------------------------------------------"); // Red ANSI Color Code default_hook(info); // Only for panics in the main thread - if main_thread == thread::current().id() && console::console_exists() { + if main_thread == thread::current().id() && console::Console::exists() { println!("\nPress SELECT to exit the software"); let hid = services::hid::Hid::init().unwrap(); @@ -31,7 +30,6 @@ pub fn init() { } } } - println!("\x1b[0m"); // Get back to white }); std::panic::set_hook(new_hook); }