From 8d30454c8db394006ff7b9305fdd55e5700a9abb Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Mon, 12 Sep 2022 17:14:49 +0100 Subject: [PATCH] native-activity: InputEvent forward compatibility When building with native-activity then we no longer directly use the `ndk::InputEvent` type and instead have our own extensible, `#[non_exhaustive]` enum that ensures we will be able to add an event for Ime state changes without necessarily needing a semver bump. Addresses: #18 --- android-activity/src/native_activity/mod.rs | 27 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/android-activity/src/native_activity/mod.rs b/android-activity/src/native_activity/mod.rs index 326e2b1..af2cbf2 100644 --- a/android-activity/src/native_activity/mod.rs +++ b/android-activity/src/native_activity/mod.rs @@ -27,9 +27,19 @@ mod ffi; pub mod input { pub use ndk::event::{ - Axis, ButtonState, EdgeFlags, InputEvent, KeyAction, KeyEvent, KeyEventFlags, Keycode, - MetaState, MotionAction, MotionEvent, MotionEventFlags, Pointer, Source, + Axis, ButtonState, EdgeFlags, KeyAction, KeyEvent, KeyEventFlags, Keycode, MetaState, + MotionAction, MotionEvent, MotionEventFlags, Pointer, Source, }; + + // We use our own wrapper type for input events to have better consistency + // with GameActivity and ensure the enum can be extended without needing a + // semver bump + #[derive(Debug)] + #[non_exhaustive] + pub enum InputEvent { + MotionEvent(self::MotionEvent), + KeyEvent(self::KeyEvent), + } } // The only time it's safe to update the android_app->savedState pointer is @@ -429,16 +439,25 @@ impl AndroidAppInner { // ref: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/jni/android_view_InputQueue.cpp // while let Ok(Some(event)) = queue.get_event() { - if let Some(event) = queue.pre_dispatch(event) { + if let Some(ndk_event) = queue.pre_dispatch(event) { + let event = match ndk_event { + ndk::event::InputEvent::MotionEvent(e) => input::InputEvent::MotionEvent(e), + ndk::event::InputEvent::KeyEvent(e) => input::InputEvent::KeyEvent(e), + }; callback(&event); + let ndk_event = match event { + input::InputEvent::MotionEvent(e) => ndk::event::InputEvent::MotionEvent(e), + input::InputEvent::KeyEvent(e) => ndk::event::InputEvent::KeyEvent(e), + }; + // Always report events as 'handled'. This means we won't get // so called 'fallback' events generated (such as converting trackball // events into emulated keypad events), but we could conceivably // implement similar emulation somewhere else in the stack if // necessary, and this will be more consistent with the GameActivity // input handling that doesn't do any kind of emulation. - queue.finish_event(event, true); + queue.finish_event(ndk_event, true); } } }