Browse Source

Use bitflags 1.0 for button presses

pull/10/head
Fenrir 7 years ago committed by FenrirWolf
parent
commit
fc0e124f43
  1. 4
      ctru-rs/Cargo.toml
  2. 21
      ctru-rs/src/services/fs.rs
  3. 8
      ctru-rs/src/services/hid.rs
  4. 14
      examples/src/bin/buttons.rs
  5. 8
      examples/src/bin/hello-both-screens.rs
  6. 4
      examples/src/bin/hello-world.rs

4
ctru-rs/Cargo.toml

@ -4,7 +4,7 @@ description = "A safe wrapper around smealum's ctrulib." @@ -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" @@ -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"

21
ctru-rs/src/services/fs.rs

@ -455,7 +455,7 @@ impl File { @@ -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 { @@ -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 { @@ -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<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<()> { @@ -688,7 +689,7 @@ pub fn create_dir<P: AsRef<Path>>(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<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<Metadata> { @@ -725,7 +726,7 @@ pub fn metadata<P: AsRef<Path>>(arch: &Archive, path: P) -> IoResult<Metadata> {
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),
}
}

8
ctru-rs/src/services/hid.rs

@ -34,10 +34,10 @@ bitflags! { @@ -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;
}
}

14
examples/src/bin/buttons.rs

@ -3,14 +3,14 @@ extern crate ctru; @@ -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() { @@ -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
}

8
examples/src/bin/hello-both-screens.rs

@ -3,7 +3,7 @@ extern crate ctru; @@ -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() { @@ -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() { @@ -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;
}
}

4
examples/src/bin/hello-world.rs

@ -3,7 +3,7 @@ extern crate ctru; @@ -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() { @@ -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;
}
}

Loading…
Cancel
Save