use std::marker::PhantomData;
pub use ndk::event::{
Axis, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction,
MotionEventFlags, Pointer, PointersIter,
};
use crate::input::{Class, Source};
#[derive(Debug)]
#[repr(transparent)]
pub struct MotionEvent<'a> {
ndk_event: ndk::event::MotionEvent,
_lifetime: PhantomData<&'a ndk::event::MotionEvent>,
}
impl<'a> MotionEvent<'a> {
pub(crate) fn new(ndk_event: ndk::event::MotionEvent) -> Self {
Self {
ndk_event,
_lifetime: PhantomData,
}
}
pub(crate) fn into_ndk_event(self) -> ndk::event::MotionEvent {
self.ndk_event
}
#[inline]
pub fn source(&self) -> Source {
let source =
unsafe { ndk_sys::AInputEvent_getSource(self.ndk_event.ptr().as_ptr()) as u32 };
source.try_into().unwrap_or(Source::Unknown)
}
#[inline]
pub fn class(&self) -> Class {
Class::from(self.source())
}
#[inline]
pub fn device_id(&self) -> i32 {
self.ndk_event.device_id()
}
#[inline]
pub fn action(&self) -> MotionAction {
self.ndk_event.action()
}
#[inline]
pub fn pointer_index(&self) -> usize {
self.ndk_event.pointer_index()
}
#[inline]
pub fn pointer_count(&self) -> usize {
self.ndk_event.pointer_count()
}
#[inline]
pub fn pointers(&self) -> PointersIter<'_> {
self.ndk_event.pointers()
}
#[inline]
pub fn pointer_at_index(&self, index: usize) -> Pointer<'_> {
self.ndk_event.pointer_at_index(index)
}
#[inline]
pub fn meta_state(&self) -> MetaState {
self.ndk_event.meta_state()
}
#[inline]
pub fn button_state(&self) -> ButtonState {
self.ndk_event.button_state()
}
#[inline]
pub fn down_time(&self) -> i64 {
self.ndk_event.down_time()
}
#[inline]
pub fn edge_flags(&self) -> EdgeFlags {
self.ndk_event.edge_flags()
}
#[inline]
pub fn event_time(&self) -> i64 {
self.ndk_event.event_time()
}
#[inline]
pub fn flags(&self) -> MotionEventFlags {
self.ndk_event.flags()
}
#[inline]
pub fn x_precision(&self) -> f32 {
self.ndk_event.x_precision()
}
#[inline]
pub fn y_precision(&self) -> f32 {
self.ndk_event.y_precision()
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct KeyEvent<'a> {
ndk_event: ndk::event::KeyEvent,
_lifetime: PhantomData<&'a ndk::event::KeyEvent>,
}
impl<'a> KeyEvent<'a> {
pub(crate) fn new(ndk_event: ndk::event::KeyEvent) -> Self {
Self {
ndk_event,
_lifetime: PhantomData,
}
}
pub(crate) fn into_ndk_event(self) -> ndk::event::KeyEvent {
self.ndk_event
}
#[inline]
pub fn source(&self) -> Source {
let source =
unsafe { ndk_sys::AInputEvent_getSource(self.ndk_event.ptr().as_ptr()) as u32 };
source.try_into().unwrap_or(Source::Unknown)
}
#[inline]
pub fn class(&self) -> Class {
Class::from(self.source())
}
#[inline]
pub fn device_id(&self) -> i32 {
self.ndk_event.device_id()
}
#[inline]
pub fn action(&self) -> KeyAction {
self.ndk_event.action()
}
#[inline]
pub fn down_time(&self) -> i64 {
self.ndk_event.down_time()
}
#[inline]
pub fn event_time(&self) -> i64 {
self.ndk_event.event_time()
}
#[inline]
pub fn key_code(&self) -> Keycode {
self.ndk_event.key_code()
}
#[inline]
pub fn repeat_count(&self) -> i32 {
self.ndk_event.repeat_count()
}
#[inline]
pub fn scan_code(&self) -> i32 {
self.ndk_event.scan_code()
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum InputEvent<'a> {
MotionEvent(self::MotionEvent<'a>),
KeyEvent(self::KeyEvent<'a>),
}