Skip to content
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

input: Replace open-coded types with ndk::event definitions #163

Open
wants to merge 2 commits into
base: main
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
44 changes: 27 additions & 17 deletions android-activity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- input: Replaced custom types with their `ndk` crate equivalent.
> [!NOTE]
> 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 result in lossless conversions, and not to mention use an ABI type that how its used by most functions (when the original constants were defined in a "typeless" way), the `ndk` types are used and reexported once again.

> [!IMPORTANT]
> **Relevant breaking changes**:
> - `repr()` types for some `enum`s have changed to match the type that returned and consumed by functions interacting with this wrapper type.
> - `Source::is_xxx_class()` functions are replaced by querying `Source::class()` and comparing against variants from the returned `SourceClass` `bitflags` enum.
> - `SourceFlags::TRACKBALL` (from `Source::is_trackball_class()`) is named `SourceClass::NAVIGATION` in the `ndk`.

## [0.6.0] - 2024-04-26

### Changed
Expand All @@ -25,32 +35,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- 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))

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

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

As far as could be seen though, it's not expected that anything was
depending on this (e.g. anything based on Winit enables the `ndk` feature
based on an equivalent `winit` feature).
As far as could be seen though, it's not expected that anything was
depending on this (e.g. anything based on Winit enables the `ndk` feature
based on an equivalent `winit` feature).

The benefit of the change is that it can help avoid a redundant
`raw-window-handle 0.6` dependency in projects that still need to use older
(non-default) `raw-window-handle` versions. (Though note that this may be
awkward to achieve in practice since other crates that depend on the `ndk`
are still likely to use default features and also pull in
`raw-window-handles 0.6`)
The benefit of the change is that it can help avoid a redundant
`raw-window-handle 0.6` dependency in projects that still need to use older
(non-default) `raw-window-handle` versions. (Though note that this may be
awkward to achieve in practice since other crates that depend on the `ndk`
are still likely to use default features and also pull in
`raw-window-handles 0.6`)

- 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))
- Improved IO error handling in `stdio-to-logcat` IO loop. ([#133](https://github.com/rust-mobile/android-activity/pull/133))

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

### Changed
- rust-version bumped to 0.68 ([#123](https://github.com/rust-mobile/android-activity/pull/123))
Expand Down
24 changes: 13 additions & 11 deletions android-activity/src/game_activity/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
// The `Class` was also bound differently to `android-ndk-rs` considering how the class is defined
// by masking bits from the `Source`.

use ndk::event::ButtonState;

use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent};
use crate::input::{
Axis, Button, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState,
MotionAction, MotionEventFlags, Pointer, PointersIter, Source, ToolType,
Axis, Button, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction,
MotionEventFlags, Pointer, PointersIter, Source, ToolType,
};

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

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

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

Expand All @@ -297,8 +300,7 @@ impl<'a> PointerImpl<'a> {
#[inline]
pub fn tool_type(&self) -> ToolType {
let pointer = &self.event.ga_event.pointers[self.index];
let tool_type = pointer.toolType as u32;
tool_type.into()
pointer.toolType.into()
}
}

Expand Down Expand Up @@ -665,7 +667,7 @@ impl<'a> KeyEvent<'a> {
///
#[inline]
pub fn source(&self) -> Source {
let source = self.ga_event.source as u32;
let source = self.ga_event.source;
source.into()
}

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

#[inline]
pub fn action_button(&self) -> KeyAction {
let action = self.ga_event.action as u32;
let action = self.ga_event.action;
action.into()
}

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

Expand Down
6 changes: 2 additions & 4 deletions android-activity/src/game_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,11 @@ impl AndroidAppInner {
}

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

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

pub fn create_waker(&self) -> AndroidAppWaker {
Expand Down
Loading
Loading