From b183d6ac13b5a3e758d12d28d713c973b82e761c Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Wed, 13 Jan 2021 23:00:51 +0100 Subject: [PATCH 1/2] ndk,ndk-glue: Do not pass Looper ident as userdata pointer The value of `0` or `1` is not a valid pointer to a userdata structure. Not that it has to be, but with the nasty consequence that winit Android code was accidentally reinterpreting this pointer as integer to extract the identifier from, instead of using the appropriate `ident` field directly. After that is addressed in winit we can set the pointer back to 0 cleanly (and prepare for a potential pointer to some user data that may be passed in the future). Also add some constants to document these "special" identifiers. --- ndk-glue/CHANGELOG.md | 5 +++++ ndk-glue/src/lib.rs | 21 +++++++++++++++++++-- ndk/CHANGELOG.md | 7 +++++++ ndk/src/input_queue.rs | 6 +++--- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/ndk-glue/CHANGELOG.md b/ndk-glue/CHANGELOG.md index 91230363..e306a2cb 100644 --- a/ndk-glue/CHANGELOG.md +++ b/ndk-glue/CHANGELOG.md @@ -1,5 +1,10 @@ # Unreleased +- **Breaking** Looper `ident` not passed in `data` pointer anymore. + If you are relying on `Poll::Event::data` to tell event fd and + input queue apart, please use `Poll::Event::ident` and the new + constants introduced in `ndk-glue`! + # 0.2.1 (2020-10-15) - Fix documentation build on docs.rs diff --git a/ndk-glue/src/lib.rs b/ndk-glue/src/lib.rs index 58f070ac..886bd031 100644 --- a/ndk-glue/src/lib.rs +++ b/ndk-glue/src/lib.rs @@ -16,6 +16,18 @@ use std::thread; pub use ndk_macro::main; +/// `ndk-glue` macros register the reading end of an event pipe with the +/// main [`ThreadLooper`] under this `ident`. +/// When returned from [`ThreadLooper::poll_*`](ThreadLooper::poll_once) +/// an event can be retrieved from [`poll_events()`]. +pub const NDK_GLUE_LOOPER_EVENT_PIPE_IDENT: i32 = 0; + +/// The [`InputQueue`] received from Android is registered with the main +/// [`ThreadLooper`] under this `ident`. +/// When returned from [`ThreadLooper::poll_*`](ThreadLooper::poll_once) +/// an event can be retrieved from [`input_queue()`]. +pub const NDK_GLUE_LOOPER_INPUT_QUEUE_IDENT: i32 = 1; + pub fn android_log(level: Level, tag: &CStr, msg: &CStr) { let prio = match level { Level::Error => ndk_sys::android_LogPriority_ANDROID_LOG_ERROR, @@ -164,7 +176,12 @@ pub unsafe fn init( let looper = ThreadLooper::prepare(); let foreign = looper.into_foreign(); foreign - .add_fd(PIPE[0], 0, ALOOPER_EVENT_INPUT as _, 0 as _) + .add_fd( + PIPE[0], + NDK_GLUE_LOOPER_EVENT_PIPE_IDENT, + ALOOPER_EVENT_INPUT as _, + std::ptr::null_mut(), + ) .unwrap(); LOOPER = Some(foreign); main() @@ -253,7 +270,7 @@ unsafe extern "C" fn on_input_queue_created( ) { let input_queue = InputQueue::from_ptr(NonNull::new(queue).unwrap()); let looper = LOOPER.as_ref().unwrap(); - input_queue.attach_looper(looper, 1); + input_queue.attach_looper(looper, NDK_GLUE_LOOPER_INPUT_QUEUE_IDENT); *INPUT_QUEUE.write().unwrap() = Some(input_queue); wake(activity, Event::InputQueueCreated); } diff --git a/ndk/CHANGELOG.md b/ndk/CHANGELOG.md index 2bd586bb..4ce41d9a 100644 --- a/ndk/CHANGELOG.md +++ b/ndk/CHANGELOG.md @@ -1,5 +1,12 @@ # Unreleased +- **Breaking** Looper `ident` not passed in `data` pointer anymore. + `attach_looper` now only sets the `ident` field when attaching an + `InputQueue` to a `ForeignLooper`. + If you are relying on `Poll::Event::data` to tell event fd and + input queue apart, please use `Poll::Event::ident` and the new + constants introduced in `ndk-glue`! + # 0.2.1 (2020-10-15) - Fix documentation build on docs.rs diff --git a/ndk/src/input_queue.rs b/ndk/src/input_queue.rs index 05d02705..62d72d79 100644 --- a/ndk/src/input_queue.rs +++ b/ndk/src/input_queue.rs @@ -71,14 +71,14 @@ impl InputQueue { } } - pub fn attach_looper(&self, looper: &ForeignLooper, id: u32) { + pub fn attach_looper(&self, looper: &ForeignLooper, id: i32) { unsafe { ffi::AInputQueue_attachLooper( self.ptr.as_ptr(), looper.ptr().as_ptr(), - id as _, + id, None, - id as _, + std::ptr::null_mut(), ); } } From f9bb13ef0d6e00fa7c55bc4e818b0cf8056e178d Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sat, 30 Jan 2021 16:51:12 +0100 Subject: [PATCH 2/2] ndk,ndk-glue: Bump version to 0.3.0 --- ndk-glue/CHANGELOG.md | 2 ++ ndk-glue/Cargo.toml | 4 ++-- ndk/CHANGELOG.md | 2 ++ ndk/Cargo.toml | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ndk-glue/CHANGELOG.md b/ndk-glue/CHANGELOG.md index e306a2cb..70d219b0 100644 --- a/ndk-glue/CHANGELOG.md +++ b/ndk-glue/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +# 0.3.0 (2021-01-30) + - **Breaking** Looper `ident` not passed in `data` pointer anymore. If you are relying on `Poll::Event::data` to tell event fd and input queue apart, please use `Poll::Event::ident` and the new diff --git a/ndk-glue/Cargo.toml b/ndk-glue/Cargo.toml index 750cb9dc..334a2c3e 100644 --- a/ndk-glue/Cargo.toml +++ b/ndk-glue/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ndk-glue" -version = "0.2.1" +version = "0.3.0" authors = ["The Rust Windowing contributors"] edition = "2018" description = "Startup code for android binaries" @@ -12,7 +12,7 @@ homepage = "https://github.com/rust-windowing/android-ndk-rs" repository = "https://github.com/rust-windowing/android-ndk-rs" [dependencies] -ndk = { path = "../ndk", version = "0.2.1" } +ndk = { path = "../ndk", version = "0.3.0" } ndk-sys = { path = "../ndk-sys", version = "0.2.1" } ndk-macro = { path = "../ndk-macro", version = "0.2.0" } lazy_static = "1.4.0" diff --git a/ndk/CHANGELOG.md b/ndk/CHANGELOG.md index 4ce41d9a..24d372b8 100644 --- a/ndk/CHANGELOG.md +++ b/ndk/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +# 0.3.0 (2021-01-30) + - **Breaking** Looper `ident` not passed in `data` pointer anymore. `attach_looper` now only sets the `ident` field when attaching an `InputQueue` to a `ForeignLooper`. diff --git a/ndk/Cargo.toml b/ndk/Cargo.toml index 346529be..33cd751d 100644 --- a/ndk/Cargo.toml +++ b/ndk/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ndk" -version = "0.2.1" +version = "0.3.0" authors = ["The Rust Windowing contributors"] edition = "2018" description = "Safe Rust bindings to the Android NDK"