Skip to content

Commit 20fc5e6

Browse files
committed
input: Split out InputEventJava into Motion and Key with Deref
When I implemented the `from_java()` constructors for `MotionEvent` and `KeyEvent` in #456, I used a single `InputEventJava` wrapper since that could wrap the existing `enum InputEvent` and only need a single destructor calling `AInputEvent_release()` (which must only be called when the input event was created from Java). This however requires existing callers to `MotionEvent::from_java()` and `KeyEvent::from_java()` to _unwrap_/unpack that nested `enum InputEvent` again in order to get access to the underlying native methods, despite already calling a specific constructor method (since Android makes a distinction between both types). Not that that is even reachable, since the nested `InputEvent` member was private and there was no `Deref` into `&InputEvent` anywhere making it impossible to use this API in any meaningful way. Solve both issues by splitting the `struct` into a `Motion` and `Key` variant, and implement `Deref` on both to their respective type. No wrapper `InputEventJava` remains since there does not appear to be any reason to pass both of these types into a single API.
1 parent 29f27b7 commit 20fc5e6

File tree

2 files changed

+48
-22
lines changed

2 files changed

+48
-22
lines changed

ndk/src/data_space.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ pub enum DataSpaceTransfer {
574574
/// n = 0.25 * 2610 / 4096 = 0.1593017578125
575575
/// ```
576576
/// - `L`: luminance of image 0 <= L <= 1 for HDR colorimetry.
577-
/// `L = 1` corresponds to `10000 cd/m2`
577+
/// `L = 1` corresponds to `10000 cd/m2`
578578
#[doc(alias = "TRANSFER_ST2084")]
579579
St2084 = ffi::ADataSpace::TRANSFER_ST2084.0,
580580
/// ARIB STD-B67 Hybrid Log Gamma.
@@ -589,7 +589,7 @@ pub enum DataSpaceTransfer {
589589
/// r = 0.5
590590
/// ```
591591
/// - `L`: luminance of image `0 <= L` for HDR colorimetry.
592-
/// `L = 1` corresponds to reference white level of `100 cd/m2`
592+
/// `L = 1` corresponds to reference white level of `100 cd/m2`
593593
/// - `E`: corresponding electrical signal
594594
#[doc(alias = "TRANSFER_HLG")]
595595
HLG = ffi::ADataSpace::TRANSFER_HLG.0,

ndk/src/event.rs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! [`android.view.MotionEvent`]: https://developer.android.com/reference/android/view/MotionEvent
1111
//! [`android.view.KeyEvent`]: https://developer.android.com/reference/android/view/KeyEvent
1212
13-
use std::ptr::NonNull;
13+
use std::{ops::Deref, ptr::NonNull};
1414

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

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

3534
#[cfg(feature = "api-level-31")]
36-
impl Drop for InputEventJava {
37-
/// Releases interface objects created by [`KeyEvent::from_java()`] or
38-
/// [`MotionEvent::from_java()`].
35+
impl Deref for MotionEventJava {
36+
type Target = MotionEvent;
37+
38+
fn deref(&self) -> &Self::Target {
39+
&self.0
40+
}
41+
}
42+
43+
#[cfg(feature = "api-level-31")]
44+
impl Drop for MotionEventJava {
45+
/// Releases interface objects created by [`MotionEvent::from_java()`].
46+
///
47+
/// The underlying Java object remains valid and does not change its state.
48+
#[doc(alias = "AInputEvent_release")]
49+
fn drop(&mut self) {
50+
unsafe { ffi::AInputEvent_release(self.0.ptr.as_ptr().cast()) }
51+
}
52+
}
53+
54+
/// Wraps a Java [`KeyEvent`] acquired from [`KeyEvent::from_java()`] with respective [`Drop`] semantics.
55+
#[cfg(feature = "api-level-31")]
56+
#[derive(Debug)]
57+
pub struct KeyEventJava(KeyEvent);
58+
59+
#[cfg(feature = "api-level-31")]
60+
impl Deref for KeyEventJava {
61+
type Target = KeyEvent;
62+
63+
fn deref(&self) -> &Self::Target {
64+
&self.0
65+
}
66+
}
67+
68+
#[cfg(feature = "api-level-31")]
69+
impl Drop for KeyEventJava {
70+
/// Releases interface objects created by [`KeyEvent::from_java()`].
3971
///
4072
/// The underlying Java object remains valid and does not change its state.
4173
#[doc(alias = "AInputEvent_release")]
4274
fn drop(&mut self) {
43-
let ptr = match self.0 {
44-
InputEvent::MotionEvent(MotionEvent { ptr })
45-
| InputEvent::KeyEvent(KeyEvent { ptr }) => ptr.as_ptr().cast(),
46-
};
47-
unsafe { ffi::AInputEvent_release(ptr) }
75+
unsafe { ffi::AInputEvent_release(self.0.ptr.as_ptr().cast()) }
4876
}
4977
}
5078

@@ -428,11 +456,11 @@ impl MotionEvent {
428456
/// [`android.view.MotionEvent`]: https://developer.android.com/reference/android/view/MotionEvent
429457
#[cfg(feature = "api-level-31")]
430458
#[doc(alias = "AMotionEvent_fromJava")]
431-
pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option<InputEventJava> {
459+
pub unsafe fn from_java(env: *mut JNIEnv, key_event: jobject) -> Option<MotionEventJava> {
432460
let ptr = unsafe { ffi::AMotionEvent_fromJava(env, key_event) };
433-
Some(InputEventJava(InputEvent::MotionEvent(Self::from_ptr(
434-
NonNull::new(ptr.cast_mut())?,
435-
))))
461+
Some(MotionEventJava(Self::from_ptr(NonNull::new(
462+
ptr.cast_mut(),
463+
)?)))
436464
}
437465

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

14311457
/// Returns a pointer to the native [`ffi::AInputEvent`].

0 commit comments

Comments
 (0)