From f10d5495621b2f8f475949bb5551acf37ebc6de0 Mon Sep 17 00:00:00 2001 From: Ronald Kinard Date: Thu, 17 Sep 2015 23:27:37 -0500 Subject: [PATCH] Add Hid struct. --- src/services/hid.rs | 158 ++++++++++++++++++++++---------------------- src/services/mod.rs | 2 + 2 files changed, 81 insertions(+), 79 deletions(-) diff --git a/src/services/hid.rs b/src/services/hid.rs index 03c9c4e..c0f1ed7 100644 --- a/src/services/hid.rs +++ b/src/services/hid.rs @@ -1,4 +1,5 @@ -use ::Result; +use core::convert::Into; +use core::marker::PhantomData; use ::raw::services::hid; @@ -34,101 +35,100 @@ pub enum PadKey { Right } -fn to_raw_padkey(key: PadKey) -> u32 { - use ::raw::services::hid::PAD_KEY::*; - use self::PadKey::*; +impl From for u32 { + fn from(p: PadKey) -> u32 { + use ::raw::services::hid::PAD_KEY::*; + use self::PadKey::*; - match key { - Up => KEY_DUP as u32 | KEY_CPAD_UP as u32, - Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32, - Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32, - Right => KEY_DRIGHT as u32 | KEY_CPAD_RIGHT as u32, + match p { + Up => KEY_DUP as u32 | KEY_CPAD_UP as u32, + Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32, + Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32, + Right => KEY_DRIGHT as u32 | KEY_CPAD_RIGHT as u32, - A => KEY_A as u32, - B => KEY_B as u32, - X => KEY_X as u32, - Y => KEY_Y as u32, - L => KEY_L as u32, - R => KEY_R as u32, - ZL => KEY_ZL as u32, - ZR => KEY_ZR as u32, - Start => KEY_START as u32, - Select => KEY_SELECT as u32, - Touch => KEY_TOUCH as u32, - CSRight => KEY_CSTICK_RIGHT as u32, - CSLeft => KEY_CSTICK_LEFT as u32, - CSUp => KEY_CSTICK_UP as u32, - CSDown => KEY_CSTICK_DOWN as u32, - CRight => KEY_CPAD_RIGHT as u32, - CLeft => KEY_CPAD_LEFT as u32, - CDown => KEY_CPAD_DOWN as u32, - CUp => KEY_CPAD_UP as u32, - DPadLeft => KEY_DLEFT as u32, - DPadRight => KEY_DRIGHT as u32, - DPadUp => KEY_DUP as u32, - DPadDown => KEY_DDOWN as u32 + A => KEY_A as u32, + B => KEY_B as u32, + X => KEY_X as u32, + Y => KEY_Y as u32, + L => KEY_L as u32, + R => KEY_R as u32, + ZL => KEY_ZL as u32, + ZR => KEY_ZR as u32, + Start => KEY_START as u32, + Select => KEY_SELECT as u32, + Touch => KEY_TOUCH as u32, + CSRight => KEY_CSTICK_RIGHT as u32, + CSLeft => KEY_CSTICK_LEFT as u32, + CSUp => KEY_CSTICK_UP as u32, + CSDown => KEY_CSTICK_DOWN as u32, + CRight => KEY_CPAD_RIGHT as u32, + CLeft => KEY_CPAD_LEFT as u32, + CDown => KEY_CPAD_DOWN as u32, + CUp => KEY_CPAD_UP as u32, + DPadLeft => KEY_DLEFT as u32, + DPadRight => KEY_DRIGHT as u32, + DPadUp => KEY_DUP as u32, + DPadDown => KEY_DDOWN as u32 + } } } -pub fn init() -> Result { - unsafe { - // TODO allow sharedMem argument? - return hid::hidInit(0 as *mut u32); - } +pub struct Hid { + pd: PhantomData } -pub fn exit() -> () { - unsafe { - hid::hidExit(); +impl Hid { + pub fn new() -> Result { + unsafe { + let r = hid::hidInit(hid::HID_SHAREDMEM_DEFAULT as *mut u32); + if r < 0 { + Err(r) + } else { + Ok(Hid { pd: PhantomData }) + } + } } -} -/// Update ctrulib's button states. -/// -/// # Examples -/// -/// ``` -/// use ctru::service::apt; -/// -/// apt::main_loop(|| { -/// scan_input(); -/// if key_down(PadKey::A) { -/// apt::set_status(apt::AppStatus::Exiting); -/// } -/// }); -/// ``` -pub fn scan_input() -> () { - unsafe { - hid::hidScanInput(); + pub fn scan_input(&mut self) { + unsafe { hid::hidScanInput() }; } -} -pub fn key_down(key: PadKey) -> bool { - unsafe { - if hid::hidKeysDown() & to_raw_padkey(key) != 0 { - return true; - } else { - return false; + pub fn key_down(&self, key: PadKey) -> bool { + let k: u32 = key.into(); + unsafe { + if hid::hidKeysDown() & k != 0 { + true + } else { + false + } } } -} -pub fn key_held(key: PadKey) -> bool { - unsafe { - if hid::hidKeysHeld() & to_raw_padkey(key) != 0 { - return true; - } else { - return false; + pub fn key_held(&self, key: PadKey) -> bool { + let k: u32 = key.into(); + unsafe { + if hid::hidKeysHeld() & k != 0 { + true + } else { + false + } } } -} -pub fn key_up(key: PadKey) -> bool { - unsafe { - if hid::hidKeysUp() & to_raw_padkey(key) != 0 { - return true; - } else { - return false; + pub fn key_up(&self, key: PadKey) -> bool { + let k: u32 = key.into(); + unsafe { + if hid::hidKeysUp() & k != 0 { + return true; + } else { + return false; + } } } } + +impl Drop for Hid { + fn drop(&mut self) { + unsafe { hid::hidExit() }; + } +} diff --git a/src/services/mod.rs b/src/services/mod.rs index 1bdeae7..d5a8f4d 100644 --- a/src/services/mod.rs +++ b/src/services/mod.rs @@ -1,3 +1,5 @@ pub mod apt; pub mod hid; pub mod gsp; + +pub use self::hid::Hid;