From 22404a9f145eee6c1d68ae129ea827f09c592ba7 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 7 Jan 2022 13:24:44 +0100 Subject: [PATCH] Print panic backtrace and exit on command --- ctru-rs/src/lib.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ctru-rs/src/lib.rs b/ctru-rs/src/lib.rs index 6c9342c..c9c3ee9 100644 --- a/ctru-rs/src/lib.rs +++ b/ctru-rs/src/lib.rs @@ -9,12 +9,31 @@ extern crate widestring; extern crate ctru_sys as libctru; /// Call this somewhere to force Rust to link some required crates -/// (ex. pthread-3ds). The call doesn't need to execute, just exist. +/// This is also a setup for some crate integration only available at runtime /// /// See https://github.com/rust-lang/rust/issues/47384 pub fn init() { linker_fix_3ds::init(); pthread_3ds::init(); + + use std::panic::PanicInfo; + + // Panic Hook setup + let default_hook = std::panic::take_hook(); + 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; + } + } + }); + std::panic::set_hook(new_hook); } pub mod applets;