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

Add a way to easily convert an AndroidAppWaker into a Waker #171

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
Cargo.lock
22 changes: 22 additions & 0 deletions android-activity/src/game_activity/mod.rs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ use std::ptr;
use std::ptr::NonNull;
use std::sync::Weak;
use std::sync::{Arc, Mutex, RwLock};
use std::task::{RawWaker, RawWakerVTable, Waker};
use std::time::Duration;

use libc::c_void;
@@ -118,6 +119,27 @@ impl AndroidAppWaker {
ALooper_wake(self.looper.as_ptr());
}
}

/// Creates a [`Waker`] that wakes up the [`AndroidApp`].
///
/// This is useful for using this crate in `async` environments.
///
/// [`Waker`]: std::task::Waker
pub fn waker(self) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &VTABLE)
}

unsafe fn wake(data: *const ()) {
ndk_sys::ALooper_wake(data as *const _ as *mut _)
}

unsafe fn drop(_: *const ()) {}

unsafe { Waker::from_raw(RawWaker::new(self.looper.as_ptr() as *const (), &VTABLE)) }
}
}

impl AndroidApp {
22 changes: 22 additions & 0 deletions android-activity/src/native_activity/mod.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ use std::panic::AssertUnwindSafe;
use std::ptr;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, RwLock, Weak};
use std::task::{RawWaker, RawWakerVTable, Waker};
use std::time::Duration;

use libc::c_void;
@@ -83,6 +84,27 @@ impl AndroidAppWaker {
ndk_sys::ALooper_wake(self.looper.as_ptr());
}
}

/// Creates a [`Waker`] that wakes up the [`AndroidApp`].
///
/// This is useful for using this crate in `async` environments.
///
/// [`Waker`]: std::task::Waker
pub fn waker(self) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &VTABLE)
}

unsafe fn wake(data: *const ()) {
ndk_sys::ALooper_wake(data as *const _ as *mut _)
}

unsafe fn drop(_: *const ()) {}

unsafe { Waker::from_raw(RawWaker::new(self.looper.as_ptr() as *const (), &VTABLE)) }
}
}

impl AndroidApp {

Unchanged files with check annotations Beta

use ndk_sys::{AAssetManager, AConfiguration, ALooper, ALooper_callbackFunc, ANativeWindow, ARect};
#[cfg(all(
any(target_os = "android", feature = "test"),

Check failure on line 20 in android-activity/src/game_activity/ffi.rs

GitHub Actions / build (stable)

unexpected `cfg` condition value: `test`
any(target_arch = "arm", target_arch = "armv7")

Check failure on line 21 in android-activity/src/game_activity/ffi.rs

GitHub Actions / build (stable)

unexpected `cfg` condition value: `armv7`
))]
include!("ffi_arm.rs");
#[cfg(all(any(target_os = "android", feature = "test"), target_arch = "aarch64"))]

Check failure on line 25 in android-activity/src/game_activity/ffi.rs

GitHub Actions / build (stable)

unexpected `cfg` condition value: `test`
include!("ffi_aarch64.rs");
#[cfg(all(any(target_os = "android", feature = "test"), target_arch = "x86"))]

Check failure on line 28 in android-activity/src/game_activity/ffi.rs

GitHub Actions / build (stable)

unexpected `cfg` condition value: `test`
include!("ffi_i686.rs");
#[cfg(all(any(target_os = "android", feature = "test"), target_arch = "x86_64"))]

Check failure on line 31 in android-activity/src/game_activity/ffi.rs

GitHub Actions / build (stable)

unexpected `cfg` condition value: `test`
include!("ffi_x86_64.rs");
self.config.read().unwrap().screen_long()
}
#[cfg(feature = "api-level-30")]

Check failure on line 123 in android-activity/src/config.rs

GitHub Actions / build (stable)

unexpected `cfg` condition value: `api-level-30`
pub fn screen_round(&self) -> ScreenRound {
self.config.read().unwrap().screen_round()
}