|
|
@ -1,4 +1,5 @@ |
|
|
|
use ::Result; |
|
|
|
use core::convert::Into; |
|
|
|
|
|
|
|
use core::marker::PhantomData; |
|
|
|
|
|
|
|
|
|
|
|
use ::raw::services::hid; |
|
|
|
use ::raw::services::hid; |
|
|
|
|
|
|
|
|
|
|
@ -34,11 +35,12 @@ pub enum PadKey { |
|
|
|
Right |
|
|
|
Right |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn to_raw_padkey(key: PadKey) -> u32 { |
|
|
|
impl From<PadKey> for u32 { |
|
|
|
|
|
|
|
fn from(p: PadKey) -> u32 { |
|
|
|
use ::raw::services::hid::PAD_KEY::*; |
|
|
|
use ::raw::services::hid::PAD_KEY::*; |
|
|
|
use self::PadKey::*; |
|
|
|
use self::PadKey::*; |
|
|
|
|
|
|
|
|
|
|
|
match key { |
|
|
|
match p { |
|
|
|
Up => KEY_DUP as u32 | KEY_CPAD_UP as u32, |
|
|
|
Up => KEY_DUP as u32 | KEY_CPAD_UP as u32, |
|
|
|
Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32, |
|
|
|
Down => KEY_DDOWN as u32 | KEY_CPAD_DOWN as u32, |
|
|
|
Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32, |
|
|
|
Left => KEY_DLEFT as u32 | KEY_CPAD_LEFT as u32, |
|
|
@ -68,67 +70,65 @@ fn to_raw_padkey(key: PadKey) -> u32 { |
|
|
|
DPadUp => KEY_DUP as u32, |
|
|
|
DPadUp => KEY_DUP as u32, |
|
|
|
DPadDown => KEY_DDOWN as u32 |
|
|
|
DPadDown => KEY_DDOWN as u32 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn init() -> Result { |
|
|
|
pub struct Hid { |
|
|
|
unsafe { |
|
|
|
pd: PhantomData<i32> |
|
|
|
// TODO allow sharedMem argument?
|
|
|
|
|
|
|
|
return hid::hidInit(0 as *mut u32); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn exit() -> () { |
|
|
|
impl Hid { |
|
|
|
|
|
|
|
pub fn new() -> Result<Hid, i32> { |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
hid::hidExit(); |
|
|
|
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.
|
|
|
|
pub fn scan_input(&mut self) { |
|
|
|
///
|
|
|
|
unsafe { hid::hidScanInput() }; |
|
|
|
/// # 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 key_down(key: PadKey) -> bool { |
|
|
|
pub fn key_down(&self, key: PadKey) -> bool { |
|
|
|
|
|
|
|
let k: u32 = key.into(); |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
if hid::hidKeysDown() & to_raw_padkey(key) != 0 { |
|
|
|
if hid::hidKeysDown() & k != 0 { |
|
|
|
return true; |
|
|
|
true |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
false |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn key_held(key: PadKey) -> bool { |
|
|
|
pub fn key_held(&self, key: PadKey) -> bool { |
|
|
|
|
|
|
|
let k: u32 = key.into(); |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
if hid::hidKeysHeld() & to_raw_padkey(key) != 0 { |
|
|
|
if hid::hidKeysHeld() & k != 0 { |
|
|
|
return true; |
|
|
|
true |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
false |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn key_up(key: PadKey) -> bool { |
|
|
|
pub fn key_up(&self, key: PadKey) -> bool { |
|
|
|
|
|
|
|
let k: u32 = key.into(); |
|
|
|
unsafe { |
|
|
|
unsafe { |
|
|
|
if hid::hidKeysUp() & to_raw_padkey(key) != 0 { |
|
|
|
if hid::hidKeysUp() & k != 0 { |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for Hid { |
|
|
|
|
|
|
|
fn drop(&mut self) { |
|
|
|
|
|
|
|
unsafe { hid::hidExit() }; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|