From fc0e124f4390deba03193d277dd4fe93ae5443a3 Mon Sep 17 00:00:00 2001 From: Fenrir Date: Tue, 6 Feb 2018 23:06:43 -0700 Subject: [PATCH] Use bitflags 1.0 for button presses --- ctru-rs/Cargo.toml | 4 ++-- ctru-rs/src/services/fs.rs | 21 +++++++++++---------- ctru-rs/src/services/hid.rs | 8 ++++---- examples/src/bin/buttons.rs | 14 +++++++------- examples/src/bin/hello-both-screens.rs | 8 ++++---- examples/src/bin/hello-world.rs | 4 ++-- 6 files changed, 30 insertions(+), 29 deletions(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index 69b2db1..c597f4a 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -4,7 +4,7 @@ description = "A safe wrapper around smealum's ctrulib." license = "https://en.wikipedia.org/wiki/Zlib_License" name = "ctru-rs" links = "ctru" -version = "0.6.0" +version = "0.7.0" [lib] crate-type = ["rlib"] @@ -15,7 +15,7 @@ path = "../ctru-sys" version = "0.4" [dependencies.bitflags] -version = "0.9.0" +version = "1.0.0" [dependencies.widestring] version = "0.2.2" diff --git a/ctru-rs/src/services/fs.rs b/ctru-rs/src/services/fs.rs index 113660b..29d30b6 100644 --- a/ctru-rs/src/services/fs.rs +++ b/ctru-rs/src/services/fs.rs @@ -455,7 +455,7 @@ impl File { self.offset, buf.as_ptr() as _, buf.len() as u32, - FS_WRITE_UPDATE_TIME.bits() + FsWrite::FS_WRITE_UPDATE_TIME.bits() ); self.offset += n_written as u64; if r < 0 { @@ -470,7 +470,7 @@ impl File { impl Metadata { /// Returns whether this metadata is for a directory. pub fn is_dir(&self) -> bool { - self.attributes == self.attributes | FS_ATTRIBUTE_DIRECTORY.bits() + self.attributes == self.attributes | FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits() } /// Returns whether this metadata is for a regular file. @@ -613,12 +613,13 @@ impl OpenOptions { fn get_open_flags(&self) -> FsOpen { match (self.read, self.write || self.append, self.create) { - (true, false, false) => FS_OPEN_READ, - (false, true, false) => FS_OPEN_WRITE, - (false, true, true) => FS_OPEN_WRITE | FS_OPEN_CREATE, - (true, false, true) => FS_OPEN_READ | FS_OPEN_CREATE, - (true, true, false) => FS_OPEN_READ | FS_OPEN_WRITE, - (true, true, true) => FS_OPEN_READ | FS_OPEN_WRITE | FS_OPEN_CREATE, + (true, false, false) => FsOpen::FS_OPEN_READ, + (false, true, false) => FsOpen::FS_OPEN_WRITE, + (false, true, true) => FsOpen::FS_OPEN_WRITE | FsOpen::FS_OPEN_CREATE, + (true, false, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_CREATE, + (true, true, false) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE, + (true, true, true) => FsOpen::FS_OPEN_READ | FsOpen::FS_OPEN_WRITE | + FsOpen::FS_OPEN_CREATE, _ => FsOpen::empty(), //failure case } } @@ -688,7 +689,7 @@ pub fn create_dir>(arch: &Archive, path: P) -> IoResult<()> { let path = to_utf16(path.as_ref()); let fs_path = ::libctru::fsMakePath(PathType::UTF16.into(), path.as_ptr() as _); let r = ::libctru::FSUSER_CreateDirectory(arch.handle, fs_path, - FS_ATTRIBUTE_DIRECTORY.bits()); + FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits()); if r < 0 { Err(IoError::new(IoErrorKind::Other, ::Error::from(r))) } else { @@ -725,7 +726,7 @@ pub fn metadata>(arch: &Archive, path: P) -> IoResult { let maybe_dir = read_dir(&arch, path.as_ref()); match (maybe_file, maybe_dir) { (Ok(file), _) => file.metadata(), - (_, Ok(_dir)) => Ok(Metadata { attributes: FS_ATTRIBUTE_DIRECTORY.bits(), size: 0 }), + (_, Ok(_dir)) => Ok(Metadata { attributes: FsAttribute::FS_ATTRIBUTE_DIRECTORY.bits(), size: 0 }), (Err(e), _) => Err(e), } } diff --git a/ctru-rs/src/services/hid.rs b/ctru-rs/src/services/hid.rs index b70d890..12aab2d 100644 --- a/ctru-rs/src/services/hid.rs +++ b/ctru-rs/src/services/hid.rs @@ -34,10 +34,10 @@ bitflags! { const KEY_CPAD_UP = 1u32 << 30; const KEY_CPAD_DOWN = 1u32 << 31; // convenience catch-all for the dpad and cpad - const KEY_UP = KEY_DUP.bits | KEY_CPAD_UP.bits; - const KEY_DOWN = KEY_DDOWN.bits | KEY_CPAD_DOWN.bits; - const KEY_LEFT = KEY_DLEFT.bits | KEY_CPAD_LEFT.bits; - const KEY_RIGHT = KEY_DRIGHT.bits | KEY_CPAD_RIGHT.bits; + const KEY_UP = KeyPad::KEY_DUP.bits | KeyPad::KEY_CPAD_UP.bits; + const KEY_DOWN = KeyPad::KEY_DDOWN.bits | KeyPad::KEY_CPAD_DOWN.bits; + const KEY_LEFT = KeyPad::KEY_DLEFT.bits | KeyPad::KEY_CPAD_LEFT.bits; + const KEY_RIGHT = KeyPad::KEY_DRIGHT.bits | KeyPad::KEY_CPAD_RIGHT.bits; } } diff --git a/examples/src/bin/buttons.rs b/examples/src/bin/buttons.rs index 00dd33e..b906e59 100644 --- a/examples/src/bin/buttons.rs +++ b/examples/src/bin/buttons.rs @@ -3,14 +3,14 @@ extern crate ctru; use ctru::gfx::Gfx; use ctru::console::Console; use ctru::services::apt::Apt; -use ctru::services::hid::{self, Hid, KeyPad}; +use ctru::services::hid::{Hid, KeyPad}; fn main() { // Setup services let apt = Apt::init().unwrap(); let hid = Hid::init().unwrap(); let mut gfx = Gfx::default(); - let mut console = Console::default(); + let console = Console::default(); println!("Hi there! Try pressing a button"); println!("\x1b[29;16HPress Start to exit"); @@ -47,19 +47,19 @@ fn main() { // You can also use the .bits() method to do direct comparisons on // the underlying bits - if keys.contains(hid::KEY_A) { + if keys.contains(KeyPad::KEY_A) { println!("You held A!"); } - if keys.bits() & hid::KEY_B.bits() != 0 { + if keys.bits() & KeyPad::KEY_B.bits() != 0 { println!("You held B!"); } - if keys.contains(hid::KEY_X | hid::KEY_Y) { + if keys.contains(KeyPad::KEY_X | KeyPad::KEY_Y) { println!("You held X and Y!"); } - if keys.intersects(hid::KEY_L | hid::KEY_R | hid::KEY_ZL | hid::KEY_ZR) { + if keys.intersects(KeyPad::KEY_L | KeyPad::KEY_R | KeyPad::KEY_ZL | KeyPad::KEY_ZR) { println!("You held a shoulder button!"); } - if keys.intersects(hid::KEY_START) { + if keys.intersects(KeyPad::KEY_START) { println!("See ya!"); break } diff --git a/examples/src/bin/hello-both-screens.rs b/examples/src/bin/hello-both-screens.rs index 627d9b0..1c214e9 100644 --- a/examples/src/bin/hello-both-screens.rs +++ b/examples/src/bin/hello-both-screens.rs @@ -3,7 +3,7 @@ extern crate ctru; use ctru::gfx::{Gfx, Screen}; use ctru::console::Console; use ctru::services::apt::Apt; -use ctru::services::hid::{self, Hid}; +use ctru::services::hid::{Hid, KeyPad}; fn main() { // Initialize services @@ -12,11 +12,11 @@ fn main() { let mut gfx = Gfx::default(); // Start a console on the top screen - let mut top_screen = Console::init(Screen::Top); + let top_screen = Console::init(Screen::Top); // Start a console on the bottom screen. // The most recently initialized console will be active by default - let mut bottom_screen = Console::init(Screen::Bottom); + let bottom_screen = Console::init(Screen::Bottom); // Let's print on the top screen first top_screen.select(); @@ -35,7 +35,7 @@ fn main() { gfx.swap_buffers(); hid.scan_input(); - if hid.keys_down().contains(hid::KEY_START) { + if hid.keys_down().contains(KeyPad::KEY_START) { break; } } diff --git a/examples/src/bin/hello-world.rs b/examples/src/bin/hello-world.rs index 6daee98..fa70c1d 100644 --- a/examples/src/bin/hello-world.rs +++ b/examples/src/bin/hello-world.rs @@ -3,7 +3,7 @@ extern crate ctru; use ctru::gfx::Gfx; use ctru::console::Console; use ctru::services::apt::Apt; -use ctru::services::hid::{self, Hid}; +use ctru::services::hid::{Hid, KeyPad}; fn main() { // Initialize ctrulib service handles. @@ -45,7 +45,7 @@ fn main() { 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(hid::KEY_START) { + if hid.keys_down().contains(KeyPad::KEY_START) { break; } }