Browse Source

Added check for Console existence on panic

pull/28/head
Andrea Ciliberti 3 years ago
parent
commit
ea80b57880
  1. 1
      ctru-rs/examples/thread-basic.rs
  2. 16
      ctru-rs/src/console.rs
  3. 2
      ctru-rs/src/lib.rs

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

@ -13,6 +13,7 @@ fn main() {
let hid = Hid::init().unwrap(); let hid = Hid::init().unwrap();
let gfx = Gfx::default(); let gfx = Gfx::default();
let _console = Console::init(gfx.top_screen.borrow_mut()); let _console = Console::init(gfx.top_screen.borrow_mut());
drop(_console);
let prio = thread::current().priority(); let prio = thread::current().priority();
println!("Main thread prio: {}\n", prio); println!("Main thread prio: {}\n", prio);

16
ctru-rs/src/console.rs

@ -5,6 +5,20 @@ use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, Print
use crate::gfx::Screen; 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> { pub struct Console<'screen> {
context: Box<PrintConsole>, context: Box<PrintConsole>,
screen: RefMut<'screen, dyn Screen>, screen: RefMut<'screen, dyn Screen>,
@ -49,8 +63,6 @@ impl<'screen> Console<'screen> {
impl Drop for Console<'_> { impl Drop for Console<'_> {
fn drop(&mut self) { fn drop(&mut self) {
static mut EMPTY_CONSOLE: PrintConsole = unsafe { const_zero::const_zero!(PrintConsole) };
unsafe { unsafe {
// Safety: We are about to deallocate the PrintConsole data pointed // Safety: We are about to deallocate the PrintConsole data pointed
// to by libctru. Without this drop code libctru would have a // to by libctru. Without this drop code libctru would have a

2
ctru-rs/src/lib.rs

@ -20,7 +20,7 @@ pub fn init() {
default_hook(info); default_hook(info);
// Only for panics in the main thread // 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"); println!("\nPress SELECT to exit the software");
let hid = services::hid::Hid::init().unwrap(); let hid = services::hid::Hid::init().unwrap();

Loading…
Cancel
Save