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

The event::Source::Any variant is a bitmask from the C API which probably shouldn't be exposed like this in the Rust API #482

Open
rib opened this issue Aug 6, 2024 · 2 comments

Comments

@rib
Copy link
Contributor

rib commented Aug 6, 2024

Since the Source is currently bound as a enum, I wouldn't expect it to contain bitmask values from the C API.

Maybe if it were instead exposed via bitflags::bitflags! {} it could be good to expose this, but I think sticking with an enum, with mutually exclusive source variants, seems appropriate here.

For the case where the source is unknown it could potentially be useful to have an .any() method (or similar) that would be able to internally use this bitmask against the unknown source value.

@MarijnS95
Copy link
Member

MarijnS95 commented Aug 6, 2024

It looks like this AINPUT_SOURCE_ANY has the inverse value of AINPUT_SOURCE_CLASS_MASK. Perhaps we could turn it into a getter of sorts? For now Source appears to only contain unique variants, besides being built up from a bitmask of a class and a per-class-unique incrementing integer. I don't think bitflags! would fit there.
If this mask is not useful for anything, we ought just remove it.

But there are a few other enums that I'm tracking in my notes, that I'd like to convert to bitflags!, specifically ButtonState, MetaState, EdgeFlags, KeyEventFlags, anything that's a newtype wrapper with bool mask-getters: wdyt?

@MarijnS95
Copy link
Member

MarijnS95 commented Aug 11, 2024

Not entirely sure what to do with this one or what it's expected to be used for, right now I just hacked it into:

diff --git a/ndk/src/event.rs b/ndk/src/event.rs
index ef447da..fa8406c 100644
--- a/ndk/src/event.rs
+++ b/ndk/src/event.rs
@@ -69,7 +69,6 @@ pub enum Source {
     Hdmi = ffi::AINPUT_SOURCE_HDMI as i32,
     Sensor = ffi::AINPUT_SOURCE_SENSOR as i32,
     RotaryEncoder = ffi::AINPUT_SOURCE_ROTARY_ENCODER as i32,
-    Any = ffi::AINPUT_SOURCE_ANY as i32,

     #[doc(hidden)]
     #[num_enum(catch_all)]
@@ -77,6 +76,10 @@ pub enum Source {
 }

 impl Source {
+    /// Bitmask of [`Source`] excluding [`SourceClass`] bits.
+    // pub const ANY: i32 = ffi::AINPUT_SOURCE_ANY as i32;
+    pub const ANY: Self = Self::__Unknown(ffi::AINPUT_SOURCE_ANY as i32);
+
     pub fn class(self) -> SourceClass {
         let class = i32::from(self) & ffi::AINPUT_SOURCE_CLASS_MASK as i32;
         // The mask fits in a u8.

We could also do or add:

/// TODO: Document what to use this for?
#[doc(alias = "AINPUT_SOURCE_ANY")]
pub fn any(self) -> i32 {
    i32::from(self) & ffi::AINPUT_SOURCE_ANY as i32
}

But again I'm not sure what to document here. If it's an unknown value, get at least the (hopefully known) class out of it, and then keep these bits as describing an unknown-to-the-NDK-crate source for the known class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants