Skip to content

input: Split out InputEventJava into Motion and Key with Deref #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ndk/src/data_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ pub enum DataSpaceTransfer {
/// n = 0.25 * 2610 / 4096 = 0.1593017578125
/// ```
/// - `L`: luminance of image 0 <= L <= 1 for HDR colorimetry.
/// `L = 1` corresponds to `10000 cd/m2`
/// `L = 1` corresponds to `10000 cd/m2`
#[doc(alias = "TRANSFER_ST2084")]
St2084 = ffi::ADataSpace::TRANSFER_ST2084.0,
/// ARIB STD-B67 Hybrid Log Gamma.
Expand All @@ -589,7 +589,7 @@ pub enum DataSpaceTransfer {
/// r = 0.5
/// ```
/// - `L`: luminance of image `0 <= L` for HDR colorimetry.
/// `L = 1` corresponds to reference white level of `100 cd/m2`
/// `L = 1` corresponds to reference white level of `100 cd/m2`
/// - `E`: corresponding electrical signal
#[doc(alias = "TRANSFER_HLG")]
HLG = ffi::ADataSpace::TRANSFER_HLG.0,
Expand Down
66 changes: 46 additions & 20 deletions ndk/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! [`android.view.MotionEvent`]: https://developer.android.com/reference/android/view/MotionEvent
//! [`android.view.KeyEvent`]: https://developer.android.com/reference/android/view/KeyEvent

use std::ptr::NonNull;
use std::{ops::Deref, ptr::NonNull};

#[cfg(feature = "api-level-31")]
use jni_sys::{jobject, JNIEnv};
Expand All @@ -26,25 +26,53 @@ pub enum InputEvent {
KeyEvent(KeyEvent),
}

/// Wraps a Java [`InputEvent`] acquired from [`KeyEvent::from_java()`] or
/// [`MotionEvent::from_java()`] with respective [`Drop`] semantics.
/// Wraps a Java [`MotionEvent`] acquired from [`MotionEvent::from_java()`] with respective [`Drop`] semantics.
#[cfg(feature = "api-level-31")]
#[derive(Debug)]
pub struct InputEventJava(InputEvent);
pub struct MotionEventJava(MotionEvent);

#[cfg(feature = "api-level-31")]
impl Drop for InputEventJava {
/// Releases interface objects created by [`KeyEvent::from_java()`] or
/// [`MotionEvent::from_java()`].
impl Deref for MotionEventJava {
type Target = MotionEvent;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(feature = "api-level-31")]
impl Drop for MotionEventJava {
/// Releases interface objects created by [`MotionEvent::from_java()`].
///
/// The underlying Java object remains valid and does not change its state.
#[doc(alias = "AInputEvent_release")]
fn drop(&mut self) {
unsafe { ffi::AInputEvent_release(self.0.ptr.as_ptr().cast()) }
}
}

/// Wraps a Java [`KeyEvent`] acquired from [`KeyEvent::from_java()`] with respective [`Drop`] semantics.
#[cfg(feature = "api-level-31")]
#[derive(Debug)]
pub struct KeyEventJava(KeyEvent);

#[cfg(feature = "api-level-31")]
impl Deref for KeyEventJava {
type Target = KeyEvent;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(feature = "api-level-31")]
impl Drop for KeyEventJava {
/// Releases interface objects created by [`KeyEvent::from_java()`].
///
/// The underlying Java object remains valid and does not change its state.
#[doc(alias = "AInputEvent_release")]
fn drop(&mut self) {
let ptr = match self.0 {
InputEvent::MotionEvent(MotionEvent { ptr })
| InputEvent::KeyEvent(KeyEvent { ptr }) => ptr.as_ptr().cast(),
};
unsafe { ffi::AInputEvent_release(ptr) }
unsafe { ffi::AInputEvent_release(self.0.ptr.as_ptr().cast()) }
}
}

Expand Down Expand Up @@ -428,11 +456,11 @@ impl MotionEvent {
/// [`android.view.MotionEvent`]: https://developer.android.com/reference/android/view/MotionEvent
#[cfg(feature = "api-level-31")]
#[doc(alias = "AMotionEvent_fromJava")]
pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option<InputEventJava> {
pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option<MotionEventJava> {
let ptr = unsafe { ffi::AMotionEvent_fromJava(env, key_event) };
Some(InputEventJava(InputEvent::MotionEvent(Self::from_ptr(
NonNull::new(ptr.cast_mut())?,
))))
Some(MotionEventJava(Self::from_ptr(NonNull::new(
ptr.cast_mut(),
)?)))
}

/// Returns a pointer to the native [`ffi::AInputEvent`].
Expand Down Expand Up @@ -1421,11 +1449,9 @@ impl KeyEvent {
/// [`android.view.KeyEvent`]: https://developer.android.com/reference/android/view/KeyEvent
#[cfg(feature = "api-level-31")]
#[doc(alias = "AKeyEvent_fromJava")]
pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option<InputEventJava> {
pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option<KeyEventJava> {
let ptr = unsafe { ffi::AKeyEvent_fromJava(env, key_event) };
Some(InputEventJava(InputEvent::KeyEvent(Self::from_ptr(
NonNull::new(ptr.cast_mut())?,
))))
Some(KeyEventJava(Self::from_ptr(NonNull::new(ptr.cast_mut())?)))
}

/// Returns a pointer to the native [`ffi::AInputEvent`].
Expand Down
Loading