Browse Source

Wrap PrintConsole in UnsafeCell

pull/159/head
Fenrir 10 months ago
parent
commit
a0aa6d5d9e
  1. 19
      ctru-rs/src/console.rs

19
ctru-rs/src/console.rs

@ -5,7 +5,7 @@
//! //!
//! Have a look at [`Soc::redirect_to_3dslink()`](crate::services::soc::Soc::redirect_to_3dslink) for a better alternative when debugging applications. //! Have a look at [`Soc::redirect_to_3dslink()`](crate::services::soc::Soc::redirect_to_3dslink) for a better alternative when debugging applications.
use std::cell::RefMut; use std::cell::{RefMut, UnsafeCell};
use std::default::Default; use std::default::Default;
use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole}; use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole};
@ -63,7 +63,7 @@ impl<S: Screen + Swap + Flush> ConsoleScreen for S {}
/// More info in the [`cargo-3ds` docs](https://github.com/rust3ds/cargo-3ds#running-executables). /// More info in the [`cargo-3ds` docs](https://github.com/rust3ds/cargo-3ds#running-executables).
#[doc(alias = "PrintConsole")] #[doc(alias = "PrintConsole")]
pub struct Console<'screen> { pub struct Console<'screen> {
context: Box<PrintConsole>, context: Box<UnsafeCell<PrintConsole>>,
screen: RefMut<'screen, dyn ConsoleScreen>, screen: RefMut<'screen, dyn ConsoleScreen>,
} }
@ -107,9 +107,9 @@ impl<'screen> Console<'screen> {
/// ``` /// ```
#[doc(alias = "consoleInit")] #[doc(alias = "consoleInit")]
pub fn new<S: ConsoleScreen>(screen: RefMut<'screen, S>) -> Self { pub fn new<S: ConsoleScreen>(screen: RefMut<'screen, S>) -> Self {
let mut context = Box::<PrintConsole>::default(); let context = Box::<UnsafeCell<PrintConsole>>::default();
unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; unsafe { consoleInit(screen.as_raw(), context.get()) };
Console { context, screen } Console { context, screen }
} }
@ -190,7 +190,7 @@ impl<'screen> Console<'screen> {
#[doc(alias = "consoleSelect")] #[doc(alias = "consoleSelect")]
pub fn select(&self) { pub fn select(&self) {
unsafe { unsafe {
consoleSelect(self.context.as_ref() as *const _ as *mut _); consoleSelect(self.context.get());
} }
} }
@ -248,7 +248,7 @@ impl<'screen> Console<'screen> {
unsafe { unsafe {
consoleSetWindow( consoleSetWindow(
self.context.as_mut(), self.context.get(),
x.into(), x.into(),
y.into(), y.into(),
width.into(), width.into(),
@ -338,7 +338,10 @@ impl Swap for Console<'_> {
/// This should be called once per frame at most. /// This should be called once per frame at most.
fn swap_buffers(&mut self) { fn swap_buffers(&mut self) {
self.screen.swap_buffers(); self.screen.swap_buffers();
self.context.frameBuffer = self.screen.raw_framebuffer().ptr as *mut u16;
unsafe {
(*self.context.get()).frameBuffer = self.screen.raw_framebuffer().ptr as *mut u16
};
} }
fn set_double_buffering(&mut self, enabled: bool) { fn set_double_buffering(&mut self, enabled: bool) {
@ -366,7 +369,7 @@ impl Drop for Console<'_> {
// Get the current console by replacing it with an empty one. // Get the current console by replacing it with an empty one.
let current_console = ctru_sys::consoleSelect(std::ptr::addr_of_mut!(EMPTY_CONSOLE)); let current_console = ctru_sys::consoleSelect(std::ptr::addr_of_mut!(EMPTY_CONSOLE));
if std::ptr::eq(current_console, &*self.context) { if std::ptr::eq(current_console, self.context.get()) {
// Console dropped while selected. We just replaced it with the // Console dropped while selected. We just replaced it with the
// empty console so nothing more to do. // empty console so nothing more to do.
} else { } else {

Loading…
Cancel
Save