Repository navigation
Android: real apps in Rust, Back key, build.py options #62
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! Android: the entry point of a Rust app ([`android_main!`](crate::android_main)). | ||
| //! | ||
| //! The NativeActivity shell (platform/android/bui_android.c) calls `bui_app_main`; the macro | ||
| //! defines it, so an app's code has no `unsafe` and nothing Android-specific. Before the app runs: | ||
| //! | ||
| //! - **JNI**: the activity's Java VM and object are given to `ndk-context`, which crates that call | ||
| //! into Java read (cpal lists sound devices through it; without it they panic). | ||
| //! - **Logs**: stdout and stderr go to logcat (tag `bui`), panics included, so `println!` and | ||
| //! `eprintln!` are seen with `adb logcat -s bui`. | ||
| //! - **Folders**: the kit already pointed HOME, XDG_CACHE_HOME and TMPDIR at the app's own | ||
| //! folders (BK_android_attach), so `dirs::cache_dir()` and `std::env::temp_dir()` work. | ||
|
|
||
| use std::ffi::{CString, c_char, c_int, c_void}; | ||
| use std::io::{BufRead, BufReader}; | ||
| use std::os::fd::IntoRawFd; | ||
| use std::sync::Once; | ||
|
|
||
| use crate::sys; | ||
|
|
||
| /// The head of NDK's `ANativeActivity` (android/native_activity.h): the fields read here. | ||
| #[repr(C)] | ||
| struct ActivityHead { | ||
| callbacks: *mut c_void, | ||
| vm: *mut c_void, | ||
| env: *mut c_void, | ||
| clazz: *mut c_void, | ||
| } | ||
|
|
||
| /// logcat's priority of an info line (android/log.h `ANDROID_LOG_INFO`). | ||
| const LOG_INFO: c_int = 4; | ||
|
|
||
| unsafe extern "C" { | ||
| fn __android_log_write(prio: c_int, tag: *const c_char, text: *const c_char) -> c_int; | ||
| fn dup2(old: c_int, new: c_int) -> c_int; | ||
| } | ||
|
|
||
| /// Runs the app `main` inside the running activity: what `bui_app_main` does. Called by | ||
| /// [`android_main!`](crate::android_main); an app doesn't call it. | ||
| pub fn run(main: fn()) { | ||
| logs_to_logcat(); | ||
| // SAFETY: the shell attached the activity (BK_android_attach) before it called bui_app_main. | ||
| let activity = unsafe { sys::BK_android_activity() }.cast::<ActivityHead>(); | ||
| if activity.is_null() { | ||
| main(); | ||
| return; | ||
| } | ||
| // SAFETY: a live ANativeActivity: its VM and object stay valid until bui_app_main returns, and | ||
| // the context is released below, before they go (a relaunch sets it again). | ||
| unsafe { ndk_context::initialize_android_context((*activity).vm, (*activity).clazz) }; | ||
| main(); | ||
| // SAFETY: set above, on this thread. | ||
| unsafe { ndk_context::release_android_context() }; | ||
| } | ||
|
|
||
| /// Sends stdout and stderr to logcat, a line at a time, once per process. | ||
| fn logs_to_logcat() { | ||
| static ONCE: Once = Once::new(); | ||
| ONCE.call_once(|| { | ||
| let Ok((reader, writer)) = std::io::pipe() else { return }; | ||
| let fd = writer.into_raw_fd(); // kept open: it is stdout and stderr from now on | ||
| // SAFETY: plain descriptors; 1 and 2 now write into the pipe. | ||
| unsafe { | ||
| dup2(fd, 1); | ||
| dup2(fd, 2); | ||
| } | ||
| let _ = std::thread::Builder::new().name("bui-logcat".into()).spawn(move || { | ||
| let tag = c"bui"; | ||
| for line in BufReader::new(reader).lines().map_while(Result::ok) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the logcat reader running after non-UTF-8 output.
Read each record with Suggested fix
|
||
| if let Ok(text) = CString::new(line) { | ||
| // SAFETY: two C strings that live through the call. | ||
| unsafe { __android_log_write(LOG_INFO, tag.as_ptr(), text.as_ptr()) }; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -395,6 +395,8 @@ pub struct App { | |
| real_clock: bool, | ||
| globals: HashMap<TypeId, Box<dyn Any>>, | ||
| pub(crate) propagate: bool, | ||
| /// A key listener acted on the key ([`App::prevent_default`]). | ||
| default_prevented: bool, | ||
| /// Work sent from other threads ([`UiSender`]). | ||
| remote: Option<(std::sync::mpsc::Sender<RemoteJob>, std::sync::mpsc::Receiver<RemoteJob>)>, | ||
| /// Called when the user closes a window. | ||
|
|
@@ -452,7 +454,7 @@ impl App { | |
| fn create(headless: bool, real_clock: bool) -> App { | ||
| crate::motion::start_clock(real_clock); | ||
| App { windows: Vec::new(), entities: Vec::new(), headless, real_clock, globals: HashMap::new(), propagate: true, | ||
| remote: None, close_hooks: Vec::new(), closed_later: None, | ||
| default_prevented: false, remote: None, close_hooks: Vec::new(), closed_later: None, | ||
| activation_hooks: Vec::new(), notified: 0, shutdown_later: false } | ||
| } | ||
|
|
||
|
|
@@ -627,6 +629,13 @@ impl App { | |
| self.propagate = false; | ||
| } | ||
|
|
||
| /// In a key listener: the app acted on the key, so the system doesn't (as a browser's | ||
| /// `preventDefault`). On Android, Back is the key `Escape`: a listener that closes something on | ||
| /// it calls this, and only a Back nothing took finishes the app. | ||
| pub fn prevent_default(&mut self) { | ||
| self.default_prevented = true; | ||
| } | ||
|
|
||
| /// Activate the app (no-op: windows come to the front when shown). | ||
| pub fn activate(&mut self, _ignoring_other_apps: bool) {} | ||
|
|
||
|
|
@@ -2406,7 +2415,12 @@ impl Window { | |
| "pageup" => -1.0, | ||
| _ => 0.0, | ||
| }; | ||
| app.default_prevented = false; | ||
| self.emit(app, target, &KeyDownEvent { keystroke: ks }, |l| l.key_down.iter().collect()); | ||
| if std::mem::take(&mut app.default_prevented) && !self.ctx.is_null() { | ||
| // SAFETY: the live context; the kit is delivering this key (BK_key) now. | ||
| unsafe { sys::BK_key_taken(self.ctx) }; | ||
|
Comment on lines
+2420
to
+2422
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honor The key-down path calls Keep the prevention result in a local variable. Run the page scroll only when the key was not prevented. Unprevented PageDown and PageUp keep their scroll. Suggested fix
Selection defaults run before the listeners, so
|
||
| } | ||
| if page != 0.0 { | ||
| self.scroll_page(page); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Start the logcat reader before redirecting stdout and stderr.
dup2runs beforeBuilder::spawn. If spawn fails, the closure dropsreader, which closes the pipe read end. Descriptors 1 and 2 still point at the pipe write end, so later writes can fail withEPIPE. The defaultSIGPIPEdisposition can terminate the process.println!andeprintln!panic on write errors.ONCEruns this block once, so no later call repairs the state.Spawn the reader first. Redirect descriptors 1 and 2 only after spawn succeeds. If spawn fails, the original descriptors stay in place, and the pipe closes.
Suggested fix