Skip to content

Commit

Permalink
ndk,ndk-glue: Do not pass Looper ident as userdata pointer (#112)
Browse files Browse the repository at this point in the history
* 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,ndk-glue: Bump version to 0.3.0
  • Loading branch information
MarijnS95 authored Jan 30, 2021
1 parent 13c322e commit 30d032a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
7 changes: 7 additions & 0 deletions ndk-glue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 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
constants introduced in `ndk-glue`!

# 0.2.1 (2020-10-15)

- Fix documentation build on docs.rs
Expand Down
4 changes: 2 additions & 2 deletions ndk-glue/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
21 changes: 19 additions & 2 deletions ndk-glue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions ndk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# 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`.
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
Expand Down
2 changes: 1 addition & 1 deletion ndk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
6 changes: 3 additions & 3 deletions ndk/src/input_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}
}
Expand Down

0 comments on commit 30d032a

Please sign in to comment.