Browse Source

Merge pull request #28 from Meziu/fix/new-panic-exit

pull/33/head
Meziu 3 years ago committed by GitHub
parent
commit
e98b0a97aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      ctru-rs/examples/thread-basic.rs
  2. 17
      ctru-rs/src/console.rs
  3. 19
      ctru-rs/src/lib.rs

2
ctru-rs/examples/thread-basic.rs

@ -16,7 +16,7 @@ fn main() { @@ -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)

17
ctru-rs/src/console.rs

@ -5,6 +5,8 @@ use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, Print @@ -5,6 +5,8 @@ use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, Print
use crate::gfx::Screen;
static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) };
pub struct Console<'screen> {
context: Box<PrintConsole>,
screen: RefMut<'screen, dyn Screen>,
@ -22,6 +24,19 @@ impl<'screen> Console<'screen> { @@ -22,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 {
@ -49,8 +64,6 @@ impl<'screen> Console<'screen> { @@ -49,8 +64,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

19
ctru-rs/src/lib.rs

@ -11,18 +11,23 @@ pub fn init() { @@ -11,18 +11,23 @@ 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--------------------------------------------------");
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() && console::Console::exists() {
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;
}
}
}
});

Loading…
Cancel
Save