Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

Android starts: the activity names the app's folders and logs to logcat

Jareer47959e3dfcd6
diff --git a/engine/Cargo.lock b/engine/Cargo.lock
index f868a507..7dec9247 100644
--- a/engine/Cargo.lock
+++ b/engine/Cargo.lock
@@ -266,6 +266,23 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04"
+[[package]]
+name = "android_log-sys"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "84521a3cf562bc62942e294181d9eef17eb38ceb8c68677bc49f144e4c3d4f8d"
+
+[[package]]
+name = "android_logger"
+version = "0.15.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dbb4e440d04be07da1f1bf44fb4495ebd58669372fe0cffa6e48595ac5bd88a3"
+dependencies = [
+ "android_log-sys",
+ "env_filter",
+ "log",
+]
+
[[package]]
name = "android_system_properties"
version = "0.1.6"
@@ -1138,7 +1155,10 @@ dependencies = [
name = "concat-android"
version = "0.2.1"
dependencies = [
+ "android_logger",
"concat",
+ "libc",
+ "log",
"slint",
]
@@ -1727,6 +1747,16 @@ dependencies = [
"syn 2.0.119",
]
+[[package]]
+name = "env_filter"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2"
+dependencies = [
+ "log",
+ "regex",
+]
+
[[package]]
name = "equator"
version = "0.4.2"
diff --git a/engine/crates/concat-android/Cargo.toml b/engine/crates/concat-android/Cargo.toml
index bc258ba8..52c6dbd2 100644
--- a/engine/crates/concat-android/Cargo.toml
+++ b/engine/crates/concat-android/Cargo.toml
@@ -21,6 +21,11 @@ slint = { version = "1.17", default-features = false, features = [
"backend-android-activity-06",
"compat-1-2",
] }
+# Words to logcat: the log facade, the logger that writes it there, and
+# libc for the pipe that carries stderr across.
+log = "0.4"
+android_logger = "0.15"
+libc = "0.2"
# `cargo apk build -p concat-android` packages the shared object, the
# sherpa-onnx runtime libraries and the manifest below into an APK. The
@@ -30,6 +35,9 @@ package = "app.concat.editor"
apk_name = "Concat"
build_targets = ["aarch64-linux-android"]
runtime_libs = "../../vendor/sherpa-onnx/jniLibs"
+# The launcher icon, at every density Android asks for; made from the
+# repository's logo by scripts/generate-icons.sh.
+resources = "res"
[package.metadata.android.sdk]
# Android 8.0: AAudio, the audio path the engine plays through.
@@ -42,6 +50,7 @@ name = "android.permission.INTERNET"
[package.metadata.android.application]
label = "Concat"
+icon = "@mipmap/ic_launcher"
# The editor keeps its own state across a rotation; the activity is not
# recreated for one.
[package.metadata.android.application.activity]
diff --git a/engine/crates/concat-android/res/mipmap-hdpi/ic_launcher.png b/engine/crates/concat-android/res/mipmap-hdpi/ic_launcher.png
new file mode 100644
index 00000000..71cec2fc
Binary files /dev/null and b/engine/crates/concat-android/res/mipmap-hdpi/ic_launcher.png differ
diff --git a/engine/crates/concat-android/res/mipmap-mdpi/ic_launcher.png b/engine/crates/concat-android/res/mipmap-mdpi/ic_launcher.png
new file mode 100644
index 00000000..8237ae53
Binary files /dev/null and b/engine/crates/concat-android/res/mipmap-mdpi/ic_launcher.png differ
diff --git a/engine/crates/concat-android/res/mipmap-xhdpi/ic_launcher.png b/engine/crates/concat-android/res/mipmap-xhdpi/ic_launcher.png
new file mode 100644
index 00000000..a46bd634
Binary files /dev/null and b/engine/crates/concat-android/res/mipmap-xhdpi/ic_launcher.png differ
diff --git a/engine/crates/concat-android/res/mipmap-xxhdpi/ic_launcher.png b/engine/crates/concat-android/res/mipmap-xxhdpi/ic_launcher.png
new file mode 100644
index 00000000..0100099b
Binary files /dev/null and b/engine/crates/concat-android/res/mipmap-xxhdpi/ic_launcher.png differ
diff --git a/engine/crates/concat-android/res/mipmap-xxxhdpi/ic_launcher.png b/engine/crates/concat-android/res/mipmap-xxxhdpi/ic_launcher.png
new file mode 100644
index 00000000..9d265192
Binary files /dev/null and b/engine/crates/concat-android/res/mipmap-xxxhdpi/ic_launcher.png differ
diff --git a/engine/crates/concat-android/src/lib.rs b/engine/crates/concat-android/src/lib.rs
index 67f62b9e..60d7d699 100644
--- a/engine/crates/concat-android/src/lib.rs
+++ b/engine/crates/concat-android/src/lib.rs
@@ -5,16 +5,96 @@
//! its own thread once the activity is up; Slint's backend takes the
//! activity from there, and the window runs exactly as it does anywhere
//! else.
+//!
+//! Two things are settled here before the window starts, because only the
+//! activity knows them. The app's directories: an Android process has no
+//! home directory, so the host's XDG bases are pointed at the app's own
+//! files folder, and its external files folder stands in for the home the
+//! project and export defaults hang off. And where words go: a process on
+//! a phone has no terminal, so the log facade and everything the window
+//! prints to stderr are forwarded to logcat under the `concat` tag, which
+//! is where `adb logcat -s concat` reads a report from.
+
+#[cfg(target_os = "android")]
+mod activity {
+ use std::io::BufRead;
+
+ /// Names the app's directories for the host. Set once, before any other
+ /// thread exists, which is what makes writing the environment sound.
+ pub fn name_directories(app: &slint::android::AndroidApp) {
+ let Some(internal) = app.internal_data_path() else {
+ return;
+ };
+ // What the phone keeps for the app: settings, recents, models.
+ // SAFETY: called from android_main before the window or any worker
+ // thread starts, so no other thread reads the environment.
+ unsafe {
+ std::env::set_var("XDG_CONFIG_HOME", &internal);
+ std::env::set_var("XDG_DATA_HOME", &internal);
+ // Projects and exports land under the folder the user can reach
+ // through the phone's file manager, falling back to the private
+ // one when there is no external storage.
+ let home = app.external_data_path().unwrap_or(internal);
+ std::env::set_var("HOME", home);
+ }
+ }
+
+ /// Routes the log facade, panics and stderr to logcat.
+ pub fn open_logcat() {
+ android_logger::init_once(
+ android_logger::Config::default()
+ .with_max_level(log::LevelFilter::Info)
+ .with_tag("concat"),
+ );
+ std::panic::set_hook(Box::new(|info| {
+ log::error!("panic: {info}");
+ }));
+ forward_stderr();
+ }
+
+ /// Everything written to stderr is read back off a pipe and logged a
+ /// line at a time, so `eprintln!` reaches logcat without every call
+ /// site knowing about phones.
+ fn forward_stderr() {
+ use std::os::fd::FromRawFd;
+ let mut ends = [0i32; 2];
+ // SAFETY: plain libc calls on descriptors this function owns; the
+ // read end is handed to exactly one File.
+ let reader = unsafe {
+ if libc::pipe(ends.as_mut_ptr()) != 0 {
+ return;
+ }
+ if libc::dup2(ends[1], libc::STDERR_FILENO) < 0 {
+ libc::close(ends[0]);
+ libc::close(ends[1]);
+ return;
+ }
+ libc::close(ends[1]);
+ std::fs::File::from_raw_fd(ends[0])
+ };
+ std::thread::Builder::new()
+ .name("stderr-to-logcat".into())
+ .spawn(move || {
+ for line in std::io::BufReader::new(reader).lines().map_while(Result::ok) {
+ log::warn!("{line}");
+ }
+ })
+ .ok();
+ }
+}
/// Called by the activity's native glue; the name is the contract.
#[cfg(target_os = "android")]
#[unsafe(no_mangle)]
fn android_main(app: slint::android::AndroidApp) {
+ activity::open_logcat();
+ activity::name_directories(&app);
+ log::info!("Concat {} starting", env!("CARGO_PKG_VERSION"));
if let Err(error) = slint::android::init(app) {
- eprintln!("concat: could not start the Android backend: {error}");
+ log::error!("could not start the Android backend: {error}");
return;
}
if let Err(error) = concat::run() {
- eprintln!("concat: {error}");
+ log::error!("{error}");
}
}
diff --git a/engine/crates/concat-host/src/dirs.rs b/engine/crates/concat-host/src/dirs.rs
index 9f497f65..b5ff6228 100644
--- a/engine/crates/concat-host/src/dirs.rs
+++ b/engine/crates/concat-host/src/dirs.rs
@@ -45,15 +45,22 @@ impl AppDirs {
data: dir,
})
} else {
- let home = home()?;
- let config_base = std::env::var_os("XDG_CONFIG_HOME")
- .map(PathBuf::from)
- .filter(|path| path.is_absolute())
- .unwrap_or_else(|| home.join(".config"));
- let data_base = std::env::var_os("XDG_DATA_HOME")
- .map(PathBuf::from)
- .filter(|path| path.is_absolute())
- .unwrap_or_else(|| home.join(".local").join("share"));
+ // Linux follows the XDG split. Android arrives here too: the
+ // activity names both bases before the window starts, since an
+ // app process there has no home directory to derive them from.
+ let base = |variable: &str| {
+ std::env::var_os(variable)
+ .map(PathBuf::from)
+ .filter(|path| path.is_absolute())
+ };
+ let config_base = match base("XDG_CONFIG_HOME") {
+ Some(path) => path,
+ None => home()?.join(".config"),
+ };
+ let data_base = match base("XDG_DATA_HOME") {
+ Some(path) => path,
+ None => home()?.join(".local").join("share"),
+ };
Ok(AppDirs {
config: config_base.join(IDENTIFIER),
data: data_base.join(IDENTIFIER),
diff --git a/engine/crates/concat/src/studio.rs b/engine/crates/concat/src/studio.rs
index d67aed25..186d2f49 100644
--- a/engine/crates/concat/src/studio.rs
+++ b/engine/crates/concat/src/studio.rs
@@ -217,9 +217,7 @@ impl Default for ExportState {
Self {
open: false,
name: "Untitled".into(),
- folder: std::env::var("HOME")
- .map(|home| format!("{home}/Movies"))
- .unwrap_or_default(),
+ folder: home_folder("Movies"),
resolution: 2,
rate: 1,
quality: 1,
@@ -232,6 +230,14 @@ impl Default for ExportState {
}
}
+/// `name` under the home directory, as a path string; empty when there is
+/// no home to speak of, and the form then asks for a folder outright.
+fn home_folder(name: &str) -> String {
+ std::env::var("HOME")
+ .map(|home| format!("{home}/{name}"))
+ .unwrap_or_default()
+}
+
/// The settings sheet's state.
#[derive(Default)]
pub struct SettingsState {
@@ -279,9 +285,13 @@ impl Default for StartState {
fn default() -> Self {
Self {
name: "Untitled project".into(),
- location: std::env::var("HOME")
- .map(|home| format!("{home}/Desktop/Concat"))
- .unwrap_or_default(),
+ // A phone has no desk: its projects live at the top of the
+ // folder the file manager shows for the app.
+ location: home_folder(if cfg!(target_os = "android") {
+ "Concat"
+ } else {
+ "Desktop/Concat"
+ }),
resolution: 0,
rate: 3,
busy: false,