Skip to content

Commit 969ba5a

Browse files
committed
Improve forwards compatibility of input API
This adds a `#[doc(hidden)]` `__Unknown(u32)` variant to the various enums to keep them extensible without requiring API breaks. We need to consider that most enums that are based on Android SDK enums may be extended across different versions of Android (i.e. effectively at runtime) or extended in new versions of `android-activity` when we pull in the latest NDK/SDK constants. In particular in the case that there is some unknown variant we at least want to be able to preserve the integer value to allow the values to be either passed back into the SDK (it doesn't always matter whether we know the semantics of a variant at compile time) or passed on to something downstream that could be independently updated to know the semantics. We don't want it to be an API break to extend these enums in future releases of `android-activity`. It's not enough to rely on `#[non-exhaustive]` because that only really helps when adding new variants in sync with android-activity releases. On the other hand we also can't rely on a catch-all `Unknown(u32)` that only really helps with unknown variants seen at runtime. (If code were to have an exhaustive match that would include matching on `Unknown(_)` values then they wouldn't be compatible with new versions of android-activity that would promote unknown values to known ones). What we aim for instead is to have a hidden catch-all variant that is considered (practically) unmatchable so code is forced to have a `unknown => {}` catch-all pattern match that will cover unknown variants either in the form of Rust variants added in future versions or in the form of an `__Unknown(u32)` integer that represents an unknown variant seen at runtime. Any `unknown => {}` pattern match can rely on `IntoPrimitive` to convert the `unknown` variant to the integer that comes from the Android SDK in case that values needs to be passed on, even without knowing it's semantic meaning at compile time. Instead of adding an `__Unknown(u32)` variant to the `Class` enum though this enum has been removed in favour of adding methods like `is_button_class()` and `is_pointer_class()` to the `Source` type, since the class flags aren't guaranteed to be mutually exclusive and since they are an attribute of the `Source`. This removes some reliance `try_into().unwrap()` that was put in place anticipating that we would support `into()` via `num_enum`, once we could update our rust-version.
1 parent ce4413b commit 969ba5a

File tree

6 files changed

+244
-146
lines changed

6 files changed

+244
-146
lines changed

android-activity/src/game_activity/input.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
// The `Class` was also bound differently to `android-ndk-rs` considering how the class is defined
1414
// by masking bits from the `Source`.
1515

16-
use std::convert::TryInto;
17-
1816
use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent};
1917
use crate::input::{
20-
Axis, ButtonState, Class, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState,
21-
MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType,
18+
Axis, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction,
19+
MotionEventFlags, Pointer, PointersIter, Source, ToolType,
2220
};
2321

2422
// Note: try to keep this wrapper API compatible with the AInputEvent API if possible
@@ -50,14 +48,7 @@ impl<'a> MotionEvent<'a> {
5048
#[inline]
5149
pub fn source(&self) -> Source {
5250
let source = self.ga_event.source as u32;
53-
source.try_into().unwrap_or(Source::Unknown)
54-
}
55-
56-
/// Get the class of the event source.
57-
///
58-
#[inline]
59-
pub fn class(&self) -> Class {
60-
Class::from(self.source())
51+
source.into()
6152
}
6253

6354
/// Get the device id associated with the event.
@@ -73,7 +64,7 @@ impl<'a> MotionEvent<'a> {
7364
#[inline]
7465
pub fn action(&self) -> MotionAction {
7566
let action = self.ga_event.action as u32 & ndk_sys::AMOTION_EVENT_ACTION_MASK;
76-
action.try_into().unwrap()
67+
action.into()
7768
}
7869

7970
/// Returns the pointer index of an `Up` or `Down` event.
@@ -275,7 +266,8 @@ impl<'a> PointerImpl<'a> {
275266
#[inline]
276267
pub fn axis_value(&self, axis: Axis) -> f32 {
277268
let pointer = &self.event.ga_event.pointers[self.index];
278-
pointer.axisValues[axis as u32 as usize]
269+
let axis: u32 = axis.into();
270+
pointer.axisValues[axis as usize]
279271
}
280272

281273
#[inline]
@@ -294,7 +286,7 @@ impl<'a> PointerImpl<'a> {
294286
pub fn tool_type(&self) -> ToolType {
295287
let pointer = &self.event.ga_event.pointers[self.index];
296288
let tool_type = pointer.toolType as u32;
297-
tool_type.try_into().unwrap()
289+
tool_type.into()
298290
}
299291
}
300292

@@ -662,14 +654,7 @@ impl<'a> KeyEvent<'a> {
662654
#[inline]
663655
pub fn source(&self) -> Source {
664656
let source = self.ga_event.source as u32;
665-
source.try_into().unwrap_or(Source::Unknown)
666-
}
667-
668-
/// Get the class of the event source.
669-
///
670-
#[inline]
671-
pub fn class(&self) -> Class {
672-
Class::from(self.source())
657+
source.into()
673658
}
674659

675660
/// Get the device id associated with the event.
@@ -685,13 +670,13 @@ impl<'a> KeyEvent<'a> {
685670
#[inline]
686671
pub fn action(&self) -> KeyAction {
687672
let action = self.ga_event.action as u32;
688-
action.try_into().unwrap()
673+
action.into()
689674
}
690675

691676
#[inline]
692677
pub fn action_button(&self) -> KeyAction {
693678
let action = self.ga_event.action as u32;
694-
action.try_into().unwrap()
679+
action.into()
695680
}
696681

697682
/// Returns the last time the key was pressed. This is on the scale of
@@ -721,7 +706,7 @@ impl<'a> KeyEvent<'a> {
721706
#[inline]
722707
pub fn key_code(&self) -> Keycode {
723708
let keycode = self.ga_event.keyCode as u32;
724-
keycode.try_into().unwrap_or(Keycode::Unknown)
709+
keycode.into()
725710
}
726711

727712
/// Returns the number of repeats of a key.

android-activity/src/game_activity/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,10 +548,12 @@ impl AndroidAppInner {
548548
}
549549

550550
pub fn enable_motion_axis(&mut self, axis: Axis) {
551+
let axis: u32 = axis.into();
551552
unsafe { ffi::GameActivityPointerAxes_enableAxis(axis as i32) }
552553
}
553554

554555
pub fn disable_motion_axis(&mut self, axis: Axis) {
556+
let axis: u32 = axis.into();
555557
unsafe { ffi::GameActivityPointerAxes_disableAxis(axis as i32) }
556558
}
557559

0 commit comments

Comments
 (0)