diff --git a/kubi/Cargo.toml b/kubi/Cargo.toml index 7e6d391..5afea81 100644 --- a/kubi/Cargo.toml +++ b/kubi/Cargo.toml @@ -51,6 +51,7 @@ winapi = { version = "0.3", features = ["wincon"] } default = ["raw-evt-mouse"] raw-evt-keyboard = [] # use raw input for keyboard. works on x11 and windows, breaks keyboard on android and wayland raw-evt-mouse = [] # required for mouse input +c-ffi = [] # generate a C-ffi-compatible `kubi_main` entry point (useful if building as a shared library) generate_visualizer_data = ["dep:serde_json", "shipyard/serde1"] safe_lz4 = ["lz4_flex/safe-encode", "lz4_flex/safe-decode"] parallel = ["shipyard/parallel"] # causes some serious issues! diff --git a/kubi/src/lib.rs b/kubi/src/lib.rs index d3e90ca..8783944 100644 --- a/kubi/src/lib.rs +++ b/kubi/src/lib.rs @@ -189,16 +189,30 @@ fn attach_console() { unsafe { AttachConsole(ATTACH_PARENT_PROCESS); } } -#[unsafe(no_mangle)] #[cfg(target_os = "android")] +#[unsafe(no_mangle)] pub fn android_main(app: android_activity::AndroidApp) { use android_activity::WindowManagerFlags; app.set_window_flags(WindowManagerFlags::FULLSCREEN, WindowManagerFlags::empty()); - kubi_main(app) + kubi_main_impl(app); } +#[cfg(feature = "c-ffi")] +#[cfg(not(target_os = "android"))] #[unsafe(no_mangle)] -pub fn kubi_main( +pub extern "C" fn kubi_main() { + // cant let unwinds cross the ffi boundary! + // also, hopefully this code should never panic either... + let panic = std::panic::catch_unwind(|| { + kubi_main_impl(); + }); + if panic.is_err() { + println!("!!! PANIC CAUGHT ON FFI BOUNDARY !!!"); + }; + std::mem::forget(panic); // forget the result, as dropping it will cause unwinding! +} + +pub fn kubi_main_impl( #[cfg(target_os = "android")] app: android_activity::AndroidApp ) { diff --git a/kubi/src/main.rs b/kubi/src/main.rs index 8cb1164..9954b64 100644 --- a/kubi/src/main.rs +++ b/kubi/src/main.rs @@ -1,8 +1,8 @@ #![cfg_attr( - all(windows, not(debug_assertions)), + all(windows, not(debug_assertions)), windows_subsystem = "windows" )] fn main() { - kubilib::kubi_main(); + kubilib::kubi_main_impl(); }