Skip to content

Commit

Permalink
ndk: Add _ = !0 catch-all constant to all bitflags, allowing them…
Browse files Browse the repository at this point in the history
… to be extended

Android may return values in `bitflags` that are not yet mapped or
recognized in the `ndk` crate.  This makes functions like `all()` and
`!` behave unexpectedly when they truncate or limit their operation to
known bits exclusively.

A more likable solution is disabling functions that are susceptible to
this, as was done in the `ash` crate.
  • Loading branch information
MarijnS95 committed Jan 19, 2024
1 parent b9d28c8 commit 250f19b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ndk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ api-level-31 = ["api-level-30"]
test = ["ffi/test", "jni", "all"]

[dependencies]
bitflags = "2.2"
bitflags = "2.4" # At least 2.4.0 for `const _ = !0`
jni-sys = "0.3"
log = "0.4.6"
num_enum = "0.7"
Expand Down
18 changes: 11 additions & 7 deletions ndk/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,28 @@ pub enum Source {
impl Source {
pub fn class(self) -> SourceClass {
let class = u32::from(self) & ffi::AINPUT_SOURCE_CLASS_MASK;
SourceClass::from_bits_retain(class)
// The mask fits in a u8.
SourceClass::from_bits_retain(class as u8)
}
}

bitflags::bitflags! {
/// Flags representing the class of an [`InputEvent`] [`Source`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SourceClass : u32 {
pub struct SourceClass : u8 {
#[doc(alias = "AINPUT_SOURCE_CLASS_BUTTON")]
const BUTTON = ffi::AINPUT_SOURCE_CLASS_BUTTON;
const BUTTON = ffi::AINPUT_SOURCE_CLASS_BUTTON as u8;
#[doc(alias = "AINPUT_SOURCE_CLASS_POINTER")]
const POINTER = ffi::AINPUT_SOURCE_CLASS_POINTER;
const POINTER = ffi::AINPUT_SOURCE_CLASS_POINTER as u8;
#[doc(alias = "AINPUT_SOURCE_CLASS_NAVIGATION")]
const NAVIGATION = ffi::AINPUT_SOURCE_CLASS_NAVIGATION;
const NAVIGATION = ffi::AINPUT_SOURCE_CLASS_NAVIGATION as u8;
#[doc(alias = "AINPUT_SOURCE_CLASS_POSITION")]
const POSITION = ffi::AINPUT_SOURCE_CLASS_POSITION;
const POSITION = ffi::AINPUT_SOURCE_CLASS_POSITION as u8;
#[doc(alias = "AINPUT_SOURCE_CLASS_JOYSTICK")]
const JOYSTICK = ffi::AINPUT_SOURCE_CLASS_JOYSTICK;
const JOYSTICK = ffi::AINPUT_SOURCE_CLASS_JOYSTICK as u8;

// https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags
const _ = !ffi::AINPUT_SOURCE_CLASS_MASK as u8;
}
}

Expand Down
8 changes: 5 additions & 3 deletions ndk/src/looper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//!
//! [`ALooper`]: https://developer.android.com/ndk/reference/group/looper#alooper

use bitflags::bitflags;
use std::mem::ManuallyDrop;
use std::os::{
fd::{AsRawFd, BorrowedFd, RawFd},
Expand All @@ -31,12 +30,12 @@ pub struct ThreadLooper {
foreign: ForeignLooper,
}

bitflags! {
bitflags::bitflags! {
/// Flags for file descriptor events that a looper can monitor.
///
/// These flag bits can be combined to monitor multiple events at once.
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct FdEvent: u32 {
pub struct FdEvent : u32 {
/// The file descriptor is available for read operations.
#[doc(alias = "ALOOPER_EVENT_INPUT")]
const INPUT = ffi::ALOOPER_EVENT_INPUT;
Expand Down Expand Up @@ -65,6 +64,9 @@ bitflags! {
/// necessary to specify this event flag in the requested event set.
#[doc(alias = "ALOOPER_EVENT_INVALID")]
const INVALID = ffi::ALOOPER_EVENT_INVALID;

// https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags
const _ = !0;
}
}

Expand Down
8 changes: 5 additions & 3 deletions ndk/src/native_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,21 @@
//! [`ANativeActivity`]: https://developer.android.com/ndk/reference/group/native-activity#anativeactivity

use super::hardware_buffer_format::HardwareBufferFormat;
use bitflags::bitflags;
use std::{
ffi::{CStr, OsStr},
os::{raw::c_void, unix::prelude::OsStrExt},
path::Path,
ptr::NonNull,
};

bitflags! {
bitflags::bitflags! {
/// Window flags, as per the Java API at [`android.view.WindowManager.LayoutParams`].
///
/// <https://developer.android.com/ndk/reference/group/native-activity#group___native_activity_1ga2f1398dba5e4a5616b83437528bdb28e>
///
/// [`android.view.WindowManager.LayoutParams`]: https://developer.android.com/reference/android/view/WindowManager.LayoutParams
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct WindowFlags: u32 {
pub struct WindowFlags : u32 {
const ALLOW_LOCK_WHILE_SCREEN_ON = ffi::AWINDOW_FLAG_ALLOW_LOCK_WHILE_SCREEN_ON;
const DIM_BEHIND = ffi::AWINDOW_FLAG_DIM_BEHIND;
#[deprecated = "Deprecated. Blurring is no longer supported."]
Expand Down Expand Up @@ -48,6 +47,9 @@ bitflags! {
#[cfg_attr(feature = "api-level-26", deprecated = "This constant was deprecated in API level 26. Use `SHOW_WHEN_LOCKED` instead.")]
const DISMISS_KEYGUARD = ffi::AWINDOW_FLAG_DISMISS_KEYGUARD;
const ATTACHED_IN_DECOR = 0x40000000;

// https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags
const _ = !0;
}
}

Expand Down
3 changes: 3 additions & 0 deletions ndk/src/native_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ bitflags::bitflags! {
const TRANSFORM_ROTATE_180 = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_ROTATE_180.0;
#[doc(alias = "ANATIVEWINDOW_TRANSFORM_ROTATE_270")]
const TRANSFORM_ROTATE_270 = ffi::ANativeWindowTransform::ANATIVEWINDOW_TRANSFORM_ROTATE_270.0;

// https://docs.rs/bitflags/latest/bitflags/#externally-defined-flags
const _ = !0;
}
}

Expand Down

0 comments on commit 250f19b

Please sign in to comment.