From 8fbad7cfeb11bedd02b4e43de144f381456589db Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 15 Jan 2022 17:09:07 +0100 Subject: [PATCH 01/15] Added mutability checks for Gfx --- ctru-rs/src/gfx.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index 8bd4694..284b075 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -48,14 +48,14 @@ impl Gfx { } /// Enable or disable the 3D stereoscopic effect - pub fn set_3d_enabled(&self, enabled: bool) { + pub fn set_3d_enabled(&mut self, enabled: bool) { unsafe { ctru_sys::gfxSet3D(enabled) } } /// Enable or disable the wide screen mode (top screen). /// /// This only works when 3D is disabled. - pub fn set_wide_mode(&self, enabled: bool) { + pub fn set_wide_mode(&mut self, enabled: bool) { unsafe { ctru_sys::gfxSetWide(enabled) }; } @@ -68,7 +68,7 @@ impl Gfx { /// /// Note that even when double buffering is disabled, one should still use the `swap_buffers` /// method on each frame to keep the gsp configuration up to date - pub fn set_double_buffering(&self, screen: Screen, enabled: bool) { + pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) { unsafe { ctru_sys::gfxSetDoubleBuffering(screen.into(), enabled) } } @@ -104,7 +104,7 @@ impl Gfx { } /// Change the framebuffer format for a screen - pub fn set_framebuffer_format(&self, screen: Screen, fmt: FramebufferFormat) { + pub fn set_framebuffer_format(&mut self, screen: Screen, fmt: FramebufferFormat) { unsafe { ctru_sys::gfxSetScreenFormat(screen.into(), fmt.into()) } } @@ -114,14 +114,12 @@ impl Gfx { /// /// Note that the pointer returned by this function can change after each call to this function /// if double buffering is enabled - pub fn get_raw_framebuffer(&self, screen: Screen, side: Side) -> (*mut u8, u16, u16) { - unsafe { - let mut width: u16 = 0; - let mut height: u16 = 0; - let buf: *mut u8 = - ctru_sys::gfxGetFramebuffer(screen.into(), side.into(), &mut width, &mut height); - (buf, width, height) - } + pub unsafe fn get_raw_framebuffer(&self, screen: Screen, side: Side) -> (*mut u8, u16, u16) { + let mut width: u16 = 0; + let mut height: u16 = 0; + let buf: *mut u8 = + ctru_sys::gfxGetFramebuffer(screen.into(), side.into(), &mut width, &mut height); + (buf, width, height) } } From 45bf4b5ec7d4688a714ec3e1a76a9a69ed4c18b9 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Mon, 17 Jan 2022 21:27:05 +0100 Subject: [PATCH 02/15] Separate screens and internal mutability for Gfx --- ctru-rs/src/console.rs | 17 ++++--- ctru-rs/src/gfx.rs | 109 +++++++++++++++++++++++++++++++---------- 2 files changed, 93 insertions(+), 33 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 0c86129..f5e2114 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -1,27 +1,30 @@ +use std::cell::Ref; use std::default::Default; use std::marker::PhantomData; use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole}; -use crate::gfx::{Gfx, Screen}; +use crate::gfx::Screen; -pub struct Console<'gfx> { +pub struct Console<'screen> { context: Box, - _gfx: PhantomData<&'gfx ()>, + _screen: PhantomData<&'screen ()>, } -impl<'gfx> Console<'gfx> { +impl<'screen> Console<'screen> { /// Initialize a console on the chosen screen, overwriting whatever was on the screen /// previously (including other consoles). The new console is automatically selected for /// printing. - pub fn init(_gfx: &'gfx Gfx, screen: Screen) -> Self { + pub fn init(screen: Ref<'screen, Screen>) -> Self { let mut context = Box::new(PrintConsole::default()); - unsafe { consoleInit(screen.into(), context.as_mut()) }; + let screen_kind = *screen; + + unsafe { consoleInit(screen_kind.into(), context.as_mut()) }; Console { context, - _gfx: PhantomData, + _screen: PhantomData, } } diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index 284b075..13be457 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -1,5 +1,6 @@ //! LCD screens manipulation helper +use std::cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut}; use std::default::Default; use std::ops::Drop; @@ -9,7 +10,10 @@ use crate::services::gspgpu::{self, FramebufferFormat}; /// provides helper functions and utilities for software rendering. /// /// The service exits when this struct is dropped. -pub struct Gfx(()); +pub struct Gfx { + top_screen: RefCell, + bottom_screen: RefCell, +} /// Available screens on the 3DS #[derive(Copy, Clone, Debug)] @@ -44,32 +48,30 @@ impl Gfx { unsafe { ctru_sys::gfxInit(top_fb_fmt.into(), bottom_fb_fmt.into(), use_vram_buffers); } - Gfx(()) + Gfx { + top_screen: RefCell::new(Screen::Top), + bottom_screen: RefCell::new(Screen::Bottom), + } } - /// Enable or disable the 3D stereoscopic effect - pub fn set_3d_enabled(&mut self, enabled: bool) { - unsafe { ctru_sys::gfxSet3D(enabled) } + /// Try to get an immutable reference to the Top screen + pub fn get_top_screen(&self) -> Result, BorrowError> { + self.top_screen.try_borrow() } - /// Enable or disable the wide screen mode (top screen). - /// - /// This only works when 3D is disabled. - pub fn set_wide_mode(&mut self, enabled: bool) { - unsafe { ctru_sys::gfxSetWide(enabled) }; + /// Try to get a mutable reference to the Top screen + pub fn get_top_screen_mut(&self) -> Result, BorrowMutError> { + self.top_screen.try_borrow_mut() } - /// Get the status of wide screen mode. - pub fn get_wide_mode(&self) -> bool { - unsafe { ctru_sys::gfxIsWide() } + /// Try to get an immutable reference to the Bottom screen + pub fn get_bottom_screen(&self) -> Result, BorrowError> { + self.bottom_screen.try_borrow() } - /// Sets whether to use double buffering. Enabled by default. - /// - /// Note that even when double buffering is disabled, one should still use the `swap_buffers` - /// method on each frame to keep the gsp configuration up to date - pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) { - unsafe { ctru_sys::gfxSetDoubleBuffering(screen.into(), enabled) } + /// Try to get a mutable reference to the Bottom screen + pub fn get_bottom_screen_mut(&self) -> Result, BorrowMutError> { + self.bottom_screen.try_borrow_mut() } /// Flushes the current framebuffers @@ -97,13 +99,63 @@ impl Gfx { pub fn wait_for_vblank(&self) { gspgpu::wait_for_event(gspgpu::Event::VBlank0, true); } +} + +impl Screen { + /// Enable or disable the 3D stereoscopic effect + /// + /// #Errors + /// When called by the Bottom screen + pub fn set_3d_enabled(&mut self, enabled: bool) -> Result<(), String> { + match self { + Screen::Top => unsafe { + ctru_sys::gfxSet3D(enabled); + Ok(()) + }, + Screen::Bottom => Err("Tried to enable 3D on bottom screen".to_string()), + } + } - /// Gets the framebuffer format for a screen + /// Enable or disable the wide screen mode (top screen). + /// This only works when 3D is disabled. + /// + /// #Errors + /// When called by the Bottom screen + pub fn set_wide_mode(&mut self, enabled: bool) -> Result<(), String> { + match self { + Screen::Top => unsafe { + ctru_sys::gfxSetWide(enabled); + Ok(()) + }, + Screen::Bottom => Err("Tried to change wide-mode on bottom screen".to_string()), + } + } + + /// Get the status of wide screen mode. + /// + /// #Errors + /// When called by the Bottom screen + pub fn get_wide_mode(&self) -> Result { + match self { + Screen::Top => unsafe { Ok(ctru_sys::gfxIsWide()) }, + Screen::Bottom => Err("Tried to check wide-mode status on bottom screen".to_string()), + } + } + + /// Sets whether to use double buffering. Enabled by default. + /// + /// Note that even when double buffering is disabled, one should still use the `swap_buffers` + /// method on each frame to keep the gsp configuration up to date + pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) { + unsafe { ctru_sys::gfxSetDoubleBuffering(screen.into(), enabled) } + } + + /// Gets the framebuffer format pub fn get_framebuffer_format(&self, screen: Screen) -> FramebufferFormat { unsafe { ctru_sys::gfxGetScreenFormat(screen.into()).into() } } - /// Change the framebuffer format for a screen + /// Change the framebuffer format pub fn set_framebuffer_format(&mut self, screen: Screen, fmt: FramebufferFormat) { unsafe { ctru_sys::gfxSetScreenFormat(screen.into(), fmt.into()) } } @@ -114,12 +166,14 @@ impl Gfx { /// /// Note that the pointer returned by this function can change after each call to this function /// if double buffering is enabled - pub unsafe fn get_raw_framebuffer(&self, screen: Screen, side: Side) -> (*mut u8, u16, u16) { + pub fn get_raw_framebuffer(&self, screen: Screen, side: Side) -> (*mut u8, u16, u16) { let mut width: u16 = 0; let mut height: u16 = 0; - let buf: *mut u8 = - ctru_sys::gfxGetFramebuffer(screen.into(), side.into(), &mut width, &mut height); - (buf, width, height) + unsafe { + let buf: *mut u8 = + ctru_sys::gfxGetFramebuffer(screen.into(), side.into(), &mut width, &mut height); + (buf, width, height) + } } } @@ -146,7 +200,10 @@ impl From for ctru_sys::gfx3dSide_t { impl Default for Gfx { fn default() -> Self { unsafe { ctru_sys::gfxInitDefault() }; - Gfx(()) + Gfx { + top_screen: RefCell::new(Screen::Top), + bottom_screen: RefCell::new(Screen::Bottom), + } } } From 53aa05a00331204706f53862ac1790c7b1dc1a68 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Tue, 18 Jan 2022 14:27:56 +0100 Subject: [PATCH 03/15] Split Screen enum into structs and trait --- ctru-rs/src/console.rs | 6 +- ctru-rs/src/gfx.rs | 166 +++++++++++++++++++++++------------------ 2 files changed, 95 insertions(+), 77 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index f5e2114..178e006 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -15,12 +15,10 @@ impl<'screen> Console<'screen> { /// Initialize a console on the chosen screen, overwriting whatever was on the screen /// previously (including other consoles). The new console is automatically selected for /// printing. - pub fn init(screen: Ref<'screen, Screen>) -> Self { + pub fn init(screen: Ref<'screen, impl Screen>) -> Self { let mut context = Box::new(PrintConsole::default()); - let screen_kind = *screen; - - unsafe { consoleInit(screen_kind.into(), context.as_mut()) }; + unsafe { consoleInit(screen.into_raw(), context.as_mut()) }; Console { context, diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index 13be457..0473bb3 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -11,19 +11,39 @@ use crate::services::gspgpu::{self, FramebufferFormat}; /// /// The service exits when this struct is dropped. pub struct Gfx { - top_screen: RefCell, - bottom_screen: RefCell, + top_screen: RefCell, + bottom_screen: RefCell, } -/// Available screens on the 3DS -#[derive(Copy, Clone, Debug)] -pub enum Screen { - /// The top screen - Top, - /// The bottom screen - Bottom, +/// Trait implemented by TopScreen and BottomScreen for common methods +pub trait Screen { + /// Returns the libctru value for the Screen kind + fn into_raw(&self) -> ctru_sys::gfxScreen_t; + + /// Sets whether to use double buffering. Enabled by default. + /// + /// Note that even when double buffering is disabled, one should still use the `swap_buffers` + /// method on each frame to keep the gsp configuration up to date + fn set_double_buffering(&mut self, enabled: bool); + + /// Gets the framebuffer format + fn get_framebuffer_format(&self) -> FramebufferFormat; + + /// Change the framebuffer format + fn set_framebuffer_format(&mut self, fmt: FramebufferFormat); + + /// Returns a tuple containing a pointer to the specifified framebuffer (as determined by the + /// calling screen and `Side`), the width of the framebuffer in pixels, and the height of + /// the framebuffer in pixels + /// + /// Note that the pointer returned by this function can change after each call to this function + /// if double buffering is enabled + fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16); } +pub struct TopScreen; +pub struct BottomScreen; + #[derive(Copy, Clone, Debug)] /// Side of top screen framebuffer /// @@ -49,28 +69,28 @@ impl Gfx { ctru_sys::gfxInit(top_fb_fmt.into(), bottom_fb_fmt.into(), use_vram_buffers); } Gfx { - top_screen: RefCell::new(Screen::Top), - bottom_screen: RefCell::new(Screen::Bottom), + top_screen: RefCell::new(TopScreen), + bottom_screen: RefCell::new(BottomScreen), } } /// Try to get an immutable reference to the Top screen - pub fn get_top_screen(&self) -> Result, BorrowError> { + pub fn get_top_screen(&self) -> Result, BorrowError> { self.top_screen.try_borrow() } /// Try to get a mutable reference to the Top screen - pub fn get_top_screen_mut(&self) -> Result, BorrowMutError> { + pub fn get_top_screen_mut(&self) -> Result, BorrowMutError> { self.top_screen.try_borrow_mut() } /// Try to get an immutable reference to the Bottom screen - pub fn get_bottom_screen(&self) -> Result, BorrowError> { + pub fn get_bottom_screen(&self) -> Result, BorrowError> { self.bottom_screen.try_borrow() } /// Try to get a mutable reference to the Bottom screen - pub fn get_bottom_screen_mut(&self) -> Result, BorrowMutError> { + pub fn get_bottom_screen_mut(&self) -> Result, BorrowMutError> { self.bottom_screen.try_borrow_mut() } @@ -101,88 +121,88 @@ impl Gfx { } } -impl Screen { +impl TopScreen { /// Enable or disable the 3D stereoscopic effect - /// - /// #Errors - /// When called by the Bottom screen - pub fn set_3d_enabled(&mut self, enabled: bool) -> Result<(), String> { - match self { - Screen::Top => unsafe { - ctru_sys::gfxSet3D(enabled); - Ok(()) - }, - Screen::Bottom => Err("Tried to enable 3D on bottom screen".to_string()), + pub fn set_3d_enabled(&mut self, enabled: bool) { + unsafe { + ctru_sys::gfxSet3D(enabled); } } /// Enable or disable the wide screen mode (top screen). /// This only works when 3D is disabled. - /// - /// #Errors - /// When called by the Bottom screen - pub fn set_wide_mode(&mut self, enabled: bool) -> Result<(), String> { - match self { - Screen::Top => unsafe { - ctru_sys::gfxSetWide(enabled); - Ok(()) - }, - Screen::Bottom => Err("Tried to change wide-mode on bottom screen".to_string()), + pub fn set_wide_mode(&mut self, enabled: bool) { + unsafe { + ctru_sys::gfxSetWide(enabled); } } /// Get the status of wide screen mode. - /// - /// #Errors - /// When called by the Bottom screen - pub fn get_wide_mode(&self) -> Result { - match self { - Screen::Top => unsafe { Ok(ctru_sys::gfxIsWide()) }, - Screen::Bottom => Err("Tried to check wide-mode status on bottom screen".to_string()), - } + pub fn get_wide_mode(&self) -> bool { + unsafe { ctru_sys::gfxIsWide() } } +} - /// Sets whether to use double buffering. Enabled by default. - /// - /// Note that even when double buffering is disabled, one should still use the `swap_buffers` - /// method on each frame to keep the gsp configuration up to date - pub fn set_double_buffering(&mut self, screen: Screen, enabled: bool) { - unsafe { ctru_sys::gfxSetDoubleBuffering(screen.into(), enabled) } +impl Screen for TopScreen { + fn into_raw(&self) -> ctru_sys::gfxScreen_t { + ctru_sys::GFX_TOP } - /// Gets the framebuffer format - pub fn get_framebuffer_format(&self, screen: Screen) -> FramebufferFormat { - unsafe { ctru_sys::gfxGetScreenFormat(screen.into()).into() } + fn set_double_buffering(&mut self, enabled: bool) { + unsafe { ctru_sys::gfxSetDoubleBuffering(ctru_sys::GFX_TOP, enabled) } } - /// Change the framebuffer format - pub fn set_framebuffer_format(&mut self, screen: Screen, fmt: FramebufferFormat) { - unsafe { ctru_sys::gfxSetScreenFormat(screen.into(), fmt.into()) } + fn get_framebuffer_format(&self) -> FramebufferFormat { + unsafe { ctru_sys::gfxGetScreenFormat(ctru_sys::GFX_TOP).into() } } - /// Returns a tuple containing a pointer to the specifified framebuffer (as determined by the - /// provided `Screen` and `Side`), the width of the framebuffer in pixels, and the height of - /// the framebuffer in pixels - /// - /// Note that the pointer returned by this function can change after each call to this function - /// if double buffering is enabled - pub fn get_raw_framebuffer(&self, screen: Screen, side: Side) -> (*mut u8, u16, u16) { + fn set_framebuffer_format(&mut self, fmt: FramebufferFormat) { + unsafe { ctru_sys::gfxSetScreenFormat(ctru_sys::GFX_TOP, fmt.into()) } + } + + fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16) { let mut width: u16 = 0; let mut height: u16 = 0; unsafe { - let buf: *mut u8 = - ctru_sys::gfxGetFramebuffer(screen.into(), side.into(), &mut width, &mut height); + let buf: *mut u8 = ctru_sys::gfxGetFramebuffer( + ctru_sys::GFX_TOP, + side.into(), + &mut width, + &mut height, + ); (buf, width, height) } } } -impl From for ctru_sys::gfxScreen_t { - fn from(g: Screen) -> ctru_sys::gfxScreen_t { - use self::Screen::*; - match g { - Top => ctru_sys::GFX_TOP, - Bottom => ctru_sys::GFX_BOTTOM, +impl Screen for BottomScreen { + fn into_raw(&self) -> ctru_sys::gfxScreen_t { + ctru_sys::GFX_BOTTOM + } + + fn set_double_buffering(&mut self, enabled: bool) { + unsafe { ctru_sys::gfxSetDoubleBuffering(ctru_sys::GFX_BOTTOM, enabled) } + } + + fn get_framebuffer_format(&self) -> FramebufferFormat { + unsafe { ctru_sys::gfxGetScreenFormat(ctru_sys::GFX_BOTTOM).into() } + } + + fn set_framebuffer_format(&mut self, fmt: FramebufferFormat) { + unsafe { ctru_sys::gfxSetScreenFormat(ctru_sys::GFX_BOTTOM, fmt.into()) } + } + + fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16) { + let mut width: u16 = 0; + let mut height: u16 = 0; + unsafe { + let buf: *mut u8 = ctru_sys::gfxGetFramebuffer( + ctru_sys::GFX_BOTTOM, + side.into(), + &mut width, + &mut height, + ); + (buf, width, height) } } } @@ -201,8 +221,8 @@ impl Default for Gfx { fn default() -> Self { unsafe { ctru_sys::gfxInitDefault() }; Gfx { - top_screen: RefCell::new(Screen::Top), - bottom_screen: RefCell::new(Screen::Bottom), + top_screen: RefCell::new(TopScreen), + bottom_screen: RefCell::new(BottomScreen), } } } From 04a35e4a6fdcf09910724752572416d839d21a3e Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Wed, 19 Jan 2022 13:56:37 +0100 Subject: [PATCH 04/15] Fixed Screen accessibility --- ctru-rs/src/console.rs | 2 +- ctru-rs/src/gfx.rs | 136 +++++++++++++---------------------------- 2 files changed, 42 insertions(+), 96 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 178e006..a25c2ab 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -18,7 +18,7 @@ impl<'screen> Console<'screen> { pub fn init(screen: Ref<'screen, impl Screen>) -> Self { let mut context = Box::new(PrintConsole::default()); - unsafe { consoleInit(screen.into_raw(), context.as_mut()) }; + unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; Console { context, diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index 0473bb3..d37f33c 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -1,36 +1,33 @@ //! LCD screens manipulation helper -use std::cell::{BorrowError, BorrowMutError, Ref, RefCell, RefMut}; +use std::cell::RefCell; use std::default::Default; use std::ops::Drop; use crate::services::gspgpu::{self, FramebufferFormat}; -/// A handle to libctru's gfx module. This module is a wrapper around the GSPGPU service that -/// provides helper functions and utilities for software rendering. -/// -/// The service exits when this struct is dropped. -pub struct Gfx { - top_screen: RefCell, - bottom_screen: RefCell, -} - /// Trait implemented by TopScreen and BottomScreen for common methods pub trait Screen { /// Returns the libctru value for the Screen kind - fn into_raw(&self) -> ctru_sys::gfxScreen_t; + fn as_raw(&self) -> ctru_sys::gfxScreen_t; /// Sets whether to use double buffering. Enabled by default. /// /// Note that even when double buffering is disabled, one should still use the `swap_buffers` /// method on each frame to keep the gsp configuration up to date - fn set_double_buffering(&mut self, enabled: bool); + fn set_double_buffering(&mut self, enabled: bool) { + unsafe { ctru_sys::gfxSetDoubleBuffering(self.as_raw(), enabled) } + } /// Gets the framebuffer format - fn get_framebuffer_format(&self) -> FramebufferFormat; + fn get_framebuffer_format(&self) -> FramebufferFormat { + unsafe { ctru_sys::gfxGetScreenFormat(self.as_raw()).into() } + } /// Change the framebuffer format - fn set_framebuffer_format(&mut self, fmt: FramebufferFormat); + fn set_framebuffer_format(&mut self, fmt: FramebufferFormat) { + unsafe { ctru_sys::gfxSetScreenFormat(self.as_raw(), fmt.into()) } + } /// Returns a tuple containing a pointer to the specifified framebuffer (as determined by the /// calling screen and `Side`), the width of the framebuffer in pixels, and the height of @@ -38,11 +35,23 @@ pub trait Screen { /// /// Note that the pointer returned by this function can change after each call to this function /// if double buffering is enabled - fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16); + fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16) { + let mut width: u16 = 0; + let mut height: u16 = 0; + unsafe { + let buf: *mut u8 = + ctru_sys::gfxGetFramebuffer(self.as_raw(), side.into(), &mut width, &mut height); + (buf, width, height) + } + } } -pub struct TopScreen; -pub struct BottomScreen; +pub struct TopScreen { + _private: (), +} +pub struct BottomScreen { + _private: (), +} #[derive(Copy, Clone, Debug)] /// Side of top screen framebuffer @@ -55,6 +64,15 @@ pub enum Side { Right, } +/// A handle to libctru's gfx module. This module is a wrapper around the GSPGPU service that +/// provides helper functions and utilities for software rendering. +/// +/// The service exits when this struct is dropped. +pub struct Gfx { + pub top_screen: RefCell, + pub bottom_screen: RefCell, +} + impl Gfx { /// Initialize the Gfx module with the chosen framebuffer formats for the top and bottom /// screens @@ -69,31 +87,11 @@ impl Gfx { ctru_sys::gfxInit(top_fb_fmt.into(), bottom_fb_fmt.into(), use_vram_buffers); } Gfx { - top_screen: RefCell::new(TopScreen), - bottom_screen: RefCell::new(BottomScreen), + top_screen: RefCell::new(TopScreen { _private: () }), + bottom_screen: RefCell::new(BottomScreen { _private: () }), } } - /// Try to get an immutable reference to the Top screen - pub fn get_top_screen(&self) -> Result, BorrowError> { - self.top_screen.try_borrow() - } - - /// Try to get a mutable reference to the Top screen - pub fn get_top_screen_mut(&self) -> Result, BorrowMutError> { - self.top_screen.try_borrow_mut() - } - - /// Try to get an immutable reference to the Bottom screen - pub fn get_bottom_screen(&self) -> Result, BorrowError> { - self.bottom_screen.try_borrow() - } - - /// Try to get a mutable reference to the Bottom screen - pub fn get_bottom_screen_mut(&self) -> Result, BorrowMutError> { - self.bottom_screen.try_borrow_mut() - } - /// Flushes the current framebuffers pub fn flush_buffers(&self) { unsafe { ctru_sys::gfxFlushBuffers() }; @@ -144,67 +142,15 @@ impl TopScreen { } impl Screen for TopScreen { - fn into_raw(&self) -> ctru_sys::gfxScreen_t { + fn as_raw(&self) -> ctru_sys::gfxScreen_t { ctru_sys::GFX_TOP } - - fn set_double_buffering(&mut self, enabled: bool) { - unsafe { ctru_sys::gfxSetDoubleBuffering(ctru_sys::GFX_TOP, enabled) } - } - - fn get_framebuffer_format(&self) -> FramebufferFormat { - unsafe { ctru_sys::gfxGetScreenFormat(ctru_sys::GFX_TOP).into() } - } - - fn set_framebuffer_format(&mut self, fmt: FramebufferFormat) { - unsafe { ctru_sys::gfxSetScreenFormat(ctru_sys::GFX_TOP, fmt.into()) } - } - - fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16) { - let mut width: u16 = 0; - let mut height: u16 = 0; - unsafe { - let buf: *mut u8 = ctru_sys::gfxGetFramebuffer( - ctru_sys::GFX_TOP, - side.into(), - &mut width, - &mut height, - ); - (buf, width, height) - } - } } impl Screen for BottomScreen { - fn into_raw(&self) -> ctru_sys::gfxScreen_t { + fn as_raw(&self) -> ctru_sys::gfxScreen_t { ctru_sys::GFX_BOTTOM } - - fn set_double_buffering(&mut self, enabled: bool) { - unsafe { ctru_sys::gfxSetDoubleBuffering(ctru_sys::GFX_BOTTOM, enabled) } - } - - fn get_framebuffer_format(&self) -> FramebufferFormat { - unsafe { ctru_sys::gfxGetScreenFormat(ctru_sys::GFX_BOTTOM).into() } - } - - fn set_framebuffer_format(&mut self, fmt: FramebufferFormat) { - unsafe { ctru_sys::gfxSetScreenFormat(ctru_sys::GFX_BOTTOM, fmt.into()) } - } - - fn get_raw_framebuffer(&self, side: Side) -> (*mut u8, u16, u16) { - let mut width: u16 = 0; - let mut height: u16 = 0; - unsafe { - let buf: *mut u8 = ctru_sys::gfxGetFramebuffer( - ctru_sys::GFX_BOTTOM, - side.into(), - &mut width, - &mut height, - ); - (buf, width, height) - } - } } impl From for ctru_sys::gfx3dSide_t { @@ -221,8 +167,8 @@ impl Default for Gfx { fn default() -> Self { unsafe { ctru_sys::gfxInitDefault() }; Gfx { - top_screen: RefCell::new(TopScreen), - bottom_screen: RefCell::new(BottomScreen), + top_screen: RefCell::new(TopScreen { _private: () }), + bottom_screen: RefCell::new(BottomScreen { _private: () }), } } } From ae00883787bf787540f080945d883da14fc5c63e Mon Sep 17 00:00:00 2001 From: Meziu <55318903+Meziu@users.noreply.github.com> Date: Thu, 20 Jan 2022 07:37:44 +0100 Subject: [PATCH 05/15] Update ctru-rs/src/gfx.rs Co-authored-by: Mark Drobnak --- ctru-rs/src/gfx.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index d37f33c..842eb0d 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -71,6 +71,7 @@ pub enum Side { pub struct Gfx { pub top_screen: RefCell, pub bottom_screen: RefCell, + _private: () } impl Gfx { From 09271676b6989c7bae6c3ecb36e9831339d8dbd7 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 20 Jan 2022 13:46:51 +0100 Subject: [PATCH 06/15] Fixed ownership of Screen borrow for Console --- ctru-rs/src/console.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index a25c2ab..ef71f78 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -8,21 +8,21 @@ use crate::gfx::Screen; pub struct Console<'screen> { context: Box, - _screen: PhantomData<&'screen ()>, + _screen: Ref<'screen, dyn Screen>, } impl<'screen> Console<'screen> { /// Initialize a console on the chosen screen, overwriting whatever was on the screen /// previously (including other consoles). The new console is automatically selected for /// printing. - pub fn init(screen: Ref<'screen, impl Screen>) -> Self { + pub fn init(screen: Ref<'screen, dyn Screen>) -> Self { let mut context = Box::new(PrintConsole::default()); unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; Console { context, - _screen: PhantomData, + _screen: screen, } } From 2ed31e56534b27a7796d895e910024ff7fcdcc38 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Thu, 20 Jan 2022 13:55:50 +0100 Subject: [PATCH 07/15] Removed PhantomData dependency --- ctru-rs/src/console.rs | 1 - ctru-rs/src/gfx.rs | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index ef71f78..33c61c1 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -1,6 +1,5 @@ use std::cell::Ref; use std::default::Default; -use std::marker::PhantomData; use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole}; diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index 842eb0d..aec2572 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -71,7 +71,7 @@ pub enum Side { pub struct Gfx { pub top_screen: RefCell, pub bottom_screen: RefCell, - _private: () + _private: (), } impl Gfx { @@ -90,6 +90,7 @@ impl Gfx { Gfx { top_screen: RefCell::new(TopScreen { _private: () }), bottom_screen: RefCell::new(BottomScreen { _private: () }), + _private: (), } } @@ -170,6 +171,7 @@ impl Default for Gfx { Gfx { top_screen: RefCell::new(TopScreen { _private: () }), bottom_screen: RefCell::new(BottomScreen { _private: () }), + _private: (), } } } From a879e02db5ff00eb54631949cd1379dd8b42c767 Mon Sep 17 00:00:00 2001 From: Meziu <55318903+Meziu@users.noreply.github.com> Date: Fri, 21 Jan 2022 07:06:35 +0100 Subject: [PATCH 08/15] Update ctru-rs/src/console.rs Co-authored-by: Mark Drobnak --- ctru-rs/src/console.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 33c61c1..64e9be7 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -7,7 +7,7 @@ use crate::gfx::Screen; pub struct Console<'screen> { context: Box, - _screen: Ref<'screen, dyn Screen>, + screen: Ref<'screen, dyn Screen>, } impl<'screen> Console<'screen> { From 526f7cc25e1bfeb39c934dc9ef367d3bc8ee493c Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 21 Jan 2022 14:06:19 +0100 Subject: [PATCH 09/15] Console requires mutable Screen --- ctru-rs/src/console.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 64e9be7..8763eb2 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -1,4 +1,4 @@ -use std::cell::Ref; +use std::cell::RefMut; use std::default::Default; use ctru_sys::{consoleClear, consoleInit, consoleSelect, consoleSetWindow, PrintConsole}; @@ -7,21 +7,21 @@ use crate::gfx::Screen; pub struct Console<'screen> { context: Box, - screen: Ref<'screen, dyn Screen>, + screen: RefMut<'screen, dyn Screen>, } impl<'screen> Console<'screen> { /// Initialize a console on the chosen screen, overwriting whatever was on the screen /// previously (including other consoles). The new console is automatically selected for /// printing. - pub fn init(screen: Ref<'screen, dyn Screen>) -> Self { + pub fn init(screen: RefMut<'screen, dyn Screen>) -> Self { let mut context = Box::new(PrintConsole::default()); unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; Console { context, - _screen: screen, + screen, } } From 38fef1630991101123cf22dbe747cd251bbe67f1 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 21 Jan 2022 14:26:13 +0100 Subject: [PATCH 10/15] Simple examples working with new Screen impl --- ctru-rs/examples/buttons.rs | 4 ++-- ctru-rs/examples/hello-both-screens.rs | 6 +++--- ctru-rs/examples/software-keyboard.rs | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ctru-rs/examples/buttons.rs b/ctru-rs/examples/buttons.rs index 36127cd..152ede6 100644 --- a/ctru-rs/examples/buttons.rs +++ b/ctru-rs/examples/buttons.rs @@ -1,5 +1,5 @@ use ctru::console::Console; -use ctru::gfx::{Gfx, Screen}; +use ctru::gfx::Gfx; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -9,7 +9,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let console = Console::init(&gfx, Screen::Top); + let console = Console::init(gfx.top_screen.borrow_mut()); println!("Hi there! Try pressing a button"); println!("\x1b[29;16HPress Start to exit"); diff --git a/ctru-rs/examples/hello-both-screens.rs b/ctru-rs/examples/hello-both-screens.rs index 180b1b2..67113a3 100644 --- a/ctru-rs/examples/hello-both-screens.rs +++ b/ctru-rs/examples/hello-both-screens.rs @@ -1,5 +1,5 @@ use ctru::console::Console; -use ctru::gfx::{Gfx, Screen}; +use ctru::gfx::Gfx; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -11,11 +11,11 @@ fn main() { let gfx = Gfx::default(); // Start a console on the top screen - let top_screen = Console::init(&gfx, Screen::Top); + let top_screen = Console::init(gfx.top_screen.borrow_mut()); // Start a console on the bottom screen. // The most recently initialized console will be active by default - let bottom_screen = Console::init(&gfx, Screen::Bottom); + let bottom_screen = Console::init(gfx.bottom_screen.borrow_mut()); // Let's print on the top screen first top_screen.select(); diff --git a/ctru-rs/examples/software-keyboard.rs b/ctru-rs/examples/software-keyboard.rs index b3b9347..6be9e81 100644 --- a/ctru-rs/examples/software-keyboard.rs +++ b/ctru-rs/examples/software-keyboard.rs @@ -1,6 +1,6 @@ use ctru::applets::swkbd::{Button, Swkbd}; use ctru::console::Console; -use ctru::gfx::{Gfx, Screen}; +use ctru::gfx::Gfx; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -9,7 +9,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let _console = Console::init(&gfx, Screen::Top); + let _console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enter some text or press Start to quit"); From 4a48926ee675b17b3a02669da15eeddbec574ab5 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 21 Jan 2022 15:50:43 +0100 Subject: [PATCH 11/15] gfx-wide-mode example working --- .../examples/{gfx_wide_mode.rs => gfx-wide-mode.rs} | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) rename ctru-rs/examples/{gfx_wide_mode.rs => gfx-wide-mode.rs} (62%) diff --git a/ctru-rs/examples/gfx_wide_mode.rs b/ctru-rs/examples/gfx-wide-mode.rs similarity index 62% rename from ctru-rs/examples/gfx_wide_mode.rs rename to ctru-rs/examples/gfx-wide-mode.rs index f4ba365..ac84d60 100644 --- a/ctru-rs/examples/gfx_wide_mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -1,7 +1,6 @@ extern crate ctru; use ctru::console::Console; -use ctru::gfx::Screen; use ctru::services::hid::KeyPad; use ctru::services::{Apt, Hid}; use ctru::Gfx; @@ -11,7 +10,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let _console = Console::init(&gfx, Screen::Top); + let mut _console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); @@ -23,7 +22,13 @@ fn main() { } if hid.keys_down().contains(KeyPad::KEY_A) { - gfx.set_wide_mode(!gfx.get_wide_mode()); + drop(_console); + + let wide_mode = gfx.top_screen.borrow().get_wide_mode(); + gfx.top_screen.borrow_mut().set_wide_mode(!wide_mode); + + _console = Console::init(gfx.top_screen.borrow_mut()); + println!("Press A to enable/disable wide screen mode."); } gfx.flush_buffers(); From 46a615cae561ea5e341bfecdcb73c00b629121d8 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 21 Jan 2022 15:55:24 +0100 Subject: [PATCH 12/15] Substituted hello-world example --- ctru-rs/Cargo.toml | 3 ++ ctru-rs/examples/hello-world.rs | 64 ++++++++++++++------------------- 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index f33e177..29323b4 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -18,3 +18,6 @@ pthread-3ds = { git = "https://github.com/Meziu/pthread-3ds.git" } libc = { git = "https://github.com/Meziu/libc.git" } bitflags = "1.0.0" widestring = "0.2.2" + +[dev-dependencies] +ferris-says = "0.2.1" diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index ccebef7..bb653dd 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -1,56 +1,44 @@ +extern crate ctru; use ctru::console::Console; -use ctru::gfx::{Gfx, Screen}; +use ctru::gfx::Gfx; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; -fn main() { - // Initialize ctrulib service handles. - // Service handles are internally reference-counted. When all instances of a - // service handle go out of scope, the service will be closed. - ctru::init(); +extern crate ferris_says; - // The APT service handles application management functions, such as enabling sleep - // mode and jumping to the home menu or to other applications - let apt = Apt::init().unwrap(); +use std::io::BufWriter; - // The HID service handles button and touch screen inputs. - let hid = Hid::init().unwrap(); - - // The GFX service manages the framebuffers for the top and bottom screens. +fn main() { + ctru::init(); let gfx = Gfx::default(); + let hid = Hid::init().expect("Couldn't obtain HID controller"); + let apt = Apt::init().expect("Couldn't obtain APT controller"); + let _console = Console::init(gfx.top_screen.borrow_mut()); - // Initialize a ctrulib console and direct standard output to it. - // Consoles can be initialized on both the top and bottom screens. - let _console = Console::init(&gfx, Screen::Top); + let out = b"Hello fellow Rustaceans, I'm on the Nintendo 3DS!"; + let width = 24; - // Now we can print to stdout! - println!("Hello, world!"); + let mut writer = BufWriter::new(Vec::new()); + ferris_says::say(out, width, &mut writer).unwrap(); - // We can use escape sequences to move the cursor around the terminal. - // The following text will be moved down 29 rows and right 16 characters - // before printing begins. - println!("\x1b[29;16HPress Start to exit"); + println!( + "\x1b[0;0H{}", + String::from_utf8_lossy(&writer.into_inner().unwrap()) + ); - // Main application loop. + // Main loop while apt.main_loop() { - // Flushes and swaps the framebuffers when double-buffering - // is enabled - gfx.flush_buffers(); - gfx.swap_buffers(); - - // Wait for the next frame to begin - gfx.wait_for_vblank(); - - // Scan for user input. + //Scan all the inputs. This should be done once for each frame hid.scan_input(); - // Check if the user has pressed the given button on this frame. - // If so, break out of the loop. if hid.keys_down().contains(KeyPad::KEY_START) { break; } - } + // Flush and swap framebuffers + gfx.flush_buffers(); + gfx.swap_buffers(); - // All of our service handles will drop out of scope at this point, - // triggering the end of our application. -} + //Wait for VBlank + gfx.wait_for_vblank(); + } +} \ No newline at end of file From 29b7a7d08d59040686ea454e33dc8b3626f98606 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 21 Jan 2022 15:59:27 +0100 Subject: [PATCH 13/15] Added network-sockets example --- ctru-rs/examples/network-sockets.rs | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ctru-rs/examples/network-sockets.rs diff --git a/ctru-rs/examples/network-sockets.rs b/ctru-rs/examples/network-sockets.rs new file mode 100644 index 0000000..3844b35 --- /dev/null +++ b/ctru-rs/examples/network-sockets.rs @@ -0,0 +1,69 @@ +extern crate ctru; +use ctru::console::Console; +use ctru::gfx::Gfx; +use ctru::services::apt::Apt; +use ctru::services::hid::{Hid, KeyPad}; +use ctru::services::soc::Soc; + +use std::io::{Read, Write}; +use std::net::{Shutdown, TcpListener}; +use std::time::Duration; + +fn main() { + ctru::init(); + let gfx = Gfx::default(); + let _console = Console::init(gfx.top_screen.borrow_mut()); + let hid = Hid::init().unwrap(); + let apt = Apt::init().unwrap(); + + println!("\nlibctru sockets demo\n"); + + let soc = Soc::init().unwrap(); + + let server = TcpListener::bind("0.0.0.0:80").unwrap(); + server.set_nonblocking(true).unwrap(); + + println!( + "Point your browser to http://{}/\n", + soc.host_address() + ); + + while apt.main_loop() { + gfx.wait_for_vblank(); + + match server.accept() { + Ok((mut stream, socket_addr)) => { + println!("Got connection from {}", socket_addr); + + let mut buf = [0u8; 4096]; + match stream.read(&mut buf) { + Ok(_) => { + let req_str = String::from_utf8_lossy(&buf); + println!("{}", req_str); + } + Err(e) => println!("Unable to read stream: {}", e), + } + + let response = b"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n\r\nHello world\r\n"; + + if let Err(e) = stream.write(response) { + println!("Error writing http response: {}", e); + } + + stream.shutdown(Shutdown::Both).unwrap(); + } + Err(e) => match e.kind() { + std::io::ErrorKind::WouldBlock => {} + _ => { + println!("Error accepting connection: {}", e); + std::thread::sleep(Duration::from_secs(2)); + } + }, + } + + hid.scan_input(); + if hid.keys_down().contains(KeyPad::KEY_START) { + break; + }; + } +} From 2bc475db68ebcdb30d40b9d50e5c60d99a6f2d08 Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Fri, 21 Jan 2022 16:03:32 +0100 Subject: [PATCH 14/15] Formatting --- ctru-rs/examples/gfx-wide-mode.rs | 2 +- ctru-rs/examples/hello-world.rs | 2 +- ctru-rs/examples/network-sockets.rs | 5 +---- ctru-rs/src/console.rs | 5 +---- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/ctru-rs/examples/gfx-wide-mode.rs b/ctru-rs/examples/gfx-wide-mode.rs index ac84d60..633488c 100644 --- a/ctru-rs/examples/gfx-wide-mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -26,7 +26,7 @@ fn main() { let wide_mode = gfx.top_screen.borrow().get_wide_mode(); gfx.top_screen.borrow_mut().set_wide_mode(!wide_mode); - + _console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); } diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index bb653dd..57407a5 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -41,4 +41,4 @@ fn main() { //Wait for VBlank gfx.wait_for_vblank(); } -} \ No newline at end of file +} diff --git a/ctru-rs/examples/network-sockets.rs b/ctru-rs/examples/network-sockets.rs index 3844b35..6f434ee 100644 --- a/ctru-rs/examples/network-sockets.rs +++ b/ctru-rs/examples/network-sockets.rs @@ -23,10 +23,7 @@ fn main() { let server = TcpListener::bind("0.0.0.0:80").unwrap(); server.set_nonblocking(true).unwrap(); - println!( - "Point your browser to http://{}/\n", - soc.host_address() - ); + println!("Point your browser to http://{}/\n", soc.host_address()); while apt.main_loop() { gfx.wait_for_vblank(); diff --git a/ctru-rs/src/console.rs b/ctru-rs/src/console.rs index 8763eb2..2df1495 100644 --- a/ctru-rs/src/console.rs +++ b/ctru-rs/src/console.rs @@ -19,10 +19,7 @@ impl<'screen> Console<'screen> { unsafe { consoleInit(screen.as_raw(), context.as_mut()) }; - Console { - context, - screen, - } + Console { context, screen } } /// Select this console as the current target for stdout From 366db9e04617b364207d20b34151e702e358cb7c Mon Sep 17 00:00:00 2001 From: Andrea Ciliberti Date: Sat, 22 Jan 2022 14:00:14 +0100 Subject: [PATCH 15/15] Fixed clippy suggestions --- ctru-rs/examples/gfx-wide-mode.rs | 8 +++----- ctru-rs/examples/hello-world.rs | 3 --- ctru-rs/examples/network-sockets.rs | 1 - ctru-rs/src/gfx.rs | 22 +++++++++------------- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/ctru-rs/examples/gfx-wide-mode.rs b/ctru-rs/examples/gfx-wide-mode.rs index 633488c..fcf3fa9 100644 --- a/ctru-rs/examples/gfx-wide-mode.rs +++ b/ctru-rs/examples/gfx-wide-mode.rs @@ -1,5 +1,3 @@ -extern crate ctru; - use ctru::console::Console; use ctru::services::hid::KeyPad; use ctru::services::{Apt, Hid}; @@ -10,7 +8,7 @@ fn main() { let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let gfx = Gfx::default(); - let mut _console = Console::init(gfx.top_screen.borrow_mut()); + let mut console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); @@ -22,12 +20,12 @@ fn main() { } if hid.keys_down().contains(KeyPad::KEY_A) { - drop(_console); + drop(console); let wide_mode = gfx.top_screen.borrow().get_wide_mode(); gfx.top_screen.borrow_mut().set_wide_mode(!wide_mode); - _console = Console::init(gfx.top_screen.borrow_mut()); + console = Console::init(gfx.top_screen.borrow_mut()); println!("Press A to enable/disable wide screen mode."); } diff --git a/ctru-rs/examples/hello-world.rs b/ctru-rs/examples/hello-world.rs index 57407a5..930549f 100644 --- a/ctru-rs/examples/hello-world.rs +++ b/ctru-rs/examples/hello-world.rs @@ -1,11 +1,8 @@ -extern crate ctru; use ctru::console::Console; use ctru::gfx::Gfx; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; -extern crate ferris_says; - use std::io::BufWriter; fn main() { diff --git a/ctru-rs/examples/network-sockets.rs b/ctru-rs/examples/network-sockets.rs index 6f434ee..84978e9 100644 --- a/ctru-rs/examples/network-sockets.rs +++ b/ctru-rs/examples/network-sockets.rs @@ -1,4 +1,3 @@ -extern crate ctru; use ctru::console::Console; use ctru::gfx::Gfx; use ctru::services::apt::Apt; diff --git a/ctru-rs/src/gfx.rs b/ctru-rs/src/gfx.rs index aec2572..1092640 100644 --- a/ctru-rs/src/gfx.rs +++ b/ctru-rs/src/gfx.rs @@ -46,12 +46,10 @@ pub trait Screen { } } -pub struct TopScreen { - _private: (), -} -pub struct BottomScreen { - _private: (), -} +#[non_exhaustive] +pub struct TopScreen; +#[non_exhaustive] +pub struct BottomScreen; #[derive(Copy, Clone, Debug)] /// Side of top screen framebuffer @@ -68,10 +66,10 @@ pub enum Side { /// provides helper functions and utilities for software rendering. /// /// The service exits when this struct is dropped. +#[non_exhaustive] pub struct Gfx { pub top_screen: RefCell, pub bottom_screen: RefCell, - _private: (), } impl Gfx { @@ -88,9 +86,8 @@ impl Gfx { ctru_sys::gfxInit(top_fb_fmt.into(), bottom_fb_fmt.into(), use_vram_buffers); } Gfx { - top_screen: RefCell::new(TopScreen { _private: () }), - bottom_screen: RefCell::new(BottomScreen { _private: () }), - _private: (), + top_screen: RefCell::new(TopScreen), + bottom_screen: RefCell::new(BottomScreen), } } @@ -169,9 +166,8 @@ impl Default for Gfx { fn default() -> Self { unsafe { ctru_sys::gfxInitDefault() }; Gfx { - top_screen: RefCell::new(TopScreen { _private: () }), - bottom_screen: RefCell::new(BottomScreen { _private: () }), - _private: (), + top_screen: RefCell::new(TopScreen), + bottom_screen: RefCell::new(BottomScreen), } } }