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

ndk: Add tool_type getter for Pointer events #323

Merged
merged 4 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions ndk-sys/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Add `tool_type` getter for `Pointer` events. (#323)
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved

# 0.4.0 (2022-07-24)

- **Breaking:** Turn `enum` type aliases into newtype wrappers. (#245, #315)
Expand Down
22 changes: 22 additions & 0 deletions ndk/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,20 @@ pub enum Axis {
Generic16 = ffi::AMOTION_EVENT_AXIS_GENERIC_16,
}

/// The tool type of a pointer.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-48)
#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)]
#[repr(u32)]
pub enum ToolType {
Unknown = ffi::AMOTION_EVENT_TOOL_TYPE_UNKNOWN,
Finger = ffi::AMOTION_EVENT_TOOL_TYPE_FINGER,
Stylus = ffi::AMOTION_EVENT_TOOL_TYPE_STYLUS,
Mouse = ffi::AMOTION_EVENT_TOOL_TYPE_MOUSE,
Eraser = ffi::AMOTION_EVENT_TOOL_TYPE_ERASER,
Palm = ffi::AMOTION_EVENT_TOOL_TYPE_PALM,
}

/// A bitfield representing the state of buttons during a motion event.
///
/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-33)
Expand Down Expand Up @@ -656,6 +670,14 @@ impl<'a> Pointer<'a> {
pub fn touch_minor(&self) -> f32 {
unsafe { ffi::AMotionEvent_getTouchMinor(self.event.as_ptr(), self.index as ffi::size_t) }
}

#[inline]
pub fn tool_type(&self) -> ToolType {
let tool_type = unsafe {
ffi::AMotionEvent_getToolType(self.event.as_ptr(), self.index as ffi::size_t) as u32
};
tool_type.try_into().unwrap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that in some cases we use .try_into().unwrap_or(XXX::Unknown), but I'd have to ask @mb64 why that wasn't done in all cases.

Though I think I'd rather panic than consider every yet-unknown value to be equivalent to the literal _UNKNOWN constant, as this typically means the NDK or these enums need an update (which I'd like to mitigate even further by generating these types and derives in bindgen directly, but that hasn't materialized yet and especially the event code uses anonymous enums and typedefs which can't yet be interpreted that way). For example, Source is missing Hdmi and Sensor :)

}
}

/// An iterator over the pointers in a [`MotionEvent`].
Expand Down