Skip to content

Commit 51d05d4

Browse files
authored
input: Replace open-coded types with ndk::event definitions (#163)
1 parent fe171bc commit 51d05d4

File tree

7 files changed

+70
-918
lines changed

7 files changed

+70
-918
lines changed

android-activity/CHANGELOG.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
- input: Replaced custom types with their `ndk` crate equivalent.
10+
> [!NOTE]
11+
> These types existed because the `ndk` crate didn't provide them in an extensible way. Now that they have the `#[non_exhaustive]` flag and contain a `__Unknown(T)` variant to provide lossless conversions, and not to mention use an ABI type that matches how it is being used by most functions (when the original constants were defined in a "typeless" way), the `ndk` types are used and reexported once again.
12+
13+
> [!IMPORTANT]
14+
> **Relevant breaking changes**:
15+
> - `repr()` types for some `enum`s have changed to match the ABI type that is used by most functions that are returning or consuming this wrapper type.
16+
> - `Source::is_xxx_class()` functions are replaced by querying `Source::class()` and comparing against variants from the returned `SourceClass` `bitflags` enum.
17+
> - `SourceFlags::TRACKBALL` (from `Source::is_trackball_class()`) is named `SourceClass::NAVIGATION` in the `ndk`.
18+
919
## [0.6.0] - 2024-04-26
1020

1121
### Changed
@@ -25,32 +35,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2535
### Changed
2636
- Avoids depending on default features for `ndk` crate to avoid pulling in any `raw-window-handle` dependencies ([#142](https://github.com/rust-mobile/android-activity/pull/142))
2737

28-
**Note:** Technically, this could be observed as a breaking change in case you
29-
were depending on the `rwh_06` feature that was enabled by default in the
30-
`ndk` crate. This could be observed via the `NativeWindow` type (exposed via
31-
`AndroidApp::native_window()`) no longer implementing `rwh_06::HasWindowHandle`.
38+
**Note:** Technically, this could be observed as a breaking change in case you
39+
were depending on the `rwh_06` feature that was enabled by default in the
40+
`ndk` crate. This could be observed via the `NativeWindow` type (exposed via
41+
`AndroidApp::native_window()`) no longer implementing `rwh_06::HasWindowHandle`.
3242

33-
In the unlikely case that you were depending on the `ndk`'s `rwh_06` API
34-
being enabled by default via `android-activity`'s `ndk` dependency, your crate
35-
should explicitly enable the `rwh_06` feature for the `ndk` crate.
43+
In the unlikely case that you were depending on the `ndk`'s `rwh_06` API
44+
being enabled by default via `android-activity`'s `ndk` dependency, your crate
45+
should explicitly enable the `rwh_06` feature for the `ndk` crate.
3646

37-
As far as could be seen though, it's not expected that anything was
38-
depending on this (e.g. anything based on Winit enables the `ndk` feature
39-
based on an equivalent `winit` feature).
47+
As far as could be seen though, it's not expected that anything was
48+
depending on this (e.g. anything based on Winit enables the `ndk` feature
49+
based on an equivalent `winit` feature).
4050

41-
The benefit of the change is that it can help avoid a redundant
42-
`raw-window-handle 0.6` dependency in projects that still need to use older
43-
(non-default) `raw-window-handle` versions. (Though note that this may be
44-
awkward to achieve in practice since other crates that depend on the `ndk`
45-
are still likely to use default features and also pull in
46-
`raw-window-handles 0.6`)
51+
The benefit of the change is that it can help avoid a redundant
52+
`raw-window-handle 0.6` dependency in projects that still need to use older
53+
(non-default) `raw-window-handle` versions. (Though note that this may be
54+
awkward to achieve in practice since other crates that depend on the `ndk`
55+
are still likely to use default features and also pull in
56+
`raw-window-handles 0.6`)
4757

4858
- The IO thread now gets named `stdio-to-logcat` and main thread is named `android_main` ([#145](https://github.com/rust-mobile/android-activity/pull/145))
4959
- Improved IO error handling in `stdio-to-logcat` IO loop. ([#133](https://github.com/rust-mobile/android-activity/pull/133))
5060

5161
## [0.5.0] - 2023-10-16
5262
### Added
53-
- Added `MotionEvent::action_button()` exposing the button associated with button press/release actions ()
63+
- Added `MotionEvent::action_button()` exposing the button associated with button press/release actions ([#138](https://github.com/rust-mobile/android-activity/pull/138))
5464

5565
### Changed
5666
- rust-version bumped to 0.68 ([#123](https://github.com/rust-mobile/android-activity/pull/123))

android-activity/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ default = []
2828
game-activity = []
2929
native-activity = []
3030
api-level-30 = ["ndk/api-level-30"]
31+
api-level-33 = ["api-level-30", "ndk/api-level-33"]
3132

3233
[dependencies]
3334
log = "0.4"

android-activity/src/game_activity/input.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
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 ndk::event::ButtonState;
17+
1618
use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent};
1719
use crate::input::{
18-
Axis, Button, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState,
19-
MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType,
20+
Axis, Button, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction,
21+
MotionEventFlags, Pointer, PointersIter, Source, ToolType,
2022
};
2123

2224
// Note: try to keep this wrapper API compatible with the AInputEvent API if possible
@@ -47,7 +49,7 @@ impl<'a> MotionEvent<'a> {
4749
///
4850
#[inline]
4951
pub fn source(&self) -> Source {
50-
let source = self.ga_event.source as u32;
52+
let source = self.ga_event.source;
5153
source.into()
5254
}
5355

@@ -63,7 +65,7 @@ impl<'a> MotionEvent<'a> {
6365
/// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionMasked())
6466
#[inline]
6567
pub fn action(&self) -> MotionAction {
66-
let action = self.ga_event.action as u32 & ndk_sys::AMOTION_EVENT_ACTION_MASK;
68+
let action = self.ga_event.action & ndk_sys::AMOTION_EVENT_ACTION_MASK as i32;
6769
action.into()
6870
}
6971

@@ -176,6 +178,7 @@ impl<'a> MotionEvent<'a> {
176178
/// See [the NDK
177179
/// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getbuttonstate)
178180
#[inline]
181+
// TODO: Button enum to signify only one bitflag can be set?
179182
pub fn button_state(&self) -> ButtonState {
180183
ButtonState(self.ga_event.buttonState as u32)
181184
}
@@ -278,7 +281,7 @@ impl PointerImpl<'_> {
278281
#[inline]
279282
pub fn axis_value(&self, axis: Axis) -> f32 {
280283
let pointer = &self.event.ga_event.pointers[self.index];
281-
let axis: u32 = axis.into();
284+
let axis: i32 = axis.into();
282285
pointer.axisValues[axis as usize]
283286
}
284287

@@ -297,8 +300,7 @@ impl PointerImpl<'_> {
297300
#[inline]
298301
pub fn tool_type(&self) -> ToolType {
299302
let pointer = &self.event.ga_event.pointers[self.index];
300-
let tool_type = pointer.toolType as u32;
301-
tool_type.into()
303+
pointer.toolType.into()
302304
}
303305
}
304306

@@ -665,7 +667,7 @@ impl<'a> KeyEvent<'a> {
665667
///
666668
#[inline]
667669
pub fn source(&self) -> Source {
668-
let source = self.ga_event.source as u32;
670+
let source = self.ga_event.source;
669671
source.into()
670672
}
671673

@@ -681,13 +683,13 @@ impl<'a> KeyEvent<'a> {
681683
/// See [the KeyEvent docs](https://developer.android.com/reference/android/view/KeyEvent#getAction())
682684
#[inline]
683685
pub fn action(&self) -> KeyAction {
684-
let action = self.ga_event.action as u32;
686+
let action = self.ga_event.action;
685687
action.into()
686688
}
687689

688690
#[inline]
689691
pub fn action_button(&self) -> KeyAction {
690-
let action = self.ga_event.action as u32;
692+
let action = self.ga_event.action;
691693
action.into()
692694
}
693695

@@ -717,7 +719,7 @@ impl<'a> KeyEvent<'a> {
717719
/// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getkeycode)
718720
#[inline]
719721
pub fn key_code(&self) -> Keycode {
720-
let keycode = self.ga_event.keyCode as u32;
722+
let keycode = self.ga_event.keyCode;
721723
keycode.into()
722724
}
723725

android-activity/src/game_activity/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,11 @@ impl AndroidAppInner {
544544
}
545545

546546
pub fn enable_motion_axis(&mut self, axis: Axis) {
547-
let axis: u32 = axis.into();
548-
unsafe { ffi::GameActivityPointerAxes_enableAxis(axis as i32) }
547+
unsafe { ffi::GameActivityPointerAxes_enableAxis(axis.into()) }
549548
}
550549

551550
pub fn disable_motion_axis(&mut self, axis: Axis) {
552-
let axis: u32 = axis.into();
553-
unsafe { ffi::GameActivityPointerAxes_disableAxis(axis as i32) }
551+
unsafe { ffi::GameActivityPointerAxes_disableAxis(axis.into()) }
554552
}
555553

556554
pub fn create_waker(&self) -> AndroidAppWaker {

0 commit comments

Comments
 (0)